//**********************************************************************************************************
//CheckVals function iterates through the form elements of the form that is passed to it.  It determines 
//which function to use based on the 'title' property of the form element.
//
//To use this function to validate a form, ...
//
//**********************************************************************************************************
function CheckVals(form){
//document.AddImage.i
//alert (form.name);

var iLen = form.length - 1;
//alert("iLen = " + iLen);
//alert("form.length = " + form.length);
var retVal = true;
//alert(retVal);
var i = 0;
while (i < iLen) {
//alert(i);
//alert(form[i].title);


	//alert("Loop " + i + ": Is " + form[i].name + ": " + form[i].title);
	//alert(form[i].title == 'CheckExist');
	
	//if (form[i].title == 'CheckExist'){
	//	CheckExist(form[i]);
	//	}

	if(InStr(form[i].title, 'CheckExist') >= 0){
		//alert(InStr(form[i].title, 'CheckExist'));
		var retExist;
		retExist = CheckExist(form[i]);
			if (retExist == false){
				retVal = false;
				//return false;
			}
		}

	if(InStr(form[i].title, 'CheckExists') >= 0){
		//alert(InStr(form[i].title, 'CheckExists'));
		var retExist;
		retExist = CheckExist(form[i]);
			if (retExist == false){
				retVal = false;
				//return false;
			}
		}

	if(InStr(form[i].title, 'Custom') >= 0 && form.format_id.value!=1){
		//alert(form.format_id.value);
		var retExist;
		retExist = CheckExist(form[i]);
			if (retExist == false){
				retVal = false;
				//return false;
			}
		}
		
	//alert("Loop " + i + ": Is " + form[i].name + "'s title = CheckEmail?");
	//alert(form[i].title == 'CheckEmail');
	
//	if (form[i].title == 'CheckEmail'){
//		CheckEmail(form[i]);
//		}

	if(InStr(form[i].title, 'CheckEmail') >= 0){
		if(InStr(form[i].title, 'req') >= 0){
			var retEmail
			retEmail = CheckEmail(form[i], 1);
			if (retEmail == false){
				retVal = false;
				//return false;
			}			
		}else{
			retEmail = CheckEmail(form[i], 0);
			if (retEmail == false){
				retVal = false;
				//return false;
			}			
		}
	}

	if(InStr(form[i].title, 'CheckDate') >= 0){
		if(InStr(form[i].title, 'req') >= 0){
			var retDate;
			retDate = CheckDate(form[i], 1);
			if (retDate == false){
				retVal = false;
				//return false;
			}			
		}else{
			retDate = CheckDate(form[i], 0);
			if (retDate == false){
				retVal = false;
				//return false;
			}			
		}
	}
		
	if(InStr(form[i].title, 'CheckNum') >= 0){
		//alert(InStr(form[i].title, 'CheckNum'));
		//CheckNum(form[i]);
		//}
		if(InStr(form[i].title, 'req') >= 0){
			var retNum;
			retNum = CheckNum(form[i], 1);
			if (retNum == false){
				retVal = false;			}			
		}else{
			retNum = CheckNum(form[i], 0);
			if (retNum == false){
				retVal = false;
			}			
		}
	}
	
//CheckState(element, req)	
	if(InStr(form[i].title, 'CheckState') >= 0){
		if(InStr(form[i].title, 'req') >= 0){
			var retState;
			retState = CheckState(form[i], 1);
			if (retState == false){
				retVal = false;			
			}			
		}else{
			retState = CheckState(form[i], 0);
			if (retState == false){
				retVal = false;
			}			
		}
	}	
	
	i++ ;
	
	}
//alert(retVal);	
return retVal;	
}


//**********************************************************************************************************
//
// Description: valid date.  sDate must be in d/m/yy or d/m/ccyy format.
//              
//**********************************************************************************************************

function isDateOLD(datein) {
    var indate=datein;
    if (indate.indexOf("-")!=-1)   {
        var sdate = indate.split("-")
    }
    else {
        var sdate = indate.split("/")
    }
    var chkDate = new Date(Date.parse(indate))
    var cmpDate = (chkDate.getMonth()+1)+"/"+(chkDate.getDate())+"/"+(chkDate.getYear())
    var indate2 = (Math.abs(sdate[0]))+"/"+(Math.abs(sdate[1]))+"/"+(Math.abs(sdate[2]))
    if (indate2 != cmpDate){
        //alert("You've entered an invalid date or date format. Please use the MM/DD/YY format.");
        return false ;
    }
    else {
        if (cmpDate=="NaN/NaN/NaN"){
            //alert("You've entered an invalid date or date format. Please use the MM/DD/YY format.");
			return false ;
        }
     }
return true;
}






// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************

function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
return false;
}

month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn`t have 31 days!")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn`t have " + day + " days!");
return false;
}
}
return true; // date is valid
}















//**********************************************************************************************************
//The following function is probably obsolete since the .req functionality was added to the other functions,
//but I left it in anyways just in case.
//
//CheckExist function checks the validity of an HTML form element.
//To use it, place a 'title' property equal to 'CheckExist' in the form element you wish to check.
//The following HTML tag will use CheckExist to validate the form element.
//
//i.e. - <input type="text" name="name" value="" title="CheckExist">
//
//**********************************************************************************************************
function CheckExist(element){
	//alert(element.value)
	if (element.value == '' || element.value == ' '){
	alert(element.name + ' is a required field!');
	return false;
	}
}


//**********************************************************************************************************
//
//
//**********************************************************************************************************
function CheckDate(element, req){
	//alert(element.value)


	if (req == 1){
		if (element.value == '' || element.value == ' '){
		alert(element.name + ' is a required field!');
		return false;
		}
	}
	
	
	var iRet = isDate(element.value);
	//alert(iRet);
	if (iRet == false && Len(element.value) > 0){
		alert(element.name + ' must be in the following format: mm/dd/ccyy or mm/dd/yy');
		return false;
	}
	return true;
	
	
	
	
}

//**********************************************************************************************************
//
//
//**********************************************************************************************************
function CheckBox(element){
	//alert(element.value)
}

//**********************************************************************************************************
//
//
//**********************************************************************************************************
function CheckState(element, req){
	//alert(element.value)
	var boState;
	
	if (req == 1){
		if (element.value == '' || element.value == ' '){
		alert(element.name + ' is a required field!');
		return false;
		}
	}
	
	arStateCode = new Array('AK', 'AL', 'AR', 'AS', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'GU', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'PR', 'RI', 'SD', 'TN', 'TX', 'UT', 'VA', 'VI', 'VT', 'WA', 'WI', 'WV', 'WY') ;	

	arState = new Array('Alaska', 'Alabama', 'Arkansas', 'American Samoa', 'Arizona', 'California', 'Colorado', 'Connecticut', 'District of Columia', 'Delaware', 'Florida', 'Georgia', 'Guam', 'Hawaii', 'Iowa', 'Idaho', 'Illinois', 'Indiana', 'Kansas', 'Kentucky', 'Louisiana', 'Massachusetts', 'Maryland', 'Maine', 'Michigan', 'Minnesota', 'Missouri', 'Mississippi', 'Montana', 'North Carolina', 'North Dakota', 'Nebraska', 'New Hampshire', 'New Jersey', 'New Mexico', 'Nevada', 'New York', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Puerto Rico', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Virginia', 'Virgin Islands', 'Vermont', 'Washington', 'Wisconsin', 'West Virginia', 'Wyoming');

	for(x=0; x<arState.length; x++){
		if(element.value == arState[x].value){
			boState = true;
		}
	
	}

	for(n=0; n<arStateCode.length; n++){
		if(element.value == arStateCode[n]){
			boState = true;
		}
	
	}
if(boState == true){
	//alert('True');
	return true;
}else{
	alert('The value you entered in ' + element.name + ' is not a valid US state.');
	return false;
}	
									
}

//**********************************************************************************************************
//CheckNum function checks the validity of an HTML form element that is supposed to be numeric.
//To use it, place a 'title' property equal to 'CheckNum' in the form element you wish to check.
//To make this a required field, append '.req' to 'CheckNum'.  The following HTML tag will use 
//CheckNum to validate the form element.  It is a required field.
//
//i.e. - <input type="text" name="num" value="" title="CheckNum.req">
//
//**********************************************************************************************************
function CheckNum(element, req){

	if (req == 1){
		if (element.value == '' || element.value == ' '){
		alert(element.name + ' is a required field!');
		return false;
		}
	}
	
	//alert(element.value)
	if (isNaN(element.value) == true && Len(element.value) > 0){
	alert(element.name + ' must be a numeric value!');
	return false;
	}
}

//**********************************************************************************************************
//CheckEmail function checks the validity of an e-mail that has been submitted through a HTML form.
//To use it, place a 'title' property equal to 'CheckEmail' in the form element you wish to check.
//To make this a required field, append '.req' to 'CheckEmail'.  The following HTML tag will use 
//CheckEmail to validate the form element.  It is a required field.
//
//i.e. - <input type="text" name="email" value="" title="CheckEmail.req">
//
//**********************************************************************************************************
function CheckEmail(element, req){

	//alert('this is CheckEmail');
	//alert(InStr(element.value, '@')) ;

	if (req == 1){
		if (element.value == '' || element.value == ' '){
		alert(element.name + ' is a required field!');
		return false;
		}
	}
	
	if (Len(element.value) > 0){

		arReq = new Array('@', '.') ;    

		for (y=0; y<arReq.length; y++){
			//alert(arInv[z]);
			if (InStr(element.value, arReq[y]) == -1) {
				alert(element.name + " does not contain a valid e-mail address.  A '" + arReq[y] + "' is required.");
				return false;
			}
		}

		//if (InStr(element.value, '@') == -1) {
		//	alert(element.name + " does not contain a valid e-mail address.  A '@' is required.");
		//	return false;
		//}

		//if (InStr(element.value, '.') == -1) {
		//	alert(element.name + " does not contain a valid e-mail address.  A '.' is required.");
		//	return false;
		//}

		arInv = new Array('!', '#', '$', '%', '^', '&', '*', '(', ')', '=', '+', '{', '}', '[', ']', '|', ';', ' ', ':', "'", '/', '?', '>', ',', '<', '\\') ;    

		//alert (arInv[0]) ;
		//alert (arInv[1]) ;

		for (z=0; z<arInv.length; z++){
			//alert(arInv[z]);
			if (InStr(element.value, arInv[z]) > -1){
				alert("'" + arInv[z] + "'" + " is an invalid character for an e-mail address!");
				return false;
			}
		}

	}
	
return true;
}





//function validate(frmName){
////frmName is the name of the form
//frmLength = document.frmName.length;
//isEmpty = false;
//
//for (i=0; i&lt;frmLength;i++){
//    if(document.frmName[i].value == "")
//        isEmpty = true;  //one element is empty
//}
//
//if(istEmpty)
//    return false;  //at least one element is empty so return false
//return true; //all fields are non-empty so return true;
//
//}

//**********************************************************************************************************
//
//The Len function is designed to mimic the VB len() function
//
//**********************************************************************************************************

function Len(str)
/***
        IN: str - the string whose length we are interested in

        RETVAL: The number of characters in the string
***/
{  return String(str).length;  }


//**********************************************************************************************************
//
//The mid function is designed to mimic the VB mid() function
//
//**********************************************************************************************************

function Mid(str, start, len)
/***
        IN: str - the string we are LEFTing
            start - our string's starting position (0 based!!)
            len - how many characters from start we want to get

        RETVAL: The substring from start to start+len
***/
{
//	alert(str);
//	alert(start);
//	alert(len);
			
        // Make sure start and len are within proper bounds
        if (start < 0 || len < 0) return "";

        var iEnd, iLen = String(str).length;
        if (start + len > iLen)
                iEnd = iLen;
        else
                iEnd = start + len;

        return String(str).substring(start,iEnd);
}


//**********************************************************************************************************
//
//The InStr function is designed to mimic the VB InStr() function
//
//**********************************************************************************************************

function InStr(strSearch, charSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is <B>not</B>
                           found, -1 is returned.)
Requires use of:
	<A href="http://www.4guysfromrolla.com/webtech/code/Mid.shtml">Mid function</A>
	<A href="http://www.4guysfromrolla.com/webtech/code/Len.shtml">Len function</A>
*/
{
var iLen = Len(charSearchFor);

	for (i=0; i < Len(strSearch); i++)
	{
	    if (charSearchFor == Mid(strSearch, i, iLen))
	    {
			return i;
	    }
	}
	return -1;
}
