/**
 * @author jwalter
 */

// Lookup hash for selmonth field.
var month = new Array()
month["January"]=0;
month["February"]=1;
month["March"]=2;
month["April"]=3;
month["May"]=4;
month["June"]=5;
month["July"]=6;
month["August"]=7;
month["September"]=8;
month["October"]=9;
month["November"]=10;
month["December"]=11; 

var limit = 63158400000;
var currentDate  = new Date();
var currentDay   = currentDate.getDate();
var currentMonth = currentDate.getMonth();
var currentYear  = currentDate.getFullYear();
currentDate = new Date(currentYear, currentMonth, currentDay);       

// Function to validate email addresses.
function checkmail(e) {
    // Regex object to validate email addresses.
    var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i    
    var returnval=emailfilter.test(e.value)

    if (returnval==false) {
        alert("Please enter a valid email address.")
        e.select()
    }

    return returnval;
}

// Function to validate the form.
function validateform(){
    if(document.form1.txtfname.value==''){
		alert('Please enter your first name.');
		document.form1.txtfname.focus();
		return false;
	}
	if(document.form1.txtlname.value==''){
		alert('Please enter your last name.');
		document.form1.txtlname.focus();
		return false;
	}
    var regex=/[0-9][0-9][0-9]/;
	if(document.form1.txtphno1.value==''){
		alert('Please enter your areacode.');
        document.form1.txtphno1.focus();
		return false;
    } else if(!regex.test(document.form1.txtphno1.value)) {
        alert('Please correct your areacode.');
		document.form1.txtphno1.focus();
		return false;
	}
	if(document.form1.txtphno2.value==''){
        alert('Please enter your telephone prefix.');
		document.form1.txtphno2.focus();
		return false;
    } else if(!regex.test(document.form1.txtphno2.value)){
		alert('Please correct your telephone prefix.');
		document.form1.txtphno2.focus();
		return false;
	}
    regex=/[0-9][0-9][0-9][0-9]/;
	if(document.form1.txtphno3.value==''){
		alert('Please enter your telephone suffix.');
		document.form1.txtphno3.focus();
		return false;
    } else if(!regex.test(document.form1.txtphno3.value)){
        alert('Please correct your telephone suffix.');
		document.form1.txtphno3.focus();
		return false;
	}
	if(document.form1.txtemailid.value==''){
		alert('Please enter your email.');
		document.form1.txtemailid.focus();
		return false;
	}else if(!checkmail(document.form1.txtemailid)){
		return false;
	}
	if(document.form1.causeofinjury.value==''){
		alert('Please select the catagory that best describes the cause of your injury.');
		document.form1.causeofinjury.focus();
		return false;
	}
	if(document.form1.selmonth.value==''){
		alert('Please select the month in which your incident occurred.');
		document.form1.selmonth.focus();
		return false;
	}
	if(document.form1.seldate.value==''){
		alert('Please select the day in which your incident occurred.');
		document.form1.seldate.focus();
		return false;
	}
	if(document.form1.selyear.value==''){
		alert('Please select the year in which your incident occurred.');
		document.form1.selyear.focus();
		return false;
	} else {
        var formDate = new Date(document.form1.selyear.value, month[document.form1.selmonth.value], document.form1.seldate.value);
        var timeFrame = currentDate.getTime() - formDate.getTime();
        if((timeFrame > limit) || (timeFrame < 0)) {
            alert('The free consultations offered on this page are intended for cases within the last 2 years.');
            document.form1.selmonth.focus();
            return false;
        }
    }
	if(document.form1.selplace.value==''){
		alert('Please select the state where your incident occurred.');
		document.form1.selplace.focus();
		return false;
	}
	if(document.form1.selcountry.value==''){
		alert('Please select the county where your incident occurred.');
		document.form1.selcountry.focus();
		return false;
	}
	if(!document.form1.rbtn[0].checked && !document.form1.rbtn[1].checked){
		alert('Please select yes or no to let us know if you currently have an attorney.');
		document.form1.rbtn[0].focus();
		return false;
	} else if(document.form1.rbtn[0].checked) {
        alert('The free consultations offered on this page are intended for people that have not already hired an attorney.');
        return false;
    }
	if(document.form1.description.value==''){
		alert('Please enter a description of your incident.');
		document.form1.description.focus();
		return false;
	}
	
}


// 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];
}


