$(function()
{

    $(".loading").ajaxStart(function(){
        $(this).show();      
    });

    $(".loading").ajaxStop(function(){
        $(this).hide(); 	     
    }); 
	
});

function toggle_cart(){
    $("#panel").slideToggle('clip');
}
function show_cart(){
    $("#panel").slideDown('clip');
}
function hide_cart(){
    $("#panel").slideUp('clip');
}


function change_qty(id,m_cart,select_id) {
  
    if(select_id==""){
        select_id=id;
    }
    //if(!check_page_loaded()){return}
    $.getJSON("../shop/ajax_server.php", {
        action 		: "change_qty",
        product 	: id,
        qty 		: document.getElementById(select_id).value,
        master_cart : m_cart
    }, function(json) { 
        $("#mini_cart,#master_cart").html(json.products);		
        $("#cart_subtotal,#ship_center_gray").show('clip',800);
        //$("#cart_total").show('clip');
		
        change_freight_and_total(json)
    });
}


$(function(){
    $("#pcode,#txtCCNumber").ForceNumericOnly();
})

function change_pcode(m_cart){
    $(function()
    {
    
        if($('#pcode').val().length<4 && $('input[name=pickup]:checked').val()!="Yes"){
            alert('Postcode Must be 4 digits');
            return;
        }

        $.getJSON("../shop/ajax_server.php", {
            action 		: "change_pcode",		
            pcode 		: $("#pcode").val(),
            pickup          : $('input[name=pickup]:checked').val(),
            master_cart : m_cart
        }, function(json) { 
            if(json.error){
                alert("Ooops incorrect postcode. Or we don't ship to this postcode. Please try a different postcode");
                return;
            }
            $("#mini_cart,#master_cart").html(json.products);
            $("#mini_cart,#master_cart").html(json.products);	
            
            $("#cart_subtotal,#ship_center_gray").show('clip',800);
        //$("#cart_total").show('clip');
		
           change_freight_and_total(json)
        });
    });
        
}

function apply_promo(m_cart){
    $(function()
    {
        $.getJSON("../shop/ajax_server.php", {
            action 		: "apply_promo",		
            code 		: $("#promo").val(),            
            master_cart : m_cart
        }, function(json) { 
            if(json.error){
                alert("Ooops! wrong Discount code");
                return;
            }
            $("#mini_cart,#master_cart").html(json.products);
            $("#mini_cart,#master_cart").html(json.products);	
            
            $("#cart_subtotal,#ship_center_gray").show('clip',800);
        //$("#cart_total").show('clip');
		
           change_freight_and_total(json)
        });
    });
        
}

function change_discount(m_cart){	

    if(!check_page_loaded()){
        return
    }
    $.getJSON("../shop/ajax_server.php", {
        action 			: "change_discount",
        discount 		: $("#discount").val(),
        freeShipping 	: $("#free_shipping").val(),
        master_cart 	: m_cart
    }, function(json) { 
        if(json.error){
            alert("Ooops incorrect postcode. Or we don't ship to this postcode. Please try a different postcode");
            return;
        }
        $("#mini_cart,#master_cart").html(json.products);		
        $("#cart_subtotal,#ship_center_gray").show('clip',800);
    //$("#cart_total").show('clip');
		
    //change_freight_and_total(json)
    });
}

function delete_product(id,m_cart) {
    if(!check_page_loaded()){
        return
    }
    $.getJSON("../shop/ajax_server.php", {
        action 		: "delete_product",
        id 			: id,
        master_cart : m_cart
    }, function(json) {
		
        $("#mini_cart,#master_cart").html(json.products);	
        change_freight_and_total(json)
        $("#cart_subtotal,#ship_center_gray").show('clip',800);
    });
    // stop the cart from jumping to the top
    return false;
}

function check_page_loaded(){
    return true;
    if(jq==true && jqui==true){      
        return true;
    }
    else{
       
        alert('One moment please. Page is still loading...');
        return false;
    }
}

$(document).ready(function() {
    $('#address1 input').StopSubmitOnEnter()

});

jQuery.fn.StopSubmitOnEnter =
    function(){	
        $(this).keydown(function(event){
            if(event.keyCode == 13) {
                event.preventDefault();
                return false;
            }
        });
    }

jQuery.fn.ForceNumericOnly =
    function(){	
        $(this).keydown(function(event) {
            // Allow only backspace and delete
            if (event.keyCode == 46 || event.keyCode == 8  || event.keyCode == 9) {
            // let it happen, don't do anything
            }
            else {
                // Ensure that it is a number and stop the keypress
                if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) {

                }
                else {
                    event.preventDefault();
                }
            }

        });	
    }


function load_sub_options(opt){
    $("#p_option").val(opt.value);
    display_price(opt);
    return
    var parent=$("#"+opt.id+" option:selected").attr("value")
    $.post("../shop/ajax_server.php", {
        action: "loadSubOptions",
        parentId:parent
    },
    function(data){			    
        $("#sub_options").html(data).show('slide');	
        $("#sub_sub_options").hide('clip');	
						   
    });
}


function hide_errors(){
    $("label.error").hide();
}


function display_price(opt){	
    if ($("#" + opt.id + " option:selected").attr("rel") > 0) {
        $("#price_display").html("$"+$("#" + opt.id + " option:selected").attr("rel")).show('clip');
    }
}

function change_freight_and_total(json) {
	
    //$("#shipping_cost").html("$" + json.freight);
    //$("#insurance_cost").html("$" + json.insurance);
    $("#total_cost").html("" + json.total);
    //$("#cc_total").html("" + json.total);
    $("#pcount").html(json.pcount);
   
    $('.inner_div2 .box_grey,.table_mid').fadeTo('slow', 0.5, function() {
      $(this).fadeTo('slow', 1, function() {});
    });
    
//$("#error").slideUp()
	
	

}

cf=true;

/*var page_loaded=false


function load_sub_options(opt){		
    var parent=$("#"+opt.id+" option:selected").attr("value")
	 $.post("../shop/ajax_server.php", { action: "loadSubOptions",parentId:parent},
			  function(data){			    
			    $("#sub_options").html(data).show('slide');	
				$("#sub_sub_options").hide('clip');	
				display_price(opt);		   
    });
}

function load_sub_sub_options(opt){ 	
   var parent=$("#"+opt.id+" option:selected").attr("value")
	$.post("../shop/ajax_server.php", { action: "loadSubSubOptions",parentId:parent},
   function(data){$("#sub_sub_options").html(data).show('slide');});
	display_price(opt);
}

function display_price(opt){	
	if ($("#" + opt.id + " option:selected").attr("rel") > 0) {
		$("#price_display").html("$"+$("#" + opt.id + " option:selected").attr("rel")).show('clip');
	}
}


function set_page_loaded(){
	page_loaded=true
}



function change_country(country) {
	if(!check_page_loaded()){return}
	$.getJSON("../shop/ajax_server.php", {
		action : "change_country",
		country : country
	}, function(json) {
		$(".flag").html(json.flag ).show('blind');	
		
		$.ajax({
			  url: "../shop/ajax_server.php?action=load_state",
			  cache: false,
			  success: function(html){$(".state_container").html(html);}
			});
	
		// $("#country_container_mini").html(json.country)
		$('.country_lable_parent .country_lable').html(country).show('highlight', 1000)
		change_freight_and_total(json)
		toggle_methord_form()
		$('.country_list').val(country)
	});
}

function change_shipping_mode() {
	if(!check_page_loaded()){return}
	$.getJSON("../shop/ajax_server.php", {
		action : "change_shipping_mode",
		mode : $("[name=mode]:checked").val()
	}, function(json) {
		change_freight_and_total(json)
	});

}


function remove_product(id) {
	if(!check_page_loaded()){return}
	$.getJSON("../shop/ajax_server.php", {
		action : "remove_product",
		id : id
	}, function(json) {
		$("#cart_details_html").html(json.products).show('clip');
		change_freight_and_total(json)
	});
	// stop the cart from jumping to the top
	return false;
}



function undo_delete(id) {
	if(!check_page_loaded()){return}
	$.getJSON("../shop/ajax_server.php", {
		action : "undo_delete",
		id : id
	}, function(json) {
		$("#cart_details_html").html(json.products).show('clip');
		change_freight_and_total(json)
	});
	// stop the cart from jumping to the top
	return false;
}

function change_freight_and_total(json) {
	
	$("#shipping_cost").html("$" + json.freight);
	$("#insurance_cost").html("$" + json.insurance);
	$("#total_cost").html("$" + json.total);
	$("#cc_total").html("AU$" + json.total);
	$("#pcount").html(json.pcount);
	$("#error").slideUp()
	
	

}

function toggle_methord_form() {
	if ($('#country_list').val() == "AUSTRALIA") {
		$('#method_form').hide('slide', 200)
	} else {
		if (!$('#method_form').is(':visible')) {
			$('#method_form').show('slide', 200)
		}
	}

}

function toggle_cart() {
	if ($('#collapsed').is(':visible')) {
		$('#collapsed').hide('blind', 400)
		// $('#toggle_cart').html("Show")

	} else {
		$('#collapsed').show('blind', 300)
		// $('#toggle_cart').html("Hide")
	}
}
function show_cart() {
	if ($('#collapsed').is(':hidden')) {
		$('#collapsed').show('blind', 400)
	}
}
function hide_cart() {
	setTimeout ( "hide_cart_now()", 350 );

	
}
function hide_cart_now(){
	if ($('#collapsed').is(':visible')) {
		$('#collapsed').hide('blind', 300)
	}
}

function float_loading(){return
    var name = ".loading";
    var menuYloc = null;

    menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")))
    $(window).scroll(function () {
        	var offset = menuYloc+$(document).scrollTop()+"px";
            $(name).animate({top:offset},{duration:500,queue:false});
    });
}

function  is_cart_empty(){
	//if(!check_page_loaded()){return false}
	if(($('#pcount').html())==0){alert('Ooops! your cart is empty \nPlaease add a product first');return false;}
	return true;
	
}

function print_div(strid)
{
var prtContent = document.getElementById(strid);
var WinPrint =window.open('','','left=0,top=0,width=800,height=900,toolbar=1,scrollbars=1,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
}

$(function(){
	
	$(".loading_modal").dialog({    	
		modal: true,
		dialogClass: 'loading_modal_class'
		
	});
	$(".loading_modal").dialog().parents(".ui-dialog").find(".ui-dialog-titlebar").remove(); 
	$(".loading_modal").dialog('close');
	
	$(".loading").slideUp(); 
	float_loading();
	// close cart when clicked outside
	  $(document).bind('click', function(e) {
                var $clicked=$(e.target); // get the element clicked
                if( ! ( $clicked.is('.cart_m') || $clicked.is('div#catr_box') || $clicked.is('div#cart_box') || $clicked.parents().is('.cart_m') || $clicked.parents().is('div#catr_box') || $clicked.parents().is('#converter_form,.ui-dialog') ||  $clicked.is('#cart_box a,#cart_box a span,#cart_box a img') ) )
                {
                	hide_cart_now();
                }

      });
	  
     $(".loading_modal").ajaxStart(function(){
     $(this).show();       
     $(".loading_modal").dialog('open')     
     });

     $(".loading_modal").ajaxStop(function(){
     $(this).slideUp(); 
     $(".loading_modal").dialog('close')
     });    
     
     $("div#catr_box").bind("click",function(){
    	 toggle_cart()
     });
     $("#cart_box").bind("click",function(){
    	 toggle_cart()
     });
      
});

*/

