addtocartdisable = 0;
$(document).ready(function(){
	showCartInfomation();
});

function showTimer(){
  if($("#timer").length>0){
      s = $("#timer").val();
      if(s<0)
         s=1;
      $("#defaultCountdown").countdown({until: '+'+s+'s',
                                              layout:"Checkout Time: {dn} D "+"{hn}:{mn}:{sn}",
                                              onExpiry: lifeOff
                });
  }
}
function lifeOff(){
  alert("Your cart timeout.");
  var url = base2 + "shop/cart/checkoutTimeOut";
  $.ajax({
    type: "POST",
    url: url,
    success: function(resp){
      if(resp=='')
        window.location = window.location; //url
      else
        window.location = resp; //url
    },
    error: function(a,b,c){
      window.location = window.location;
    }
  });
}

function popOutCart(){
}
function setVisible(obj){
	obj = document.getElementById(obj);
	obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible';
}
function showMiniCart(obj, standalonecart ){
	if(standalonecart == null){
		standalonecart = 0;
	}
	obj = document.getElementById(obj);
	if ($('#floater').attr("id") != undefined) { 
		$('#loader').removeClass('ajaxLoad');
	}
	if ($('#floater2').attr("id") != undefined) { 
		$('#loader2').removeClass('ajaxLoad');
	}
	
	if ($('#miniCartFloater').attr("id") != undefined) { 
		$('#minCartLoader').removeClass('ajaxLoad');
	}
	
	obj.style.visibility = 'visible';
	
}
function addToMiniCart(form, standalonecart, productID){
  if(addtocartdisable){
    return;
  }
  addtocartdisable = 1;
  formObj = $('#'+form+ '_' + productID);
  $(formObj).find(".addtocartbtn")[0].disabled = true;
	var formInfo = $('#'+form+ '_' + productID).serialize();
  //alert(formInfo); return false;
	
	var url = 'shop/cart/add/';
	
	if ($('#floater').attr("id") != undefined) {
		$('#floater').html(' ');
		$('#loader').addClass('ajaxLoad');
		
		$.ajax({
			type: "POST",
			url: url,
			data: formInfo,
			success: function(resp){
        if(resp != "") alert(resp);
				$('#loader').removeClass('ajaxLoad');
        if($("#refreshpage").length>0){ //only for timebased work shopping.
          window.location = window.location;
        }else{
          showCartInfomation(standalonecart);
          if( standalonecart != '0' ) { // Need to redirect to a cart page
            window.location = base2 + 'cart';
          }
          addtocartdisable = 0;
          $(formObj).find(".addtocartbtn")[0].disabled = false;
        }
			}
		});
		
	}
	if ($('#floater2').attr("id") != undefined) {
		$('#floater2').html(' ');
		$('#loader2').addClass('ajaxLoad');
		$.ajax({
			type: "POST",
			url: url,
			data: formInfo,
			success: function(resp){
        if(resp != "") alert(resp);
				$('#loader2').removeClass('ajaxLoad');
          showCartInfomation(standalonecart);
          if( standalonecart != '0' ) { // Need to redirect to a cart page
            window.location = base2 + 'cart';
          }
          addtocartdisable = 0;
          $(formObj).find(".addtocartbtn")[0].disabled = false;
			}
		});
	}
	if ($('#miniCartFloater').attr("id") != undefined) {
		$('#miniCartFloater').html(' ');
		$('#minCartLoader').addClass('ajaxLoad');
		$.ajax({
			type: "POST",
			url: url,
			data: formInfo,
			success: function(resp){
        if(resp != "") alert(resp);
				$('#minCartLoader').removeClass('ajaxLoad');
          showCartInfomation(standalonecart);
          if( standalonecart != '0' ) { // Need to redirect to a cart page
            window.location = base2 + 'cart';
          }
          addtocartdisable = 0;
          $(formObj).find(".addtocartbtn")[0].disabled = false;
			}
		});
	}
}
//Shows all cart info whren person browses to another page
function showCartInfomation(standalonecart){
	var arg1 = 1;
	if(pageurl.replace('cart','') == base2){arg1 = 0;} // This is to help not show the view full cart button
	
	var arg2 = 1;
	if( here2 == base2 + 'shop/checkout/contactinfomation' ){arg2 = 0;}
	              
	
	if( standalonecart == null){
		standalonecart = 0;
	}
	
	if ($('#deliveryID').attr("id") != undefined) { 
		var deliveryID = $('#deliveryID').val();
	}else{
		var deliveryID = 0;
	}
	
	if ($('#shippingHolder').attr("id") != undefined) { 
		var country = $('#shippingHolder').val();
	}else{
		var country = 0;
	}
	
	
	//Full cart
	if ($('#floater').attr("id") != undefined) {
		var url = base2 + 'shop/cart/view/'+ arg1 + '/' + arg2	 ; 
		$.ajax({
			type: "POST",
			url: url,
      async:   false,
			data: 'deliveryID=' + deliveryID + '&countryID=' + country,
			success: function(resp){
				$('#loader').removeClass('ajaxLoad');
				document.getElementById('floater').innerHTML = resp;
				showMiniCart('floater', "'"+ standalonecart +"'");
				updateOrderTotalOnContactInfomation();
        showTimer();
			}
		});
	}
	//Mini Cart
	if ($('#floater2').attr("id") != undefined) { 
		var url = base2 + 'shop/cart/viewCompact/' + arg1	 + '/' + arg2	 ; 
		
		$.ajax({
			type: "POST",
			url: url,
      async:   false,
			data: 'deliveryID=' + deliveryID + '&countryID=' + country,
			success: function(resp){
				$('#loader2').removeClass('ajaxLoad');
				document.getElementById('floater2').innerHTML = resp;
				showMiniCart('floater2', "'"+ standalonecart +"'");
				updateOrderTotalOnContactInfomation();
        showTimer();
			}
		});
	}
	//Micro Cart
	if ($('#miniCartFloater').attr("id") != undefined) { 
		var url = base2 + 'shop/cart/viewMicro/' + arg1	 + '/' + arg2	 ; 
		
		$.ajax({
			type: "POST",
			url: url,
      async:   false,
			data: 'deliveryID=' + deliveryID + '&countryID=' + country,
			success: function(resp){
				$('#loader2').removeClass('ajaxLoad');
				document.getElementById('miniCartFloater').innerHTML = resp;
				showMiniCart('miniCartFloater', "'"+ standalonecart +"'");
				updateOrderTotalOnContactInfomation();
        showTimer();
			}
		});
	}
	
	
	if ($('#orderTotal').attr("id") != undefined) {
		$.ajax({
			type: "POST",
			url: base2 + 'search/ajax2/getShopTotal',
			data: 'type=grand&format=true',
			success: function(resp){
				$('#orderTotal')[0].innerHTML = resp; 
			}
		});
	}

}


function drawLoader(){
	if ($('#floater').attr("id") != undefined) { 
		$('#floater').html( ' ' );
		$('#loader').addClass('ajaxLoad');
	}
	if ($('#floater2').attr("id") != undefined) { 
		$('#floater2').html( ' ' );
		$('#loader2').addClass('ajaxLoad');
	}
	
	if ($('#miniCartFloater').attr("id") != undefined) { 
		$('#miniCartFloater').html( ' ' );
		$('#minCartLoader').addClass('ajaxLoad');
	}
}



function removeFromMiniCart(id){
	var url = base2 + 'shop/cart/remove/'	+ id ; 
	
	drawLoader();
	$.ajax({
		type: "GET",
		url: url,
		success: function(msg){
      if(msg=='refreshPage'){
        window.location = window.location;
      }else if(msg.substring(0,5)=='http:'){
        window.location = msg;
      }else{
        showCartInfomation();
      }
		}
	});
}
function emptyCart(obj){
	var url = base2 + 'shop/cart/empty/';

	drawLoader();
	$.ajax({
		type: "GET",
		url: url,
		success: function(msg){
      if(msg=='refreshPage'){
        window.location = window.location;
      }else if(msg.substring(0,5)=='http:'){
        window.location = msg;
      }else{
        showCartInfomation();
      }
		}
	});
}
function updateCartQty(id, index) {
	var qty = document.getElementById('qty['+id+']').value;	//qty that user has updated line item to
	
	//make sure qty is numeric
	if(!IsNumeric(qty)) {qty = 1;}
	
	
	var url = base2 + 'shop/cart/updateQty/';
	
	$.ajax({
		type: "POST",
		url: url,
		data: 'index=' + index + '&qty=' + qty,
		success: function(msg){
      if(msg != "")
        alert(msg);
			showCartInfomation();
		}
	});
}
function updateOrderTotalOnContactInfomation(){
	if ($('#deliveryID').attr("id") != undefined) { 
		var deliveryID = $('#deliveryID').val();
	}else{
		var deliveryID = 0;
	}
	
	if ($('#shippingHolder').attr("id") != undefined) { 
		var country = $('#shippingHolder').val();
	}else{
		var country = 0;
	}
	
}
function autocompleteCallbackSave(){
  $("#saveClient").addClass("loader");
  $.ajax({
    type: "POST",
    url: base2 + "search/ajax2/cartQuotee",
    data: "quoteeID=" + $("#quoteeID-1").val(),
    success: function(resp){
      $("#autocompleteClient").hide();
      $("#saveClient").removeClass("loader");
      $("#saveClient").hide();
      $("#changeClient").show();
      $("#quoteForClient").html(resp);
      $("#quoteForClient").show();
    }
  });
}
function changeClient(){
  $("#autocompleteClient").show();
  $("#saveClient").show();
  $("#changeClient").hide();
  $("#quoteForClient").html('<h3>Quote For: </h3>');
}
function goTocheckOut(obj,url,msg){
	if($("#quoteeID").length > 0 ){ //node isset then check it should not be nill.
		if($("#quoteeID-1").val()=="" || $("#changeClient").css('display')=='none'){
			$("#quoteeID").focus();
      if($("#saveClient").css("display")!='none' && $("#quoteeID-1").val()!=""){
        alert("Please save Quote For");
      }else{
        alert("Please select a client and press save");
      }
			return;
		}
	}
	if(msg==1){
    $("#dialog-confirm").dialog("destroy");

    $("#dialog-confirm").dialog({
      resizable: false,
      modal: true,
      buttons: {
        'Ok': function() {
          $(this).dialog('close');
          window.location.href = url;
        },
        Cancel: function() {
          $(this).dialog('close');
        }
      }
    });
  }else if(msg==0){
    $("#dialog-confirm").dialog("destroy");

    $("#dialog-confirm").dialog({
      resizable: false,
      modal: true,
      buttons: {
        'Ok': function() {
          $(this).dialog('close');
          window.location.href = url;
        },
        Cancel: function() {
          $(this).dialog('close');
        }
      }
    });
  }else{
     window.location.href = url;
  }
}

function proceedToPaymentConfirm(obj){
  $("#dialog-proceedToPayment-confirm").dialog("destroy");

  $("#dialog-proceedToPayment-confirm").dialog({
    resizable: false,
    modal: true,
    buttons: {
      'Ok': function() {
        $(this).dialog('close');
        $("#proceedToPayment2").attr('name','proceedToPayment');
        if(chkVoucher($("#editProp")[0])){
          $("#editProp").submit();
        }
      },
      Cancel: function() {
        $(this).dialog('close');
        return false;
      }
    }
  });
}

