// JavaScript Document

function valbutton(chkButton, errMsg) {
// place any other field validations that you require here
  myOption = -1;
  for (i=0; i<chkButton.length; i++) {
    if (chkButton[i].checked) {
      myOption = i;
    }
  }
  if (myOption == -1) {
    alert(errMsg);
    return false;
  }
}



function valother(chkButton,btnOther,testValue,errMsg) {
// This used when 'Yes' or 'No' radio button is selected and a value
// is expected for the in an input field.
// chkButton = radio button label
// btnOther = input field label
// testValue = if this value then btnOther must be present
// errMsg = the error message
  isValid = 'Yes';
  
  for (i=0; i<chkButton.length; i++) {
    if (chkButton[i].checked) {
	  if (chkButton[i].value == testValue) {
	 	if ((btnOther.value == '') || (btnOther.value == null)) {
		  isValid = 'No';
		}
	  }
    }  
  }
  if (isValid == 'No') {
    alert(errMsg);
    return false;
  }
}



function valCompareDates(date,testvalue,errMsg){
	var today = Now();
	var one_day=1000*60*60*24;
	var difference = Math.ceil((today.getTime()-date.getTime())/(one_day));
	if (difference < testvalue){
		alert(errMsg);
	}
}



function valChkBoxOther(chkButton,btnOther,testValue,errMsg) {
// This used when 'Yes' or 'No' radio button is selected and a value
// is expected for the in an input field.
// chkButton = check box label
// btnOther = input field label
// testValue = if this value then btnOther must be present
// errMsg = the error message
  isValid = 'Yes';

  if (chkButton.checked) {
     if (chkButton.value == testValue) {
	    if ((btnOther.value == '') || (btnOther.value == null)) {
		  isValid = 'No';
		}
	 }
  }  
  if (isValid == 'No') {
    alert(errMsg);
    return false;
  }
}



/*
function valRadioOther(rdoButton,rdoOther,testValue,errMsg) {
// This used when 'Yes' or 'No' radio button is selected and a value
// is expected for the in an input field.
// rdoButton = radio button label
// btnOther = input field label
// testValue = if this value then btnOther must be present
// errMsg = the error message
  isValid = 'Yes';
  
  for (var i=0; i < rdoButton.length; i++)
  	if (rdoButton[i].checked) {
		if (rdoButton.value == testValue) {
	 		if ((rdoOther.value == '') || (rdoOther.value == null)) {
				isValid = 'No';
	 		}
		}
	 }
  }
    
  if (isValid == 'No') {
    alert(errMsg);
    return false;
  }
}
*/


function emptyvalidation(entered,errMsg) {
// Emptyfield Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
with (entered)
{
if (value==null || value==""){
  if (errMsg!="") {
    alert(errMsg);
	return false;
  } else {
    return true;
  }
}
}
}



function ValidDate2(DateField,errMsg) {
var IsValid = "yes";
string = DateField.value
var valid = "0123456789/"
var ok = "yes";
var temp;
for (var i=0; i< string.length; i++) {
temp = "" + string.substring(i, i+1);
if (valid.indexOf(temp) == "-1") IsValid = "no";
}
if (string.length != 8) IsValid = "no"
b = string.substring(0, 2) // month
c = string.substring(2, 3)// '/'
d = string.substring(3, 5) // day
e = string.substring(5, 6)// '/'
f = string.substring(6, 8) // year
if (b<1 || b>12) IsValid = "no"
if (c != '/') IsValid = "no"
if (d<1 || d>31) IsValid = "no"
if (e != '/') IsValid = "no"
if (f<0 || f>99) IsValid = "no"
if (b==4 || b==6 || b==9 || b==11){
if (d==31) IsValid = "no"
}
if (b==2){
var g=parseInt(f/4)
if (isNaN(g)) {
  IsValid = "no"
}
if (d>29) IsValid = "no"
if (d==29 && ((f/4)!=parseInt(f/4))) IsValid = "no"
}
if (IsValid=="no") {
  alert(errMsg);
  return false;
}
}



function ValidDate(year,month,day,errMsg){
IsValid = 'Yes';
IsLeapYeap = 'No';
tmpyear = year.value;
tmpmonth = month.value;
tmpday = day.value;
today = new Date();
edate = new Date(tmpyear, tmpmonth - 1, tmpday);

// Validation leap-year / february / day 
if ((tmpyear % 4 == 0) || (tmpyear % 100 == 0) || (tmpyear % 400 == 0)) {
  IsLeapYeap = 'Yes';
}
if ((tmpmonth == 2) && (IsLeapYeap == 'Yes') && (tmpday > 29)) {
   IsValid = 'No';
}
if ((tmpmonth == 2) && (IsLeapYeap == 'No') && (tmpday > 28)) {
   IsValid = 'No';
}
// Validation of other months 
if ((tmpday > 31) && ((tmpmonth == "1") || (tmpmonth == "3") || (tmpmonth == "5") || (tmpmonth == "7") || (tmpmonth == "8") || (tmpmonth == "10") || (tmpmonth == "12"))) {
   IsValid = 'No';
}
if ((tmpday > 30) && ((tmpmonth == "4") || (tmpmonth == "6") || (tmpmonth == "9") || (tmpmonth == "11"))) {
   IsValid = 'No';
}
if (edate < today){
	IsValid = 'No';
}

if (IsValid == 'No'){
	alert(errMsg);
	return false;
}
}



function ValidateEmail(str,errMsg) {
var email = str.value;
//var reg = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{2})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
var reg = /(^[a-z]([a-zA-Z0-9_\.]*)@([a-zA-Z0-9_\.]*)([.][a-zA-Z]{3})$)|(^[a-z]([a-zA-Z0-9_\.]*)@([a-zA-Z0-9_\.]*)([.][a-zA-Z]{2})$)|(^[a-z]([a-zA-Z0-9_\.]*)@([a-zA-Z0-9_\.]*)(\.[a-zA-Z]{3})(\.[a-zA-Z]{2})*$)/i;

if (!reg.test(email)) {
	alert(errMsg);
	return false;
}	
}



function CompareTwoFields(Field1,Field2,errMsg){
  isValid = 'Yes';
  
  if(Field1.value!=Field2.value){
  	isValid = 'No';	
  }
  if (isValid == 'No') {
    alert(errMsg);
    return false;
  }
}