//////////////////////////////////////////////
var	class_input_warning	=	"input_warning";
var	class_input_correct	=	"input_correct";
var	class_input_need		=	"input_need";
var class_input_ghosted = "input_ghosted";
//////////////////////////////////////////////
function DateTester(aIso){
	this.mIso	=	aIso;
	this.test	=	DateTester_test;
}

function authTest(formName){
	if(document.forms[formName].authorize[0].checked){
		check_Form(formName);
	}else{
		alert('Sorry. This transaction cannot continue until it has been authorized.');
	}
}	

function DateTester_test(aValue){
	var	lPattern = null;
	var	lYear	=	null;
	var	lDay = null;
	var	lMonth = null;

	if ( this.mIso ){
		
		// ISO:	yyyy-mm-dd
		lPattern = new RegExp(/^(\d{4})[\-](0?[0-9]|1[0-2])[\-](0?[0-9]|1[0-9]|2[0-9]|3[0-1])$/);
		lMatchArray	=	lPattern.exec(aValue);
		if ( lMatchArray ==	null ){
			return false;
		}	
		
		lYear	 = lMatchArray[1];
		lMonth = lMatchArray[2];
		lDay	 = lMatchArray[3];

	}else{

		// USA:	mm/dd/yyyy or	mm-dd-yyyy		
		lPattern = new RegExp(/^(0?[0-9]|1[0-2])[\-\/](0?[0-9]|1[0-9]|2[0-9]|3[0-1])[\-\/](\d{4})$/);
		lMatchArray	=	lPattern.exec(aValue);	
		if ( lMatchArray ==	null ){
			return false;
		}	
		
		lMonth = lMatchArray[1];
		lDay	 = lMatchArray[2];
		lYear	 = lMatchArray[3];
		
	}
	
	if (lMonth < 1 ||	lMonth > 12	|| lDay	<	1	|| lDay	>	31 ){
		return false;
	}

	lValidLengths	=	new	Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31	);
	
	if ( lYear / 4 ==	parseInt(	lYear	/	4	)	){
		lValidLengths[1] = 29;
	}

	if ( lValidLengths[lMonth	-	1] < lDay	){
		return false;
	}
	
	return true;
	
}

function MM_findObj(n, d)	{	//v4.01
	var	p,i,x;	
	if (!d)	d=document;	
	if ((p=n.indexOf("?"))>0&&parent.frames.length)	{
			d=parent.frames[n.substring(p+1)].document;	
		n=n.substring(0,p);
	}
		if (!(x=d[n])&&d.all)	x=d.all[n];	
	for	(i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
		for	(i=0;!x&&d.layers&&i<d.layers.length;i++)	
		x=MM_findObj(n,d.layers[i].document);
		if (!x &&	d.getElementById)	
		x=d.getElementById(n); 
	return x;
}
												
function _getRegEx(element)	{
	regex	=	MM_findObj(element).getAttribute('re');
	if (regex	== null) {
		regex	=	MM_findObj(element).getAttribute('nre');
		if (regex	== null) {
			return null;
		}
	}
	retype = typeof(regex);
	if (retype.toUpperCase() ==	"FUNCTION")
		return regex;
	else if	(retype.toUpperCase()	== "STRING") {
		switch (regex) {
			
			case "email":
				return new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/);
			
			case "tel":
				return new RegExp(/^1?[\-	]?\(?\d{3}\)?[\- ]?\d{3}[\-	]?\d{4}$/);

			case "date":
				return new DateTester(false);	// mm/dd/yyyy	or mm-dd-yyyy
				
			case "perc":
				return new RegExp(/^(\d{1,3})$/);
							
			case "account":
				return new RegExp(/^(\d{6})$/);

			case "text":
				return new RegExp(/^([a-z]|[A-Z]|-|,|\.|\s|\d|_|&|'|\!|\?)+$/);

			case "money":
				return new RegExp(/^\d+([\.]\d\d)?$/);

			case "ssn":
				return new RegExp(/^(\d){3}\-\d{2}\-\d{4}$/);

			case "postalzip":
				if(MM_findObj(element).value.length	== 6 ||	MM_findObj(element).value.length ==	7) {
					return new RegExp(/^[a-zA-Z]\d[a-zA-Z] ?\d[a-zA-Z]\d$/);
				}	else if(MM_findObj(element).value.length ==	5	|| MM_findObj(element).value.length	== 10) {
					return new RegExp(/^\d{5}(\-\d{4})?$/);
				}	else {
					return new RegExp(/^\d{5}(\-\d{4})?$/);
				}
				break;

			default:
				nBegin = 0;	nEnd = regex.length-1;
				if (regex.charAt(0)	== "/")	nBegin=1;
				if (regex.charAt(regex.length-1) ==	"/") nEnd=regex.length-1;
				return new RegExp(regex.slice(nBegin,	nEnd));

		}
	}
}


function check_Form(thisForm)	{
	check_Form_Base(thisForm, true);
}

function is_form_errors()	{
	return check_Form_Base(false);
}

function check_Form_Base(thisForm, aAutoSubmit)	{
	var	iError;
	iError=0;

	if (document.forms.length	>	0) {
		var	field	=	document.forms[thisForm];
		for	(i = 0;	i	<	field.length;	i++) {
			if (field.elements[i].disabled == false) {
				if ((field.elements[i].type	== "text") ||	(field.elements[i].type	== "password") 
				|| (field.elements[i].type ==	"textarea") ) {
										
					this.rf	=	MM_findObj(field.elements[i].name).getAttribute('re');
					if (this.rf	!= null) {
						this.regexp2 = this._getRegEx(field.elements[i].name);
						if (MM_findObj((field.elements[i].name)).value ==	"")	{
							iError=iError+1;
							MM_findObj(field.elements[i].name).className = class_input_warning;
						} else {
							if (regexp2.test(MM_findObj(field.elements[i].name).value)==true)	{
								MM_findObj(field.elements[i].name).className = class_input_correct;
							}	else {
								iError=iError+1;
								MM_findObj(field.elements[i].name).className = class_input_warning;
							}
						}
					}
				}
			}
		}
	}

	if (iError==0) {
		if ( aAutoSubmit ){
			try	{
				document.forms[thisForm].submit();
			}	catch	(error)	{
				alert('An	error	occured	while	submitting the form.');
			}
		}
	}	else {
		alert('One or more fields are not correct.');
	}
		
	return iError;
}


function check_Field(FieldName)	{
	this.regexp	=	this._getRegEx(FieldName);

	this.rf	=	MM_findObj(FieldName).getAttribute('re');
	if (this.rf	== null) {
			if (regexp.test(MM_findObj(FieldName).value)==true)	{
			MM_findObj(FieldName).className=class_input_correct;
			return true;
			}
	}	else {
		this.regexp2 = this._getRegEx(FieldName);
			if (MM_findObj(FieldName).value	== "") {
			MM_findObj(FieldName).className=class_input_warning;
			return false;
		}
		else {
			if (regexp2.test(MM_findObj(FieldName).value)==true) {
				MM_findObj(FieldName).className=class_input_correct;
				return true;
			}	else {
				MM_findObj(FieldName).className=class_input_warning;
				return false;
			}
		}
	}
}


function check_Select(FieldName){
	
	this.rf	=	MM_findObj(FieldName).getAttribute('re');
	fieldValue = MM_findObj(FieldName).options[MM_findObj(FieldName).selectedIndex].value;
	
	if (this.rf	== null) {
			if (regexp.test(fieldValue) == true)	{
			MM_findObj(FieldName).className = class_input_correct;
			return true;
			}
	}	else {
		this.regexp2 = this._getRegEx(FieldName);
			if (fieldValue	== "") {
			MM_findObj(FieldName).className=class_input_warning;
			return false;
		}
		else {
			if (regexp2.test(fieldValue) == true) {
				MM_findObj(FieldName).className=class_input_correct;
				return true;
			}	else {
				MM_findObj(FieldName).className=class_input_warning;
				return false;
			}
		}
	}	
}

function markNeed(thisForm)	{
	if (document.forms.length	>	0) {
		var	field	=	document.forms[thisForm];
		for	(i = 0;	i	<	field.length;	i++) {
			if (field.elements[i].disabled == false) {
				if ((field.elements[i].type	== "text") ||	(field.elements[i].type	== "password") 
				|| (field.elements[i].type ==	"textarea")	|| (field.elements[i].type.toString().charAt(0)	== "s")) {
					try	{
							this.rf	=	MM_findObj(field.elements[i].name).getAttribute('re');
							if (this.rf	== null) {

							}	else {
							MM_findObj(field.elements[i].name).className = class_input_need;
							}
					}	catch	(error)	{
					
					}
				}
			}
		}
	}
}

function getFixed(val, places, isRounded,	isZeros){
	if (val	== 0 &&	isZeros) {
		return val.toFixed(places);
	}

	var	factor;
	var	i;
	factor = 1;
	for	(i=0;	i<places;	i++) {
		factor *=	10;
	}
	val	=	(val * factor) + (isRounded	?	0.50000000001	:	0.00000000001);
	val	=	isRounded	?	Math.floor(val)	:	(val < 0 ? Math.ceil(val)	:	Math.floor(val));
	var	strVal = val.toString();
	
	var	zeros	=	'';
	for	(i=0;	i<places;	i++) {
		if (strVal.charAt(strVal.length	-	1	-	i) ==	'0') {
			zeros	+= ''	+	'0';
			if ((i + 1)	== places) zeros = '.' + zeros;
		} else {
			break;
		}
	}
	
	val	=	val	/	factor;
	if (isZeros) {
		return val + ''	+	zeros;
	}

	return val;
}

function isDate(p_Expression){
	return !isNaN(new	Date(p_Expression));		// <<--- this	needs	checking
}

// NOT SUPPORTED:	firstdayofweek and firstweekofyear (defaults for both)
function dateDiff(p_Interval,	p_Date1, p_Date2,	p_firstdayofweek,	p_firstweekofyear){
	
	if(!isDate(p_Date1)){return	"invalid date: '"	+	p_Date1	+	"'";}
	if(!isDate(p_Date2)){return	"invalid date: '"	+	p_Date2	+	"'";}
	var	dt1	=	new	Date(p_Date1);
	var	dt2	=	new	Date(p_Date2);

	// get ms	between	dates	(UTC)	and	make into	"difference" date
	var	iDiffMS	=	dt2.valueOf()	-	dt1.valueOf();
	var	dtDiff = new Date(iDiffMS);

	// calc	various	diffs
	var	nYears	=	dt2.getUTCFullYear() - dt1.getUTCFullYear();
	var	nMonths	=	dt2.getUTCMonth()	-	dt1.getUTCMonth()	+	(nYears!=0 ? nYears*12 : 0);
	var	nQuarters	=	parseInt(nMonths/3);	//<<-- different than	VBScript,	which	watches	rollover not completion
	
	var	nMilliseconds	=	iDiffMS;
	var	nSeconds = parseInt(iDiffMS/1000);
	var	nMinutes = parseInt(nSeconds/60);
	var	nHours = parseInt(nMinutes/60);
	var	nDays	 = parseInt(nHours/24);
	var	nWeeks = parseInt(nDays/7);


	// return	requested	difference
	var	iDiff	=	0;		
	switch(p_Interval.toLowerCase()){
		case "yyyy": return	nYears;
		case "q":	return nQuarters;
		case "m":	return nMonths;
		case "y":			// day of	year
		case "d":	return nDays;
		case "w":	return nDays;
		case "ww":return nWeeks;		// week	of year	// <-- inaccurate, WW	should count calendar	weeks	(# of	sundays) between
		case "h":	return nHours;
		case "n":	return nMinutes;
		case "s":	return nSeconds;
		case "ms":return nMilliseconds;	// millisecond	// <-- extension for JS, NOT available in	VBScript
		default: return	"invalid interval: '"	+	p_Interval + "'";
	}
}

function padDigits(n, totalDigits){ 
	n = n.toString(); 
	var pd = ''; 
	if (totalDigits > n.length){ 
		for (i=0; i < (totalDigits-n.length); i++){ 
			pd += '0'; 
		} 
	} 
	return pd + n.toString(); 
} 

function formatDate(thisField){

	thisStr 		= thisField.value;
	thisStr 		= thisStr.replace(/\/|\-|\ |\./g, 'x');
	thisArray 	= thisStr.split('x');
		
	if(thisArray.length == 3){
		
		thisMonth 	= padDigits(thisArray[0], 2);
		thisDay 		= padDigits(thisArray[1], 2);
		thisYear 		= thisArray[2];
		thisField.value = thisMonth + "/" + thisDay + "/" + thisYear;
				
	} else {
		
		thisStr = thisStr.replace('x', '');	
		if(thisStr.length == 8){
			
			thisMonth 	= thisStr.substring(0, 2);
			thisDay 		= thisStr.substring(2, 4);
			thisYear 		= thisStr.substring(4, 8);
			thisField.value = thisMonth + "/" + thisDay + "/" + thisYear;
		
		} else {
			
			thisField.className = 'input_warning';
			
		}
	}
}

function formatSSN(thisField){

	thisStr 		= thisField.value;
	thisStr 		= thisStr.replace(/\/|\-|\ |\./g, 'x');
	thisArray 	= thisStr.split('x');
		
	if(thisArray.length == 3){
		
		setOne 		= thisArray[0];
		setTwo 		= thisArray[1];
		setThree 	= thisArray[2];
		thisField.value = setOne + "-" + setTwo + "-" + setThree;
				
	} else {
		
		thisStr = thisStr.replace('x', '');
		if(thisStr.length == 9){
			
			setOne 		= thisStr.substring(0, 3);
			setTwo 		= thisStr.substring(3, 5);
			setThree 	= thisStr.substring(5, 9);
			thisField.value = setOne + "-" + setTwo + "-" + setThree;
		
		} else {
			
			thisField.className = 'input_warning';
			
		}
	}
}

function formatPhone(thisField){

	thisStr 		= thisField.value;
	thisStr 		= thisStr.replace(/\/|\(|\)/g, '');	
	thisStr 		= thisStr.replace(/\ |\-|\./g, 'x');
	thisArray 	= thisStr.split('x');
		
	if(thisArray.length == 3){
		
		setOne 		= thisArray[0];
		setTwo 		= thisArray[1];
		setThree 	= thisArray[2];
		thisField.value = setOne + "-" + setTwo + "-" + setThree;

		thisField.className = 'input_correct';
				
	} else {
		
		thisStr = thisStr.replace('x', '');
		if(thisStr.length == 10){
			
			setOne 		= thisStr.substring(0, 3);
			setTwo 		= thisStr.substring(3, 6);
			setThree 	= thisStr.substring(6, 10);
			thisField.value = setOne + "-" + setTwo + "-" + setThree;
		
		} else {
			
			thisField.className = 'input_warning';
			
		}
	}
}