<!--
function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

//Checks that a Date Field contains a valid date
//	if false, pops up an alertbox and focuses on the field
function checkDateData(objField, sFieldName) {
	if (!isDate(objField.value, "mm/dd/yyyy")) {
		return getFocusAlert(objField, "You must enter a valid date (mm/dd/yyyy) for " + sFieldName + ".");
	}
	else {
		return true;
	}
}

//Checks that a Date Field contains date after a specific date
//	if false, pops up an alertbox and focuses on the field
function checkDateAfterData(objField, sFieldName, dDate) {
	if (!isDate(objField.value, "mm/dd/yyyy")) {
		return getFocusAlert(objField, "You must enter a valid date (mm/dd/yyyy) for " + sFieldName + ".");
	}
	else {
		if (compareDates(objField.value, "mm/dd/yyyy", dDate, "mm/dd/yyyy") != 0)
			return false;
		else
			return true;
	}
}

//Checks that a Text Field contains numeric data within a range
//	if false, pops up an alertbox and focuses on the field
function checkRangeData(objField, sFieldName, minValue, maxValue) {
	if (!isValidRange(objField.value, minValue, maxValue)) {
		return getFocusAlert(objField, "You must enter a valid number for " + sFieldName + " between " + minValue + " and " + maxValue + ".");
	}
	else {
		return true;
	}
}

//Checks that a Text Field contains numeric data
//	if false, pops up an alertbox and focuses on the field
function checkNumberData(objField, sFieldName) {
	if (!isValidNumber(objField.value)){
		return getFocusAlert(objField, "You must enter a valid number for " + sFieldName + ".");
	}
	else {
		return true;
	}
}

//Checks if a Text Field contains any data
//	if false, pops up an alertbox and focuses on the field
function checkStringData(objField, sFieldName) {
	if (!isValidString(objField.value)) {
		return getFocusAlert(objField, "You must enter a valid value for " + sFieldName + ".");
	}
	else {
		return true;
	}
}

//Checks if a Text Field contains any data less than a given length
//	if false, pops up an alertbox and focuses on the field
function checkLengthData(objField, sFieldName, maxLength) {
	if (!isValidLength(objField.value, maxLength)) {
		return getFocusAlert(objField, "You cannot enter more than " + maxLength +" characters for " + sFieldName + ".");
	}
	else {
		return true;
	}
}

//Checks if a Text Field contains any data with a fix length
//	if false, pops up an alertbox and focuses on the field
function checkMinMaxLengthData(objField, sFieldName, minLength, maxLength) {
	if (!isMinMaxLength(objField.value, minLength, maxLength)) {
		var strErrorMsg;
		if (minLength == maxLength)
		{
			strErrorMsg = "You have to enter exactly " + minLength +" characters for " + sFieldName + "."
		}
		else
		{
			strErrorMsg = "You have to enter a minimum of " + minLength + " and a maximum of " + maxLength + " characters for " + sFieldName + "."
		}
		
		return getFocusAlert(objField, strErrorMsg);
	}
	else {
		return true;
	}
}

//Checks if a Text Field contains any data with a fix length
//	if false, pops up an alertbox and focuses on the field
function checkMinLengthData(objField, sFieldName, minLength) {
	if (!isMinLength(objField.value, minLength)) {
		var strErrorMsg;
		strErrorMsg = "You have to enter at least a minimum of " + minLength + " characters for " + sFieldName + "."
				
		return getFocusAlert(objField, strErrorMsg);
	}
	else {
		return true;
	}
}

//Checks if a Text Field contains data that matches any digit 
//	and common phone number characters [(, ), ., -, x]
//	if false, pops up an alertbox and focuses on the field
function checkPhoneData(objField, sFieldName) {
	var result = false;
	
	//Check if the field has data
	if (isValidString(objField.value)) {
		//strip out acceptable non-numeric characters
		var stripped = objField.value.replace(/[x\(\)\.\-\ ]/g, '');
	
		//Check if there are at least 7 characters
		if (stripped.length >= 10) {
			//Check if the remaining characters are only digits
			if (!isNaN(stripped)) {
				result = true;
			}
		}
	}
	
	if (result == false) {
		return getFocusAlert(objField, "You must enter a valid value for " + sFieldName + ".");
	}
	return result;
}

//Checks if a Text Field contains data that matches a zipcode (5 or 9 digits)
//	if false, pops up an alertbox and focuses on the field
function checkZipcodeData(objField, sFieldName) {
	if (!isValidZipcode(objField.value)) {
		return getFocusAlert(objField, "You must enter a valid value for " + sFieldName + ".");
	}
	else {
		return true;
	}
}


//Checks if a Text Field contains any non-numeric data
//	if false, pops up an alertbox and focuses on the field
function checkTextData(objField, sFieldName) {
	if (!isValidString(objField.value) || !isNaN(objField.value)) {
		return getFocusAlert(objField, "You must enter a valid value for " + sFieldName + ".");
	}
	else {
		return true;
	}
}

//Checks if a Text Field contains a valid file name
//	if false, pops up an alertbox and focuses on the field
function checkFileName(objField, sFieldName, sFileExtension) {
	if (!isValidFile(objField.value, sFileExtension)) {
		var sMsg = "You must enter a valid file name for " + sFieldName + " that does not contain spaces or the characers \\/\':*?\"<>[]{}|";
		
		if (isValidText(sFileExtension)) {
			sMsg = sMsg + " and ends with the extension '" + sFileExtension+ "'"; 
		}
		
		sMsg = sMsg + ".";
		
		return getFocusAlert(objField, sMsg);
	}
	else {
		return true;
	}
}

//Checks if a select object does not have a specified value selected
//	if false, pops up an alertbox and focuses on the field
function checkSelectData(objField, sFieldName, invalidValue) {
	if (!isValidSelectionValue(objField, invalidValue)) {
		return getFocusAlert(objField, "You must select a valid value for " + sFieldName + ".");
	}
	else {
		return true;
	}
}
//Checks if a Text Field contains any data
//	if false, pops up an alertbox and focuses on the field
function checkEmailData(objField, sFieldName) {
	if (!isValidEmail(objField.value)) {
		return getFocusAlert(objField, "You must enter a valid value for " + sFieldName + ".");
	}
	else {
		return true;
	}
}

//Checks if a checkbox object is checked
//	if false, pops up an alertbox and focuses on the field
function checkCheckboxData(objField, sFieldName) {
	if (!isChecked(objField)) {
		return getFocusAlert(objField, "You must check " + sFieldName + ".");
	}
	else {
		return true;
	}
}

//Checks if a radio button has any selected items
//	if false, pops up an alert box and focuses on the first element
function checkRadioData(objField, sMsg) {
	return checkObjectArrayData(objField, sMsg);
}

//Checks if a radio button or checkbox has any selected items
// if false, pops up an alertbox and focuses on the first element
function checkObjectArrayData(objField, sMsg) {
	var result = getObjectArrayCheckedIndex(objField)
	
	//Checks if there is only one element and not checked
	if (isNaN(result) && objField.checked == false)	{
		return getFocusAlert(objField, sMsg);
	}
	//Checks if there are multiple elements and nothing checked
	else if (result == -1) {
		objField[0].focus();
		return errorAlert(sMsg);
	}
	//At least one element is checked
	else {
		return true;
	}
}

//Sets focus to the given field, pops up an alertbox, returns false
function getFocusAlert(objField, sMsg) {
	objField.focus();
	return errorAlert(sMsg);
}

//Returns a false result and creates an alert box popup	
function errorAlert(sMsg) {
	alert(sMsg);
	return false;
}

//returns the value of the checked radio button option or
//	returns null if no option is selected
function getRadioValue(objField) {
	var result = null;
	var checkedIndex = getCheckedIndex(objField);
	
	if (isNaN(checkedIndex)) {
		result = objField.value;	
	} else {
		result = objField[checkedIndex].value;
	}
	return result;
}

//calls getObjectArrayCheckedIndex
function getCheckedIndex(objField) {
	return getObjectArrayCheckedIndex(objField);
}

//checks a radio button or checkbox object
//	-returns a non-numeric result if it has only one element
// -returns its length if it has more than one element,
function getObjectArrayLength(objField) {
	var result = "only one element";
	if (!isNaN(objField.length)) {
		result = objField.length;
	}
	return result;
}

//checks a radio button or checkbox object
//	-returns a non-numeric result if it has only one element
//	-returns the index of the first checked element
//	-returns -1 if there are no checked elements
function getObjectArrayCheckedIndex(objField) {
	var length = getObjectArrayLength(objField);
	var checkedIndex = -1;
	
	//Check if the object has only one element
	if (isNaN(length)) {
		checkedIndex = "Not Checked";
	}
	else {
		//Loops through all object elements and 
		//	identifies the index of the first checked element
		for(var i = 0; i < length; i++) {
			if (objField[i].checked) {
				checkedIndex = i;
				break;
			}
		}
	}
	return checkedIndex;
}

//Validates String is not zero length
function isValidString(StringValue) {
	var result = false;
	if ((StringValue != null) && (trim(StringValue).length > 0)) {
		result = true;
	}
	return result;
}

//Validates Text is not zero length and not a number
function isValidText(StringValue) {
	var result = false;
	if ((isValidString(StringValue)) && (isNaN(StringValue))) {
		result = true;
	}
	return result;
}

//Validates String is composed only of a-z or A-Z characters
function isAlphabetic(StringValue) {
	var result = false;
	if ((isValidString(StringValue)) && (StringValue.search(/[^a-zA-Z]/g) == -1)) {
		result = true;
	}
	return result;
}

//Validates that a String is not longer than a given value
function isValidLength(StringValue, maxLength) {
	var result = false;
	StringValue = trim(StringValue);
	if (StringValue.length <= maxLength) {
		result = true;
	}
	return result;
}

//Validates that a String length is between two numbers
function isMinMaxLength(StringValue, minLength, maxLength) {
	var result = false;
	StringValue = trim(StringValue);
	if ((StringValue.length >= minLength) && (StringValue.length <= maxLength)) {
		result = true;
	}
	return result;
}

//Validates that a String length is between two numbers
function isMinLength(StringValue, minLength) {
	var result = false;
	StringValue = trim(StringValue);
	if (StringValue.length >= minLength) {
		result = true;
	}
	return result;
}

//Validates String data as a Number
function isValidNumber(StringValue) {
  	var result = false;
	if ((isValidString(StringValue)) && (!isNaN(StringValue))) {
		result = true;
	}
	return result;
}

//Validates String data as a Number within a set range
function isValidRange(StringValue, minValue, maxValue) {
  	var result = false;
	if (isValidNumber(StringValue)) {
		if((StringValue <= maxValue) && (StringValue >= minValue)) {
			result = true;
		}		
	}
	return result;
}

//Validates the selected value of a select object does not match a given value
function isValidSelectionValue(objField, invalidValue) {
	var result = false;
	if (objField.options[objField.options.selectedIndex].value != invalidValue) {
		result = true;
	}
	return result;		
}

//Validates the selected index of a select object does not match a given index
function isValidSelectionIndex(objField, invalidIndex) {
	var result = false;
	if (objField.selectedIndex != invalidIndex) {
		result = true;
	}
	return result;	
}


//Validates the selected index of a select object does not match a given index
function isValidFile(sFileName, sFileExtension) {
	sFileName = sFileName.toLowerCase();
	sFileExtension = sFileExtension.toLowerCase();
	var result = false;
	if (isValidText(sFileName)) {
		//Check that the filename doesn't contain invalid characters
		if (sFileName.search(/[\\\/\:\*\?\<\>\|\s\"\'\{\}\[\]]/g) == -1) {	//"
		
			//Check if a file extension was supplied
			if (isValidText(sFileExtension)) {
				//Check that the filename has a valid file extension
				if (sFileName.indexOf(sFileExtension) > 0) {
					result = true;
				}
			} else {
				result = true;
			}
		}
	}
	return result;	
}


//Validates String data as an Email Address
function isValidEmail(email) {
	var result = false;
	if(isValidString(email)) {
		var theStr = new String(email);
		var index = theStr.indexOf("@");
		if (index > 0)	{
			var pindex = theStr.indexOf(".",index);
			if ((pindex > index+1) && (theStr.length > pindex+2)) {
				result = true;
			}
		}
	}
	return result;
}

//Validates if the integer values for day, month, and year
//	combine to form a valid date - if not, returns false
function isValidDate(day, month, year) {

	day = "" + parseInt(day);
	month = "" + parseInt(month);
	year = "" + parseInt(year);

	//Check that the month value is in the correct range
	if (!isValidRange(month, 1, 12)) {
		return false;
	}
	//Check that the day value is in the correct range
	if (!isValidRange(day, 1, 31)) {
		return false;
	}
	
	// Make sure user doesn't put 31 for a month that only has 30 days
	if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
		return false;
	}

	// Check for February date validity (including leap years) 
	if (month == 2) {

		// figure out if "year" is a leap year; don't forget that
		// century years are only leap years if divisible by 400
		var isleap=(year%4==0 && (year%100!=0 || year%400==0));
		if (day > 29 || (day == 29 && !isleap)) {
			return false;
		}
	}
	return true;
}

//Validates if the integer values for day, month, and year
//	combine to form a valid future date - if not, returns false
function isFutureDate(day, month, year) {

	day = "" + parseInt(day);
	month = "" + parseInt(month);
	year = "" + parseInt(year);

	//Check that this is a valid date
	if (!isValidDate(day, month, year)) {
		return false;
	}
	
	var currentDate = new Date();
	var testDate = new Date(year, month - 1, day);
	
	if (currentDate.getFullYear() > testDate.getFullYear()) {
		return false;
	}
	else if (currentDate.getFullYear() == testDate.getFullYear()) {
		if (currentDate.getMonth() > testDate.getMonth()) {
			return false;
		}
		else if (currentDate.getMonth() == testDate.getMonth()) {
			if (currentDate.getDate() >= testDate.getDate()) {
				return false;
			}
		}
	}
	return true;
}

//Sets a default value for an empty field
function setDefaultValue(objField, sDefaultVal) {
	if (!isValidString(objField.value)) {
		objField.value = sDefaultVal;
	}
}

//Validates a Checkbox is checked
function isChecked(objField) {
  	var result = false;
	if (objField.checked == true) {
		result = true;
	}
	return result;
}

//Sets the checked/unchecked value for a checkbox or radio button
function setCheckValue(objField, checkedValue, uncheckedValue) {
	if (objField.checked == true)	{
		objField.value = checkedValue;
	}
	else {
		objField.value = uncheckedValue;
	}
}

//remove everything that isn't a number and return the result
function cleanNumberic(strNumeric) {
	return strNumeric.replace(/\D/g,'')
}

function checkCardNumWithMod10(cardNum) {
	var i;
	var cc = new Array(16);
	var checksum = 0;
	var validcc;

	// assign each digit of the card number to a space in the array	
	for (i = 0; i < cardNum.length; i++) {
		cc[i] = Math.floor(cardNum.substring(i, i+1));
	}

	// walk through every other digit doing our magic
	// if the card number is sixteen digits then start at the
	// first digit (position 0), otherwise start from the
	// second (position 1)
	for (i = (cardNum.length % 2); i < cardNum.length; i+=2) {
		var a = cc[i] * 2;
		if (a >= 10) {
			var aStr = a.toString();
			var b = aStr.substring(0,1);
			var c = aStr.substring(1,2);
			cc[i] = Math.floor(b) + Math.floor(c);
		} else {
			cc[i] = a;
		}
	}

	// add up all of the digits in the array
	for (i = 0; i < cardNum.length; i++) {
		checksum += Math.floor(cc[i]);
	}

	// if the checksum is evenly divisble by 10
	// then this is a valid card number
	validcc = ((checksum % 10) == 0);

	return validcc;
}

function isValidCCNum(cardNum, cardType)
{
	var upperCardType = cardType.toUpperCase();
	var result = false;
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);
	
	switch (upperCardType)
	{	
		case "VISA":	//Visa Card
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AMEX":	//American Express
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "MSTR":	//MasterCard
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "DISC":	//Discover
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		/*
		case "DINERS":	//Diner's Club
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		*/
		default:
			return errorAlert(cardType + " is not a recognized credit card type");
			break;
	}
	
	/*
	if (result == true) {
		// card number seems OK so do the Mod10
		if (!checkCardNumWithMod10(cardNum)) {
			result = false;
		}
	}
	*/

	return result;
}

//Checks if a credit card field contains valid data
//	if false, pops up an alertbox and focuses on the field
function checkCCData(objField, sFieldName, sCardType) {
	
	//Check if it is a valid string
	if (isValidString(objField.value)) {
		sCardNum = objField.value;
	
		// clean up any spaces or dashes in the card number
		sCardNum = sCardNum.replace(/[-\s]/g, '');
	}
	else {
		return getFocusAlert(objField, "You must enter a valid value for " + sFieldName + ".");
	}

	if (isNaN(sCardNum) || (!isValidCCNum(sCardNum, sCardType))) {
		return getFocusAlert(objField, "You must enter a valid value for " + sFieldName + ".");
	} else {
		// set the field value to the updated value
		objField.value = sCardNum;
		return true;
	}
}

//check for valid US Zipcode
function isValidZipcode(strZipCode) {
  	var result = false;

	if (isValidString(strZipCode)) {
		var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
		result = objRegExp.test(strZipCode);
	}
	return result;
}


// ------------------------------------------------------------------
// These functions use the same 'format' strings as the 
// java.text.SimpleDateFormat class, with minor exceptions.
// The format string consists of the following abbreviations:
// 
// Field        | Full Form          | Short Form
// -------------+--------------------+-----------------------
// Year         | yyyy (4 digits)    | yy (2 digits), y (2 or 4 digits)
// Month        | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits)
// Day of Month | dd (2 digits)      | d (1 or 2 digits)
// Hour (1-12)  | hh (2 digits)      | h (1 or 2 digits)
// Hour (0-23)  | HH (2 digits)      | H (1 or 2 digits)
// Hour (0-11)  | KK (2 digits)      | K (1 or 2 digits)
// Hour (1-24)  | kk (2 digits)      | k (1 or 2 digits)
// Minute       | mm (2 digits)      | m (1 or 2 digits)
// Second       | ss (2 digits)      | s (1 or 2 digits)
// AM/PM        | a                  |
//
// NOTE THE DIFFERENCE BETWEEN MM and mm! Month=MM, not mm!
// Examples:
//  "MMM d, y" matches: January 01, 2000
//                      Dec 1, 1900
//                      Nov 20, 00
//  "M/d/yy"   matches: 01/20/00
//                      9/2/00
//  "MMM dd, yyyy hh:mm:ssa" matches: "January 01, 2000 12:30:45AM"
// ------------------------------------------------------------------

var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
function LZ(x) {return(x<0||x>9?"":"0")+x}

// ------------------------------------------------------------------
// isDate ( date_string, format_string )
// Returns true if date string matches format of format string and
// is a valid date. Else returns false.
// It is recommended that you trim whitespace around the value before
// passing it to this function, as whitespace is NOT ignored!
// ------------------------------------------------------------------
function isDate(val,format) {
	var date=getDateFromFormat(val,format);
	if (date==0) { return false; }
	return true;
	}

// -------------------------------------------------------------------
// compareDates(date1,date1format,date2,date2format)
//   Compare two date strings to see which is greater.
//   Returns:
//   1 if date1 is greater than date2
//   0 if date2 is greater than date1 of if they are the same
//  -1 if either of the dates is in an invalid format
// -------------------------------------------------------------------
function compareDates(date1,dateformat1,date2,dateformat2) {
	var d1=getDateFromFormat(date1,dateformat1);
	var d2=getDateFromFormat(date2,dateformat2);
	if (d1==0 || d2==0) {
		return -1;
		}
	else if (d1 > d2) {
		return 1;
		}
	return 0;
	}

// ------------------------------------------------------------------
// formatDate (date_object, format)
// Returns a date in the output format specified.
// The format string uses the same abbreviations as in getDateFromFormat()
// ------------------------------------------------------------------
function formatDate(date,format) {
	format=format+"";
	var result="";
	var i_format=0;
	var c="";
	var token="";
	var y=date.getYear()+"";
	var M=date.getMonth()+1;
	var d=date.getDate();
	var H=date.getHours();
	var m=date.getMinutes();
	var s=date.getSeconds();
	var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
	// Convert real date parts into formatted versions
	var value=new Object();
	if (y.length < 4) {y=""+(y-0+1900);}
	value["y"]=""+y;
	value["yyyy"]=y;
	value["yy"]=y.substring(2,4);
	value["M"]=M;
	value["MM"]=LZ(M);
	value["MMM"]=MONTH_NAMES[M-1];
	value["d"]=d;
	value["dd"]=LZ(d);
	value["H"]=H;
	value["HH"]=LZ(H);
	if (H==0){value["h"]=12;}
	else if (H>12){value["h"]=H-12;}
	else {value["h"]=H;}
	value["hh"]=LZ(value["h"]);
	if (H>11){value["K"]=H-12;} else {value["K"]=H;}
	value["k"]=H+1;
	value["KK"]=LZ(value["K"]);
	value["kk"]=LZ(value["k"]);
	if (H > 11) { value["a"]="PM"; }
	else { value["a"]="AM"; }
	value["m"]=m;
	value["mm"]=LZ(m);
	value["s"]=s;
	value["ss"]=LZ(s);
	while (i_format < format.length) {
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		if (value[token] != null) { result=result + value[token]; }
		else { result=result + token; }
		}
	return result;
	}
	
// ------------------------------------------------------------------
// Utility functions for parsing in getDateFromFormat()
// ------------------------------------------------------------------
function _isInteger(val) {
	var digits="1234567890";
	for (var i=0; i < val.length; i++) {
		if (digits.indexOf(val.charAt(i))==-1) { return false; }
		}
	return true;
	}
function _getInt(str,i,minlength,maxlength) {
	for (var x=maxlength; x>=minlength; x--) {
		var token=str.substring(i,i+x);
		if (token.length < minlength) { return null; }
		if (_isInteger(token)) { return token; }
		}
	return null;
	}
	
// ------------------------------------------------------------------
// getDateFromFormat( date_string , format_string )
//
// This function takes a date string and a format string. It matches
// If the date string matches the format string, it returns the 
// getTime() of the date. If it does not match, it returns 0.
// ------------------------------------------------------------------
function getDateFromFormat(val,format) {
	val=val+"";
	format=format+"";
	var i_val=0;
	var i_format=0;
	var c="";
	var token="";
	var token2="";
	var x,y;
	var now=new Date();
	var year=now.getYear();
	var month=now.getMonth()+1;
	var date=now.getDate();
	var hh=now.getHours();
	var mm=now.getMinutes();
	var ss=now.getSeconds();
	var ampm="";
	
	while (i_format < format.length) {
		// Get next token from format string
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		// Extract contents of value based on format token
		if (token=="yyyy" || token=="yy" || token=="y") {
			if (token=="yyyy") { x=4;y=4; }
			if (token=="yy")   { x=2;y=2; }
			if (token=="y")    { x=2;y=4; }
			year=_getInt(val,i_val,x,y);
			if (year==null) { return 0; }
			i_val += year.length;
			if (year.length==2) {
				if (year > 70) { year=1900+(year-0); }
				else { year=2000+(year-0); }
				}
			}
		else if (token=="MMM"){
			month=0;
			for (var i=0; i<MONTH_NAMES.length; i++) {
				var month_name=MONTH_NAMES[i];
				if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
					month=i+1;
					if (month>12) { month -= 12; }
					i_val += month_name.length;
					break;
					}
				}
			if ((month < 1)||(month>12)){return 0;}
			}
		else if (token=="MM"||token=="M") {
			month=_getInt(val,i_val,token.length,2);
			if(month==null||(month<1)||(month>12)){return 0;}
			i_val+=month.length;}
		else if (token=="dd"||token=="d") {
			date=_getInt(val,i_val,token.length,2);
			if(date==null||(date<1)||(date>31)){return 0;}
			i_val+=date.length;}
		else if (token=="hh"||token=="h") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<1)||(hh>12)){return 0;}
			i_val+=hh.length;}
		else if (token=="HH"||token=="H") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<0)||(hh>23)){return 0;}
			i_val+=hh.length;}
		else if (token=="KK"||token=="K") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<0)||(hh>11)){return 0;}
			i_val+=hh.length;}
		else if (token=="kk"||token=="k") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<1)||(hh>24)){return 0;}
			i_val+=hh.length;hh--;}
		else if (token=="mm"||token=="m") {
			mm=_getInt(val,i_val,token.length,2);
			if(mm==null||(mm<0)||(mm>59)){return 0;}
			i_val+=mm.length;}
		else if (token=="ss"||token=="s") {
			ss=_getInt(val,i_val,token.length,2);
			if(ss==null||(ss<0)||(ss>59)){return 0;}
			i_val+=ss.length;}
		else if (token=="a") {
			if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";}
			else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";}
			else {return 0;}
			i_val+=2;}
		else {
			if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
			else {i_val+=token.length;}
			}
		}
	// If there are any trailing characters left in the value, it doesn't match
	if (i_val != val.length) { return 0; }
	// Is date valid for month?
	if (month==2) {
		// Check for leap year
		if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
			if (date > 29){ return false; }
			}
		else { if (date > 28) { return false; } }
		}
	if ((month==4)||(month==6)||(month==9)||(month==11)) {
		if (date > 30) { return false; }
		}
	// Correct hours value
	if (hh<12 && ampm=="PM") { hh+=12; }
	else if (hh>11 && ampm=="AM") { hh-=12; }
	var newdate=new Date(year,month-1,date,hh,mm,ss);
	return newdate.getTime();
	}



//-->

