//-------------------------------------
// dfeehely - Feb 16, 2005
// check form values.
//-------------------------------------
function checkForm(theform, emailfield, ignoreList) {
	var str;
	var strIgnoreList;
	var strEmailField;
	var arrIgnoreList;
	var arrEmailFieldList;
	var boolIgnoreFlag;

	str="";
	strIgnoreList = new String(ignoreList);
	strEmailField = new String(emailfield);
	arrIgnoreList = strIgnoreList.split(',');
	arrEmailFieldList = strEmailField.split(',');
	boolIgnoreFlag=false;


	for(i=0; i < theform.elements.length; i++) {
		if(theform.elements[i].name != "") {
			boolIgnoreFlag=false;
			//Check if current field is on ignore list
			for (var m=0; m <  arrIgnoreList.length; m++) {
				if(theform.elements[i].name == arrIgnoreList[m].toString()) {
					boolIgnoreFlag=true;
				}
			}

			if (boolIgnoreFlag ==  false) {
				// Loop through email fields and validate
				for (var k=0; k <  arrEmailFieldList.length; k++) {
					if(theform.elements[i].name == arrEmailFieldList[k]) {
						if(echeck(theform.elements[i].value) == false) {
							alert("Please enter a valid email address");
							theform.elements[i].focus();
							return false;
						}
					}
				}
				
				if(theform.elements[i].type == "select-one") {
					var strSelRes = theform.elements[i].options[theform.elements[i].selectedIndex].value;
					if((strSelRes == "")||(strSelRes == "none")) {
						alert("Please select a value for " + theform.elements[i].name );
						theform.elements[i].focus();
						return false;
					}
				} else if(theform.elements[i].type == "text") {
					if(theform.elements[i].value == "") {
						alert("Please enter a value for " + theform.elements[i].name );
						theform.elements[i].focus();
						return false;
					}
				} else if(theform.elements[i].type == "textarea") {
					var strVal;
					strVal = "";
					strVal = theform.elements[i].value
					strVal = strReplace(theform.elements[i].value, "\\n", "")
					if(strVal == "") {
						alert("Please enter a value for " + theform.elements[i].name );
						theform.elements[i].focus();
						return false;
					}
				} else if(theform.elements[i].type == "radio") {
					var strRadioName;
					var boolCheckedFlag;
					strRadioName = theform.elements[i].name;
					boolCheckedFlag = false;

					for(j=0;j<theform.elements[strRadioName].length;j++) {
						//alert(theform.elements[strRadioName][j].checked);
						if(theform.elements[strRadioName][j].checked == true) {
							boolCheckedFlag = true;
						}
					}

					if(boolCheckedFlag == false) {
						alert("Please select a value for " + theform.elements[i].name );
						theform.elements[i].focus();
						return false;
					}
				}


			}
		}
	}

	return true;
}

//---------------------------------
// dfeehely - check email format
//---------------------------------
function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Invalid E-mail ID")
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		alert("Invalid E-mail ID")
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Invalid E-mail ID")
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		alert("Invalid E-mail ID")
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert("Invalid E-mail ID")
		return false
	 }

	 return true					
}

//-------------------------------------
// dfeehely - Feb 16, 2005
// replace string.
//-------------------------------------
function strReplace(strsrc, strfind, strreplace) {
	var thisSrcString;
	var thisRepString;
	var thisResString;
	var thisRegExp;

	thisSrcString = new String(strsrc);
	thisRepString = new String(strreplace);
	thisRegExp = new RegExp(strfind,'gi')

	thisResString = thisSrcString.replace(thisRegExp, thisRepString);
	return thisResString
}
