/**
 * Create ajaxObject
 */
function ajaxObject()
{
var xmlhttp = null;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
  
  return xmlhttp;
}

/**
 * trim
 * @param {type} str 
 */
 function trim(str) {
 	return str.replace(/^\s+|\s+$/g, "");
 }
 
 function checkNumber(myfield, e, dec)
 {
 	var key;
 	var keychar;
 	
 	if (window.event)
 	   key = window.event.keyCode;
 	else if (e)
 	   key = e.which;
 	else
 	   return true;
 	keychar = String.fromCharCode(key);
 	
 	// control keys
 	if ((key==null) || (key==0) || (key==8) || 
 		(key==9) || (key==13) || (key==27) )
 	   return true;
 	
 	// numbers
 	else if ((("0123456789").indexOf(keychar) > -1))
 	   return true;
 	
 	// decimal point jump
 	else if (dec && (keychar == "."))
 	   {
 	   myfield.form.elements[dec].focus();
 	   return false;
 	   }
 	else
 	   return false;
 }


 function checkPhone(myfield, e, dec) {
     var key;
     var keychar;

     if (window.event)
         key = window.event.keyCode;
     else if (e)
         key = e.which;
     else
         return true;
     keychar = String.fromCharCode(key);

     // control keys
     if ((key == null) || (key == 0) || (key == 8) ||
 		(key == 9) || (key == 13) || (key == 27))
         return true;

     // numbers
     else if ((("0123456789 -)(").indexOf(keychar) > -1))
         return true;

     // decimal point jump
     else if (dec && (keychar == ".")) {
         myfield.form.elements[dec].focus();
         return false;
     }
     else
         return false;
 }
 
 var digits = "0123456789";
 function isInteger(s)
 {   var i;
     for (i = 0; i < s.length; i++)
     {   
         // Check that current character is number.
         var c = s.charAt(i);
         if (((c < "0") || (c > "9"))) return false;
     }
     // All characters are numbers.
     return true;
 }
 /**
  * chech checkDigit
  * @param myfield
  * @return
  */
 function checkDigit(myfield) {
	var myphone = document.getElementById(myfield).value;;
	if(myphone != "") {
		if(!isInteger(myphone)) {
				alert("Please provide a valid phone number.\nPhone number only contains numbers.");
			
			mozBug=myfield; 
			setTimeout("document.getElementById(mozBug).focus()",0);
			
			return false;
		}		
	}
	return true;
}
 
 /**
  * chech checkDigit
  * @param myfield
  * @return
  */
 function checkAge(myfield) {
	var myage = document.getElementById(myfield).value;;
	if(myage != "") {
		if(!isInteger(myage)) {			
			alert("Please provide a valid age.");			
			mozBug=myfield; 
			setTimeout("document.getElementById(mozBug).focus()",0);
			
			return false;
		}		
	}
	return true;
}
 
 
 
 
 /**
  * Check valid email
  * @param theemail
  * @return error message
  */
 function checkEmail(theemail) {
	 var email_filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	 var msg = "";
	 if (!email_filter.test(theemail)) {
		  msg = "Please provide a valid Email.\n";
		 return msg;
	 }	 
	 
	 return msg; 
	 
}
 var requirefields2=new Array("name","phone","age", "email");
 var fieldsassoc2=new Array();
 fieldsassoc2['name'] = "Name";
 fieldsassoc2['phone'] = "Phone";
 fieldsassoc2['age'] = "Age";
 fieldsassoc2['email'] = "Email";
 
 /**
  * check required fields
  * @return boolean
  */
 function checkrequire2() {
	 var errmsg = "";
	 for(x in requirefields2) {
		var thefield = requirefields2[x];
		var data2 = document.getElementById(thefield).value;
		
		if(data2 == "") {
			errmsg += fieldsassoc2[thefield] + " is required.\n";
		}
		
		if(thefield == "phone") {			
			if(data2.length != 10) {
				errmsg += "Please provide a valid phone number.\n";
			}
		}
		
		if(thefield == "age") {			
			if(data2.length != 2 || data2 <=18) {
				errmsg += "Please provide a valid age.\n";
			}
		}
		
		if(thefield == "email") {
			errmsg += checkEmail(data2);
		}
		
	 }
	 
	 if(errmsg != "") {
		 alert(errmsg);
		 return false;
	 }
	 
	 return true;
}
 
 
 var requirefields=new Array("zipcode","occupation","income", "marital_status");
 var fieldsassoc=new Array();
 fieldsassoc['zipcode'] = "ZipCode";
 fieldsassoc['occupation'] = "Occupation";
 fieldsassoc['income'] = "Income";
 fieldsassoc['marital_status'] = "Marital Status";
 
 
 /**
  * check required fields
  * @return boolean
  */
 function checkrequire() {
	var errmsg = "";
	 for(x in requirefields) {
		var thefield = requirefields[x];
		var data2 = document.getElementById(thefield).value;
	
		if(data2 == "") {
			errmsg += fieldsassoc[thefield] + " is required.\n";
		}
		
		if(thefield == "zipcode") {
			if(data2.length != 5) {
				errmsg += "Please provide a valid zip code.\n";
			}
		}
		
	 }
	 
	 if(errmsg != "") {
		 alert(errmsg);
		 return false;
	 }
	 
	 return true;
}
/**
 * Update lead demographics
 * @param myfield
 * @return
 */
function updateFields(myfield, value1, leadid, sid) {
	
	var nocache = Math.random();
  	var ajaxObj = null;
   	var url = "updatedata.php?nocache=" + encodeURIComponent(nocache) + "&f=" + encodeURIComponent(myfield);
   	
	url += "&d=" + encodeURIComponent(value1) + "&l=" + encodeURIComponent(leadid) + "&s=" + encodeURIComponent(sid);
	//create ajax object 		
	ajaxObj = ajaxObject();
	if(ajaxObj != null) {
		ajaxObj.onreadystatechange=function()
		{
			if(ajaxObj.readyState==4){
				rsp=ajaxObj.responseText;
				
			}
		};
		ajaxObj.open("GET",url,true);
		ajaxObj.send(null);
	}
}



/**
 * Update lead demographics
 * @param myfield
 * @return
 */
function updateFieldsSelect(myfield, leadid, sid) {
	var flag = 0;
	var data1 = document.getElementById(myfield).value;
	
	for(x in requirefields) {
		if(requirefields[x] == myfield) {
			flag = 1;
			break;
		}
	}
	
	if(flag == 1) {
		if(data1 == "") {
			alert(fieldsassoc[myfield] + " is required");
			document.getElementById(myfield).focus();
			return;
		}
	}
	
	if(myfield == "zipcode") {
		if(data1.length != 5) {
			alert("Please provide a valid zip code");
			document.getElementById(myfield).focus();
			return;
		}
	}
	
	var nocache = Math.random();
  	var ajaxObj = null;
   	var url = "updatedata.php?nocache=" + encodeURIComponent(nocache) + "&f=" + encodeURIComponent(myfield);
   	
 
	url += "&d=" + encodeURIComponent(data1) + "&l=" + encodeURIComponent(leadid) + "&s=" + encodeURIComponent(sid);
	//create ajax object 		
	ajaxObj = ajaxObject();
	if(ajaxObj != null) {
		ajaxObj.onreadystatechange=function()
		{
			if(ajaxObj.readyState==4){
				rsp=ajaxObj.responseText;
				
			}
		};
		ajaxObj.open("GET",url,true);
		ajaxObj.send(null);
	}
	
}


 
