function ChangeSelectedMenu()
{
	var url = location.href;
	$("#menu li > a").each(function(){
		
		link = new RegExp('\\'+$(this).attr("href")+'$');
		
		if(url.match(link))
		{

			$(this).parent("li").addClass("selected");
			$(this).parent("li").parent("ul").children("li").show();
			$(this).parent("li").children("ul").show();
		}else{
			$(this).parent("li").removeClass("selected");
		}

	});
}
$(function(){
	ChangeSelectedMenu();


	$("#menu a").click(function () {
		$(this).delay(500,function(){
			ChangeSelectedMenu();
		});
	});
	$('a.modal1').modal(); 
	var close=function(hash) {$("#jp").hide();$("#en").show();hash.w.hide();hash.o.remove();};
	$('.jqmWindow').jqm({onHide:close});
	$("#jp_btn").click(function(){
		$("#en").hide();
		$("#jp").show();
	});
	$("img[alt='Japanese']").click(function(){
		$(".jqmWindow").jqm().jqmShow();
		$("#en").hide();
		$("#jp").show();
	});
	$(".jqmWindow").jqmAddClose($("#close_btn"));
	if(screen.availHeight <= 800){$(".jqmWindow").height(600).css('overflow','scroll').css('top','0');}
});
function qtyCheck(color_id, stock)
{
	input_value = $("#fm"+color_id+" input[name='qty']").val();
	if ( !input_value.match(/^[1-9]{1}$|^[1-9]{1}[0-9]{1}$/) )
	{
		if(($("#fm"+color_id+" input[name='qty']").next("span").html()==" in basket.")&&(input_value=='0')){;return true;}
        	$("#fm"+color_id+" input[name='qty']").parent().next("div").html("incorrect input");
		$("#fm"+color_id+" input[name='qty']").parent().next("div").css("color","red");
        	return false;
	}
	if ( input_value > stock )
	{
        	$("#fm"+color_id+" input[name='qty']").parent().next("div").html("Sorry, current stock is "+stock);
		$("#fm"+color_id+" input[name='qty']").parent().next("div").css("color","red");
		return false;
	}
}
//address form
$(function(){
    $("#country2").hide();
	$("select[name='Country']").change(function(){
		if($(this).val() == 'Other')
		{
			$("#country2").show();
			$("select[name='Delivery']").children("option[value='2']").removeAttr('selected');
			$("select[name='Delivery']").children("option[value='3']").attr('selected','selected');
		}
		else
		{
			$("#country2").hide();
			$("select[name='Delivery']").children("option[value='3']").removeAttr('selected');
			$("select[name='Delivery']").children("option[value='2']").attr('selected','selected');
		}
		ChangeShipping();
	});
	$("select[name='Delivery']").change(function(){
	    ChangeShipping();
	});	
	$("input[name='send']").click(function(){
	    return CheckForm();
	});
});
function ChangeShipping()
{
    var tariff = new Array(3) 
    tariff[1] = 0; //pickup
    tariff[2] = 6; //New Zealand wide
    // tariff[2] = 0; //New Zealand wide Campaing
    tariff[3] = -1; //overseas
    var shippingDoller = tariff[$("select[name='Delivery']").val()].toFixed(2);
    var subtotal = Number($("#subtotal").html());
    var shippingmessage = "TBA";
    var totalmessage = "TBA";
    if(shippingDoller >= 0)
    {
        shippingmessage = shippingDoller;
        totalmessage = (subtotal + Number(shippingDoller)).toFixed(2);
    }
    $("#shipping").html(shippingmessage);
    $("#total").html(totalmessage);
}
function CheckForm()
{
return(
	(
	 (
 	  (
 	   (
	    (
	     (
	      BlankCheck($("input[name='FirstName']")) 
	      && BlankCheck($("input[name='LastName']")) 
	     )
	     && BlankCheck($("input[name='Street']")) 
	    ) 
	    && BlankCheck($("input[name='Town/City']"))
	   )
	   && BlankCheck($("input[name='Email']"))
	  )  
	  && (($("select[name='Delivery']").val() != 1)||(BlankCheck($("textarea[name='Comment']"))))
	 ) 
	 && BlankCheck($("input[name='Country2']"))
	)
      ) ;
}
function BlankCheck(field)
{
    if( $(field).is(':visible') ){
     $(field).next("div").remove();
     if ( ($(field).val()).length  == 0 )
     {
        $(field).after("<div class='error'>Please fill in.</div>");
        $(field).css("border-color","#FF0000");
	$(field)[0].focus();
        return false;
     }
     else
     {
        return true;
     }  
    }else{return true;}
}   
function addstring() 
{

      $("#searchinput").val('site:www.zucker.co.nz ' + $("#searchinput").val()) ;
     return true;
}
function CheckEmailForm()
{
	return(  ((BlankCheck($("input[name='sSender']")) 
	&& BlankCheck($("input[name='sTopic']"))) 
	&& BlankCheck($("textarea[name='sMailContent']")))  ) ;
}
function changeComment()
{
	if($("select[name='Delivery']").val() == 1)
	{
		$("#commenttitle").html("When would you like to come?").css('color','red').css('font-weight','bold');
		$("textarea[name='Comment']")[0].focus();
	}
	else
	{
		$("#commenttitle").html("Comment on Order :").css('color','black').css('font-weight','normal');
		$("textarea[name='Comment']").next("div").remove()
	}
}

