var tm1 = 0; 
var sumWord = ["plus", "add"]; 

$(document).ready(function() {
   
   //  Add in a logic question and answer to prevent spamming 
   window.num1 = Math.floor(Math.random()*10)+1; 
   window.num2 = Math.floor(Math.random()*10)+1; 
   displayWord = sumWord[Math.floor(Math.random()*2)]; 
   $('#logicQuestionTD').html(num1+" "+displayWord+" "+num2+"?"); 
   
   displayWord = sumWord[Math.floor(Math.random()*2)]; 
   $('#logicQuesDiv').html(num1+" "+displayWord+" "+num2+"?"); 
   
   	window.submitEmailForm = function ( frm, ajaxLoader ) {
		
		dataObj = {}; 
		$('input, select, textarea', frm).each(function(){
			if ( $(this).attr('type')!="radio" || $(this).attr('type')=="radio" && $(this).attr('checked') ) 
			dataObj[$(this).attr('name')] = $(this).val(); 
		}); 
		
		
		$.ajax({
			data : dataObj, 
			type : 'POST', 
			dataType : 'html', 
			context : ajaxLoader, 
			success : function (data) { 
			
				closeLink = $('<a class="floatingbox-close" />');
				closeLink.click(function(){
					$(this).parent().hide(400, function(){ $(this).html(""); }); 
					return false;
				}); 
			
				frm = $(this).parent().parent(); 
				$(this).html(""); 
				$('.popupmsg', frm).html(data.replace(/\n/, "<br />").replace(/\r/, "")); 
				$('.popupmsg', frm).append(closeLink);
				$('.popupmsg', frm).show(400);
				frm.get(0).reset(); 
				$("#logicAnswer", frm).attr({ "disabled" : false }); 
			}
		}); 
		
   	}
   
   	$('#freeFixOfferSubmitBtn').click(function(){ 
											
		ans = parseInt($("#logicAnswer", $("#freeFixOfferForm")).val()); 
											
		if ( $("#custName", $("#freeFixOfferForm")).val()=="" || $("#custName", $("#freeFixOfferForm")).val().length<2 ||
		   $("#custEmail", $("#freeFixOfferForm")).val()=="" /* || Validate email format as well */ ) 
			alert("Make sure you fill in your Name and Email correctly first."); 
		else if ( $("#logicAnswer", $("#freeFixOfferForm")).val()=="" || isNaN(ans) || ans!=(num1+num2) ) 
			alert("Please answer the Math Sum question correctly before proceeding."); 
		else {
			$("#logicAnswer", $("#freeFixOfferForm")).val(ans+""); 
			$("#logicAnswer", $("#freeFixOfferForm")).attr({ "disabled" : "disabled" }); 
			$("#freeFixITOffer_hiddFld", $("#freeFixOfferForm")).val(ans+""); 
			submitEmailForm($("#freeFixOfferForm"), $("#ajaxLoad-freeFixOffer")); 
		}
	}); 
	
	$('#referralSubmitBtn').click(function(){ 
										   
		ans = parseInt($("#logicAnswer", $("#referralForm")).val()); 
										   
		if ( $("#referCustName", $("#referralForm")).val()=="" || $("#referCustName", $("#referralForm")).val().length<2 ||
		   $("#referBusEmail", $("#referralForm")).val()=="" /* || Validate email format as well */ ) 
			alert("Make sure you fill in the Name and Email of the Business you're refering."); 
		else if ( $("#custName", $("#referralForm")).val()=="" || $("#custName", $("#referralForm")).val().length<2 ||
		   $("#custEmail", $("#referralForm")).val()=="" /* || Validate email format as well */ ) 
			alert("Make sure you fill in your own Name and Email as well."); 
		else if ( $("#logicAnswer", $("#referralForm")).val()=="" || isNaN(ans) || ans!=(num1+num2) ) 
			alert("Please answer the Math Sum question correctly before proceeding."); 
		else {
			$("#logicAnswer", $("#referralForm")).val(ans+""); 
			$("#logicAnswer", $("#referralForm")).attr({ "disabled" : "disabled" }); 
			$("#referral_hiddFld", $("#referralForm")).val(ans+""); 
			submitEmailForm($("#referralForm"), $("#ajaxLoad-referral")); 
		}
	}); 
	
   
   if ( document.getElementById("emailAddress") ) {
		
		var emlAdd = document.getElementById("emailAddress"); 
		var subBtn = document.getElementById("subscribeBtn"); 
		
		if ( window.attachEvent ) { 
		
			emlAdd.attachEvent('onfocus', function(){ changeEmailAddTxt("focus"); }); 
			emlAdd.attachEvent('onclick', function(){ changeEmailAddTxt("focus"); }); 
			emlAdd.attachEvent('onblur', function(){ changeEmailAddTxt("blur"); }); 
			
			if ( subBtn ) subBtn.attachEvent('onclick', function(){ confirmSubscribe(); }); 
			
		} else if ( window.addEventListener ) { 
		
			emlAdd.addEventListener('focus', function(){ changeEmailAddTxt("focus"); }, false); 
			emlAdd.addEventListener('click', function(){ changeEmailAddTxt("focus"); }, false); 
			emlAdd.addEventListener('blur', function(){ changeEmailAddTxt("blur"); }, false); 
			
			if ( subBtn ) subBtn.addEventListener('click', function(){ confirmSubscribe(); }, false); 
			
		}
		
   }
   
   	$('#freeFixOfferBtn').click(function(){
		
		//  open fancybox with div & form as the content 
		$.fancybox({
			type : 'inline', 
			href : '#freeFixOfferDiv', 
			overlayColor : 'white', 
			overlayOpacity : 0.7, 
			hideOnOverlayClick : false, 
			onClosed : function () {
				$('.popupmsg', $('#freeFixOfferDiv')).html(""); 
				$('.popupmsg', $('#freeFixOfferDiv')).hide(); 
			}
		}); 
		
	}); 
	
	$('#referralBtn').click(function(){
		
		//  open fancybox with div & form as the content 
		$.fancybox({
			type : 'inline', 
			href : '#referralForm', 
			overlayColor : 'white', 
			overlayOpacity : 0.7, 
			hideOnOverlayClick : false, 
			autoDimensions : true, 
			padding : 15, 
			margin : 5, 
			onClosed : function () {
				$('.popupmsg', $('#referralForm')).html(""); 
				$('.popupmsg', $('#referralForm')).hide(); 
			}
		}); 
		
	}); 
   
 }); 


function changeEmailAddTxt ( focusOrBlur ) {
		
	var emlAdd = document.getElementById("emailAddress"); 
		
	if ( focusOrBlur=="focus" ) {
		
		emlAdd.style.color = "black"; 
		if ( emlAdd.value.toLowerCase()=="email address" ) 
		emlAdd.value = ""; 
		
	} else if ( focusOrBlur=="blur" ) {
		
		if ( emlAdd.value=="" ) {
			emlAdd.style.color = "#d2d2d2"; 
			emlAdd.value = "Email Address";
		}
		
	}
		
}


function confirmSubscribe () {
	
	if ( document.getElementById("emailAddressBox2").value=="" ) {
		
		//  Validate email address 
		
		var emailAdd = document.getElementById("emailAddress"); 
		
		if ( emailAdd ) { 
		
			var compareRegEx = /^[A-Za-z0-9\._\-]{2,}@[A-Za-z0-9\._\-:;\|#]{2,}\.[A-Za-z]{2,}$/; 
			
			if ( emailAdd.value=="" || emailAdd.value.toLowerCase()=="email address" ) 
				alert("Fill in an Email Address first."); 
			else if ( !emailAdd.value.match(compareRegEx) )
				alert("This website only accepts valid email addresses. Please try the address again before continuing."); 
			else if ( emailAdd.value.match(compareRegEx) ) {
				
				functionsArray.push(' getDBInfo("./includes/phpIncs/phpAjax.php?submitEml=true&emlAdd=' + encodeURIComponent(emailAdd.value) + '", displayJSONInfo, "ajaxDiv01", "GET"); '); 
				functionsArray.push(' displayEmailConfirmInfo(); '); 
				runArrayFunction(); 
				
			}
			
		}
		
	}
	
}


function displayEmailConfirmInfo () {
	
	// Display return message here.... 
	
	if ( typeof(jsonObj.msg)!="undefined" && document.getElementById("generalMsgDiv") ) {
		
		msgDiv = document.getElementById("generalMsgDiv"); 
		
		msgDiv.innerHTML = '<div class="closeBtn" onclick="closeGeneralMsgDiv();">X</div>' + jsonObj.msg; 
		msgDiv.style.display = "block"; 
		
	}
	
}

function closeGeneralMsgDiv () {
	
	document.getElementById('generalMsgDiv').style.display = 'none'; 
	
}


