// JScript File

var xmlDoc = importXML("calculator/Resources/XML/Restaurant.xml");
var xmlFastFood = importXML("calculator/Resources/XML/FastFood.xml");
var list;
var avgMin = 4;

function RestaurantsFees(decAverageTicket)
{
    try
    { 
        if ($("#spanType")[0].innerHTML == "Restaurant")
	{
		list=xmlDoc.documentElement.childNodes;
	}
	else if ($("#spanType")[0].innerHTML == "Quick Serve")
	{
		list=xmlFastFood.documentElement.childNodes;
	}

        for(var count=0;count<list.length;count++)
        {
            if(list[count].nodeType!=3)
            {
                 var LowRange=list[count].childNodes[0].text;
                 var HighRange=list[count].childNodes[1].text;
                 var MinRate=list[count].childNodes[2].text;
                 var MaxRate=list[count].childNodes[3].text;
                 var AvgRate=list[count].childNodes[4].text;
		 var stdDev=list[count].childNodes[5].text;

                 if(getBrowseType()!="MSIE")
                 {
                        LowRange=list[count].childNodes[1].textContent;
                        HighRange=list[count].childNodes[3].textContent;
                        MinRate=list[count].childNodes[5].textContent;
                        MaxRate=list[count].childNodes[7].textContent;
                        AvgRate=list[count].childNodes[9].textContent;
                        stdDev=list[count].childNodes[11].textContent;
                 }
               
                if(parseFloat(decAverageTicket)>=parseFloat(LowRange) 
					&& (parseFloat(decAverageTicket)<parseFloat(HighRange)
					|| HighRange == ""))
                {
					$("#AverageCostValue")[0].innerHTML = FormatMoney(ForDight(AvgRate, 2), 2) + "%";
					$("#divRestaurantsFeesValue")[0].innerHTML = FormatMoney(ForDight(MinRate, 3), 3) + "%-" + FormatMoney(ForDight(MaxRate, 3), 3) + "%";
					$("#divStandard")[0].innerHTML = FormatMoney(ForDight(stdDev, 2), 2) + "%";
					return ForDight(AvgRate, 2);
                }
            }        
        }       
        
     }catch(ae)
     {
     }
}

function Calculator()
{
    	var decTotalPaid = ResumeMoney($("#txtTotalPaid").val());
	var decTotalVMC = ResumeMoney($("#txtTotalVMC").val());    
	var decLessFee = ResumeMoney($("#txtLessFee").val());
	var decAverage = ResumeMoney($("#txtAverageTicket").val());
	var decERate;
	var decAvgRate;

	if ($("#txtTotalPaid").val() != "" && $("#txtLessFee").val() != "")
	{
		if (decTotalPaid < decLessFee)
		{
			Clear();
			alert("Total Fees Paid needs to be greater than Fees for American Express and Other.");
			return false;		
		}		
	}	

	if ($("#txtLessFee").val() != "")
	{
		// Calculator the Effective Rate
		decERate = ForDight((decTotalPaid - decLessFee) * 100 /decTotalVMC, 2);
		$("#divEffectiveRateValue")[0].innerHTML = FormatMoney(decERate, 2) + "%";
	}	

	
	if ($("#txtAverageTicket").val() != "")
	{
		// Get Average Cost
		decAvgRate = RestaurantsFees(decAverage);
	}
	var decIssuing = decTotalVMC * (decAvgRate * .01);
	$("#divPaidIssuing")[0].innerHTML = FormatMoney(decIssuing,2);

	if ($("#txtTotalPaid").val() == "" || $("#txtLessFee").val() == "" 
		|| $("#txtTotalVMC").val() == "" || $("#txtAverageTicket").val() == "")
	{
		Clear();
		return;
	}	

	if (decERate < decAvgRate)
	{
		Clear();
		alert("Effective Rate needs to be greater than the Average Cost of Interchange and Fees to V/MC.");
		return false;
	}
	
	// Calculator the Fees to Processor
	var decFees = decERate - decAvgRate;
	$("#divFeesProcessorValue")[0].innerHTML = FormatMoney(decFees, 2) + "%";

	// Calculator the Paid to Processor
	var decPaid = ForDight(decTotalVMC * decFees / 100, 2);
	$("#divPaidProcessor")[0].innerHTML = FormatMoney(decPaid, 2);
	
	// Calculator the Annual Paid
	$("#divAnnualPaidProcessor")[0].innerHTML = FormatMoney(decPaid * 12, 2);
	
	return true;
}

function TotalFeesPaid()
{
	if ($("#txtTotalPaid").val() == "")
	{
		Clear();
		return;
	}

	var decTotalPaid = ResumeMoney($("#txtTotalPaid").val());

	if (decTotalPaid <= 0)
	{
		alert("Total Fees Paid should be greater than 0.");
		$("#txtTotalPaid").val("");
		Clear();
		return;
	}
	
	$("#txtTotalPaid").val(FormatMoney(decTotalPaid, 2));	
	Validate();
}

function TotalVMC(obj)
{
	if ($("#txtTotalVMC").val() == "")
	{
		Clear();
		return;
	}
	
	var decTotalVMC = ResumeMoney($("#txtTotalVMC").val());

	if (ResumeMoney($("#txtTotalVMC").val()) <= 0)
	{
		alert("Total $ of V/MC (bankcard total) should be greater than 0.");
		$("#txtTotalVMC").val("");
		Clear();
		return;
	}	

	$("#txtTotalVMC").val(FormatMoney(decTotalVMC, 2));
}

function AMEXFees()
{
	if ($("#txtLessFee").val() == "")
	{
		Clear();
		return;
	}

	var decLessFee = ResumeMoney($("#txtLessFee").val());

	if (decLessFee < 0)
	{
		alert("Less Fees for American Express and other should be at least 0.");
		$("#txtLessFee").val("");
		Clear();
		return;
	}	
	
	$("#txtLessFee").val(FormatMoney(decLessFee, 2));
	Validate();
}

function AverageTicket()
{
	if ($("#txtAverageTicket").val() == "")
	{
		Clear();
		return;
	}

	var decAverage = ResumeMoney($("#txtAverageTicket").val());

	if (decAverage < avgMin)
	{
		alert("Average Ticket should be at least $"+avgMin+".");
		$("#txtAverageTicket").val("");
		Clear();
		return;
	}	
    
	$("#txtAverageTicket").val(FormatMoney(decAverage, 2));
}

function OpenStatments()
{   
    var strHelpPageUrl="calculator/Statments.htm?id=" + $("#spanValue")[0].innerHTML + "&text=" + $("#spanDefault")[0].innerHTML;
    var openWindow = window.open(strHelpPageUrl,'Help','width=1024,height=768,status=no,resizable=yes,top=140,left=220,location=no,menubar=no,scrollbars=yes,toolbar=no');
    openWindow.focus();    
    return false;
}

function Clear()
{
	$("#AverageCostValue")[0].innerHTML = "";
	$("#divRestaurantsFeesValue")[0].innerHTML = "";
	$("#divEffectiveRateValue")[0].innerHTML = "";
	$("#divFeesProcessorValue")[0].innerHTML = "";
	$("#divPaidProcessor")[0].innerHTML = "";
	$("#divAnnualPaidProcessor")[0].innerHTML = "";
}

function Validate()
{
	var decTotalPaid = ResumeMoney($("#txtTotalPaid").val());	 
	var decLessFee = ResumeMoney($("#txtLessFee").val());

	if ($("#txtTotalPaid").val() != "" && $("#txtLessFee").val() != "")
	{
		if (decTotalPaid < decLessFee)
		{
			Clear();
			alert("Total Fees Paid needs to be greater than Fees for American Express and Other.");
			return false;		
		}		
	}
}
