/*
#########################################################################################
##Author      : Torsten Heinze 
##Version     : 1.0
##Created     : 06/09/98
##Updated     : 03/14/02
##Description : JavaScript Library for Validatation
##Requires    : -
#########################################################################################
*/

function validateEmpty(oField,sTitle){
	if(Trim(oField.value)==""){
		alert("The field "+sTitle+" is required! Please enter the requested information.");
		oField.focus();
		return false;
	}else{
		return true;
	}
}

function validateText(oField,sTitle,minLength,maxLength){
	sString=Trim(oField.value);
	if (sString.length<minLength){
		alert("Value in field "+sTitle+" must contain at least "+minLength+" character(s).");
		oField.focus();
		return false;
	}else if (sString.length>maxLength){
		alert("Value in field "+sTitle+" can not contain more than "+maxLength+" characters.");
		oField.focus();
		return false;
	}else{
		return true;
	}
}

function validateCheckboxList(oField){
	if(oField){
	 if(typeof(oField.length)!="undefined"){
		for (var i=0;i<oField.length;i++){if(oField[i].checked) return true}
	 }else{
		return oField.checked;
	 }
	}
	return false;
}

function validateMatch(oField1,oField2,sTitle){
	if (oField1.value!=oField2.value){
		alert(sTitle+" does not match first entry. Please try again.");
		oField2.focus();
		return false;
	}else{
		return true;
	}
}

function validateEmail(oField,sTitle) {
  if(oField.value=="") return true;
  var error = ""

  var emailFilter = new RegExp("^.+@.+\\..{2,4}$","ig");
  if (!(emailFilter.test(oField.value))) { 
     alert("Please enter a valid email address in field '"+sTitle+"'.\n");
     oField.focus();
     return false;
  }

  var illegalChars = new RegExp("[\\s\\(\\)\\<\\>\\,\\;\\:\\\\\/\"\\[\\]]","ig");
  if(oField.value.match(illegalChars)) {
  	 if(oField.value.match(illegalChars).length>0) {
     	alert("The email address in field '"+sTitle+"' contains at least one invalid character.\n\nThe invalid character is: \" "+oField.value.match(illegalChars)[0]+" \"");
     	oField.focus();
     	return false;
     }
  }
  
  return true;
} 

function validateDate(oField){
	var dtStr=oField.value;
	if(dtStr=="") return true;
	
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;

	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")
    	oField.focus();
	    return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
    	oField.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")
    	oField.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)
    	oField.focus();
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
    	oField.focus();
		return false
	}
	return true
}

//string functions:
function Trim(str) { 
	str = str + "";
	return RTrim(LTrim(str))
}

function LTrim(str) { 
	str = str + "";
	while (str.substring(0,1)==" ") str = str.substring(1);
	return str;
}

function RTrim(str){ 
	str = str + "";
	while (str.substring(str.length-1)==" ") str = str.substring(0,str.length-1);
	return str;
}

function Right(str,i) {
	str = str + "";
	return str.substring(str.length-i,str.length);
}

function Chr(charcode) { 
	return String.fromCharCode(charcode);
}

function IsEmpty(value) { 
	return (SE(value==""));
}

function IsNumeric(value) { 
	return (!isNaN(value*1));
}

function IsEven(i) { 
	return ((i % 2)==0);
}

// standard empty
function SE(value) { 
	return (((value=="") || ((value+"")=="undefined") || (value+""=="null"||typeof(value)=="null")) ? "" : value);
}

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
}

