

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

function checkForm(){
	var errorMessage = "";
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;

	//check to make sure name is filled out (characters only)
	if(document.quick_form.elements["firstname"].value=="" || document.quick_form.elements["firstname"].value=="First Name:")
		errorMessage = "Name field left blank.";
	// email is a valid email
	if(document.quick_form.elements["email"].value=="" || document.quick_form.elements["email"].value=="Email:")
		errorMessage = errorMessage + "Email field left blank.";
	else if(document.quick_form.elements["email"].value.search(emailRegEx) == -1)
		errorMessage = errorMessage + "Invalid email format.";
	// phone is valid phone number


	return errorMessage;
}
function checkFormb(){
var errorMessage = "";
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	//check to make sure name is filled out (characters only)
	if(document.quick_formb.elements["firstname"].value=="" || document.quick_formb.elements["firstname"].value=="First Name:")
		errorMessage = "Name field left blank.<br/>";
	// email is a valid email
	if(document.quick_formb.elements["email"].value=="" || document.quick_formb.elements["email"].value=="Email:")
		errorMessage = errorMessage + "Email field left blank.<br/>";
	else if(document.quick_formb.elements["email"].value.search(emailRegEx) == -1)
		errorMessage = errorMessage + "Invalid email format.<br/>";
	// phone is valid phone number



	return errorMessage;
}

function doFormA() {
		var errorMessage = "";
		//LOADING POPUP
		//Click the button event!
		
		errorMessage = checkForm();

		alert(errorMessage);
		$("#showform").css("display","none");	
		$("#showthanks").css("display","block");	
		if(errorMessage == '') sendForm('a');
		else {
			$("#showform").css("display","block");	
			$("#showthanks").css("display","none");			
		}
		//centering with css
		centerPopup();
		//load popup
		loadPopup();

	return false;
}

	function sleep(milliSeconds){
		// runs Java Applets sleep method
		document.devCheater.sleep(milliSeconds); 
	}

	//if submit button is clicked
	function sendForm(whichform) {	
		var seq = "";
		var group = "";
		var message = "";
		//Get the data from all the fields
		if(whichform == 'a')
		{
			var name = document.quick_form.elements["firstname"].value;
			var email = document.quick_form.elements["email"].value;
			var home_phone = document.quick_form.elements["home_phone"].value;
				if(document.quick_form.elements["seq"])  seq = document.quick_form.elements["seq"].value;
				if(document.quick_form.elements["group"])  group = document.quick_form.elements["group"].value;
				if(document.quick_form.elements["message"])  message = document.quick_form.elements["message"].value;
		}
		else {
			var name = document.quick_formb.elements["firstname"].value;
			var email = document.quick_formb.elements["email"].value;
			var home_phone = document.quick_formb.elements["home_phone"].value;
				if(document.quick_formb.elements["seq"]) seq = document.quick_formb.elements["seq"].value;
				if(document.quick_formb.elements["group"]) group = document.quick_formb.elements["group"].value;
				if(document.quick_formb.elements["message"]) message = document.quick_formb.elements["message"].value;
		}
		
		//organize the data properly
		var data = 'name=' + name + '&email=' + email + '&home_phone=' + home_phone + '&seq=' + seq + '&group='+group + '&message=' + message;
		

		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "sendpepper.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {		
			var regx = "/<result>Success</result>/";

				//if process.php returned 1/true (send mail success)
				alert(html);
				if (regex.exec(html)) {
						if(seq == '*/*1*/*') {
							 window.location = 'http://www.prohomebuyer.com/prequalify-cash-offer.html?' + data;
							sleep(500);

						}						
				//if process.php returned 0/false (send mail failed)
				} else {

							$("#showform").css("display","block");		
}			
			}		
		});
		
			
		//cancel the submit button default behaviours
		return false;
	};	




//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	$("#button").click(function(){
		doFormA();
		
	});

	$("#buttonb").click(function(){
		errorMessage = checkFormb();
		$("#showform").css("display","none");
		$("#showthanks").css("display","block");	
		if(errorMessage == '') sendForm('b');
		else {
	
	
							$("#showthanks").css("display","none");	
		
}



	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
