$(document).ready(function() {

	$('.error').hide();  
	$("#submit").click(function() {  
		// validate and process form here  
		$('.error').hide();
		
		var errors = null;
		
		var name = jQuery.trim($("#name").val());
		var email = jQuery.trim($("#email").val());
		var sex = jQuery.trim($("#sex").val());
		var phone = jQuery.trim($("#phone").val());
		var country = jQuery.trim($("#country").val());
		var year = jQuery.trim($("#year").val());
		
		if ($("#name").hasClass('block_input_error')) { $("input#name").removeClass('block_input_error').addClass('block_input'); }
		if ($("#email").hasClass('block_input_error')) { $("input#email").removeClass('block_input_error').addClass('block_input'); }

		
		if (name == "") {
			errors = true;
			$("#name_error").html("This field is required.").show('drop');
			$("#name").addClass('block_input_error').focus();
		} 
		
		if (email == "") {
			errors = true;
			$("#email_error").html("This field is required.").show('drop');
			$("#email").addClass('block_input_error').focus();	
		} else {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test(email) == false) {
				errors = true;
				$("#email_error").html("Invalid Email Address").show('drop');
				$("#email").addClass('block_input_error').focus();	
			}
		}
		
		
		if (errors == true) {
			return false;
		}
		  var dataString = 'name='+ name + '&email=' + email + '&sex=' + sex + '&phone=' + phone + '&country=' + country + '&year=' + year;
		  $.ajax({
			type: "POST",
			url: "process.php",
			data: dataString,
			success: function() {
			  $('#getDemoInfo').html("<div id='message' class='ui-corner-all'><b>Thanks for taking the time to fill out the questionaire. Please <a href='http://customcms.net/demo/'>click here</a> to view the demo.</b></div>");
			}
		  });
		  return false;
  
	}); 
		
		$("#getDemoInfo").dialog();
		
		
		$('.view-demo').click(function() {
			$('#getDemoInfo').dialog('open');
		});
		
		$('#cancel').click(function() {
			$('#getDemoInfo').dialog('close');
		});
});