// JavaScript Document
var page = baseurl+"ajax.php";

function hide_cart_js(obj) {
	var This = $(obj);
	if(!$.cookie('cart_view') || $.cookie('cart_view') == 'visible') {
		$('#cart_items').slideUp(350,function() { $('.cart_block').animate({  "width" : "100px" },350) });
		This.text('Show');
		$.cookie('cart_view','hidden',{ path : "/"});
	} else {
		$('.cart_block').animate({ "width" : "200px" },350,function() { $('#cart_items').slideDown(350) });
		This.text('Hide');
		$.cookie('cart_view','visible',{ path : "/"});
	}
}

function toggle_divs(hideobj,showobj)

{

	$(hideobj).hide('slide', function(){ direction: 'left' }, 1000);

	$(showobj).show('slide', function(){ direction: 'left' }, 1000)

}

function add_to_cart(obj,id) {	
		var $this = $(obj);
		$this.text('Adding');
		
		$.post(page, 
		{ 	
			mode : "add_to_cart",
			id : id
		},
		function(data)
		{
			if(!data) {
				alert("No data");
			} else {
				$this.removeAttr('onclick').text('Added');
				load_cart();
			}
		},'text');
}

function remove_from_cart(obj,id) {
		var $this = $(obj);
		$this.text('Removing')
		$.post(page, 
		{ 	
			mode : "remove_from_cart",
			id : id
		},
		function(data)
		{
			if(!data)
				alert("No data");
			else
				load_cart();
		},'text');	
}

function load_cart() {
		$.post(page, 
		{ 	
			mode : "show_cart"
		},
		function(data)
		{
			if(data == "") {
				$('#cart_block').hide();
			} else {
				$('#cart_block').show().html(data);
				
				if($.cookie('cart_view') == 'hidden') {
					$('.cart_block').addClass('hidden_cart_block');
					$('#text').text('Show');
				} else {
					$('.cart_block').removeClass('hidden_cart_block');
					$('#text').text('Hide');		
				}					
			}
		},'text');	
}

function clearCart(obj) {
	
		$(obj).text('Clearing');
	
		$.post(page, 
		{ 	
			mode : "clear_cart"
		},
		function(data)
		{
			if(!data)
				alert("No data");
			else
				load_cart();
		},'text');			
}

function check_inquiry(form,obj) {
	var HTML = $('#details').html(),
		  Details = $('#details'),
		  Inquiry = $('#inquiry_details'),
		  This = $(obj),
		  Form = "'"+form+"'";
		  
	if(Inquiry.css('display') == 'none') {
		Details.fadeOut(150,function() { Inquiry.fadeIn(350)  })
		$('#in_details').val(HTML);
		$('#form_buttons').append('<input type="submit" value="Send Inquiry" onclick="send(this);" name="send_inquiry" id="send_inquiry" />');
		This.val('Back');
	} else {
		Inquiry.fadeOut(150,function() { Details.fadeIn(350)  })
		$('#send_inquiry').remove();
		This.val('Inquiry');
	}
}
	
function send(obj) {
	var Name = $('#your_name'),
		  Email = $('#your_email'),
		  This = $(obj),
		  hasError = false;
		  
		  if($('#your_name').val() == '') {
			  	if($('#name_err').length == 0) 
					Name.after("<span id='name_err' class='error'>Please enter your name.</span>")
				hasError = true;
		  } 
		  
		  if($('#your_email').val() == "") {
			 if($('#email_err').length == 0) 
			 	Email.after("<span id='email_err' class='error'>Please enter your email address</span>")
			 hasError = true;
		  }
		
		if(hasError == false) {
			This.attr('value','Sending ...');
			
			$.post(page,
				   {
					   mode : "sending_inquiry",
						your_name: Name.val(),
						your_email: Email.val(),
						in_details: $('#in_details').val(),
						msg_opt: $('#msg_opt').val()
						
					},
					function (data)
					{ 
						if(!data) {
							alert('No Data');	
						} else {
							$('#inquiry_details').hide();
							$('#form_buttons').hide();
							$('#form_buttons').after('<div id="result" style="font-size:14px; font-weight:bold;" />');
							$('#result').html("Your Email is sent");
						}
							
					},'text')
		}
			
}

function update_quantity($id,obj) {
	var $this = $(obj);
	if($this.val() == "") {
		alert('Quantity can not be empty');
		$this.val("1");	
	} else {
		$.post(page,
			{
				mode : 'update_quantity',
				id : $id,
				qtn : $this.val()
			},
			function(data)
			{
				if(!data){
					alert('No Data');	
				} else {
					load_cart();
				}
					
			})
	}
}
