// JavaScript Document
	
/*
function add_fabrics()
{


	if($('#fabric_colour_id').val()=="")
	{
		alert("Please select fabrics!!");
		return false;
	}
	
	if($('#quantity').val()=="0")
	{
		alert("Please select Quantity!!");
		return false;
	}
	  $('#order_summary').html('<img src="'+sitepath+'images/loading.gif" />')
	$.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);
  });
  return false;
}

*/


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;

	}
	
	
	$('#order_summary').html('<img src="'+sitepath+'images/loading.gif" />')
	
	$.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);
  });

}




function change_fabric(colour_id)
{
	$('#fabric_colour_id').val(colour_id);
	add_fabrics();
}

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" />');
		
		$.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);
		 $('#fabric_colour_id').val("");
		
		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();
//alert("accessories_quantity:"+acc_quantity);

acc_fabric_colour_id=$('#hdn_fabric_colour_id'+accessories_id).val();
//alert("accessories_fabric_id:"+acc_fabric_colour_id);


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 show_acc(id,type)
{
	$("#wrapper").find(".accessories_div").each(function(i){
    if(i==id)
	{   
	    switch(type)
		{
		case 'acc':
			var ht=$('#acc_detail_'+id).html();
			$('#acc_detail_'+id).remove();
			var i_d='acc_detail_'+id;
			$('#td_acc_detail').prepend('<div id="'+i_d+'">'+ht+'</div>');
			break;
		 
		  case 'kr':
		    var ht=$('#king_related_detail_'+id).html();
		 	$('#king_related_detail_'+id).remove();
			var i_d='king_related_detail_'+id;
		    $('#td_king_related').prepend('<div id="'+i_d+'">'+ht+'</div>');
		 	break;
		}
		$("#option"+i).fadeOut(500);
		$("#accessories_div_"+id).slideToggle(1500);
		//$(this).slideToggle(1000);
	  	
	}
	else
	 {
	   $("#option"+i).show();
	   $("#accessories_div_"+i).fadeOut('fast');
	   //$(this).fadeOut("fast");
	 }
  });
}
*/
function calc_delivery(delivery_type)
  {
	 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);	  
	  }
	//$("#total_price").html('<img src="'+sitepath+'images/loading.gif" />');
	$("#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
	{
	 $("#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;
				 }
			 }
			 //val=$('[name=delivery_method]').val();
		 }
       ); 
	   if(!delivery_method_selected)
	   { 
	     alert('Please select delivery method');
	     return false;
	   }
	   
	   if(!req)
	   return false;
	   
	   return true;
   }
  

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 = "shopping_cart.php";
		theForm.submit();
	}
	  
 }

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;
}