/*
	Copyright 2000-2006 Nekosoft
	www.nekosoft.com
*/

var minYear=1900;
var maxYear=2012;


function trim(myStr) {
	return myStr.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function ltrim(myStr) {
	return myStr.replace(/^\s+/g, '');
}

function rtrim(myStr) {
	return myStr.replace(/\s+$/g, '');
}




function checkPhone(oData, label ) {
	validateFieldName = (label ? label : oData.name);
	strPhone = trim(oData.value);

	strPhone = strPhone.replace(/\D/g, '');

	if (strPhone == '') {
		oData.value = '';
		return true;
	}

	if (strPhone.length !=  10 ) {
		alert('Invalid ' + validateFieldName + ' entered.\n\nValid format: 555-555-1234');
		return false;
	}
	
	if ( strPhone.match( /^(...)(...)(....)/ )) {
		strPhone = RegExp.$1 + '-' + RegExp.$2 + '-' + RegExp.$3 ;
		oData.value =  strPhone;
		return true;
	}
}





function checkSSN(oData, label) {
	validateFieldName = (label ? label : oData.name);
	strSSN = trim(oData.value);

	if (strSSN == '') {return ''}

	strSSN = strSSN.replace(/\D/g, '');

	if (strSSN.length <  9 ) {
	  alert('Invalid ' + validateFieldName + ' entered.\n\nValid format: 222-33-4444');
	  return false;
	}
	if ( strSSN.match( /^(...)(..)(....)/ )) {
		strSSN = RegExp.$1 + '-' + RegExp.$2 + '-' + RegExp.$3 ;
		oData.value =  strSSN;
		return true;
	}
}  



function checkDate2K(oDate, label) {
	//valid formats: mmddyyyy,mmddyy, mm/dd/yy,  mm/dd/yyyy, mm-dd-yyyy, m/d/yyyy, m-d-yyyy, m.d.yyyy, etc 
	// where ./- or any other non-numeric charater is used to delim
	//this should cover most cases.
	//however, if user enters 01/32/2002, javascript will still return
	//	valid date as 02/01/2002 (ie, 01/31/2002 + 1 day)		
	validateFieldName = (label ? label : oDate.name);
	strDate = oDate.value;
	var dDate = new Date();
	var dCurYear = dDate.getFullYear();
	dCurYear =  new String(dCurYear);


	if (trim(strDate) == '') {
		oDate.value = '';
		return true;
	}

	if (strDate.match(/^\d{1,2}\D{1}\d{1,2}\D{1}\d{4}$/)) {
		var testDate = new Date(strDate);
	}
	if (strDate.match(/^\d{1,2}\D{1}\d{1,2}\D{1}\d{2}$/)) {
		var testDate = new Date(strDate);

		//add 100 year first. if it's greater than current date, then move back
		if (testDate.setYear(testDate.getFullYear() + 100) > dDate) {
			testDate.setYear(testDate.getFullYear() - 100);
		}

		
	}
	if (strDate.match(/^\d{1,2}[/\-]{1}\d{1,2}$/)) {
		var testDate = new Date();
		var testyear = testDate.getFullYear();		
		var testDate = new Date(strDate + '/' + testyear);
	}

	if (strDate.match(/^\d{6}$/)) {
		var testmonth = strDate.substring(0,2);
		var testdate  = strDate.substring(2,4);
		var testyear  = strDate.substring(4,6);

		if ('20' + testyear > dCurYear) {
			testyear = '19' + testyear;
		} else {
			testyear = '20' + testyear;
		}

		var testDate  = new Date(testyear, testmonth-1, testdate );
	}

	if (strDate.match(/^\d{8}$/)) {
		var testmonth = strDate.substring(0,2);
		var testdate  = strDate.substring(2,4);
		var testyear  = strDate.substring(4,8);
		//if ('20' + testyear > dCurYear) {
		//	testyear = '19' + testyear;
		//} else {
		//	testyear = '20' + testyear;
		//}
		var testDate  = new Date(testyear, testmonth-1, testdate);
	}

	if (isNaN(testDate)) {
		alert('Invalid ' + validateFieldName + ' entered.\n\nValid formats are:\nmm/dd/yyyy\nmmddyyyy');
		oDate.focus();
		oDate.select();
		return false;
	} else if (testDate.getFullYear() > maxYear || testDate.getFullYear() < minYear) {
		alert('Please enter year between ' + minYear + ' and ' + maxYear);
		oDate.focus();
		oDate.select();
		return false;
	} else {
		oDate.value =  (testDate.getMonth() + 1) + '/' + testDate.getDate() + '/' + testDate.getFullYear();
		return true;
	}
}



function checkEmail(oData) {
	//One or more characters before the "@"
	//A sequence of letters, numbers, and periods, which are all valid domain or IP address characters
	//removed: A period followed by a 2-3 letter suffix
	//updated:: A period followed by a 2-4 letter suffix (include .info)
	var str = trim(oData);
	if (str == '') {
		oData.value = '';
		return;
	}

	//validateFieldName = (label ? label : oData.name);
	if (str.match(/^.+\@[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4})$/)) {
		return true
	} else {
		//alert('Invalid ' + validateFieldName + ' entered.');
		return false;
	}
}




function checkZip(oData, label) {
	validateFieldName = (label ? label : oData.name);
	strZip = trim(oData.value);

	if (strZip == '') {return ''}

	strZip = strZip.replace(/\D/g, '');
	
	if (strZip.length !=5 && strZip.length !=9) {
		alert('Invalid ' + validateFieldName + ' entered.\n\nvalid formats are:  00000 or 00000-0000');
		oData.focus();
		return false;
	}

	if ( strZip.match( /^(\d{5})(\d{4})/ )) {
		strZip = RegExp.$1 + '-' + RegExp.$2 ;
	} else 
	if (strZip.match( /^\d{5}$/ )) {
		return true;
	}

	oData.value =  strZip;
	return true;

} 

function isZip(oData) {
	strZip = trim(oData.value);

	strZip = strZip.replace(/\D/g, '');
	
	if (strZip.length !=5 && strZip.length !=9) {
		return false;
	}
	return true;
}