/*
 * Custom JS Functions
 * author: darius aphonia
 * @copyright: darius aphonia
 * Contact: dario at aphoniastream dot me dot uk
 * Created: 10 Feb 2010
 *
 */
 
 
jQuery(function(){
	

   subTtotalPrice();
   FireFlyContribution();
   

});
 
 
 //function validates authentication form
 function validateLogin(){
 	
 	$('.error').html('').hide();
 	var email = document.getElementById('email').value;
 	var pass = document.getElementById('password').value;
 	
 	
 		
 		if(email == '' || pass == ''){//check if the fields are not missing
 			$('.error').html('Missing email/password!').show();
 			return false;
 			
 		}else if(!ValidateEmail(email)){//validate email
 			
 			$('.error').html('Please enter valid email!').show();
 			return false;
 			
 		}else{
 			
 		   //show progress loader
 		   $('.loader').html('Logging in..').show();
 		$.ajax({
          type : 'POST',
            url : 'mod/customer-authentication.php',
            dataType : 'script',
            data: {
                email : email,
                pass : pass
                
            },
            success : function(data){
              
              if(data == 1){
              	
              	 document.location = 'account.php';
              	 
              }else if(data == 0){
              	
              	$('.error').html('Incorect email/password!').show();
              	$('.loader').html('Logging in..').hide();
              	
              }else{
              	
              	$('.error').html('System error occured!').show();
              	$('.loader').html('Logging in..').hide();
              	
              }
			 
            },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
                $('.loader').html('Logging in..').hide();
                $('.error').html('System error occured!').show();
            }
       });
 		   
 		}
 		
 	
 	
 	
 }
 
 
 //registration form validation function
 function validateRegistration(){
 	 $('#reg-error').html('').hide();
 	 $('#reg-loader').html('').hide();
 	 $('#error-fname').hide();
 	 $('#error-lname').hide();
 	 $('#error-email').hide();
 	 $('#error-pass').hide();
 	 $('#error-confpass').hide();
 	 
 	 
 	//grab bunch of variables
 	 var title = $('#title').val(),
	     fname = $('#first-name').val(),
		 lname = $('#last-name').val(),
		 email = $('#reg-email').val(),
		 pass = $('#reg-password').val(),
		 confpass = $('#conf-password').val();
		 
		 //check if the required fields are present
		 if(fname == '' || lname == '' || email == '' || pass == '' || confpass == ''){
		 	 if(fname == ''){
		 	 	
		 	 	$('#error-fname').show();
		 	 }
		 	 
		 	 if(lname == ''){
		 	 	
		 	 	$('#error-lname').show();
		 	 }
		 	 
		 	 if(email == ''){
		 	 	
		 	 	$('#error-email').show();
		 	 	
		 	 }
		 	 
		 	 if(pass == ''){
		 	 	
		 	 	$('#error-pass').show();
		 	 }
		 	 
		 	 if(confpass == ''){
		 	 	
		 	 	$('#error-confpass').show();
		 	 }
		 	 
		 	 
		 	  
		 	 
		 	 
		 	 $('#reg-error').html('Please correct marked fields!').show();
		 	
		 	 
			 return false;
			 
		 }else if(pass !== confpass){//check if the passwords match
		 	

		 	 	
		 	 	$('#error-confpass').show();
		 	 	$('#error-pass').show();
		 	 	
		 	 	$('#reg-error').html('Passwords don\'t match!').show();
		 	 
			    return false;
			    
		 }else if(pass.length < 6){//check if the password is not too short
		     
		     $('#error-pass').show();
		     $('#reg-error').html('Has to be at least 6 character long!').show();
		     
		 }else if(!ValidateEmail(email)){
		 	 	
		 	 	$('#error-email').show();
		 	 	
		 	 $('#reg-error').html('Please enter valid email!').show();
			 
		 }else{
		 	
		 	//show progress loader
 		   $('#reg-loader').html('Processing..').show();
 		   
 		 $.ajax({
          type : 'POST',
            url : 'mod/customer-registration.php',
            dataType : 'script',
            data: {
                title : title,
                fname: fname,
                lname : lname,
                email : email,
                pass : pass
                
            },
            success : function(data){
               
			   if(data == 0){
			   	   $('#reg-loader').html('').hide();
			   	   $('#reg-error').html('This email is already registered!').show();
			   	   $('#error-email').show();
			   	   
			   }else if(data == 2){
			   	   $('#reg-loader').html('').hide();
			   	   $('#reg-error').html('System error occured!').show();
			   	
			   }else{
			   	
			   	   document.location = 'checkout-address.php'
			   }
			 
            },
            
           error : function(XMLHttpRequest, textStatus, errorThrown) {
                $('#reg-error').html('System error occured!').show();
                $('#reg-loader').html('').hide();
            }
       });
		 	
	}
 	 
 	 
 	return false;
 	
 }
 
 
 function showForgotPassword(){
 	$('#sizeqty').html('').hide();
 	
 	$('#sizeqty').html('<div id="overlay"></div><div id="pass-retriev-popup">'
      +'<div id="close-label" title="close" onclick="closeForgotPassword();"></div>'
      +'<p id="retrieve-loader"></p><p id="retrieve-error"></p>'
      +'<p id="retrieve-success"></p>'
      +'<form method="post" action="">'
      +'<label for="retrieve-email">Registered Email Address</label><br />'
      +'<input type="text" id="retrieve-email" /><br />'
      +'<a href="#" id="submit" onclick="return validateRetrievePass()"></a>'
      +'</form></div>').show();
 	
 	$('#pass-retriev-popup').animate({ 
       width: "370px",
       height: "240px",
       opacity: 1,
       left: ($(document).width()- 370)/2,
       top: "20%"
    
  }, 500 );

 
 	$('#overlay').show();
 	return false;
 }
 
 function closeForgotPassword(){
 	$('#overlay').fadeOut('slow');
 	$('#pass-retriev-popup').animate({ 
       width: "0px",
       height: "0px",
       opacity: 0,
       left: "0",
       top: "680px"
    
  }, 500 );
  setTimeout("$('#sizeqty').html('').hide();", 500);
 }
 
 
 function validateRetrievePass(){
 	 $('#retrieve-loader').html('').hide();
	 $('#retrieve-error').html('').hide();
 	var email = $('#retrieve-email').val();
 	
 	if(email == ''){
 		$('#retrieve-error').html('Please enter email first!').show();
 	
	}else if(!ValidateEmail(email)){
		
		$('#retrieve-error').html('Please enter valid email!').show();
		
	}else{
		
		$('#retrieve-loader').html('Processing...').show();
		$.ajax({
          type : 'POST',
            url : 'mod/customer-pass-retrive.php',
            dataType : 'script',
            data: {
                email : email
                
            },
            success : function(data){
            	
            	if(data == 1){
            		
            		$('#retrieve-success').html('Email with new password has been sent!').show();
			        $('#retrieve-loader').html('').hide();
            		
            	}else if(data == 0){
            		
            		$('#retrieve-error').html('Email doesn\'t exist!').show();
			        $('#retrieve-loader').html('').hide();
            		
            		
            	}else{
            		
            		$('#retrieve-error').html('System error occured!').show();
			        $('#retrieve-loader').html('').hide();
            		
            	}
               
               
			 
            },
            
           error : function(XMLHttpRequest, textStatus, errorThrown) {
                $('#retrieve-error').html('System error occured!').show();
                $('#retrieve-loader').html('').hide();
            }
       });
		
	}
	
	return false;
 }
 
//email validation function
function ValidateEmail(email){
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if(reg.test(email) == false){
	
	
	   return false;
	
	}else{
	
	   return true;
	}
}


function copyShippingAddress(){
	
	if(document.getElementById('copy-address').checked == true){
	    
	  document.getElementById('billing-title').value = document.getElementById('shipping-title').value;
	  document.getElementById('billing-first-name').value = document.getElementById('shipping-first-name').value;
	  document.getElementById('billing-last-name').value = document.getElementById('shipping-last-name').value;
	  document.getElementById('billing-address1').value = document.getElementById('shipping-address1').value;
	  document.getElementById('billing-address2').value = document.getElementById('shipping-address2').value;
	  document.getElementById('billing-city').value = document.getElementById('shipping-city').value;
	  document.getElementById('billing-postcode').value = document.getElementById('shipping-postcode').value;
	  document.getElementById('billing-county').value = document.getElementById('shipping-county').value;
	  document.getElementById('billing-country').value = document.getElementById('shipping-country').value;
	  document.getElementById('billing-phone').value = document.getElementById('shipping-phone').value;
	  
	 
	  
	
	}else{
		
	  document.getElementById('billing-title').value = "";
	  document.getElementById('billing-first-name').value = "";
	  document.getElementById('billing-last-name').value = "";
	  document.getElementById('billing-address1').value = "";
	  document.getElementById('billing-address2').value = "";
	  document.getElementById('billing-city').value = "";
	  document.getElementById('billing-postcode').value = "";
	  document.getElementById('billing-county').value = "";
	  document.getElementById('billing-country').value = "";
	  document.getElementById('billing-phone').value = "";
	  
	  
		
	}
}


//shipping billing address form validation function 
function validateAddress(){
	
	$('#error').html('').hide();
	$('.error-icon').hide();
	
	var bftitle = document.getElementById('billing-title').value,
	    sftitle = document.getElementById('shipping-title').value,
	    bfname = document.getElementById('billing-first-name').value, 
		sfname = document.getElementById('shipping-first-name').value,
	    blname = document.getElementById('billing-last-name').value, 
		slname = document.getElementById('shipping-last-name').value,
	    baddress1 = document.getElementById('billing-address1').value,
		saddres1 = document.getElementById('shipping-address1').value,
	    baddress2 = document.getElementById('billing-address2').value,
		saddress2 = document.getElementById('shipping-address2').value,
	    bcity = document.getElementById('billing-city').value,
		scitty = document.getElementById('shipping-city').value,
		bpostcode = document.getElementById('billing-postcode').value,
		spostcode = document.getElementById('shipping-postcode').value,
	    bcounty = document.getElementById('billing-county').value,
		scounty = document.getElementById('shipping-county').value,
	    bcountry = document.getElementById('billing-country').value,
		scountry = document.getElementById('shipping-country').value,
	    bphone = document.getElementById('billing-phone').value,
		sphone = document.getElementById('shipping-phone').value;
		
	//check firs if the required fields are present
	if(bfname == '' || sfname == '' || blname == '' || slname == '' || baddress1 == '' || bcity == '' || bpostcode == '' || spostcode == '' || scitty == ''|| bcountry == '' || scountry == '' || bphone == '' || sphone == ''){
		
		if(sfname == ''){
			$('#error-shipping-fname').show();
			
		}
		
		if(bfname == ''){
			$('#error-billing-fname').show();
		
		}
		
		if(slname == ''){
			$('#error-shipping-last-name').show();
		
		}
		
		if(blname == ''){
			$('#error-billing-last-name').show();
		
		}
		
		if(saddres1 == ''){
			$('#error-shipping-address1').show();
		
		}
		
		if(baddress1 == ''){
			$('#error-billing-address1').show();
		
		}
		
		if(scitty == ''){
			$('#error-shipping-city').show();
		
		}
		
		if(bcity == ''){
			$('#error-billing-city').show();
		
		}
		
		if(spostcode == ''){
			$('#error-shipping-postcode').show();
		
		}
		
		if(bpostcode == ''){
			$('#error-billing-postcode').show();
		
		}
		
		if(scountry == 0){
			$('#error-shipping-country').show();
		
		}
		
		if(bcountry == 0){
			$('#error-billing-country').show();
		
		}
		
		if(sphone == ''){
			$('#error-shipping-phone').show();
		
		}
		
		if(bphone == ''){
			$('#error-billing-phone').show();
		
		}
		
		
		$('#error').html('Please correct marked fields!').show();
		return false;
		
	}else if(!IsNumeric(sphone)){//check if the phone number is numeric value
		
		
			$('#error-shipping-phone').show();
			
			$('#error').html('Phone number doesn\'t seem to be valid!').show();
		    return false;
		
		
	}else if(!IsNumeric(bphone)){
		   
		   $('#error-billing-phone').show();
			
			$('#error').html('Phone number doesn\'t seem to be valid!').show();
		    return false;
		
		
		
	}else{
		
		//show progress loader
		$('#address-loader').html('Processing...').show();
		
		$.ajax({
          type : 'POST',
            url : 'mod/process-address.php',
            dataType : 'script',
            data: {
                    bftitle : bftitle,
				    sftitle : sftitle,
				    bfname : bfname, 
					sfname : sfname,
				    blname : blname, 
					slname : slname,
				    baddress1 : baddress1,
					saddres1 : saddres1,
				    baddress2 : baddress2,
					saddress2 : saddress2,
				    bcity : bcity,
					scitty: scitty,
					bpostcode : bpostcode,
					spostcode : spostcode,
				    bcounty : bcounty,
					scounty : scounty,
				    bcountry : bcountry,
					scountry : scountry,
				    bphone : bphone,
					sphone : sphone
                
            },
            success : function(data){
              
              if(data == 1){
              	
              	document.location = 'order-summary.php';
            
              	 
              }else{
              	 

              	 $('#address-loader').html('').hide();
                 $('#error').html('System error occured!').show();
              	
              }
			 
            },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
                $('#address-loader').html('Logging in..').hide();
                $('#error').html('System error occured!').show();
            }
       });
		
	}
	
	
	return false;
}



//function that check if the input is numeric
function IsNumeric(sText){
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++){
	    Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1){
		   IsNumber = false;
		}
	}
	  return IsNumber;
}


//function that sets shipping on order summary page
function functionSetShipping(shipping, shipping_id){
	
	switch (shipping){
		
		case 'standard-uk':
		$('#standard-uk').css({
			'background' : 'transparent url(img/layout/selected.gif) no-repeat',
			'cursor' : 'default'
		});
		
		$('#express-uk').css({
			'background' : 'transparent url(img/layout/not-selected.gif) no-repeat',
			'cursor' : 'pointer'
		});
		
		$('#eu').css({
			'background' : 'transparent url(img/layout/not-selected.gif) no-repeat',
			'cursor' : 'pointer'
		});
		
		$('#international').css({
			'background' : 'transparent url(img/layout/not-selected.gif) no-repeat',
			'cursor' : 'pointer'
		});
		
		$('input[name=shipping]').val('standard-uk');
		
		break;
		
		case 'express-uk':
		$('#express-uk').css({
			'background' : 'transparent url(img/layout/selected.gif) no-repeat',
			'cursor' : 'default'
		});
		
		$('#standard-uk').css({
			'background' : 'transparent url(img/layout/not-selected.gif) no-repeat',
			'cursor' : 'pointer'
		});
		
		$('#eu').css({
			'background' : 'transparent url(img/layout/not-selected.gif) no-repeat',
			'cursor' : 'pointer'
		});
		
		$('#international').css({
			'background' : 'transparent url(img/layout/not-selected.gif) no-repeat',
			'cursor' : 'pointer'
		});
		
			$('input[name=shipping]').val('express-uk');
		break;
		
		case 'eu':
		$('#eu').css({
			'background' : 'transparent url(img/layout/selected.gif) no-repeat',
			'cursor' : 'default'
		});
		
		$('#standard-uk').css({
			'background' : 'transparent url(img/layout/not-selected.gif) no-repeat',
			'cursor' : 'pointer'
		});
		
		$('#express-uk').css({
			'background' : 'transparent url(img/layout/not-selected.gif) no-repeat',
			'cursor' : 'pointer'
		});
		
		$('#international').css({
			'background' : 'transparent url(img/layout/not-selected.gif) no-repeat',
			'cursor' : 'pointer'
		});
		
			$('input[name=shipping]').val('eu');
		break;
		
		case 'international':
		$('#international').css({
			'background' : 'transparent url(img/layout/selected.gif) no-repeat',
			'cursor' : 'default'
		});
		
		$('#standard-uk').css({
			'background' : 'transparent url(img/layout/not-selected.gif) no-repeat',
			'cursor' : 'pointer'
		});
		
		$('#express-uk').css({
			'background' : 'transparent url(img/layout/not-selected.gif) no-repeat',
			'cursor' : 'pointer'
		});
		
		$('#eu').css({
			'background' : 'transparent url(img/layout/not-selected.gif) no-repeat',
			'cursor' : 'pointer'
		});
		
			$('input[name=shipping]').val('international');
		break;
		
	}
	
	//show loaders
    $('#shipping-price h2').html('<span></span>');
    $('#total-price h2').html('<span></span>');
	$('#shipping-price h2 span').addClass('shipping-loader');
	$('#total-price h2 span').addClass('shipping-loader');

	
	$.ajax({
          type : 'POST',
            url : 'mod/set-shipping.php',
           dataType : 'script',
            data: {
                shipping_id : shipping_id
            },
            success : function(data){
               $('#shipping-price h2').html('');
               $('#total-price h2').html('');
               $('#shipping-val').val(data);
               
               total = totalPrice(data);
               $('#total-price h2').html('&pound;'+formatAsMoney(total));
               $('input[name=amount]').val(formatAsMoney(total));
			   $('#shipping-price h2').html('&pound;'+formatAsMoney(data));
			   
			  
            },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("System error occured! Appologies for any inconvenience");
                $('#shipping-price h2').html('');
                $('#total-price h2').html('');
            }
       });
}


//function that brings up the add to cart popup
function ShoAddtoCartPopup(pid){
	
	
	$.ajax({
          type : 'POST',
            url : 'mod/size-popup.php',
           dataType : 'script',
            data: {
                pid : pid
            },
            success : function(data){
                $('#sizeqty').html(data);
               	$('#overlay').show();
				$('#add-to-cart-popup').animate({ 
			       width: "370px",
			       height: "240px",
			       opacity: 1,
			       left: ($(window).width() - 370)/2,
                   top: "20%"
			    
			  }, 500 );
			  
			  $('.size-dropdown').textdropdown();
            },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("System error occured! Appologies for any inconvenience")
            }
       });
   
   
	


  return false;
}

function closeAddTocart(){
	$('#overlay').hide();
	$('#add-to-cart-popup').animate({ 
       width: "0px",
       height: "0px",
       opacity: 0,
       left: "0",
       top: "880px"
    
  }, 500 );
  
  setTimeout("$('#add-to-cart-popup').html('');", 500);

}

function placeOrder(cid){
	
	
	 var shipping = $('#shipping-val').val();
	 
	 if(shipping == ''){
	 	
	 	alert("Please specify shipping method!");
	 	
	 }else{
	 	
	 	$('#sizeqty').html('<div id="overlay"></div><div style="left:42%;top:40%" id="update-loader">'
		+'<h3>Processing Order...</h3></div>');
	 	
	 	$.ajax({
          type : 'POST',
            url : 'mod/process-order.php',
           dataType : 'script',
            data: {
                cid : cid,
                shipping : shipping
                
            },
            success : function(data){
            	
                if(data == '0'){
                	
                	alert("System error occured! Appologies for any inconvenience.");
                	$('#sizeqty').html('');
                	
                	
                }else{
                	
                	$('input[name=cartId]').val(data);
                	submitWpForm();
                }
			  
            },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
           	    $('#sizeqty').html('');
                alert("System error occured! Appologies for any inconvenience")
            }
       });
	 	
	 	
	 	
	 	
	 }
	  
	  return false;
}

function submitWpForm(){
	
	    $('#sizeqty').html('<div id="overlay"></div><div style="width: 380px; left:35%;top:40%" id="update-loader">'
		+'<h3 style="width: 280px">Redirecting to Payment Gateways...</h3></div>');
		$('#overlay').show().css('background-color', 'transparent');
	 	$('form').submit();
}


function addToCart(pid){
	
	$('#error-qty').html('').hide();
	$('#error-size').html('').hide();
	$('#retrieve-error').html('').hide();
	
    var size = $('#item-size').val();
    var qty = $('#item-qty').val();
    
    if(qty == '' || size == ''){
    	
    	if(size == ''){
    		
    		$('#error-size').show();
    		
    		
    	}
    	
    	if(qty == ''){
    		
    		$('#error-qty').show();
    		
    		
    	   }
    	
    	$('#retrieve-error').html('Please correct marked fields!').css({
    			'margin' : '30px 0 0 105px',
    			'display' : 'block'
    			
    		});
    		
    }else if(!IsNumeric(qty) || qty == 0){
    	
    	   $('#error-qty').show();
    	   
    	   $('#retrieve-error').html('Please correct marked fields!').css({
    			'margin' : '30px 0 0 105px',
    			'display' : 'block'
    			
    		});
    	
    	
    }else{
    	
    	//show progress loader
    	$('#retrieve-loader').html('Adding to cart...').show().css('margin', '30px 0 0 125px').css('height', '16px');
    	$.ajax({
          type : 'POST',
            url : 'mod/add-to-cart.php',
           dataType : 'script',
            data: {
                pid : pid,
                qty : qty,
				size : size
            },
            success : function(data){
            	
             if(data == ''){
                
				  closeAddTocart();
				  
				  onAdd(pid);
			  
			}else{
				
				$('#error-qty').show();
				$('#retrieve-loader').hide();
    	   
	    	       $('#retrieve-error').html(data).css({
	    			'margin' : '30px 0 0 100px',
	    			'display' : 'block'
    			
    		  });
				 
			}
			  
            },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("System error occured! Appologies for any inconvenience")
            }
       });
    	
    	
    }
    
   return false;
}


function onAdd(pid){
		$.ajax({
          type : 'POST',
            url : 'mod/on-add.php',
           dataType : 'script',
            data: {
                pid : pid
                
            },
            success : function(data){
            	
            	//converts encoded £ sign
            	data = data.replace('%C2%A3','£');
                
			  $('#shopping-cart a').html(data);
			  
            },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("System error occured! Appologies for any inconvenience")
            }
       });
}


function refreshCart(pid,action,cid){
	$('#error-qty'+cid).hide();
	 var size = '';
	 var qty = '';
	 size = $('.sizes'+cid).val();
	 qty = $('.qty'+cid).val();

	$('#sizeqty').html('');
	if(cid != '' && action != '' && size != '' && qty != ''){
		
		
		if(!IsNumeric(qty) || qty == 0){//check if the quantity value is numeric
			
				
			$('#error-qty'+cid).show();
			
			return false;
		}
	
		$('#sizeqty').html('<div id="overlay"></div><div id="update-loader"><h3>Updating Cart...</h3></div>');
		$('#overlay').show().css('background-color', 'transparent');
		
		$.ajax({
          type : 'POST',
            url : 'mod/cart-actions.php',
            dataType : 'script',
            data: {
                cid : cid,
                pid : pid,
                action : action,
                size : size,
                qty : qty
                
            },
            success : function(data){
            if(data.substr(0,108) === 'I\'m sorry but you\'re trying to add more items than there are in the stock. The current stock of this item is'){
            	
            	  alert(data);
            	  $('#overlay').hide();
				  $('#update-loader').fadeOut();
				  $('#error-qty'+cid).show();
            	
            }else{
                
				  $('#cart').html(data);
				  $('#overlay').hide();
				  $('#update-loader').fadeOut();
				
				 
				  onAdd(pid);
				  
				 $('.shopping-cart-sizes').sSelect();
			 
			 }
			 
			 FireFlyContribution();
			  return false;
			  
			 
            },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("System error occured! Appologies for any inconvenience");
                $('#sizeqty').html('');
            }
       });
		
	}else{
		
		if(qty == ''){
			
			$('#error-qty'+cid).show();
			
			return false;
		}else{
			
			
		
		
		
		        alert("System error occured! Appologies for any inconvenience");
                $('#sizeqty').html('');
                
		}
	}
	
	
	return false;
}


function removeCartItem(cid, pid){
	
	var action = "remove";
	var conf = confirm("Are you sure you want to remove this item from your shopping cart?");
  
  if(cid != '' && pid != ''){
  	
	if(conf == true){
    
    	$('#sizeqty').html('<div id="overlay"></div><div id="update-loader"><h3>Updating Cart...</h3></div>');
		$('#overlay').show().css('background-color', 'transparent');
		
		$.ajax({
          type : 'POST',
            url : 'mod/cart-actions.php',
            dataType : 'script',
            data: {
                cid : cid,
                action : action
                
            },
            success : function(data){
              
              if(data !== ''){
                
				  $('#cart').html(data);
				  $('#overlay').hide();
				  $('#update-loader').fadeOut();
				 
				  onAdd(pid);
				  
				 $('.shopping-cart-sizes').sSelect();
			  
			  }else{
			  	
			  	  document.location = 'shopping-cart.php';
			  	
			  }
			  
			  FireFlyContribution();
			 
            },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("System error occured! Appologies for any inconvenience");
                $('#update-loader').fadeOut();
            }
       });
	}
	
 }else{
 	
 	
 	 alert("System error occured! Appologies for any inconvenience");
 	
 }
 
 return false;
	
}

function subTtotalPrice(){
	
	var subtotal = document.getElementById('subtotal');

	
	if(subtotal){
	
		
		$('#total-price h2').html('&pound;'+formatAsMoney(subtotal.value));
		  
	}
}


function totalPrice(shipping_price){
	
	var subtotal = parseFloat(document.getElementById('subtotal').value);
	var total = parseFloat(shipping_price) + subtotal;
	
	return total;
}


function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}


function FireFlyContribution(){

  $('#shopping-cart-content a#proceed').click(function(){
     
	  
	 if(document.getElementById('ffid') && !document.getElementById('donated')){
	  var ffqty = [];
	  
	  $('input[name=ffqty]').each(function(){
	     
		  var sffqty = $(this).val();
		  ffqty.push(parseInt(sffqty));
		  
		  
	  
	  });
	  
	  var ffqtys = ffqty.join(",");
	  var qty = ffqtys.split(",");
	  var sizes = qty.length;
	  
	  if(sizes > 1){
		  for(var i = 0; i < sizes; i++){
			  
			  var q = parseInt(qty[i]) + parseInt(qty[i -1])
			
		  }
	  
	 }else{
	   
	    var q = $('input[name=ffqty]').val();
	    
	 }
	  
	
	  
	  $('#sizeqty').html('<div id="overlay"></div><div id="add-to-cart-popup">'
	               +'<div id="close-label" title="close" onclick="closeAddTocart();"></div>'
				   +'<p class="ffly" style="padding-bottom: 34px">Make a voluntary contribution '
				   +'of £20 which will go directly to the charity.You can then buy Fireflies Commemorative '
				   +'jersey for promotional price of £25 .</p><p id="donation-loader">Processing Donation...</p>'
				   +'<table id="donate"><tr><td><input onclick="processDonation('+q+')" type="radio"  name="donation" /></td>'
				   +'<td><span class="ffly">I understand and agree to make a &pound;20 donation to Leuka for each jersey I buy</span></td></tr>'
				   +'<td><input type="radio" onclick="NoDonation();"  name="donation" value="disagree"/></td>'
				   +'<td><span class="ffly" style="color:#da2520">I disagree to make any donation</span></td></tr></table>'
				   +'<div class="leuka-popup"></div><div class="pushme"></div>'
				   +'</div>');
				   
	          $('#overlay').show(); 
				$('#add-to-cart-popup').animate({ 
			       width: "370px", 
			       height: "310px",
			       opacity: 1,
			       left: ($(window).width() - 370)/2,
                   top: "20%"
			     
			  }, 500 ); 
			  
			  
			 
			  
			  
	  
	 }else{
	 
	   document.location = "customer-login.php";
	 }
	 
	 
  });

}



function NoDonation(){
  
  closeAddTocart();
  document.location = 'customer-login.php';

}


function processDonation(qty){

  $('#donation-loader').fadeIn('slow');
  
  
  $.ajax({
          type : 'POST',
            url : 'mod/process-donation.php',
            dataType : 'script',
            data: {
                
				qty : qty
                
            },
            success : function(data){
			  if(data == 1){
				  closeAddTocart();
				  document.location = 'customer-login.php';
			  }else{
			    
				  alert("System error occured! Appologies for any inconvenience");
                  closeAddTocart();
			  }
			 
            },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("System error occured! Appologies for any inconvenience");
                closeAddTocart();
            }
       });
  
}

function removeDonation(ssid){
       $('#sizeqty').html('');
	   
	   var r = confirm("Are you sure you wish to remove contribution to Leuka?");
	   
	  if(r == true){
   
		$('#sizeqty').html('<div id="overlay"></div><div id="update-loader"><h3>Updating Cart...</h3></div>');
		$('#overlay').show().css('background-color', 'transparent');
		
		
		 $.ajax({
          type : 'POST',
            url : 'mod/remove-donation.php',
            dataType : 'script',
            data: {
                
				ssid : ssid
				
                
            },
            success : function(data){
			  
			  
			    document.location = 'customer-login.php';
			  
			 
            },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
                 alert("System error occured! Appologies for any inconvenience");
				 $('#sizeqty').html('');
				 $('#sizeqty').fadeOut('slow');
                
            }
       });
	  
	  
	  return true;
	}
		

}



 


