function checkFranchiseForm(form) {
var emailFlag = "0";
var theFocus = "";
var error = "0";
var warning = "The following required field(s) are empty or have errors:\n";

// put form fields into variables
var FName = "" + form.elements[0].value;
var LName = "" + form.elements[1].value;
var Address = "" + form.elements[2].value;
var City = "" + form.elements[4].value;
var State = "" + form.elements[5].value;
var ZipCode = "" + form.elements[6].value;
var Country = "" + form.elements[7].value;
var Email = "" + form.elements[8].value;
var HomePhone = "" + form.elements[9].value;
var BusPhone = "" + form.elements[10].value;
var Fax = "" + form.elements[11].value;
var City1 = "" + form.firstCity.value;


//alert(City);


/*
var City2 = "" + form.elements[34].value;
var City3 = "" + form.elements[35].value;
*/

/*
var l =form.elements.length;
var index = "";
for(j=0;j<l;j++)
{
index = index + j + "<" + form.elements[j].name +">"+ "\n";
 }
 alert(index); 
*/
//Check for empty namefield and check for valid name characters
    if (FName == ""){
        warning += "     -First Name\n";
        error = "1";
        if (theFocus == "") {
            theFocus = "0";
            }
    }
    else{
        if (checkName(FName) == false){
            warning += "     -Name\n";
            error = "1";
            if (theFocus == "") {
                theFocus = "0";
            }
        }
    }
//Check for empty Last namefield and check for valid name characters
    if (LName == ""){
        warning += "     -Last Name\n";
        error = "1";
        if (theFocus == "") {
            theFocus = "1";
            }
    }
    else{
        if (checkName(LName) == false){
            warning += "     -Last Name\n";
            error = "1";
            if (theFocus == "") {
                theFocus = "1";
            }
        }
    }
//Check for empty Address field
    if (Address == ""){
        warning += "     -Address Line 1\n";
        error = "1";
        if (theFocus == "") {
            theFocus = "2";
        }
    }
//Check City name for valid characters
    if (City == ""){
        warning += "     -City\n";
        error = "1";
        if (theFocus == "") {
            theFocus = "4";
        }
    }
    else{
        if (checkName(City) == false){
            warning += "     -City\n";
            error = "1";
            if (theFocus == "") {
                theFocus = "4";
            }
        }
    }
//Check State/province name for valid characters
    if (State == ""){
        warning += "     -State/Province\n";
        error = "1";
        if (theFocus == "") {
            theFocus = "5";
        }
    }
    else{
        if (checkName(State) == false){
            warning += "     -State/Province\n";
            error = "1";
            if (theFocus == "") {
                theFocus = "5";
            }
        }
    }
//Check for empty zip/postcode and for valid characters
  if (ZipCode == ""){
        warning += "     -Zip/Postal Code\n";
        error = "1";
        if (theFocus == "") {
            theFocus = "6"
        }
    }

/*
  if ((ZipCode == "") || (ZipCode.length < 5) || (ZipCode.length > 10) || ((ZipCode.length > 5) && (ZipCode.length < 9)) ){
        warning += "     -Zip Code\n";
        error = "1";
        if (theFocus == "") {
            theFocus = "5"
        }
    }
    if ((ZipCode.length == 5) || (ZipCode.length == 9)) {
        if((CheckForNumber(0,ZipCode.length,ZipCode)) == false){
            warning += "     -Zip Code\n";
            error = "1";
            if (theFocus == "") {
                theFocus = "5";
            }
        }
    }
    if (ZipCode.length == 10){
        if (ZipCode.charAt(5) != "-"){
            warning += "     -Zip Code";
            error = "1";
            if (theFocus == "") {
                theFocus = "5";
            }
        }
      else if ((CheckForNumber(6,10,ZipCode) == false) || (CheckForNumber(0,5,ZipCode) == false)){
            warning += "     -Zip Code\n";
            error = "1";
            if (theFocus == "") {
                theFocus = "5";
            }
        }
    }
*/
//Check Country name for valid characters
    if (Country == ""){
        warning += "     -Country\n";
        error = "1";
        if (theFocus == "") {
            theFocus = "7";
        }
    }
    else{
        if (checkName(Country) == false){
            warning += "     -Country\n";
            error = "1";
            if (theFocus == "") {
                theFocus = "7";
            }
        }
    }

//Check email address

if (Email == ""){
          emailFlag = "1";
		} else {
         //Check for @
          if (Email.indexOf("@") == -1) {
            emailFlag = "1";
            }
         //Check for .
         else if (Email.indexOf(".") == -1) {
            emailFlag = "1";
        }
        //Check for only valid charachters a-z A-Z @ . 0-9 - _
        else {
        for(var i = 0; i < Email.length; i++){
                ch = Email.substring(i, i + 1)
                if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch == "@") || (ch == ".") || (ch == "_") || (ch == "-") || (ch >= "0" && ch <= "9")) {
                    error = error;
            }
                else{
                    emailFlag = "1";
                }
        }
    }
    }
	if (emailFlag == "1"){
        warning += "     -Email\n";
        error ="1";
        if (theFocus == "") {
            theFocus = "8";
        }

    }

//Check for empty HomeNO field
    if (HomePhone == ""){
        warning += "     -Home Phone\n";
        error = "1";
        if (theFocus == "") {
            theFocus = "9";
        }
    }
//Check Current Net Worth for valid characters
        if((form.worth[0].checked == false) && (form.worth[1].checked == false) && (form.worth[2].checked == false) && (form.worth[3].checked == false)){
			warning += "     -Current Net Worth\n";
			error = "1";
			if (theFocus == "") {
				theFocus = "26";
			}
		}
//Check Current Liquid Assets for valid characters
        if((form.invest[0].checked == false) && (form.invest[1].checked == false) && (form.invest[2].checked == false) && (form.invest[3].checked == false)){
			warning += "     -Current Liquid Assets\n";
			error = "1";
			if (theFocus == "") {
				theFocus = "30";
			}
		}
//Check City Choice names for valid characters
    if (City1 == ""){
        warning += "     -City Choice \n";
        error = "1";
        if (theFocus == "") {
            theFocus = "35";
            }
    }
    else{
        if (checkCityName(City1) == false){
            warning += "     -City Choice 1\n";
            error = "1";
            if (theFocus == "") {
                theFocus = "35";
            }
        }
    }
/*
    if (City2 != ""){      
        if (checkName(City2) == false){
            warning += "     -City Choice 2\n";
            error = "1";
            if (theFocus == "") {
                theFocus = "34";
            }
        }
    }
    if (City3 != ""){      
        if (checkName(City3) == false){
            warning += "     -City Choice 3\n";
            error = "1";
            if (theFocus == "") {
                theFocus = "35";
            }
        }
    }
*/
	// Alert If a Form Field is empty and set the focus to the first empty form field 

	
    if (error == "1"){
        alert(warning);
        form[theFocus].focus();
        return false;
        }
     else {
        return true;
        }



}   


function CheckForNumber(beg,end,string){
    for (var i = beg; i < end; i++){
        var c = string.charAt(i)
        c = parseInt(c);
        if (isNaN(c)){
            return false;
        }
    }
    return true;
}
function checkName(theName){
var badChar ="0";
    for (var i = 0; i < theName.length; i++) {
            ch = theName.substring(i, i + 1)
            if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch >= "0" && ch <= "9") || (ch == "'") || (ch == ".") || (ch == " ")){
                badChar = badChar;
            }
            else{
                badChar ="1";
            }
    }
    if (badChar == "1"){
        return false;
    }
    else{
        return true;
    }
}
function checkCityName(theName){
var badChar ="0";
    for (var i = 0; i < theName.length; i++) {
            ch = theName.substring(i, i + 1)
            if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch >= "0" && ch <= "9") || (ch == "'") || (ch == ".") || (ch == " ") || (ch == ",")){
                badChar = badChar;
            }
            else{
                badChar ="1";
            }
    }
    if (badChar == "1"){
        return false;
    }
    else{
        return true;
    }
}