//--- Constants ---

//--- Procedures ---

//Submit/Cancel/Validation.
//Show/Hide.
//Dates.
//Numeric.

//Submit/Cancel/Validation.

function gSubmit(sPageMode) {
	frmForm.hidPageMode.value = sPageMode;
	frmForm.submit();
}

function gCancel() {
	//if (confirm('Are you sure you want to cancel?')) {window.location.assign(window.location.href);}
	return confirm('Are you sure you want to cancel?');
}

function gIsDirty(v) {
	eval('frmForm.hidIsDirty' + v).value = 1;
}

function gCheckDirty(v) {
	var hid = eval('frmForm.hidIsDirty' + v);
	if (eval('frmForm.txt' + v).value=='') {hid.value=''} else {hid.value=1}
}

function gIsPopulated(sControl, sHeading) {
	if (eval('frmForm.' + sControl).value == '') {alert('Please enter something for ' + sHeading); return false;} else {return true;}
}

function gIsSelected(sControl, sHeading) {
	if (eval('frmForm.' + sControl).value < 1) {alert('Please select a ' + sHeading); return false;} else {return true;}
}

function gIsDate(sControl, sHeading) {
	if (!gDateValidate(eval('frmForm.' + sControl).value, 'dd/mm/yyyy')) {alert('Please use dd/mm/yyyy format for ' + sHeading); return false;} else {return true;}
}

function gIsNumeric(sControl, sHeading, bInteger, bMandatory) {
	var s = 'Please enter a numeric value for ' + sHeading;
	if (!bMandatory) {s = s + ' or leave it empty'}
	if (!gNumericValidate(eval('frmForm.' + sControl).value, bInteger, bMandatory)) {alert(s); return false;} else {return true;}
}

//Show/Hide.

function gShow(sControl, bShow) {
	gShowMe('frmForm.' + sControl, bShow);
}

function gShowElement(sElement, bShow) {
	gShowMe(sElement, bShow);
}

function gShowMe(s, bShow) {
	//This function should only be called by gShow() or gShowElement().
	var v = eval(s);
	if (bShow) {v.style.display = ''} else {v.style.display = 'none'}
}

//Combos.

function gComboPopulate(combo, array)
{	
	combo.options.length = 0;

	for (var i = 0; i < array.length; i++) {
		combo.options[i] = new Option(array[i][1], array[i][0]);
	}
}

function gComboPopulateConditional(combo, array, iConditionalID)
{	
	var j = 0;
	
	combo.options.length = 0;
	
	for (var i = 0; i < array.length; i++) {
		if (array[i][0] == iConditionalID) {
			combo.options[j] = new Option(array[i][2], array[i][1]);
			j = j + 1;
		}
	}
}

function gComboPopulateChild(combo, array, iParentComboValue, sTopOption)
{
	combo.options.length = 0;		
	
	for (var i = 0; i < array.length; i++) {
		if (array[i][0] == iParentComboValue) {
			if (sTopOption != '') {combo.options[0] = new Option(sTopOption, -1);}
			
			for (var j = 0; j < array[i][2].length; j++) {
				if (sTopOption != '') {
					combo.options[j+1] = new Option(array[i][2][j][1], array[i][2][j][0]);
				}
				else {
					combo.options[j] = new Option(array[i][2][j][1], array[i][2][j][0]);
				}
			}
			break;
		}
	}
}

function gComboSelectValue(combo, value)
{	
	for (var i = 0; i < combo.options.length; i++) {
		if (combo.options[i].value == value) {
			combo.selectedIndex = i;
			break;
		}
	}
}

function gComboCopy(cboSource, cboDestination)
{
	cboDestination.options.length = 0;			
	
	for (var i = 0; i < cboSource.length; i++) {
		cboDestination.options[i] = new Option(cboSource.options[i].text, cboSource.options[i].value);
	}
}

//Dates.

function gDateValidate(sDate, sFormat)
{	//Dates may be separated by /.		NOT . or - or ' '.
	//Supports dd/mm/yy, dd/mm/yyyy and dd/mmm/yy.

	var sProcedure = 'gDateValidate()';
	
	try {
		//var sSeparators = " -./";
		var sSeparators = "/";
		var s;
		var iDay;
		var iMonth;
		var iYear;

		//Disallow anything that is not the right length.
		if (sDate.length != sFormat.length) {return false;}
				
		//Ensure there are no invalid characters.
		for (var i = 0; i < sDate.length; i++) {
			s = sDate.substring(i, i + 1);
			
			switch (sFormat) {
				case 'dd/mm/yy':
				case 'dd/mm/yyyy':
					if (i == 2 || i == 5) {
						if (sSeparators.indexOf(s) == -1) {return false;}
					}
					else
					{
						if (s < "0" || s > "9") {return false;}
					}
					break;
				case 'dd/mmm/yy':
					if (i == 2 || i == 6) {
						if (sSeparators.indexOf(s) == -1) {return false;}
					}
					else if (i < 2 || i > 6) {
						if (s < "0" || s > "9") {return false;}
					}	
					else if (i > 2 && i < 6) {
						if (!((s >= "A" && s <= "Z") || (s >= "a" && s <= "z"))) {return false;}
					}
					break;
				default: 
					alert('Sorry, the ' + sFormat + ' date format is not supported by the function ' + sProcedure);
					return false;
			}
		}	

		//Get the three component values.
		//Multiplying by 1 converts, for example, '08' to 8. Don't know why iMonth = parseInt(sDate.substring(3, 5)) doesn't work.
		iDay = sDate.substring(0, 2) * 1;
		switch (sFormat) {
			case 'dd/mm/yy':
				iMonth = sDate.substring(3, 5) * 1;
				iYear = sDate.substring(6, 8) * 1;
				break;
			case 'dd/mm/yyyy':
				iMonth = sDate.substring(3, 5) * 1;
				iYear = sDate.substring(6, 10) * 1;
				break;
			case 'dd/mmm/yy':
				iMonth = sDate.substring(3, 6);
				iMonth = iMonth.toLowerCase();
				iYear = sDate.substring(7, 9) * 1;
				break;
		}

		//Test the year.
		switch (sFormat) {
			case 'dd/mm/yyyy':
				if (iYear < 1900 || iYear > 2099) return false;
				break;
			case 'dd/mm/yy':
			case 'dd/mmm/yy':
				if (iYear < 0 || iYear > 99) return false;
				break;
		}

		//Test the month.
		switch (sFormat) {
			case 'dd/mm/yy':
			case 'dd/mm/yyyy':
				if (iMonth < 1 || iMonth > 12) {return false;}		
				break;
			case 'dd/mmm/yy':
				switch (iMonth) {
					case "jan": iMonth = 1; break;
					case "feb": iMonth = 2; break;
					case "mar": iMonth = 3; break;
					case "apr": iMonth = 4; break;
					case "may": iMonth = 5; break;
					case "jun": iMonth = 6; break;
					case "jul": iMonth = 7; break;
					case "aug": iMonth = 8; break;
					case "sep": iMonth = 9; break;
					case "oct": iMonth = 10; break;
					case "nov": iMonth = 11; break;
					case "dec": iMonth = 12; break;
					default: return false;				
				}
				break;
		}

		//Test the days, depending on the month.
		switch (iMonth) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				if (iDay < 0 || iDay > 31) {return false;}
				break;			
			case 2:
				if (iYear/4 - parseInt(iYear/4) == 0) {
					if (iDay > 29) {return false;}					
				}
				else {
					if (iDay > 28) {return false;}
				}
				break;				
			default:
				if (iDay > 30) {return false;}
				break;				
		}		
	}	
	catch (e) {
		alert('Error in ' + sProcedure + '\n\r\n\r' + e.number + ": " + e.description);
		return false;
	}

	return true;
}

function gDateFormat(sDate, sFormat)
{
	var dDate = new Date(sDate);
	var iDay = dDate.getDate();
	var iMonth = dDate.getMonth() + 1;
	var iYear = dDate.getFullYear();
	
	switch (sFormat) {
		case 'dd/mm/yyyy':
			if (iDay < 10) {iDay = '0' + iDay}
			if (iMonth < 10) {iMonth = '0' + iMonth}
			return iDay + '/' + iMonth + '/' + iYear;
			break;
		default:
			alert('The gDateFormat() function does not yet cater for the date format provided (' + sFormat + ').');
			break;
	}
}

function gMonth(iMonth)
{
	try {
		var s;
		
		switch(iMonth) {
			case 1: s = "Jan"; break;
			case 2: s = "Feb"; break;
			case 3: s = "Mar"; break;
			case 4: s = "Apr"; break;
			case 5: s = "May"; break;
			case 6: s = "Jun"; break;
			case 7: s = "Jul"; break;
			case 8: s = "Aug"; break;
			case 9: s = "Sep"; break;
			case 10: s = "Oct"; break;
			case 11: s = "Nov"; break;
			case 12: s = "Dec"; break;
			default: s = ""; break;
		}
		return s;
	}
	catch (e) {
		alert("gMonth error " + e.number + "\n" + e.description);
		return false;
	}
}

function gMonthNumber(sMonth)
{
	try {
		var i;
		
		switch(sMonth) {
			case "Jan": i = 1; break;
			case "Feb": i = 2; break;
			case "Mar": i = 3; break;
			case "Apr": i = 4; break;
			case "May": i = 5; break;
			case "Jun": i = 6; break;
			case "Jul": i = 7; break;
			case "Aug": i = 8; break;
			case "Sep": i = 9; break;
			case "Oct": i = 10; break;
			case "Nov": i = 11; break;
			case "Dec": i = 12; break;
			default: i = 0; break;
		}
		return i;
	}
	catch (e) {
		alert("gMonthNumber error " + e.number + "\n" + e.description);
		return false;
	}
}	

function gIsLeapYear(year) {
    return ((new Date(year, 1, 29, 0, 0).getMonth() == 2) ? false : true);
}

/*
//Date Compare 1.
var s, d, m, y, dAuditDate, dMax;

s = String(sAuditDate);
d = s.substring(0,2);
m = s.substring(3,5);
y = s.substring(6,10);
dAuditDate = Date.UTC(y, m, d, 0, 0, 0, 0);

s = new Date();
d = s.getDate();
m = s.getMonth() + 1; //Plus 1!
y = s.getFullYear() + 1;
dMax = Date.UTC(y, m, d, 0, 0, 0, 0);

if (dAuditDate > dMax) {alert('Next Audit cannot be more than 1 year in the future.'); return false;}

//Date Compare 2.
function z_callsSearch() {
		var sFrom = frmForm.txtSearch_CallsFrom.value;
		var sTo = frmForm.txtSearch_CallsTo.value;		
		
		var dFrom = date_Create(sFrom, 'dd/mm/yyyy');
		var dTo = date_Create(sTo, 'dd/mm/yyyy');
		if (dFrom > dTo) {
			alert('Please make sure the From date is not later than the To date.');
			return false;
		}
}
*/

//Email.

function gEmailValidate(sEmail)
{
	var iAtPosition;
	var iDotPosition;
	
	if (sEmail.length < 6) {return false};
	
	iAtPosition = sEmail.indexOf("@");
	if (iAtPosition < 1) {return false};
	
	iDotPosition = sEmail.indexOf(".");
	
	//If there is a dot before the @ then...
	if (iDotPosition - iAtPosition < 0) {
	//...if it is at the beginning of sEmail or immediately before the @, return false.
		if (iDotPosition == 0 || iDotPosition - iAtPosition == -1) {
			return false;
		}
	//...otherwise find the position of the next dot.
		else {
			iDotPosition = (sEmail.substr(iDotPosition + 1)).indexOf(".") + iDotPosition + 1;
		}
	}
	
	//The dot should not be immediately after the @.
	if (iDotPosition - iAtPosition < 2) {return false};
	
	//The dot should be followed by at least 2 characters.
	if (sEmail.length - iDotPosition < 3) {return false};
	
	return true;
}

//Numeric.

function gNumericValidate (sValue, bInteger, bMandatory) {
	var sChar;
	var bAlreadyContainsPoint = false;
	var sValidChars = '0123456789';
	if (!bInteger) {sValidChars = sValidChars + '.'}

	if (bMandatory && (sValue === '')) {return false}
	if ((sValue === '') && !bMandatory) {return true}
	
	for (i = 0; i < sValue.length; i++) { 
		sChar = sValue.charAt(i); 
		if (sValidChars.indexOf(sChar) == -1) {
			return false;
		}
		else {
			if (sChar == '.') {if (bAlreadyContainsPoint) {return false} else {bAlreadyContainsPoint = true}}
		}
	}
	
	return true;	   
}

//Popup.

function gPopup(sURL, iPersonID, sMode){
	var iWidth, iHeight, iLeft, iTop, sFeatures;
	
	iWidth = 390; iHeight = 200;
	
	//Centre the popup over the parent window.
	iLeft = window.screenLeft + (screen.width - iWidth) / 2;
	iTop = window.screenTop + (screen.height - iHeight) / 2;

	sFeatures = 'width=' + iWidth + ', height=' + iHeight + ', left=' + iLeft + ', top=' + iTop;
	sFeatures = sFeatures + ', scrollbars=1, dependent=yes, alwaysRaised=yes';

	//window.open ('http://jason/' + sURL, sWindowName, sFeatures);
	//if (sURL.indexOf("http") != -1){window.open (sURL + '?PersonID=' + iPersonID + '&Mode=' + sMode, '', sFeatures)} else {window.open ('/' + sURL + '?PersonID=' + iPersonID + '&Mode=' + sMode, '', sFeatures)};
	if (sURL.indexOf("http") != -1){window.open (sURL + '?PersonID=' + iPersonID + '&Mode=' + sMode, '', sFeatures)} else {window.open (sURL + '?PersonID=' + iPersonID + '&Mode=' + sMode, '', sFeatures)};
}

//Text.

function gTitleCase(s) {
	var t;
	
	if (s != '') {
		t = s.substring(0, 1);
		t = t.toUpperCase();
		if (s.length > 1) {t = t + s.substring(1, s.length)}
	}
	else {
		t = s;
	}
	
	return t;
}

//Other.

//Money.
function gMoney(value, sCurrencySymbol) {
	var s = value;

	if (s == '') {
		s = '0.00';
	}
	else {	
		if (s.indexOf('.') != -1) { //If s contains a decimal point with only 1 digit after it then add a zero.
			if (s.length - s.indexOf('.') != 3) {s = s + '0'}
		}
		else { //If s does not contain a decimal point then add a decimal point and two zeros.
			s = s + '.00';
		}
		
		if (s.indexOf(',') == -1) { //If s does not contain a comma but is 1000 or more then insert a comma.
			if (s >= 1000) { //Add a comma 3 places to the left of the decimal point.
				s = s.substring(0, s.indexOf('.') - 3) + ',' + s.substring(s.indexOf('.') - 3, s.length);
			}
		}
	}

	s = sCurrencySymbol + s;
	
	return s;
}