/*********************************************************************************/
/*                                               Validation   14-JUN-03 (RK)                                                    */
/*********************************************************************************/

		function allBlur(valtype, obj){
			var curValue= obj.value;
			if(curValue.length>0){
				switch(valtype){
					case "letters":
						//alert();
						var validRegExp = /^[a-z][A-Z]$/i;
						if (curValue.search(validRegExp) == -1){
							alert("You can only enter letters in this field!\n\nPlease ensure that you have entered your details correctly.");
							obj.value="";
							return false;
						}
						break;
						
					case "phone":
						curValue=curValue.replace(/\(/g,"") 
						curValue=curValue.replace(/\)/g,"")
						curValue=curValue.replace(/-/g,"")
						curValue=curValue.replace(/ /g,"")
						var temp=""
						for(i=0;i<curValue.length;i++){
							if (i==0) temp += "(" 
							if (i==6) temp += "-" 
							if (i==10) temp += " " 
							temp += curValue.charAt(i)
							if (i==2) temp += ") " 
						}
						obj.value=temp
						if (i<10){
							alert("Please include your area-code and minimum 7-digit phone number.     ")
							obj.value="";
							return false;
						}
						break;
					
					case "email":
						var validRegExp = /^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-z]{2,6}$/i;
						if (curValue.search(validRegExp) == -1){
							alert("You have entered an invalid e-mail address!\n\nPlease ensure that you have entered your details correctly.");
							obj.value="";
							return false;
						}
						break;
						
					case "SIN":
							if (curValue.replace(/ /g,"").length!="9"){
								alert("Social Insurance number must contain 9 digits.");
								obj.value="";
							}else {
								var SIN=curValue.replace(/ /g,"");		
								one=SIN.charAt(0);		
								two=SIN.charAt(1);
								two=(two * 2) + "";
								if (two.length=="2"){
									two=parseInt(two.charAt(0)) + parseInt(two.charAt(1));
								}		
								three=SIN.charAt(2);
								four=SIN.charAt(3);
								four=(four * 2) + "";
								if (four.length=="2"){
									four=parseInt(four.charAt(0)) + parseInt(four.charAt(1));
								}		
								five=SIN.charAt(4);
								six=SIN.charAt(5);
								six=(six * 2) + "";
								if (six.length=="2"){
									six=parseInt(six.charAt(0)) + parseInt(six.charAt(1));
								}		
								seven=SIN.charAt(6);
								eight=SIN.charAt(7);
								eight=(eight * 2) + "";
								if (eight.length=="2"){
									eight=parseInt(eight.charAt(0)) + parseInt(eight.charAt(1));
								}		
								nine=SIN.charAt(8);		
								partTwo=parseInt(one) + parseInt(three) + parseInt(five) + parseInt(seven);
								partOne=parseInt(two) + parseInt(four) + parseInt(six) + parseInt(eight);
								partThree=parseInt(partOne) + parseInt(partTwo);
								partThree=partThree + "";
								
								if(parseInt(partThree.charAt(1))!=0){
									num=(parseInt(partThree.charAt(0)) + 1) * 10;
									num=parseInt(num) - parseInt(partThree);
								}else{
									num=parseInt(partThree.charAt(0)) * 10;
									num=parseInt(num) - parseInt(partThree);
								}
								if (parseInt(num) != parseInt(nine)){
									alert("\nYou have entered an invalid Social Insurance Number.     \n");
									obj.value="";
								}else{
									obj.value=SIN.substr(0,3) + " " + SIN.substr(3,3) + " " + SIN.substr(6,3);			
								}		
							}	
						break;
						
					case "postal":
						var temp="", msg="Please use one of the following postal formats.       \n\n Canadian postal codes:  A#A#A#      \n US zip codes:  ##### or #########        \n European postal codes:  A#####    ";
						curValue=curValue.replace(/-/i,"");
						curValue=curValue.replace(/ /i,"");
						if (isNaN(curValue)){
							for(i=0;i<obj.value.length;i++){
								if ((obj.value.charAt(i)!=" ")&&(obj.value.charAt(i)!="-")){
									temp=temp+obj.value.charAt(i).toUpperCase()
								}
							}
							obj.value=temp;
							if ((obj.value.length==6)&&(obj.value.charAt(0)>="A")&&(obj.value.charAt(1)<=9)&&(obj.value.charAt(2)>="A")&&(obj.value.charAt(3)<=9)&&(obj.value.charAt(4)>="A")&&(obj.value.charAt(5)<=9)){
									temp=obj.value.charAt(0)+obj.value.charAt(1)+obj.value.charAt(2)+" "+obj.value.charAt(3)+obj.value.charAt(4)+obj.value.charAt(5);
							}else{
								//if ((obj.value.length<=3)&&(obj.value.charAt(0)>="A")){
								if(obj.value.length>3 && obj.value.charAt(0)>="A" && obj.value.charAt(1)<="9"&& obj.value.charAt(2)<="9"&& obj.value.charAt(3)<="9"&& obj.value.charAt(4)<="9"&& obj.value.charAt(5)<="9"){
									temp=obj.value.charAt(0)+"-"+obj.value.charAt(1)+obj.value.charAt(2)+obj.value.charAt(3)+obj.value.charAt(4)+obj.value.charAt(5);
								}else{
									alert(msg);
									temp=""
								}
							}
							obj.value=temp
						}else{	
							for(i=0;i<obj.value.length;i++){
								if ((obj.value.charAt(i)>=0)&&(obj.value.charAt(i)<=9)){
									temp=temp+obj.value.charAt(i)}}
							obj.value=temp;
							if (obj.value.length==9){
								temp=obj.value.charAt(0)+obj.value.charAt(1)+obj.value.charAt(2)+obj.value.charAt(3)+obj.value.charAt(4)+"-"+obj.value.charAt(5)+obj.value.charAt(6)+obj.value.charAt(7)+obj.value.charAt(8);
							}else if(obj.value.length==5){
								temp=obj.value.charAt(0)+obj.value.charAt(1)+obj.value.charAt(2)+obj.value.charAt(3)+obj.value.charAt(4);
							}else{
								temp=obj.value.charAt(0)+obj.value.charAt(1)+obj.value.charAt(2)+obj.value.charAt(3)+obj.value.charAt(4)+"-"+obj.value.charAt(5)+obj.value.charAt(6)+obj.value.charAt(7)+obj.value.charAt(8);
								alert(msg);
								obj.value=""
							}
							obj.value=temp
						}
						break;
						
					case "date":
						var passDate=chkdate(obj) 
						if (passDate>0 && passDate<20) {
							var badDateMsg='You have entered an incorrect date value.      \n\n'
							switch(passDate){
								case 4:
									badDateMsg +='Please provide a valid year.'
									break;
								case 5:
									badDateMsg += 'Please select a valid numeric month between 1 and 12 or alpha month between "JAN" and "DEC".'
									break;
								case 6:
									badDateMsg += 'Please select a valid day between 1 and 31 for the selected month.     '
									break;	
								case 7:
									badDateMsg += 'Please select a valid day between 1 and 30 for the selected month.     '
									break;	
								case 8:
									badDateMsg += 'Please select a valid day greater than or equal to 1 for the month of February.     '
									break;	
								case 9:
									badDateMsg += 'Please select a valid day between 1 and 29 for the month of February in the selected year.     '
									break;		
								case 10:
									badDateMsg += 'The year you have entered is not a leap year.     \nPlease select a valid day between 1 and 28 for the selected month.      '
									break;
								case 11:
									badDateMsg += 'Please select a valid day between 1 and 28 for the selected month.      '
									break;
								default:
									badDateMsg +='Please re-enter your date using one of the following formats:     \n     mm/dd/yy     \n     mm/dd/yyyy \n     dd-mmm-yy      \n     dd-mmm-yyyy     \n'
							}
							obj.value=""
							alert(badDateMsg)
						}else if (passDate>20){
								if(passDate.length >4) {
									var passDate =passDate.substring(0,4)
									alert("Please note that you have entered a year value exceeding 4 digits.     \nThis application assumes that the year " + passDate + " was intended.      \n\nIf this is incorrect please correct the date using a 4-digit reference.       ")
								}else{
									alert("Please note that you have entered a year value with fewer than 4 digits.     \nThis application assumes that the year " + passDate + " was intended.      \n\nIf this is incorrect please correct the date using a 4-digit reference.       ")
								}
						}
						
						break;		
					case "money":
						var decLoc=curValue.indexOf(".")
						if(decLoc==-1) {
							decLoc=curValue.length
							curValue=curValue + ".00"
						}else{
							if(curValue.length==decLoc+1) curValue=curValue + "00"
							if(curValue.length==decLoc + 2) curValue=curValue + "0"
						}
						curValue=curValue.replace(/,/g,'')
						
						var delimiter = ","; // replace comma if desired
						var a = curValue.split('.',2),d = a[1], i = parseInt(a[0]);
						if(isNaN(i)) { return ''; }
						var minus = '';
						if(i < 0) { minus = '-'; }
						i = Math.abs(i);
						var n = new String(i);
						var a = [];
						while(n.length > 3){
							var nn = n.substr(n.length-3);
							a.unshift(nn);
							n = n.substr(0,n.length-3);
						}
						if(n.length > 0) { a.unshift(n); }
						n = a.join(delimiter);
						if(d.length < 1) {
							amount = n; 
						}else { 
							amount = n + '.' + d; 
						}
						amount = minus + amount;
						obj.value= amount;
						break;	
					case "propercase":
						var temp=""
						for(i=0;i<curValue.length;i++){
							if (i==0){
								temp = curValue.charAt(i).toUpperCase()
							}else{
								if(curValue.charAt(i-1)=='-'){
									temp += curValue.charAt(i).toUpperCase()
								}else if(curValue.charAt(i-1)==' '){
									temp += curValue.charAt(i).toUpperCase()
								}else{
									temp += curValue.charAt(i).toLowerCase()
								}
							}
						}
						obj.value=temp;
						break;
				}//end switch
			}
		} 
		
		function allKeyPress(e,valtype, obj) {
			var inputKey=0, autoPass=false
			if (window.event) {
				inputKey=window.event.keyCode;
			} else {
				inputKey=e.which ;
			}
			if (inputKey==0 || inputKey==9 ||  inputKey==8  ||  inputKey==39 || inputKey==32 || inputKey==45 || inputKey==46) autoPass=true //inputKey==16 || inputKey==37 || inputKey==46 ||
			//alert(inputKey)
			
			if(autoPass==false){
				var curValue= obj.value;
				switch(valtype){
					case "integer":
						if (inputKey==45 || (inputKey >= 48 && inputKey <= 57)){
							if(curValue.length>0 && inputKey==45) return false
						}else{return false}
						break;
					case "decimal":
						if (inputKey==45   || inputKey==189 || inputKey==46  || inputKey==190 || (inputKey >= 48 && inputKey <= 57)){
							if(curValue.length>0 && inputKey==45) return false
							if ((inputKey==46  || inputKey==190) && curValue.indexOf(".") != -1) return false;
							if(curValue.indexOf(".")==0) obj.value="0" + curValue
							if(curValue.indexOf("-.")==0) obj.value=curValue.replace(/-./i,"-0.")
						}else{return false}
						break;
					case "money":
						if (inputKey==45   || inputKey==189 || inputKey==46  || inputKey==190 || (inputKey >= 48 && inputKey <= 57)){
							if(curValue.length>0 && inputKey==45) return false
							if ((inputKey==46  || inputKey==190) && curValue.indexOf(".") != -1) return false;
							if(curValue.indexOf(".")==0) obj.value="0" + curValue
							if(curValue.indexOf("-.")==0) obj.value=curValue.replace(/-./i,"-0.")
							if(curValue.indexOf(".")>-1) {
								var decLoc=curValue.indexOf(".")
								if(curValue.length>decLoc + 2) return false;
							}
						}else{return false}
						break;
					case "numbersOnly":
						if ((inputKey < 48 || inputKey > 57))  return false
						break;
					case "lettersOnly":
						if ((inputKey < 65 || inputKey > 90))  return false
						break;
				}
			}
		}
		
		function LeapYear(intYear) {
			if (intYear % 100 == 0) {
				if (intYear % 400 == 0)  return true; 
			}else {
				if ((intYear % 4) == 0)  return true; 
			}
			return false;
		}
		
		function chkdate(obj) {
			var yrRef=""
			var strDatestyle = "US"; //United States date style
			//var strDatestyle = "EU";  //European date style
			var strDate,strDateArray,strDay,strMonth,strYear,intday,intMonth,intYear,booFound = false,intElementNr,err = 0
			var strSeparatorArray = new Array("-"," ","/",".");
			var strMonthArray = new Array(12);
			strMonthArray[0] = "Jan";strMonthArray[1] = "Feb";strMonthArray[2] = "Mar";strMonthArray[3] = "Apr";strMonthArray[4] = "May";strMonthArray[5] = "Jun";
			strMonthArray[6] = "Jul";strMonthArray[7] = "Aug";strMonthArray[8] = "Sep";strMonthArray[9] = "Oct";strMonthArray[10] = "Nov";strMonthArray[11] = "Dec";
			strDate = obj.value;
			
			strDate=strDate.replace(/\W/g,'-')
			var sMo=strDate.replace(/\W|\d/ig,'')
			
			if(sMo.length>0){
				strDate=eval("strDate.replace(/" + sMo +"/ig,'')")
				sMo=sMo.substr(0,3)
				strDate=sMo +'-' + strDate
				strDate=strDate.replace(/--|---|----/g,'-')
			}
			
			for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
				if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
					strDateArray = strDate.split(strSeparatorArray[intElementNr]);
					if (strDateArray.length < 2) {
						err = 1;
					}else if(strDateArray.length==2){
						strDay = strDateArray[0];
						strMonth = '01'
						strYear = strDateArray[1];
					}else {
						strDay = strDateArray[0];
						strMonth = strDateArray[1];
						strYear = strDateArray[2];
					}
					booFound = true;
				}
			}
			
			if (booFound == false) {
				if (strDate.length>5) {
					strDay = strDate.substr(0, 2);
					strMonth = strDate.substr(2, 2);
					strYear = strDate.substr(4);
				}
			}
			if (strYear.length == 2) {
				var yy=parseInt(strYear),preyy="20"
				if(yy>50) preyy="19"
				strYear = preyy + strYear;
				err=strYear
			}else if(strYear.length == 1) {
				strYear = '200' + strYear;
				err=strYear
			}else if(strYear.length >4) {
				err=strYear
				strYear =strYear.substring(0,4)
			}
			// US style
			if (strDatestyle == "US") {
				var strTemp = strDay;
				strDay = strMonth;
				strMonth = strTemp;
			}
			
			intday = parseInt(strDay, 10);
			if (isNaN(intday)) return (2);
			intMonth = parseInt(strMonth, 10);
			if (isNaN(intMonth)) {
				for (i = 0;i<12;i++) {
					if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
						intMonth = i+1;
						strMonth = strMonthArray[i];
						i = 12;
					}
				}
				if (isNaN(intMonth)) return (3);
			}
			intYear = parseInt(strYear, 10);
			if (isNaN(intYear)) return (4);
			if (intMonth>12 || intMonth<1) return (5);
			if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) return (6);

			if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) return (7);
			if (intMonth == 2) {
				if (intday < 1) return (8);
				if (LeapYear(intYear) == true) {
					if (intday > 29) return (9);
				}else {
					if (intday == 29) return (10);
					if (intday > 29) return (11);
				}
			}
			if (strDatestyle == "US") {
				//obj.value = strMonthArray[intMonth-1] + "us " + intday+" " + strYear;
				if (intMonth<10){zeroM="0"}else{zeroM=""}
				if (intday<10){zeroD="0"}else{zeroD=""}
				obj.value = zeroM + intMonth + "/" + zeroD + intday+"/" + strYear;
			}else {
				obj.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
			}
		return (err);
		}
	
		//------------------------------------------------------
		
		if (navigator.appName == 'Netscape') {
			window.captureEvents(Event.KEYPRESS);
			window.onkeypress = allKeyPress;
		}
		
//--------------------------Date Validation---------------------------
function es_valid_date() {
	var item = event.srcElement;
	event.returnValue = vs_valid_date(item);
}


function checkdate(objName) {
var datefield = objName;
if (chkdate(objName) == false) {
datefield.select();
alert("That date is invalid.  Please try again.");
datefield.focus();
return false;
}
else {
return true;
   }
}
function chkdate(objName) {

var strDatestyle = "US"; //United States date style
//var strDatestyle = "EU";  //European date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
strDate = datefield.value;
//strDate=datefield.value.replace(/jan/i,"01");

if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) {
err = 1;
return false;
}
else {
strDay = strDateArray[0];
strMonth = strDateArray[1];
strYear = strDateArray[2];
}
booFound = true;
   }
}
if (booFound == false) {
if (strDate.length>5) {
strDay = strDate.substr(0, 2);
strMonth = strDate.substr(2, 2);
strYear = strDate.substr(4);
   }
}
if (strYear.length == 2) {
strYear = '20' + strYear;
}
// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
   }
}
if (isNaN(intMonth)) {
err = 3;
return false;
   }
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
err = 4;
return false;
}
if (intMonth>12 || intMonth<1) {
err = 5;
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}
if (intMonth == 2) {
if (intday < 1) {
err = 8;
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 10;
return false;
}
}
}
if (strDatestyle == "US") {
//datefield.value = strMonthArray[intMonth-1] + "us " + intday+" " + strYear;

  if (intMonth<10){zeroM="0"}else{zeroM=""}
  if (intday<10){zeroD="0"}else{zeroD=""}

  datefield.value = zeroM + intMonth + "/" + zeroD + intday+"/" + strYear;
}
else {
datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
}
return true;
}
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}
function doDateCheck(from, to) {
if (Date.parse(from.value) <= Date.parse(to.value)) {
alert("The dates are valid.");
}
else {
if (from.value == "" || to.value == "") 
alert("Both dates must be entered.");
else 
alert("To date must occur after the from date.");
   }
}

function vs_valid_date(item) {
    checkdate(item)
}


//-------------------------------------------------------------------------------------

// Column Resize Function - by Paul Bellows

function setContent() {
	if (document.getElementById) {
		var contentHeight = document.getElementById('content');
		}
				if (contentHeight.offsetHeight <= 449 ) {
				contentHeight.style.height = 450 + 'px';
			}
	}

window.onload = function() {
	setContent();
}
