function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}

function fxcalc_validate(fm) {  // v1.0
						
	// this validates the form
	
	var fd = fm.amount;         // field name
	var input = fd.value;
	
	var ok = true;
	var negativeNum = false;

	if (input == '') {    // empty string, no input
		ok = false;
	}
	else if (input.charAt(0)==' ') {
		ok = false;
	}
	else {
		var n = input * 1;
		if ((n != input)) {
			ok = false;
		}else if(n <= 0){
			negativeNum = true;
		}
		else {
			fd.value = n;
		}
	}

	var errMsg = '';
	
	if (!ok) {
		errMsg = 'Please enter numeric value.';
	}else if(negativeNum){
		errMsg = 'Please enter positive numeric value.';
	}
	
	if (fm.from_currency.value == fm.to_currency.value) {
		errMsg = 'Please choose two different currencies.';
	}

	if(errMsg!=''){
		showError(errMsg);
		return false;
	}
		

	return true;
						
}


function showError(msg) {
	alert(msg);
}
	
// functions for forward calculator : start

var current_forward_calc;
function forward_calc_inverse(forward_form)
{
	current_forward_calc = forward_form;
	var tempCurr = current_forward_calc.to_currency.value;
	current_forward_calc.to_currency.value = current_forward_calc.from_currency.value;
	current_forward_calc.from_currency.value = tempCurr;
	callForwardCalculator(current_forward_calc,current_forward_calc.from_currency.value,  current_forward_calc.to_currency.value);
}


function forward_calc_go(forward_form)
{
	current_forward_calc = forward_form;
	callForwardCalculator(current_forward_calc,current_forward_calc.from_currency.value,  current_forward_calc.to_currency.value);
}

function callForwardCalculator(forward_calc_form, from_currency_value, to_currency_value)
{
	if(isVaildForwardRequest(forward_calc_form))
	{
	
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		  {
		  alert ("Your browser does not support AJAX!");
		  return;
		  } 
		var url="/treasury/forward_calc?para1="+from_currency_value+"&para2="+to_currency_value+"&para3="+forward_calc_form.value_date.value+"&para4="+forward_calc_form.forwardf.value;
		xmlHttp.onreadystatechange=showForwardRate;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);	
	
	}
	else
	{
		forward_calc_form.sell_amount.value = "";
		forward_calc_form.rate_value.value = "";
	}
}

	
function showForwardRate()
{

	if (xmlHttp.readyState==4)
		{
			var xmlDoc=xmlHttp.responseText;
	
			var sell_amt_value = getValueFromXML(xmlDoc, "<sell_amount>", "</sell_amount>");
			var rate_value = getValueFromXML(xmlDoc, "<forward_rate>", "</forward_rate>");
			var error_msg = getValueFromXML(xmlDoc, "<error_msg>", "</error_msg>");
			var to_curr = getValueFromXML(xmlDoc, "<to_currency>", "</to_currency>");
			var from_curr =getValueFromXML(xmlDoc, "<from_currency>", "</from_currency>"); 
				
			if(trim(sell_amt_value)=="" || trim(rate_value)=="")
			{
				if(trim(error_msg)=="")// if there is no error message then display Service is not available message
				{
					document.getElementById('unavail_msg').style.display = "block";
					document.getElementById('error_msg').style.display = "none";	
					current_forward_calc.sell_amount.value = "";
					current_forward_calc.rate_value.value = "";
					document.getElementById('sellcurrency').value = "";
					document.getElementById('forcurrency').value = "";
					document.getElementById('calcResult').style.display = "none";					
				}else{// else if error msg present then dispaly error message
					document.getElementById('error_msg').style.display = "block";
					document.getElementById('unavail_msg').style.display = "none";	
					document.getElementById('calcResult').style.display = "none";					
					current_forward_calc.sell_amount.value = "";
					current_forward_calc.rate_value.value = "";
					document.getElementById('sellcurrency').value = "";
					document.getElementById('forcurrency').value = "";	
				}

			}else{
				document.getElementById('unavail_msg').style.display = "none";
				document.getElementById('error_msg').style.display = "none";
				document.getElementById('calcResult').style.display = "block";				
				current_forward_calc.sell_amount.value = sell_amt_value;
				current_forward_calc.rate_value.value = rate_value;
				document.getElementById('sellcurrency').value = from_curr;
				document.getElementById('forcurrency').value = to_curr;				
			    current_forward_calc.sell_amount.disabled = false;	    
	   		    current_forward_calc.rate_value.disabled = false;
   		    }
		}
		
}

function getValueFromXML(xmlDoc, startElement, endElement)
{
		var start_element_index = xmlDoc.indexOf(startElement); 
		var end_element_index = xmlDoc.indexOf(endElement);
		if(start_element_index > -1 && end_element_index > -1)
		{
			var value_index = start_element_index + startElement.length;
			return xmlDoc.substring(value_index, end_element_index);
		}
	return "";	
}

function isVaildForwardRequest(forward_form)
{
	var ok = true;
	var errMsg = "";
	var buyValue = trim(forward_form.buy_amount.value);
	forward_form.buy_amount.value =  ltrim(rtrim(buyValue));
	

	if(forward_form.from_currency.value == forward_form.to_currency.value)
	{
		errMsg = "Please choose two different currencies.";
		forward_form.to_currency.focus();
		ok = false;
	}else if(buyValue == "")
	{
		//if buy amount value is empty then show error message
		errMsg = "Please enter numeric value.";
		forward_form.buy_amount.focus();
		ok = false;
	}else{
	
		var n = buyValue * 1;
		if ((n != buyValue)) {
			errMsg = "Please enter numeric value.";
			forward_form.buy_amount.focus();
			ok = false;
		}else if(n <= 0){
			errMsg = "Please enter positive numeric value.";
			forward_form.buy_amount.focus();
			ok = false;
		}
	
	}	

	if(!ok)
	{
		alert(errMsg);
	}
	
return ok;
	
}

function ltrim(str) {
 for (var k=0; k<str.length && str.charAt(k)<=" " ; k++) ;
 return str.substring(k,str.length);
}
function rtrim(str) {
 for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--) ;
 return str.substring(0,j+1);
}

//This function accepts a String and trims the string in both sides of the string ignoring space characters
function trim(stringValue){
    return ltrim(rtrim(stringValue));
}


// functions for forward calculator : end

//functions for new spot fx calculator : start
	function fxcalc_submit(f) {
	if ( ! fxcalc_validate(f)) {

	}
	
	else{
		callSpotFXCalculator(f);
	}	
  }
  
  function callSpotFXCalculator(f)
	{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="/treasury/fxcalculator?para1="+f.from_currency.value+"&para2="+f.to_currency.value+"&para3=SPT&para4="+f.amount.value;
	xmlHttp.onreadystatechange=showSpotCalc;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	}
  
  function showSpotCalc()
	{
		if (xmlHttp.readyState==4)
			{
			 document.getElementById("spotfhidden1").innerHTML = xmlHttp.responseText;
			 document.getElementById('spotfhidden1').style.display = "inline";
			}	
	}
  
//functions for new spot fx calculator : start 
