//################################################################
//																//
//		This JS File is used for Calling Fusebox Web Service	//
//		Author : pkapoor		Date : March 01,2008			//
//																//	
//###############################################################



// ##################### fuseboxPostURL #####################
var fuseboxPostURL = "/fusebox_rate_ws";
//var fuseboxPostURL = "http://aaadb.fusebox.com/RatesService.asmx";
// ##################### fuseboxPostURL #####################



// ##################### CHECK FOR oXmlHttp #####################
var oXmlHttp;
if(window.ActiveXObject) // Check for IE
{
	oXmlHttp = new ActiveXObject("msxml2.xmlhttp");
}
else if(window.XMLHttpRequest) // Check for Firefox/Others
{
	oXmlHttp = new XMLHttpRequest();
}
// ##################### CHECK FOR oXmlHttp #####################



// ##################### CHECK FOR xmlobject #####################
function getXMLObject(xmlString) // Returns xmlobject based on browser Type
{
	var xmlobject;	
	if (document.implementation && document.implementation.createDocument) // Check for Firefox/Others
	{	
		xmlobject = (new DOMParser()).parseFromString(xmlString, "text/xml");
	}	
	else if (window.ActiveXObject) // Check for IE
	{
		xmlobject = new ActiveXObject("MSXML.DOMDocument");
		xmlobject.loadXML(xmlString);	
	}
	else
	{
		alert("Your browser cannot handle this script.");
	}
	return xmlobject;
}
// ##################### CHECK FOR xmlobject #####################



// For CD and IRA 1 Year Rate $10,000 - Used on index.htm
function GetRateByProductTypeAndAmountAndRegion(prodCode, amount, aff, region)
{	
	var response;
	try
	{
		oXmlHttp.open("POST", fuseboxPostURL, false);

		oXmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
		oXmlHttp.setRequestHeader("SOAPAction", "https://aaadb.fusebox.com/GetRateByProductTypeAndAmountAndRegion");
		
		soapReq = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
			soapReq = soapReq+"<soap:Header>";
				soapReq = soapReq+"<AuthenticationHeader xmlns='https://aaadb.fusebox.com/'>";
				soapReq = soapReq+"<Username></Username>";
				soapReq = soapReq+"<Password></Password>";
				soapReq = soapReq+"</AuthenticationHeader>";
			soapReq = soapReq+"</soap:Header>";	
			soapReq = soapReq+"<soap:Body>";
				soapReq = soapReq+"<GetRateByProductTypeAndAmountAndRegion xmlns='https://aaadb.fusebox.com/'>";
				soapReq = soapReq+"<inputProduct>"+prodCode+"</inputProduct>";
				soapReq = soapReq+"<inputAmount>"+amount+"</inputAmount>";
				soapReq = soapReq+"<inputAffiliate>"+aff+"</inputAffiliate>";
				soapReq = soapReq+"<inputRegion>"+region+"</inputRegion>";
				soapReq = soapReq+"</GetRateByProductTypeAndAmountAndRegion>";
			soapReq = soapReq+"</soap:Body>";
		soapReq = soapReq+"</soap:Envelope>";
		
		oXmlHttp.send(soapReq);
		response = oXmlHttp.responseText;
		response = response.substring(response.indexOf("<APY>")+5,response.indexOf("</APY>"));
		response= response*100;
		response= response.toPrecision(3);
		response= response+'%';
	}
	catch (err)
	{		
		response = "FBERROR";	//Show Default Rate for CD and IRA
	}
	return response;
}

// For Effective Date - Used on index.htm
function GetInstrumentValidDate(instrument, aff, region)
{
	var response;
	try
	{
		oXmlHttp.open("POST", fuseboxPostURL, false);

		oXmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
		oXmlHttp.setRequestHeader("SOAPAction", "https://aaadb.fusebox.com/GetInstrumentValidDate");
		
		soapReq = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
			soapReq = soapReq+"<soap:Header>";
				soapReq = soapReq+"<AuthenticationHeader xmlns='https://aaadb.fusebox.com/'>";
				soapReq = soapReq+"<Username></Username>";
				soapReq = soapReq+"<Password></Password>";
				soapReq = soapReq+"</AuthenticationHeader>";
			soapReq = soapReq+"</soap:Header>";	
			soapReq = soapReq+"<soap:Body>";
				soapReq = soapReq+"<GetInstrumentValidDate xmlns='https://aaadb.fusebox.com/'>";
				soapReq = soapReq+"<inputInstrument>"+instrument+"</inputInstrument>";
				soapReq = soapReq+"<inputAffiliate>"+aff+"</inputAffiliate>";
				soapReq = soapReq+"<inputRegion>"+region+"</inputRegion>";
				soapReq = soapReq+"</GetInstrumentValidDate>";
			soapReq = soapReq+"</soap:Body>";
		soapReq = soapReq+"</soap:Envelope>";
		
		oXmlHttp.send(soapReq);
		response = oXmlHttp.responseText;
		response = response.substring(response.indexOf("<GetInstrumentValidDateResult>")+30,response.indexOf("</GetInstrumentValidDateResult>"));
	}
	catch (err)
	{
		response = "FBERROR"; //Show Default Date
	}	
	return response;
}

// MyDiscoverBank Specific Products - Super Now, Statement Savings, Savings Club - Used on index.htm
function GetAllRatesByAffiliate(aff, region)
{
	var response;
	try
	{
		oXmlHttp.open("POST", fuseboxPostURL, false);

		oXmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
		oXmlHttp.setRequestHeader("SOAPAction", "https://aaadb.fusebox.com/GetAllRatesByAffiliate");
		
		soapReq = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
			soapReq = soapReq+"<soap:Header>";
				soapReq = soapReq+"<AuthenticationHeader xmlns='https://aaadb.fusebox.com/'>";
				soapReq = soapReq+"<Username></Username>";
				soapReq = soapReq+"<Password></Password>";
				soapReq = soapReq+"</AuthenticationHeader>";
			soapReq = soapReq+"</soap:Header>";	
			soapReq = soapReq+"<soap:Body>";
				soapReq = soapReq+"<GetAllRatesByAffiliate xmlns='https://aaadb.fusebox.com/'>";
				soapReq = soapReq+"<inputAffiliate>"+aff+"</inputAffiliate>";
				soapReq = soapReq+"<inputRegion>"+region+"</inputRegion>";
				soapReq = soapReq+"</GetAllRatesByAffiliate>";
			soapReq = soapReq+"</soap:Body>";
		soapReq = soapReq+"</soap:Envelope>";
		
		oXmlHttp.send(soapReq);
		response = oXmlHttp.responseText;
		response = response.substring(response.indexOf("<NewDataSet"),response.indexOf("/NewDataSet>")+12);
		response = replaceAll(response);
	}
	catch (err)
	{
		response = "FBERROR"; //Show Default Rate For Super Now
	}	
	return response;
}

function replaceAll(str)
{
	str=str.replace( new RegExp("diffgr:","g"), "" );
	return str.replace( new RegExp("msdata:","g"), "" );
}

function toCurrencyFrmt(nbr)
{
	nbr = Math.floor(nbr);
	nbr = nbr+'';
	if(10000>nbr && nbr>1000)
	{
		nbr=nbr.substring(0,1)+','+nbr.substring(1,nbr.length);
	}
	else if(nbr>9999)
	{
		nbr=nbr.substring(0,2)+','+nbr.substring(2,nbr.length);
	}
	return nbr;
}	