/*
**	CBA, 12/05/05; this file contains those Jscript calls used by pages... this is the master file located on root for this 
**  app... when you reference this file in your pages, ex: <script>.... src=""...</script>
**  make sure you specify CORRECT relative path.
**  CBA, 02/25/06; Fine tune calendar presentation while I am working on TO 14, Item 199; set width=height
**  and allow user to resize. Okay, now set left = 50, top = 100, from 200/250; so calendar is closer to
**  left edge...HEHEHE... okay... now apply some of these changes to my new window in EditProjectDetailsNew.aspx
**  client script call(will xfer script code here when we roll to production).
**
*/
var popUp; 
var iIndex;
var newWindow; // CBA, 03/16/06; added
var popupsBlocked;

function OpenCalendar(idname, postBack)
{	
	popUp = window.open('Calendar.aspx?formname=' + document.forms[0].name + 
		'&id=' + idname + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack, 
		'popupcal', 
		'width=270,height=270,left=50,top=100,resizable=yes');
}
function OpenCalendar2(idname, postBack)
{	
	popUp = window.open('/QBWEB_NEW/Calendar.aspx?formname=' + document.forms[0].name + 
		'&id=' + idname + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack, 
		'popupcal', 
		'width=270,height=270,left=50,top=100,resizable=yes');
}

function SetDate(formName, id, newDate, postBack)
{
	eval('var theform = document.' + formName + ';');
	popUp.close();
	theform.elements[id].value = newDate;
	if (postBack)
		__doPostBack(id,'');
}

function MoveSelected(lst1, lst2)
{
//alert(lst2)
//alert(document.forms[0].elements[lst2].length)
  var i;
  for (i=0; i<document.forms[0].elements[lst1].length; i++)
  {
   if (document.forms[0].elements[lst1].item(i).selected)
    {
  //  alert(document.forms[0].elements[lst1].item(i));
		document.forms[0].elements[lst2].items.Add(document.forms[0].elements[lst1].item(i));
          
    }
  }
}

function AddSelected(lst1, lst2, hiddenTextBox)
{

 
var i;
var valueSelected = null;
  for (i=0; i<lst1.length; i++)
  {
   if (lst1.item(i).selected)
    {    
	 AddItem(lst2, lst1.item(i).value, lst1.item(i).value);
	 
	 for (x = 0; x < (lst2.length);  x++)
			{
				 
				if(valueSelected == null)
				{
					
					valueSelected = lst2.item(x).value;
				}
				else
				{
					
					valueSelected = valueSelected + "," + lst2.item(x).value;
				}
				
			}
			
			hiddenTextBox.value =  valueSelected; 
    }
  }
}

function RemoveSelected(lst1, hiddenTextBox)
{
var i;
var valueSelected = null;
  for (i=0; i<lst1.length; i++)
  {
   if (lst1.item(i).selected)
    {    
	 RemoveItem(lst1, lst1.item(i).value);
	 
	 for (x = 0; x < (lst1.length);  x++)
			{
				 
				if(valueSelected == "")
				{
					
					valueSelected = lst1.item(x).value;
				}
				else
				{
					
					valueSelected = valueSelected + "," + lst1.item(x).value;
				}
				
			}
			
			hiddenTextBox.value =  valueSelected; 
    }
  }
}

function AddItem(lbxSelection,sDisplay,sKey)
{
RemoveItem(lbxSelection,sKey)
iIndex=lbxSelection.options.length;
lbxSelection.options[iIndex]=new Option(sDisplay,iIndex+1);
lbxSelection.options[iIndex].value=sKey;			 
				 
}

function RemoveItem(lbxSelection,sKey)
{
	var srcLen = lbxSelection.options.length;
	for (var x=srcLen-1; x>=0; x--)
   	if (lbxSelection.options[x].value == sKey) 
    	{
		lbxSelection.options[x]=null;
	}
}


function RemoveAll(lbxSelection, hiddenTextBox)
{
	var srcLen = lbxSelection.options.length;
	for (var x=srcLen-1; x>=0; x--)   	 
		lbxSelection.options[x]=null;
}

/*
**	CBA, 12/05/05; set up setFocus with correct code; used for followup to TO13, Item 141
**  objectID is passed in from the calling event in the .aspx page, NOT FROM CODEBEHIND FILE!
**  search in EditProjectDetailsNew.aspx for more info.
*/
function setFocus(objectID)
{
	var destControl = document.getElementById(objectID);
	if (destControl != null) {
		destControl.focus();
	}
}

/*
**	CBA, 12/07/05; FINALLY; figured way to trap key event and if tab key, goes to 
**	objectID passed in...this code was cabbaged from one of MANY Web page postings
**	See EditProjectDetailsNew.aspx for other side; 
*/
function setFocusOnTabKey( e, objectID ) {
	// next check is if we are Netscape, or other(IE)... if other, e.keyCode assigned to key
	var key = (navigator.appName == "Netscape") ? e.which : e.keyCode;
	switch (key)
	{
		case 9: // tab key pressed...
		setFocus(objectID);
		break;
	}
}

/*
** 	CBA, 02/28/06; per TO 14, Item 175; dynamically change the cursor
**	for cases where mouseonhover, and showing 'wrong' cursor...
**	current cursor styles - ref Web Development -> HTML and CSS -> etc...
**	Most common options:
**	'crosshair'
**	'hand'
**	'help'
**	'pointer'
**	'progress'
**	'text'
**	and so on... see MSDN reference.
*/
function setCursor(objectID, sCursor) {

	var OBJECT = document.getElementById(objectID);
	if (OBJECT != null) {
		OBJECT.style.cursor = sCursor;
	}
	// hmmmm... best to leave else out for now, should not occur(yea, right!), but I dont need users
	// raising hoolabaloola either.
//	else
//		alert("Cursor Object is NULL");

}

/*
**	CBA, 03/16/06; per TO14, Item 199; opening a second window (or popup)
**	from a link... code is combination of my hackwork and solid script
**	(with good explanations) at www.quirksmode.org/js/croswin.html#topopup
**  JEEZ... heavy mods done related to popupblocker and focus... always case
**  of something simple costing mucho time.
*/

function openNewWindow(sURL, sName, sFeatures) {
	if (newWindow!=null)
	{
		alert('You are only allowed one active print window. Please close your current print window and press the "GO" button.');
		// newWindow.location.href = url;
	}
	else
	{
		var tempWindow = window.open(sURL, sName, sFeatures );
		//if (!IsNull(tempWindow))... this doesnt work, says needs object
		// huh... STILL running into issue of tempwindow is an [object] 
		// EVEN THOUGH Yahoo popup blocker is blocking it!!!
		//	okay, found a solution; see try/catch block further down... 
		//	so I THINK I have all bases covered?
		// googlebar, MSIE XP SP2 blockers function as should be (see jguru.com forum, where I got final fix)
		// yahoo bar popup blocker is nonstandard in this case.  Wasted 2 days on 'simple' issues...
		
		if((typeof(tempWindow)=="undefined") || (tempWindow==null)) {
			popupsBlocked = true;
			resetReportCookie();
			alert('Your popupblocker is turned on.  Please add this website to your allowed list, or press the <CTRL> button while pressing the "GO" button.');
			return false; //leave function
		}
		else {
			popupsBlocked = false;
			newWindow = tempWindow; // assign global var to window
		}
	}

	// add a check if newWindow still there, then set focus....
	if (!popupsBlocked) {
		try{
			newWindow.focus();
			}
		catch(e) {
			// error occurs because yahoo popup blocker is NOT returning same as MSIE or GoogleBar so add this general try/catch
			popupsBlocked = true;
			resetReportCookie();
			alert('Your popup blocker is turned on.  Please add this website to your allowed list, or press the <CTRL> button while pressing the "GO" button.');
			return false; //leave function
		}
	}
	return false;
}


/******************************  Manage Cookies Section  ********************************************/
/*
**	CBA,03/22/06;  this section added for Cookies solution related to issues when opening
**	a second window(popup) and timing issues.  This is for client side, equivalent code will be in 
**	new class, ManageCookies.cs?  End up doing code in EditProjectDetailsNew.aspx to check for cookie
**	This code was cabbaged from several web sites: www.webreference.com, www.the-cool-place.co.uk, arachna 
**	web site, Walther's book and other places used for reference.
*/

function listCookies() {
	//alert('What a RAW cookie looks like: ' + document.cookie );
    var theCookies = document.cookie.split(';');
    var aString = '';
    for (var i = 1 ; i <= theCookies.length; i++) {
        aString += i + ' ' + theCookies[i-1] + "\n";
    }
    return aString;
}

function setCookie(name, value, expires, path, domain, secure) {
	//alert('In setCookie. parms: ' + name + ' ' + expires + ' ' + path + ' ' + domain + ' ' + secure + ' ' );
    var curCookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    document.cookie = curCookie;
    return curCookie;
}

function getCookie(name) {
	// technique 1, build an array and search for name; this has its own advantages
	// vs Shiran's code; both are good.
//    var theCookies = document.cookie.split(/[; ]+/);
//    for (var i = 0 ; i < theCookies.length; i++) {
//        var aName = theCookies[i].substring(0,theCookies[i].indexOf('='));
//        if (aName == name) {
//            return theCookies[i];
//        }
//    }
    // return null as default 
//    return null;

	// Use alternate code version (ref Y. Shiran code); searches whole string.
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);  // takes into account if it is first in string
		if (begin != 0) return null; // not found
	} else
		begin += 2; // places pointer at first letter of 'name'
	var end = document.cookie.indexOf(";", begin);
	if( end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name) {
    var aCookie = getCookie(name);
    if (aCookie != null) {
	//	alert('In deleteCookie; before deleting cookie; name: ' + name );
        document.cookie = aCookie + '; expires=Thu, 01-Jan-70 00:00:01 GMT';  // a cookie is deleted by changing date to previous date.
        return name;
    }
    return null; // add default
}

function getCookieValue(name) {
    var theCookies = document.cookie.split(/[; ]+/); // uses regex, search on '; ' more than one occurence
    for (var i = 0 ; i < theCookies.length; i++) {
        var aName = theCookies[i].substring(0,theCookies[i].indexOf('='));
        if (aName == name) {
			var value = unescape(theCookies[i].substring( (theCookies[i].indexOf('=') + 1),(theCookies[i].length) ));
			return value;
        }
    }
}

function setCookieValue(name, value) {
	var aCookie = getCookie(name);
	if (aCookie != null) {
		setCookie(name,value,null,null,null,null); // will this work? Yep... it does update current cookie value.
	}
}

// CBA, 03/24/06; add cleanup function for case where popupblocker blocked window, need to reset cookie
function resetReportCookie(){
	var CurrentSessionId = getCookieValue('ASP.NET_SessionId');
	var CookieValue = CurrentSessionId + ' 0';
	setCookieValue( 'QBWEBQuickReportingWindow',CookieValue);
}

function wesWasHere(x){
	var z = x;
	z.enabled = false;
}
function enableJobDropDown(objJobCB, objItemCB){
	var x = objJobCB.checked;
	alert(x);
}
/******************************  End Manage Cookies Section  ****************************************/


/******************************  String Management Section  *****************************************/
/*
**	CBA, 03/24/06; This section contains (or will) string manipulation
**	functions using provided javascript internal funcs...
**	1) the string function I wrote in PB to parse a string
**	2) the string funcs I wrote in C
*/

/******************************  End String Management Section  *************************************/