/**
 * DHTML date validation script for mm/dd/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/DHTML/date_validation.asp)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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 validateDate(dtStr){
	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){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//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){
		//alert("Please enter a valid date")
		return false
	}
//alert('valid date');
return true
}

function validatePhone(number) {
/*	new version by Laurence 2004 Nov 26
	RegExp is a Javascript1.2 feature; tested successfully in WinIE 5.0+, MacIE 5.1, Safari 1.0, WinNN 4.7, WinNN 6.2, WinMoz 1.7
	The pattern matches groups of 3, 3 and 4 digits found in that order anywhere in the string.
*/
	var myRegExp = /(\d{3}).*(\d{3}).*(\d{4})/;	
	var sPos = number.search(myRegExp);
	if ( sPos >= 0 ) {
	//	number = "(" + RegExp.$1 + ")" + RegExp.$2 + "-" + RegExp.$3; this would format as (000)000-0000
		return true;
	} else {
		return false;
	}
}

function validateInternationalPhone(number) {
/*	by Laurence 2005 Apr 29
	similar to validatePhone(), above
	The pattern matches a total of 10 digits found anywhere in the string; it's better than nothing...
*/
	var myRegExp = /(\d.*){9,}\d/;
	var sPos = number.search(myRegExp);
	if ( sPos >= 0 ) {
		return true;
	} else {
		return false;
	}
}

function validateEmail(email) {
/*	new version by Laurence 2004 Nov 26
	revised 2005 Nov 04 by Laurence: allow hyphens before @ symbol; rename 'verifyEmail' to 'validateEmail'

	RegExp is a Javascript1.2 feature; tested successfully in WinIE 5.0+, MacIE 5.1, Safari 1.0, WinNN 4.7, WinNN 6.2, WinMoz 1.7
	The pattern matches: 
	- at least one word character ( a-zA-Z0-9_ )
	- followed by any number more including periods and hyphens
	- then a single @
	- then at least one alphanumeric character plus any number more including hyphens and periods
	- plus an ending of an alphanumeric char, a single period, and at least 2 more letters. 
	There can be nothing before or after the pattern; all whitespace, punctuation, etc. not explicitly allowed is forbidden.
*/
	var myRegExp = /^[\w][\w\.\-]*@[a-zA-Z0-9]+[\w\.\-]*[a-zA-Z0-9]\.[a-zA-Z]{2,}$/;
	var sPos = email.search(myRegExp);
	if (sPos >= 0) {
		return true;
	} else {
		return false;
	}
}

function isPostCode(entry){ // CANADIAN CODES ONLY
	strlen = entry.length; 
	if ((strlen != 7) && (strlen != 6)) {return false;}
	entry=entry.toUpperCase();        // in case of lowercase characters
	// Check for legal characters in string - note index starts at zero
	if (strlen == 7) {
		if ('ABCEHJKLMNPRSTVXY'.indexOf(entry.charAt(0)) < 0) {return false;}
		if ('0123456789'.indexOf(entry.charAt(1)) < 0) {return false;}
		if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(2)) < 0) {return false;}
		if ('0123456789'.indexOf(entry.charAt(4)) < 0) {return false;}
		if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(5)) < 0) {return false;}
		if ('0123456789'.indexOf(entry.charAt(6)) < 0) {return false;}
	}
	
	if (strlen == 6) {
		if ('ABCEHJKLMNPRSTVXY'.indexOf(entry.charAt(0)) < 0) {return false;}
		if ('0123456789'.indexOf(entry.charAt(1)) < 0) {return false;}
		if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(2)) < 0) {return false;}
		if ('0123456789'.indexOf(entry.charAt(3)) < 0) {return false;}
		if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(4)) < 0) {return false;}
		if ('0123456789'.indexOf(entry.charAt(5)) < 0) {return false;}
	}
	
	return true; 	
}

function isZip(entry) { // US zip code
	 reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);

	 if (!reZip.test(entry)) {
			 // alert("Zip Code Is Not Valid");
				return false;
	 }
 
	return true;
}

function isCurrency(txtValue) {
  isValidCurrency = RegExp(/^[0-9\,]+(\.\d{2})?$/).test(String(txtValue).replace(/^\s+|\s+$/g, ""));
  return isValidCurrency;
}


/* used in view/admin/add , view/admin/edit
	check that required fields are filled in
*/
function validateAddEdit() {
	var theForm = document.frmAddEdit;
	var emptyFields = "";

//	alert("Validating Add / Edit form...");
	if (theForm.title.value == ""){
		emptyFields += "Title cannot be blank." + "\n" ;
	} else if (theForm.title.value.length > 100){
		emptyFields += "Title is " + (theForm.title.value.length - 100).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}

	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		theForm.submit();
		return true;
	}
}


/* used in view/login/frmLogin
	check that required fields are filled in
*/
function validateLogin() {
	var theForm = document.frmLogin;
	var emptyFields = "";

//	alert("validating Login...");	
	
	if (theForm.login.value == ""){
		emptyFields += "Your Email Address" + "\n" ;
	}
	if (theForm.password.value == ""){
		emptyFields += "Your Password" + "\n" ;
	}
		
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		theForm.submit();
		return true;
	}
}

function validateLoginPin() {
	var theForm = document.frmLoginByPin;
	var emptyFields = "";

//	alert("validating Login...");

	if (theForm.pin.value == ""){
		emptyFields += "Your PIN" + "\n" ;
	}	
	
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		theForm.submit();
		return true;
	}
}

/* used in view/login/frmPasswordReminder
	check that required fields are filled in
*/
function validatePassword() {
	var theForm = document.frmPassword;
	var emptyFields = "";

//	alert("validating Login...");

	if (theForm.login.value == ""){
		emptyFields += "Your Email Address" + "\n" ;
	}
		
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		theForm.submit();
		return true;
	}
}

/* used in view/user/frmSignup
	check that required fields are filled in
*/
function validateUser() {
	var theForm = document.frmUser;
	var emptyFields = "";

	//alert("validating User...");
	if (theForm.organization.value == ""){
		emptyFields += "Organization Name cannot be blank." + "\n" ;
	} else if (theForm.organization.value.length > 255){
		emptyFields += "Organization Name is " + (theForm.organization.value.length - 255).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if ((theForm.website.value == "") || (theForm.website.value.toLowerCase() == "http://")){
		emptyFields += "Organization Website cannot be blank." + "\n" ;
	} else if (theForm.organization.value.length > 255){
		emptyFields += "Organization Website is " + (theForm.website.value.length - 255).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.description.value == ""){
		emptyFields += "Organization Description cannot be blank." + "\n" ;
	} 
	
	if (theForm.address1.value == ""){
		emptyFields += "Address cannot be blank." + "\n" ;
	} else if (theForm.address1.value.length > 100){
		emptyFields += "Address is " + (theForm.address1.value.length - 100).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.address2.value.length > 100) {
		emptyFields += "Address Line 2 is " + (theForm.address2.value.length - 100).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.suite.value.length > 50) {
		emptyFields += "Suite is " + (theForm.suite.value.length - 50).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.city.value == ""){
		emptyFields += "City cannot be blank." + "\n" ;
	} else if (theForm.city.value.length > 100){
		emptyFields += "City is " + (theForm.city.value.length - 100).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.province.value == ""){
		emptyFields += "Province cannot be blank." + "\n" ;
	} else if (theForm.province.value.length > 50){
		emptyFields += "Province is " + (theForm.province.value.length - 50).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.country.value == ""){
		emptyFields += "Country cannot be blank." + "\n" ;
	} else if (theForm.country.value.length > 50){
		emptyFields += "Country is " + (theForm.country.value.length - 50).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.postalCode.value == ""){
		emptyFields += "Postal Code cannot be blank." + "\n" ;
	} else if (!(isPostCode(theForm.postalCode.value)) && !(isZip(theForm.postalCode.value))){
		emptyFields += "Postal Code/ZIP Code is invalid" + "\n" ;
	}
	
	if (theForm.firstName.value == ""){
		emptyFields += "Contact First Name cannot be blank." + "\n" ;
	} else if (theForm.firstName.value.length > 50){
		emptyFields += "Contact First Name is " + (theForm.firstName.value.length - 50).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	if (theForm.lastName.value == ""){
		emptyFields += "Contact Last Name cannot be blank." + "\n" ;
	} else if (theForm.lastName.value.length > 50){
		emptyFields += "Contact Last Name is " + (theForm.lastName.value.length - 50).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.telephone.value.length > 50){
		emptyFields += "Phone numbers can be at most 50 characters." + "\n" ;
	} else if ((theForm.telephone.value.length > 0) && !validatePhone(theForm.telephone.value)) {
		emptyFields += "Phone number is not required, but if present must contain\na 3-digit area code + 3 digits + 4 digits; an extension,\npunctuation, comments, etc. are optional." + "\n" ;
	}
	
	if (theForm.fax.value.length > 50){
		emptyFields += "Fax numbers can be at most 50 characters." + "\n" ;
	} else if ((theForm.fax.value.length > 0) && !validatePhone(theForm.fax.value)) {
		emptyFields += "Fax number is not required, but if present must contain\na 3-digit area code + 3 digits + 4 digits; an extension,\npunctuation, comments, etc. are optional." + "\n" ;
	}
	
	if (theForm.email.value == ""){
		emptyFields += "Email cannot be blank." + "\n" ;
	} else if (theForm.email.value.length > 100){
		emptyFields += "Email addresses can be at most 100 characters. Please supply a shorter address." + "\n" ;
	} else if (!validateEmail(theForm.email.value)) {
		emptyFields += "The email address must be correctly formatted." + "\n" ;
	}
	
	if (theForm.confirmemail.value == ""){
		emptyFields += "Confirm Email cannot be blank." + "\n" ;		
	} else if (theForm.email.value != theForm.confirmemail.value) {
		emptyFields += "Confirm Email is not the same as Eamil." + "\n" ;	
	}

	if (theForm.password.value == ""){
		emptyFields += "Password cannot be blank." + "\n" ;
	} else if (theForm.password.value.length < 6){
		emptyFields += "Password must have at least 6 characters. Please edit text to fit." + "\n" ;
	} else if (theForm.password.value.length > 50){
		emptyFields += "Password is " + (theForm.password.value.length - 50).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.confirmpassword.value == ""){
		emptyFields += "Confirm Password cannot be blank." + "\n" ;		
	} else if (theForm.password.value != theForm.confirmpassword.value) {
		emptyFields += "Confirm Password is not the same as Password." + "\n" ;	
	}

	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		theForm.submit();
		return true;
	}
}

/* used in view/job/frmJobPosting
	check that required fields are filled in
*/
function validateJob() {
	var theForm = document.frmJob;
	var emptyFields = "";
	
	if (theForm.jobcategoryId.selectedIndex <= 0) {
		emptyFields += "Job Category cannot be blank." + "\n" ;
	};
	
	if (theForm.termId.selectedIndex <= 0) {
		emptyFields += "Job Term cannot be blank." + "\n" ;
	};
	
	if (theForm.title.value == ""){
		emptyFields += "Position Title cannot be blank." + "\n" ;
	} else if (theForm.title.value.length > theForm.title_hint.value){
		emptyFields += "Organization Name is " + (theForm.title.length - theForm.title_hint.value).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}

	/*if (theForm.startDate.value == ""){
		emptyFields += "Position Start Date cannot be blank." + "\n" ;
	}else if (!(validateDate(theForm.startDate.value))) {
		emptyFields += "Start Date is invalid." + "\n" ;
	}*/
	
//alert('test: ' + emptyFields);
//return false;
	if (theForm.salary.value == ""){
		//emptyFields += "Address cannot be blank." + "\n" ;
	} else if (theForm.salary.value.length > theForm.salary_hint.value){
		emptyFields += "Salary is " + (theForm.salary.value.length - theForm.salary_hint.value).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.location.value == ""){
		//emptyFields += "Location cannot be blank." + "\n" ;
	} else if (theForm.location.value.length > theForm.location_hint.value){
		emptyFields += "Location is " + (theForm.location.value.length - theForm.location_hint.value).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.description.value == ""){
		emptyFields += "Position Description cannot be blank." + "\n" ;
	}
	
	if (theForm.publishDate.value == ""){
		emptyFields += "Post Date cannot be blank." + "\n" ;
	}else if (!(validateDate(theForm.publishDate.value))) {
		emptyFields += "Post Date is invalid." + "\n" ;
	}
	
	
	var dayDifference = ((Date.parse(theForm.removeDate.value) - Date.parse(theForm.publishDate.value))/(24*60*60*1000));
	/*alert (dayDifference);
	if (!((0 <= dayDifference) && (dayDifference <= 90))) {
		alert('wrong day');
	} else {
		
	}*/
	if (theForm.removeDate.value == ""){
		emptyFields += "Posting Remove Date cannot be blank." + "\n" ;
	}else if (!(validateDate(theForm.removeDate.value))) {
		emptyFields += "Posting Remove Date is invalid." + "\n" ;
	}else if (!((0 <= dayDifference) && (dayDifference <= 90)))  {
		//alert((Date.parse(theForm.publishDate.value) - Date.parse(theForm.removeDate.value))/(24*60*60*1000));
		emptyFields += "Posting Remove Date must be no more than 90 days in the future from the date to POST." + "\n" ;
	} 
	
	if (theForm.contactFirstName.value.length > theForm.contactFirstName_hint.value) {
		emptyFields += "Contact First Name is " + (theForm.contactFirstName.value.length - theForm.contactFirstName_hint.value).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.contactLastName.value.length > theForm.contactLastName_hint.value) {
		emptyFields += "Contact Last Name is " + (theForm.contactLastName.value.length - theForm.contactFirstName_hint.value).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	/*alert(theForm.contactWebsite);
	
	if (theForm.contactWebsite.value.length > theForm.contactWebsite_hint.value) {
		emptyFields += "Organization website is " + (theForm.contactWebsite.value.length - theForm.contactWebsite_hint.value).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}*/
	
	if (theForm.contactWebsite.value == ""){
		emptyFields += "Organization website cannot be blank." + "\n" ;
	}
	
	if (theForm.deadline.value == ""){
		emptyFields += "Submission Deadline cannot be blank." + "\n" ;
	}else if (!(validateDate(theForm.deadline.value))) {
		emptyFields += "Submission Deadline is invalid." + "\n" ;
	}
	
	if (theForm.contactTelephone.value.length > 50){
		emptyFields += "Phone numbers can be at most 50 characters." + "\n" ;
	} else if ((theForm.contactTelephone.value.length > 0) && !validatePhone(theForm.contactTelephone.value)) {
		emptyFields += "Phone number is not required, but if present must contain\na 3-digit area code + 3 digits + 4 digits; an extension,\npunctuation, comments, etc. are optional." + "\n" ;
	}
	
	if (theForm.contactFax.value.length > 50){
		emptyFields += "Fax numbers can be at most 50 characters." + "\n" ;
	} else if ((theForm.contactFax.value.length > 0) && !validatePhone(theForm.contactFax.value)) {
		emptyFields += "Fax number is not required, but if present must contain\na 3-digit area code + 3 digits + 4 digits; an extension,\npunctuation, comments, etc. are optional." + "\n" ;
	}
	
	if (theForm.contactEmail.value == ""){
		//emptyFields += "Email cannot be blank." + "\n" ;
	} else if (theForm.contactEmail.value.length > theForm.contactEmail.value){
		emptyFields += "Email addresses can be at most " + theForm.contactEmail_hint.value + " characters. Please supply a shorter address." + "\n" ;
	} else if (!validateEmail(theForm.contactEmail.value)) {
		emptyFields += "The email address must be correctly formatted." + "\n" ;
	}
	
	if (theForm.address1.value.length > theForm.address1_hint.value) {
		emptyFields += "Mail Address Line1 is " + (theForm.address1.value.length - theForm.address1_hint.value).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.address2.value.length > theForm.address2_hint.value) {
		emptyFields += "Mail Address Line2 is " + (theForm.address2.value.length - theForm.address2_hint.value).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.suite.value.length > theForm.suite.value) {
		emptyFields += "Mail Address Suite is " + (theForm.suite.value.length - theForm.suite_hint.value).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.city.value == ""){
		//emptyFields += "City cannot be blank." + "\n" ;
	} else if (theForm.city.value.length > theForm.city_hint.value){
		emptyFields += "City is " + (theForm.city.value.length - theForm.city_hint.value).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.province.value == ""){
		//emptyFields += "Province cannot be blank." + "\n" ;
	} else if (theForm.province.value.length > theForm.province_hint.value){
		emptyFields += "Province is " + (theForm.province.value.length - theForm.province_hint.value).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.country.value == ""){
		//emptyFields += "Province cannot be blank." + "\n" ;
	} else if (theForm.country.value.length > theForm.country_hint.value){
		emptyFields += "Country is " + (theForm.country.value.length - theForm.country_hint.value).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (theForm.postalCode.value == ""){
		//emptyFields += "Postal Code cannot be blank." + "\n" ;
	} else if (!(isPostCode(theForm.postalCode.value)) && !(isZip(theForm.postalCode.value))){
		emptyFields += "Postal Code/ZIP Code is invalid" + "\n" ;
	}

	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		theForm.submit();		
		return true;
	}
}
