function filterTemplate(type) {
	switch (type) {
		case "leftSide":
			jQuery(".options2 a").addClass("selected");
			jQuery(".options1 a, .options3 a").removeClass("selected");
			jQuery(".rightSide, .single").fadeOut(500);
			jQuery(".leftSide").fadeIn(400);
			break;
		case "rightSide":
			jQuery(".options3 a").addClass("selected");
			jQuery(".options1 a, .options2 a").removeClass("selected");
			jQuery(".leftSide, .single").fadeOut(500);
			jQuery(".rightSide").fadeIn(400);
			break;
		default:
			jQuery(".options1 a").addClass("selected");
			jQuery(".options2 a, .options3 a").removeClass("selected");
			jQuery(".rightSide, .leftSide").fadeOut(500);
			jQuery(".single").fadeIn(400);
			break;			
	}	
}






function validateFormOnSubmitSubscribe(theForm) {
	// First, disable the form from submitting
    jQuery('#subscribeENews').submit(function() { return false; });
    
    // Grab form action
    var formAction = jQuery("#subscribeENews").attr("action");
    
	// Validate email address with regex
	emailId = "subscribeBox";
    if (!checkEmail(emailId)) 
    {
        //fld.style.background = '#E9E9E9';
		jQuery("#subscribeBox").css({'background-color':'#E9E9E9', 'color':'#000', 'border':'1px solid #FF0000'});
        return;
    }

    // Serialize form values to be submitted with POST
    var str = jQuery("form").serialize();
    
    // Add form action to end of serialized data
    final = str + "&action=" + formAction;

    // Submit the form via ajax
    jQuery.ajax({
       url: "proxy.php",
        type: "POST",
        data: final,
        success: function(html){
            jQuery("#subscribeBox").css({'background-color':'#FFF', 'color':'#7E7756', 'border-color':'#848484 #FAFAFA #FAFAFA #848484'});
			jQuery(".black_overlay").fadeIn(500);
			jQuery("#light").fadeIn(800);		
        }
    });
}

function checkEmail(email) {    
    var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var emailVal = jQuery("#" + email).val();
    return pattern.test(emailVal);
}






function validateFormOnSubmitContactUs(theForm) {
	var reason = "";
	
	reason += validateEmpty(theForm.name, 'name');
	reason += validateEmpty(theForm.company, 'company');
	reason += validateEmail(theForm.email, 'email');
	reason += validateEmpty(theForm.message, 'message');
	
	if (reason != "") {
		return false;
	}
	
	var name = jQuery("#name").val();
	var company = jQuery("#company").val();
	var email = jQuery("#email").val();
	var message = jQuery("#message").val(); 
		
	jQuery.post("send-mail-contact.php", {name: ""+name+"", company: ""+company+"", email: ""+email+"", message: ""+message+""},
		function(data){
			window.location = "http://www.beansmail.com/contact-success.html";
		}
	);		
}


function validateFormOnSubmitFreeTrial(theForm) {
	var reason = "";
	
	reason += validateEmpty(theForm.name, 'name');
	reason += validateEmpty(theForm.busName, 'busName');
	reason += validateEmail(theForm.email, 'email');
	
	if (reason != "") {
		return false;
	}
	
	var name = jQuery("#name").val();
	var busName = jQuery("#busName").val();
	var email = jQuery("#email").val();	
	
	var options = { path: '/', expires: 1 };
	jQuery.cookie("freeTrialEmail", email, options);
	
	
	if (jQuery('input[name=update]').attr('checked') == true) {
		var update = "Yes";
	} else {
		var update = "No";
	}
	
	if (jQuery('input[name=full]').attr('checked') == true) {
		var full = "Yes";
	} else {
		var full = "No";
	}
	
	if (jQuery('input[name=diy]').attr('checked') == true) {
		var diy = "Yes";
	} else {
		var diy = "No";
	}
	
	var html = "<p>Thank you for joining us! We usually reply within a few hours during the week, if you do not hear from us in two business days please do not hesitate to give us a call on +852 3106 5181</p>";
		
	jQuery.post("send-mail-free-trial.php", {name: ""+name+"", busName: ""+busName+"", email: ""+email+"", update: ""+update+"", full: ""+full+"", diy: ""+diy+""},
		function(data){	
			window.location = "http://www.beansmail.com/free-trial-success.html";
		}
	);
}


function validateEmpty(fld, fval) {
    var error = "";
	
    if (fld.value.length == 0) {
        fld.style.background = '#E9E9E9'; 
        jQuery("#"+fval).css({'color':'#000'});
		jQuery("#"+fval).css({'border':'2px solid #FF0000'});
		jQuery("#"+fval+"_error").text('Please fill in missing fields');
		jQuery("#"+fval+"_error").show();
        error = "The required field has not been filled in.\n"
    } else {
        jQuery("#"+fval).css({'color':'#000'});
		jQuery("#"+fval+"_error").hide();
        fld.style.background = 'White';
		jQuery("#"+fval).css({'border':'1px solid #CCC'});
    }
    return error;  
}
function validateUsername(fld, fval) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
	
    if (fld.value == "") {
        fld.style.background = '#E9E9E9'; 
        jQuery("#"+fval).css({'color':'#000'});
		jQuery("#"+fval).css({'border':'2px solid #FF0000'});		
		jQuery("#"+fval+"_error").text('Please fill in missing fields');
		jQuery("#"+fval+"_error").show();
        error = "You didn't enter a username.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#E9E9E9';
        jQuery("#"+fval).css({'color':'#000'}); 
		jQuery("#"+fval).css({'border':'2px solid #FF0000'});
		jQuery("#"+fval+"_error").text('Please fill in missing fields');
		jQuery("#"+fval+"_error").show();
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
        jQuery("#"+fval).css({'color':'#000'});
		jQuery("#"+fval).css({'border':'1px solid #CCC'});
		jQuery("#"+fval+"_error").hide();
    }
    return error;
}
function trim(s)
{
	return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld, fval) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	
if (fld.value == "") {
	fld.style.background = '#E9E9E9';
	jQuery("#"+fval).css({'color':'#000'});
	jQuery("#"+fval).css({'border':'2px solid #FF0000'});
	jQuery("#"+fval+"_error").text('Please fill in missing fields');
	jQuery("#"+fval+"_error").show();
	error = "You didn't enter an email address.\n";
} else if (!emailFilter.test(tfld)) {              //test email for illegal characters
	fld.style.background = '#E9E9E9';
	jQuery("#"+fval).css({'color':'#000'});
	jQuery("#"+fval).css({'border':'2px solid #FF0000'});
	jQuery("#"+fval+"_error").text('Please fill in a valid email address');
	jQuery("#"+fval+"_error").show();
	error = "Please enter a valid email address.\n";
} else if (fld.value.match(illegalChars)) {
	fld.style.background = '#E9E9E9';
	jQuery("#"+fval).css({'color':'#000'});
	jQuery("#"+fval).css({'border':'2px solid #FF0000'});
	jQuery("#"+fval+"_error").text('The email address contains illegal characters');
	jQuery("#"+fval+"_error").show();
	error = "The email address contains illegal characters.\n";
} else {
	fld.style.background = 'White';
	jQuery("#"+fval).css({'color':'#000'});
	jQuery("#"+fval).css({'border':'1px solid #CCC'});
	jQuery("#"+fval+"_error").hide();
}
return error;
}




