$(document).ready(function() {
	// SIGNUP FORM
	options = {
		//target:        '#contact_response',   // target element(s) to be updated with server response 
		beforeSubmit:  showRequest,  // pre-submit callback 
		success:       showResponse  // post-submit callback 
	};
	
	$('.koreSignup li, .koreSignup li').click(function(){
		$('.koreSignup li span').removeClass('selected');
		$('span', this).addClass('selected');
		$('input#email_type').val($('label', this).html());
		return false;
	});
	
	// bind to the form's submit event 
	$('#subscribe_form').submit(function() { 
		$(this).ajaxSubmit(options);
		// always return false to prevent standard browser submit and page navigation 
		return false; 
	});
	
	$('span#signup_submit').click(function(){
		$('#signup_form').ajaxSubmit(options);
		return false;
	});
	
	// FORM INTERACTIVITY
	// FIX PLACEHOLDER TEXT IN IE + OTHERS
	$('[placeholder]').focus(function() {
	  var input = $(this);
	  if (input.val() == input.attr('placeholder')) {
		input.val('');
		input.removeClass('placeholder');
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == '' || input.val() == input.attr('placeholder')) {
		input.addClass('placeholder');
		input.val(input.attr('placeholder'));
	  }
	}).blur().parents('form').submit(function() {
	  $(this).find('[placeholder]').each(function() {
		var input = $(this);
		if (input.val() == input.attr('placeholder')) {
		  input.val('');
		}
	  })
	});
	
	var checked = true;
	$('#contact_subscriber, #contact_checkbox').click(function(){
		if(checked){
			$('#contact_checkbox').addClass("contact_unchecked").removeClass("contact_checked");
			checked = false;
			$('#contact_subscriber').val("false");
		}else{
			$('#contact_checkbox').addClass("contact_checked").removeClass("contact_unchecked");
			checked = true;
			$('#contact_subscriber').val("true");
		};
		return false;
	});
});

///////////////////////////////////////
// CONTACT FORM PRE + POST FUNCTIONS
///////////////////////////////////////
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 	
	var form = jqForm[0];
	
	if(form.fname.value == "FIRST NAME." || !form.fname.value || form.fname.value == " "){
		jAlert('Please insert your first name...', 'Error');
		return false;	
	}
	if(form.lname.value == "LAST NAME." || !form.lname.value || form.lname.value == " "){
		jAlert('Please insert your last name...', 'Error');
		return false;	
	}
	if(!validateEmail(form.email.value)){
		jAlert('Please insert a valid email...', 'Error');
		return false;	
	}
	
	// Submit the form
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
	responseMessage = responseText;
	
	if(responseMessage.indexOf("Oops") !== -1){
		jAlert(responseMessage, 'Email already subscribed...');
	}else if(responseMessage.indexOf("Sorry") !== -1){
		jAlert(responseMessage, 'An error has occurred...');
	}else{
		jAlert(responseMessage, 'Success!');
		$(".koreSignup form input").val("");
	}
	
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.'); 
	//$('#contact_response').html(responseText);
}

function validateEmail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\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);
	return pattern.test(emailAddress);
}
