$(document).ready(function() {

	var debug = false;

	/*
	jQuery.ajaxSetup({ 
	contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1" 
	});
	*/

	rebindCart();
	rebindDiscountCodeInfo();
	rebindDeliveryType();
	rebindPayType();
	rebindCustomerInfo();


	
	$("#fastCheckoutFormSubmit").click(function() {

		var result = true;
		var resultForm = true;
		var alertText = "";

		$("#orgNoStatus").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=getCustomerOrgNoStatus", { orgNo: $("#orgNo").val() } , function() {	
			if(!( $("#orgNoStatus").html() == '' ))
			{
				alertText = alertText + trans_org_no_is_already_in_use_please_enter_another +'. \n'+ trans_if_it_belongs_to_you_please_log_in +'.\n\n';
				//result = false;
			}
		
			if(!($("#disclaimer").attr("checked")))
			{
				alertText = alertText + trans_agree_with_the_terms_of_condition + '.\n\n';
				//result = false;
			}
			


			if(!( $("#emailStatus").html() == '' ))
			{
				alertText = alertText + trans_email_is_already_beeing_us +'. \n'+ trans_if_it_belongs_to_you_please_log_in +'.\n\n';
				result = false;
			}
			
			if (alertText !="")
			{
				alert(alertText);
				result = false;
			}
			
			if (result == true)
			{
		        var formObj = document.getElementById('fastCheckoutForm');
				resultForm = mySubmitForm(formObj);	
			}

			else
			{
				return false;
			}		

			if (resultForm == true && result == true)
			{
							
				$.post("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=fastCheckoutCustomerSubmit", $("#fastCheckoutForm").serialize(),function(data){
				    $("#customerId").val(data);

					//LÄÄGG TILL EN FUNKTION SOM *KOLLAR OM VI FÅTT NÅGOT TILLBAKA.
					//if  result = false;
					//alert(data);
					//alert("datat är:"+ $("#customerId").val());
					
					$("#fastCheckoutForm").submit();

				 });
			
			}

		});
	});
				
	// Aktivera validering för alla inputfält för vald betaltyp
    	$("#customerData input[validateData*='"+$("input[name='payType']:checked").val()+",']").attr("validate","1");
		
	// Vid varje ajax-laddnings start skall popup visas + centreras
	$("#fastCheckout").ajaxStart(function(){
		$("#ajaxPopup").center();
		loadPopup();
		//centerPopup();
	 });

	// Vid varje ajax-laddnings slut skall popup döljas
	$("#fastCheckout").ajaxStop(function(){
		disablePopup();
	 });

	// Aktivera så att kommentatorsfältet växer efter behov
	//$("#pComment").growfield({});
		                
function updateSummary()
{
	if (debug == true) alert('updateSummary');
	$("#fastCheckoutSummary").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateSummary");			
}
 
function rebindPayType()
{
	if (debug == true) alert('rebindPayType');
	$("input[name='payType']:radio").click(function() {
	
	    	$("#fastCheckoutForm").attr("action",$(this).attr("action"));
		$("#cartWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateCart", { payType: $(this).val() } , rebindCart );			
		
		$("#payTypeInfo").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updatePayTypeInfo", { payType: $(this).val() } );			
				    	
	    	if($(this).val()=='K')
	    	{		
			$("#customerWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateCustomer&getCustomerInformationFromKreditor=1", { orgNo: $("#orgNo").val() } , rebindCustomerInfo );					
			if (debug == true) alert('show kreditorinfo');
	    	}
	    	
	    	else
	    	{
			rebindCustomerInfo();	    	
    			if (debug == true) alert('hide kreditorinfo');			
	    	}	
	    		    			    	
	});
}

function rebindDeliveryType()
{			
	if (debug == true) alert('rebindDeliveryType');
	$("input[name='deliveryType']:radio").click(function() {
		$("#cartWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateCart", { deliveryType: $(this).val() } , rebindCart );	
		$("#deliveryTypeInfo").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateDeliveryTypeInfo", { deliveryType: $(this).val() } );						
	});
}

function rebindDiscountCodeInfo()
{
	if (debug == true) alert('rebindDiscountCodeInfo');
	$("#discountCodeSubmit").click(function() {
		$.post("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=getDiscountCodeDetails", { discountCode: $("#discountCode").val() } , updateCart );	
	});

	$("#discountCode").keyup(function(e) {	
	if(e.keyCode == 13) 
	{		
		$.post("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=getDiscountCodeDetails", { discountCode: $("#discountCode").val() } , updateCart );	
	}
	});

}	

function updateCart()
{
	if (debug == true) alert('updateCart');
	$("#cartWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateCart", rebindCart );				
}
		    	   
function rebindCart()
{
	if (debug == true) alert('rebindCart');

	$("input[cat='noItems']").change(function() {
		$("#cartWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateCart", { rowNo : $(this).attr("rowNo"), noItems: $(this).val() } , rebindCart );	
	});
	
	$(".removeArticle").click(function() {
		$("#cartWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateCart", { removeArticleId : $(this).attr("articleId") } , rebindCart );				
	});			

	$(".incNoOfItems").click(function() {
		$("#cartWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateCart", { incArticleRow : $(this).attr("rowNo") } , rebindCart );				
	});			

	$(".decNoOfItems").click(function() {
		$("#cartWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateCart", { decArticleRow : $(this).attr("rowNo") } , rebindCart );				
	});	

	$(".activateGiftWrap").click(function() {
		$.get("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateGiftWrap", { articleId : $(this).attr("articleId") , giftWrapActive : $(this).attr("checked") } ,updateCart );	
	});			

	$(".dropruta.giftWrapCard").change(function() {						
		$.get("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateGiftWrap", { articleId : $(this).attr("articleId"), giftWrapCard : $(this).val() } , updateCart );		
	});

	$(".dropruta.giftWrapPackage").change(function() {			
		$.get("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateGiftWrap", { articleId : $(this).attr("articleId"), giftWrapPackage : $(this).val() } , updateCart );				
	});	

	$(".textbox.giftWrapCardText").change(function() {			
		$.get("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateGiftWrap", { articleId : $(this).attr("articleId"),  giftWrapCardText : $(this).val() } , updateCart );				
	});	

	rebindDiscountCodeInfo();	
	updateSummary();
				
}	    		 
		    
function rebindCustomerInfo()
{

	$("#login").click(function() {
		if ($("#username").val() == '' || $("#password").val() == '')
		{
			alert(trans_Please_specify + ': ' + trans_email +', ' + trans_password);
		}
		else
		{
			$("#customerWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=login", { username: $("#username").val() , password: $("#password").val() } , function() {
			rebindCustomerInfo();
			updateCart();
			} );
		}
	});
	
	$('#password').keyup(function(e) {	
	//alert(e.keyCode);	
	if(e.keyCode == 13) 
	{		
		if ($("#username").val() == '' || $("#password").val() == '')
		{
			alert(trans_Please_specify + ': ' + trans_email +', ' + trans_password);
		}
		else
		{
			$("#customerWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=login", { username: $("#username").val() , password: $("#password").val() } , rebindCustomerInfo );					
		}
	}
	});

	if (debug == true) alert('rebindCustomerInfo');

	if($("input[name='customerMode']:checked").val() == '1')
	{
		$("#fastCheckoutFormSubmit").attr("disabled", "disabled");
	}
	else
	{
		$("#fastCheckoutFormSubmit").attr("disabled", "");
	}
	    
	switch ( $("input[name='payType']:checked").val() )
	{
		case 'K':
	    	$("#customerData input[kreditorDisable='true']").attr("readonly","readonly");	
   	  	$("#kreditorInfo").show();
   	  	$("#otherPayInfo").hide();
		break;
		
		default:
	    	$("#customerData input[kreditorDisable='true']").attr("readonly","");
   	  	$("#kreditorInfo").hide();
   	  	$("#otherPayInfo").show();
		break;		     
	}

	$("#customerData input").attr("validate","");
	$("#customerData input[validateData*='"+$("input[name='payType']:checked").val()+",']").attr("validate","1");
						 
 	$("input[name='emailAddress']").blur(function() {			
		$("#emailStatus").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=getWebUserLoginStatus", { email: $(this).val() }  );																	
	});
	 		
	$("input[name='customerMode']:radio").click(function() {				
		$("#customerWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateCustomer", { customerMode: $(this).val() } , rebindCustomerInfo );																	
	});

	$("input[name='customerOrganizationalForm']:radio").click(function() {		
		$("#customerWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateCustomer", { customerOrganizationalForm: $(this).val() } , rebindCustomerInfo );													
	});

	$("input[name='activateDeliveryAddress']").click(function() {
		$.post("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateActivateDeliveryAddress", { ActivateDeliveryAddress : $("input[name='activateDeliveryAddress']:checked").val() }  );	
	
	var v = $("input[name='activateDeliveryAddress']:checked").val();
					
	if (v == "on")
	{
		$("#deliveryAddress").slideDown();	
	}
	else
	{
		$("#deliveryAddress").slideUp();
	}				
	});	

	$("input[name='getCustomerInformationFromKreditor']").click(function() {
		
		$("#orgNoStatus").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=getCustomerOrgNoStatus", { orgNo: $("#orgNo").val() } , function() {
			if (($("#orgNoStatus").html()) == '')
			{
				$("#customerWhitespace").load("/shop/shopfunctions/fastCheckoutAJAXAction.asp?action=updateCustomer&getCustomerInformationFromKreditor=1", { orgNo: $("#orgNo").val() } , rebindCustomerInfo );
			}		
		});	
	});
}
	
});


var popupStatus = 0;  

function loadPopup()
{   

	if(popupStatus==0)
	{   
		//$("#ajaxBackground").css({   "opacity": "0.5"  });   
		$("#ajaxPopup").toggle();
		//$("#ajaxBackground").toggle();
		popupStatus = 1;   
	}   
}  

function centerPopup()
{   
	var windowWidth = document.documentElement.clientWidth;   
	var windowHeight = document.documentElement.clientHeight;   
	var popupHeight = $("#ajaxPopup").height();  
	var popupWidth = $("#ajaxPopup").width();   
	$("#ajaxPopup").css({   "position": "absolute",   "top": windowHeight/2-popupHeight/2,   "left": windowWidth/2-popupWidth/2   });  	
	$("#ajaxBackground").css({   "height": windowHeight   });   
}

function disablePopup()
{   
	if(popupStatus==1)
	{   
		//$("#ajaxBackground").toggle();   
		$("#ajaxPopup").toggle();   
		popupStatus = 0;   
	}   
}