// JavaScript Document
var is_product_added=false;
var add_option_after_product=false;
var option_ele;
var selected_option;
function add_fabrics()
{
	if($('#quantity').val()=="0")
	{
		alert("Please select product quantity");
		return false;
	}
		if($('.color_cha').val()=="cha")
	{
		if($('#fabric_colour_id').val()=="")
		return false;
	}
	
	
	if(!is_option_selected())
	  {
		/* alert("Please select one of the options."); 
		 var top=$("#product_option_choices").offset().top;
		 window.scrollTo(0,top);*/
		 return false;
	  }
	
	$('#order_summary').html('<img src="'+sitepath+'images/loading.gif" />');
	is_product_added=true;
	$.post(sitepath+"ajax/add_fabrics.php", {product_id: $('#product_id').val(), quantity: $('#quantity').val(), fabric_colour_id: $('#fabric_colour_id').val(),mode:"add" },
  	function(data){
	
	$('#order_summary').hide();
    $('#order_summary').html(data);$('#order_summary').fadeIn(1500);
	if(add_option_after_product)
	 {
		 add_option_after_product=false;
		 option_to_cart(option_ele,selected_option);
	 }
	
  });

}

function change_fabric(colour_id)
{
	$('#fabric_colour_id').val(colour_id);
	 add_fabrics();
}

function is_option_selected()
   {
	   if($(".fb_option_colour_id").size()==0)
	   return true;
	   
	   var is_option_selected=false;
	 $(".fb_option_colour_id").each(function(i){
			if($(this).attr("checked"))
			{
			is_option_selected=true;
			}
	   }); 
	 
	 return is_option_selected;
  }


function add_product_option(ele,option_id)
 {
	 if($('#quantity').val()=="0")
	{
		alert("Please select product quantity.");
		$('#quantity').focus();
		$(ele).attr("checked",false);
		return false;
	}
	
	 if($('#fabric_colour_id').val()=="")
	   {
		 alert("Please select product fabrics."); 
		  $(ele).attr("checked",false);
		  return false;
		  
	   }
	   
	   if(!is_product_added)
	   {
	   $('#order_option_summary_'+option_id).html('<img src="'+sitepath+'images/loading.gif" />');
		 add_option_after_product=true; 
		 option_ele=ele;
		 selected_option=option_id;
	     add_fabrics();
	   }
	   else
	   {
		  option_to_cart(ele,option_id);
	   }
   	 
 }
 
 function validate_on_checkout()
   {
	 
			if($('#quantity').val()=="0")
			{
				alert("Please select product quantity.");
				$('#quantity').focus();
				return false;
			}
			if($('#fabric_colour_id').val()=="")
			{
				alert("Please select product fabrics."); 
				return false;
			}
			if(!is_option_selected())
			 {
				 alert("Please select product option."); 
				return false;
			 }
		
		 return true;
   }
 
 
 function option_to_cart(ele,option_id)
  {
  $('#order_option_summary_'+option_id).html('<img src="'+sitepath+'images/loading.gif" />');
$.post(sitepath+"ajax/add_fabric_option.php", {product_id: $('#product_id').val(),option_id:option_id,option_colour_id:ele.value,mode:"add"},
  	function(data){
	 $('#order_option_summary_'+option_id).hide();
     $('#order_option_summary_'+option_id).html(data);$('#order_option_summary_'+option_id).fadeIn(1500);
    }); 
  }

 
 
 function remove_product_option(cart_id,product_id,option_id)
  {
	   if(confirm("Selected option will be removed. Proceed?"))
	  {
		    
		$('#order_option_summary_'+option_id).html('<img src="'+sitepath+'images/loading.gif" />');
		$.post(sitepath+"ajax/add_fabric_option.php", {cart_id:cart_id,product_id:product_id,option_id:option_id,mode:"remove"},
		function(data){
		 $('#order_option_summary_'+option_id).hide();
		 $('#order_option_summary_'+option_id).html(data);$('#order_option_summary_'+option_id).fadeIn(1500);
		 $('.option_colour_id_'+option_id).attr("checked", false);
  });
	
	  }
	  return false;
  }
 

function print_accessories(accessories_id,data)
{
	 $('#accessories_content_'+accessories_id).hide();
	 $('#accessories_content_'+accessories_id).html(data);
	 $('#accessories_content_'+accessories_id).fadeIn(1500);
}

function remove_accessories(accessories_id,sno)
{
	if(confirm("Accessories will be removed. Proceed?"))
	{
		$('#accessories_content_'+accessories_id).html('<img src="'+sitepath+'images/loading.gif" />');
		$.post(sitepath+"ajax/add_accessories.php", {sno:sno, mode:"remove"},
		function(data){
		print_accessories(accessories_id,data);
		return false;
		});
	}
}
function remove_product(sno,prod_id)
   {
   if(confirm("Product will be removed. Proceed?"))
	{
		 $('#order_summary').html('<img src="'+sitepath+'images/loading.gif" />');
		is_product_added=false;
		$.post(sitepath+"ajax/add_fabrics.php", { sno:sno, mode:"remove"},
		function(data){
			
		 $('#order_summary').hide();
	 	 $('#order_summary').html(data);
	     $('#order_summary').fadeIn(1500);
		
		 $('.fb_colour_id').attr("checked", false);
		  $('.fb_option_colour_id').attr("checked", false);
		 $('#fabric_colour_id').val("");
		  $('.order_option_summary').html("&nbsp;");
		
		return false;
		});
	}
  }
function change_acc_fabric(fabric_colour_id,accessories_id)
{	
	$('#hdn_fabric_colour_id'+accessories_id).val(fabric_colour_id);
}

function add_accessories(accessories_id)
{
acc_quantity=$('#acc_quantity'+accessories_id).val();
acc_fabric_colour_id=$('#hdn_fabric_colour_id'+accessories_id).val();
if(acc_quantity=="0")
	{
		alert("Please select Quantity!!");
		return false;
	}
if(acc_fabric_colour_id=="")
	{
		alert("Please select fabrics!!");
		return false;
	}
	 $('#accessories_content_'+accessories_id).html('<img src="'+sitepath+'images/loading.gif" />');
$.post(sitepath+"ajax/add_accessories.php", {mode:"add",accessories_id:accessories_id, acc_quantity:acc_quantity, acc_fabric_colour_id: acc_fabric_colour_id,product_id:$('#product_id').val(),browsed_type:$('#browsed_type').val()},
  	function(data){
    print_accessories(accessories_id,data);

  });
  return false;
}
function add_product_related(parent_product_id,product_related_id)
{
product_related_quantity=$('#product_related_quantity'+product_related_id).val();
product_related_fabric_colour_id=$('#hdn_product_related_fabric_colour_id'+product_related_id).val();

if(product_related_fabric_colour_id=="")
{
	alert("Please select fabrics!!");
	return false;
}
if(product_related_quantity=="0")
{
	alert("Please select quantity!!");
	return false;
}
 $('#product_related_content_'+product_related_id).html('<img src="'+sitepath+'images/loading.gif" />');
$.post(sitepath+"ajax/add_king_related.php", {mode:"add",product_related_id:product_related_id, product_related_quantity:product_related_quantity, product_related_fabric_colour_id: product_related_fabric_colour_id,product_id:parent_product_id},
  	function(data){
    print_product_related(product_related_id,data);
  });
  return false;
}

function print_product_related(product_related_id,data)
{

	 $('#product_related_content_'+product_related_id).hide();
	 
	 $('#product_related_content_'+product_related_id).html(data);
	 $('#product_related_content_'+product_related_id).fadeIn(1500);
}

function remove_product_related(sno,product_related_id)
{
	if(confirm("Product will be removed. Proceed?"))
	{  
	$('#product_related_content_'+product_related_id).html('<img src="'+sitepath+'images/loading.gif" />');
		$.post(sitepath+"ajax/add_king_related.php", {sno: sno,mode:"remove"},
		function(data){
		print_product_related(product_related_id,data);
	
		return false;
		});
	}
}


function change_product_related_fabric(product_related_fabric_id,product_related_id)
{	
	$('#hdn_product_related_fabric_colour_id'+product_related_id).val(product_related_fabric_id);
}

function show_acc(id,type)
{
	$("#wrapper").find(".accessories_div").each(function(i){
    if(i==id)
	{   
	 
	  $("#option"+i).hide();
	  $(this).show();
	}
	else
	 {
	   $("#option"+i).show();
	   $(this).hide();
	 }
  });
	
}
function calc_delivery(delivery_type)
  {
	  
	 if(delivery_type!="free") 
	    {
			 if(delivery_type!='nonmetro')
			 {
			 $('#postcode').attr('disabled', true);
			 $('#postcode').val('');
			 $('#source_port').attr('selectedIndex',0)
			 $('#source_port').attr('disabled', true);	
			// $('#postcode').removeClass("required");
			 }
			 else
			 {
				 if(!$('#delivery_method_2').attr('checked'))
				 return ;
			  if($('#postcode').val()=='')
			  {
				alert('Please enter your postcode.');
				$("#postcode").focus();
				return;
			  }
			   
			   if($('#source_port').val()=='')
			  {
				alert('Please select port where you want your delivery to come from.');
				$("#source_port").focus();
				return;
			  }
			  
			  var postcode=$('#postcode').val();
		  }
			 
			  if(delivery_type=='warehouse')
			  {
				  $('#warehouse').removeAttr('disabled');
				  var postcode=$('#warehouse').val();
				  if(postcode=='')
				  return;
			  }
			  else
			  {
			   $('#warehouse').attr('selectedIndex',0)
			   $('#warehouse').attr('disabled', true);	  
			  }
			  
		}
	$("#del_price").html('<img src="'+sitepath+'images/loading.gif" />');
	$.post(sitepath+"ajax/calc_delivery_price.php", {delivery_type: delivery_type,postcode:postcode,subtotal:$("#subtotal").val(),weight:$("#total_weight").val(),source_port:$('#source_port').val()},
  	function(data){
 if(data=='Invalid')
    {
		$("#invalidPostcode").html("Invalid Postcode");
		$("#total_price").html(formatNumber($("#subtotal").val(),2,',','.','','','-',''));
		$("#del_price").html(formatNumber(0,2,',','.','','','-',''));
	}
	else if(data=='illegal')
	{
		$('#frmCheckout').find("[name=delivery_method]").attr("checked","false");
	    $("#del_price").html("0.00"); 
		 alert("Invalid Selection");
	}
	else
	{
	 $("#invalidPostcode").html("");
	 var res=data.split("|");
	 $("#total_price").html(res[0]);
	  $("#del_price").html(res[1]);
	}
  }); 
  }
  
 function enable_postcode()
  {
	$('#postcode').removeAttr('disabled');
	$('#source_port').removeAttr('disabled');
	$('#warehouse').attr('selectedIndex',0)
	   $('#warehouse').attr('disabled', true);	
  }
 function validate_delivery()
   {
	   var delivery_method_selected=false;
	   var req=true;
	   var j=0;
	   $('#frmCheckout').find("[name=delivery_method]").each(function(i)
         {
			 j=i+1;
			  if($(this).attr('checked'))
			 {
				 delivery_method_selected=true;
			  if($(this).attr("id")=="delivery_method_2")
			     {
					 if(trim($("#postcode").val())=='')
					 {
					 alert('Please enter your postcode.');
					 $("#postcode").focus();
					 req=false;
					 }
					 else if($("#source_port").val()=='')
					 {
					 alert('Please select port where you want your delivery to come from.');
					 $("#source_port").focus();
					 req=false;
					 }
				 }
				 
				 if($("#invalidPostcode").html().length>0)
				    {
					 alert('Please enter valid postcode.');
					 $("#postcode").focus();
					  req=false;
					}
				 
				 if($(this).attr("id")=="delivery_method_4" && ($("#warehouse").val()==''))
			     {
					 alert('Please select depot.');
					 $("#warehouse").focus();
				     req=false;
				 }
			 }
			 }
       ); 
	   if(!delivery_method_selected)
	   { 
	     alert('Please select delivery method');
	     return false;
	   }
	   
	   if(!req)
	   return false;
	   
	   return true;
   }
   
   function apply_for_finance()
     {
		 if(!validate_delivery())
		  return false;
		  else
		  {
				var theForm=document.getElementById("frmCheckout");
				theForm.action = "shopping_cartaction.php?mode=apply_finance";
				theForm.submit();  
			  
		  }
		 
     }
  

function validate_promo()
  {
   if(trim($("#promo_code").val())=="")
     {
	   alert("Please enter promotional code.");
	   $("#promo_code").focus()
	   return false;
	 }
	else
	{
	
		var theForm=document.getElementById("frmCheckout");
		theForm.action = sitepath+"checkoutsteps/shopping_cartaction.php?mode=promo_code";
		theForm.submit();
	}
	  
 }
function showonceterms()
 {
	 newwindow('http://onceonline.com.au/Retail-Finance/Interest-Free-Online.aspx',"Once","height=700,width=800,left=0,top=0,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes");
	 
 }
function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2)
{
	var x = Math.round(num * Math.pow(10,dec));
	if (x >= 0) n1=n2='';
	var y = (''+Math.abs(x)).split('');
	var z = y.length - dec; if (z<0) z--;
	for(var i = z; i < 0; i++) 
	y.unshift('0'); 
	if (z<0) z = 1; 
	y.splice(z, 0, pnt);
	if(y[0] == pnt) y.unshift('0'); 
	while (z > 3) 
	{z-=3;
	y.splice(z,0,thou);
	}
	var r = curr1+n1+y.join('')+n2+curr2;
	return r;
}


 function trim(str)
{
 while(''+str.charAt(0)==' ')
  str=str.substring(1,str.length);
  while(''+str.charAt(str.length-1)==' ')
   str=str.substring(0,str.length-1);
 return str;
}
