var options = { 
	beforeSubmit:  validate,  // pre-submit callback 
	success:       showResponse,  // post-submit callback 
	resetForm: true        // reset the form after successful submit 
}; 
				
$('#contact_us').ajaxForm(options); 
				
function showResponse(responseText, statusText){
	$('#contact_us').slideUp({ opacity: "hide" }, "normal")
	$('#success').fadeIn(2000)
	
}
				
function validate(formData, jqForm, options) {
	$("p.error").slideUp({ opacity: "hide" }, "fast");
			 
	var nameValue = $('input[name=Name]').fieldValue(); 
	var emailValue = $('input[name=Email]').fieldValue();
	var messageValue = $('textarea[name=Message]').fieldValue(); 
	
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var correct = true;
	
	if (!nameValue[0]) {
		$("p.error.wrong_name").slideDown({ opacity: "show" }, "slow");
		correct = false;
	}
	
	if (!emailValue[0]) {
		$("p.error.wrong_email").slideDown({ opacity: "show" }, "slow");
		correct = false;
	} else if(!emailReg.test(emailValue[0])) {
		$("p.error.wrong_email").slideDown({ opacity: "show" }, "slow");
		correct = false;
	}
	
	if (!messageValue[0]) {
		$("p.error.wrong_message").slideDown({ opacity: "show" }, "slow");
		correct = false;
	}
	
	if (!correct) {return false;}
} 	

var request_code_options = { 
	beforeSubmit:  validateRequestCode,  // pre-submit callback 
	success:       showResponse,  // post-submit callback 
	resetForm: true        // reset the form after successful submit 
}; 
$('#request_code').ajaxForm(request_code_options); 
				
function showResponse(responseText, statusText){
	$('#request_code').slideUp({ opacity: "hide" }, "normal")
	$('#success').fadeIn(2000)	
}
$("#enter_code").submit(function(event){
	$("p.error").slideUp({ opacity: "hide" }, "fast");
	event.preventDefault();
	var codeValue = $('#code_field').fieldValue(); 
	if (codeValue.toString().length == 4){
		$.post('/partners/check_code', {'data[Code][x]':codeValue[0]}, function(data){
			if (data && data.response == 'bad'){
				$('p.error.bad_code').slideDown({ opacity: "show" }, "slow");
			} else if (data && data.response == 'good' && data.redirect) {
				window.location = data.redirect;
			}
		},'json');
	}
});
	
function validateRequestCode(formData, jqForm, options) {
	$("p.error").slideUp({ opacity: "hide" }, "fast");
			 
	var nameValue = $('input[name=Name]').fieldValue(); 
	var emailValue = $('input[name=Email]').fieldValue();
	var messageValue = $('input[name=Phone]').fieldValue(); 
	
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var correct = true;
	
	if (!nameValue[0]) {
		$("p.error.wrong_name").slideDown({ opacity: "show" }, "slow");
		correct = false;
	}
	
	if (!emailValue[0]) {
		$("p.error.wrong_email").slideDown({ opacity: "show" }, "slow");
		correct = false;
	} else if(!emailReg.test(emailValue[0])) {
		$("p.error.wrong_email").slideDown({ opacity: "show" }, "slow");
		correct = false;
	}
	
	if (!messageValue[0]) {
		$("p.error.wrong_message").slideDown({ opacity: "show" }, "slow");
		correct = false;
	}
	
	if (!correct) {return false;}
} 	
					 
