function deleteSelectedItemsFromCart(){

	itemsToDelete = ''; 

	// Get list of lineitem numbers and convert to array
	var valueArray = document.getElementById('lineItemList').value.split(",");
	// Loop over array length (uses < instead of <= because arrays are 0 based)

for(var i=0; i < valueArray.length; i++){

		// Is this checkbox checked?
		if (document.getElementById('delete'+valueArray[i]).checked){

			if (itemsToDelete.length > 0){
				
				// Prepend comma
				if (i < (valueArray.length)){
					itemsToDelete = itemsToDelete + ',';
				}
			}

			// Append value of current checked checkbox
			itemsToDelete = itemsToDelete + document.getElementById('delete'+valueArray[i]).value;
			// If this is not the last item, append comma 
			// The minus 1 is to account for the zero based array.
		/*	if (i < (valueArray.length-1)){
				itemsToDelete = itemsToDelete + ',';
			}*/

		}
	
	}

	if(itemsToDelete != ''){
		
		if (confirm("Do you wish to remove the selected items from the cart?")){
		
	        URL = 'index.cfm?fuseaction=bas.RemoveItemFromBasket&ln=' + itemsToDelete;
		    location.replace(URL);	
			return;
		}
	}
	else{
		alert("You have not selected any items to be deleted");	
	}
}

function validateQtyField(linenumber){

	if (IsNumeric(document.getElementById('qty_'+linenumber).value)) {
	}else{
		// restore original value from hidden field
		document.getElementById('qty_'+linenumber).value = document.getElementById('prevQty'+linenumber).value;
	}
	

}


function UpdateQTY(basketid,linenumber,qty,prodid){



if (IsNumeric(document.getElementById('qty_'+linenumber).value)) {
	}else{
		// restore original value from hidden field
		document.getElementById('qty_'+linenumber).value = document.getElementById('prevQty'+linenumber).value;
		return;
	}


// If value is zero, ask user if they wish to remove item from cart
if (qty == 0){
	if (confirm("Do you wish to remove this item from the cart?")){
	
		URL = 'index.cfm?fuseaction=bas.RemoveItemFromBasket&ln=' + linenumber;
	    location.replace(URL);	
		return;
	
	}
	else{ 
			// restore original value from hidden field
			document.getElementById('qty_'+linenumber).value = document.getElementById('prevQty'+linenumber).value;
			return;
	}
	

}




// Update previous qty field to current value
document.getElementById('prevQty'+linenumber).value = qty;


/////// UPDATE BASKET ////////////////
// The server-side script
var url = "ajax_functions.cfm"; 
//alert(basketid + "," + linenumber + "," + qty + "," + prodid);
// Update basket using AJAX call
http.open("GET", url + "?action=UpdateQTY" + "&basketid=" + basketid + "&linenumber=" + linenumber + "&qty=" + qty + "&prodid=" + prodid, true);

http.onreadystatechange = basketUpdated;

http.send(null);


//////////// DISPLAY UPDATE TO USER //////////////
// subtotal
/*var st = 0;
st = qty * cost;

// Update Subtotal Display for this line	
document.getElementById('subtotal'+linenumber).innerHTML = dollarFormat(st);	
*/

	
}

function basketUpdated(){

 if (http.readyState == 4) {
	
	results = http.responseText.split(",");
 //alert(http.status);
document.getElementById('subtotal'+results[1]).innerHTML = dollarFormat(results[0]);	
	new Effect.Pulsate(document.getElementById('subtotal'+results[1]));
	updateBasketTotal();
  }

	
	
}

function updateSubtotal(r,a){
	// r= row, a=amount
	q = document.getElementById('qty_'+r).value; 
	document.getElementById('subtotal'+r).innerHTML = dollarFormat(a * q) ;	

}


function updateBasketTotal(){

	bTotal = 0;

	dis = document.getElementById('DiscountAmount').value;
	don = parseInt(document.getElementById('Donation').value);

	
	// Get list of lineitem numbers and convert to array
	var valueArray = document.getElementById('lineItemList').value.split(",");
		// Loop over array length (uses < instead of <= because arrays are 0 based)
		for(var i=0; i < valueArray.length; i++){
	    
		//do something by accessing valueArray[i]; *equal to ListGetAt in CF*

		bTotal = bTotal + parseFloat(document.getElementById('subtotal'+valueArray[i]).innerHTML.substring(1));
		
		}

		// Add donation
		bTotal = bTotal + don;

		// Sub discount
		bTotal = bTotal - dis;

	document.getElementById('basketTotal').innerHTML = dollarFormat(bTotal);
    new Effect.Pulsate(document.getElementById('basketTotal'));
	updateDonationText();
	
	

}


function UpdateDonation(basketid){

amount = document.getElementById('Donation').value;

if (IsNumeric(amount)) {
	}else{
		alert('Please make sure your donation amount is a number. (No commas or decimals)');
		return;
	}


// If value is zero, ask user if they wish to remove item from cart
/*if (amount == 0){
		return;
}*/



/////// UPDATE BASKET ////////////////
// The server-side script
var url = "ajax_functions.cfm"; 

// Update basket using AJAX call
http.open("GET", url + "?action=UpdateDonation" + "&basketid=" + basketid + "&donation=" + amount, true);

http.onreadystatechange = donationUpdated;

http.send(null);

}

function donationUpdated(){

 if (http.readyState == 4) {

	results = http.responseText;
	document.getElementById('basketDonation').innerHTML = dollarFormat(results);	
	new Effect.Pulsate(document.getElementById('basketDonation'));
	
	updateBasketTotal();
  }

	
	
}

function updateDonationText(){
	// Adjust message based on value of donation
	if (document.getElementById('Donation').value > 0){
		document.getElementById('YesDonation').style.display = 'block';
		document.getElementById('NoDonation').style.display = 'none';
		document.getElementById('DonationButton').value = 'Update Donation';
		new Effect.Highlight(document.getElementById('YesDonation'), {startcolor: '#ffffff', endcolor: '#bbbbdd'});
	}
	else{
		document.getElementById('YesDonation').style.display = 'none';
		document.getElementById('NoDonation').style.display = 'block';
		document.getElementById('DonationButton').value = 'Add Donation';
		new Effect.Highlight(document.getElementById('NoDonation'), {startcolor: '#ffffff', endcolor: '#bbbbdd'});	}
	
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
// Stock function to convert to money display format
function dollarFormat(myValue) {
// Save original input string
var original_value = myValue
// Get float value
var amount = parseFloat(myValue)
// Return unmodified input if problem with the parse
if (isNaN (amount)) {
   return original_value
}
// Convert amount in pennies, rounded to the penny
amount = Math.round (100 * amount)
// Convert amount to string and pad with leading zeros if necessary
var string
if (amount < 10) {
   string = "00" + amount
  }
else {
       if (amount < 100) {
          string = "0" + amount
      }
     else {
            string = "" + amount
           }
      }
// Concatenate dollar sign with string
string = "$" + string
// Insert decimal point before last two digits
string = string.substring(0, string.length - 2) +"." + string.substring(string.length - 2, string.length)
// Return dollar format string
return string
}


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 isValidCreditCard(ccnum) {
/*   if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MC") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Disc") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "AmEx") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "Diners") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(ccnum)) return false;*/
   // Remove all dashes for the checksum checks to eliminate negative numbers
   ccnum = ccnum.split("-").join("");
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}
