var config_path_site = "/";
$(document).ready(function () 
{
	hideMsg();
	highlightDetailsTable();
	
	removeItemButton();
	updateQtyEvents();
	clearOnFocus();
	
	$('.buyNowBut').click(
		function()
		{
		    addToCartItem('butBuy' + $(this).attr('id'), $(this).attr('id'))
		}
	);//End change brand event	
	
	$(".fadeInOut").hover(
		function () {
			$(this).fadeTo('slow', 0.5);
		}, 
		function () {
			$(this).fadeTo('fast',1);
		}
	);

	
});
function clearOnFocus()
{
	varSearch = $('input[name=search]').val();
	if(varSearch == "Item Code")
	{
		$('input[name=search]').css("font-weight","normal");
		$('input[name=search]').css("font-style","italic");
	}
	$("#search").focus(
		function(){
			varSearch = $('input[name=search]').val();
			if(varSearch == "Item Name")
			{
				$('input[name=search]').css("font-weight","bold");
				$('input[name=search]').css("font-style","normal");
				$('input[name=search]').attr("value","");
			}
		}
	);
	$("#search").blur(
		function(){
			varSearch = $('input[name=search]').val();
			if(varSearch == "")
			{
				$('input[name=search]').css("font-weight","normal");
				$('input[name=search]').css("font-style","italic");
				$('input[name=search]').attr("value","Item Name");
			}
		}
	);
}
function hideMsg()
{
	$('.msg_warning,.msg_error,.msg_info,.msg_general').click(
		function()
		{
			$(this).fadeOut("slow");
		}
	);
	window.setTimeout('autoHideMsg()',6000);
}
function autoHideMsg()
{
	$('.msg_warning,.msg_error,.msg_info,.msg_general').click(
		function()
		{
			$(this).sideUp("slow");
		}
	);
}

function removeItemButton()
{

	$('.butRemoveItem').click(
		function()
		{
		    removeCartItem('rowRemove' + $(this).attr('id'), $(this).attr('id'))
		}
	);//End change brand event

}
function setPaths(sitePath)
{
	config_path_site = sitePath;
}

function addToCart()
{

	tempHTML = $("#addToCartMain").html();
	
	varQty = $('#product_qty').attr("value");
	varId = $('#product_id').attr("value");

	$("#addToCartMain").html('<div class="wating"><img src="/images/progress.gif" height="16px" width="16px" alt="" /> Adding Porduct...</div>');
	
	
	randNum = Math.random()*100;
	
	$.ajax({
	   type: "GET",
	   url: config_path_site + "_ajax_cart_add.do",
	   data: "rand=" + randNum + "&id=" + varId + "&qty=" + varQty,
		   error: function(){
			 alert("Can't complete process at present please try after some time!");
			 $("#addToCartMain").html(tempHTML);
		   },
	   success: function(msg){
		   $("#addToCartMain").html('<h2>Product Added Successfully</h2><h3>Please check on top left corner</h3>');
		   updateCartShort();
	   }
	 });
}

function addToCartItem(butId, varId)
{

	tempHTML = $("#" + butId).html();

	$("#" + butId).html('wait...');
	
	
	randNum = Math.random()*100;
	
	$.ajax({
	   type: "GET",
	   url: config_path_site + "_ajax_cart_add.do",
	   data: "rand=" + randNum + "&id=" + varId + "&qty=1",
		   error: function(){
			 alert("Can't complete process at present please try after some time!");
			 $("#" + butId).html(tempHTML);
		   },
	   success: function(msg){
	   		alert(msg);
		   $("#" + butId).html('buy now');
		   updateCartShort();
		   alert("Product Added Successfully!");
	   }
	 });
}

function removeCartItem(butId, varId)
{

	randNum = Math.random()*100;
	
	$.ajax({
	   type: "GET",
	   url: config_path_site + "_ajax_cart_remove.do",
	   data: "rand=" + randNum + "&id=" + varId + "&qty=1",
		   error: function(){
			 alert("Can't complete process at present please try after some time!");
			 //$("#" + butId).html(tempHTML);
		   },
	   success: function(msg){
		   //alert("Product Added Successfully!");
		   $("#" + butId).fadeOut();
		   updateCartShort();
	   }
	 });
}




function showHide(id)
{
	$('#' + id).toggle()
}
function highlightDetailsTable()
{

	if ( $('#countryId').length )
	{
		$('#countryId').change(
			function()
			{
			    loadVisaForms();
			}
		);//End change brand event
	}//End if Brand Check
	

	$("Table.details tr").hover(
		function () {
			if($(this).find("td"))
			{
				$(this).css("background-color","#F2F1CA");
			}
		}, 
		function () {
			if($(this).find("td"))
			{
				$(this).css("background-color","");
			}
		}
	);
	
	$("Table.details2 tr").hover(
		function () {
			if($(this).find("td"))
			{
				$(this).css("background-color","#F2F1CA");
			}
		}, 
		function () {
			if($(this).find("td"))
			{
				$(this).css("background-color","");
			}
		}
	);

	
	
}


function updateQtyEvents()
{
	$('.qtyDecrease').click(
		function()
		{
			varId = $(this).attr('id');
			tempHTML = $("#itemQty" + varId).html();
			
			varQty = $('#qty_' + varId).attr("value");
		
			$("#itemQty" + varId).html('<img src="' + config_path_site + 'images/wait.gif" />');
			
			
			randNum = Math.random()*100;
			
			$.ajax({
			   type: "GET",
			   url: config_path_site + "_ajax_cart_qty_dec.do",
			   data: "rand=" + randNum + "&id=" + varId + "&qty=" + varQty,
				   error: function(){
					 alert("Can't complete process at present please try after some time!");
					 $("#itemQty" + varId).html(tempHTML);
					 updateQtyEvents();
				   },
			   success: function(msg){
				   $("#itemQty" + varId).html(tempHTML);
				   $('#qty_' + varId).attr("value", msg);
				   updateCartShort();
				   //updateQtyEvents();
			   }
			 });
		}
	);//End change brand event
	
	$('.qtyIncrease').click(
		function()
		{
			varId = $(this).attr('id');
			tempHTML = $("#itemQty" + varId).html();
			
			varQty = $('#qty_' + varId).attr("value");
		
			$("#itemQty" + varId).html('<img src="' + config_path_site + 'images/wait.gif" />');
			
			
			randNum = Math.random()*100;
			
			$.ajax({
			   type: "GET",
			   url: config_path_site + "_ajax_cart_qty_inc.do",
			   data: "rand=" + randNum + "&id=" + varId + "&qty=" + varQty,
				   error: function(){
					 alert("Can't complete process at present please try after some time!");
					 $("#itemQty" + varId).html(tempHTML);
					 updateQtyEvents();
				   },
			   success: function(msg){
				   $("#itemQty" + varId).html(tempHTML);
				   $('#qty_' + varId).attr("value", msg);
				   updateCartShort();
				   //updateQtyEvents();
			   }
			 });
		}
	);//End change brand event
	
	
	$('.qtyDecreaseOnly').click(
		function()
		{
			varId = $(this).attr('id');
			var varQty = $('#product_qty').attr("value");
			if(varQty > 1)
			{
				varQty--;
				$('#product_qty').attr("value",varQty);
			}
		}
	);//End change brand event
	
	$('.qtyIncreaseOnly').click(
		function()
		{
			varId = $(this).attr('id');
			var varQty = $('#product_qty').attr("value");
			varQty++;
			$('#product_qty').attr("value",varQty);
		
		}
	);//End change brand event

	
	
	$('.qtyUpdate').click(
		function()
		{
			varId = $(this).attr('id');
			tempHTML = $("#itemQty" + varId).html();
			
			varQty = $('#qty_' + varId).attr("value");
		
			$("#itemQty" + varId).html('<img src="' + config_path_site + 'images/wait.gif" />');
			
			
			randNum = Math.random()*100;
			
			$.ajax({
			   type: "GET",
			   url: config_path_site + "_ajax_cart_qty_update.do",
			   data: "rand=" + randNum + "&id=" + varId + "&qty=" + varQty,
				   error: function(){
					 3("Can't complete process at present please try after some time!");
					 $("#itemQty" + varId).html(tempHTML);
					 updateQtyEvents();
				   },
			   success: function(msg){
				   $("#itemQty" + varId).html(tempHTML);
				   $('#qty_' + varId).attr("value", msg);
				   updateCartShort();
				   //updateQtyEvents();
			   }
			 });
		}
	);//End change brand event
}

function updateCartShort()
{
	updateCart();
	tempHTML = $("#carShort").html();
	
	$("#tblCartShort").fadeTo(200, 0.33);

	
	$("#carShortLoader").fadeIn(200);
	
	randNum = Math.random()*100;
	
	$.ajax({
	   type: "GET",
	   url: config_path_site + "_ajax_cart_short.do",
	   data: "rand=" + randNum,
		   error: function(){
			 alert("Can't complete process at present please try after some time!");
			 $("#carShort").html(tempHTML);
			 removeItemButton();
		   },
	   success: function(msg){
		   $("#carShort").html(msg);
		   removeItemButton();
		   $("#carShortLoader").fadeOut();
	   }
	 });
	 
}



function updateCart()
{

	tempHTML = $("#cart").html();
	
	$("#cartLoader").html('<img src="' + config_path_site + 'images/wait.gif" />');
	
	$("#cartLoader").fadeIn();
	
	randNum = Math.random()*100;
	
	$.ajax({
	   type: "GET",
	   url: config_path_site + "_ajax_cart.do",
	   data: "rand=" + randNum,
		   error: function(){
			 alert("Can't complete process at present please try after some time!");
			 $("#cart").html(tempHTML);
		   },
	   success: function(msg){
		   $("#cart").html(msg);
		   updateQtyEvents();
	   }
	 });
	 $("#cartLoader").fadeOut();
	 
	 
}




