/**
 * @author Jeff Walter
 * @date Mar. 13, 2009
 * @desc Form validation functions.
 */
function validateform(){
	if(document.form1.state.value==''){
		alert('Please select the state where your incident occurred.');
		document.form1.state.focus();
		return false;
	}
	if(document.form1.county.value==''){
		alert('Please select the county where your incident occurred.');
		document.form1.county.focus();
		return false;
	}
	if(document.form1.first_name.value==''){
		alert('Please enter your first name.');
		document.form1.first_name.focus();
		return false;
	}
	if(document.form1.last_name.value==''){
			alert('Please enter your last name.');
		document.form1.last_name.focus();
		return false;
	}
	var regex=/[0-9][0-9][0-9]/;
	if(document.form1.ph1_1.value==''){
		alert('Please enter your areacode.');
        document.form1.ph1_1.focus();
		return false;
    } else if(!regex.test(document.form1.ph1_1.value)) {
        alert('Please correct your areacode.');
		document.form1.ph1_1.focus();
		return false;
	}
	if(document.form1.ph1_2.value==''){
        alert('Please enter your telephone prefix.');
		document.form1.ph1_2.focus();
		return false;
    } else if(!regex.test(document.form1.ph1_2.value)){
		alert('Please correct your telephone prefix.');
		document.form1.ph1_2.focus();
		return false;
	}
    regex=/[0-9][0-9][0-9][0-9]/;
	if(document.form1.ph1_3.value==''){
		alert('Please enter your telephone suffix.');
		document.form1.ph1_3.focus();
		return false;
    } else if(!regex.test(document.form1.ph1_3.value)){
        alert('Please correct your telephone suffix.');
		document.form1.ph1_3.focus();
		return false;
	}
	if(document.form1.email.value==''){
		alert('Please enter your email.');
		document.form1.email.focus();
		return false;
	}else if(!checkmail(document.form1.email)){
		return false;
	}
	if(document.form1.crime.value==''){
		alert('Please select the crime that best describes the incident.');
		document.form1.crime.focus();
		return false;
	}
	if(document.form1.financial_status.value==''){
		alert('Please select your current financial situation.');
		document.form1.financial_status.focus();
		return false;
	}
	if(document.form1.description.value==''){
		alert('Please enter a description of your incident.');
		document.form1.description.focus();
		return false;
	}
	
}

var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function checkmail(e) {

		var returnval=emailfilter.test(e.value)
		if (returnval==false) {
				alert("Please enter a valid email address.")
				e.select()
		}
		return returnval;
}
// Functions to set the counties in the onChange() event of the state dropdown.
setdebCounties = function(statecode) {

    //var URL = "index.php?action=getCountieslist&param="+statecode;
    var URL = "form/queries.php?action=getCountieslist&state_id="+statecode;
    var listCounties = document.getElementById("listCounties");
    var handleSuccess = function( o ) { 
        listCounties.innerHTML=o.responseText
        //alert(o.responseText);
    }

    var handleFailure = function( o ) {
        //alert(o.responseText);

    }

    callback={success:handleSuccess,failure:handleFailure}
    YAHOO.util.Connect.asyncRequest("GET", URL, callback);
}

setresCounties = function(statecode) {

    //var URL = "index.php?action=getResCountieslist&param="+statecode;
    var URL = "form/queries.php?action=getCountieslist&state_id="+statecode;
    var listresCounties = document.getElementById("listresCounties");
    var handleSuccess = function( o ) { 
        listresCounties.innerHTML=o.responseText
    }

    var handleFailure = function( o ) {
        //alert(o.responseText);
    }

    callback={success:handleSuccess,failure:handleFailure}
    YAHOO.util.Connect.asyncRequest("GET", URL, callback);
}

function getElementId(s) {

    return(document.getElementById) ? document.getElementById(s) : document.all[s];
}

function validate() { 
	
		var val = getElementId("financial_status").value;
		if(val == "I have no money and require a public defender") {
				var where_to= confirm("The free consultations on this site are intended for those people with the neccesary funds to retain an attorney. \n If you can not afford an attorney please press 'cancel' to be taken to a Google search for a Pro Bono Attorney or 'ok' to select another option and continue");	
				if (where_to== true) {
						getElementId("financial_status").value = "";
						return false;
				} else {
						window.location="http://www.google.com/search?hl=en&q=pro+bono+attorneys&btnG=Google+Search&aq=0&oq=pro+bono+atto";
						return false;
				}
		} else {
			return true;
		} 
}
