﻿$(document).ready(function () {

	$('.colorbox').colorbox({onComplete:function(){
	    $('.colorbox2').colorbox();
		$.fn.colorbox.resize();
	}});
	
	//Star rating: show correct amount of stars on hover
	$(".stars .star").hover(function() {
		$(this).siblings().andSelf().addClass("hoverOff");
		$(this).prevAll().andSelf().addClass("hoverOn");
	},function() {
		$(this).siblings().andSelf().removeClass("hoverOn hoverOff");
	});
	
	//Star rating: save rating on click and update rating
	$(".stars .star").click(function() {
							  
		var sRating = $(this).attr("rel");
		var iId = $(this).parent().parent().prev().attr("rel");
		
		$(this).parent().load("/ajax/rating.asp?id=" + iId + "&rating=" + sRating).next().html("<strong>Thank you for rating!</strong>");
		
	});
	
	//Contact form validation
	$("#contactForm").submit(function() {
		if ($("#Firstname").val() == "") {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the first name field</strong></p>', innerHeight:20});
			return false;
		}
		if ($("#email").val() == "") {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the email address field</strong></p>', innerHeight:20});
			return false;
		}else if (!isValidEmailAddress($("#email").val())) {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the email address field</strong></p>', innerHeight:20});
			return false;
		}
	});
	
	//Sainsbury's competition form validation
	$("#sainsburyscomp").submit(function() {
		if ($("#fname").val() == "") {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the first name field</strong></p>', innerHeight:20});
			return false;
		}
		if ($("#lname").val() == "") {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the surname field</strong></p>', innerHeight:20});
			return false;
		}
		if ($("#email").val() == "") {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the email address field</strong></p>', innerHeight:20});
			return false;
		}else if (!isValidEmailAddress($("#email").val())) {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the email address is valid</strong></p>', innerHeight:20});
			return false;
		}
		
		if ($("#address").val() == "") {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the address field</strong></p>', innerHeight:20});
			return false;
		}
		
		if ($("#postcode").val() == "") {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the postcode field</strong></p>', innerHeight:20});
			return false;
		}
		
		if ($("#terms").is(":checked")) {
		chkTerms = true;
	}else {
		$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please read and accept our terms and conditions to continue.</strong></p>', innerHeight:20});
			return false;
	}

	});
	
	
	//Tesco competition form validation
	$("#tescocomp").submit(function() {
		if ($("#fname").val() == "") {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the first name field</strong></p>', innerHeight:20});
			return false;
		}
		if ($("#lname").val() == "") {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the surname field</strong></p>', innerHeight:20});
			return false;
		}
		if ($("#email").val() == "") {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the email address field</strong></p>', innerHeight:20});
			return false;
		}else if (!isValidEmailAddress($("#email").val())) {
			$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please check the email address is valid</strong></p>', innerHeight:20});
			return false;
		}
		
		if ($("#terms").is(":checked")) {
		chkTerms = true;
	}else {
		$.fn.colorbox({html:'<p style="color:#fff;font-size:12px"><strong>Please read and accept our terms and conditions to continue.</strong></p>', innerHeight:20});
			return false;
	}

	});
	
});

function callLogin() {
	
	var e = $("#loginemail").val();
	var p = $("#loginpassword").val();
	var r = $("#loginremember").val();
	var id = $("#recipeid").val();

	$.ajax({
		type: "POST",
		url: "/ajax/recipes/",
		data: "action=login&loginemail="+e+"&loginpassword="+p+"&checkbox="+r+"&id="+id,
		async: false,
		success: function(msg) {
			$("#formArea").html(msg);
		},
		error: function() { alert("error"); }
	});
	
}

function callFavourite() {
	
	var sTitle = $("#sTitle").val();
	var forename = $("#forename").val();
	var surname = $("#surname").val();
	var email = $("#email").val();
	var ingredients = $("#ingredients").val();
	var method = $("#method").val();

	$.ajax({
		type: "POST",
		url: "/ajax/favourite/",
		data: "sTitle="+sTitle+"&forename="+forename+"&surname="+surname+"&email="+email+"&ingredients="+ingredients+"&method="+method,
		async: false,
		success: function(msg) {
			$("#formArea").html(msg);
			$.fn.colorbox.resize();
		},
		error: function() { alert("error"); }
	});
	
}

function callComment() {
	
	var sTitle = $("#sTitle").val();
	var forename = $("#forename").val();
	var surname = $("#surname").val();
	var email = $("#email").val();
	var comment = escape($("#comment").val());
	var iId = $("#iId").val();

	$.ajax({
		type: "POST",
		url: "/ajax/comment/",
		data: "sTitle="+sTitle+"&forename="+forename+"&surname="+surname+"&email="+email+"&comment="+comment+"&iId="+iId,
		async: false,
		success: function(msg) {
			$("#formArea").html(msg);
			$.fn.colorbox.resize();
		},
		error: function() { alert("error"); }
	});
	
}

function callComp() {
	
	var sTitle = $("#sTitle").val();
	var forename = $("#forename").val();
	var surname = $("#surname").val();
	var email = $("#email").val();
	var terms;
	if ($("#terms").is(":checked")) {
		terms = true;
	}else {
		terms = false;
	}

	
	var optIn;
	if ($("#optIn").is(":checked")) {
		optIn = true;
	}else {
		optIn = false;
	}

		
	$.ajax({
		type: "POST",
		url: "/ajax/competition/",
		data: "sTitle="+sTitle+"&forename="+forename+"&surname="+surname+"&email="+email+"&terms="+terms+"&optIn="+optIn,
		async: false,
		success: function(msg) {
			$("#formArea").html(msg);
			$.fn.colorbox.resize();
		},
		error: function() { alert("error"); }
	});
	
}


function callComp2() {
	
	var sTitle = $("#sTitle").val();
	var forename = $("#forename").val();
	var surname = $("#surname").val();
	var email = $("#email").val();
	var terms;
	if ($("#terms").is(":checked")) {
		terms = true;
	}else {
		terms = false;
	}

	
	var optIn;
	if ($("#optIn").is(":checked")) {
		optIn = true;
	}else {
		optIn = false;
	}
		
	$.ajax({
		type: "POST",
		url: "/ajax/competition2/",
		data: "sTitle="+sTitle+"&forename="+forename+"&surname="+surname+"&email="+email+"&terms="+terms+"&optIn="+optIn,
		async: false,
		success: function(msg) {
			$("#formArea").html(msg);
			$.fn.colorbox.resize();
		},
		error: function() { alert("error"); }
	});
	
}


function callRegister() {
	
	var sTitle = $("#sTitle").val();
	var forename = $("#forename").val();
	var surname = $("#surname").val();
	var address1 = $("#address1").val();
	var address2 = $("#address2").val();
	var town = $("#town").val();
	var county = $("#county").val();
	var postcode = $("#postcode").val();
	var gender = $("#gender").val();
	var dob_dd = $("#dob_dd").val();
	var dob_mm = $("#dob_mm").val();
	var dob_yy = $("#dob_yy").val();
	var shop= $("#shop").val();
	var email = $("#email").val();
	var password = $("#password").val();
	var password2 = $("#password2").val();
	var optin;
	if ($("#optin").is(":checked")) {
		optin = true;
	}else {
		optin = false;
	}

	$.ajax({
		type: "POST",
		url: "/ajax/register/",
		data: "sTitle="+sTitle+"&forename="+forename+"&surname="+surname+"&address1="+address1+"&address2="+address2+"&town="+town+"&county="+county+"&postcode="+postcode+"&gender="+gender+"&dob_mm="+dob_mm+"&dob_dd="+dob_dd+"&dob_yy="+dob_yy+"&shop="+shop+"&email="+email+"&password="+password+"&password2="+password2+"&optin="+optin,
		async: false,
		success: function(msg) {
			$("#formArea").html(msg);
			$.fn.colorbox.resize();
		},
		error: function() { alert("error"); }
	});
	
}

function callSignup() {
	
	var sTitle = $("#sTitle").val();
	var forename = $("#forename").val();
	var surname = $("#surname").val();
	var email = $("#email").val();
	var optin;
	if ($("#optin").is(":checked")) {
		optin = true;
	}else {
		optin = false;
	}
	

	$.ajax({
		type: "POST",
		url: "/ajax/signup/",
		data: "sTitle="+sTitle+"&forename="+forename+"&surname="+surname+"&email="+email+"&optin="+optin,
		async: false,
		success: function(msg) {
			$("#formArea").html(msg);
			$.fn.colorbox.resize();
		},
		error: function() { alert("error"); }
	});
	
}

function isValidEmailAddress(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);
}