$(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();	
		
			writeTextArea();
		}
			
		//fill in the textarea with retained value
		function writeTextArea()
		{
			issue.val($("#restoreIssue").val());
		}
		
		//Be sure the name only includes letters, spaces
		//periods and commas and that's it's at least 5
		//characters long
		function validateName()
		{
			var filter = /^[\w\s.,]*$/i;
			if(name.val().length < 5 || !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();
			}
		}
		
		//check required fields
		$("#submit").click(
			function()
			{
				initVars();

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

				validateIssue();
			
				if(problems > 0) {
					return false;
				}
				else {
					process.show();
					buttons.hide();
					return true;
				}
			}
		);
		
		$('a').hover(function()
		{
			$(this).css('cursor','pointer');
		});
		
		//initialize
		init();
	}
);

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

function tabselected(index)
{	
	var styleName = "class";
	var sBrowser = navigator.userAgent;
	if (sBrowser.toLowerCase().indexOf('msie') > -1)
		styleName = "className";

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