
function SendCustomer() {
    var cartPalCheckout = {};

    cartPalCheckout['sid'] = '86866ce1f3530466f68992efb329ae2d';
    cartPalCheckout['user_id'] = '4f45671e87b942.50957360';
    cartPalCheckout['referer'] = location.host;
    cartPalCheckout['license_key'] = cartPalLicenseKey;

    cartPalCheckout['billing_email'] = jQuery('#billing\\:email').val();
    cartPalCheckout['billing_firstname'] = jQuery('#billing\\:firstname').val();
    cartPalCheckout['billing_lastname'] = jQuery('#billing\\:lastname').val();
    cartPalCheckout['billing_street1'] = jQuery('#billing\\:street1').val();
    cartPalCheckout['billing_street2'] = jQuery('#billing\\:street2').val();
    cartPalCheckout['billing_city'] = jQuery('#billing\\:city').val();
    cartPalCheckout['billing_region_id'] = jQuery('#billing\\:region_id option:selected').html();
    cartPalCheckout['billing_region'] = jQuery('#billing\\:region').val();
    cartPalCheckout['billing_country_id'] = jQuery('#billing\\:country_id').val();
    cartPalCheckout['billing_postcode'] = jQuery('#billing\\:postcode').val();
    cartPalCheckout['billing_telephone'] = jQuery('#billing\\:telephone').val();

    cartPalCheckout['shipping_firstname'] = jQuery('#shipping\\:firstname').val();
    cartPalCheckout['shipping_lastname'] = jQuery('#shipping\\:lastname').val();
    cartPalCheckout['shipping_street1'] = jQuery('#shipping\\:street1').val();
    cartPalCheckout['shipping_street2'] = jQuery('#shipping\\:street2').val();
    cartPalCheckout['shipping_city'] = jQuery('#shipping\\:city').val();
    cartPalCheckout['shipping_region_id'] = jQuery('#shipping\\:region_id option:selected').html();
    cartPalCheckout['shipping_region'] = jQuery('#shipping\\:region').val();
    cartPalCheckout['shipping_country_id'] = jQuery('#shipping\\:country_id').val();
    cartPalCheckout['shipping_postcode'] = jQuery('#shipping\\:postcode').val();
    cartPalCheckout['shipping_telephone'] = jQuery('#shipping\\:telephone').val();

    jQuery.ajax({
        url: 'https://www.cartpal.net/integration/magento1.6/save_customer.php',
        cache: false,
        dataType: 'jsonp',
        data: cartPalCheckout
    });
}

jQuery.noConflict();

jQuery(document).ready(function() {  

    if(navigator.userAgent.indexOf('Safari') != -1) {
        jQuery("body").append('<iframe id="sessionframe" name="sessionframe" src="javascript:false;" style="display:none;"></iframe><form id="sessionform" enctype="application/x-www-form-urlencoded" action="https://www.cartpal.net/integration/magento1.6/sessionframe.php?key=" target="sessionframe" method="post"></form>');
    }
    
    jQuery('#sessionform').submit();
	
	// Set an array that we later pass to PHP
	var cartPalProductArr = {};
	var cartPalI = 0;
	var cartPalInsideCart = false;

	// Product name
	jQuery('#shopping-cart-table tr .product-name a').each(function() {
	
		cartPalInsideCart = true;
	
		var cartPalProduct = jQuery(this).html();

		// Product options
		var cartPalProductOptionMaster = '';
		jQuery(this).parent().parent().children('.item-options').each(function() {
			jQuery(this).children("dt").each(function(){
				var cartPalProductOption = jQuery(this);

				var cartPalProductOptionVal = jQuery(cartPalProductOption).parent().children("dd").eq( (cartPalProductOption.index()/2) ).html();

				cartPalProductOptionMaster = cartPalProductOptionMaster + cartPalProductOption.html() + ' ' + cartPalProductOptionVal + '\n';
                
			});
		});

		// Product unit price
		var cartPalProductPrice = jQuery(this).parent().parent().parent().find('td.a-right .cart-price span.price').html();

		// Product quantity
		var cartPalProductQuantity = jQuery(this).parent().parent().parent().find('td.a-center input.qty').val();

		cartPalProductArr[cartPalI] = {cartPalProduct: cartPalProduct, cartPalProductOption: cartPalProductOptionMaster, cartPalProductPrice: cartPalProductPrice, cartPalProductQuantity: cartPalProductQuantity};

		cartPalI++;
	});
	
	if(cartPalInsideCart == true){
        cartPalProductArr['sid'] = '86866ce1f3530466f68992efb329ae2d';
		cartPalProductArr['user_id'] = '4f45671e87b942.50957360';
		cartPalProductArr['referer'] = location.host;
		cartPalProductArr['license_key'] = cartPalLicenseKey;
        jQuery.ajax({
	        url: 'https://www.cartpal.net/integration/magento1.6/save_cart.php',
		    cache: false,
	        dataType: 'jsonp',
		    data: cartPalProductArr
        });
	}

	
		
	// Checkout field processing
	jQuery('#billing\\:email, #billing\\:firstname, #billing\\:lastname, #billing\\:street1, #billing\\:street2, #billing\\:city, #billing\\:region_id, #billing\\:region, #billing\\:postcode, #billing\\:telephone, #shipping\\:firstname, #shipping\\:lastname, #shipping\\:street1, #shipping\\:street2, #shipping\\:city, #shipping\\:region_id, #shipping\\:region, #shipping\\:postcode, #shipping\\:telephone').blur(function(){
		
        SendCustomer();
		
	});
	
	
	// Check onload if one of the fields is populated, if so it's probably a checkout by a logged in user
	if (jQuery('#billing\\:firstname').length){
    
        SendCustomer();
	}
    
	// Checkout field processing
	jQuery('#login-email').live('blur',function(){
        
        var cartPalLogin = {};
  
		cartPalLogin['sid'] = '86866ce1f3530466f68992efb329ae2d';  
		cartPalLogin['user_id'] = '4f45671e87b942.50957360';
		cartPalLogin['referer'] = location.host;
		cartPalLogin['license_key'] = cartPalLicenseKey;        
		cartPalLogin['billing_email'] = jQuery(this).val();

		jQuery.ajax({
	        url: 'https://www.cartpal.net/integration/magento1.6/save_customer.php',
		    cache: false,
	        dataType: 'jsonp',
		    data: cartPalLogin
        });
		
	});
	
	
	// User selected payment method?
	if (typeof payment != "undefined"){
		var tempFunctionSol = payment.switchMethod;
		payment.switchMethod = function(argsArr) {
			var cartPalPayment = {};
			
            cartPalPayment['sid'] = '86866ce1f3530466f68992efb329ae2d';
			cartPalPayment['user_id'] = '4f45671e87b942.50957360';
			cartPalPayment['referer'] = location.host;
			cartPalPayment['license_key'] = cartPalLicenseKey;
		
			jQuery.ajax({
				url: 'https://www.cartpal.net/integration/magento1.6/set_payment.php',
				cache: false,
				dataType: 'jsonp',
				data: cartPalPayment
			});

		    // Call original function
		    tempFunctionSol(argsArr);
		};
	}
	
	
	// Has user successfully paid for the product?
	// We check the URI for this: /checkout/onepage/success/
	var pathname = window.location.pathname;
	if(pathname == '/checkout/onepage/success/'){
		var cartPalSuccess = {};
        
		cartPalSuccess['sid'] = '86866ce1f3530466f68992efb329ae2d';	
		cartPalSuccess['user_id'] = '4f45671e87b942.50957360';
		cartPalSuccess['referer'] = location.host;
		cartPalSuccess['license_key'] = cartPalLicenseKey;
	
		jQuery.ajax({
			url: 'https://www.cartpal.net/integration/magento1.6/set_success.php',
			cache: false,
			dataType: 'jsonp',
			data: cartPalSuccess
		});
	}

});
