//----------------------------------------------------------------------
/*Function to add to cart */
/* function call(id,docroot,indicator) 
{
indicator = indicator || 0; 
var temp			= 'prod'+id;
var qty 			= document.getElementById(temp).value;
var variant_parent  = $("#variants_parent_"+id).val();
var variant_child   = $("#variants_parent_related_"+id).val();
	if(document.getElementById(temp).value > 0 )
	{
		
		//var url = docroot+"index.php/product/ajax_call/add_product/";
		if(typeof variant_parent == 'undefined') 
			variant_parent = 0;
		
		if(typeof variant_child == 'undefined') 
			variant_child = 0;
			//product_variants CLASS NAME 
		var url 			= docroot+"index.php/product/ajax_call/add_product/";
		var data_values		= 'p_id='+id+'&variant_parent='+variant_parent+'&variant_value='+variant_child+'&qty='+qty;
		var refresh_url     = site_url+'cart';
		$.ajax({
					type: "POST",   
					url: url,
					cache:false,
					data: data_values,
					success: function(msg){ 
								 load_all_flash(id);
								 update_header_cart(docroot);
									if(indicator)
									{
										setTimeout("handle_redirect('"+refresh_url+"')",3000);
									}
								}	
			   });
		
	}
	else
	{
		alert("Please enter appropriate quantity.");
	}

} */

function call(id,docroot,indicator) 
{
	indicator = indicator || 0; 
	var temp			= 'prod'+id;
	var qty 			= document.getElementById(temp).value;
	if(document.getElementById(temp).value > 0 )
	{
		
	if(typeof variant_parent == 'undefined') 
			variant_parent = 0;
		
		if(typeof variant_child == 'undefined') 
			variant_child = 0;
			//product_variants CLASS NAME 
			
			
		var selected_variants = [];
			$('.product_variants :selected').each(function(i, selected){
				selected_variants[i] = $(selected).val();
			});
	 
	
		var url 			= docroot+"index.php/product/ajax_call/add_product/";
		var data_values		= 'p_id='+id+'&variant_detail='+selected_variants+'&qty='+qty;
		var refresh_url     = site_url+'cart';
		$.ajax({
					type: "POST",   
					url: url,
					cache:false,
					data: data_values,
					success: function(msg){ 
								 load_all_flash(id);
								 update_header_cart(docroot);
									if(indicator)
									{
										setTimeout("handle_redirect('"+refresh_url+"')",3000);
									}
								}	
			   }); 
		
	}
	else
	{
		alert("Please enter appropriate quantity.");
	}

}


function changeVariant(parent,child,productId)
	{
		var parentValue = $('#'+parent).attr('value');
		if(parentValue == 0)
		{
			$('#'+child).html("<option value='0' title='select'>Select<\/option>");
			var product_price = $("#product_org_price_"+productId).val();
			$("#product_price_"+productId).html("$"+product_price);
		}
		else
		{
			$('#'+child).html('');
			$('#'+child).html($("#parent_child_"+productId+" .sub_"+parentValue).clone());
			var option = $('#'+child+" .sub_"+parentValue);
			option[0].selected="selected";
		}	
	}

function changeprice(productId)
{
	var variant_child 		= $("#variants_parent_related_"+productId).val();
	var product_price_org	= parseFloat($("#product_org_price_"+productId).val());
	var variant_price		= $("#variant_variantions_"+productId+"_"+variant_child).val();
	if(variant_price)
	{
		var product_sign		=  variant_price.substr(0,1);
		var product_price       = parseFloat(variant_price.substr(1));
		if(product_sign == "+")
			var purchase_price 		=  product_price_org + product_price;
		else
			var purchase_price 		=  product_price_org - product_price;
		if (purchase_price.toFixed) //if browser supports toFixed() method
			$("#product_price_"+productId).html("$"+purchase_price.toFixed(2));
		else
			$("#product_price_"+productId).html("$"+purchase_price);
	}
}



function change_variants_price(variantId,productId)
{
	var product_price_org		= parseFloat($("#product_org_price_"+productId).val());
	var product_price_modify	= parseFloat($("#product_new_price_"+productId).val());
	var selectBoxValue      	= variantId.split('_');
	var parent_variant_id		=  selectBoxValue[0];
	var child_variant_id		=  selectBoxValue[1];
	var variant_price			= $("#variant_variantions_"+variantId).val();
	var product_sign			=  variant_price.substr(0,1);
	var product_price       	= parseFloat(variant_price.substr(1));
	
	//LOGIC TO DEDUCT VALUES IF CLIENT 
	  var  check_variants = $("#variant_variantions_"+parent_variant_id+'_'+0).val();
	  if(child_variant_id == 0)
	  {
				
		//GET THE DETAILS OF THE VARIANTS PREVIOUSLY ASSIGNED
		var product_sign_temp			=  check_variants.substr(0,1); //SIGN OF PREVIOUSLY ASSIGNED VARIANTS
		var product_price_temp       	=  parseFloat(check_variants.substr(1));
		
		//DEDUCE OR ADD THE VALUES
		if(product_sign_temp == "+")
			product_price_modify 		=  product_price_modify - product_price_temp;
		else if(product_sign_temp == "-")
			product_price_modify 		=  product_price_modify + product_price_temp;
		
		purchase_price = product_price_modify
		//UPDATE HIDDEN PRODUCT PRICE VALUES 
			$("#product_new_price_"+productId).val(product_price_modify);
		//UPDATE HIDDEN PRODUCT VARIANTS VALUES 
			$("#variant_variantions_"+parent_variant_id+'_'+0).val('');
		
	  }
	  else
	  {
			//FIRST CHECK THE VALUE EXIST IN THE FEILD variant_variantions_PARENTID_0
			if(check_variants == '')
			{
				
				//SET THE VALUES OF VARIANTS USED
				$("#variant_variantions_"+parent_variant_id+'_'+0).val(variant_price);
				
				//CALCULATE THE PRODUCT NEW PRICE AND ATTACH THE VARIANTS
				if(product_sign == "+")
					var purchase_price 		=  product_price_modify + product_price;
				else if(product_sign == "-")
					var purchase_price 		=  product_price_modify - product_price;
				else	
					var purchase_price 		= product_price_modify;
				
				//UPDATE THE HIDDEN FEILD OF THE PRODUCT MODIFY PRICE 
				$("#product_new_price_"+productId).val(purchase_price);
				product_price_modify = purchase_price;
			}
			else
			{
				
				//FIRST GET THE PREVIOUS ASSIGNED VARIANTS ie check_variants AND DEDUCT THE ASSIGNED COST PREVIOUSLY
				var product_sign_temp			=  check_variants.substr(0,1); //SIGN OF PREVIOUSLY ASSIGNED VARIANTS
				var product_price_temp       	=  parseFloat(check_variants.substr(1));
				
				if(product_sign_temp == "+")
					product_price_modify 		=  product_price_modify - product_price_temp;
				else if(product_sign_temp == "-")
					product_price_modify 		=  product_price_modify + product_price_temp;
				
				//SINCE THE OLD COST IS DEDUCED NOW 
				//EITHER ADD/SUBTRACT THE NEW COST 
				//OR JUST RESET THE ORIGINAL VALUE 
				if(product_sign == "+")
					var purchase_price 		=  product_price_modify + product_price;
				else if(product_sign == "-")
					var purchase_price 		=  product_price_modify - product_price;
				else	
					var purchase_price 		= product_price_modify;
				
				//UPDATE THE HIDDEN FEILD OF THE PRODUCT MODIFY PRICE 
				$("#product_new_price_"+productId).val(purchase_price);
				product_price_modify = purchase_price;
				
				//UPDATE THE VARIANTS 
				$("#variant_variantions_"+parent_variant_id+'_'+0).val(variant_price);
			}
			
	  }
	  
	  //FINALLY UPDATE THE DISPLAY PRODUCT PRICE 
	  if (purchase_price.toFixed) //if browser supports toFixed() method
	  {
		$("#product_price_"+productId).html("$"+purchase_price.toFixed(2));
	  }	
	  else
	  {
		$("#product_price_"+productId).html("$"+purchase_price);
	  }	
	
}


function product_enquire(id,docroot){
	if(id > 0){
		var url = docroot+"/product/ajax_call/add_productenquire/";
		$.post(url, {pid: id},
			function(data){
				window.location = docroot+"contact-us";
		});
	}
}

function update_header_cart(docroot)
{
		var url = docroot+"index.php/product/ajax_call/update_header_cart/";
		$.ajax({
				url:url, 
				dataType:'json', 	  
				success:function (data)
				{
					$("#item_count").html(data.prd_cnt+" item(s)");
					$("#item_cost").html("A$"+data.total);
				}
			  });

}

function updateCart()
{
   document.getElementById("shop_cart").innerHTML=obj.msg;
}


function calcPrice() 
{
	var totalPrice = 0;
	$("#cart tr .quantity").parent().each(function() {
		var quantity = $(".quantity input", this).val();
		var unitPrice = $(".unit_price", this).text().slice(1);
		var extendedPrice = quantity*unitPrice;
		totalPrice += extendedPrice;
		
		$(".extended_price", this).html("$" + extendedPrice);
		$("#total_price").html("$"+totalPrice);
	});
	if ( totalPrice == 0 ) {
		$("#cart").parent().replaceWith("<p class='center'>You have no items in your cart.</p>");
	}
}
