$(document).ready(function(){
	gnashArr = ["c56sku", "7y4fg8", "h1864w", "88gmxl", "j9csdq", "v6nb2s", "et6yhg", "31juy7", "z0oh14", "77gfsp", "2sliww", "xki49d"];
	// Place ID's of all required fields here.
	required = ["fullName", "comments", "captCode"];
	// If using an ID other than #email or #error then replace them here
	email = $("#eMail");
	errornotice = $("#errorMsg");
	// The text to show up within a field when it is incorrect
	emptyerror = "ERROR - Please fill out this field.";
	emailerror = "ERROR - Please enter a valid e-mail.";
	/* POST01 SECTION */
	$(".submit").click(function(){	
		// Get forms elements and their values
		//alert($("#fName").val());
		//alert($("input[name='radPAY']:checked").val());
				
		//Validate required fields
		for (i = 0; i < required.length; i++) {
			var input = $('#' + required[i]);
			var inputIMG = input.attr("id");
			inputIMG = inputIMG + "Img";
			if ((input.val() == "") || (input.val() == emptyerror)) {
				input.addClass("gnashEmpty");
				input.val(emptyerror);
				$('#' + inputIMG).show();
				errornotice.fadeIn(500);
			}
			else {
				input.removeClass("gnashEmpty");
			}
		}
		// Validate Email Address.
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("gnashEmpty");
			email.val(emailerror);
		}
		//If any input boxes on this page have the class 'gnashEmpty' the form will not submit
		if ($(":input").hasClass("gnashEmpty")) {
			return false;
		}
		//CHECK CAPTCHA CODE ENTERED
		if ($.inArray($("#captCode").val().toLowerCase(), gnashArr) == -1) {
			alert("Invalid Code Entered! Please check and try again.");
			//alert($("#captCode").val());
			return false;
		}
		else {
			errornotice.hide();
			//PREVENT THE FORM BEING SUBMITTED MORE THAN ONCE //url: $form.attr('action'), data: $form.serialize()
			$(":submit", this).attr("disabled", "disabled");
			$.ajax({ 
            	type: "POST",
            	url: "grEmail.aspx",
            	data: $("#contactForm").serialize(),
            	error: function() {
            		//$(".grForm").hide();
					//$(".grERR").fadeIn(1000);
					alert('Sorry, there has been an error. Please try again later.');
				}, 
            	success: function() { 
                	//do something with response 
                	//alert( $.cookie("whichPOST") );
                	alert('Thank you');
                	//$("#addSnippets").dialog("close");
                	window.location = "Default.aspx";
                	//$(".grForm").hide();
					//$(".grTHX").fadeIn(1000);
					//alert( $('input[name=radPAY]:checked').val() );
					/*if ( $('input[name=radPAY]:checked').val() == "PayPal" ) {
						window.location.replace("paypal-button.aspx");
					}*/ 
        	    } 
        	});
        	return false;
		}
	});
	// Clears any fields in the form when the user clicks or tabs to them
	$(":input").focus(function(){		
		if ($(this).hasClass("gnashEmpty") ) {
			$(this).val("");
			$(this).removeClass("gnashEmpty");
			
			for (i = 0; i < required.length; i++) {
				var input = $('#' + required[i]);
				var clearMSG = false;
				if ((input.val() == emailerror) || (input.val() == emptyerror)) {
					clearMSG = false;
					break;
				}
				else {
					clearMSG = true;
				}
			}
			if (clearMSG == true) {
				errornotice.hide();
			}
			//CHECK FOR IMAGE AND HIDE IT
			var inputIMG = $(this).attr("id");
			inputIMG = inputIMG + "Img";
			if(document.getElementById(inputIMG)) {
				$('#' + inputIMG).hide();
			}
		}
	});
});	
