$(function() {
	$('.error').hide();
	$('.error_radio').hide();
	$(".newsletterSubmit").click(function() {
		validateAndProcessData();
	});
});

function validateAndProcessData () {
	$('.error').hide();
	$('.error_radio').hide();
	var emailSubject = "Newsletter Sign Up";
	var toEmail = "brobertson@apxalarm.com";
	var ccAddress = "jhouser@apxalarm.com";
	var toName = "";
	var ccName = "";
	var fName = $("input#newsLetterFirstName").val();
	if (typeof(fName)=="undefined" || fName == null || fName == "") {
		$("label#newsLetterFirstName_error").show();
		$("input#newsLetterFirstName").focus();
		return false;
	}

	var lName = $("input#newsLetterLastName").val();
	if (typeof(lName)=="undefined" || lName == null || lName == "") {
		$("label#newsLetterLastName_error").show();
		$("input#newsLetterLastName").focus();
		return false;
	}

	var hPhone = $("input#newsLetterPhone").val();
	if (typeof(hPhone)=="undefined" || hPhone == null || hPhone == "") {
		$("label#newsLetterPhone_error").show();
		$("input#newsLetterPhone").focus();
		return false;
	}

	var email = $("input#newsLetterEmail").val();
	if (typeof(email)=="undefined" || email == null || email == "") {
		$("label#newsLetterEmail_error").show();
		$("input#newsLetterEmail").focus();
		return false;
	}
	if ((email.indexOf('@') <= 0) || ((email.charAt(email.length-4) != '.') && (email.charAt(email.length-3) != '.')))
	{	
		$("label#newsLetterEmail_error").show();
		$("input#newsLetterEmail").focus();
		return false;
	} 
	
	var zip = $("input#newsLetterZip").val();
	if (typeof(zip)=="undefined" || zip == null || zip == "") {
		$("label#newsLetterZip_error").show();
		$("input#newsLetterZip").focus();
		return false;
	}
	
	var customer = $("input[name=areYouCustomer]:checked").val();
	if (typeof(customer)=="undefined" || customer == null || customer == "") {
		$("label#areYouCustomer_error").show();
		return false;
	}
	
	var optIn = $("input[name=receiveInfo]:checked").val();
	if (typeof(optIn)=="undefined" || optIn == null || optIn == "") {
		$("label#receiveInfo_error").show();
		return false;
	}

	// DEBUG ONLY!!!  REMOVE LATER --------
	//toEmail = "tcoleman@apxalarm.com";
	//  toEmail = "jfackrell@apxalarm.com";
	//  mailCC = "mwarner@apxalarm.com";
	// ------------------------------------

	var emailData = 'toEmail=' + toEmail + '&toName=' + toName + '&ccAddress=' + ccAddress + '&ccName=' + ccName + '&emailSubject=' + emailSubject;
	var messageBody = 'fName='+ php_urlencode(fName) + '&lName=' + php_urlencode(lName) + '&hPhone=' + php_urlencode(hPhone);
	var messageBody2 = 'email=' + php_urlencode(email) + '&zip=' + php_urlencode(zip) + '&customer=' + customer + '&optIn=' + optIn;
	
	var dataString = emailData + '&' + messageBody + '&' + messageBody2;
	submitFormData (dataString);
}

function submitFormData (dataString) {
	// email
	var urlText = "/Collector/FormCollector/Email/XML";
	//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")
			{
				$('#newsLetterMain').html("<div id='success'></div>");  
				$('#success').html("<p>Thank you for signing up. You will begin receiving the newsletter shortly.</p>")  
				.hide()  
				.fadeIn(1500, function() {  
					$('#success').append("");  
				});  
			} else {
				$('#newsLetterMain').html("<div id='success'></div>");  
				$('#success').html("<p>There was an 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")
			{
				$('#newsLetterMain').html("<div id='success'></div>");  
				$('#success').html("<p>Thank you for signing up. You will begin receiving the newsletter shortly.</p>")  
				.hide()  
				.fadeIn(1500, function() {  
					$('#success').append("");  
				});
			} else {
				$('#newsLetterMain').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;
}
);
}