/*---------------------------------------------------------------
 * PRODUCT.JS : Manages packages related calculations and events
 *---------------------------------------------------------------
 */
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,n,m)
{
	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');
	}
	// Calculating maximum NUM LINKS
	var sNumList = document.getElementById('paypalform').num_links_list.value;
	var aNumList = sNumList.split(",");
	var iMax     = aNumList.length;
	var i;
	var iNumMax  = 0;
	var sListPackages = '';
	var fTotalOff = 0;
	var oPriceOff;
	var fTotalDiscountedPrice = 0;
	var oDiscountedPrice;
	var iTotalLinks = 0;
	for (i = 0; i < iMax; i++)
	{
		var j = i+1;
		var sFormElement = eval('document.getElementById(\'paypalform\').product'+j);
		var sSelectIndex = sFormElement.checked;
		if (sSelectIndex == true)
		{
			//alert ('i ='+i+' aNumList[i] = '+aNumList[i]);
			iNumMax = (parseInt(iNumMax) > parseInt(aNumList[i])) ? iNumMax : aNumList[i];
			id= 'sPackageNo' + j;
			//alert(eval(document.getElementById(id)).value);
			
			sLinkId = 'sTotalLink' + j;
			if(document.getElementById(sLinkId))
			{
				iTotalLinks = iTotalLinks + ( eval(document.getElementById(sLinkId)).value * 1);
			}
			//alert(iTotalLinks);
			
			if (sListPackages == '') sListPackages += eval(document.getElementById(id)).value;
			else sListPackages += ','+ eval(document.getElementById(id)).value;
			//alert ('iNumMax = '+iNumMax);
			oPriceOff = eval('document.getElementById(\'paypalform\').hidPriceOff'+j);
			fTotalOff = fTotalOff + eval(oPriceOff.value);
			oDiscountedPrice = eval('document.getElementById(\'paypalform\').hidDiscountedPrice'+j);
			fTotalDiscountedPrice += eval(oDiscountedPrice.value);
		}
	}
	document.getElementById('paypalform').num_links.value = iNumMax;
	document.getElementById('paypalform').packages.value = sListPackages;
	if (fTotalOff > 0) document.getElementById('totaloff').innerHTML = '$'+fTotalOff+' Off';
	else document.getElementById('totaloff').innerHTML = '';
	document.getElementById('paypalform').ftotal.value = fTotalDiscountedPrice;
	document.getElementById('paypalform').amount.value = fTotalDiscountedPrice;
	document.getElementById('total').innerHTML = fTotalDiscountedPrice;
	if(document.getElementById('totalLinks'))
	{
		document.getElementById('totalLinks').innerHTML = iTotalLinks;
	}
	//alert(fTotalDiscountedPrice);
	return true;
}

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);
	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);
	}
	formEl.ftotal.value=theField.value;
}

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

function calculateDiscount2() // To calculate discount on the Admin at Add package to Submission Page.
{
	var val2 = document.getElementById('paypalform').amount.value;
	var val3 = document.getElementById('paypalform').fdiscount.value;
	var val4 = parseInt(val2) - parseInt(val3);
	document.getElementById('total').innerHTML = val4;
	document.getElementById('paypalform').amount.value = val4;
}

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;
	formEl.ftotal.value=theField.value;
}

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);
}