var i, aobjBuyFC, tran, extype, agency;
var aobjBuyD, aobjBuyW, aobjBuyDTC, aobjBuyFTC;
var intselect, intAlert;

intAlert = 0;
tran = "Buy";
extype = "FC";
intselect = 0;
aobjBuyFC = rate;

function start()
{
}

function Exchange()
{
        var rate, index, exrate;
        rate = 0;
	if(document.converter.Country.options[document.converter.Country.options.selectedIndex].value != ""){
        index = document.converter.Country.options.selectedIndex
        if(typeof(eval("aobj"+tran+extype+"["+index+"]")) == "undefined")
        {
                alert("To obtain the most current exchange rate, contact 1-877-EZFOREX (393-6739) or email sales@ezforex.com.")
                document.converter.ExchangeRate.value = ""
        }else{
			strMessage = eval("aobj"+tran+extype+"["+index+"]")
			if(isNaN(strMessage))
			{
				if(strMessage.indexOf("Please be advised that this country is now the ") != -1)
				{
				alert(eval("aobj"+tran+extype+"["+index+"]"))
				for(i=0; i < document.converter.Country.options.length; i++)
        		{
		           if(document.converter.Country.options[i].text == strMessage.substr(47))
        		    {
                		 document.converter.Country.options.selectedIndex = i
		                 break;
        		     }
		        }
				index = document.converter.Country.options.selectedIndex
				if(tran=='Buy')
                {
                    rate = Number(eval("aobj"+tran+extype+"["+index+"]")) + Number(eval("aobj"+tran+extype+"["+index+"]") * (document.converter.Commission.value/100))
					rate = getCCRate(rate)
                }else{
                    rate = Number(eval("aobj"+tran+extype+"["+index+"]")) - Number(eval("aobj"+tran+extype+"["+index+"]") * (document.converter.Commission.value/100))
                }
				if(! isNaN(rate)){
					exrate = "Today's Rate: " + format(rate, 10)
				}else{
					Exchange()
					exit()
				}
               document.converter.ExchangeRate.value = format(rate, 10)
			}else if(strMessage.indexOf("Please enter the amount of the") != -1){
				alert(strMessage)
				document.converter.ExchangeRate.value = "0"
			}else if(isNaN(eval("aobj"+tran+extype+"["+index+"]"))){
                        alert("For today\'s rates, contact 1-877-EZFOREX (393-6739).")
						document.converter.ExchangeRate.value = ""
				}
               }else{
                        if(tran=='Buy')
                        {
                             rate = Number(eval("aobj"+tran+extype+"["+index+"]")) + Number(eval("aobj"+tran+extype+"["+index+"]") * (document.converter.Commission.value/100))
							 rate = getCCRate(rate)
                        }else{
                             rate = Number(eval("aobj"+tran+extype+"["+index+"]")) - Number(eval("aobj"+tran+extype+"["+index+"]") * (document.converter.Commission.value/100))
                        }
						exrate = "Today's Rate: " + format(rate, 10)
						document.converter.ExchangeRate.value = format(rate, 10)
                        
                }
        }
	}else{
		document.converter.ExchangeRate.value = ""
	}
}

function submitHandler()
{
	
	Recompute()

	if(document.converter.ExchangeRate.value == "")
    {
        alert("Please contact your Administrator for rates.")
        return false
   }
	if(document.converter.amount.value == "" && document.converter.foreignamount.value == "")
	{
		alert("A amount must be filled in.")
		return false
	}
	if(document.converter.amount.value < 0 || document.converter.foreignamount.value < 0)
	{
		alert("You can only buy amounts greater than zero.")
		return false
	}
	if(document.converter.amount.value.indexOf(",") != -1 || document.converter.foreignamount.value.indexOf(",") != -1)
	{
		alert("Please do not use commas in the amount of purchase.")
		return false
	}
	
	return true

}

function getCCRate(dblRate)
{
	if(typeof(document.converter.days) != "undefined")
	{
		if(typeof(document.converter.days[1]) != "undefined")
		{
			if(document.converter.days[1].checked)
			{
				dblRate = dblRate * (1 + (2.5/100))
			}
		}else if(document.converter.days.value == "creditcard"){
			dblRate = dblRate * (1 + (2.5/100))
		}
	}
	return dblRate
}

function Recompute()
{
	var LowestDen, LocalUnits;

	LowestDen = 1

	if(document.converter.amount.value > 0 || document.converter.foreignamount.value > 0)
	{	
		if(document.converter.ExchangeRate.value > 0)
		{
			Convert(document.converter.amount, document.converter.ExchangeRate, LowestDen, document.converter.foreignamount, document.converter.SelectedElement.value, 0)
		}else{
			alert("Please click Convert in order to convert transaction.")
		}
	}
	
}

function format(expr, decplaces)
{
	str = "" + Math.round(eval(expr) * Math.pow(10, decplaces))
	while(str.length <= decplaces)
	{
		str = "0" + str
	}
	decpoint = str.length - decplaces
	return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length)
}

function Convert(LocalAmount, ExchangeRate, LowestDen, AmountElement, SelectedElement, Packet)
{
	if(SelectedElement == "local")
		{
			LocalExchange = LocalAmount.value / ExchangeRate.value
			WholeAmount = Math.floor(LocalExchange/LowestDen)
			Result = LowestDen * WholeAmount
			if(Math.round(LocalExchange) == Result)
			{
				FinalAmount = Result
			}else{
				if(Result % LowestDen == 0)
				{
					FinalAmount = Result
				}else{
					if(! isOKCount(AmountElement.value))
					{
						FinalAmount = Result + LowestDen
					}else{
						FinalAmount = Result
					}
				}
			}
			document.getElementById("converter").SelectedElement.value = "foreign"
		}else{
			if(Math.round(AmountElement.value) % LowestDen == 0)
			{
				FinalAmount = Math.round(AmountElement.value)
			}else{
				if(! isOKCount(AmountElement.value))
				{
					while((Math.round(AmountElement.value) % LowestDen) != 0)
					{
						AmountElement.value = Number(Math.round(AmountElement.value)) + 1
					}
				}
				FinalAmount = AmountElement.value
			}
		}
		LocalAmount.value = format((FinalAmount*ExchangeRate.value), 2)
		AmountElement.value = format(FinalAmount, 2)
	
}

