var CurrentlyValidatingControl = '';

function BeginValidation(ctl){
	if (CurrentlyValidatingControl == ''){
		CurrentlyValidatingControl = ctl.id;
	}
	return (CurrentlyValidatingControl == ctl.id);
}
function FinishValidation(){
	CurrentlyValidatingControl = '';
}
function FormatDateAndTime(ctl){
	if(!BeginValidation(ctl)){ return true; }
	var strDate = new String(ctl.value);
	switch (strDate.length) {	
		case 0:
			//FinishValidation();
			return true;
			break
		case 6:
			var strMonth = strDate.slice(0,2);
			var strDay = strDate.slice(2,4);
			var strYear = '20' + strDate.slice(4);
			break
		case 8:
			var strMonth = strDate.slice(0,2);
			var strDay = strDate.slice(2,4);
			var strYear = strDate.slice(4);
			break
		case 10:
			if ((strDate.slice(2,3) == '/') && (strDate.slice(5,6) == '/')){
				var strMonth = strDate.slice(0,2);
				var strDay = strDate.slice(3,5);
				var strYear = strDate.slice(6);
			} else {
				InValidDateMsg(ctl);
				//FinishValidation();
				return false;
			}
			break
		case 19:
			if ((strDate.slice(2,3) == '/') && (strDate.slice(5,6) == '/')){
				var strMonth = strDate.slice(0,2);
				var strDay = strDate.slice(3,5);
				var strYear = strDate.slice(6);
			} else {
				InValidDateMsg(ctl);
				//FinishValidation();
				return false;
			}
			break
		default:
			InValidDateMsg(ctl);
			//FinishValidation();
			return false;
			break
	}	
	if (IsDateValid(strMonth, strDay, strYear)){
		ctl.value = strMonth + '/' + strDay + '/' + strYear;
	} else {
		InValidDateMsg(ctl);
		//FinishValidation();
		return false;
	}
	FinishValidation();
	return true;
}
function FormatDate(ctl){
	if(!BeginValidation(ctl)){ return true; }
	var strDate = new String(ctl.value);
	switch (strDate.length) {	
		case 0:
			//FinishValidation();
			return true;
			break
		case 4:
			var strMonth = '0' + strDate.slice(0,1);
			var strDay = '0' + strDate.slice(1,2);
			var strYear = '20' + strDate.slice(2);
			break
		case 5:
			var strMonth = '0' + strDate.slice(0,1);
			var strDay = strDate.slice(1,3);
			var strYear = '20' + strDate.slice(3);
			break
		case 6:
			var strMonth = strDate.slice(0,2);
			var strDay = strDate.slice(2,4);
			var strYear = '20' + strDate.slice(4);
			break
		case 8:
			var strMonth = strDate.slice(0,2);
			var strDay = strDate.slice(2,4);
			var strYear = strDate.slice(4);
			break
		case 10:
			if ((strDate.slice(2,3) == '/') && (strDate.slice(5,6) == '/')){
				var strMonth = strDate.slice(0,2);
				var strDay = strDate.slice(3,5);
				var strYear = strDate.slice(6);
			} else {
				InValidDateMsg(ctl);
				//FinishValidation();
				return false;
			}
			break
		default:
			InValidDateMsg(ctl);
			//FinishValidation();
			return false;
			break
	}	
	if (IsDateValid(strMonth, strDay, strYear)){
		ctl.value = strMonth + '/' + strDay + '/' + strYear;
	} else {
		InValidDateMsg(ctl);
		//FinishValidation();
		return false;
	}
	FinishValidation();
	return true;
}
function IsDateValid(strMonth, strDay, strYear){
	var dteTest = new Date(strYear, strMonth - 1, strDay);
	return((dteTest.getDate() == strDay) && (dteTest.getMonth() == (strMonth - 1)) && (dteTest.getFullYear() == strYear));
}
function InValidDateMsg(ctl){
	var msg = 'The date that you entered is not valid.\n';
	msg += 'Please provide a valid date in \"mm/dd/yyyy\" format, i.e.:\n\n';
	msg += '2-digit month\n2-digit day\n4-digit year\n\n';
	msg += '(You may omit the \"/\" characters if you like.)';
	alert(msg);
	CtlSetFocus(ctl);
}
function CtlTestForValidNumber(ctl) {
//	if(!BeginValidation(ctl)){ return true; }
	if (isNaN(ctl.value) && (ctl.value > ' ')) {
		alert('Please enter a valid number');
		CtlSetFocus(ctl);
		return false;
	}
	return true;
}
function CtlTestMaxValueExceeded(ctl, maxValue, decimalPlaces){
//	if(!BeginValidation(ctl)){ return true; }
	if (ctl.value > ' ') {
		if (CtlTestForValidNumber(ctl, true)){
			if (parseFloat(ctl.value) > maxValue) {
				alert('The value entered (' + ctl.value + ') exceeds the total possbile value (' + maxValue + '). \nPlease reenter. ');
				CtlSetFocus(ctl);
				return false;
			} else {
				CtlSetNumberDecimalFormat(ctl, decimalPlaces);
			}
		} else {
			return false;
		}
	}
	FinishValidation();
	return true;
}
function CtlTestMinValueExceeded(ctl, minValue, decimalPlaces){
//	if(!BeginValidation(ctl)){ return true; }
	if (ctl.value > ' ') {
		if (CtlTestForValidNumber(ctl, true)){
			if (parseFloat(ctl.value) < minValue) {
				alert('The value entered (' + ctl.value + ') is less then a reasonable value (' + minValue + '). \nPlease reenter. ');
				CtlSetFocus(ctl);
				return false;
			} else {
				CtlSetNumberDecimalFormat(ctl, decimalPlaces);
			}
		} else {
			return false;
		}
	}
	FinishValidation();
	return true;
}
function clearZero(CellNum) {
	if (CellNum == "0") {
		return "";
	} else if(CellNum == 0) {
		return "";
	} else return CellNum;
}
function clearCell(CellNum) {
	if (CellNum == "0.00") {
		return "";
	} else if(CellNum == 0) {
		return "";
	} else return CellNum;
}
function CtlSetValueToZero(CellNum) {
	if (CellNum == ""){
		return "0";
	} else return CellNum;
} 
/**************************************************************************************************************
Replace this with formatNumber, below
**************************************************************************************************************/
function formatCell(Num) {
	if (Num != "") {      
		if ((Num.toString().length - Num.toString().lastIndexOf('.')) > 0) {
       		var Rounder = Math.pow(10, 4);
			var checkNum = Math.round(Num * Rounder) / Rounder;
			if ((checkNum.toString().lastIndexOf('.') != -1) && (checkNum.toString().length - checkNum.toString().lastIndexOf('.')) == 4) {
				return Math.round(Num * Rounder) / Rounder + "0";
			} else if ((checkNum.toString().lastIndexOf('.') == (-1)) && (checkNum.toString().length - checkNum.toString().lastIndexOf('.')) == 4) {
				return Math.round(Num * Rounder) / Rounder + ".0000";
			} else if (checkNum.toString().lastIndexOf('.') != -1) {
	       		return Math.round(Num * Rounder) / Rounder;
			} else
				return Math.round(Num * Rounder) / Rounder + ".0000";
		}
		else return Num;
	}
	else return "0.0000";
}
function CtlFormatNumber(Num,decimalPlaces) {
	if (isNaN(Num) && (Num > ' ')) {
		alert('Please enter a valid number');
		return false;
	}
	if (Num == ""){
		var Num = 0.00000000;
	}
	var multiplier = Math.pow(10, decimalPlaces);
	var roundedNum = Math.round(Num * multiplier) / multiplier;
	var roundedNumString = roundedNum.toString();
	var decimalLoc = roundedNumString.indexOf('.');
	if (decimalLoc != -1) {
		curDecimalPlaces = roundedNumString.length - decimalLoc - 1;
	} else {
		// no decimal in number
		curDecimalPlaces = 0;
		roundedNumString = (decimalPlaces ? roundedNumString + '.' : roundedNumString);
	}
	for (var i = 0; i < (decimalPlaces - curDecimalPlaces); i++ ){
		roundedNumString += '0';
	}
	return roundedNumString;
}
function CtlSetNumberDecimalFormat(ctl, decimalPlaces){
	if (CtlTestForValidNumber(ctl)){
		var num = ((ctl.value != '') ? ctl.value : 0);
		var multiplier = Math.pow(10, decimalPlaces);
		var roundedNum = Math.round(num * multiplier) / multiplier;
		var roundedNumString = roundedNum.toString();
		var decimalLoc = roundedNumString.indexOf('.');
		if (decimalLoc != -1) {
			curDecimalPlaces = roundedNumString.length - decimalLoc - 1;
		} else {
			// no decimal in number
			curDecimalPlaces = 0;
			roundedNumString = (decimalPlaces ? roundedNumString + '.' : roundedNumString);
		}
		for (var i = 0; i < (decimalPlaces - curDecimalPlaces); i++ ){
			roundedNumString += '0';
		}
		ctl.value = roundedNumString;
	}
}
function CtlSubtractTwoNumbersStoreInCtl(x,y,z) {
	if (z > y) {
		alert('The value entered (' + z + ') exceeds the total possbile value (' + y + '). \nPlease reenter. ');
	} else {
		x.value = y - z; 
	}
}
function CtlAddTwoNumbersStoreInCtl(x,y,z) {
	if (CtlTestForValidNumber(x)){
		if (CtlTestForValidNumber(y)){
			z.value = parseFloat(x.value) + parseFloat(y.value);
		}
	}
}
function CtlMultiplyTwoNumbersStoreInCtl(x,y,z) {
	if (CtlTestForValidNumber(x)){
		if (CtlTestForValidNumber(y)){
			z.value = parseFloat(x.value) * parseFloat(y.value);
		}
	}
}
function CtlCheckForSelection(ctl,label){
	if (ctl.length == 0) {
		alert('Please select a ' + label);
		CtlSetFocus(ctl);
		return false;
	}
}
function FormatSocial(ssn) {
	if (ssn.length > 0){
		var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
		var numDashes = ssn.split('-').length - 1;
		if (matchArr == null || numDashes == 1) {
			alert('Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.');
			msg = "does not appear to be valid";
		} else {
			if (parseInt(matchArr[1],10)==0) {
				alert("Invalid SSN: SSN's can't start with 000.");
				msg = "does not appear to be valid";
			}
		}
	}
}
/**************************************************************************************************************
Phone functions
**************************************************************************************************************/
function FormatPhone(ff,pphh,m){
	p1=m;
	var formName = ff;
	var fieldName = pphh;
	ValidatePhone(ff,pphh,m)
}
function ValidatePhone(ff,pphh,m){
	p=p1.value
	var theFormField = eval("document." + ff + "." + pphh);
	if(p.length==3){
		pp=p;
		d4=p.indexOf('(')
		d5=p.indexOf(')')
		if(d4==-1){
			pp="("+pp;
		}
		if(d5==-1){
			pp=pp+") ";
		}	
		theFormField.value = "";
		theFormField.value = pp;
	}
	if(p.length>3){
		d1=p.indexOf('(')
		d2=p.indexOf(') ')
		if (d2==-1){
			l30=p.length;
			p30=p.substring(0,4);
			p30=p30+") "
			p31=p.substring(4,l30);
			pp=p30+p31;
			theFormField.value = "";
			theFormField.value = pp;
		}
	}
	if(p.length>6){
		p11=p.substring(d1+1,d2);
		if(p11.length>3){
			p12=p11;
			l12=p12.length;
			l15=p.length
			p13=p11.substring(0,3);
			p14=p11.substring(3,l12);
			p15=p.substring(d2+1,l15);
			theFormField.value = "";
			pp="("+p13+") "+p14+p15;
			theFormField.value = pp;
		}
		l16=p.length;
		p16=p.substring(d2+2,l16);
		l17=p16.length;
		if(l17>3&&p16.indexOf('-')==-1){
			p17=p.substring(d2+2,d2+5);
			p18=p.substring(d2+5,l16+1);
			p19=p.substring(0,d2+2);
			pp=p19+p17+"-"+p18;
			theFormField.value = "";
			theFormField.value = pp;
		}
	}
}
function StripWhitespace(strIn){
	(strIn.replace(/^\W+/,'')).replace(/\W+$/,'');
	return strIn;
}
function CtlCompareDate(ctl, datCompare, operator, dayOffset, dateDescription){
	if (datCompare == ''){
		return true;
	}
	dayOffset = ((typeof(dayOffset) == 'undefined') ? 0 : dayOffset);
	var datTest = new Date(ctl.value);
	var datCompareDate = new Date(datCompare);
	var datCompareOffset;
	if (dayOffset > 0){
		datCompareOffset = new Date(datCompareDate.getYear(), datCompareDate.getMonth(), (datCompareDate.getDate() + dayOffset));
	} else {
		datCompareOffset = new Date(datCompareDate);
	}
	var MonthFormat = datCompareOffset.getMonth() + 1;
	var DayFormat = datCompareOffset.getDate();
	var YearFormat = ((datCompareOffset.getYear() < 1000) ? datCompareOffset.getYear() + 1900 : datCompareOffset.getYear());
	var CompareDateString = MonthFormat + '/' + DayFormat + '/' + YearFormat;
	dateDescription = ((typeof(dateDescription) == undefined) ? '' : ' the ' + dateDescription + ', ');
	var result;
	var message;
	switch(operator){
		case 'gt':
			message = 'The date entered must be greater than ' + dateDescription + CompareDateString;
			result = (datTest > datCompareOffset);
			break;
		case 'gte':
			message = 'The date entered must be greater than or equal to ' + dateDescription + CompareDateString;
			result = (datTest >= datCompareOffset);
			break;
		case 'eq':
			message = 'The date entered must be equal to ' + dateDescription + CompareDateString;
			result = (datTest == datCompareOffset);
			break;
		case 'lt':
			message = 'The date entered must be less than ' + dateDescription + CompareDateString;
			result = (datTest < datCompareOffset);
			break;
		case 'lte':
			message = 'The date entered must be less than or equal to ' + dateDescription + CompareDateString;
			result = (datTest <= datCompareOffset);
			break;
	}
	
	if(!result){
		alert(message);
		CtlSetFocus(ctl);
	}
	return result;
}
// 
// 	1 point for each character over 6 in length
// 	1 point for being mixed case, upper and lower
// 	1 point for including both numbers and letters
// 	1 point for including punctuation
// 
// 	Additionally, the score is automatically 0 if the password contains any of the following:
// 		User's first name
// 		User's last name
// 		User's email address
// 
var lc = /[a-z]{1}/; // lowercase letters
var uc = /[A-Z]{1}/; // uppercase letters
var nm = /[0-9]{1}/; // numbers
var un = /[^A-Za-z0-9]{1}/; // upper- and lower-case letters and numbers
var sp = "!@#$%^&*()_+-={[}]\<>?/"; // special characters

function checkStrength (pswd,firstname,lastname,LoginUserId) {
	if (ValidLength(pswd, true)){
		document.getElementById('pwdmsg').value = '';
		if (pswd.value == firstname) {
			document.getElementById('pwdmsg').value = "Password may not be your first name";	
			return false;
		} else {
			if (pswd.value == lastname) {
				document.getElementById('pwdmsg').value = "Password may not be your last name";	
				return false;
			} else {
				if (pswd.value == LoginUserId) {
					document.getElementById('pwdmsg').value = "Password may not be your Login User Id";	
					return false;
				} else {
					var pwd = pswd.value;
					var score = 0;
					if (pwd.match(lc) && pwd.match(uc)) score = score + 1;
					if (pwd.match(nm) && (pwd.match(lc) || pwd.match(uc))) score = score + 2;
					for (var i=0; i<pwd.length; i++) {
						temp = "" + pwd.substring(i, i+1);
						if (sp.indexOf(temp) == "0") score = score + 2;;
					}
					document.getElementById('pw2').disabled=false;
					document.getElementById('pw2').focus();
					if (score < 2){
						if (pwd.length > 10){
							document.getElementById('pwdmsg').value = 'Medium password';
						} else {
							document.getElementById('pwdmsg').value = 'Weak password';
						}
					} else {
						if (score < 3){
							if (pwd.length > 10){
								document.getElementById('pwdmsg').value = 'Strong password';
							} else {
								document.getElementById('pwdmsg').value = 'Weak password';
							}
						} else {
							document.getElementById('pwdmsg').value = 'Strong password';
						}
					}
				}
			}
		}
	} else {
		return false;
	}
}
function ValidLength(pswd){
	if (pswd.value.length < 6) {
		document.getElementById('pwdmsg').value = "Password must be at least 6 characters";
		return false;
	} else {
		return true;;
	}
}
function CheckPswdConfirm(){
	if (document.getElementById('pw1').value == document.getElementById('pw2').value) {
		document.getElementById('SubmitForm').disabled=false;
	} else { 
		document.getElementById('pwdmsg').value = 'Confirmation password not equal to new password';
		document.getElementById('SubmitForm').disabled=true;
	}	
}
function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.getElementById('email').value;
   if(reg.test(address) == false) {
      alert('Invalid Email Address');
	  document.getElementById('email').focus();
      return false;
   }
}