$(document).ready(function(){
									
   jQuery.popOver = function(section,id,ug_from){
	
		var document_height = parseInt($(document).height()); 
		$("#OVERLAY").height(document_height);	
		$("#OVERLAY").fadeIn("fast"); 
		$("body").append('<div id="config_div"></div>');
		$("#config_div").load("../shop/load/configure" + section + ".php",{PRODUCT:id,upgrade_from:ug_from})
		$("#config_div").css({"marginTop": "-" + parseInt(($("#config_div").height() / 2) + 100,10) + "px"});
		$("#config_div").fadeIn("fast");	    
		 
	}
	
	$(".config_click").click(function(){

		$.popOver('',$(this).attr('ID'),$(this).attr('from'));		

   });
	
	$(".upgrade").live('click',function(){
		$.popOver('_upgrade',$(this).attr('ID'),$(this).attr('ugfrom'));		
   });	
	
   $("#add_sqlrdd").live('click',function(){
  
		$.popOver('_sqlrdd',$(this).attr('ID'));

   });	
	
   $(".add_sqlrdd_bcc").click(function(){
  
		$.popOver('_sqlrdd_bcc',$(this).attr('ID'));

   });	
	
	$("#cancel_add").live('click',function(){
	   $("#config_div").fadeOut("fast").html(); 
		$("#config_div").remove();
		$("#OVERLAY").fadeOut("fast");
   });	
	
	if($("#cart_content")){$("#cart_content").load("../config/actions.php",{action:'load_cart'});}
	if($("#cart_content_clean")){$("#cart_content_clean").load("../config/actions.php",{action:'load_cart',clean:true});}

	$("#process3").click(function(){
	   $.post("../config/actions.php",{action:'wire_transfer'},function(){
		   window.location='../user/';																					  
		});   										
   });
	
	
	jQuery.RegisterUser = function(){
      
		var block = false;
		
		$('.required').each(
			function(){
				if(!($(this).val())){ 
				   $(this).addClass('input_required'); 
					block = true;
					//alert($(this).attr("id"));
				}else{ $(this).removeClass('input_required'); }  
		});
			
		if(block){ return false; }else{ 	
		
		$.post("../config/actions.php",{action:'new_login',email:$("#email").val(),firstname:$("#firstname").val(),lastname:$("#lastname").val(),companyname:$("#companyname").val(),city:$("#city").val(),state:$("#state").val(),zip:$("#zip").val(),country:$("#country").val(),phone:$("#phone").val(),address:$("#address").val()},function(data){
																																																																																																																						         if(data){ 
			   $("#register_result").html("<p style='margin: 0 0 5px 0; padding:0; color:red;'>" + data + "</p>")					 
			}else{
				
				var document_height = parseInt($(document).height()); 
				$("#OVERLAY").height(document_height);	
				$("#OVERLAY").fadeIn("fast"); 
				$("body").append("<div id='config_div' style='padding:30px;'><p class='h1 addbold'>Please click the checkout button only once, you will be redirected to the paypal site.<br>Please do not use the back and forward button.<br>Click the 'Please click here to complete your order' link after completing your payment.</p><br><div id='payment_loading'></div></div>");
				$("#config_div").css({"marginTop": "-" + parseInt(($("#config_div").height() / 2) + 100,10) + "px"});
				$("#config_div").fadeIn("fast");	   				
				
			
				$("#username").html($("#firstname").val() + " " + $("#lastname").val());
				$("#registerform").fadeOut(300, function() { $(this).remove(); });
 				$("#payment_loading").fadeIn("fast");
				$.post("../config/actions.php",{action:'write_cart'},function(data){
					 $("#os0").attr("value",data)				
					 $("#custom").attr("value",data)		
					 $("#return").attr("value","http://www.xharbour.com/config/actions.php?action=process_order&wwnr=" + data);
					 $("#frm_paypal").submit(); 			 
				});
				
			} 																																																																																																															  	   });

		}

	}
	
	jQuery.Process1 = function(){
		$("#payment_result").html('');			
		$("#payment_result").load("load/paypal_direct.php",{cc_type:$("#cc_type").val(),cc_nr:$("#cc_nr").val(),cc_name:$("#cc_name").val(),cc_month:$("#cc_month").val(),cc_year:$("#cc_year").val(),cc_code:$("#cc_code").val(),amount:'10'},function(result){																																																																															     	});
	}
	
	$("#process1").click(function(){

		$("#payment_result").ajaxSend(function(r,s){
			done = 0
			t = setTimeout(function() { 
				if(done==0){
					$("#payment_loading").fadeIn("fast");
				}
			}, 50);
		});

		$("#payment_result").ajaxStop(function(r,s){
		  $("#payment_loading").fadeOut("fast");
		  done = 1;
		});

	   var block = false;
		var block_process = false;
		
		$('.required').each(
			function(){
				if(!($(this).val())){ 
				   $(this).addClass('input_required'); 
					block = true;
				}else{ $(this).removeClass('input_required'); }  
		});
			
		if(block){ return false; }else{ 		
		   
			if ($('#firstname').is(":visible")) {		
			   alert("register");
			   $.RegisterUser();				
			}else{
			    $.Process1();	
			}
			
		}
	 
   });
	
	$(".tab_pay").click(function(){  
										  
      $("#tab_" + $(this).attr('id')).show().siblings().hide(); 
	   $(this).addClass("tab0").removeClass("tab1").siblings().addClass("tab1").removeClass("tab0"); 
	
	});
	
	/*$("#paypal_express").click(function(){
	   											
	    $.post("../config/actions.php",{action:'write_cart'},function(){ $("#frm_paypal").submit(); });   	
	    return false;
   });*/
	
	$(".btt_del_cart").live('click',function(){
	   if(confirm('Remove this item from your cart?')){
		   $.post("../config/actions.php",{action:'delete_item',index:$(this).attr('index')},function(){ 
			$("#cart_content").load("../config/actions.php",{action:'load_cart'}); });  	
			if($("#cart_content_clean")){$("#cart_content_clean").load("../config/actions.php",{action:'load_cart',clean:true});}
		}
	});
	
	$("#paypal_checkout").click(function(){
													 
			if ($('#firstname').is(":visible")) {		
			   $.RegisterUser();				
			}else{
				 //$("#payment_loading").fadeIn("fast");
				 
				var document_height = parseInt($(document).height()); 
				$("#OVERLAY").height(document_height);	
				$("#OVERLAY").fadeIn("fast"); 
				$("body").append("<div id='config_div' style='padding:30px;'><p class='h1 addbold'>Please click the checkout button only once, you will be redirected to the paypal site.<br>Please do not use the back and forward button.<br>Click the 'Please click here to complete your order' link after completing your payment.</p><br><div id='payment_loading'></div></div>");
				$("#config_div").css({"marginTop": "-" + parseInt(($("#config_div").height() / 2) + 100,10) + "px"});
				$("#config_div").fadeIn("fast");	    				 
				 
				 $.post("../config/actions.php",{action:'write_cart'},function(data){
					 $("#os0").attr("value",data)	
					 $("#custom").attr("value",data)		
					 $("#return").attr("value","http://www.xharbour.com/config/actions.php?action=process_order&wwnr=" + data);
					 //alert($("#return").attr("value"));
					 $("#frm_paypal").submit(); 			 
				 });
			}													 
		 return false;
   });
	
	$(".add2cart").click(function(){
	   
		var pROD = $(this).attr("qty") + ";"+$(this).attr("id")+";"+$(this).attr("price")+";"+$(this).attr("title");		
		var redirect = $(this).attr("autoredirect");
		
	   $.post("../config/actions.php",{action:'add_item',pROD:pROD},function(){																																																																																																									
		   $("#cart_content").load("../config/actions.php",{action:'load_cart'});					
			if(redirect){window.location='checkout.php';}
		});
		
   });

	$("#apply_code").live('click',function(){
       $.post("../config/actions.php",{action:'apply_code',code:$("#discount_code").val()},function(){window.location='../shop/checkout.php';});  	      	
		  //$("#cart_content_clean").load("../config/actions.php",{action:'load_cart',clean:true});
		  
	});

});
