/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////
WP SIMPLE PAYPAL SHOPPING CART DISCOUNT CODE PATCH v.4.1
Written by Ben Newton
http://www.bnewton.co.uk
////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

// USER-DEFINED VARIABLES //
	var useCurrency = "$"; // currency symbol used
	var decimalPlaces = 2; // number of decimal places to display for currency
	var noCode = "Please enter a discount code"; // message if code is left blank
	var notRecognised = "Discount code is unknown"; // message if code is unknown

/*
DEFINE DISCOUNT CODES, VALUES and PERCENTAGES
theValue = % value of discount
thePercent = % value of discount with % symbol
theRef = # HASTAG code for the discount
*/
	var discountCodes = {
	'WelcomeFriend': {theValue: '25', thePercent: '25%', theRef: '#WelcomeFriend'}, // no comma after last discountCode
	'DISCOUNTCODE2': {theValue: '15', thePercent: '15%', theRef: '#DISCOUNTCODE2'} // no comma after last discountCode
	};

/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU WANT TO ACHIEVE :)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

	function theDiscount() {
		if (document.getElementById("shippingcost")) {
			var shippingValue = parseFloat(replaceChars(document.getElementById("shippingcost").innerHTML));
		}
		else {
			var shippingValue = 0;
		}
		var enteredCode = document.paypal.discountcode.value;
		if(enteredCode == ''){
			alert(noCode);
		return false;
	}
	
	function codeWorkings(EiD) {
		var x = document.paypal["amount_"+EiD];
		var newx = parseFloat(x.value/100*codePercentage);
		var newx = roundNumber(newx, decimalPlaces);
		var subtotal = parseFloat(x.value)-parseFloat(newx);
		x.value = roundNumber(subtotal, decimalPlaces);
		document.getElementById("price_"+EiD).innerHTML = useCurrency+x.value;
		var y = document.paypal["item_name_"+EiD];
		y.value = y.value+" "+codeRef;
	}
		var codeRequest = getCode(enteredCode);
		var discountValue = document.getElementById("discountvalue").innerHTML;
		if (discountValue == "0%") {
				var codePercentage = parseFloat(codeValue);
				var maxNumber = ((document.paypal.elements.length)-11)/4;
				for (i=1; i<=maxNumber; i=i+1) {
				codeWorkings(i);
			}
			document.getElementById('discountvalue').innerHTML = codePercent;
			var totalValue = document.getElementById('totalvalue');
			var priceTag = document.getElementById('pricetag');
			priceTag.innerHTML = "Unit Price";
			var theTotal = replaceChars(totalValue.innerHTML);
			theTotal = parseFloat(theTotal-shippingValue);
			var totalFigure = parseFloat(((theTotal)/100*codePercentage));
			totalFigure = parseFloat(theTotal)-parseFloat(totalFigure);
			totalFigure = roundNumber(totalFigure, decimalPlaces);
			totalValue.innerHTML = useCurrency+parseFloat(totalFigure+shippingValue);
			if (document.getElementById("sub")) {
				var sub = document.getElementById("sub");
				sub.innerHTML = useCurrency+totalFigure;
			}
		}
		
		else if (discountValue != "0%") {
			var currentDiscount = document.getElementById("discountvalue").innerHTML;
			alert("You have already applied a discount code giving you: "+currentDiscount+'\n'+'\n'+"You are trying to use: "+codeRef+'\n'+
			"Which will entitle you to "+codePercent+" discount."+'\n'+'\n'+"To remove the existing discount press F5 or refresh the page.");
			return false;
		}
	}
	
	function roundNumber(num, dec) {
		var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
		return result;
	}
	
	function replaceChars(entry) {
		out = useCurrency;
		add = "";
		temp = entry;
		while (temp.indexOf(out)>-1) {
			pos= temp.indexOf(out);
			temp = (temp.substring(0, pos) +
			temp.substring((pos + out.length), temp.length));
		}
		return temp;
	}
	
	function getCode(enteredCode){
		var code = discountCodes[enteredCode];
		if(code == undefined){
			alert(notRecognised);
			document.paypal.action="";
	}
	
	else {
		codeValue = code.theValue;
		codePercent = code.thePercent;
		codeRef = code.theRef;
	}
	
		return theDiscount;
		
	}

