// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dateFld){
   var dtStr = dateFld.value;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		dateFld.selectAll();
		dateFld.focus();
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		dateFld.focus();
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		dateFld.focus();
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		dateFld.focus();
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		dateFld.focus();
		return false
	}
   return true
}

var numb = '0123456789.,';
function isValid(parm,val) {
  if (parm == "") return true;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}
function isDecimal(parm) {
   return isValid(parm,numb);
}

function ValidateAmount (field) {
   if (! isDecimal(field.value)) {
      alert("Please enter a valid amount");
      field.focus();
      return true;
   }
   return false;
}

function validate_required(field) {
	with (field) {
		if (value==null||value=="") {
			className="error";
			return false;
		} else {
			className="info";
			return true;
		}
	}
}
function validate_email(field) {
	with (field) {
		if (value==null||value == "" ) return true;
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2){
			className="error";
			return false;
		} else {
			className="info";
			return true;
		}
	}
} 
function trim(sString) {
   while (sString.substring(0,1) == ' ')
      sString = sString.substring(1, sString.length);

   while (sString.substring(sString.length-1, sString.length) == ' ')
      sString = sString.substring(0,sString.length-1);

   return sString;
}

function formatZipCode(thisZip,e){
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
if (keycode == 8 || keycode==46) return;

	zip = trim(thisZip.value);
   var zipRE = /^\d\d\d\d\d-[0-9]+$/;
	if (zip.length > 0) {
      if (zip.match(zipRE))
         return true;
		if ((zip.length == 5) && (parseInt(zip) == zip)) {
			thisZip.value = zip + "-";
		} else if (zip.length > 5) {
			if (parseInt(zip) == zip) {
				zip5 = zip.substr(0,5);
				zipR = zip.substr(5);
				zipPre = zip5 + "-";
				if (zipR.length <= 4) {
					thisZip.value = zipPre + zipR;
				} else {
					zip5 = zipR.substr(0,4);
					thisZip.value = zipPre + zip5;
				}
			}
		}
	}
	return true;
}

function formatPhone(thisPhone){
	phone = trim(thisPhone.value);
	if (phone.length > 0) {
		if ((phone.length == 3) && (parseInt(phone) == phone)) {
			if (phone.substr(0,1) != '(') {
				thisPhone.value = "(" + phone + ") ";
			}
		} else if (phone.length > 3) {
			if (parseInt(phone) == phone) {
				phone3 = phone.substr(0,3);
				phoneR = phone.substr(3);
				phonePre = "(" + phone3 + ") ";
				if (phoneR.length <= 3) {
					thisPhone.value = phonePre + phoneR;
//					return true;
				} else {
					phone3 = phoneR.substr(0,3);
					phoneR = phoneR.substr(3);
					thisPhone.value = phonePre + phone3 + "-" + phoneR;
				// return true;
				}
			} else {
				var phoneRE = /^\(\d\d\d\) $/;
				phonePre = phone.substr(0,6);
				if (phonePre.match(phoneRE)) {
					phoneR = phone.substr(6);
					if (phoneR.length > 3) {
						if (parseInt(phoneR) == phoneR) {
							phone3 = phoneR.substr(0,3);
							phoneR = phoneR.substr(3);
							thisPhone.value = phonePre + phone3 + "-" + phoneR;
							// return true;
						}
					}
				}
			}
		}
	}
	return true;
}

function validate_form(thisform) {
	var is_error = false;
	var error_msg = "Please correct the following:";
	with (thisform) {
		if (! validate_required(org_name)) {
  			org_name.focus();
			error_msg = error_msg + "\n\r" + "- Organization's Name must be filled out!";
  			is_error=true;
		}
		if (! validate_required(org_city)) {
			if (! is_error)
  				org_city.focus();
  			error_msg = error_msg + "\n\r" + "- Organization's City must be filled out!";
  			is_error=true;
		}
		if (! validate_required(org_state)) {
			if (! is_error)
  				org_state.focus();
  			error_msg = error_msg + "\n\r" + "- Organization's State must be filled out!";
  			is_error=true;
		}
		if (! validate_required(captchacode)) {
			if (! is_error)
  				captchacode.focus();
  			error_msg = error_msg + "\n\r" + "- Image value must be filled out!";
  			is_error=true;
		}
		/*
		if (! validate_required(contact_phone)) {
			if (! is_error)
  				contact_phone.focus();
  			error_msg = error_msg + "\n\r" + "- Contact Phone must be filled out!";
  			is_error=true;
		}
		if (! validate_email(contact_email)) {
			if (! is_error)
  				org_state.focus();
  			error_msg = error_msg + "\n\r\n\r" + "- Email is not in the correct format!";
  			error_msg = error_msg + "\n\r" + "        Correct Format: aaa@bbbb.ccc";
  			error_msg = error_msg + "\n\r" + "        Example: bob@anywhere.com";
  			is_error=true;
		}
		*/
	}
	if (is_error) {
		alert(error_msg);
		return false;
	}
	return true;
}

function validate_login (thisform) {
	with (thisform) {
		if (! validate_required(login)) {
  			login.focus();
  			alert ("Please enter your login id");
  			return false;
		}
		if (! validate_required(password)) {
  			password.focus();
  			alert("Please enter your password");
  			return false;
  		} else {
         document.dummy.login.value = login.value;
  			document.dummy.password.value = hex_md5(password.value);
  			document.dummy.submit();
  			return false;
		}
	}
	return true;
}

function validate_user (thisform) {
	var is_error = false;
	var error_msg = "Please correct the following:";
	var bUpdate = document.getElementById("submit").value;

	with (thisform) {
		if (! validate_required(user_name)) {
  			user_name.focus();
			error_msg = error_msg + "\n\r" + "- Please enter a user Name.";
  			is_error=true;
		}
		if (bUpdate == 'Add')
		if (! validate_required(password)) {
			if (! is_error)
  				password.focus();
  			error_msg = error_msg + "\n\r" + "- Please enter a password";
  			is_error=true;
		}
		if (bUpdate == 'Add')
		if (! validate_required(confirmp)) {
			if (! is_error)
  				confirmp.focus();
  			error_msg = error_msg + "\n\r" + "- Please enter a password confirmation";
  			is_error=true;
		}
		if (! validate_required(full_name)) {
			if (! is_error)
  				full_name.focus();
  			error_msg = error_msg + "\n\r" + "- Please enter the full name";
  			is_error=true;
		}
		if (! validate_required(short_name)) {
			if (! is_error)
  				short_name.focus();
  			error_msg = error_msg + "\n\r" + "- Please enter the short name";
  			is_error=true;
		}
		if (password.value != confirmp.value) {
			password.className = "error";
			confirmp.className = "error";
			if (! is_error)
  			   password.focus();
  			error_msg = error_msg + "\n\r" + "- Passwords do not match";
  			is_error=true;
  		}
   } 
	if (is_error) {
		alert(error_msg);
		return false;
	}
	with (thisform) {
      if (password.value != "") {
         password.value = hex_md5(password.value);
         confirmp.value = hex_md5(confirmp.value);
      }
   }

   return true;
}

function validateSign(signField) {
   var signType;
   if (signField == 1) {
      signType = document.getElementById("sign_standard").value;
      if (signType.toLowerCase() != "choose one") {
         document.getElementById("sign_premier").value = "Choose One";
         return true;
      }
      return true;
   }
   signType = document.getElementById("sign_premier").value;
   if (signType.toLowerCase() != "choose one") {
      document.getElementById("sign_standard").value = "Choose One";
   }
   return true;
}