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 setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
	+"; path=/" ;
}

function getCookie(c_name){
	var c_start,c_end;
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

function setStartCurrency(){
	
	var selectedIndex,currency_code;
	currency_code = getCookie('currency');
	
	if(currency_code == ''){
		currency_code = 'THB';
	}
	
	selectedIndex = getSelectedIndex(currency_code);
	
//	$("select").each(function(){	
//		if ($(this).is("#select_currency")) {
//			$(this).attr({"selectedIndex":selectedIndex});
//		}
//	});
	
	changeCurrency(currency_code,selectedIndex);
}

function getSelectedIndex(currency_code){
	
	var i = 0;
	var ret;
	$.each(r_currency,function(key,val){
		
		if( key == currency_code){ 
			ret = i;
		}
		i++;
	})
	
	return ret;
}

function gotoFactsheet(path){
	if(path != '0'){
		location.href = homePath + path;
	}
}

function changeCurrency(currency_code_to,selectedIndex){
//	var exchange_rate = r_currency
	
	setCookie("currency",currency_code_to,365);
	
	var currency_code_from = $("#currence_currency_code").val();
	var exchange_rate,val_to,val_from;
	
	$.each(r_currency,function(key,val){
			
		if( key == currency_code_to){ 
			 val_to = val.rate;
			
		}
		
		if( key == currency_code_from){
			 val_from = val.rate;
		}
	})
	var tmp_rate;
	
	
	exchange_rate =   val_from / val_to ;
	
	$("select").each(function(){	
		if ($(this).is("#select_currency")) {
			$(this).attr({"selectedIndex":selectedIndex});
		}
	});
	
	$(".obj_currency").each(function(){
		
	   var cal;
	   var cal_from;
	   var tmp_rate = $(this).find("#obj_tmp_rate").val();
	   var tmp_rate_from = $(this).find("#obj_tmp_rate_from").val();
	   
	  
	   
	   cal = exchange_rate * tmp_rate;
	 //  alert(tmp_rate);
	   cal = Math.ceil(cal); 
	   cal = addCommas(cal);
	   
	   cal_from = exchange_rate * tmp_rate_from;
	   cal_from = Math.ceil(cal_from); 
	   cal_from = addCommas(cal_from);
	   
	   
	   
//   alert(tmp_rate +"======="+exchange_rate+"  form = "+cal+"  to = "+val_to);
	   
	
	   
	   $(this).find("#obj_currency-rate").html(cal);
	   $(this).find("#obj_currency-code").html(currency_code_to);
	   $(this).find("#obj_currency-full_rate").html(currency_code_to + " " +cal_from);
	});

	
}