//FormValidation
function formCheck(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("First_Name", "Last_Name", "Email", "Cell_Phone");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("First Name", "Last Name", "Email", "Cell Phone");
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
	//create cookie
	createCookie('stateName', document.case_evaluation.stateName.value + "-" + document.case_evaluation.stateName.value, 7);
}



function EnableElements()
{
	var objForm = document.forms.case_evaluation;
	for (i=0;i<objForm.elements.length;i++) {
		objForm.elements[i].disabled = false;
	}
}

function isEmailAddr(email) {
	var result = false;
	var theStr = new String(email);
	var index = theStr.indexOf("@");
	if (index > 0) {
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1)) {
		result = true;
		}
	}
	return result;
}
function isAlpha(obj) {
	var result = false;
	var inThere = obj.match(/\D/);
	if (inThere) {
		result = true;
	}
	return result;
}
function isAlphaNum(obj) {
	var result = false;
	var inThere = obj.match(/\w/);
	if (inThere) {
		result = true;
	}
	return result;
}
function isNotAlphaNum(obj) {
	var result = false;
	var inThere = obj.match(/[^\w\s]/);
	if (inThere) {
		result = true;
	}
	return result;
}

function isNum(obj) {
	var result = false;
	var inThere = obj.match(/\d/);
	if (inThere) {
		result = true;
	}
	return result;
}

function hasWhiteSpace(obj) {
	var result = false;
	var inThere = obj.match(/\s/);
	if (inThere) {
		result = true;
	}
	return result;
}

function isChecked(obj) {
	for (var i = 0; i < obj.length; i++) {
		if (obj[i].checked) {
			return "yes";
		}
	}
	return "no";
}


// Functions that validates the forms

function fvalid(case_evaluation) {
	
	
	// State
		
	if (document.case_evaluation.First_Name.value != "") 
		{
		if (isNum(document.case_evaluation.First_Name.value)) {
			alert("Numbers are not allowed in the \"First Name\" field.");
			document.case_evaluation.First_Name.focus();
			return false;
		} 
		else if (isNotAlphaNum(document.case_evaluation.First_Name.value)) 
		{
			alert("Special characters are not allowed in the \"First Name\" field.");
			document.case_evaluation.First_Name.focus();
			return false;
		}
		} 
		else 
		{
		alert("The \"First Name\" field cannot be left blank.");
		document.case_evaluation.First_Name.focus();
		return false;
		}
	
	if (document.case_evaluation.Last_Name.value != "") {
		if (isNum(document.case_evaluation.Last_Name.value)) {
			alert("Numbers are not allowed in the \"Last Name\" field.");
			document.case_evaluation.Last_Name.focus();
			return false;
		} else if (isNotAlphaNum(document.case_evaluation.Last_Name.value)) {
			alert("Special characters are not allowed in the \"Last Name\" field.");
			document.case_evaluation.Last_Name.focus();
			return false;
		}
	} else {
		alert("The \"Last Name\" field cannot be left blank.");
		document.case_evaluation.Last_Name.focus();
		return false;
	}
	
	// EmailAddr
	if (document.case_evaluation.Email.value != "") {
		if (!isEmailAddr(document.case_evaluation.Email.value)) {
			alert("Please enter a complete Email address in the form: yourname@yourdomain.com");
			document.case_evaluation.Email.focus();
			return false;
		} else if (hasWhiteSpace(document.case_evaluation.Email.value)) {
			alert("Your Email address cannot contain blank spaces.");
			document.case_evaluation.Email.focus();
			return false;
		}
	} else {
		alert("The \"Email Address\" field cannot be left blank.");
		document.case_evaluation.Email.focus();
		return false;
	}
	
	// Phone
	if (document.case_evaluation.Cell_Phone.value != "") {
		if (isAlpha(document.case_evaluation.Cell_Phone.value)) {
			alert("Only numbers are allowed in a Phone Number.");
			document.case_evaluation.Cell_Phone.focus();
			return false;
		} else if (document.case_evaluation.Cell_Phone.value.length < 10) {
			alert("This \"Phone Number\" field must have 10 characters.");
			document.case_evaluation.Cell_Phone.focus();
			return false;
		}
	} else {
		alert("The \"Phone Number\" field cannot be left blank.");
		document.case_evaluation.Cell_Phone.focus();
		return false;
	}	
	
	
}




//start ppc cookie creation

function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else {
			var expires = "";

		//alert(value);
		document.cookie = name+"="+value+expires+"; path=/";
		//alert(document.cookie);
	}
	}