/* VSS Infomation  $Header: /LAD/V10.0.0/ASP/LADRoot/Scripts/JavaScript.js 6     6/08/04 15:46 Ezbsuk $ */
/* File_Name = "JavaScript.js
'--------------------------------------------------------------------'
' See JavaScript.js.doc for spcification
'-----------------------------------------------------------------------------------
*/
// global variables
var gDate = new Date();
var gDateFormat = "DD/MM/YYYY";
var gDateDelimiter = "/";

function showHelp(anchor) {
	var helpWin = window.open(jsAppRoot + "help/help.asp#" + anchor,"helpWin", "height=450,width=610,toolbar=no,menubar=no,location=no,left=30,screenX=30,top=30,screenY=30,directories=no,resizable=yes,scrollbars=yes")
	helpWin.focus();
}

function isInteger(pStr){
	return (pStr.replace(/[0-8]/g,"9").replace(/[^9]/g,"X").indexOf("X") == -1);
}

function isDate(pDate, pFormat, pDelimit)
{
	var okDate = true;
	var daysInMonths = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	var fullMonths = new Array("JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER");
	var abrvMonths = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");

	var day, month, year, expDays, adjustment, nowyear, sYear;

	gDate.setTime(0);

	day = month = year = adjustment= -1;
	// use default format and delimiter if not supplied
	dArr = (pDate.toUpperCase()).split(((arguments.length <3)?gDateDelimiter:pDelimiter));
	fArr = (((arguments.length <2)?gDateFormat:pFormat).toUpperCase()).split(((arguments.length <3)?gDateDelimiter:pDelimiter));

	if	(dArr.length != fArr.length)
		// format mask and date string contain different numbers of elements
		okDate = false;
	else
		// go through our arrays processing date elements against format mask
		for(i=0; i < fArr.length; i++) {
			if (fArr[i]=='DD')
					if (!isInteger(dArr[i])||dArr[i]>31||dArr[i]<1)
						okDate = false;
					else
						day = parseInt(dArr[i],10);
			else
				if (fArr[i]=='MM')
					if (!isInteger(dArr[i])||dArr[i]>12||dArr[i]<1)
						okDate = false;
					else
						month = parseInt(dArr[i],10);
				else
					if (fArr[i]=='YYYY') {
						if ((!dArr[i])||((dArr[i].length) != 4)||(!isInteger(dArr[i])))
							okDate = false;
						else
							year = parseInt(dArr[i],10);
					}
					else
						if (fArr[i]=='YY') {
							if ((!dArr[2])||((dArr[2].length) != 2)||(!isInteger(dArr[i])))
								okDate = false;
							else {
								sYear = parseInt(dArr[i],10);
								nowyear = getFullYear(new Date());
								year = Math.floor(nowyear/100)*100; // the base century
								// adjust the year to include a century component
								year += (((nowyear%100) < 50) ? ((sYear <= (nowyear%100)+50) ? sYear : sYear-100 ) : ((sYear > (nowyear%100)-50) ? sYear : sYear+100)); /* the years after the base century */
							}
						}
						else
							if (fArr[i]=='MON')
								for (j=0; j<abrvMonths.length; j++) {
									okDate = false;
									if (abrvMonths[j]==dArr[i]) {
										month = j+1;
										okDate = true;
										break;
									}
								}
							else
								if (fArr[i]=='MONTH') {
									okDate = false;
									for (j=0; j<fullMonths.length; j++) {
										if (fullMonths[j]==dArr[i]) {
											month = j+1;
											okDate = true;
											break;
										}
									}
								}
								else okDate = false;
			if (!okDate)
				break;
		} //for(i=0; i < fArr.length; i++)

	// Check number of days in month - can only check it if we have a day and month
	if (day!=-1&&month!=-1) {
		// get the max days for the month from daysInMonths array
		expDays = daysInMonths[month-1];

		// for Feb only make leap year adjustment (can only check if we have a year)
		if (month==2&&year!=-1) {
			// note that these adjustments may be cumulative
			if (year%4==0) adjustment++;
			if (year%100==0) adjustment--;
			if (year%400==0) adjustment++;
			expDays += adjustment;
		}
		// compare expected max number of days against that entered
		if (expDays<day) okDate = false;
	}

	if (okDate) {
		if(year!=-1) gDate.setYear(year);
		if(month!=-1) gDate.setMonth(month-1);
		if(day!=-1) gDate.setDate(day);
		gDate.setHours(0,0,0,0);
	}
	return okDate;
}

function isFrmDate(pCtl, pMsg){
	if (!isDate(pCtl.value))
		{ if(pMsg.length) alert(pMsg); pCtl.focus(); return false;}
	return true;
}

function ShowPopup(pPage) {
	var newURL = jsAppRoot + pPage;
	var newWin = window.open(newURL,"popup","resizable,scrollbars,width=200,height=200,screenX=0,screenY=0,left=0,top=0");
	newWin.focus();
}

function reOrderAVSResults(pOrderBy) {	
	document.frmCriteria.orderByAVS.value = (arguments.length > 0 ? pOrderBy : "");		
	document.frmCriteria.submit();
}

function reOrderFrmFndResults(pOrderBy) {		
	document.frmCriteria.orderByFFCmn.value = (arguments.length > 0 ? pOrderBy : "");	
	document.frmCriteria.submit();
}

function reOrderCmnResults(pOrderBy) {	   
	document.frmCriteria.orderByCmn.value = (arguments.length > 0 ? pOrderBy : "");	
	document.frmCriteria.submit();
}

function reOrderResults(pOrderBy) {		
	document.frmCriteria.orderBy.value = (arguments.length > 0 ? pOrderBy : "");	
	
	if (document.frmCriteria.pageNo!=null)	
		document.frmCriteria.pageNo.value = 1;	
	
	document.frmCriteria.submit();
}

// strips white space from begining and end of string
function trim(pString) {
	return (pString.replace(/^(\s*)([^\s]*)/g, "$2")).replace(/([^\s]*)(\s*)$/g, "$1");
}

// validates quick search criteria
function valQSCriteria(pFrm) {
	var criteria = trim(pFrm.quickSearchCriteria.value.toUpperCase());

	if(criteria == "") {
		alert("Quick Search: Keyword(s) must be entered.");
		pFrm.quickSearchCriteria.value = trim(pFrm.quickSearchCriteria.value);
		return false;
	}
	return true;
}




// pURL: page to submit the form to search
function submitCriteria(pURL) {
	var URLargs;
	var critForm = document.frmCriteria;
	// use split to strip off existing page info
	URLargs = critForm.action.indexOf("?") == -1 ? "" : critForm.action.slice(critForm.action.indexOf("?"));
	critForm.action = pURL + URLargs;	
	critForm.submit();
}


