
// ********** COMMON JS FUNCTIONS  ********

//********** GLOBAL VARIABLES **********
var dtCh= "/";
var minYear=1900;
var maxYear=2100;



// **********FUNCTIONS ********
//*****************************

function ValidTelephone(vString, vMin, vMax) {
    if (!vString) return false;
    var Chars = "0123456789-()";
 
    for (var i = 0; i < vString.length; i++) {
      if (Chars.indexOf(vString.charAt(i)) == -1){
        return false;
      }else if(vString.length > vMax || vString.length < vMin){
        return false;
      }  
    }
  return true;
} 

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){
	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 (month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (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
	}
return true
}


//**Validates that a date occurs in the future. Pass a day,month,year as arguments and it evaluates that it is later than today. (NOTE: Months start with January=0 and Dec=11)

function FindDateDiff(d,m,y){
	vD = d;
	vM = m;
	vY = y;
	
	vMyDate = new Date(vY, vM, vD);
	vTodaysDate = new Date;
	
    var difference =
        Date.UTC(vMyDate.getYear(),vMyDate.getMonth(),vMyDate.getDate())
      - Date.UTC(vTodaysDate.getYear(),vTodaysDate.getMonth(),vTodaysDate.getDate());
   

	if(difference < 0){
		return false;
	}else{
		return true;
	}
}

function OpenPopup(pURL, pWidth, pHeight, pScrollbars){
	vWidth = pWidth;
	vHeight = pHeight;
	vleft=screen.width/2-(vWidth/2);
	vtop=screen.height/2-(vHeight/2)-27;
		
	vPopup = window.open(pURL, "popup", "toolbar=no,location=0,directories=no,status=no,menubar=0,scrollbars=" + pScrollbars + ",resizable=0,width="+vWidth+",height="+vHeight+",top="+vtop+",left="+vleft);
	vPopup.focus();
}

//******************* FORM VALIDATION *******************
/**********************************************************

This is a catch-all javascript validation function for an HTML form.  If you use the naming conventions outlined below for your form elements (namely textboxes and textareas), and use the ValidateForm function in the onsubmit event of your form, all validation is taken care of for you.

To use this file, do the following:

	1.Include the file on your page by using the following line in the :
		<script language="javascript" src="js_functions.js"></script>
	
	2.Then in your form tag, add the following:
		onsubmit="return ValidateForm(this, 1);"
	
	3.Note: if using an image to call the submit of the form, use the following line:
		<a href="javascript:if(ValidateForm(document.form1, 1)){document.form1.submit();}">

	4.Textboxes in your form must be named with "_xy" at the end, where x is the type of data in the field, and y designates if it is required or not.  Use "r" for required, "o" for optional.

Data types & their designators:
-regular text		_t
-email       		_e
-number      		_n
-telephone   		_p	//not handled yet
-web address 		_w	//not handled yet
-postal code 		_??	//not handled yet
-date        		_d

ie: -for a required textbox, end the name with "_tr"
    -optional textboxes don't need anything (or "_to" if you really want to)
    -for a required email field, end the name with "_er"
    -for an optional email field, end the name with "_eo"
    -etc...


The pLanguage parameter takes one of the following values:

1 - English messages will be displayed
2 - French messages will be displayed

**********************************************************/

vAlreadySubmitted = false;

function ValidateForm(pForm, pLanguage){
	vForm = pForm;
	
	for(i=0;i<vForm.length;++i){
		vElement = vForm.elements[i];
		vSuffix = vElement.name.substr(vElement.name.length-3, 3);
		vChar1 = vSuffix.substr(0, 1);
		vChar2 = vSuffix.substr(1, 1);
		vChar3 = vSuffix.substr(2, 1);
		
		if(vChar1=="_"){ //check if element is required
			if(vChar3=="r" && vElement.value==""){
				if(pLanguage==1){alert("Please enter all required fields.");}
				else if(pLanguage==2){alert("Veuillez entrer tous les champs demandés.");}
				vElement.focus();
				return false;
			}
			
			if(vChar2=="e"){ //validate email address
				vValidEmail = true;
				
				if(vElement.value.length>0){
					for(j=0; j<=vElement.value.length-1; ++j){
						if(vElement.value.charAt(j) == " "){vValidEmail = false;}
					}
						
					vCount = 0;
					for(k=0; k<=vElement.value.length-1; ++k){
						if(vElement.value.charAt(k) == "@"){++vCount;}
					}
					if(vCount != 1){vValidEmail = false;}
						
					if(vElement.value.indexOf("@") < 1){vValidEmail = false;}
					if(vElement.value.indexOf("@") == vElement.value.length-1){vValidEmail = false;}
					if(vElement.value.indexOf(".") < 1){vValidEmail = false;}
					if(vElement.value.indexOf(".") == vElement.value.length-1){vValidEmail = false;}
				}
				
				vInvalidChars = "!*&/\?+=)(^%$#:;";
				
				for(m=0; m<vInvalidChars.length; ++m){
					if(vElement.value.indexOf(vInvalidChars.charAt(m)) > -1){vValidEmail = false;}
				}
				
				if(vValidEmail==false){
					if(pLanguage==1){alert("Please enter a valid email address.");}
					else if(pLanguage==2){alert("Veuillez entrer une courriel valide.");}
					vElement.focus();
					return false;
				}
			}
			
			if(vChar2=="n" && isNaN(vElement.value)==true){ //validate number field
				if(pLanguage==1){alert("You have entered an invalid number in a number field.");}
				else if(pLanguage==2){alert("Vous avez entré un symbole invalide.");}
				vElement.focus();
				return false;
			}
			
			if(vChar2=="d" && isNaN(Date.parse(vElement.value))){ //validate date field
				if(pLanguage==1){alert("You have entered an invalid date in a date field.");}
				else if(pLanguage==2){alert("Vous avez entré une date invalide.");}
				vElement.focus();
				return false;
			}
		}
	}
	
	if(vAlreadySubmitted == false){
		vAlreadySubmitted = true;
		return true;
	}
	else{
		return false;
	}
	
}

function ValidEmail(pEmail){
	if(pEmail.length>0){
		for(j=0; j<=pEmail.length-1; ++j){
			if(pEmail.charAt(j) == " "){return false;}
		}

		vCount = 0;
		for(k=0; k<=pEmail.length-1; ++k){
			if(pEmail.charAt(k) == "@"){++vCount;}
		}
		if(vCount != 1){return false;}

		if(pEmail.indexOf("@") < 1){return false;}
		if(pEmail.indexOf("@") == pEmail.length-1){return false;}
		if(pEmail.indexOf(".") < 1){return false;}
		if(pEmail.indexOf(".") == pEmail.length-1){return false;}
	}
	else{
		return false;
	}

	vInvalidChars = "!*&/\?+=)(^%$#:;";

	for(m=0; m<vInvalidChars.length; ++m){
		if(pEmail.indexOf(vInvalidChars.charAt(m)) > -1){return false;}
	}

	return true;
}

	
