/*Global Email validation*/
function emailValidate(email){
    var emailRE = /^[a-zA-Z0-9_\+-]+(\.[a-zA-Z0-9_\+-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,4})$/;
    if (email.match(emailRE)){
        return true;
    }
    else {
        return false;
    }
}

/*Validate Tell a Friend Form before send it*/
function validateTellFriend(){
	error = 0;
	var form = document.getElementById("formTellFriend");	
	
	cleanTellFriend();
	
	if (form.tellFriendName.value == ""){
		error = 1;
		$('#tellFriendName').css("border","solid 1px red");
		$('#tellFriendName-error').css("display","block");
	}
	if (!(emailValidate(form.tellFriendMyEmail.value))){
		error = 1;
		$('#tellFriendMyEmail').css("border","solid 1px red");
		$('#tellFriendMyEmail-error').css("display","block");
	}
	if (!(emailValidate(form.tellFriendFEmail.value))){
		error = 1;
		$('#tellFriendFEmail').css("border","solid 1px red");
		
		if (form.tellFriendFEmail.value == "")
			$('#tellFriendFEmail-error').text("And who might be getting this suggestion?");
		else $('#tellFriendFEmail-error').text("Mmm... this doesn't look good...");
		
		$('#tellFriendFEmail-error').css("display","block");
	}
	if (form.tellFriendMessage.value == ""){
		error = 1;
		$('#tellFriendMessage').css("border","solid 1px red");
		$('#tellFriendMessage-error').css("display","block");
	}
	
	if (error == 1){		
		return false;
	}
	else{
		TellaFriendAjaxRequest();
		cleanTellFriend();
		return false;
	}
}

function cleanTellFriend(){	
	var form = document.getElementById("formTellFriend");
	$('#tellFriendName').css("border","solid 1px #e2e2e2");
	$('#tellFriendMyEmail').css("border","solid 1px #e2e2e2");
	$('#tellFriendFEmail').css("border","solid 1px #e2e2e2");
	$('#tellFriendMessage').css("border","solid 1px #e2e2e2");	
	$('.tellFrienderror').css("display","none");
}

/*Validate Submit Form before send it*/
function validateSubmitFormEmail(){
	error = 0;
	var form = document.getElementById("submitForm");
	//$('#FormEmailerror').css("visibility","hidden");	
	form.submitFormEmail.style.border = "solid 1px #fbe6e5";
	
	if (!(emailValidate(form.submitFormEmail.value))){
		error = 1;
		form.submitFormEmail.style.border = "solid 1px red";

		if (form.submitFormEmail.value == "email address")
			$('#FormEmailerror').text("Yes! We need your email address!");
		else $('#FormEmailerror').text("This does not seem right... please check.");
		
		$('#FormEmailerror').css("color","red");
		$('#FormEmailerror').css("visibility","visible");
	}
	
	if (error == 1){		
		return false;
	}
	else{		
		document.getElementById("submitForm").submit();	
	}
}


/*Validate Search Form before send it*/
function validateSearchForm(){
	error = 0;
	var form = document.getElementById("searchForm");	
	
	form.searchFormText.style.border = "solid 1px #fbe6e5";
	$('#FormSearcherror').css("visibility","hidden");
	
	if (form.searchFormText.value == "" || form.searchFormText.value == "search shopafrolic"){
		error = 1;
		form.searchFormText.style.border = "solid 1px red";
	}
	
	if (error == 1){
		$('#FormSearcherror').css("visibility","visible");
		return false;
	}
	else{
	
	}
}


/*Validate Send Item Form before send it*/
function validateSendItem(){
	error = 0;
	var form = document.getElementById("formSendItem");	
	cleanSendItem();
	
	if (form.sendItemName.value == ""){
		error = 1;
		$('#sendItemName').css("border","solid 1px red");
		$('#sendItemName-error').css("display","block");
	}
	if (form.sendItemLive.value == ""){
		error = 1;
		$('#sendItemLive').css("border","solid 1px red");
		$('#sendItemLive-error').css("display","block");
	}
	if (!(emailValidate(form.sendItemEmail.value))){
		error = 1;
		$('#sendItemEmail').css("border","solid 1px red");
		
		if (form.sendItemEmail.value == "")
			$('#sendItemEmail-error').text("We won't spam you! Please enter your email.");
		else $('#sendItemEmail-error').text("Can you double check this? It does not seem right");
		
		$('#sendItemEmail-error').css("display","block");
	}
	if (form.sendItemUrl.value == ""){
		error = 1;
		$('#sendItemUrl').css("border","solid 1px red");
		$('#sendItemUrl-error').css("display","block");
	}
	if (form.sendItemQuestion.value == ""){
		error = 1;
		$('#sendItemQuestion').css("border","solid 1px red");
		$('#sendItemQuestion-error').css("display","block");
	}
	
	if (error == 1){		
		return false;
	}
	else{

		SendItemAjaxRequest();
		cleanSendItem();
		return false;
	}
}

function cleanSendItem(){
	var form = document.getElementById("formSendItem");	
	$('#sendItemName').css("border","solid 1px #E0E0E0");
	$('#sendItemLive').css("border","solid 1px #E0E0E0");
	$('#sendItemEmail').css("border","solid 1px #E0E0E0");
	$('#sendItemUrl').css("border","solid 1px #E0E0E0");
	$('#sendItemQuestion').css("border","solid 1px #E0E0E0");
	$('.sendItemerror').css("display","none");
}


/*Clean submit form*/
function cleanSubmitForm(){
	var form = document.getElementById("submitForm");	
	if (form.submitFormEmail.value == "email address")
		form.submitFormEmail.value = "";
}


/*Clean search form*/
function cleanSearchForm(){
	var form = document.getElementById("searchForm");	
	if (form.searchFormText.value == "search shopafrolic")
		form.searchFormText.value = "";
}


/*If the input is empty set value = "email address" again*/
function checkSubmitForm(){
	var form = document.getElementById("submitForm");	
	if (form.submitFormEmail.value == ""){
		form.submitFormEmail.value = "email address";	
		var form = document.getElementById("submitForm");
		$('#FormEmailerror').css("visibility","hidden");	
		form.submitFormEmail.style.border = "solid 1px #fbe6e5";
	}
}


/*If the input is empty set value = "search shopafrolic" again*/
function checksearchForm(){
	var form = document.getElementById("searchForm");	
	if (form.searchFormText.value == ""){
		form.searchFormText.value = "search shopafrolic";		
	}
	form.searchFormText.style.border = "solid 1px #fbe6e5";
}

function breadcrumCheck(){		
	var result = $('a').is('.page-item-2.current_page_item')
	if (result){				
		$('#breadcrum').css("visibility","hidden");
	}
	
	$("#breadcrum a:contains('Shop')").css("text-decoration", "none");
	$("#breadcrum a:contains('Shop')").css("cursor", "text");
	$("#breadcrum a:contains('Shop')").click(function(e) {
		return false;
	});
}

function fixBorders(){	
	$('#content').addClass("shopSectionChild")
}

function fixMenuIE6(){	
	var result = $('a').is('.current_page_item')
	var result2 = $('a').is('.page-item-2.current_page_item')
	
	if (result && !result2){			
		var image = $(".current_page_item").css('background-image')
		image = image.replace(/.jpg/,"-active.jpg")
		//image = image.replace(/.gif/,"-active.gif")
		$(".current_page_item").css('background-image', image)
	}
}

function cleanContactCenter(){	
	var form = document.getElementById("contactCenterForm");
	$('#CCname').css("border","solid 1px #e2e2e2");
	$('#CCemail').css("border","solid 1px #e2e2e2");
	$('#CCmessage').css("border","solid 1px #e2e2e2");
	$('.CCerror').css("display","none");
}

function validateContactCenter(){
	error = 0;
	var form = document.getElementById("contactCenterForm");	
	
	cleanContactCenter();
	
	if (form.CCname.value == ""){
		error = 1;
		$('#CCname').css("border","solid 1px red");
		$('#CCnameError').css("display","block");
	}
	if (!(emailValidate(form.CCemail.value))){
		error = 1;
		$('#CCemail').css("border","solid 1px red");
		$('#CCemailError').css("display","block");
	}
	if (form.CCmessage.value == ""){
		error = 1;
		$('#CCmessage').css("border","solid 1px red");
		$('#CCmessageError').css("display","block");
	}
	
	if (error == 1){		
		return false;
	}
	else{
		cleanContactCenter();

		return true;
	}
}

function tellFriendClean(){
	var form = document.getElementById("formTellFriend");	
	if (form.tellFriendMessage.value == "i hope you likee!"){
		form.tellFriendMessage.value = "";
	}

}

function tellFriendBlur(){	
	var form = document.getElementById("formTellFriend");	
	if (form.tellFriendMessage.value == ""){
		form.tellFriendMessage.value = "i hope you likee!";
	}
}

function menuhover(){
	$("#mainMenu ul li ul")        
        .hover(function () {
              $(".page-item-2").css("background-image", "none")
            }, function () {
              $(".page-item-2").removeAttr("style")
            });

}

function display_popup_message(title, msg) {

	$('#labelPopup').hide();
	$('#popupContent').hide();
	$('#popupContentCustomTitle').html( title );
	$('#popupContentCustomMessage').html( msg );
	centerPopup(1);		
	loadPopup();
}

var popup_title_bak = '';
var popup_content_bak = '';

$(document).ready(function(){
						   
	$(".btnmailaFriends").click(function(){	
		centerPopup();		
		loadPopup();
		
		var Pid    = $(this).parent().parent().attr('id');
		var PidNum = Pid.slice( 8 , Pid.length );
		
		$('#popupProduct').attr('src', $('#product_'+PidNum+' .colSectionOne img').attr('src') );
		$('#popupProductTitle').html( $('#product_'+PidNum+' .colSectionTwo  h2').html()  );
		$('#tellFriendItenToSend').val(PidNum);							 
	});
	
	/*Closing Popup*/
	$("#popupContactClose").click(function(){
		cleanTellFriend();
		disablePopup();
	});
	
	/*Click out*/
	$("#backgroundPopup").click(function(){
		cleanTellFriend();
		disablePopup();
	});
	
	/*Press ESC*/
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});				   
	
	$('.page-item-2').click(function(e) {
		return false;
	});
	
	breadcrumCheck()
	fixMenuIE6()
	menuhover()
	
	$('#CCSubmitBtn').click(validateContactCenter)							   	
	$('#tellFriendSubmit').click(validateTellFriend)	
	$('#submitFormEmailSubmit').click(validateSubmitFormEmail)
	$('#searchFormTextSubmit').click(validateSearchForm)
	$('#sendItemSubmit').click(validateSendItem)	
	$('#submitFormEmail').click(cleanSubmitForm)
	$('#searchForm').click(cleanSearchForm)
	$('#tellFriendMessage').click(tellFriendClean)
	$('#tellFriendMessage').blur(tellFriendBlur)
	$('#submitFormEmail').blur(checkSubmitForm)
	$('#searchFormText').blur(checksearchForm)
	
	try {
		if (typeof(submitted) != 'undefined' && submitted == 1){
			display_popup_message('daily emails', 'You have been subscribed. Thanks!');
		}
		else if (typeof(duplicated_email) != 'undefined' && duplicated_email == 1) {
			display_popup_message('daily emails', 'That email address is already registered...');
		}
		else if (typeof(email_error) != 'undefined' && email_error == 1) {
			display_popup_message('daily emails', 'Request Incomplete... Try Again Later !!!');
		}
	}
	catch(err){}
	
});