/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isEmpty(field) {
	return (field == null || field == undefined || !field.value.match("\\S"));
}

function isEmptyPhone(f1, f2, f3) {
	return (isEmpty(f1) && isEmpty(f2) && isEmpty(f3));
}
	
function checkText(field) {
	return (!isEmpty(field));
}

function checkTextWithFlag(field) {
	var result = checkText(field);
	if (!result)
		flagField(field);
	else
		unflagField(field);
	return result;
}

function checkNoText(field) {
	return (isEmpty(field));
}

function checkPhone(f1, f2, f3) {
	return !(f1 == null || f2 == null || f3 == null || !f1.value.match("[0-9][0-9][0-9]") || !f2.value.match("[0-9][0-9][0-9]") || !f3.value.match("[0-9][0-9][0-9][0-9]"));
}

function checkPhoneWithFlag(f1, f2, f3) {
	unflagField(f1);
	unflagField(f2);
	unflagField(f3);
	if (f1 == null || !f1.value.match("[0-9][0-9][0-9]")) {
		flagField(f1);
		return false;
	}
	else if (f2 == null || !f2.value.match("[0-9][0-9][0-9]")) {
		flagField(f2);
		return false;
	}
	else if (f3 == null || !f3.value.match("[0-9][0-9][0-9][0-9]")) {
		flagField(f3);
		return false;
	}
	return true;
}

function checkEmail(field) {
	// regex from Kalani p. 171
	return field.value.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
	// without two @ validation("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
}

function checkEmailWithFlag(field) {
	var result = checkEmail(field);
	if (!result)
		flagField(field);
	else
		unflagField(field);
	return result;
}

function checkRadio(field) {
	if (field == null)
		return false;
	for (var i = 0; i < field.length; i++)
		if (field[i].checked)
			return true;
	return false;
}

function checkRadioWithFlag(field) {
	var result = checkRadio(field);
	if (!result)
		flagField(field);
	else
		unflagField(field);
	return result;
}

function checkSelect(field) {
	return !(valueOfSelect(field) == "");
}

function checkSelectWithFlag(field) {
	var result = checkSelect(field);
	if (!result)
		flagField(field);
	else
		unflagField(field);
	return result;
}

function valueOfSelect(field) {
	return field.options[field.selectedIndex].value;
}

function valueOfRadio(field) {
	for (var i = 0; i < field.length; i++)
		if (field[i].checked)
			return field[i].value;
	return undefined;
}

function checkDate(field, required) {
	if (required && isEmpty(field)) {
		field.value = 'MM/DD/YYYY';
		return false;
	}
	else if (!required && isEmpty(field))
		return true;
	else if (!field.value.match("^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d$")) {
		return false;
	}
	return true;
}

function checkNumber(field, minimum, maximum) {
	if (isEmpty(field)) {
		return false;
	}
	if (isNaN(parseInt(field.value))) {
		return false;
	}
	var n = parseInt(field.value);
	if (minimum != NaN && n < minimum) {
		return false;
	}
	if (maximum != NaN && n > maximum) {
		return false;
	}
	return true;
}

function checkNumberWithFlag(field, minimum, maximum) {
	var result = checkNumber(field,minimum,maximum);
	if (!result)
		flagField(field);
	else
		unflagField(field);
	return result;
}

function defaultField(field, val) {
	if (isEmpty(field))
		field.value = val;
}

function flagField(field) {
	if (field.name == undefined) {	// fix for radio buttons
		flag = document.getElementById(field[0].name);
	}
	else {
		flag = document.getElementById(field.name);
	}
	flag.childNodes[0].nodeValue = "*";
}

function unflagField(field) {
	if (field.name == undefined) {	// fix for radio buttons
		flag = document.getElementById(field[0].name + "_flag");
	}
	else {
		flag = document.getElementById(field.name + "_flag");
	}
	flag.innerHTML = "&nbsp;";
}

function focusField(field) {
	if (field.name == undefined)
		field[0].focus();
	else
		field.focus();
}

function checkDate(day, month, year, required) {
	dayVal = parseInt(valueOfSelect(day));
	monthVal = parseInt(valueOfSelect(month));
	yearVal = parseInt(valueOfSelect(year));
	
	// if the date isn't required and ALL values aren't specified, we're okay.
	if (!required && isNaN(dayVal) && isNaN(monthVal) && isNaN(yearVal))
		return true;
	
	// whether the date is required or not, if any value isn't specified at this point, we're not okay.
	if (isNaN(dayVal) || isNaN(monthVal) || isNaN(yearVal))
		return false;
	
	if ((monthVal == 4 || monthVal == 6 || monthVal == 9 || monthVal == 11) && dayVal == 31)
		return false;

	if (monthVal == 2) { 
		// check for february 29th
		var isLeap = (yearVal % 4 == 0 && (yearVal % 100 != 0 || yearVal % 400 == 0));
		if (dayVal > 29 || (dayVal == 29 && !isLeap))
			return false;
	}

	return true;
}

function isPastDate(day, month, year) {
	dayVal = parseInt(valueOfSelect(day));
	monthVal = parseInt(valueOfSelect(month));
	yearVal = parseInt(valueOfSelect(year));
	var rightNow = new Date();

	if (yearVal > rightNow.getFullYear())
		return false;
	else if (yearVal < rightNow.getFullYear())
		return true;

	// because getMonth() returns 0..11
	if (monthVal > rightNow.getMonth() + 1)
		return false;
	else if (monthVal < rightNow.getMonth() + 1)
		return true;

	return (dayVal < rightNow.getDate());
}

function checkDateWithFlag(day, month, year, required) {
	var result = checkDate(day,month,year,required);
	if (!result) {
		flagField(day);
		flagField(month);
		flagField(year);
	}
	else {
		unflagField(day);
		unflagField(month);
		unflagField(year);
	}
	return result;
}
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr, doAlert){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		if (doAlert) {
			alert("Please enter a valid date.  The date format should be MM/DD/YYYY.");
		}
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		if (doAlert) {
			alert("Please enter a valid month.  The date format should be MM/DD/YYYY.");
		}
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		if (doAlert) {
			alert("Please enter a valid day.  The date format should be MM/DD/YYYY.");
		}
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		if (doAlert) {
			alert("Please enter a valid 4 digit year between " + minYear + " and " + maxYear + ".");
		}
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		if (doAlert) {
			alert("Please enter a valid date.  The date format should be MM/DD/YYYY.");
		}
		return false;
	}
	return true;
}

/* ************************************************ */
/* Ryan's validation routines                       */
/* ************************************************ */

function isEmpty(field) {
	return (field == null || field == undefined || !field.value.match("\\S"));
}

function isEmptyPhone(f1, f2, f3) {
	return (isEmpty(f1) && isEmpty(f2) && isEmpty(f3));
}
	


function radioSelection(field) {
	for (var i = 0; i < field.length; i++)
		if (field[i].checked)
			return i;
	return -1;	
}

function checkSelect(field, message) {
	if (valueOfSelect(field) == "") {
		alert(message);
		field.focus();
		return false;
	}
	return true;
}

function valueOfSelect(field) {
	return field.options[field.selectedIndex].value;
}

function checkNumberWithMessage(field, minimum, maximum, message) {
	if (isEmpty(field)) {
		alert(message);
		field.focus();
		return false;
	}
	if (isNaN(parseInt(field.value))) {
		alert(message);
		field.focus();
		return false;
	}
	var n = parseInt(field.value);
	if (minimum != NaN && n < minimum) {
		alert(message);
		field.focus();
		return false;
	}
	if (maximum != NaN && n > maximum) {
		alert(message);
		field.focus();
		return false;
	}
	return true;
}

function defaultField(field, val) {
	if (isEmpty(field))
		field.value = val;
}

function validateBrokerNumber(bn, doAlert) {
	if (!bn.match("[0-9]{7}") || bn.length != 7) {
		if (doAlert)
			alert("Broker number " + bn + " is not a 7-digit number.");
		return false;
	}
	
	if (bn.charAt(0) != '0') {
		if (doAlert)
			alert("Broker number " + bn + " does not start with a zero.");
		return false;
	}
	
	var validBranches = "0123457";
	if (validBranches.indexOf(bn.charAt(1)) == -1) {
		if (doAlert)
			alert("Broker number " + bn + " contains an incorrect branch number.  Valid branches are 0, 1, 2, 3, 4, 5 and 7.");
		return false;
	}
	
	return true;
}

function returnObjById(id)
{
    if (document.getElementById)
	    var returnVar = document.getElementById(id);
    else if (document.all)
	    var returnVar = document.all[id];
    else if (document.layers)
	    var returnVar = document.layers[id];
    return returnVar;
}

function valueOfRadio(field) {
	for (var i = 0; i < field.length; i++)
		if (field[i].checked)
			return field[i].value;
	return undefined;
}
/*
try
{
$(document).ready(function() {
	try
	{     
		  var lng = document.getElementById('accountdetails_form_language');
      var formV = document.getElementById('accountdetails_form');
      var erdiv = document.createElement('div');
			var bdiv = document.getElementById('errorDiv');
      if(erdiv != null && formV != null && lng != null)
      {
      	  if(lng.value == 'FR')
      	  {
      	 erdiv.innerHTML = "<span style=\"color: red;\"><p><strong>AVIS</strong></p><p>En raison d'activités de mise à jour, l'option <strong>Payer mon compte</strong> ne sera pas disponible les jours suivants :</p><ul><li>lundi 19 septembre, entre 4 h et 6 h heure avancée de l'Est (HAE);</li><li>dimanche 25 septembre, entre 2 h et 7 h 30 (HAE);</li><li>mercredi 12 octobre entre 2 h et 7 h heure (HAE).</li></ul><p>Nous sommes désolés des inconvénients causés par cette situation. Nous vous invitons à visiter notre site avant ou après les heures ci-dessus afin de profiter de l'option pratique du paiement en ligne par carte de crédit.</p><p>Merci pour votre compréhension et collaboration.</p></span><hr/>";
      	  }
      	  else
      	  {
      	erdiv.innerHTML = "<span style=\"color: red;\"><p><strong>* NOTICE *</strong></p><p>On the following dates, the <strong>Pay My Account</strong> option will not be available due to scheduled maintenance: </p><ul><li>Monday, September 19 between 4:00am - 6:00am Eastern Daylight Time (EDT)</li><li>Sunday, September 25 between 2:00am - 7:30am EDT</li><li>Wednesday, October 12 between 2:00am - 7:00am EDT</li></ul><p>We apologize for any inconvenience this may cause and invite you to visit our site before or after the above hours to take advantage of the convenient Online Credit Card Payment option.</p><p>Thank you for your understanding and cooperation.</p></span><hr/>";
      	  }
      	formV.insertBefore(erdiv,bdiv);
      }
  } catch(ex) { //nop
  	 }
});
} catch(ext)
{
//nop
}
*/
