var first_show_col = 3;
var last_show_col = 16;
var last_col = 30;

var new_first_show_col = 0;
var new_last_show_col = 13;
var new_last_col = 27;



var current_first_col = new Array();
var current_last_col = new Array();

function readCurrency()
{
	var currency = readCookie("lt_currency");
	var oSelect = document.getElementById("SelectCurrency");
	if (currency != null)
	{
		oSelect.selectedIndex = currency;
		changeCurrency(oSelect);
	}
}

function changeCurrency(oSelect, base_curr, from_no, to_no)
{
	var currency = oSelect.value;
	
	/* we have more than one currecncy in promotion page
	var currCurrency = document.getElementById("CurrencyCode").value;
	*/
	if (base_curr == null)
	{
		base_curr = document.getElementById("CurrencyCode").value;
	}
	
	if (currency != "0")
	{
		//var to_rate=document.dat.elements["Currencies['"+currency+"']"].value;
		var to_rate = CurrencyArray[currency]["Value"];

		//var cname=document.dat.elements["CurrencyName['"+currency+"']"].value;
		var cname = CurrencyArray[currency]["Name"];
		var l_cname = cname.length;
		
/*
		document.getElementById("currency_display").value = cname;
		document.getElementById("currency_display").size = l_cname;
*/		

//		var to = new Number(0);

		if (to_no == null)
		{
			var rate_count = document.getElementById("total_rate").value;
		}
		else
		{
			var rate_count = to_no;
		}
		
		var loop_from = 0;
		if (from_no != null)
		{
			loop_from = from_no;
		}

		for (i=loop_from; i<rate_count; i++)
		{
			from = document.getElementById("thai_rate_" + i).value;
			to = new Number(from/to_rate);
			oTo = document.getElementById("rate_" + i);
			
			// we use only first 3 decimal point to prevent very small different value
			str_to = to+'';
			arr_to = str_to.split('.');
			str_integer = arr_to[0];
			str_decimal = arr_to.length > 1 ? '.' + arr_to[1] : '';
			str_decimal = str_decimal.substr(0, 4);
			str_to = str_integer+str_decimal;
			to = new Number(str_to);
			
			if (currency == base_curr)
			{
				oTo.style.display = 'none';
				oTo.innerHTML = '';
			}
			else
			{
				if (oTo != null && to > 0)
				{
					oTo.innerHTML = addCommas(Math.ceil(to));
					oTo.style.display = '';
				}
			}
		}
		
		// put into form hidden field, used by secure site
		var forms = document.getElementsByTagName("form");
		for (var i=0; i<forms.length; i++)
		{
			if(forms[i].id.substring(0,4)=="form")
			{
				forms[i].ExchangeRate.value = to_rate;
				forms[i].ShowCurrencyCode.value = currency;
				forms[i].ShowCurrencyName.value = cname;
			}
		}		
		
	}
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function moveNextNew(tbl_no)
{
	var first_show = new_first_show_col;
	var last_show = new_last_show_col;
	
	if (tbl_no==null)
	{
		tbl_no = "0";
		first_show = new_first_show_col;
		last_show = new_last_show_col;
	}
	else
	{
		if (current_first_col[tbl_no] == undefined)
		{
			current_first_col[tbl_no] = first_show;
		}
		else
		{
			first_show = current_first_col[tbl_no];
		}
		if (current_last_col[tbl_no] == undefined)
		{
			current_last_col[tbl_no] = last_show;
		}
		else
		{
			last_show = current_last_col[tbl_no];
		}
	}
	
	var oTable = document.getElementById("main_tbl"+ tbl_no);
	var offer_row = document.getElementById("numberOffer_"+ tbl_no).value;
		
	// dateTD_0_1
	document.getElementById("dateTD_"+ tbl_no +"_"+ first_show).style.display = "none";
	document.getElementById("dateTD_"+ tbl_no +"_"+ (last_show + 1)).style.display = "";
	
	for (loop = 0; loop < offer_row; loop++)
	{
		document.getElementById("offerTD_"+ tbl_no +"_"+ loop +"_"+ first_show).style.display = "none";
		document.getElementById("offerTD_"+ tbl_no +"_"+ loop +"_"+ (last_show + 1)).style.display = "";
	}
	
	// show or hide action button according to current display location
	document.getElementById("movePrevious_"+ tbl_no).style.visibility = "visible";
	
	
	// increase global variable
	first_show += 1;
	last_show += 1;
	
	if (last_show == new_last_col)
	{
		document.getElementById("moveNext_"+ tbl_no).style.visibility = "hidden";	
	}
	else
	{
		document.getElementById("moveNext_"+ tbl_no).style.visibility = "visible";
	}
	
	if (tbl_no === "")
	{
		new_first_show_col = first_show;
		new_last_show_col = last_show;
	}
	else
	{
		current_first_col[tbl_no] = first_show;
		current_last_col[tbl_no] = last_show;
	}
}

function movePreviousNew(tbl_no)
{
	var first_show = new_first_show_col;
	var last_show = new_last_show_col;
	if (tbl_no==null)
	{
		tbl_no = "";
		first_show = new_first_show_col;
		last_show = new_last_show_col;
	}
	else
	{
		if (current_first_col[tbl_no] == undefined)
		{
			current_first_col[tbl_no] = first_show;
		}
		else
		{
			first_show = current_first_col[tbl_no];
		}
		if (current_last_col[tbl_no] == undefined)
		{
			current_last_col[tbl_no] = last_show;
		}
		else
		{
			last_show = current_last_col[tbl_no];
		}
	}
	
	var oTable = document.getElementById("main_tbl"+tbl_no);
	var offer_row = document.getElementById("numberOffer_"+ tbl_no).value;
	
	// dateTD_0_1
	document.getElementById("dateTD_"+ tbl_no +"_"+ (first_show - 1)).style.display = "";
	document.getElementById("dateTD_"+ tbl_no +"_"+ last_show).style.display = "none";
	
	for (loop = 0; loop < offer_row; loop++)
	{
		document.getElementById("offerTD_"+ tbl_no +"_"+ loop +"_"+ (first_show - 1)).style.display = "";
		document.getElementById("offerTD_"+ tbl_no +"_"+ loop +"_"+ (last_show)).style.display = "none";
	}
	
	// show or hide action button according to current display location
	document.getElementById("moveNext_"+ tbl_no).style.visibility = "visible";
	
	
	// decrease global variable
	first_show -= 1;
	last_show -= 1;
	
	if (first_show == 0)
	{
		document.getElementById("movePrevious_"+ tbl_no).style.visibility = "hidden";	
	}
	else
	{
		document.getElementById("movePrevious_"+ tbl_no).style.visibility = "visible";	
	}
	
	if (tbl_no === "")
	{
		first_show_col = first_show;
		last_show_col = last_show;
	}
	else
	{
		current_first_col[tbl_no] = first_show;
		current_last_col[tbl_no] = last_show;
	}
}






function moveNext(tbl_no)
{
	var first_show = first_show_col;
	var last_show = last_show_col;
	if (tbl_no==null)
	{
		tbl_no = "";
		first_show = first_show_col;
		last_show = last_show_col;
	}
	else
	{
		if (current_first_col[tbl_no] == undefined)
		{
			current_first_col[tbl_no] = first_show;
		}
		else
		{
			first_show = current_first_col[tbl_no];
		}
		if (current_last_col[tbl_no] == undefined)
		{
			current_last_col[tbl_no] = last_show;
		}
		else
		{
			last_show = current_last_col[tbl_no];
		}
	}
	var oTable = document.getElementById("main_tbl"+tbl_no);
	
	for (var r=1; r<oTable.rows.length-1; r++)
	{
		var oRow = oTable.rows[r];
		
		// hide left most column
		oRow.cells[first_show].style.display = "none";
		
		// show right most column
		oRow.cells[last_show+1].style.display = "";
		oRow.cells[last_show+1].style.width = "40";
	}
	
	// show or hide action button according to current display location
	document.getElementById("movePrevious"+tbl_no).style.visibility = "visible";
	
	
	// increase global variable
	first_show += 1;
	last_show += 1;
	
	if (last_show == last_col)
	{
		document.getElementById("moveNext"+tbl_no).style.visibility = "hidden";	
	}
	else
	{
		document.getElementById("moveNext"+tbl_no).style.visibility = "visible";	
	}
	
	if (tbl_no === "")
	{
		first_show_col = first_show;
		last_show_col = last_show;
	}
	else
	{
		current_first_col[tbl_no] = first_show;
		current_last_col[tbl_no] = last_show;
	}
	
	// alert('last_show_col = '+ last_show_col +'\n'+'last_col = '+ last_col);
}

function movePrevious(tbl_no)
{
	var first_show = first_show_col;
	var last_show = last_show_col;
	if (tbl_no==null)
	{
		tbl_no = "";
		first_show = first_show_col;
		last_show = last_show_col;
	}
	else
	{
		if (current_first_col[tbl_no] == undefined)
		{
			current_first_col[tbl_no] = first_show;
		}
		else
		{
			first_show = current_first_col[tbl_no];
		}
		if (current_last_col[tbl_no] == undefined)
		{
			current_last_col[tbl_no] = last_show;
		}
		else
		{
			last_show = current_last_col[tbl_no];
		}
	}
	var oTable = document.getElementById("main_tbl"+tbl_no);
	
	for (var r=1; r<oTable.rows.length-1; r++)
	{
		var oRow = oTable.rows[r];
		
		// show left most column
		oRow.cells[first_show-1].style.display = "";
		oRow.cells[first_show-1].style.width = "40";
		
		// hide right most column
		oRow.cells[last_show].style.display = "none";
	}
	
	// show or hide action button according to current display location
	document.getElementById("moveNext"+tbl_no).style.visibility = "visible";
	
	
	// decrease global variable
	first_show -= 1;
	last_show -= 1;
	
	if (first_show == 3)
	{
		document.getElementById("movePrevious"+tbl_no).style.visibility = "hidden";	
	}
	else
	{
		document.getElementById("movePrevious"+tbl_no).style.visibility = "visible";	
	}
	
	if (tbl_no === "")
	{
		first_show_col = first_show;
		last_show_col = last_show;
	}
	else
	{
		current_first_col[tbl_no] = first_show;
		current_last_col[tbl_no] = last_show;
	}
	
}

function updateSource()
{
	// put agent id in cookie into form hidden field
	var agentId = readCookie("source");
	if (agentId != null)
	{
		var forms = document.getElementsByTagName("form");
		for (var i=0; i<forms.length; i++)
		{
			if (forms[i].refid)
			{
				forms[i].refid.value = agentId;
			}
		}		
	}
}

function initJavascript()
{
	var cForm = document.initValues;
	last_col = cForm.last_col.value;
}
