var popupStatus = 0;
var actionTaken = false;
var t;
var popupOn = true;
var formlayer = 1;

$(document).ready(function(){	
    //$('.field.text.inline.phonesi').mask("999-999-9999? x999999");  
	
	$("#button").click(function(){
		actionTaken = false;
		clearTimeout(t);
		send();
	});
	
	$("#button2").click(function(){
		actionTaken = false;
		clearTimeout(t);
		send2();
	});
	
	$("#okbutton").click(function(){
		disablePopup();
		clearTimeout(t);
		actionTaken = true;					  
	});
	
	$(".formerror").hide();

	$(".sneatnight").hide();
	$(".learnmore.sneatnight2").click(function(){
		$(this).hide();
		$(".sneatnight").fadeIn("fast");
	});

	$(".eat2much.iejsfix").hide();
	$(".learnmore.eat2much").click(function(){
		$(this).hide();
		$(".eat2much.iejsfix").fadeIn("fast");
	});

	$(".yoyo.iejsfix").hide();
	$(".learnmore.yoyo").click(function(){
		$(this).hide();
		$(".yoyo.iejsfix").fadeIn("fast");
	});

	$(".conscfort.iejsfix").hide();
	$(".learnmore.conscfort").click(function(){
		$(this).hide();
		$(".conscfort.iejsfix").fadeIn("fast");
	});

	$(".challulties.iejsfix").hide();
	$(".learnmore.challulties").click(function(){
		$(this).hide();
		$(".challulties.iejsfix").fadeIn("fast");
	});

});

function Xhttp(url, postinfo) {
	//alert(url);
    if(window.XMLHttpRequest) {
    	try {
			xmlhttp = new XMLHttpRequest();
        } catch(e) {
			xmlhttp = false;
        }

    } else if(window.ActiveXObject) {
       	try {
        	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		xmlhttp = false;
        	}
		}
    }
	if(xmlhttp) {
		xmlhttp.open('POST', url, true);
		
		xmlhttp.onreadystatechange = function() {
	    	if (xmlhttp.readyState == 4) {
				var msg = xmlhttp.responseText;
				//alert(msg);
			}
	    }
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(postinfo);
}

function send() {
	var emailcheck = verifyEmail();
	fieldC();
	if (formlayer == 1 && emailcheck == true && document.getElementById('fname') != "" && document.getElementById('phone').value != "") {
		Xhttp("senddata.php?fname="+document.getElementById('fname').value+"&email="+document.getElementById('email').value+"&phone="+document.getElementById('phone').value+"&phone2="+document.getElementById('phone2').value+"&cell="+document.getElementById('cell').value+"&btime="+document.getElementById('btime').value+"&formid=1", null);
		document.getElementById('phoneduplicate').value = document.getElementById('phone').value;
		document.getElementById('main-signup-form').submit();
	} else {
		var name = document.getElementById('fname').value;
		var fillin = "Please fill in the following fields correctly:<ul>";
		var errors = 0;
		if (document.getElementById('fname').value == "") {
			fillin = fillin + "<li>Your First Name</li>";
			errors = errors + 1;
			$(".trname").css({
				"background-color": "#fff692"
			});
			$(".field.text.inline.firsti").css({
				"background-color": "#fff692"
			});
		} else {
			$(".trname").css({
				"background-color": "inherit"
			});
			$(".field.text.inline.firsti").css({
				"background-color": "white"
			});
		}
		if (emailcheck == false) {
			errors = errors + 1;
			fillin = fillin + "<li>Your Email Address</li>";
			$(".tremail").css({
				"background-color": "#fff692"
			});
			$(".field.text.inline.emaili").css({
				"background-color": "#fff692"
			});
		} else {
			$(".tremail").css({
				"background-color": "inherit"
			});
			$(".field.text.inline.emaili").css({
				"background-color": "white"
			});
		}

		if (document.getElementById('phone').value == "") {
			if (errors == 0) {
				$(".formerror").html("Fill in your phone number and hit submit");
				$(".formerror").fadeIn("fast");
			}
			fillin = fillin + "<li>Your Phone Number</li>";
			$(".trphone").css({
				"background-color": "#fff692"
			});
			$(".field.text.inline.phonesi").css({
				"background-color": "#fff692"
			});
		} else {
			$(".trphone").css({
				"background-color": "inherit"
			});
			$(".field.text.inline.phonesi").css({
				"background-color": "white"
			});
		}

		if (document.getElementById('btime').value == "") {
			errors = errors + 1;
			fillin = fillin + "<li>Best Time to Call</li>";
			$(".trbtime").css({
				"background-color": "#fff692"
			});
			$(".field.text.inline.btimei").css({
				"background-color": "#fff692"
			});
		} else {
			$(".trbtime").css({
				"background-color": "inherit"
			});
			$(".field.text.inline.btimei").css({
				"background-color": "white"
			});
		}

		
		if (errors != 0) {
			$(".formerror").html(fillin + "</ul>");
			$(".formerror").fadeIn("fast");
		}

		Xhttp("senddata.php?fname="+document.getElementById('fname').value+"&email="+document.getElementById('email').value+"&phone="+document.getElementById('phone').value+"&phone2="+document.getElementById('phone2').value+"&cell="+document.getElementById('cell').value+"&btime="+document.getElementById('btime').value+"&formid=1", null);
		//document.write("fname="+document.getElementById('fname').value+"&email="+document.getElementById('email').value+"&phone="+document.getElementById('phone').value);
	}
}

function send2() {
	var emailcheck = verifyEmail2();
	fieldC2();
	if (formlayer == 2 || formlayer == 1 && emailcheck == true && document.getElementById('fname2') != "" && document.getElementById('phone11').value != "") {
		Xhttp("senddata.php?fname="+document.getElementById('fname2').value+"&email="+document.getElementById('email2').value+"&phone="+document.getElementById('phone11').value+"&phone2="+document.getElementById('phone112').value+"&cell="+document.getElementById('cell11').value+"&btime="+document.getElementById('btime2').value+"&formid=2", null);
		document.getElementById('phoneduplicate2').value = document.getElementById('phone11').value;
		document.getElementById('main-signup-form2').submit();
	} else {
		var name = document.getElementById('fname2').value;
		var fillin = "Please fill in the following fields correctly:<ul>";
		var errors = 0;
		formlayer = 2;
		if (document.getElementById('fname2').value == "") {
			fillin = fillin + "<li>Your First Name</li>";
			errors = errors + 1;
			$(".trname").css({
				"background-color": "#fff692"
			});
			$(".field.text.inline.firsti").css({
				"background-color": "#fff692"
			});
		} else {
			$(".trname").css({
				"background-color": "inherit"
			});
			$(".field.text.inline.firsti").css({
				"background-color": "white"
			});
		}
		if (emailcheck == false) {
			errors = errors + 1;
			fillin = fillin + "<li>Your Email Address</li>";
			$(".tremail").css({
				"background-color": "#fff692"
			});
			$(".field.text.inline.emaili").css({
				"background-color": "#fff692"
			});
		} else {
			$(".tremail").css({
				"background-color": "inherit"
			});
			$(".field.text.inline.emaili").css({
				"background-color": "white"
			});
		}
		
		if (document.getElementById('phone11').value == "") {
			if (errors == 0) {
				$(".formerror").html("Fill in your phone number and hit submit");
				$(".formerror").fadeIn("fast");
			}
			fillin = fillin + "<li>Your Phone Number</li>";
			$(".trphone").css({
				"background-color": "#fff692"
			});
			$(".field.text.inline.phonesi").css({
				"background-color": "#fff692"
			});
		} else {
			$(".trphone").css({
				"background-color": "inherit"
			});
			$(".field.text.inline.phonesi").css({
				"background-color": "white"
			});
		}
		
		if (document.getElementById('btime2').value == "") {
			errors = errors + 1;
			fillin = fillin + "<li>Best Time to Call</li>";
			$(".trbtime").css({
				"background-color": "#fff692"
			});
			$(".field.text.inline.btimei").css({
				"background-color": "#fff692"
			});
		} else {
			$(".trbtime").css({
				"background-color": "inherit"
			});
			$(".field.text.inline.btimei").css({
				"background-color": "white"
			});
		}
		
		if (errors != 0) {
			$(".formerror").html(fillin + "</ul>");
			$(".formerror").fadeIn("fast");
		}

		Xhttp("senddata.php?fname="+document.getElementById('fname2').value+"&email="+document.getElementById('email2').value+"&phone="+document.getElementById('phone11').value+"&phone2="+document.getElementById('phone112').value+"&cell="+document.getElementById('cell11').value+"&btime="+document.getElementById('btime2').value+"&formid=2", null);
		//document.write("fname="+document.getElementById('fname').value+"&email="+document.getElementById('email').value+"&phone="+document.getElementById('phone').value);
	}
}

function sendagain() {
	if (!actionTaken) {
		document.getElementById('main-signup-form').submit();
	}
	actionTaken = false;
}

function verifyEmail(){
var status = false;     
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
     if (document.getElementById('email').value.search(emailRegEx) == -1) {
		status = false; 
     } else {
          status = true;
     }
     return status;
}

function verifyEmail2(){
var status = false;     
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
     if (document.getElementById('email2').value.search(emailRegEx) == -1) {
		status = false; 
     } else {
          status = true;
     }
     return status;
}

function fieldC() {
	document.getElementById('email2').value = document.getElementById('email').value;
	document.getElementById('fname2').value = document.getElementById('fname').value;
	document.getElementById('phone11').value = document.getElementById('phone').value;
	document.getElementById('phone112').value = document.getElementById('phone2').value;
	document.getElementById('cell11').value = document.getElementById('cell').value;
	document.getElementById('phoneduplicate2').value = document.getElementById('phoneduplicate').value;
	document.getElementById('btime2').value = document.getElementById('btime').value;
}

function fieldC2() {
	document.getElementById('email').value = document.getElementById('email2').value;
	document.getElementById('fname').value = document.getElementById('fname2').value;
	document.getElementById('phone').value = document.getElementById('phone11').value;
	document.getElementById('phone2').value = document.getElementById('phone112').value;
	document.getElementById('cell').value = document.getElementById('cell11').value;
	document.getElementById('phoneduplicate').value = document.getElementById('phoneduplicate2').value;
	document.getElementById('btime').value = document.getElementById('btime2').value;
}
