function validator(inputType, inputField, inputDescription) {
	var strError = "";
	switch(inputType) {
	// Email
	case 2 :
		if (!validateEmail(inputField.value)) {
			strError = inputDescription;
		}
		break;
	
	// Lists 
	case 3 :
		if (inputField.selectedIndex==0) {
			strError = inputDescription;
			
		}
		break;
		
	// Radio Buttons
	case 4 :
		if (!validateRadioButtons(inputField)) {
			strError = inputDescription;
		}
		break;
		
	// Phone Number 1 e.g. 444-444-4444
	case 5 :
		if (!isPhoneNumber1(inputField.value)) {
			strError = inputDescription;
		}
		break;
	
	// Integers
	case 6 :
		if (!isInteger(inputField.value)) {
			strError = inputDescription;
		}
		break;
		
	// Decimal
	case 7 :
		if (!isDecimal(inputField.value)) {
			strError = inputDescription;
		}
		break;
		
	// Postal Code
	case 8 :
		if (!isPostalCode(inputField.value)) {
			strError = inputDescription;
		}
		break;
		
	// English Currency
	case 9 :
		if (!isEnglishCurrency(inputField.value)) {
			strError = inputDescription;
		}
		break;
		
	// PETRO POINTS validation
	case 10 :
		if (inputField.value != ""){
			if (!maxLength(inputField.value,16) || !minLength(inputField.value,16) || !isInteger(inputField.value)) {
				strError = inputDescription;
			}
		}
		break;
		
	// KidsFutures validation
	case 11 :
		if (inputField.value != ""){
			if (!maxLength(inputField.value,7) || !minLength(inputField.value,7) || !isInteger(inputField.value)) {
				strError = inputDescription;
			}
		}
		break;
	
	// SIN validation
	case 12 :
		if (inputField.value != ""){
			if (!maxLength(inputField.value,9) || !minLength(inputField.value,9) || !isInteger(inputField.value)) {
				strError = inputDescription;
			}
		}
		break;

	// POSTAL and ZIP validation
	case 13 :
		if (isPostalCode(inputField.value) || isValidZip(inputField.value) || inputField.value == "") {
		//alert("success");
		}
		else {
		strError = inputDescription;
		//alert("Please provide a valid  postal code (eg. L7S 1M1) or a valid ZIP code (eg. 90210)");
		     }
		break;

	// Alpha text only fields - optional
	case 14:
		if (inputField.value != ""){
			if (!isAlpha(inputField.value)) {
			strError = inputDescription;
			}
		}
		break;

		
	// Alpha text only fields - required
	case 15:
		if (inputField.value != ""){
			if (!isAlpha(inputField.value)) {
			strError = inputDescription;
			}
		}
		if (inputField.value == ""){
			strError = inputDescription;
		}
		break;		


	// Numeric text only fields - optional
	case 16:
		if (inputField.value != ""){
			if (!isInteger(inputField.value)) {
			strError = inputDescription;
			}
		}
		break;

		
	// Numeric text only fields - required
	case 17:
		if (inputField.value != ""){
			if (!isInteger(inputField.value)) {
			strError = inputDescription;
			}
		}
		if (inputField.value == ""){
			strError = inputDescription;
		}
		break;			
		
		
	// Image file fields (gif or jpg extension) - required
	case 18:
		if (inputField.value!=""){
			if (!filterFileType(inputField, "jpg") && !filterFileType(inputField, "gif")){
				strError = inputDescription;
			}
		}
		break;		
	

	// Lists with existing values as first item
	case 19 :
		if (inputField[inputField.selectedIndex].value=="") {
			strError = inputDescription;
			
		}
		break;

		
	// Text file fields (doc or txt extension) - required
	case 20:
		if (inputField.value!=""){
			if (!filterFileType(inputField, "doc") && !filterFileType(inputField, "txt")){
				strError = inputDescription;
			}
		}
		break;	

		
	// Text Fields  
	default :
		if (inputField.value==""){
			strError = inputDescription;
		}
		break;
	}
	
	return(strError)
}


function validateEmail(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false;
	 }
	 
	 if ((ldot+1) == lstr) {
	 	return false;
	 }

	 return true;			
}

function validateRadioButtons(radioField) {
	// set var radio_choice to false
	var radio_choice = false;
	
	// Loop from zero to the one minus the number of radio button selections
	for (counter = 0; counter < radioField.length; counter++) {
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (radioField[counter].checked)
			radio_choice = true; 
	}
	return (radio_choice);
}

function isValidDate(day,month,year){
	var dteDate;

	dteDate=new Date(year,month,day);

	return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}


function isInteger(numb)
{
	var new_msg = true;
	inputStr = numb.toString();
	for (var i = 0; i < inputStr.length; i++)
		{
		var oneChar = inputStr.charAt(i);
		if ((oneChar < "0" || oneChar > "9") && oneChar != "/")
				{
					new_msg = false;
				}
		}
	return (new_msg);
}

function isPhoneNumber1(numb)
{
	var new_msg = true;
	var oneChar;
	inputStr = numb.toString();
	if (inputStr.length==10) {
		for (var i = 0; i < inputStr.length; i++){
			oneChar = inputStr.charAt(i);		
			if ((oneChar < "0" || oneChar > "9") && oneChar != "/") {
				new_msg = false;
			}
		}
	}
	else if (inputStr.length==12) {
		for (var i = 0; i < inputStr.length; i++){
			oneChar = inputStr.charAt(i);
			if (i != 3 && i != 7) {
				if ((oneChar < "0" || oneChar > "9") && oneChar != "/") {
					new_msg = false;
				}
			}
			else {
				if (oneChar != "-" && oneChar != "." && oneChar != " ") {
					new_msg = false;
				}
			}
		}
	}
	else {
		new_msg = false;
	}
	return (new_msg);
}

function isDecimal(dNum){
	var new_msg = true;
	var rePrice = /^\d{1,8}(\.\d\d?)?$/
	if(!rePrice.test(dNum)){
	  new_msg = false;
	}
	return (new_msg);
}

function isEnglishCurrency(dNum){
	var new_msg = true;
	for (var i = 0; i < dNum.length; i++){
		oneChar = dNum.charAt(i);		
		if ((oneChar < "0" || oneChar > "9") && oneChar != "," && oneChar != "." && oneChar != "$" && oneChar != " ") {
			new_msg = false;
		}
	}
	return (new_msg);
}

function isPostalCode(postalCode){
	var myResult = true;
	if (!isLetter(postalCode.charCodeAt(0)))
			myResult = false;
	if (!isDigit(postalCode.charCodeAt(1)))
			myResult = false;
	if (!isLetter(postalCode.charCodeAt(2)))
			myResult = false;

	if (postalCode.length == 7) {
		if ((postalCode.charCodeAt(3)!=32) && (postalCode.charCodeAt(3)!=45))
				myResult = false;
		if (!isDigit(postalCode.charCodeAt(4)))
				myResult = false;
		if (!isLetter(postalCode.charCodeAt(5)))
				myResult = false;
		if (!isDigit(postalCode.charCodeAt(6)))
				myResult = false;
	}
	else {
		if (!isDigit(postalCode.charCodeAt(3)))
				myResult = false;
		if (!isLetter(postalCode.charCodeAt(4)))
				myResult = false;
		if (!isDigit(postalCode.charCodeAt(5)))
				myResult = false;
	}
	if (postalCode == "")
		myResult = false;

	return myResult;
}



// check for alphabetic characters
function isAlpha(str) {
	var i = 0;
	var myResult = true;
	for (i=0; i<str.length; i++) {
		if (!isLetter(str.charCodeAt(i))) {
			myResult = false;
		}
	}

	return (myResult);
}




// check for 5 digit ZIP code
function isValidZip(ZIP) {
	var i = 0;
	var myResult = true;
	for (i=0; i<5; i++) {
		if (!isDigit(ZIP.charCodeAt(i))) {
			myResult = false;
		}
	}
	
	// check length to be = to 5
	if (ZIP.length != 5) {
		myResult = false;
	}
	
	return (myResult);
}



function removeExtraChars( strValue ) {
/************************************************
DESCRIPTION: Removes currency formatting from
  source string.

PARAMETERS:
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[.]|[ ]|[-]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

function isLetter(myCharCode) {
	var myResult = false;
	if (myCharCode >= 65 && myCharCode <= 90)
		myResult = true;
	else if (myCharCode >= 97 && myCharCode <= 122)
		myResult = true;
	else if (myCharCode == 32 || myCharCode == 39 || myCharCode == 45 || myCharCode == 46 || myCharCode == 192 || myCharCode == 193 || myCharCode == 194 || myCharCode == 195 || myCharCode == 196 || myCharCode == 198 || myCharCode == 199 || myCharCode == 200 || myCharCode == 201 || myCharCode == 202 || myCharCode == 203 || myCharCode == 204 || myCharCode == 205 || myCharCode == 206 || myCharCode == 209 || myCharCode == 210 || myCharCode == 211 || myCharCode == 212 || myCharCode == 213 || myCharCode == 214 || myCharCode == 217 || myCharCode == 218 || myCharCode == 219 || myCharCode == 220 || myCharCode == 221 || myCharCode == 224 || myCharCode == 225 || myCharCode == 226 || myCharCode == 227 || myCharCode == 228 || myCharCode == 230 || myCharCode == 231 || myCharCode == 232 || myCharCode == 233 || myCharCode == 234 || myCharCode == 235 || myCharCode == 236 || myCharCode == 237 || myCharCode == 238 || myCharCode == 239 || myCharCode == 241 || myCharCode == 242 || myCharCode == 243 || myCharCode == 244 || myCharCode == 245 || myCharCode == 246 || myCharCode == 249 || myCharCode == 250 || myCharCode == 251 || myCharCode == 252 || myCharCode == 253 || myCharCode == 255)
	
		myResult = true;

	return myResult;
}

function isDigit(myCharCode) {
	var myResult = false;
	if (myCharCode >= 48 && myCharCode <= 57)
		myResult = true;

	return myResult;
}

function removeExtraChars2( strValue ) {
/************************************************
DESCRIPTION: Removes currency formatting from
  source string.

PARAMETERS:
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[$]|[ ]|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

function DaysDelta(_v1,_v2) {
  var _delta = 0;
  var _a1 = _v1;
  var _a2 = _v2;
  if (_v1.getTime()<_v2.getTime()) {
    _a1 = _v2;
	_a2 = _v1;
  }
  _delta = _a1.getTime() - _a2.getTime(); // in msecs
  _delta /= (1000 * 60 * 60 * 24 * 365); // days
  return _delta;
}

function onlyNumbers(inputString)
{
  var searchForNumbers = /\D+\_+\W+\s+\S+/
  return (searchForNumbers.test(inputString)) ? false : true;
} 

function maxLength(inputString,inputLength)
{
  return (inputString.length <= inputLength) ? true : false;
} 

function minLength(inputString,inputLength)
{
  return (inputString.length >= inputLength) ? true : false;
}  

function removeLeadingAndTrailingChar (inputString, removeChar) 
{
	var returnString = inputString;
	if (removeChar.length)
	{
	  while(''+returnString.charAt(0)==removeChar)
		{
		  returnString=returnString.substring(1,returnString.length);
		}
		while(''+returnString.charAt(returnString.length-1)==removeChar)
	  {
	    returnString=returnString.substring(0,returnString.length-1);
	  }
	}
	return returnString;
}

// validates a file extension
function filterFileType(field, ext) {
	var filename = field.value.toLowerCase();
	if (filename.indexOf('.' + ext) == -1) 
		return false;
	else
		return true;
} 
