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

	FUNCTION FOR VALIDATING TRIPP CALCULATOR DATA.
	PARAMS => FORM NAME(STR)
	
	RETURNS FALSE IF ANY ERROR OCCURS, TRUE OTHERWISE

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

function validateSearch(formname){
	
	//collect the form elements that we need to validate
	var add = eval('document.'+formname+'.address.value');
	var tof = eval('document.'+formname+'.tofrom');
	var zip = eval('document.'+formname+'.zipcode.value');
	
	
	//init an error message and error counter
	var err = "The following errors occurred:\n\n";
	var err_count = 0;

	//validate search address against being blank, or too short to be meaningful
	if(add == '' || add.length < 2){
		err += "- Please enter your address or intersection.\n";
		err_count++;
	}
	
	//validate search zip
	if(!isZip(zip)){
		err += "- Please enter your zip code.\n";
		err_count++;
	}
	
	//init the to/from value
	var tofrom = false;
	
	//loop through the radio buttons and set value of selected
	for(i=0;i<tof.length;i++){
		if(tof[i].checked){ tofrom = tof[i].value;}
	}
	
	//validate radio button data
	if(!tofrom){
		err += "- Please choose whether you are coming from, or going to, LA/Ontario.\n";
		err_count++;
	}
	
	//check the count of errors, if greater than 0, output error messages
	if(err_count > 0){
		alert(err);
		return false;
	}
	
	//everything seems fine
	document.tripCalc.submit();
	return true;
}



// returns true if the string is 5 digits
	function isZip(str){
		var re = /\d{5,}/;
		if(re.test(str)) return true;
		return false;
	}