//vars to determine browser
bV=parseInt(navigator.appVersion);
NS4=(document.layers) ? true : false;
IE4=((document.all)&&(bV>=4))?true:false;

//used to pop up help window
  function help()
    {
    window.open("", "help", "width=600,height=600,resizable=yes,scrollbars=yes,status=no,directories=no,menubar=no,toolbar=no")
  }


  // doomSay drives the warning which pops up when an attempt is made to delete or unlink something
  function doomSay(VoiceOfDoom) {
    if (confirm(VoiceOfDoom)) {
      return true
    } else {
      return false
    }
  }

  // droplist is maintained only for reference for later when addressing the rogue lists
  function droplist(member) {
    if (confirm('You are about to remove the current address from this list.  Are you sure?')) {
      window.open('ListLink.asp?member=' + member + '&return=' + escape('address.asp?address=<% = URLaddress %>&process=<% = URLprocess %>&track=av'), '_self')
    }
  }



function stripEntry(newdata) {
	newdata=newdata.replace(/\(/g, "")
	newdata=newdata.replace(/\)/g, "")
	newdata=newdata.replace(/ /g, "")
	newdata=newdata.replace(/-/g, "")
	return newdata;
}


//checkspecific - this function can be used to check for specific character, number or combination sets.
//i.e. if you want to only check for alpha characters, use a type value of alpha.

function checkspecific(thisField, type) {
	var validchars;
	if (type == "int") {
     validchars = "0123456789+-";
  } else {
  	if (type == "absint") {
  		validchars = "0123456789";
  	} else {
  	  if (type == "abs") {
  	  	validchars = "0123456789.";
      } else {
  	    if (type == "dec") {
  	    	validchars = "0123456789.-+";
        } else {
    	    if (type == "char") {
  	      	validchars = "abcdefghijklmnopqrstuvwxyz";
          } else {
  	        if (type == "numchar") {
  	        	validchars = "abcdefghijklmnopqrstuvwxyz1234567890";
  	        } else {
  	        if (type == "nozero") {
  	        	validchars = "123456789";
              }
            }
          }
        }
      }
    }
  }
 newdata = thisField.value;
  x = true;
  for (var i=0; i < newdata.length; i++) {
    temp = "" + newdata.substring(i, i+1);
    if (validchars.indexOf(temp) == "-1") x = false;
  }
  if (!x) return false;
  return true;
}

function isdigit(thisField) {
 var validchars = "0123456789";
 newdata = thisField.value
  x = true
  for (var i=0; i < newdata.length; i++) {
    temp = "" + newdata.substring(i, i+1);
    if (validchars.indexOf(temp) == "-1") x = false;
  }
  //if (!x) return false;
  if (!x) {
    alert("This field can be a numeric value only. Please enter a number.")
    thisField.focus();
    return false;
  }
  return true;
}


function rightlength(thisField) {
	newdata = thisField.value
	if (newdata.length < 10) return false;
	return true;
}

function formatnum(thisField) {
  newdata = thisField.value
  len = newdata.length
  if (len == 11 && (newdata.substring(0,4) == "1800" || newdata.substring(0,4) == "1888" || newdata.substring(0,4) == "1900" || newdata.substring(0,4) == "1866" || newdata.substring(0,4) == "1877")) {
  newdata = '(' + newdata.substring(0,4) + ') ' + newdata.substring(4,7) +  '-'  + newdata.substring(7,len);
  }
  if (len == 10) {
   newdata = '(' + newdata.substring(0,3) + ') ' + newdata.substring(3,6) +  '-'  + newdata.substring(6,10);
  }
  thisField.value = newdata
}

function validatePhoneNumber(thisField) {
  var x = true
  thisField.value = stripEntry(thisField.value)
  if (!isdigit(thisField)) { x = false;}
  if (!rightlength(thisField)) { x = false;}
  if (!x) {
   alert("Your phone number is incorrect. Please check if it is in the right format (i.e. (###)###-####). Ref: " + thisField.name.toUpperCase())
   thisField.focus()
   x = true
   return false
  }
  formatnum(thisField)

  return true;
}

//checks to see if the user has entered more than the allowed field limit
function checkFieldLen(thisField,len) {
    fieldLen = thisField.value.length
    if (fieldLen > len) {
    //alert("Make sure that the " + thisField.name.toUpperCase() + " filed has been properly completed.")
    alert("The " + thisField.name.toUpperCase() + " field must be less than or equal to " + len + " characters (spaces and other special characters included).")
    thisField.focus()
    return false
  }
 return true;
}
//checks to ensure that the field has at least x characters in it
function checkFieldLenMin(thisField,len) {
    fieldLen = thisField.value.length
    if (fieldLen < len) {
    //alert("Make sure that the " + thisField.name.toUpperCase() + " filed has been properly completed.")
    alert("The " + thisField.name.toUpperCase() + " field must have at least  " + len + " characters (spaces and other special characters included).")
    thisField.focus()
    return false;
  }
 return true;
}



function reqField(thisField) {
	 fieldLen = thisField.value.length
	 if (fieldLen == 0) {
	 	if (thisField.name == "FILE1") {
	   alert("Please select file to upload.")
	  }else{
	   alert("Please enter " + thisField.name.toUpperCase() + " for this record.");
	  }
    thisField.focus();
    return false;
	 }
 return true;
}

function reqFieldExt(thisField, FieldName) {
	 fieldLen = thisField.value.length
	 if (fieldLen == 0) {
	 	if (thisField.name == "FILE1") {
	   alert("Please select file to upload.")
	  }else{
	   alert("Please enter " + FieldName.toUpperCase() + " for this record.");
	  }
    thisField.focus();
    return false;
	 }
 return true;
}


//checks for required fields, returns friendly error message based on what errormsg
function reqFieldName(thisField, errorMsg) {

	 fieldLen = thisField.value.length
	 if (fieldLen == 0) {
	 	if (thisField.name == "FILE1") {
	   alert("Please select file to upload.")
	  }else{
	   if (errorMsg == '') {
    	     alert("Please enter " + thisField.name.toUpperCase() + " for this record.");
    	     }else{
	   alert(errorMsg);
	   }
	  }
    thisField.focus();
    return false;
	 }
 return true;
}

// new version using array.
function checkFileXt(thisField)  {
   //check file extensions
   var filename = thisField.value.toLowerCase()
//   var okimg = false
  var okimg = true
   var imgExtArray = new Array(".exe", ".vbs", ".mpg", ".vsd", ".vba", ".pif", ".ocx", ".ini", ".hta", ".dll", ".bin", ".com", ".avi", ".bat", ".mp3");
   //alert(imgExtArray.length);

   var x = filename.lastIndexOf(".")
   var y = filename.length
   var ext = filename.substring(x,y)

   //check to make sure image is of the allowable type
   for (var i=0; i<imgExtArray.length; i++)
    //alert(imgExtArray[i]);
    if (ext == imgExtArray[i]){
    okimg = false
    }

    //alert(okimg)

    if (okimg == false) {
    //alert("You are not permitted to upload files of this extension: (" + ext + ") Please upload a file as permitted in the description of this page.");
    alert("For security concerns, we do not permit files of this type (" + ext + ") to be uploaded to our systems. Please choose another file.");
    thisField.focus()
    return false;
    }

  return true;
}

// improved version from checkFileXt. This one allows you to send in an array of values that you WANT the file you are uploading to be equal to
// if the file does not match, an error is returned.
// Sample:
//   var imgExtArray = new Array(".xls", ".doc", ".rtf", ".pdf");    -these are the file xt I am checking against
//   if (!checkFileXt2(uploadedfile, imgExtArray) return false;

function checkFileXtRev(thisField, extArray)  {
   //check file extensions
   var filename = thisField.value.toLowerCase()
   var okimg = false
   //var imgExtArray = new Array(".vbs", ".mpg", ".mpeg", ".vsd", ".vba", ".pif", ".ocx", ".ini", ".hta", ".dot", ".dll", ".bin", ".com", ".avi", ".bat", ".mp3");
   //alert(imgExtArray.length);

   var x = filename.lastIndexOf(".")
   var y = filename.length
   var ext = filename.substring(x,y)

   //check to make sure image is of the allowable type
   for (var i=0; i<extArray.length; i++)
    //alert(imgExtArray[i]);
    if (ext == extArray[i]){
    okimg = true
    }

    //alert(okimg)

    if (okimg == false) {
    alert("Files of this type (" + ext + ") are not permitted.");
    thisField.focus()
    return false;
    }

  return true;
}



function checkFileXtImage(thisField)  {
   //check file extensions
   var filename = thisField.value.toLowerCase()
   var okimg = false
   var imgExtArray = new Array(".gif", ".GIF", ".jpg", ".JPG");
   //alert(imgExtArray.length);

   var x = filename.lastIndexOf(".")
   var y = filename.length
   var ext = filename.substring(x,y)

   //check to make sure image is of the allowable type
   for (var i=0; i<imgExtArray.length; i++)
    //alert(imgExtArray[i]);
    if (ext == imgExtArray[i]){
    okimg = true
    }

    //alert(okimg)

    if (okimg == false) {
    alert("You are not permitted to upload files of this extension: (" + ext + ") Please upload a file as permitted in the description of this page.");
    thisField.focus()
    return false;
    }

  return true;
}

function checkFileName(thisField)   {
   s = thisField.value;           //whole string
   filteredValues = "~!@#$%^*+|\=`{}'><,";        // Characters stripped out
   var i;
   var period = 0;
   for (i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
    var c = s.charAt(i);
//    if (c == ".") period  = period + 1;
    if (filteredValues.indexOf(c) != -1)  {
     //alert("You are not permitted to use " + c + " in the filename. Please rename your file and upload again.");
     alert("You are not permitted to use " + c + " in the filename.\nPLEASE RENAME FILE ON YOUR COMPUTER FIRST, then choose BROWSE again and add it.");
     return false
    }
//    if (period > 1) {
//     alert("You are not allowed to use more than 1 period '.' in the filename. \nFor example, an allowed filename would look like this: filename.ext. \nPlease rename your file and upload again.");
//     return false
//    }
   }
  return true;
}
// checks if user entered any special characters - used in search submission boxes. Used in combination with CheckFieldLenMin() to ensure that
//user has both entered a min # of chars and not used special characters.
//sample:  checkSpecialCharFilter(Lname)
function checkSpecialChar(thisField)   {
   t = thisField.value;           //whole string
   filteredValues = ",~!@#$%^&*+|\=`{}'><";        // Characters stripped out
   var j;
   for (j = 0; j < t.length; j++) {  // Search through string and append to unfiltered values to returnString.
    var d = t.charAt(j);
    if (filteredValues.indexOf(d) != -1)  {
     alert("You are not permitted to use the special character(s)>> " + d + "\nPlease remove them to continue.");
     return false;
    }
   }
  return true;
}

// checks if user entered any special characters - used in search submission boxes. this one is different from CheckSpecialChar()
//in that it allows the user to pass in the characters to be checked against.
// sample:  checkSpecialCharFilter(Lname, "~!@#$%^&*+|\=`{}'><_")
function checkSpecialCharFilter(thisField, FilterVals)   {
   t = thisField.value;           //whole string
   filteredValues = FilterVals;
   var j;
   for (j = 0; j < t.length; j++) {  // Search through string and append to unfiltered values to returnString.
    var d = t.charAt(j);
    if (filteredValues.indexOf(d) != -1)  {
     alert("You are not permitted to use the special character(s) " + d + "\nPlease remove them to continue.");
     return false;
    }
   }
  return true;
}

function noCommas(thisField)   {
   s = thisField.value;           //whole string
   var i;
   x = true
   for (i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
    var c = s.charAt(i);
    if (c == ",")  x = false;
   }
   if (!x)  {
    alert("Please do not use commas ',' in the field (Keyword) . A simple space between words is sufficient. (i.e. instead of 'water, sewage' use 'water sewage'");
    thisField.focus()
    return false
   }
  return true;
}

function reqSelectList(thisField) {
	fieldVal = thisField.options[0].selected
  if (fieldVal) {
  	var len = thisField.name.length
  	var fieldname = thisField.name.substring(0,len-2)
    alert("Please select " + fieldname.toUpperCase() + " for this record.")
    //alert("Please make a selection for this field.")
    thisField.focus()
    return false
   }
  return true;
}

function isEmail(email) {
    var x = email.value.indexOf("@")
    var y = email.value.indexOf(".")
    var z = x * y
    if (z < 2) {
      alert("You must enter a valid Email Address")
      email.focus()
      return false
   }
  return true;
}

function checkPostCode(thisField) {
  postalCode = stripEntry(thisField.value)
  postalCodelen = postalCode.length
  if (postalCodelen < 5) {
  	alert("Please make sure the Postal Code you have entered is valid")
  	thisField.focus()
  	return false;
	}else {
    if (postalCodelen == 6) {postalCode = postalCode.substring(0,3) + ' ' + postalCode.substring(3,6)}
    postalCode = postalCode.toUpperCase()
	}
  thisField.value = postalCode
	return postalCode;
}



 function checkDate(thisField)  {
 	dateVal = thisField.value

  // Checks for the following valid date formats:
  // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
  // Also separates date into month, day, and year variables

  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

  // To require a 4 digit year entry, use this line instead:
  // var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;


  var matchDate = dateVal.match(datePat); // is the format ok?
  if (matchDate == null) {
    alert("The field " + thisField.name.toUpperCase() + " is not in a valid format. Use MM/DD/YYYY")
    thisField.focus();
    return false;
  }


  month = matchDate[1]; // parse date into variables
  day = matchDate[3];
  year = matchDate[4];

  var message = "";

  if (isNaN(year))
  {
    message = "The year must be a number! Please make sure that the " + thisField.name.toUpperCase() + " is correct.";
  }
  else
  {
    if (year.length != 2 && year.length != 4)
        message = "The year is not the correct length! Please make sure that the " + thisField.name.toUpperCase() + " is correct.";
    else
        if (year.length == 4)
            if (year < 1900)
                message = "The year is not within proper range! Please make sure that the " + thisField.name.toUpperCase() + " is correct.";
  }

  if (month == 2) { // check for february 29th
   var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
   if (day>29 || (day==29 && !isleap))  message = "February " + year + " doesn't have " + day + " days!  Please make sure that the " + thisField.name.toUpperCase() + " is correct."
  }
  if ((month==4 || month==6 || month==9 || month==11) && day==31)  message = "Month "+month+" doesn't have 31 days!  Please Please make sure that the " + thisField.name.toUpperCase() + " is correct."
  if (day < 1 || day > 31) message = "Day must be between 1 and 31.  Please make sure that the " + thisField.name.toUpperCase() + " is correct."
  if (month < 1 || month > 12)  message = "Month must be between 1 and 12. Please make sure that the " + thisField.name.toUpperCase() + " is correct."  // check month range
  if (message !=0) {
    alert(message)
  	thisField.focus()
  	return false
  }

  return true;
 }

 function checkDateName(thisField,errorMsg)  {
 	dateVal = thisField.value

	if (dateVal == null){
		return false;
	}
  // Checks for the following valid date formats:
  // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
  // Also separates date into month, day, and year variables

  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

  // To require a 4 digit year entry, use this line instead:
  // var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;


  var matchDate = dateVal.match(datePat); // is the format ok?
  if (matchDate == null) {
	  if (errorMsg == ''){
		alert("The field " + thisField.name.toUpperCase() + " is not in a valid format. Use MM/DD/YYYY");
	  }
	  else{
		alert(errorMsg);
	  }
		
    
    thisField.focus();
    return false;
  }


  month = matchDate[1]; // parse date into variables
  day = matchDate[3];
  year = matchDate[4];

  var message = "";

  if (isNaN(year))
  {
    message = "The year must be a number! Please make sure that the " + thisField.name.toUpperCase() + " is correct.";
  }
  else
  {
    if (year.length != 2 && year.length != 4)
        message = "The year is not the correct length! Please make sure that the " + thisField.name.toUpperCase() + " is correct.";
    else
        if (year.length == 4)
            if (year < 1900)
                message = "The year is not within proper range! Please make sure that the " + thisField.name.toUpperCase() + " is correct.";
  }

  if (month == 2) { // check for february 29th
   var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
   if (day>29 || (day==29 && !isleap))  message = "February " + year + " doesn't have " + day + " days!  Please make sure that the " + thisField.name.toUpperCase() + " is correct."
  }
  if ((month==4 || month==6 || month==9 || month==11) && day==31)  message = "Month "+month+" doesn't have 31 days!  Please Please make sure that the " + thisField.name.toUpperCase() + " is correct."
  if (day < 1 || day > 31) message = "Day must be between 1 and 31.  Please make sure that the " + thisField.name.toUpperCase() + " is correct."
  if (month < 1 || month > 12)  message = "Month must be between 1 and 12. Please make sure that the " + thisField.name.toUpperCase() + " is correct."  // check month range
  if (message !=0) {
    alert(message)
  	thisField.focus()
  	return false
  }

  return true;
 }


//textCounter - displays characters left while user is typing in a text/texarea field.
//SAMPLE - code from page:
//response.write("<textarea name=orgdesc rows=3 cols=45 wrap=virtual " & _
//               "onKeyDown=textCounter(this.form.orgdesc,this.form.remLen_1,255); " & _
//               "onKeyUp=textCounter(this.form.orgdesc,this.form.remLen_1,255);></textarea><br>")

//  response.write("Characters left:<input readonly type=text name=remLen_1 size=5 maxlength=4 value= class=tinytext>")
//  response.write("<script language=JavaScript>")
//  response.write("document.regform.remLen_1.value = (255 - document.regform.orgdesc.value.length);")
//  response.write("</script>")

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	} else {
		countfield.value = maxlimit - field.value.length;
	}
}



function isInteger (s)
// isInteger (STRING s [, BOOLEAN emptyOK])
//
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true),
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

{
   var defaultEmptyOK = false
   var i;

    if (isEmpty(s))
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}



function textFieldError(data, alertString) {
  if (alertString != '') {
    window.alert(alertString);
    data.focus();
    data.select();
  }
  return false;
}

function validateInteger(data, nonblank, numlength, intname) {
  var
    digitcount = 0;

  // Return ok for non-mandatory fields that the user blanks out.
  if (!nonblank && (data.value.length == 0)) {
    return true;
  }

  if (intname != '')
    errStr = pleaseStr + intname;
  else
    errStr = '';

  if (data.value.length == 0)
    return textFieldError(data, errStr);

  for (i=0; i < data.value.length; i++) {
    if (isNaN(parseInt(data.value.charAt(i), 10)))
      return textFieldError(data, errStr);
    if (numlength != 0)
      digitcount++;
  }

  if ((numlength != 0) && (digitcount != numlength))
    return textFieldError(data, errStr);

  return true;
}

function validateSIN(data, asinStr) {
  // The algorithm is as follows:
  //   1. Multiply digits 2, 4, 6, and 8 in the SIN by 2.
  //   2. Separate the results into single digits i.e.14 becomes 1, 4
  //   3. Sum all the digits in step 2 with digits 1, 3, 5, 7, and 9 in the
  //      original SIN.
  //   4. The result from step 3 should be evenly divisible by 10.

  var
    result = data.value;
    sum = 0;
    temp = 0;
    save = 0;
    firstdig = data.value.charAt(0);

  // Not an integer.

  if (data.value.length == 0) {
    return textFieldError(data, pleaseStr + asinStr);
  }

  if (!validateInteger(data, mandatory, 9, '')) {
    return textFieldError(data, sinerrStr);
  }

  if ((firstdig == '0') || (firstdig == '3') ||
      (firstdig == '8') || (firstdig == '9'))
    return textFieldError(data, sinerrStr);

  temp = parseInt(result.charAt(1), 10) * 2;
  result = result.substring(0, 1) + temp.toString() +
           result.substring(2, result.length);
  if (temp >= 9)
    save += 1;

  temp = parseInt(result.charAt(3+save), 10) * 2;
  result = result.substring(0, 3+save) + temp.toString() +
           result.substring(4+save, result.length);
  if (temp >= 9)
    save += 1;

  temp = parseInt(result.charAt(5+save), 10) * 2;
  result = result.substring(0, 5+save) + temp.toString() +
           result.substring(6+save, result.length);
  if (temp >= 9)
    save += 1;

  temp = parseInt(result.charAt(7+save), 10) * 2;
  result = result.substring(0, 7+save) + temp.toString() +
           result.substring(8+save, result.length);

  for (i = 0; i < result.length; i++) {
    temp = result.charAt(i);
    sum = sum + parseInt(temp, 10);
  }

  if ((sum % 10) != 0) {
    return textFieldError(data, sinerrStr);
  }

  return true;
}


function SINValidator(form)
{                               
  for (var i = 0; i < form.length; i++) //get all form elements
  {
    var f = form.elements[i]; //organize elements into an array
                    
    //separate sin into 9 digits
    if (f.name == "sin")
    {
      if (f.value.length != 9)
        alert("Error! A Social Insurance Number contains 9 digits, please re-enter your number.");
      else
      {
        for (var j = 0; j < 9; j++)
        {                               
                //separate digits
                var num1 = f.value.charAt(0);
                var num2 = f.value.charAt(1);
                var num3 = f.value.charAt(2);
                var num4 = f.value.charAt(3);
                var num5 = f.value.charAt(4);
                var num6 = f.value.charAt(5);
                var num7 = f.value.charAt(6);
                var num8 = f.value.charAt(7);
                var num9 = f.value.charAt(8);                           
        }

        //validate
        var step1 = 2 * (num2 + num4 + num6 + num8);
        var step2 = 0;
        var tempNum = Math.floor(step1 / 10000);
        step2 += tempNum;                       
        step1 -= (tempNum * 10000);
        tempNum = Math.floor(step1 / 1000);
        step2 += tempNum;                       
        step1 -= (tempNum * 1000);
        tempNum = Math.floor(step1 / 100);
        step2 += tempNum;                       
        step1 -= (tempNum * 100);                       
        tempNum = Math.floor(step1 / 10);
        step2 += tempNum;                       
        step1 -= (tempNum * 10);
        tempNum = Math.floor(step1 / 1)
        step2 += tempNum;
                        
        var step3 = num1 + num3 + num5 + num7;                          
        tempNum = Math.floor(step3 / 1000);
        step2 += tempNum;
        step3 -= (tempNum * 1000);                      
        tempNum = Math.floor(step3 / 100);
        step2 += tempNum;                                       
        step3 -= (tempNum * 100);                       
        tempNum = Math.floor(step3 / 10);
        step2 += tempNum;                                               
        step3 -= (tempNum * 10);
        tempNum = Math.floor(step3 / 1)
        step2 += tempNum;
                                
        tempNum = Math.floor(step2 / 10);
        valid = 10 - (step2 - (tempNum * 10));          
        var msg = "The Social Insurance Number " + num1 + num2 + num3 +
                " " + num4 + num5 + num6 + " " + num7 + num8 + num9 + " is ";
        
        if (valid == 10)
          valid -= 10;          
        
        
        if (valid == num9)
        {
          return true;
        }
          //msg += "valid."
        else
        {
          msg += "not valid."
          
          alert(msg);
          form.sin.focus()
          return false;
        } 
        
        //if (valid == num9)
        //  msg += "valid."
        //else
        //  msg += "not valid."
         
        //  alert(msg);
              	
      }
    }
  }
}


function fixTime(fld,starthour) 
{ // tenacious time correction 
  if(!fld.value.length||fld.disabled) return true; // blank fields are the domain of requireValue 
  var hour= 0; 
  var mins= 0;
  var ampm= ' AM';
  val= fld.value;
  var dt= new Date('1/1/2000 ' + val);

  if(('9'+val) == parseInt('9'+val))
  { hour= val; }
  else if(dt.valueOf())
  { hour= dt.getHours(); 
    mins= dt.getMinutes(); 
    if(hour == 0) {hour = 12;}
  }
  else
  {
    val= val.replace(/\D+/g,':');
    hour= parseInt(val);
    mins= parseInt(val.substring(val.indexOf(':')+1,20));
    if(val.indexOf('PM') > -1) ampm= ' PM';
    if(isNaN(hour)) hour= 0;
    if(isNaN(mins)) mins= 0;
  }
  if(hour < starthour) { ampm= ' PM'; }
  if(hour == 12) { ampm= ' PM'; }
  while(hour > 12) { hour-= 12; ampm= ' PM'; }
  while(mins > 60) { mins-= 60; hour++; }
  if(mins < 10) mins= '0' + mins;
  if(!hour)
  { // the date was unparseable 
    status= 'The '+fieldname(fld)+' field has the wrong time.';
    return false;
  }
  fld.value= hour + ':' + mins + ampm;
  return true;
}

function fixTime24(fld) 
{ // tenacious time correction 
  if(!fld.value.length||fld.disabled) return true; // blank fields are the domain of requireValue 
  var hour= 0; 
  var mins= 0;
  val= fld.value;
  var dt= new Date('1/1/2000 ' + val);
  if(('9'+val) == parseInt('9'+val))
  { hour= val; }
  else if(dt.valueOf())
  { hour= dt.getHours(); mins= dt.getMinutes(); }
  else
  {
    val= val.replace(/\D+/g,':');
    hour= parseInt(val);
    mins= parseInt(val.substring(val.indexOf(':')+1,20));
    if(isNaN(hour)) hour= 0;
    if(isNaN(mins)) mins= 0;
    if(val.indexOf('pm') > -1) hour+= 12;
  }
  hour%= 24;
  mins%= 60;
  if(mins < 10) mins= '0' + mins;
  fld.value= hour + ':' + mins;
  return true;
}

function validateTelephone(fieldName, theField)
{
  //Validate the Telephone number
  var theValue = theField.value;
  if (theValue.length != 0)  
  {
    //strip non-numeric characters
    while(theValue.indexOf('#') != -1) theValue = theValue.replace('#', '');
    while(theValue.indexOf('-') != -1) theValue = theValue.replace('-', '');
    while(theValue.indexOf('(') != -1) theValue = theValue.replace('(', '');
    while(theValue.indexOf(')') != -1) theValue = theValue.replace(')', '');
    while(theValue.indexOf(' ') != -1) theValue = theValue.replace(' ', '');
    //2008-10-24 A.D set this to greater then 11 to ensure telephone numbers with 10 digits correctly validate 
    if (isNaN(theValue)||(theValue.length > 11 && theValue.length < 10)) 
    {
      alert('The ' + fieldName + ' number is not valid.');
      theField.focus();
      return false;
    }
    //2008-10-24 A.D Added Else if statments for proper formatting
    else if (theValue.length == 10)
    {
      theValue = theValue.substring(0,3) + ' ' + theValue.substring(3,6) + ' ' + theValue.substring(6,10);
      theField.value = theValue;
      return true;
    }
    else if (theValue.length = 11)
    {
      theValue = theValue.substring(0,1) + ' ' + theValue.substring(1,4) + ' ' + theValue.substring(4,7) + ' ' + theValue.substring(7,11);
      theField.value = theValue;
      return true;    
    }
  }
  return true;
}

function checkCanadaPostCode(thisField) 
{
  postalCode = thisField.value; 
  postalCode = postalCode.replace(/\(/g, ""); 
  postalCode = postalCode.replace(/\)/g, ""); 
  postalCode = postalCode.replace(/ /g, ""); 
  postalCode = postalCode.replace(/-/g, ""); 
  postalCodelen = postalCode.length; 
  if (postalCodelen == 0) {return true;} 
  
  if (postalCodelen != 6) 
  { 
  	alert("Please make sure the Postal Code has 6 characters (A1A A1A)."); 
  	thisField.focus();
  	return false;
  }
  
  postalCode = postalCode.toUpperCase();
  //check for correct sequence of letters and numbers
  badCode = 'Please make sure the postal code is in the format A1A 1A1.';
  if ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.indexOf(postalCode.charAt(0)) < 0) {alert(badCode); return false;}
  if ('0123456789'.indexOf(postalCode.charAt(1)) < 0) {alert(badCode); return false;}
  if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(postalCode.charAt(2)) < 0) {alert(badCode); return false;}
  if ('0123456789'.indexOf(postalCode.charAt(3)) < 0) {alert(badCode); return false;}
  if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(postalCode.charAt(4)) < 0) {alert(badCode); return false;}
  if ('0123456789'.indexOf(postalCode.charAt(5)) < 0) {alert(badCode); return false;}
  if (postalCodelen == 6) {postalCode = postalCode.substring(0,3) + ' ' + postalCode.substring(3,6)}
  thisField.value = postalCode;
  return postalCode;
}


function ckdate(tmonth, tday, tyear) 
{
  var passtday
  passtday == 0
  if (tday > 31) 
  {
  	 passtday = 1
  }

  if (((tmonth == 4)||(tmonth == 6) ||(tmonth == 9) || (tmonth == 11)) && (tday > 30)) 
  {
  	passtday = 1
  }

  if (tmonth == 2) 
  {
    if (tday > 29) 
    {
      tday = 29
    }
    isleap = (tyear % 4 == 0 && (tyear % 100 != 0 || tyear % 400 == 0));
    if ((tday==29) && (!isleap))  
    {
      passtday = 1
  		 //return false;
    }
  }
  
  if (passtday == 1)
  {
    return '';
  } else {
    return tday;
  }
}


function Trim(str)
/***
PURPOSE: Remove trailing and leading blanks from our string.
IN: str - the string we want to Trim

RETVAL: A Trimmed string!
***/
{
	return RTrim(LTrim(str));
}

function LTrim(str)
/***
  PURPOSE: Remove leading blanks from our string.
  IN: str - the string we want to LTrim

  RETVAL: An LTrimmed string!
***/
{
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
          j++;


      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
  }

  return s;
} //End Function
	
function RTrim(str)
/***
  PURPOSE: Remove trailing blanks from our string.
  IN: str - the string we want to RTrim

  RETVAL: An RTrimmed string!
***/
{
  // We don't want to trip JUST spaces, but also tabs,
  // line feeds, etc.  Add anything else you want to
  // "trim" here in Whitespace
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
          i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
  }

  return s;
}

 //End Function

function Len(str)
/***
IN: str - the string whose length we are interested in

RETVAL: The number of characters in the string
***/
{  
	return String(str).length; 
} //End Function

function Left(str, n)
/***
  IN: str - the string we are LEFTing
      n - the number of characters we want to return

  RETVAL: n characters from the left side of the string
***/
{
  if (n <= 0)     // Invalid bound, return blank string
          return "";
  else if (n > String(str).length)   // Invalid bound, return
          return str;                // entire string
  else // Valid bound, return appropriate substring
          return String(str).substring(0,n);
} //End Function

function Right(str, n)
/***
  IN: str - the string we are RIGHTing
      n - the number of characters we want to return

  RETVAL: n characters from the right side of the string
***/
{
  if (n <= 0)     // Invalid bound, return blank string
     return "";
  else if (n > String(str).length)   // Invalid bound, return
     return str;                     // entire string
  else { // Valid bound, return appropriate substring
     var iLen = String(str).length;
     return String(str).substring(iLen, iLen - n);
  }
} //End Function

function Mid(str, start, len)
/***
  IN: str - the string we are LEFTing
      start - our string's starting position (0 based!!)
      len - how many characters from start we want to get

  RETVAL: The substring from start to start+len
***/
// Keep in mind that strings in JavaScript are zero-based, so if you ask
// for Mid("Hello",1,1), you will get "e", not "H".  To get "H", you would
// simply type in Mid("Hello",0,1)

// You can alter the above function so that the string is one-based.  Just
// check to make sure start is not <= 0, alter the iEnd = start + len to
// iEnd = (start - 1) + len, and in your final return statement, just
// return ...substring(start-1,iEnd)

{
  // Make sure start and len are within proper bounds
  if (start < 0 || len < 0) return "";

  var iEnd, iLen = String(str).length;
  if (start + len > iLen)
          iEnd = iLen;
  else
          iEnd = start + len;

  return String(str).substring(start,iEnd);
} //End Function

function InStr(strSearch, charSearchFor)
/*
	InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
	Requires use of:
		Mid function
		Len function
*/
{
	for (i=0; i < Len(strSearch); i++)
	{
	    if (charSearchFor == Mid(strSearch, i, 1))
	    {
			return i;
	    }
	}
	return -1;
} //End Function
