function roundDecimal(iNumber)
{
	iNumber = '' + iNumber;
	iIndex = iNumber.indexOf('.');
	if(iIndex >= 0)
	{
		iEnd = eval(iIndex) + 2;
		iNumber = iNumber.substring(0,iEnd);
	}
	return iNumber;
}
function isInteger(s)
{  
    var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9")) && (c != '-')) 
        {
	        if(c != '.')
	        	return false;
        }
    }
    // All characters are numbers and for decimal '.' character is allowed.
   return true;
}


function roundNumber(rnum) 
{
	var rlength = 2; // The number of decimal places to round to
	if (rnum > 8191 && rnum < 10485) 
	{
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		newnumber = newnumber+5000;
	} 
	else 
	{
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	return newnumber;
}


function perHourPayment(n,iIndex)
{
	var theform = document.getElementById('paypalform');
	sTxtHourName = 'txtHour' + iIndex; 
	var iCurVal = document.getElementById(sTxtHourName).value;
	if(!isInteger(iCurVal))
	{
		alert('Hour Field must contain only numeric value!');
		document.getElementById(sTxtHourName).focus;
	}
	else
	{
		sUnitName = 'unitPrice' + iIndex;
		var iUnitPrice = document.getElementById(sUnitName).value;
		var iFinalPrice = Math.round(iUnitPrice * iCurVal);    //roundNumber(iUnitPrice * iCurVal);
		sTotPriceName = 'totPrice' + iIndex; 
		document.getElementById(sTotPriceName).innerHTML = iFinalPrice;
		sText = iFinalPrice.toString();
		iLength = eval(sText.length);
		sPriceValName = 'hidDiscountedPrice' + iIndex;
		document.getElementById(sPriceValName).value = iFinalPrice;
		sProductName = 'product' + iIndex;
		calcHourPrice('x',n,iIndex);
	}
}

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 calcHourPrice(obj,n,m)
{
	if (document.getElementById('discount'))
	{
		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 sFinalPck = '';
	var fTotalOff = 0;
	var oPriceOff;
	var fTotalDiscountedPrice = 0;
	var oDiscountedPrice;
	var iTotalLinks = 0;
	var iTotalSites = 0;
	var iVouPercent = 0;
    var iVouAmount = 0;
    var iVouLabour = 0;
    var iAmountLeft = 0;
    var iTemp = 0; 
    var sVoucherLabour = 0;
    var iVoucherPrice = 0;
    var sVoucherPrice = 0;
    var iVoucherPriceTemp = 0;
    
	
	for (i = 0; i < iMax; i++)
	{
		var j = i+1;
		iFinal1 = i;		
		iFinal2 = j;
		id1= 'sPackageNo' + j;
		id2 = 'hidDisplayType' + j;
		iDisplayType = eval(document.getElementById(id2)).value;
		if(iDisplayType == 1)
		{
			iCurPackageVal = eval(document.getElementById(id1)).value;
			var sFormElement = eval('document.getElementById(\'paypalform\').product'+iCurPackageVal);
		}
		else
		{
			id3 = 'hidGroupId' + j;
			iGroupId = eval(document.getElementById(id3)).value;
			id3 = 'hidSize' + iGroupId;
			iSize = eval(document.getElementById(id3)).value;
			if(iSize == 1)
				var sFormElement = eval('document.getElementById(\'paypalform\').products'+iGroupId);
			else
			{
				bFound = false;
				for(iCt=0;iCt<iSize;iCt++)
				{
					if(!bFound)
					{
						sFormElement = eval('document.getElementById(\'paypalform\').products'+iGroupId+'['+iCt+']');
						if(sFormElement.checked)
						{
							bFound = true;
							iFinal1 = i;		
							iFinal2 = j;
						}
					}
					i++;j++;
				}
				if(iCt == iSize)
				{ i--;j--; }
			}
		}
		var sSelectIndex = sFormElement.checked;
		
		if (sSelectIndex == true)
		{
			if (document.getElementById('paypalform').add_stage.value == 1)
			{
				var iDiscountType = eval('document.getElementById(\'paypalform\').hidDiscountType'+iFinal2).value;
	    		oPriceOff = eval('document.getElementById(\'paypalform\').hidPriceOff'+iFinal2);
	    		oVoucherAmtLeft = eval('document.getElementById(\'paypalform\').hidVoucherAmtLeft'+iFinal2);
	    		oVoucherOff = eval('document.getElementById(\'paypalform\').hidVoucherOff'+iFinal2);
	    		
	    		var oVouPercent = eval('document.getElementById(\'paypalform\').hidVouPercent'+iFinal2);
	    		var oVouAmount = eval('document.getElementById(\'paypalform\').hidVouAmount'+iFinal2);
	    		var oVouLabour = eval('document.getElementById(\'paypalform\').hidVouLabour'+iFinal2);
	    		
	    		var iDiscountChoice1 = eval('document.getElementById(\'paypalform\').hidDiscountChoice1'+iFinal2).value;
	    		var iDiscountChoice2 = eval('document.getElementById(\'paypalform\').hidDiscountChoice2'+iFinal2).value;
				var iWholesalePrice  = eval('document.getElementById(\'paypalform\').hidWholesalePrice'+iFinal2).value;
				var oDiscountedPrice = eval('document.getElementById(\'paypalform\').hidDiscountedPrice'+iFinal2);
	    		var oLabourPrice = eval('document.getElementById(\'paypalform\').hidLabourPrice'+iFinal2);
                
	    		
	        		
               

                    if(eval(oVouLabour.value))
	      		    {
	                    sVoucherLabour += eval(oLabourPrice.value);
	                }
                    
                 
 			        iLabourOff = (eval(oVouLabour.value)/100)*parseFloat(sVoucherLabour);	
 			        
 			        
 			       // DOLLAR VOUCHER DISCOUNT
 			       
 			        if(iLabourOff != 0)
 			    {
					if(eval(oVouAmount.value) > iLabourOff )
					{
						iAmountLeft = eval(oVouAmount.value) - iLabourOff;
						oVoucherAmtLeft.value = iAmountLeft;
						iVoucherPrice = iLabourOff - iVoucherPriceTemp;
						iVoucherPriceTemp = iVoucherPriceTemp + iVoucherPrice;
						iTemp = 1;
						
						
					}
					else if (iTemp == 1)
					{ 
						iVoucherPrice = iAmountLeft;
						iAmountLeft = 0;
						oVoucherAmtLeft.value = iAmountLeft;
						
					}
					else if(iTemp == 0)
					{
						iVoucherPrice = eval(oVouAmount.value);
						iAmountLeft = 0;
						oVoucherAmtLeft.value = iAmountLeft;
						iTemp = 1;
						
					}
					else
					{
						iVoucherPrice = 0;
						iAmountLeft = 0;
						oVoucherAmtLeft.value = iAmountLeft;
						
					}
			   }
			   else if(iTemp == 0)
				   {
					     iVoucherPrice = eval(oVouAmount.value);
						 iAmountLeft = 0;
						 oVoucherAmtLeft.value = iAmountLeft;
						 iTemp = 1;
				   }
				   else
				   {
					    iVoucherPrice = 0;
					    iAmountLeft = 0;
						oVoucherAmtLeft.value = iAmountLeft;
				   }
				   
				if(eval(oVoucherAmtLeft.value) != 0)
					{
						var iAmtLeft = eval(oVoucherAmtLeft.value);
						voucherAmountLeft(iAmtLeft);
					}
				else
					{
						var iAmtLeft = 0;
						voucherAmountLeft(iAmtLeft);
					}
				   
				   
        		sVoucherPrice = iVoucherPrice;
        		
        		if(iDiscountChoice1 == 1)
	        		{
	            		
	            		sVoucherPrice += parseFloat(iWholesalePrice);
	            		
	        		}
	        		
	        		 if(eval(oVouPercent.value))
	        		{
	            		 var sVoucherPercent = (eval(oVouPercent.value)/100)*parseFloat(eval(oDiscountedPrice.value));
	            		 sVoucherPrice += sVoucherPercent;
	        		}
                    		
                    		if(sVoucherPrice != 0)
                    		{
                    		  fTotalOff = fTotalOff + parseFloat(sVoucherPrice);
                 		    }
                		    
                		    iNumMax = (parseInt(iNumMax) > parseInt(aNumList[iFinal1])) ? iNumMax : aNumList[iFinal1];
                		    sPckid = 'sPackageId' + iFinal2;
							id = 'sPackageNo' + iFinal2;
							sLinkId = 'sTotalLink' + iFinal2;
							if(document.getElementById(sLinkId))
							{
								iTotalLinks = iTotalLinks + ( eval(document.getElementById(sLinkId)).value * 1);
							}
							sSiteId = 'sTotalSite' + iFinal2;
							if(document.getElementById(sSiteId))
							{
								iTotalSites = iTotalSites + ( eval(document.getElementById(sSiteId)).value * 1);
							}
							if (sListPackages == '') 
							{
								sFinalPck += eval(document.getElementById(sPckid)).value;
								sListPackages += eval(document.getElementById(id)).value;
								sTxtHourName = 'txtHour' + (iFinal1+1); 
								if(document.getElementById(sTxtHourName))
								{
									sListPackages += '-' + document.getElementById(sTxtHourName).value;
								}
							}
							else 
							{
								sFinalPck += ','+ eval(document.getElementById(sPckid)).value;
								sListPackages += ','+ eval(document.getElementById(id)).value;
								sTxtHourName = 'txtHour' + (iFinal1+1); 
								if(document.getElementById(sTxtHourName))
								{
									sListPackages += '-' + document.getElementById(sTxtHourName).value;
								}
							}
							//oPriceOff = eval('document.getElementById(\'paypalform\').hidPriceOff'+iFinal2);
// 							if(eval(oPriceOff.value))
// 							{
// 								fTotalOff = fTotalOff + eval(oPriceOff.value);
// 							}
							oDiscountedPrice = eval('document.getElementById(\'paypalform\').hidDiscountedPrice'+iFinal2);
							fTotalDiscountedPrice += eval(oDiscountedPrice.value);
                		    
                		    
                		    
                		    
			}
			else if(document.getElementById('paypalform').add_stage.value == 7)
			{
				            
                    		  fTotalOff = 0;
                 		    
                		    
                		    iNumMax = (parseInt(iNumMax) > parseInt(aNumList[iFinal1])) ? iNumMax : aNumList[iFinal1];
                		    sPckid = 'sPackageId' + iFinal2;
							id = 'sPackageNo' + iFinal2;
							sLinkId = 'sTotalLink' + iFinal2;
							if(document.getElementById(sLinkId))
							{
								iTotalLinks = iTotalLinks + ( eval(document.getElementById(sLinkId)).value * 1);
							}
							sSiteId = 'sTotalSite' + iFinal2;
							if(document.getElementById(sSiteId))
							{
								iTotalSites = iTotalSites + ( eval(document.getElementById(sSiteId)).value * 1);
							}
							if (sListPackages == '') 
							{
								sFinalPck += eval(document.getElementById(sPckid)).value;
								sListPackages += eval(document.getElementById(id)).value;
								sTxtHourName = 'txtHour' + (iFinal1+1); 
								if(document.getElementById(sTxtHourName))
								{
									sListPackages += '-' + document.getElementById(sTxtHourName).value;
								}
							}
							else 
							{
								sFinalPck += ','+ eval(document.getElementById(sPckid)).value;
								sListPackages += ','+ eval(document.getElementById(id)).value;
								sTxtHourName = 'txtHour' + (iFinal1+1); 
								if(document.getElementById(sTxtHourName))
								{
									sListPackages += '-' + document.getElementById(sTxtHourName).value;
								}
							}
							//oPriceOff = eval('document.getElementById(\'paypalform\').hidPriceOff'+iFinal2);
// 							if(eval(oPriceOff.value))
// 							{
// 								fTotalOff = fTotalOff + eval(oPriceOff.value);
// 							}
							oDiscountedPrice = eval('document.getElementById(\'paypalform\').hidDiscountedPrice'+iFinal2);
							fTotalDiscountedPrice += eval(oDiscountedPrice.value);
							
							
			}
			
			else
			{
			iNumMax = (parseInt(iNumMax) > parseInt(aNumList[iFinal1])) ? iNumMax : aNumList[iFinal1];
			sPckid = 'sPackageId' + iFinal2;
			id = 'sPackageNo' + iFinal2;
			sLinkId = 'sTotalLink' + iFinal2;
			if(document.getElementById(sLinkId))
			{
				iTotalLinks = iTotalLinks + ( eval(document.getElementById(sLinkId)).value * 1);
			}
			sSiteId = 'sTotalSite' + iFinal2;
			if(document.getElementById(sSiteId))
			{
				iTotalSites = iTotalSites + ( eval(document.getElementById(sSiteId)).value * 1);
			}
			if (sListPackages == '') 
			{
				sFinalPck += eval(document.getElementById(sPckid)).value;
				sListPackages += eval(document.getElementById(id)).value;
				sTxtHourName = 'txtHour' + (iFinal1+1); 
				if(document.getElementById(sTxtHourName))
				{
					sListPackages += '-' + document.getElementById(sTxtHourName).value;
				}
			}
			else 
			{
				sFinalPck += ','+ eval(document.getElementById(sPckid)).value;
				sListPackages += ','+ eval(document.getElementById(id)).value;
				sTxtHourName = 'txtHour' + (iFinal1+1); 
				if(document.getElementById(sTxtHourName))
				{
					sListPackages += '-' + document.getElementById(sTxtHourName).value;
				}
			}
			oPriceOff = eval('document.getElementById(\'paypalform\').hidPriceOff'+iFinal2);
			if(eval(oPriceOff.value))
			{
				fTotalOff = fTotalOff + eval(oPriceOff.value);
			}
			oDiscountedPrice = eval('document.getElementById(\'paypalform\').hidDiscountedPrice'+iFinal2);
			fTotalDiscountedPrice += eval(oDiscountedPrice.value);
		    }
			
		} //END OF TRUE SELECTION
	}
	document.getElementById('paypalform').num_links.value = iNumMax;
	document.getElementById('paypalform').packages.value = sListPackages;
	document.getElementById('paypalform').pckid.value = sFinalPck;
	fTotalOff = Math.round(fTotalOff);
	if (fTotalOff > 0)
	{
		 document.getElementById('totaloff').innerHTML = '<strong>Discount:&nbsp;</strong><span style="color:red">$'+fTotalOff+' Off</span>';
		 //document.getElementById('paypalform').discountprice.value = fTotalOff;
 	}
	else document.getElementById('totaloff').innerHTML = '';
	document.getElementById('paypalform').ftotal.value = fTotalDiscountedPrice-fTotalOff;
	document.getElementById('paypalform').amount.value = fTotalDiscountedPrice-fTotalOff;
	document.getElementById('total').innerHTML = fTotalDiscountedPrice-fTotalOff;
	
	if(document.getElementById('totalLinks'))
	{
		document.getElementById('totalLinks').innerHTML = iTotalLinks;
	}
	
	if(document.getElementById('totalDirs'))
	{
		document.getElementById('totalDirs').innerHTML = iTotalSites;
	}
	if(document.getElementById("percentDiscount"))
    {
	    if(document.getElementById("percentDiscount").value == 1)
	    {
	        document.getElementById("percentDiscount").value = 0;
	        document.getElementById("onetime-discount").value = 0;
	        document.getElementById("voucher_val").innerHTML = '';
        }
    }
	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 = number_format(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);
	}
}

function addtototal2(val,fval) {
	
	var formEl = document.getElementById('paypalform');
	var sId1 = 'product';
	
	if(document.getElementById(sId1+fval).checked==true)
	{
		var newVal = parseInt(document.getElementById('paypalform').amount.value) + parseInt(val);
	}
	else
	{
		var newVal = parseInt(document.getElementById('paypalform').amount.value) - parseInt(val);
	}
	document.getElementById('paypalform').amount.value = newVal;
	document.getElementById('total').innerHTML = newVal;
	
}

function addtototal3(val,fval,bval) 
{
	var sId4 = 'sPackagePrice';
	var val2 = eval(document.getElementById(sId4+fval)).value;
	var val3 = eval(document.getElementById('paypalform').amount).value;
	var val4 = document.getElementById('paypalform').rdValue.value;
	var val3 = parseInt(val3) - parseInt(val4); 
		
	if(bval==true)
	{
		var newVal =  parseInt(val2) + parseInt(val3);
	}
	
	document.getElementById('paypalform').rdValue.value = val;
	document.getElementById('paypalform').amount.value = newVal;
	document.getElementById('total').innerHTML = newVal;
}

function setTotalToZero() 
{
  document.getElementById('total').innerHTML = "0";
}

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

function voucherAmountLeft(amt)
{
		var originalRequest = './app/sub/retrieveVoucherValue.php?dollar='+amt;
		var xmlhttp = makeXmlHttpRequest();
        xmlhttp.open("GET", originalRequest);
        xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
        xmlhttp.send(null);
        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4)
            {
                if (xmlhttp.status == 200)
                {                    
	                    document.getElementById("voucher_val").innerHTML = xmlhttp.responseText;                   
                }
            }
        }                    
}

function makeXmlHttpRequest()
{
    var xmlhttp = false;
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
        try
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(E)
            {
                window.location.reload('uploadFile.aspx');
            }
        }
    }
    return xmlhttp;
}