/* guw-custom */
function newsletterSignUp(_this) {

	strEmail = jQuery('#nsu-email-1').val();
	re_email = /^[a-zA-Z0-9\._-]+@([a-zA-Z0-9]){1}[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,3}$/g;
	if( !re_email.test(strEmail) ) {
		alert('Please enter a valid email address');
		return false;
	}

	oform = jQuery(_this).parents('form').get(0);
	strdata = jQuery(oform).serialize();
	
	jQuery.ajax({
		url : '/_processNewsSignup.php',
		type : 'POST',
		data : strdata,
		datatype : 'json',
		success : function(response){
			console.log(response);
			jQuery(oform).replaceWith('<div id="newsletter-confirmation" style="display:none;"><p>You have been added to the Give Us Wings mailing list. <p></div>');
			jQuery('#newsletter-confirmation').fadeIn(200);
		},
		error : function(response){
		},
		failure : function(response){
		},
		loading : function(){
		}
	});
}




function verifyAndSendContact() {
	var oForm = {};
	var str = jQuery('#contact-us-form').serialize();
	var bsend = true;
	str.replace(
		new RegExp("([^?=&]+)(=([^&]*))?", "g"),
		function($0, $1, $2, $3) { oForm[$1] = $3; }
	);
	if( !oForm['email'].length ) {
		flag('email', 'Please include your e-mail address');
		bsend = false;
	} else {
		re = new RegExp("^[0-9a-zA-Z-._]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}$");
		if(!re.test(decodeURIComponent(oForm['email']))){
			flag('email', ( oForm['email'].length > 15 ? oForm['email'].substr(0,15) +'...': oForm['email'] )+ ' is not a valid e-mail address');
			bsend = false;
		} else {
			unflag('email');
		}
	}
	if( oForm['message'].length < 5 ) {
		flag('message', 'Please include a message');
		bsend = false;
	} else {
		unflag('email');
	}
	if(bsend) {
		// Disable send button 
		jQuery('#send-button').unbind('click');
		sendContact(str);
	}
}

function flag(ele, str) {
	jQuery('#' + ele).css({"border-color":"#c00"});
	jQuery('#' + ele).next('div').text(decodeURIComponent(str));
}
function unflag(ele) {
	jQuery('#' + ele).css({"border-color":"#360"});
	jQuery('#' + ele).next('div').text("");
}



function sendContact(qs){
	var bsuccess = false;
	jQuery.ajax({
		url : '/_processContactUsForm.php',
		type : 'POST',
		data : qs,
		datatype : 'json',
		success : function(response){
			bsuccess = true;
			jQuery('#contact-us-form').after("<h3>Sent!</h3><p>Your message has been sent! Thank you for contacting Give Us Wings!</p>");
			jQuery('#contact-us-form').remove();
		},
		error : function(response){
			// re-enable send button
			jQuery('#send-button').click(verifyAndSendContact());
			flag('message','There was an error');
		},
		failure : function(response){
			jQuery('#send-button').click(verifyAndSendContact());
			flag('message','There was an error');
		},
		loading : function(){ }
	});	
	if(bsuccess){
	} else {
	
	}
}
