$(document).ready(
	function()
	{
		//set up variables
		var name = $("#name");
		var email = $("#email");
		var product = $("#product");
		var issue = $("#issue");
		
		var checkname = $("#checkname");
		var checkemail = $("#checkemail");
		var checkproduct = $("#checkproduct");
		var checkissue = $("#checkissue");
		var process = $("#processing");
		var buttons = $("#buttons");
		
		var tech = $("tech");
		var sales = $("sales");
		
		var problems = 0;
		
		function init() 
		{
			initVars();
			process.hide();	
		}
					
		//validate name
		function validateName()
		{
			var filter = /^[\w\s.,]*$/i;
			if(name.val().length < 2 || !filter.test(name.val()))
			{
				
				problems++;
				checkname.show();
			}
		}
		
		//validate email addresss
		function validateEmail()
		{
			var filter = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(\+?[\w-]+)?(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
			
			if(!filter.test(email.val()))
			{
				problems++;
				checkemail.show();
			}
		}
		
		//validate product name
		function validateProduct()
		{
			var filter = /^[\w\s.,]*$/i;
			if(product.val().length < 3 || !filter.test(product.val()))
			{
				
				problems++;
				checkproduct.show();
			}
		}
		
		//validate that a issue exists
		function validateIssue()
		{
			if(issue.val().length < 2 )
			{
				problems++;
				checkissue.show();
			}
		}
		
		function clearForm()
		{
			name.val('');
			email.val('');
			product.val('');
			issue.val('');

			$("#recaptcha_response_field").val('');
			Recaptcha.reload();
		}
		
		//check required fields
		$("#supportForm").submit(
			function(event)
			{
				event.preventDefault();
				
				initVars();

				problems = 0;
				validateName();
				validateEmail();
				validateProduct();

				validateIssue();
			
				if(problems == 0) 
				{
					process.show();
					buttons.hide();
					
					$.ajax({	
						url: '/sendmail/ajaxrecaptcha',
						data: 'recaptcha_challenge_field=' + $("#recaptcha_challenge_field").val() + "&recaptcha_response_field=" + $("#recaptcha_response_field").val(),
						dataType: 'json',
						type: 'post',
						success: function (response) 
						{	
							$("#checkcaptcha").hide();
							
							if (response.verified == true)
							{
								var dataString = 'type=' + Base64.encode('support') + '&name=' + Base64.encode(name.val()) + '&email=' + Base64.encode(email.val());
								dataString += "&product=" + Base64.encode(product.val()) + "&issue=" + Base64.encode(issue.val()); 
								dataString += "&subject=" + Base64.encode($("#subject").val()) + "&ipAddress=" + response.ipAddress;
								
								sendMail(dataString, "popup");
								
								clearForm();
							}
							else
							{
								process.hide();
								buttons.show();
								
								$("#checkcaptcha").show();
								
								$("#recaptcha_response_field").val('');
							}	
						},
						error: function(xhr) { process.hide(); buttons.show(); alert(xhr.responseText); }
					});					
				}
			}
		);
		
		$('a').hover(function() { $(this).css('cursor','pointer'); });
			
		$(".sendmailPopupLink").openDOMWindow({ height:150, width:200, windowSourceID:'#test' });
		
		//initialize
		init();
	}
);

function initVars()
{
	$("#checkname").hide();
	$("#checkemail").hide();
	$("#checkproduct").hide();
	$("#checkissue").hide();
}

// Toggle between the support and technical support forms
function tabselected(index)
{	
	var styleName = "class";
	if($.browser.msie)
		styleName = "className";

	if(index == 1)
	{
		$("#sales").attr(styleName, "unselected");
		$("#tech").attr(styleName, "selected");

		$("#subject").val("Technical");
		$("#submit").val("CONTACT TECHNICAL SUPPORT");
		$('#techHeading').css ('display', "inline");
		$('#salesHeading').css ('display', "none");
	}
	else if(index == 2)
	{
		$("#sales").attr(styleName, "selected");
		$("#tech").attr(styleName, "unselected");
		
		$("#subject").val("Sales");
		$("#submit").val("CONTACT SALES");
		$('#techHeading').css ('display', "none");
		$('#salesHeading').css ('display', "inline");
	}
	
	initVars();
}
