
function showIt(div){
    if (document.getElementById) {  // for Netscape
      myDiv = document.getElementById(div);
         myDiv.style.display = 'block';
    }

    if (document.all) {     // for IE
      eval("myDiv = "+div+";");
         myDiv.style.display = 'block';
    }
}

function hideIt(div){
     if (document.getElementById) {  // for Netscape
       myDiv = document.getElementById(div);
          myDiv.style.display = 'none';
     }

     if (document.all) {     // for IE
       eval("myDiv = "+div+";");
          myDiv.style.display = 'none';
     }
}

function calc(obj) {
	if(obj.checked) addtototal(obj.value);
	else subfromtotal(obj.value);
	
	if (document.getElementById('discount')) {
		//hide discount
		hideIt('show_discount');
		//fix total
		total_now = parseInt(document.getElementById('discount').innerHTML) +  parseInt(document.getElementById('paypalform').amount.value);		
		document.getElementById('total2').innerHTML = total_now.toFixed(2);
		showIt('show_total');
		// reset discount
		document.getElementById('discount').innerHTML = 0;
		document.getElementById('total3').innerHTML = 0;
		// show voucher
		showIt('show_voucher');
	}
	
}

function addtototal(val) {
	var formEl = document.getElementById('paypalform');
	var theObj = document.getElementById('total');
	var theField = formEl.amount;
	var newVal = parseInt(strip_commas(theObj.innerHTML)) + parseInt(val);
	//var resVal = newVal - document.getElementById('discount').innerHTML; // Added to get total after discount
	//theObj.innerHTML = number_format(newVal);
	theObj.innerHTML = newVal;
	theField.value = newVal;
	if(parseInt(strip_commas(theObj.innerHTML)) < 0) theObj.innerHTML = 0;
	if (document.getElementById('total2')) {
		document.getElementById('total2').innerHTML = newVal.toFixed(2);
		//document.getElementById('total3').innerHTML = resVal.toFixed(2); // Added to calculate the total after discount
	}
	//alert(theField.value);
	//paypalform.ftotal.value=theField.value; // Old statement working with IE
	formEl.ftotal.value=theField.value;
}

function calculateDiscount() // To calculate discount on the Admin save form.
{
	//alert(document.getElementById('total').innerHTML);
	document.getElementById('total').innerHTML = paypalform.ftotal.value - paypalform.fdiscount.value;
	paypalform.amount.value = paypalform.ftotal.value - paypalform.fdiscount.value;
	return true;
}

function subfromtotal(val) {
	var formEl = document.getElementById('paypalform');
	var theObj = document.getElementById('total');
	var theField = formEl.amount;
	var newVal = parseInt(strip_commas(theObj.innerHTML)) - parseInt(val);
	theObj.innerHTML = number_format(newVal);
	theField.value = newVal;
	if(parseInt(strip_commas(theObj.innerHTML)) < 0) theObj.innerHTML = 0;
}

function strrev(str) {
	var inp = str.toString();
	var outp = '';
	for (i=0; i<=inp.length; i++) outp = inp.charAt(i) + outp;
	return outp;
} 

function number_format(num) {
	var tn = num.toString();
	var len = tn.length;
	if(len < 4) return tn;
	var rv = '';
	for(i=len-1; i>=0; i--) rv += (i % 3 == 0 && i != len-1 ? ',' : '') + tn.charAt(i);
	return strrev(rv);
}

function strip_commas(str) {
	var rv = '';
	for(i=0; i<str.length; i++) rv += (str.charAt(i) != ',' ? str.charAt(i) : '');
	return parseInt(rv);
}