<!-- form validation script -->



//displays alert if a message exits
function showAlert(message) {
   if (!(message==null || message=="")) alert(message);
}

//trim and return a string
function trim(str1) {
    start=0;
    end=0;
    if (str1=="") return str1;
    var str=new String(str1);
   	for (var i = 0; i < str.length; i++) {
   		if (str.charAt(i) != " ") { start = i; break;}
   	}
   	for (var i = str.length-1; i >=0; i--) {
   		if (str.charAt(i) != " ") { end = i+1; break;}
   	}
   	return str.substring(start,end);
}

// checks if a field is empty
function isEmpty(field,message) {
 if (isEmptyV(field.value,message)) { field.focus(); return true }
 else return false;
}

// checks if a value is empty
function isEmptyV(value,message) {
 value=trim(value);
 if (value==null || value=="") { showAlert(message); return true; }
 return false;
}

// checks if a field contains a number
function isNumber(field,message) {
 value=trim(field.value);
 if (!isNumberV(value,message)) { field.focus(); return false }
 else return true;
}

// checks if a value is a number
function isNumberV(value,message) {
	value=trim(value);
	oneDecimal = false
	inputStr = value.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);
		if (i == 0 && oneChar == "-") {
			continue;
		}
		if (oneChar == "." && !oneDecimal) {
			oneDecimal = true;
			continue;
		}
		if (oneChar < "0" || oneChar > "9") {
			showAlert(message);
			return false;
		}
	}
	return true
}

// checks if a field contains a positive number
function isPosNumber(field,message) {
 value=trim(field.value);
 if (!isPosNumberV(value,message)) { field.focus(); return false }
 else return true;
}

// checks if a value is a positive number
function isPosNumberV(value,message) {
	value=trim(value);
	oneDecimal = false
	inputStr = value.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);
		if (oneChar == "." && !oneDecimal) {
			oneDecimal = true;
			continue;
		}
		if (oneChar < "0" || oneChar > "9") {
			showAlert(message);
			return false;
		}
	}
	return true
}

// checks if a field is an integer
function isInteger(field,message) {
 value=trim(field.value);
 if (!isIntegerV(value,message)) { field.focus(); return false }
 else return true;
}

// checks if a value is an integer
function isIntegerV(value,message) {
	value=trim(value);
	inputStr = value.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
			continue
		}
		if (oneChar < "0" || oneChar > "9") {
			showAlert(message);
			return false
		}
	}
	return true
}

// checks if a field is a positive integer
function isPosInteger(field,message) {
 value=trim(field.value);
 if (!isPosIntegerV(value,message)) { field.focus(); return false; }
 else return true;
}

// checks if a value is a positive integer
function isPosIntegerV(value,message) {
	value=trim(value);
	inputStr = value.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar < "0" || oneChar > "9") {
			showAlert(message);
			return false
		}
	}
	return true
}

// checks if a field is between two integers
function isBetweenInts(field,first,second,message) {
 value=trim(field.value);
 if (!isBetweenIntsV(value,first,second,message)) { field.focus(); return false }
 else return true;
}

// checks if a value is between two integers
function isBetweenIntsV(value,first,second,message) {
 value=trim(value);
 if (!isIntegerV(value,message)) return false;
 num=parseInt(value,10);
 if (isNaN(num)) { showAlert(message); return false;}
 if (num<first || num>second) { showAlert(message); return false;}
 return true;
}
// checks if a field is between two numbers
function isBetweenNums(field,first,second,message) {
 value=trim(field.value);
 if (!isBetweenNumsV(value,first,second,message)) { field.focus(); return false }
 else return true;
}

// checks if a value is between two numbers
function isBetweenNumsV(value,first,second,message) {
 value=trim(value);
 if (!isNumberV(value,message)) return false;
 num=parseFloat(value,10);
 if (isNaN(num)) { showAlert(message); return false;}
 if (num<first || num>second) { showAlert(message); return false;}
 return true;
}

// checks if a field is a date
function isDate(field,message) {
 value=trim(field.value);
 if (!isDateV(value,message)) { field.focus(); return false }
 else return true;
}

// checks if a value is a date
function isDateV(value,message) {
 value=trim(value);
 if (value.indexOf("/") != -1) separator="/";
 else  if (value.indexOf("-") != -1) separator="-";
 else  { showAlert(message); return false;}
 
 var myarray = value.split(separator,3);
 if (!(isBetweenIntsV(myarray[0],1,12,message))) return false;
 if (!(isBetweenIntsV(myarray[1],1,31,message))) return false;
 if (!(isPosIntegerV(myarray[2],message))) return false;

 dat = Date.parse(value);
 if (isNaN(dat)) { showAlert(message); return false;}
 return true;
}


// checks if a value is zipcode
function isZip(field,str) {
  value = trim(field.value);
  if (!isZipV(value,str)) { field.focus(); return
false; }        
  else return true;
}

function isZipV(value,message) {
  if (value.length == 5) {
    if (!isPosIntegerV(value)) { showAlert(message);
return false; }
  }
  else if (value.length == 10) {
    if (!isPosIntegerV(value.substr(0,5)))  {
showAlert(message); return false; }
    if (!isPosIntegerV(value.substr(6,10)))  {
showAlert(message); return false; }
  }
  else
     { showAlert(message); return false; }
  return true;

}

//check if a value is email
function isEmail(field,str) {
  value = trim(field.value);
  if (!isEmailV(value,str)) { field.focus(); return
false; }        
  else return true;
}

function isEmailV(value,message) {
  invalidChars = " /:,;";
 
  for (i=0; i<invalidChars.length; i++) {
      badChar = invalidChars.charAt(i);
      if (value.indexOf(badChar,0) > -1) {
         showAlert(message); return false;
      }
  }
  atPos = value.indexOf("@",1);
  if (atPos == -1) {
     showAlert(message); return false;
  }
  if (value.indexOf("@",atPos+1) > -1) {
     showAlert(message); return false;
  }
  periodPos = value.indexOf(".",atPos+2);
  if (periodPos == -1) {
     showAlert(message); return false;
  }
  if (periodPos+3 > value.length)   {
     showAlert(message); return false;
  }
  return true
}

//check for length
function isLength(field,message,length) {
 value = trim(field.value);
 if (!isLengthV(value,message,length)) {
field.focus(); return false; }
 else return true;
}

function isLengthV(value,message,length1) {
  value=trim(value);
  if (value.length !=length1)  {
	showAlert(message);
	return false;
  }
  return true;
}


