// JavaScript Document 


function checkStudentInsertForm(theForm) {
	
	for(i=0; i<document.theForm.elements.length; i++)
{
document.theForm.elements[i].className='regular';
}

	
    var why = "";
	
	why += isEmpty(document.getElementById("firstname").value,'firstname','first name');
	why += isEmpty(document.getElementById("lastname").value,'lastname','last name');
	why += isEmpty(document.getElementById("address").value,'address','address');
	why += isEmpty(document.getElementById("city").value,'city','city');
	why += checkDropdown(document.getElementById("province").value,'province','province');
	why += checkPostalCode(document.getElementById("postalcode").value,'postalcode','postal code');
	why += checkDropdown(document.getElementById("country").value,'country','country');
    why += checkEmail(document.getElementById("email").value,'email','email');
    why += checkHomePhone(document.getElementById("homephone").value,'homephone','home telephone');
	why += checkBusPhone(document.getElementById("busphone").value,'busphone','business telephone');
	why += checkMobilePhone(document.getElementById("mobilephone").value,'mobilephone','mobile phone');
	why += isEmpty(document.getElementById("birthdate").value,'birthdate','birth date');
	why += checkSin(document.getElementById("sin").value,'sin','SIN');
	why += isNumber(document.getElementById("legacystudentnumber").value,'legacystudentnumber','legacy student number');
    if (why != "") {
       //alert(why);
	   document.getElementById("errormessage").className='errormessage';
	   document.getElementById("errormessage").innerHTML=why;

       return false;
    }

return true;
}

function checkInstructorInsertForm(theForm) {
	
	for(i=0; i<document.theForm.elements.length; i++)
{
document.theForm.elements[i].className='regular';
}

	
    var why = "";
	
	why += isEmpty(document.getElementById("firstname").value,'firstname','first name');
	why += isEmpty(document.getElementById("lastname").value,'lastname','last name');
		why += isEmpty(document.getElementById("username").value,'username','user name');
	why += isEmpty(document.getElementById("address").value,'address','address');
	why += isEmpty(document.getElementById("city").value,'city','city');
	why += checkDropdown(document.getElementById("province").value,'province','province');
	why += checkPostalCode(document.getElementById("postalcode").value,'postalcode','postal code');
	why += checkDropdown(document.getElementById("country").value,'country','country');
    why += checkEmail(document.getElementById("email").value,'email','email');
    why += checkHomePhone(document.getElementById("homephone").value,'homephone','home telephone');
	why += checkBusPhone(document.getElementById("busphone").value,'busphone','business telephone');
	why += checkMobilePhone(document.getElementById("mobilephone").value,'mobilephone','mobile phone');

    if (why != "") {
       //alert(why);
	   document.getElementById("errormessage").className='errormessage';
	   document.getElementById("errormessage").innerHTML=why;
       return false;
    }
return true;
}

function checkCourseInsertForm(theForm) {
	
	for(i=0; i<document.theForm.elements.length; i++)
{
document.theForm.elements[i].className='regular';
}

	
    var why = "";
	
	why += isEmpty(document.getElementById("coursename").value,'coursename','course name');
	why += isEmpty(document.getElementById("coursecode").value,'coursecode','course code');
	why += isEmpty(document.getElementById("coursedescription").value,'coursedescription','course description');
	

    if (why != "") {
       //alert(why);
	   document.getElementById("errormessage").className='errormessage';
	   document.getElementById("errormessage").innerHTML=why;
       return false;
    }
return true;
}

function checkProgramInsertForm(theForm) {
	
	for(i=0; i<document.theForm.elements.length; i++)
{
document.theForm.elements[i].className='regular';
}

	
    var why = "";
	
	why += isEmpty(document.getElementById("programname").value,'programname','program name');
	why += isEmpty(document.getElementById("programcode").value,'programcode','program code');
	why += isEmpty(document.getElementById("programdescription").value,'programdescription','program description');
	

    if (why != "") {
       //alert(why);
	   document.getElementById("errormessage").className='errormessage';
	   document.getElementById("errormessage").innerHTML=why;
       return false;
    }
return true;
}

function checkProgramIntake(theForm) {
	
	for(i=0; i<document.theForm.elements.length; i++)
{
document.theForm.elements[i].className='regular';
}

	
    var why = "";
	
	why += isEmpty(document.getElementById("programstartdate").value,'programstartdate','start date');
	why += isEmpty(document.getElementById("programenddate").value,'programenddate','end date');
	why += isEmpty(document.getElementById("seats").value,'seats','seats');
	

    if (why != "") {
       //alert(why);
	   document.getElementById("errormessage").className='errormessage';
	   document.getElementById("errormessage").innerHTML=why;
       return false;
    }
return true;
}

function checkApplicationForm(theForm){



for(i=0; i<document.theForm.elements.length; i++)
{



document.theForm.elements[i].className='regular';
}

	
    var why = "";
	why += checkDropdown(document.getElementById("ProgramSelected").value,'ProgramSelected','program selected');
	why += isEmpty(document.getElementById("FirstName").value,'FirstName','first name');
	why += isEmpty(document.getElementById("LastName").value,'LastName','last name');
	why += isEmpty(document.getElementById("Address").value,'Address','address');
	why += isEmpty(document.getElementById("City").value,'City','city');
	why += checkDropdown(document.getElementById("Province").value,'Province','province');
	why += checkPostalCode(document.getElementById("PostalCode").value,'PostalCode','postal code');
    why += checkEmail(document.getElementById("Email").value,'Email','email');
    why += checkHomePhone(document.getElementById("HomePhone").value,'HomePhone','home telephone');
	why += checkBusPhone(document.getElementById("OtherPhone").value,'OtherPhone','other telephone');
	why += isEmpty(document.getElementById("ContactTime").value,'ContactTime','contact time');
    if (why != "") {

	   document.getElementById("errormessage").className='errormessage';
	   document.getElementById("errormessage").innerHTML=why;

       return false;
    }

return true;

}


// email

function checkEmail (strng, field) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.<br />";document.getElementById(field).className='error';
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.<br />";document.getElementById(field).className='error';
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.<br />";document.getElementById(field).className='error';
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkHomePhone (strng,field) {
var error = "";
if (strng == "") {
   error = "You didn't enter a home phone number.<br />";document.getElementById(field).className='error';
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The home phone number contains illegal characters.<br />";
	   	 document.getElementById(field).className='error';
  
    }
    if (!(stripped.length == 10)) {
	error = "The home phone number is the wrong length. Make sure you included an area code.<br />";document.getElementById(field).className='error';
    } 
		if(strng.length>0){
	document.getElementById(field).value=parseInt(stripped);
	}
return error;
}

// phone number - strip out delimiters and check for 10 digits

function checkBusPhone (strng,field) {
var error = "";
if (strng == "") {

   return error;
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The business phone number contains illegal characters.<br />";
	   	 document.getElementById(field).className='error';
  
    }
    if (!(stripped.length == 10)) {
	error = "The business phone number is the wrong length. Make sure you included an area code.<br />";document.getElementById(field).className='error';
    } 
	
		if(strng.length>0){
	document.getElementById(field).value=parseInt(stripped);
	}

return error;
}

// phone number - strip out delimiters and check for 10 digits

function checkMobilePhone (strng,field) {
var error = "";
if (strng == "") {

   return error;
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The mobile phone number contains illegal characters.<br />";
	   	 document.getElementById(field).className='error';
  
    }
    if (!(stripped.length == 10)) {
	error = "The mobile phone number is the wrong length. Make sure you included an area code.<br />";document.getElementById(field).className='error';
    } 
	
		if(strng.length>0){
	document.getElementById(field).value=parseInt(stripped);
	}

return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.<br />";}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.<br />";}
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.<br />";} 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.<br />";}  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a username.<br />";}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.<br />";}
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.<br />";} 
return error;
}       


// non-empty textbox

function isEmpty(strng,field,propername) {

var error = "";
  if (strng.length == 0) {
     error = "Please enter the "+propername+".<br />"
	 document.getElementById(field).className='error';
	 
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.<br />";}
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.<br />";}
return error;
}

// valid selector from dropdown list

function checkDropdown(choice,field,propername) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the " + propername + " drop-down list.<br />";document.getElementById(field).className='error';
    }    
return error;
}    

// sin number - strip out delimiters and check for 10 digits

function checkSin (strng,field) {
var error = "";
if (strng == "") {
   error = "You didn't enter a SIN number.<br />";document.getElementById(field).className='error';
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The SIN number contains illegal characters.<br />";
	   	 document.getElementById(field).className='error';
  
    }
    if (!(stripped.length == 9)) {
	error = "The SIN number is the wrong length. Maximum of 9 digits.<br />";document.getElementById(field).className='error';
    } 
	
	if(strng.length>0){
	document.getElementById(field).value=parseInt(stripped);
	}
return error;
}

function isNumber (strng,field){
	var error = "";
	
if(isNaN(strng)){
	error = strng + " is not a numeric value that was expected.<br />";document.getElementById(field).className='error';
}
	return error;
}


function checkPostalCode(strng,field){
var error = "";	
var postalPattern = /^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d\s*$/i;
var postalRegExp = new RegExp(postalPattern);

var testResult = postalRegExp.test(strng);


if(!(testResult))
{
	
	error = "A properly formatted postal code was not entered.<br />";document.getElementById(field).className='error';
}	

if ((strng == "")||(strng == null))
{
	error = "A Postal Code was not entered.<br />";document.getElementById(field).className='error';

}

document.getElementById(field).value=strng.replace(/ /,"");
return error;
		
}
