/**
* Written by Rob Schmitt, The Web Developer's Blog
* http://webdeveloper.beforeseven.com/
*/
// The following variables may be adjusted

// This is the validation function:
function validateMyAjaxInputs() {
  // Start validation:
  jQuery.validity.start();
  //jQuery.validity.setup({ outputMode:"modal" });
	jQuery.extend(jQuery.validity.messages, {
    require:"Please enter a valid email address...",
    // Format validators:
    email:"Please enter a valid email address..."
	});
    
  // Validator methods go here:
  jQuery("#email").require().match('email');
  
  // All of the validator methods have been called:
  // End the validation session:
  var result = jQuery.validity.end();
  
  // Return whether it's okay to proceed with the Ajax:
  return result.valid;
}


jQuery(document).ready(function() {	

	//if enter is pressed
	$(document).keyup(function(event) {
		if (event.keyCode == 13) {
			$('.button').trigger('click');
			}
	})

	//if submit button is clicked		
	$('.submit-button').click(function () {
    if (validateMyAjaxInputs()) {				
			//Get the data from all the fields
			var name = $('input[name=name]');
			var company = $('input[name=company]');
			var email = $('input[name=email]');	
			var agents = $("input:radio:checked").val();
			var disrupt = $('input[name=disrupt]');
				
			//organize the data properly
			var data = 'name=' + name.val() + '&company=' + company.val() + '&email=' + email.val() + '&agents=' + agents  + '&disrupt=' + disrupt.val();		
			//disabled all the text fields and hide the submit button
			$('.text').attr('disabled','true');
			//$('#sendinfo-button').attr("disabled", "true");
			$('#sendinfo-button').hide();
			$('#senddone-button').show();
			//$('#sendinfo-submitted').show();
			//show the loading sign
			$('#loading').show();
			//start the ajax
			jQuery.ajax({
				//this is the php file that processes the data and send mail
				url: "server/sendmail.php",			
				//GET method is used
				type: "POST",
				//pass the data			
				data: data,		
				//Do not cache the page
				cache: false,
				//success
				success: function (html) {				
					if(html!='Success'){
						document.getElementById("done-text").innerHTML=html;}
					$('#contact-area').hide();
					$('#loading').hide();
					$('#signup-area').hide();				
					//show the message
					document.getElementById('betasignuptext').innerHTML="Sign up complete...";
					$('#thankyouninja').show();
					$('#done-text').fadeIn('slow');
					_kmq.push(['record', 'Signed up for Interest List']);
					}						
				});
				//cancel the submit button default behaviours
				return false;
		}else{
			//cancel the submit button default behaviours
			return false;
	    }
	});	
});		
