/* ************************************************************
	The Conference Desk : www.theconferencedesk.com
	Developed by:	The MayaTech Corporation
					http://www.mayatech.com
					301-587-1600
	Filename:		js_check_simplesearch_form.js
	Description:	form checking javascript for simple search
	Revisions:		KJ 09/04/05 initial version
****************************************************************
	Form Error Checking 
	Check that required fields are filled-out on the search form 
	
	REQUIRES: 
	prototype trim() function 
			-available in js_std_functions.js
	standard form checking functions
			-available in js_form_validate.js
*/

//determine if the client browser can do fancy HTML DOM stuff
var W3CDOM = (document.getElementsByTagName && document.createElement && document.getElementById);
//initialize error string
var errorstring = '';

/*syntax: onsubmit=return checkRequiredFields(form)
	checks a given list of required fields and displays a list of those still pending response
	requires aligned lists of form field names and form field labels (see 1st & 2nd line in function)
	calls ValidateEmail() from standard functions*/
	function checkRequiredFields(input) {

		var requiredFields = new Array("search_term","start_dt","end_dt");
		var fieldNames = new Array("Search Term(s)","Start Date","End Date");
		var fieldCheck = false;
		
		validForm = false;
		firstError = null;
		errorstring = '';
		errdiv = document.getElementById('errordiv'); //get the visible error string element
		
		// reset visible error string
		while (errdiv.hasChildNodes()){
			errdiv.removeChild(errdiv.lastChild);
		}
		
		//clear the error for all the fields in the form
		for(var i=0; i<input.elements.length; i++){
			fld = input.elements[i];
			if(fld.className.indexOf(' ') >= 0){
				fld.className=fld.className.substring(0,fld.className.lastIndexOf(' '));
			}
			fld.hasError = null;
			fld.onchange = null;
		}
		
		//check the list of required fields for content
		for(var i=0; i < requiredFields.length; i++) {
			thisfield = input.elements[requiredFields[i]];
			
			switch(thisfield.type){ // if it's req'd, make an error
				case "text": 
					if(!check_text(thisfield)){
						if(thisfield.name=="search_term"){
							writeError(thisfield,'At least one search term is required');
						} else {
							writeError(thisfield,fieldNames[i] + ' is required'); 
						}
					}
					break;
				case "select-one":
					if(!check_select(thisfield)){
						writeError(thisfield,fieldNames[i] + ' is required'); 
					}
					break;
				case "select-multiple":
					if(!check_select(thisfield)){
						writeError(thisfield,fieldNames[i] + ' is required'); 
					}
					break;
				case "checkbox":
					if(!check_checkbox(thisfield)){
						writeError(thisfield,fieldNames[i] + ' is required'); 
					}
					break;
				case "radio":
					if(!check_radio(thisfield)){
						writeError(thisfield,fieldNames[i] + ' is required'); 
					}
					break;
				case "textarea":
					if(!check_text(thisfield)){
						writeError(thisfield,fieldNames[i] + ' is required'); 
					}
					break;
				case "password":
					if(!check_text(thisfield)){
						writeError(thisfield,fieldNames[i] + ' is required'); 
					}
					break;
			}//end of switch
		}//end of form field loop
		
		//check that the datefields are formatted correctly
		if (input.elements["start_dt"].value.trim()!="" && !isvaliddate(input.elements["start_dt"].value.trim(),false)) {
			writeError(input.elements["start_dt"],'Start Date must be a valid date'); 
		}
		if (input.elements["end_dt"].value.trim()!="" && !isvaliddate(input.elements["end_dt"].value.trim(),false)) {
			writeError(input.elements["end_dt"],'Ending Date must be a valid date'); 
		}
		
		//if the browser is DOM capable, output the error messages on the screen
		if (!W3CDOM){
			alert(errorstring);
		}
	
		//initialize success string, assume success;
		success = true;
	
		//put focus on the first field that has an error
		if (firstError){
			success=false;
			firstError.focus();
			scroll(0,0);
		}
		
		//tell the form to post or not (if form errors, halt the form posting)
		return success;
}


//gets called by the validate function
function writeError(obj,message)
{
	validForm = false;
	if (obj.hasError) return; //only show one error per field at a time
	//if the browser is DOM capable, do magic 
	if (W3CDOM)
	{
		errdiv = document.getElementById('errordiv'); //get the visible error string element
		obj.className += ' error';
		obj.onchange = removeError;
		errdiv.appendChild(document.createElement('br'));
		errdiv.appendChild(document.createTextNode(message));
		obj.hasError = true;
	}
	
	else
	{
		errorstring += message + '\n';
		obj.hasError = true;
	}
	if (!firstError)
		firstError = obj;
}

//gets called when an error'd field is changed
function removeError()
{
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.hasError = null;
	this.onchange = null;
	errorstring = '';
}
