// Declaring required variables 
var digits = "0123456789";

//
// strips all non numeric number from phone
// 

function stripPhone (phone) {

    var i;
    var newPhone = "";

    for (i = 0; i < phone.length; i++) {   

        // Check that current character is number.
        var c = phone.charAt(i);

        // if char is in allowed digits the append to newPhone
        if (digits.indexOf(c) >= 0) {
		     newPhone += c;
	    }

    }

    // new stripped phone
    return newPhone;

}

function validatePhone (phone) {

    // strip out all non digit characters	
    newPhone = stripPhone(phone);

    // if phone is not 10 digits the return false
    if (newPhone.length != 10) {
	     return false;
    }

    return true;

}

////////////////////////////////////////////////////////////////////////////////////////////////////
function validateZipCode (zipCode) {

    var i;
    var newZipCode = "";

	if (zipCode.length != 5) return false
	
    for (i = 0; i < zipCode.length; i++) {   

        // Check that current character is number.
        var c = zipCode.charAt(i);

        // if char is in allowed digits the return false
        if (digits.indexOf(c) == -1) return false
    }

    return true;

}


////////////////////////////////////////////////////////////////////////////////////////////////////
// ^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$ 
//
// This is were the true power of Jscript this function lies, so let's 
//
//  The first part:
//  ^[\\w-_\.]
//
//  ^ means "check the first character". In this case it's checking to make sure its a word character (a-z0-9) using \\w and it can also be a underscore, hyphen, or period (although this isn't normal, they are legal email characters) 
//
//  Next:
//  *[\\w-_\.] 
//
//  The * means "match the preceding zero or more times". and of course the next part [\\w-_\.] makes sure they are word characters or underscores, etc. 
//
//  Next:



//  \@[\\w]\.+ 
//
//  \@ checks for the @ symbol. \.+ means find at least one period after symbol. This means it must be in the @w. format and not @. or @#. 
//
//  Last:
//  [\\w]+[\\w]$ 
//
//  [\\w] makes sure there is a word character after the period. [\\w]$ checks the last character to make sure it's a word character (domain or IP address) and not a odd character. $ means "check the last character". 

function validateEmail(email) {

     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";

     var regex = new RegExp(emailReg);

     return regex.test(email);

  }

//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//
//
//

//

var fi = new Array(16);
var ea = new Array();

// field object to be validate
function FormInput(FieldName, Type, min, msg) {

     this.FieldName = FieldName;
	 this.min = min;
     this.Type = Type;				// vlaid types ZipCode, Email, Phone
     this.msg = msg;
}

// field object to be validate
function selectedElement(name, count) {
     this.name = name;
	this.count = count;
}

function doElement(theElement) {

	 var inArray = false;
	 var el;
	 
     for (var i = 0; i < ea.length; i++) {

	 	el = ea[i];

		if (el.name == theElement.name) {
		
			if (theElement.checked) {
				el.count = el.count + 1;
			}
				
			ea[i] = el; 

			inArray = true;
			break;
			
		}
		
	 }
	 
	 if (inArray == false) {
	 
		el = new selectedElement(theElement.name, 0);

		if (theElement.checked) {
			el.count = el.count + 1;
		}

	 	ea[ea.length] = el;		
			
	 }

}

function selected() {

    for (var i = 0; i < ea.length; i++) {
	 
	 	el = ea[i];
	
		// if not checked then get error message
		if (el.count <= 0)	{
		
			fiIndex = getFormInput(el.name);
			
	          theFormElement = fi[fiIndex];   
			alert(theFormElement.msg);
				
			return false;
		}
		
	}
	
	return true;

}



function getFormInput(name) {

     for (var i = 0; i < fi.length; i++) {

          if (fi[i].FieldName.toUpperCase() == name.toUpperCase()) return i;

     }

     return -1
}



//
// if checkboxes are present and none are selected then erro will be generated
//
function Form_Validator(theForm) {

   var theElement;
   var theFormElement;
   var flag = false;
   var checkBoxFlag = false;
   var checkedCount = 0;


   // loop through all elements on form
   for (var iElement = 0; iElement < theForm.length; iElement++) {

      theElement = theForm[iElement];

      var index = getFormInput(theElement.name);

      if ( (theElement.type == "checkbox") || (theElement.type == "radio") ) {

			doElement(theElement);
		   
	}	  

      if (index >= 0) {

		flag = true;
		theFormElement = fi[index];   
	
           if (theElement.type == "text") {

                if (theFormElement.min != 0 && theElement.value.length < theFormElement.min) { 

					flag = false;

				} else if (theFormElement.Type == "Email") {
				
					flag = validateEmail(theElement.value)
					
				} else if (theFormElement.Type == "Phone") {
				
					flag = validatePhone(theElement.value)					
					
				} else if (theFormElement.Type == "ZipCode") {
				
					flag = validateZipCode(theElement.value)										

  		        } else if (!theElement.value || theElement.length == 0) {

					flag = false;
					
                } 
				
				if (flag == false) {
				
                    alert(theFormElement.msg)		

                    theElement.focus();
                    theElement.select();

                    return false;
				
				}

           } else if (theElement.type == "select-one") {

                if (theElement.selectedIndex <= theFormElement.min) {

                    alert(theFormElement.msg)		

                    theElement.focus();

                    return false;

                }

           }

      } // if index > 0

   } // for

   // if checkboxes are present and none where check then return error
	if ( selected() == false) {
	
	   return false;
	   
   }

  return true;

}

function initlizeArray() {

     fi[0] = new FormInput("FirstName", "", 0, "Please Enter First Name")	
     fi[1] = new FormInput("LastName", "", 0, "Please Enter Last Name")	
     fi[2] = new FormInput("Address", "", 0, "Please Enter Address")	
     fi[3] = new FormInput("Address2", "", 0, "Please Enter Address2")	
     fi[4] = new FormInput("City", "", 0, "Please Enter City")	
     fi[5] = new FormInput("State", "", 0, "Please Select State")	
     fi[6] = new FormInput("ZipCode", "ZipCode", 0, "Plese Enter Zip Code. 5 digits")	
     fi[7] = new FormInput("Phone", "Phone", 0, "Please Enter Valid Phone. Area Code + Phone number")	
     fi[8] = new FormInput("PhonePM", "Phone", 0, "Please Enter Valid Phone. Area Code + Phone number")	
     fi[9] = new FormInput("Email", "Email", 0, "Please Enter Valid Email.  ex: test@domain.com")	
     fi[10] = new FormInput("TimeFrame", "", 0, "Please Enter Time Frame")	
     fi[11] = new FormInput("LiquidCapital", "", 1, "Additional investment required for Franchise Opportunities, Please try again!")	
     fi[12] = new FormInput("ContactTime", "", 0, "Please Select Best Time To Call")		 
     fi[13] = new FormInput("NetWorth", "", 0, "Please Select Net Worth")		 	 
     fi[14] = new FormInput("Selectfran[]", "", 0, "Please Select Franchise")		 	 
     fi[15] = new FormInput("LeadType", "", 0, "Please Select Lead Type")		 	 		

}


// initilize array
initlizeArray();