// Update1:  25/01/07
// Changed how setDate gets it's field values


//////////////////////////////////////////////////////////////////////////////////////////////////

// Check that an email address is valid based on RFC 821 (?)
function isValidEmail(address) {
	if (address.indexOf('@') < 1) return false;
	var name = address.substring(0, address.indexOf('@'));
	var domain = address.substring(address.indexOf('@') + 1);
	if (name.indexOf('(') != -1 || name.indexOf(')') != -1 || name.indexOf('<') != -1 || name.indexOf('>') != -1 || name.indexOf(',') != -1 || name.indexOf(';') != -1 || name.indexOf(':') != -1 || name.indexOf('\\') != -1 || name.indexOf('"') != -1 || name.indexOf('[') != -1 || name.indexOf(']') != -1 || name.indexOf(' ') != -1) return false;
	if (domain.indexOf('(') != -1 || domain.indexOf(')') != -1 || domain.indexOf('<') != -1 || domain.indexOf('>') != -1 || domain.indexOf(',') != -1 || domain.indexOf(';') != -1 || domain.indexOf(':') != -1 || domain.indexOf('\\') != -1 || domain.indexOf('"') != -1 || domain.indexOf('[') != -1 || domain.indexOf(']') != -1 || domain.indexOf(' ') != -1) return false;
	return true;
}


// Check that an email address has the form something@something.something
// This is a stricter standard than RFC 821 (?) which allows addresses like postmaster@localhost
function isValidEmailStrict(address) {
	if (isValidEmail(address) == false) return false;
	var domain = address.substring(address.indexOf('@') + 1);
	if (domain.indexOf('.') == -1) return false;
	if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1) return false;
	return true;
}

function noOp() {

}

function isAlphabetic(string) {
	if (string.search) {
		if ((string.search(/[^a-zA-Z\s]/) != -1)) return false;
	}
	return true;
}

function printDoc(id) {
	var leftPos = (screen.availWidth-500) / 2
	var topPos = (screen.availHeight-300) / 2 
	window.open("printWin.php?sectionID="+id,"","width=500,height=300,scrollbars=yes,resizable=no,titlebar=0,top="+topPos+",left="+leftPos);
}

function emailDoc(id) {
	var leftPos = (screen.availWidth-500) / 2
	var topPos = (screen.availHeight-300) / 2 
	window.open("emailWin.php?sectionID="+id,"","width=500,height=300,scrollbars=no,resizable=no,titlebar=0,top="+topPos+",left="+leftPos);
}

//Update1:   25/01/07
// changed forms[0] to frmMain

function setDate(fld) {
	dateFld=fld;
	var d=new Date();
	var sTmp=eval("document.frmMain."+fld+".value");
	if (sTmp!="0000-00-00"&&sTmp.length==10) {
		d.setDate(sTmp.substr(8,2));
		d.setMonth(eval(sTmp.substr(5,2))-1);
		d.setYear(sTmp.substr(0,4));
	}
	var calWin = window.open("calendar.php?setDate="+sTmp+"&month="+eval(d.getMonth()+1)+"&year="+d.getFullYear(),"calWin","width=230,height=205");
}
var dateFld
function validateDate(fld,showAlert) {
	if (fld.value=="") {
		document.getElementById(fld.name.replace("_Display","")).value="";
		return true;
	} else {
		var dtFld = fld.value.split("/");
		var dt = new Date(dtFld[2],dtFld[1]-1,dtFld[0]);
		if (fld.value.length>0&&(dt=="Invalid Date"||isNaN(dt)||dtFld[2]<99||dt.getMonth()+1!=dtFld[1])) {
			if (showAlert) alert("The date you entered is invalid.\n\nDates should be in the format dd/mm/yyyy");
			return false;
		} else {
			var sDate = dt.getFullYear()+"-";
			if (eval(dt.getMonth()+1)<10) 
				sDate+="0"+eval(dt.getMonth()+1);
			else
				sDate+=eval(dt.getMonth()+1)
			if (dt.getDate()<10)
				sDate+="-0"+dt.getDate();
			else
				sDate+="-"+dt.getDate();
			document.getElementById(fld.name.replace("_Display","")).value=sDate;
			return true;
		}
	}
}

// Check that a credit card number is valid based using the LUHN formula (mod10 is 0)
function isValidCreditCard(number) {
	number = '' + number;
	if (number='4646464646464646') return true;
	if (number.length > 16 || number.length < 13 ) return false;
	else if (getMod10(number) != 0) return false;
	else if (arguments[1]) {
		var type = arguments[1];
		var first2digits = number.substring(0, 2);
		var first4digits = number.substring(0, 4);
		if (type.toLowerCase() == 'visa' && number.substring(0, 1) == 4 &&
			(number.length == 16 || number.length == 13 )) return true;
		else if (type.toLowerCase() == 'mastercard' && number.length == 16 &&
			(first2digits == '51' || first2digits == '52' || first2digits == '53' || first2digits == '54' || first2digits == '55')) return true;
		else if (type.toLowerCase() == 'american express' && number.length == 15 && 
			(first2digits == '34' || first2digits == '37')) return true;
		else if (type.toLowerCase() == 'diners club' && number.length == 14 && 
			(first2digits == '30' || first2digits == '36' || first2digits == '38')) return true;
		else if (type.toLowerCase() == 'discover' && number.length == 16 && first4digits == '6011') return true;
		else if (type.toLowerCase() == 'enroute' && number.length == 15 && 
			(first4digits == '2014' || first4digits == '2149')) return true;
		else if (type.toLowerCase() == 'jcb' && number.length == 16 &&
			(first4digits == '3088' || first4digits == '3096' || first4digits == '3112' || first4digits == '3158' || first4digits == '3337' || first4digits == '3528')) return true;
		
    // if the above card types are all the ones that the site accepts, change the line below to 'else return false'
    else return true;
	}
	else return true;
}

// Returns a checksum digit for a number using mod 10
function getMod10(number) {
	
	// convert number to a string and check that it contains only digits
	// return -1 for illegal input
	number = '' + number;
	number = removeSpaces(number);
	if (!isNumeric(number)) return -1;
	
	// calculate checksum using mod10
	var checksum = 0;
	for (var i = number.length - 1; i >= 0; i--) {
		var isOdd = ((number.length - i) % 2 != 0) ? true : false;
		digit = number.charAt(i);
		
		if (isOdd) checksum += parseInt(digit);
		else {
			var evenDigit = parseInt(digit) * 2;
			if (evenDigit >= 10) checksum += 1 + (evenDigit - 10);
			else checksum += evenDigit;
		}
	}
	return (checksum % 10);
}

// Remove all spaces from a string
function removeSpaces(string) {
	var newString = '';
	for (var i = 0; i < string.length; i++) {
		if (string.charAt(i) != ' ') newString += string.charAt(i);
	}
	return newString;
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
	}
	return true;
}

// JavaScript Document
function lastUpdated() {
var m_names = new Array("Jan", "Feb", "Mar", 
"Apr", "May", "June", "July", "Aug", "Sept", 
"Oct", "Nov", "Dec");

var d = new Date();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
//document.write(m_names[curr_month] + " " + curr_year);
document.write(curr_year);
}