// JavaScript Document
function ajaxSetup()
{
    var xmlHttp;
    try
    {  
        // Firefox, Opera 8.0+, Safari
	    xmlHttp = new XMLHttpRequest();  
    }
    catch (e)
    {  
        // Internet Explorer  
	    try{    
		    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");    
	    }
	    catch (e){    
		    try{      
  			    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  		    }
		    catch (e){
  			    alert("Your browser does not support AJAX!");
  			    return false;
  		    }
	    }
    }
    return xmlHttp;
}

/* ======================== Shopping Basket ============================ */

function addToBasket(itemid){
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
			document.getElementById("basket").innerHTML = xmlHttp.responseText;
			alert("This item has been added to your Shopping Bag");
		}
	}
	xmlHttp.open("GET","../ajax/addbasket.php?itemid=" + itemid + "&basketid=" + document.getElementById("basketid").value, true);
	xmlHttp.send(null);
}

function removeFromBasket(itemid){
	if (confirm("Are you sure that you want to remove this item?"))
	{
		var xmlHttp = ajaxSetup();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState == 4)   // 4 = Completed
			{
				var response = String(xmlHttp.responseText);
				document.getElementById("basket_holder").innerHTML = response.substr(response.indexOf("@@@") + 3); 
				document.getElementById("basket").innerHTML = response.substr(0, response.indexOf("@@@")); 
			}
		}
		xmlHttp.open("GET","../ajax/basket.php?type=remove&id=" + itemid, true);
		xmlHttp.send(null);
	}
}


function submitForm(formName, navigateValue) {
	if (navigateValue != null && navigateValue != "") {
		document.forms[formName].navigate.value = navigateValue;
	}
    document.forms[formName].submit();
}

function sageformProceed(){
		document.getElementById('SagePayForm').submit();
}

function deliveryOption(price){
	var items = parseInt(document.getElementById("itemcount").value) + 1;
	var giftcost = 0;
	document.getElementById("deliveryTotal").value = price;
	if ($("#ribbon").attr('checked')){
		$("#paypallink").attr("href", "https://www.paypal.com/cgi-bin/webscr?" + document.getElementById("basicquerystring").value + "item_name_" + items + "=Gift Box and Ribbon&amount_" + items + "=2.95&quantity_" + items + "=1&handling_cart=" + document.getElementById("deliveryTotal").value);
		giftcost = 2.95;
	}
	else{
		$("#paypallink").attr("href", "https://www.paypal.com/cgi-bin/webscr?" + document.getElementById("basicquerystring").value + "handling_cart=" + document.getElementById("deliveryTotal").value);
	}
	document.getElementById("deliveryspan").innerHTML = price;
	document.getElementById("totalspan").innerHTML = (parseFloat(price) + parseFloat(document.getElementById("costTotal").value) + giftcost - parseFloat(document.getElementById("discountTotal").value)).toFixed(2);
	document.getElementById("totalspan2").innerHTML = (parseFloat(price) + parseFloat(document.getElementById("costTotal").value) + giftcost - parseFloat(document.getElementById("discountTotal").value) - parseFloat(document.getElementById("boxdiscountTotal").value)).toFixed(2);
	
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
		}
	}
	xmlHttp.open("GET","../ajax/delivery.php?delivery=" + price + "&basketid=" + document.getElementById("basketid").value, true);
	xmlHttp.send(null);
	
}
function ribbon(){
	var items = parseInt(document.getElementById("itemcount").value) + 1;
	var giftbox = 0;
	if ($("#ribbon").attr('checked')){
		$("#paypallink").attr("href", "https://www.paypal.com/cgi-bin/webscr?" + document.getElementById("basicquerystring").value + "item_name_" + items + "=Gift Box and Ribbon&amount_" + items + "=2.95&quantity_" + items + "=1&handling_cart=" + document.getElementById("deliveryTotal").value);
		$("#box_details").html('<div class="basket_row"><div class="basket_item">Gift Box and Ribbon</div><div class="basket_qty">1</div><div class="basket_price">&pound;2.95</div></div>');
		
		$(".total2").show();
		$(".total1").hide();
		//document.getElementById("totalspan").innerHTML = parseFloat(document.getElementById("totalspan").innerHTML) + 2.95;
		giftbox = 1;
	}
	else{
		$("#paypallink").attr("href", "https://www.paypal.com/cgi-bin/webscr?" + document.getElementById("basicquerystring").value + "handling_cart=" + document.getElementById("deliveryTotal").value);
		$("#box_details").html('');
		
		$(".total2").hide();
		$(".total1").show();
		//document.getElementById("totalspan").innerHTML = parseFloat(document.getElementById("totalspan").innerHTML) - 2.95;
	}
	deliveryOption(document.getElementById("deliveryTotal").value);
	
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
		}
	}
	xmlHttp.open("GET","../ajax/delivery.php?giftbox=" + giftbox + "&basketid=" + document.getElementById("basketid").value, true);
	xmlHttp.send(null);
	
}

function message(){
	if ($("#messagecb").attr('checked')){
		$("#message").animate({'opacity':1.0},500);
	}
	else{
		$("#message").animate({'opacity':0.0},500);
		$("#message").html('');
		savemessage();
	}
}
function savemessage(){
	var message = document.getElementById("message").value;
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
		}
	}
	xmlHttp.open("GET","../ajax/delivery.php?message=" + message + "&basketid=" + document.getElementById("basketid").value, true);
	xmlHttp.send(null);
	
}



/* drop down menu */
$(document).ready(function(){
	$('.productsubmenu').css({opacity: 0.0});
	
	$('.menu_block').hover(
		function(){
			$(":first-child", this).next().stop().show().animate({opacity:1.0}, 500);		
			$(this).addClass("menu_over");
		},
		function(){
			$(":first-child", this).next().stop().animate({opacity:0.0}, 500).hide();	
			$(this).removeClass("menu_over");
		}
	);
	
	$("#emailip").focus(function(){
		if ($(this).val() == "Enter Email Address"){
			$(this).val("");
		}
	});
	$("#emailip").blur(function(){
		if ($(this).val() == ""){
			$(this).val("Enter Email Address");
		}
	});
	
	$(".type_link, .type_image, .product_link, .product_image").hover(
		function(){
			$(this).parent().addClass("product_over");	
		},
		function(){
			$(this).parent().removeClass("product_over");	
		}
								   
	);
	$(".type_link_offers").hover(
		function(){
			$(this).parent().addClass("product_over_offers");	
		},
		function(){
			$(this).parent().removeClass("product_over_offers");	
		}
								   
	);
	
});	



/* newsletter */

function signup(){
	var emailip = document.getElementById("emailip").value;
	var nameip = document.getElementById("nameip").value;
	
	if (nameip == ""){
		alert("Please input your full name");	
	}
	else if (emailip.indexOf("@") < 0){
		alert("Please input an appropriate email address");	
	}
	else{
		
		var xmlHttp = ajaxSetup();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState == 4)   // 4 = Completed
			{
				document.getElementById("signupbar_holder").innerHTML = xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET","../ajax/newsletter.php?name=" + nameip + "&email=" + emailip, true);
		xmlHttp.send(null);
	}
	
}
function signup2(){
	var emailip = document.getElementById("email").value;
	var nameip = document.getElementById("fullname").value;
	
	if (nameip == ""){
		alert("Please input your full name");	
	}
	else if (emailip.indexOf("@") < 0){
		alert("Please input an appropriate email address");	
	}
	else{
		
		var xmlHttp = ajaxSetup();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState == 4)   // 4 = Completed
			{
				document.getElementById("signup_holder").innerHTML = xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET","../ajax/newsletter.php?name=" + nameip + "&email=" + emailip, true);
		xmlHttp.send(null);
	}
	
}



/* ========================== newsletter signup ============================== */

function nameInput(action, input){
	var name = document.getElementById(input);
	if (action == "click"){
		if (name.value == "Full Name"){
			name.value = "";
		}
	}
	if (action == "blur"){
		if (name.value == ""){
			name.value = "Full Name";	
		}
	}
}
function emailInput(action, input){
	var name = document.getElementById(input);
	if (action == "click"){
		if (name.value == "Email Address"){
			name.value = "";
		}
	}
	if (action == "blur"){
		if (name.value == ""){
			name.value = "Email Address";	
		}
	}
}



function submitcode(){
	var code = document.getElementById("code").value;
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
			document.getElementById("costfooter").innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET","../ajax/discount.php?code=" + code, true);
	xmlHttp.send(null);
	
}

