//'***************************************************************************
//' FILENAME:       		$Workfile: globalFns.js $ 
//' DESCRIPTION: 			DC-PRO Admin - Global javascript functions
//' CREATION DATE:  		15 July 2005
//' INITIAL AUTHOR:  		MMcDermott
//' LAST MODIFIED DATE:	$Date: 15/Jul/05 12:00a $
//' LAST MODIFIED BY: 	$Author: MMcDermott $
//'
//'$History: globalFns.js $
//'
//'Functions:
//'		OpenWindow
//'		openCalendar
//'		changeStatus
//'		stripQuerystringFromUrl
//'		trim
//'		isanumber
//'		onKeyPress_IntField
//'		
//'		
//'		
//'***************************************************************************

/* ******************************************************************
   * This function calls the calendar
   *	in_bSingleField - FALSE if separate fields are used for day, month, year.
   *	in_bReverseDate - true if year/month/day to be returned iso day/month/year
   ****************************************************************** */
function OpenCalendar(FrmName, in_FieldName, in_bSingleField, in_bReverseDate)
{
	//alert("OpenCalendar('" + FrmName + "','" + in_FieldName + "', in_bSingleField='" + in_bSingleField + "', in_bReverseDate='" + in_bReverseDate + "')");
	var theDate, theday, themonth, theyear;
	if (!in_bSingleField)
	{
		theDate = "";
		theday=eval('document.' + FrmName + '.' + in_FieldName + '[0].value');
		themonth=eval('document.' + FrmName + '.' + in_FieldName + '[1].value');
		theyear=eval('document.' + FrmName + '.' + in_FieldName + '[2].value');
		if ((theday + themonth + theyear) != "")
		{
			theDate = theday + "-" + WhichMonthName(parseInt(themonth)) + "-" + theyear;
		}	
	}
	else
	{
		theDate = eval('document.' + FrmName + '.' + in_FieldName + '.value');
	}
	var sQSDefaultDate = "";
	if(theDate != "")
	{
		sQSDefaultDate="&date=" + theDate;
	}
	
	msgWindow=window.open('calendar.asp?FrmOpener=' + FrmName + sQSDefaultDate + '&Field=' + in_FieldName + "&SingleField=" + in_bSingleField + "&ReverseDate=" + in_bReverseDate,'Explaining','toolbar=no,directories=no,menubar=no,scrollbars=0,resizable=no,width=180,height=260,top=200,left=380');
	//msgWindow=window.open('calendar.asp?FrmOpener=' + FrmName + sQSDefaultDate + '&Field=' + in_FieldName + "&SingleField=" + in_bSingleField + "&ReverseDate=" + in_bReverseDate,'Explaining','toolbar=no,directories=no,menubar=no,scrollbars=0,resizable=no,width=300,height=400,top=200,left=380');
}


/* ******************************************************************
   * This function verifies the date input to make sure the three   *
   * txt fields (day, month and year) have been filled with values  *
   * included in their different allowed ranges:                    *
   * 		day (txtDay) = 1 .. 31				    *
   * 		month (txtMonth) = 1 .. 12				    *
   * 		year  (txtYear) = 1000 .. 9999			    *
   *
   * It makes sure the user doesn't input a 31st day for those months
   * that don't have it:February,April,June,September and November
   * 
   * It also detects wheter the 29th of February is a valid day for
   * the chosen year.		
   ****************************************************************** */
function validatedate(myobj,FrmName,in_field,inputtype){
	var theday="";
	var themonth="";
	var theyear="";

	if(isNaN(myobj.value)){
		alert("Please enter a numeric value");
		myobj.focus();
		myobj.value="";
	}else{
		if ((myobj.value>0)&&(myobj.value<10)&&(myobj.value.substring(0,1)!=0)&&(in_field!="YEAR")){
		myobj.value="0" + myobj.value;
		}

		if ((in_field=="DAY")&&(myobj.value!="")){      //day

		   if ((myobj.value<=0)||(myobj.value>31)){    
			alert("Invalid day");
			myobj.value="";
			myobj.focus();
		   }
		}
		else if ((in_field=="MONTH")&&(myobj.value!="")){     //month
		
			var MonthNames=new Array('January','February','March','April','May','June','July','August','September','October','November','December');

		
		   if ((myobj.value<=0)||(myobj.value>12)){
				alert("Invalid month");
				myobj.value="";
				myobj.focus();
		   }else{
				theday=eval('document.' + FrmName + '.' + inputtype + '[0].value');	//day
				if((theday==31)&&((myobj.value==2)||(myobj.value==4)||(myobj.value==6)||(myobj.value==9)||(myobj.value==11))){
					alert("Invalid date: There aren't 31 days in " + MonthNames[parseInt(myobj.value) - 1]);
					myobj.value=""
					myobj.focus();
				}else if((theday==30)&&(myobj.value==2)){
					alert("Invalid date: February doesn't have a 30th day");
				}
		   }
		}
		else if ((in_field=="YEAR")&&(myobj.value!="")){    //year
		   if ((myobj.value<1000)||(myobj.value>9999)){
				alert("Invalid year");
				myobj.value="";
				myobj.focus();
		   }else{
				theday=parseInt(eval('document.' + FrmName + '.' + inputtype + '[0].value'));	//day
				themonth=parseInt(eval('document.' + FrmName + '.' + inputtype + '[1].value'));	//month
				if((themonth==2)&&(theday==29)){
					var checkLeapYear=parseInt(myobj.value) + 2000;
					if(((checkLeapYear%4==0)&&(checkLeapYear%100!=0))||(checkLeapYear%400==0)){
						//it's ok		
					}else{
						alert('Invalid date: February only has 28 days in the year ' + myobj.value);
						myobj.value="";
						myobj.focus();
					}
					
				}
		   }
		}
	}	
}
// end validatedate****************************************************** 

// This function opens a window
function OpenNamedWindow(thePage,in_sName,theWidth,theHeight,theTop,theLeft,theScroll)
{
	//alert("OpenNamedWindow(" + thePage + ", ...)");
	if(theScroll=="")theScroll=0;
	msgWindow=window.open(thePage,in_sName,'toolbar=no,directories=no,menubar=no,status=0,scrollbars=' + theScroll + ',resizable=no,width=' + theWidth + ',height=' + theHeight + ',top=' + theTop + ',left=' + theLeft);
}

// This function opens a window
function OpenWindow(thePage,theWidth,theHeight,theTop,theLeft,theScroll)
{
	//alert("OpenWindow(" + thePage + ", ...)");
	if(theScroll=="")theScroll=0;
	msgWindow=window.open(thePage,'none','toolbar=no,directories=no,menubar=no,status=0,scrollbars=' + theScroll + ',resizable=no,width=' + theWidth + ',height=' + theHeight + ',top=' + theTop + ',left=' + theLeft);
}
	
// Set message in Status Bar
function changeStatus(text) {
	//includes time delay for IE bug
	window.setTimeout('window.status="' + text + '"', 1);
	return true;
}

// Strip Querystring from Url
function stripQuerystringFromUrl(in_sUrl) {
	var sOutput = in_sUrl.toString();
	var nPosQuest = sOutput.indexOf("?");
	if (nPosQuest > -1) sOutput = sOutput.substring(0, nPosQuest);
	return sOutput;
}

// trim function
function trim(in_sString) {
	while (in_sString.substring(0,1) == ' ') {
		in_sString = in_sString.substring(1, in_sString.length);
	}
	while (in_sString.substring(in_sString.length-1, in_sString.length) == ' ') {
		in_sString = in_sString.substring(0,in_sString.length-1);
	}
	return in_sString;
}

// ------------------------------------------------------------------------
//	Function : isanumber
//	This function verifies the contents of obj as numeric, 
//	checks type, range of values.
//   
// ------------------------------------------------------------------------
function onBlur_IsNumber(obj,bMand,sType,minval,maxval) {
	bValid = true;
	// Non-Mandatory and empty
	if ((bMand != true) && (obj.value==""))	
		return;
	// Check Numeric
	if(isNaN(obj.value)) {
		bValid=false;
		alert("You must specify a number.");
		obj.focus();
		obj.value="";
	}
	// Check number type
	if (bValid == true)	{
		if(sType=="INTEGER") {		
			if(obj.value!=parseInt(obj.value)) {
				bValid=false;
				alert("You must specify an integer value.");
				obj.value="";
				obj.focus();
			}
		}
	}
	// Check min/max values
	if (bValid == true)	{
		if((obj.value<minval)||(obj.value>maxval)) {
 			alert("You must specify a value in the Range: " + minval + ".." + maxval);
			obj.value="";
			obj.focus();
		}
	}
}


// ------------------------------------------------------------------------
//	Function : onKeyPress_IntField
//	Permits only numeric chars to be entered when used with input field.
//  Allows + - symbols, onBlur should be used to convert to valid integer.
// ------------------------------------------------------------------------
function onKeyPress_IntField(obj, in_bAllowNeg)
{
	var bValid = true;
	// Permitted -> plus, minus(if in_bAllowNeg), 0-9
	if ( ((in_bAllowNeg) && (window.event.keyCode==45)) || (window.event.keyCode==43) || ((window.event.keyCode>=48) && (window.event.keyCode<=57)) ) {
		// valid
	}
	else {
		//alert("Character is not allowed.");
		bValid = false;	// not valid character.
		window.event.keyCode=null;	// ignore the keypress
	}
}



var type = "IE";	//Variable used to hold the browser name
BrowserSniffer();

//detects the capabilities of the browser
function BrowserSniffer() {
	if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) type="OP";		//Opera
	else if (document.all) type="IE";														//Internet Explorer e.g. IE4 upwards
	else if (document.layers) type="NN";													//Netscape Communicator 4
	else if (!document.all && document.getElementById) type="MO";							//Mozila e.g. Netscape 6 upwards
	else type = "IE";		//I assume it will not get here
}

function ShowLayer(id, action)
{
	if (type=="IE") eval("document.all." + id + ".style.visibility='" + action + "'");
	if (type=="NN") eval("document." + id + ".visibility='" + action + "'");
	if (type=="MO" || type=="OP") eval("document.getElementById('" + id + "').style.visibility='" + action + "'");
}

