/**************************************

		ERROR FUNCTIONS...

***************************************/


// error in element
//
//------------------------------
function aErr( objElem, strMsg ) {
	aType = objElem.type;
	alert( "Error: "+ strMsg );
	if (aType == 'text' || aType == 'select-one'){
		objElem.style.backgroundColor="yellow";
		objElem.style.color="red";
		objElem.focus();
	} // if
	return false;
} // errElem


// validated element
//
//------------------------------
function aValid( objElem, aType){
	aType = objElem.type
	if (aType == 'text'|| aType == 'select-one'){
		objElem.style.backgroundColor="#FFFFFF";
		objElem.style.color="blue";
	} // if
	return true;
} // aValid


/****************************************

		VALIDATIONS....

****************************************/
// validate email
//
//-----------------------------
function isValidEmail(objElem, strName) {

    strErr = "Please Enter a valid "+ strName +".";
    sEmail = objElem.value;

    // Return true if this is probably an email address or empty
    // Valid email string must have a @ followed by a period
    if ( ! sEmail.length > 0 ) return aValid(objElem); // Empty email ok

    var Loc1= sEmail.indexOf("@");
    var Loc2= sEmail.lastIndexOf(".");

    if (Loc1 == -1) return aErr(objElem, strErr); // must have an @ imbedded in string
    if (Loc2 < Loc1) return aErr(objElem, strErr); // must have an . after the @
    if (2 > (sEmail.length-Loc2)) return aErr(objElem, strErr); // more than 2 characters after .

    return aValid(objElem);

} // isValidEmail


//
//
//-----------------------------
function isSelected( objElem, strName ) {

    if ( objElem.options[ objElem.selectedIndex ].value <= 0 ) {
        return aErr(objElem, strName +" Requires a Selected Value.");
    } else {
        return aValid(objElem); 
    } // if

} // isSelected


// validate phone
//
//-----------------------------
function isValidPhone(objElem, strName)  {
    // Return false if field isn't in Phone Format (10 digit) or empty"
    // Also reformat phone to xxx-xxx-xxxx format
    var i,Tmp,String,NewString;
    String=objElem.value;

    var strErr = "Enter valid "+ strName +" Number: ###-###-####"

    if (String.length == 0) {

        return aValid(objElem);

    } else {

        NewString="";
        var NumChars = 0;
        var OKchars = '0123456789';

        for (var i=0; i < String.length; i++)  {

            Tmp = String.substring (i, i+1);

            if (OKchars.indexOf (Tmp, 0) > -1) {
                NewString=NewString+Tmp;
                NumChars=NumChars+1;
            }//if (OKchars.indexOf (Tmp, 0) > -1) 
        }//for (var i=0; i < String.length; i++)  

        if (10==NumChars) { // ok reformat
            String="";
        } else {
            return aErr(objElem, strErr);
        }//if (10<=NumChars)  			

        String+=NewString.substring(0,3)+"-";
        String+=NewString.substring(3,6)+"-";
        String+=NewString.substring(6,10)+" ";
        String+=NewString.substring(10,NewString.length);
        objElem.value=String;

        return aValid(objElem);
    } // if (String.length == 0) 

    return aErr(objElem, strErr);
} // function isPhoneFmt (objElem)

//
// valid number
//
//------------------------------
function isValidInt(objElem, strName){
	var strErr = strName +" is not a Valid Number!";
	var theVal = objElem.value;
	if(isNaN(theVal)) {
        return aErr(objElem,strErr);
	} else {
		return aValid(objElem);
	} // if
} // isValidInt


// element is required
//
//------------------------------
function isRequired( objElem, strName ) {
	var theReturn = false;
	var strErr=strName+" is Required!";
	if(objElem.length > 0){
		for(i=0;i<objElem.length;i++){
			if(objElem[i].checked || objElem[i].selected){
				if(objElem[i].value.length>0)
					theReturn = true;
			} // if
		} // for
		if(!theReturn)
			return aErr(objElem, strErr);
		else
			return aValid(objElem);
	}else{
		if(objElem.value.length>0)
			return aValid(objElem);
		else
			return aErr(objElem, strErr);
	} // if
} // isRequired


// isDate
//
//		Validates the Dates
//
// ---------------------------------------
function isDate(objElem, strName, type) {
	var errMsg;
	var dtStr = objElem.value;
	var daysInMonth = daysArray(12)
	var pos1=dtStr.indexOf("/")
	var pos2=dtStr.indexOf("/",pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear

	if (type){
		if(dtStr=='ne' || dtStr=='NE'){
			return aValid(objElem);
		} else {
			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)
			} // for
			month=parseInt(strMonth)
			day=parseInt(strDay)
			year=parseInt(strYr)
			if (pos1==-1 || pos2==-1){
				errMsg = "The date format should be : mm/dd/yyyy for "+ strName;
				return aErr(objElem,errMsg);
			} // if
			if (strMonth.length<1 || month<1 || month>12){
				errMsg = "Please enter a valid month for "+ strName;
				//alert("Please enter a valid month")
				return aErr(objElem,errMsg);
			} // if
			if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
				errMsg = "Please enter a valid day for "+ strName;
				//alert("Please enter a valid day");
				return aErr(objElem,errMsg);
			} // if
			if (strYear.length != 4 || year==0 || year<1900 || year>2100){
				//alert("Please enter a valid 4 digit year between "+ 1900 +" and "+ 2100)
				errMsg = "Please enter a valid 4 digit year for "+ strName;
				return aErr(objElem,errMsg);
			} // if
			if (dtStr.indexOf("/",pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, "/"))==false){
				//alert("Please enter a valid date");
				errMsg = "Please enter a valid date for "+ strName;
				return aErr(objElem,errMsg);
			} // if
		} //
	} else {
		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)
		} // for
		month=parseInt(strMonth)
		day=parseInt(strDay)
		year=parseInt(strYr)
		if (pos1==-1 || pos2==-1){
			errMsg = "The date format should be : mm/dd/yyyy for "+ strName;
			return aErr(objElem,errMsg);
		} // if
		if (strMonth.length<1 || month<1 || month>12){
			errMsg = "Please enter a valid month for "+ strName;
			//alert("Please enter a valid month")
			return aErr(objElem,errMsg);
		} // if
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
			errMsg = "Please enter a valid day for "+ strName;
			//alert("Please enter a valid day");
			return aErr(objElem,errMsg);
		} // if
		if (strYear.length != 4 || year==0 || year<1900 || year>2100){
			//alert("Please enter a valid 4 digit year between "+ 1900 +" and "+ 2100)
			errMsg = "Please enter a valid 4 digit year for "+ strName;
			return aErr(objElem,errMsg);
		} // if
		if (dtStr.indexOf("/",pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, "/"))==false){
			//alert("Please enter a valid date");
			errMsg = "Please enter a valid date for "+ strName;
			return aErr(objElem,errMsg);
		} // if
		return aValid(objElem);
	} // if
} // isDate

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}
   } // for
   return this;
} // daysArray

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 );
} // daysInFebruary

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;
    } // for
    return returnString;
} // stripCharsInBag

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;
	} // for
	// All characters are numbers.
	return true;
} // isInteger
// ---------------------------------------


