function selectOption(selObj, optVal){
	for (var i=0; i<selObj.options.length; i++){
		if (selObj.options[i].value == optVal){
			selObj.options[i].selected = true;
			break;
		}
	}
}


function validEmail(str) {
//Function downloaded from http://www.webreference.com/js/tips/000310.html
  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
    return true;
  }
  return false;
}



function validateForm(theFormObj, requiredFields){
	errMsg = "";
	focusField = "";

	for(var i=0; i<requiredFields.length; i++){
		validateField(requiredFields[i]);
	}

	 if(errMsg == ""){
	 	//Everything OK submit form
	 	return true;
	 } else {
	 	errMsg = "We found some errors while processing your information:\n\n" + errMsg;
	 	errMsg += "\nPlease verify this to continue with your submission.";
	 	alert(errMsg);
	 	if(focusField != ""){
	 		eval("theFormObj." + focusField + ".focus();");
	 	}
		return false;
	 }
	return false;
  

}

function validateField(field){
//Validate a field according to its type

	for(i=0; i<field.fInvalidConds.length; i++){
		////// TEXT FIELDS
		if (field.fType == "text" || field.fType == "hiddentext"){
			invalidVal = eval("field.fObj.value" + field.fInvalidConds[i]); //return true if the field has an invalid value
		}

		/////// SELECT FIELDS
		if (field.fType == "select"){
			invalidVal = eval("field.fObj.options[field.fObj.selectedIndex].value" + field.fInvalidConds[i]); //return true if the field has an invalid value
		}

		/////// EMAIL FIELDS 
		if (field.fType == "email"){
			invalidVal = eval("!validEmail(field.fObj.value)"); //return true if the field has an invalid value
		}
		if (invalidVal){
			errMsg += "- " + field.fErrMsgs[i] + "\n";
			focusField = (focusField == "" && field.fType!="hiddentext") ? field.fObj.name : focusField;
		}

	}	
}

//This is a definition of an object to contain required fiels.. used for form validation
function ReqField(fObj, fType, fErrMsgs, fInvalidConds){
	this.fObj = fObj;
	this.fType = fType;
	this.fErrMsgs = fErrMsgs; //An array of error messages to each invalid condition
	this.fInvalidConds = fInvalidConds; //An array of invalid conditions for the field
}



//This is a definition of an object to contain files submitted for a resource.. used in Resource database
function SubmitFile(sysFilename, origFilename, fileTitle){
	this.sysFilename = sysFilename;
	this.origFilename = origFilename;
	this.fileTitle = fileTitle;
}

/*
Change Cell Backgrounds -
� Shivaji Basu (www.shivbasu.com)
To add more shock to your site, visit www.DHTML Shock.com
*/

function cOn(td){
 if(document.getElementById||(document.all && !(document.getElementById))){
  td.style.backgroundColor="#E6E9F0";
 }
}

function cOut(td){
if(document.getElementById||(document.all && !(document.getElementById))){
  td.style.backgroundColor="#FFFFFF";
 }
}

/*  END OF SHIVAJUS CODE */


function showPageJS(page){
	theForm = document.forms["showPage"];
	if (theForm == null){
		alert(">>> Form does not exist");
	} else {
		theForm.page.value = page;
		theForm.target="_self";
		theForm.submit();
	}

	
}
function showPage2(page){
	theForm = document.forms["showPage"];
	thewindow = window.open("", "open", "width=760,height=760,scrollbars=1,location=0,toolbar=0,status=0,resizable=1");
	if (theForm == null){
		alert(">>> Form does not exist");
	} else {
		theForm.page.value = page;
		theForm.target="open";
		theForm.submit();
	}

}

function passHintValues(srcField, destField){
	selValues = "";
	for (i=0; i<srcField.options.length; i++){
		if (srcField.options[i].selected){
			tmp = srcField.options[i].text;
			selValues += (selValues == "") ? tmp : (", " + tmp);
		}
	}

	if (selValues != ""){
		destField.value = (destField.value == "") ? selValues : (destField.value + ", " + selValues);
	}
}

function showHintsPage(fieldKey, formName, fieldName){
	thewindow = window.open("fieldHintsForm.jsp?fieldKey=" + fieldKey + "&formName=" + formName + "&fieldName=" + fieldName, "open", "width=400,height=400,scrollbars=0,location=0,toolbar=0,status=0,resizable=1");
}

function clearDateField(formName, fieldName){
	eval("document.forms[\'" + formName + "\']." + fieldName + ".value = \"\"");
}

function showCommentsPage(obj, objId){
	thewindow = window.open("comments.jsp?obj=" + obj + "&objId=" + objId, "open", "width=400,height=400,scrollbars=0,location=0,toolbar=0,status=0,resizable=1");
}
