$(function() {
	$('.error').hide();
	$('.error_radioBtn').hide();
	$('.error_comment').hide();
	$(".getSystemSubmit").click(function() {
		validateAndProcessData();
	});
});

function validateAndProcessData () {
	$('.error').hide();
	$('.error_radioBtn').hide();
	$('.error_comment').hide();
	var subject = "Get A System";

	var first_name = $("input#getASystemFirstName").val();
	if (typeof(first_name)=="undefined" || first_name == null || first_name == "") {
		$("label#getASystemFirstName_error").show();
		$("input#getASystemFirstName").focus();
		return false;
	}

	var last_name = $("input#getASystemLastName").val();
	if (typeof(last_name)=="undefined" || last_name == null || last_name == "") {
		$("label#getASystemLastName_error").show();
		$("input#getASystemLastName").focus();
		return false;
	}

	var phone = $("input#getASystemPhone").val();
	if (typeof(phone)=="undefined" || phone == null || phone == "") {
		$("label#getASystemPhone_error").show();
		$("input#getASystemPhone").focus();
		return false;
	}
	var test_phone = phone.replace(/[^\d]/g, '');
	var phoneLength = test_phone.length;
	if (phoneLength == 11)
	{
		var firstDigit = test_phone.substring(0,1);
		if (firstDigit == "1")
		{
			test_phone = test_phone.substring(1,test_phone.length);	
		}
	}
	if (test_phone.length != 10)
	{
		$("label#getASystemPhone_error").show();
		$("input#getASystemPhone").focus();
		return false;
	}
	phone = test_phone;

	var email = $("input#getASystemEmail").val();
	if (typeof(email)=="undefined" || email == null || email == "") {
		$("label#getASystemEmail_error").show();
		$("input#getASystemEmail").focus();
		return false;
	}
	if ((email.indexOf('@') <= 0) || ((email.charAt(email.length-4) != '.') && (email.charAt(email.length-3) != '.')))
	{	
		$("label#getASystemEmail_error").show();
		$("input#getASystemEmail").focus();
		return false;
	} 
	
	var zipcode = $("input#getASystemZip").val();
	if (typeof(zipcode)=="undefined" || zipcode == null || zipcode == "") {
		$("label#getASystemZip_error").show();
		$("input#getASystemZip").focus();
		return false;
	}
	
	var ownHome = $("input[name=ownHome]:checked").val();
	if (typeof(ownHome)=="undefined" || ownHome == null || ownHome == "") {
		$("label#ownHome_unchecked").show();
		return false;
	}
	if (ownHome != "yes")
	{
		$("label#ownHome_error").show();
		return false;
	} else {
		ownHome = "yes";	
	}

	var comments_txt = $("textarea#getASystemComments").val();
	if (typeof(comments_txt)=="undefined" || comments_txt == null || comments_txt == "") {
		$("label#getASystemComments_error").show();
		$("textarea#getASystemComments").focus();
		return false;
	}	

	var emailData = 'subject=' + subject;
	var messageBody = 'first_name='+ php_urlencode(first_name) + '&last_name=' + php_urlencode(last_name) + '&phone=' + php_urlencode(phone);
	var messageBody2 = 'email=' + php_urlencode(email) + '&zipcode=' + php_urlencode(zipcode) + '&own_home=' + ownHome + '&comments_txt=' + php_urlencode(comments_txt);
	
	var dataString = emailData + '&' + messageBody + '&' + messageBody2;
	submitFormData (dataString);
}

function submitFormData (dataString) {
	// lead gen
	var urlText = "/Collector/FormCollector/HttpReflector/XML";
	var reflectAddress = "https://production.leadconduit.com/ap/v2/PostLeadAction?xxNodeId=02e4315zm";
	var reflectMethod = "POST";
	dataString = dataString + '&AFID=2100' + '&reflectAddress=' + reflectAddress + '&reflectMethod=' + reflectMethod;
	//alert (dataString);return false;  
	$.ajax({  
		type: "POST",  
		url: urlText,  
		data: dataString,  
		success: function(data) {  
			var str = data.getElementsByTagName("success")[0].childNodes[0].nodeValue;
			if (str == "true")
			{
				$('#getASystemMain').html("<div id='success'></div>");  
				$('#success').html("<p>Thank you for your interest in APX Alarm. A sales specialist will contact you shortly.</p>")  
				.hide()  
				.fadeIn(1500, function() {  
					$('#success').append("");  
				});  
			} else {
				$('#getASystemMain').html("<div id='success'></div>");  
				$('#success').html("<p>There was a problem submitting your information. Please try again later.</p>")  
				.hide()  
				.fadeIn(1500, function() {  
					$('#success').append("");  
				});  
			}
		},
		error: function(data, errorType) {  
			var IE = /*@cc_on!@*/false;
			if (IE && errorType.toUpperCase() == "PARSERERROR")
			{
				$('#getASystemMain').html("<div id='success'></div>");  
				$('#success').html("<p>Thank you for your interest in APX Alarm. A sales specialist will contact you shortly.</p>")  
				.hide()  
				.fadeIn(1500, function() {  
					$('#success').append("");  
				});  
			} else {
				$('#getASystemMain').html("<div id='success'></div>");  
				$('#success').html("<p>Error submitting form. Please try again later.</p>")  
				.hide()  
				.fadeIn(1500, function() {  
					$('#success').append("");  
				});  
			}

		}

	});  
	return false;  
}

function php_urlencode (str) {
str = escape(str);
return str.replace(/[*+\/@]|%20/g,
function (s) {
switch (s) {
case "*": s = "%2A"; break;
case "+": s = "%2B"; break;
case "/": s = "%2F"; break;
case "@": s = "%40"; break;
case "%20": s = "+"; break;
}
return s;
}
);
}