var havexml		=	true;
var MonthNames 	= 	new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var partURL		=	ServerPath+"/04-exchange-rate/xml/";
var partIMG		=	ServerPath+"/01-home/images/";


function loadXMLDoc(dname) 
{
	var xmlDoc;

	try
	  {
		  if(window.ActiveXObject){
			  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		  }else{
				xmlDoc=document.implementation.createDocument("","",null);
			  
		  }
	  }
	catch(e)
	  {
		  alert(e.message)
	  }
	xmlDoc.async=false;
	try
   	{
  		 xmlDoc.load(dname);
   	}
  	catch(e)
    { 
		  alert(e.message)	
		havexml	=	false;
    	return;
    }	
	return(xmlDoc);
}

function getXMLFile(ThisDate)
{
	CurrentDate	=	"";
	getDataThisDate(CurrentDate);
	getAllYear();
	getAllMonth();
	getAllDate();
}

function getAllYear()
{
	xmlfile	=	partURL+"exrate_date.xml";	
	xmlDoc	= loadXMLDoc(xmlfile);	
	var AllYear		=	xmlDoc.getElementsByTagName("ex_year");		
	ShowYear		=	"";
	ShowYear		=	ShowYear+"<select name='yyyy' onchange='getAllMonth()'>";
	for (i=0;i<AllYear.length;i++)	
	{	
		Year	=	AllYear.item(i).attributes.getNamedItem("year").value;		
		showStatus	=	""
		if(i+1==AllYear.length){
			showStatus	=	"selected='selected'";
		}
		ShowYear	=	ShowYear+"<option value='"+i+"' "+showStatus+">"+Year+"</option>";
	}			
	ShowYear		=	ShowYear+"</select>";
	document.getElementById("ViewYear").innerHTML	=	ShowYear;
}

function getAllMonth()
{
	var iYear	=	document.view.yyyy.value
	xmlfile		=	partURL+"exrate_date.xml";	
	xmlDoc		= 	loadXMLDoc(xmlfile);	
	var AllYear		=	xmlDoc.getElementsByTagName("ex_year")[iYear];			
	var AllMonth	=	AllYear.getElementsByTagName("ex_month");		
	
	ShowMonth		=	"";
	ShowMonth		=	ShowMonth+"<select name='mm' onchange='getAllDate()'>";

	for (i=0;i<AllMonth.length;i++)	
	{	
		Month		=	AllMonth.item(i).attributes.getNamedItem("month").value;	
		showStatus	=	""
		if(i+1==AllMonth.length){
			showStatus	=	"selected='selected'";
		}		
		ShowMonth	=	ShowMonth+"<option value='"+i+"' "+showStatus+">"+MonthNames[(Month-1)]+"</option>";
	}			
	ShowMonth		=	ShowMonth+"</select>";
	document.getElementById("ViewMonth").innerHTML	=	ShowMonth;
}

function getAllDate()
{
	var iYear	=	document.view.yyyy.value	
	var iMonth	=	document.view.mm.value
	xmlfile		=	partURL+"exrate_date.xml";	
	xmlDoc		= 	loadXMLDoc(xmlfile);	
	var AllYear		=	xmlDoc.getElementsByTagName("ex_year")[iYear];			
	var AllMonth	=	AllYear.getElementsByTagName("ex_month")[iMonth];		
	var AllDate		=	AllMonth.getElementsByTagName("ex_date");		
	
	ShowDate		=	"";
	ShowDate		=	ShowDate+"<select name='dd'>";

	for (i=0;i<AllDate.length;i++)	
	{	
		TDate		=	AllDate[i].childNodes[0].nodeValue;
		if(i+1==AllDate.length){
			showStatus	=	"selected='selected'";
		}		
		ShowDate	=	ShowDate+"<option value='"+i+"' "+showStatus+">"+TDate+"</option>";
	}			
	ShowDate		=	ShowDate+"</select>";
	document.getElementById("ViewDate").innerHTML	=	ShowDate;
}

function getDataThisDate(ThisDate)
{
	if(ThisDate != ""){
		xmlfile = new String(ThisDate);
		xmlfile = xmlfile.replace("/","_");
		xmlfile = xmlfile.replace("/","_");	
		xmlfile = partURL+"exrate"+xmlfile+".xml";		
	}else{
		xmlfile	=	partURL+"exrate_lastdate.xml";	
	}	
	xmlDoc	= loadXMLDoc(xmlfile);		
	if(havexml)
	{
		var AllExrate	=	xmlDoc.getElementsByTagName("exrate");
		if(AllExrate.length <= 0){
			havexml		=	false;		
		}else{
			havexml		=	true;	
		}
	 }	
	if(havexml){
		document.getElementById("ThisDate").innerHTML	=	AllExrate.item(0).attributes.getNamedItem("date").value	;	
		document.getElementById("ThisTime").innerHTML	=	AllExrate.item(0).attributes.getNamedItem("time").value	;	
		
		var AllForeign		=	xmlDoc.getElementsByTagName("foreign");		
		var arrayForeign	=	new Array();
		for (i=0;i<AllForeign.length;i++)	
		{
			arrayForeign[i]	=	AllForeign[i].childNodes[0].nodeValue;				
		}			

		var Allbuy_esb		=	xmlDoc.getElementsByTagName("buyrate_esb");				
		var arrayBuy_esb	=	new Array();
		for (i=0;i<Allbuy_esb.length;i++)	
		{
			arrayBuy_esb[i]	=	Allbuy_esb[i].childNodes[0].nodeValue;					
		}			
		
		var Allbuy_tt		=	xmlDoc.getElementsByTagName("buyrate_tt");		
		var arrayBuy_tt	=	new Array();
		for (i=0;i<Allbuy_tt.length;i++)	
		{
			arrayBuy_tt[i]	=	Allbuy_tt[i].childNodes[0].nodeValue;		
		}			
		
		var Allsell_tt		=	xmlDoc.getElementsByTagName("sellrate_tt");		
		var arraySell_tt	=	new Array();
		for (i=0;i<Allsell_tt.length;i++)	
		{
			arraySell_tt[i]	=	Allsell_tt[i].childNodes[0].nodeValue;		
		}					
		var ShowExrate	=	"<table width='640' border='0' cellpadding='1' cellspacing='1' bgcolor='#EEEEEE'>";
			ShowExrate	=	ShowExrate+"<tr>";
			ShowExrate	=	ShowExrate+"<td width='210' height='59' rowspan='2' align='left' valign='middle' bgcolor='#F4F4F4'><h3 align='center'>Foreign Currencies</h3></td>";
			ShowExrate	=	ShowExrate+"<td height='29' colspan='2' align='left' valign='middle' bgcolor='#F4F4F4'><h3 align='center'>Buying Rates</h3></td>";
			ShowExrate	=	ShowExrate+"<td height='29' align='left' valign='middle' bgcolor='#F4F4F4'><h3 align='center'>Selling Rates</h3></td>";
			ShowExrate	=	ShowExrate+"</tr>";
			ShowExrate	=	ShowExrate+"<tr>";
			ShowExrate	=	ShowExrate+"<td height='29' align='left' valign='middle' bgcolor='#F4F4F4'><h3 align='center'>Export/Sight Bill</h3></td>";
			ShowExrate	=	ShowExrate+"<td height='29' align='left' valign='middle' bgcolor='#F4F4F4'><h3 align='center'>Telegraphic Transfer</h3></td>";
			ShowExrate	=	ShowExrate+"<td height='29' align='left' valign='middle' bgcolor='#F4F4F4'><h3 align='center'>Telegraphic Transfer</h3></td>";
			ShowExrate	=	ShowExrate+"</tr>";
		for (i=0;i<arrayForeign.length;i++)	
		{

			if(i % 2 ==0){
				row_color	=	"display_column1";
			}
			else{
				row_color	=	"display_column2";			
			}
			
			ShowExrate	=	ShowExrate+"<tr>";			
			ShowExrate	=	ShowExrate+"<td height='25'  width='210' valign='middle' bgcolor='#FFFFFF'><p align='center'>"+arrayForeign[i]+"</p></td>";
			ShowExrate	=	ShowExrate+"<td height='25' width='140'  valign='middle' bgcolor='#FFFFFF'><p align='center'>"+arrayBuy_esb[i]+"</p></td>";
			ShowExrate	=	ShowExrate+"<td height='25'  width='140' valign='middle' bgcolor='#FFFFFF'><p align='center'>"+arrayBuy_tt[i]+"</p></td>";
			ShowExrate	=	ShowExrate+"<td height='25' width='140' valign='middle' bgcolor='#FFFFFF'><p align='center'>"+arraySell_tt[i]+"</p></td>";					
			ShowExrate	=	ShowExrate+"</tr>";			
		}
		ShowExrate	=	ShowExrate+"</table>";

		document.getElementById("ShowRate").innerHTML	=	ShowExrate;
	
	 }else{
		document.getElementById("ShowRate").innerHTML	=	"<div class='errer_msg'>Data Not Found!</div>";	 
		document.getElementById("ThisDate").innerHTML	=	ThisDate;	
		document.getElementById("ThisTime").innerHTML	=	"0:00";					
	 }
}
function ChooseDate()
{
	FormDay		=	document.view.dd.value;
	FormMonth	=	document.view.mm.value;
	FormYear	=	document.view.yyyy.value;

	xmlfile		=	partURL+"exrate_date.xml";	
	xmlDoc		= 	loadXMLDoc(xmlfile);	
	
	var AllYear		=	xmlDoc.getElementsByTagName("ex_year");			
	thisYear		=	AllYear.item(FormYear).attributes.getNamedItem("year").value;		
	
	AllYear			=	xmlDoc.getElementsByTagName("ex_year")[FormYear];			
	var AllMonth	=	AllYear.getElementsByTagName("ex_month");			
	thisMonth		=	AllMonth.item(FormMonth).attributes.getNamedItem("month").value;	
	
	
	var AllYear		=	xmlDoc.getElementsByTagName("ex_year")[FormYear];			
	var AllMonth	=	AllYear.getElementsByTagName("ex_month")[FormMonth];		
	var AllDate		=	AllMonth.getElementsByTagName("ex_date");		
	thisDay			=	AllDate[FormDay].childNodes[0].nodeValue;	
	
	
	var ThisDate	=	thisDay+"/"+thisMonth+"/"+thisYear;	
	havexml		=	true;		
	getDataThisDate(ThisDate);

}
function loading()
{
			document.getElementById("ShowRate").innerHTML	=	"<div style='width:350px; height:250px; padding:150px 0px 150px 250px'><img src='"+ partURL+"loading02.gif' width='130' height='60' />";
}


function ShowMiniRate()
{
	var havexml	=	true;
	xmlfile	= partURL+"exrate_lastdate.xml";	
	xmlDoc	= loadXMLDoc(xmlfile);	
	
	if(havexml)
	{
		var AllExrate	=	xmlDoc.getElementsByTagName("exrate");
		if(AllExrate.length <= 0){
			havexml		=	false;		
		}else{
			havexml		=	true;	
		}
	 }	

	if(havexml){
		ThisDate	=	AllExrate.item(0).attributes.getNamedItem("date").value	;	
		ThisTime	=	AllExrate.item(0).attributes.getNamedItem("time").value	;	
		
		var AllForeign		=	xmlDoc.getElementsByTagName("foreign");		
		var arrayForeign	=	new Array();
		for (i=0;i<AllForeign.length;i++)	
		{
			arrayForeign[i]	=	AllForeign[i].childNodes[0].nodeValue;				
		}			

		var Allbuy_esb		=	xmlDoc.getElementsByTagName("buyrate_esb");				
		var arrayBuy_esb	=	new Array();
		for (i=0;i<Allbuy_esb.length;i++)	
		{
			arrayBuy_esb[i]	=	Allbuy_esb[i].childNodes[0].nodeValue;					
		}			
		
		var Allbuy_tt		=	xmlDoc.getElementsByTagName("buyrate_tt");		
		var arrayBuy_tt	=	new Array();
		for (i=0;i<Allbuy_tt.length;i++)	
		{
			arrayBuy_tt[i]	=	Allbuy_tt[i].childNodes[0].nodeValue;		
		}			
		
		var Allsell_tt		=	xmlDoc.getElementsByTagName("sellrate_tt");		
		var arraySell_tt	=	new Array();
		for (i=0;i<Allsell_tt.length;i++)	
		{
			arraySell_tt[i]	=	Allsell_tt[i].childNodes[0].nodeValue;		
		}					
		var ShowExrate	=	"";
		no	=	0;
		
		ShowExrate	=	ShowExrate+"<div style='background-image: url("+partIMG+"menu_ex_rate.gif); height:24px; width:166px;background-repeat:no-repeat;' ></div>";
		ShowExrate	=	ShowExrate+"<div style'width:166px;padding-bottom:1px'>";
		ShowExrate	=	ShowExrate+"<div style='width:166px;height:34px;padding-top:1px;'>";
		ShowExrate	=	ShowExrate+"<span style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;vertical-align: middle;height:33px;background-color:#F69494;color:#FFFFFF;font-weight:bold;text-align:center;float:left;width:65px;display:block;'>Currency<br />/Baht</span>";
		ShowExrate	=	ShowExrate+"<span  style=' float:left;width:1px; height:35px;display:block;'></span>";
		ShowExrate	=	ShowExrate+"<span 	style=' font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;vertical-align: middle;width:100px; height:14px;display:block;background-color:#F69494;color:#FFFFFF;font-weight:bold;text-align:center;float:left'>TT</span>";
		ShowExrate	=	ShowExrate+"<div style='float:left; width:100px; padding-top:1px; height:11px'>";
		ShowExrate	=	ShowExrate+"<span  style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;vertical-align: middle;height:18px;background-color:#F69494;color:#FFFFFF;font-weight:bold;text-align:center;float:left;width:49px;display:block;'>Buy</span>";
		ShowExrate	=	ShowExrate+"<span  style='float:left;width:1px; height:18px;display:block;'></span>";
		ShowExrate	=	ShowExrate+"<span  style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;vertical-align: middle;height:18px;background-color:#F69494;color:#FFFFFF;font-weight:bold;text-align:center;float:left;width:50px;display:block;'>Sell</span>";				
		ShowExrate	=	ShowExrate+"</div>";
		ShowExrate	=	ShowExrate+"</div>";
		ShowExrate	=	ShowExrate+"<div style='width:164px;height:94px;padding:1px;background-color:#F7D9D9;'>";
		
		for (i=0;i<arrayForeign.length;i++)	
		{

			if(no % 2 ==0){
				row_color	=	"width:164px;background-color:#FFFFFF;display:block;padding-top:5px;height:15px;";
			}
			else{
				row_color	=	"width:164px;background-color:#F7D9D9;display:block;padding-top:2px;height:15px;";			
			}

			
			if((arrayForeign[i] == "USD")||(arrayForeign[i] == "GBP")||(arrayForeign[i] == "EUR")||(arrayForeign[i] == "JPY")||(arrayForeign[i] == "HKD")){
				ShowExrate	=	ShowExrate+"<div  style='"+row_color+"'>";
				ShowExrate	=	ShowExrate+"<div  style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;vertical-align: middle;width:60px;height:15px;float:left;display:block;text-align:center;'>"+arrayForeign[i]+"</div>";
				Btt	=	arrayBuy_tt[i]* 1;
				ShowExrate	=	ShowExrate+"<div   style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;vertical-align: middle;height:15px;width:49px;display:block;float:left;display:block;text-align:center;'>"+Btt.toFixed(2)+"</div>";
				var Att	=	arraySell_tt[i]*1;
				ShowExrate	=	ShowExrate+"<div   style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;vertical-align: middle;height:15px;float:left;display:block;text-align:center;width:50px;display:block;'>"+Att.toFixed(2)+"</div>";	
				ShowExrate	=	ShowExrate+"</div>";
				no	=	no+1;			
			}
			
		}		
		
		ShowExrate	=	ShowExrate+"</div>";
		ShowExrate	=	ShowExrate+"<div style='background-color:#F69494;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:9px; color:#FFFFFF; padding:5px 5px 5px 15px ;font-weight:bold;height:12px'>"+ThisDate+"&nbsp;&nbsp;at "+ThisTime+"</div>";		
		ShowExrate	=	ShowExrate+"</div>";
		ShowExrate	=	ShowExrate+"<div  style='background-image: url("+partIMG+"menu_empty.gif); height:24px; width:166px;background-repeat:no-repeat;'>";
		ShowExrate	=	ShowExrate+"<a href='javascript:OpenPopup()'>";
		ShowExrate	=	ShowExrate+"<img src='"+partIMG+"exchange_more.gif' align='right'></a></div><div style='height:10px'></div>";		

		document.write(ShowExrate);
	 }else{
		document.write("<div class='errer_msg'>Data Not Found!</div>");			
	 }
}
function OpenPopup() { 
	window.open('/1/2/exchange-rate','ExchangRate','menubar=yes,scrollbars=yes,resizable=yes');
}
