var isIE = (navigator.userAgent.indexOf('MSIE') != -1);
//nghiahd
var SubUrl = "/";

function AboutMoore()
{
	alert("mailto:contact@moore.vn");
}

function utf8(wide) 
{
  var c, s;
  var enc = "";
  var i = 0;
  while(i<wide.length) {
    c= wide.charCodeAt(i++);
    // handle UTF-16 surrogates
    if (c>=0xDC00 && c<0xE000) continue;
    if (c>=0xD800 && c<0xDC00) {
      if (i>=wide.length) continue;
      s= wide.charCodeAt(i++);
      if (s<0xDC00 || c>=0xDE00) continue;
      c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
    }
    // output value
    if (c<0x80) enc += String.fromCharCode(c);
    else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
    else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
  }
  return enc;
}

var hexchars = "0123456789ABCDEF";

function toHex(n) 
{
  return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}

var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

function encodeURIComponentNew(s) 
{
  var s = utf8(s);
  var c;
  var enc = "";
  for (var i= 0; i<s.length; i++) {
    if (okURIchars.indexOf(s.charAt(i))==-1)
      enc += "%"+toHex(s.charCodeAt(i));
    else
      enc += s.charAt(i);
  }
  return enc;
}

function buildURL(fld)
{
	if (typeof encodeURIComponent == "function")
	{
		// Use JavaScript built-in function
		// IE 5.5+ and Netscape 6+ and Mozilla
		return encodeURIComponent(fld);
	}
	else 
	{
		// Need to mimic the JavaScript version
		// Netscape 4 and IE 4 and IE 5.0
		return encodeURIComponentNew(fld);
	}
}

function destroyURL(fld)
{
	if (typeof decodeURIComponent == "function")
	{
		// Use JavaScript built-in function
		// IE 5.5+ and Netscape 6+ and Mozilla
		return decodeURIComponent(fld).replace(/\+/g, " ");
	}
	else 
	{
		// Need to mimic the JavaScript version
		// Netscape 4 and IE 4 and IE 5.0
		return "";
	}
}

function moveCalendar(offset)
{
	var currentDate = new Date();

	var dateString = GetPostVariable('Date', '');
	if (dateString != '')
	{
		var dateElements = dateString.split('-');
		var t = dateElements[0];
		dateElements[0] = dateElements[1];
		dateElements[1] = t;
		currentDate = new Date(dateElements.join('-'));
		
	}
	currentDate = new Date(currentDate.setDate(currentDate.getDate() + offset));
	dateString = currentDate.getDate() + '-' + (currentDate.getMonth() + 1) + '-' + currentDate.getYear();
	
	location.search = 'ChannelID=' + GetPostVariable('ChannelID', 1) + '&Date=' + dateString;
}

//nghiahd
function ShowImage(ImageID)
{
	showDialog(SubUrl + ImageID + '/PhotoView.htm', 620, 600);
}

function showDialog(url, width, height)
{
	return showWindow(url, false, false, false, false, false, false, true, true, width, height, 0, 0);
}

function showWindow(url, isStatus, isResizeable, isScrollbars, isToolbar, isLocation, isFullscreen, isTitlebar, isCentered, width, height, top, left)
{
	if (isCentered)
	{
		top = (screen.height - height) / 2;
		left = (screen.width - width) / 2;
	}

	open(url, '_blank', 'status=' + (isStatus ? 'yes' : 'no') + ','
	+ 'resizable=' + (isResizeable ? 'yes' : 'no') + ','
	+ 'scrollbars=' + (isScrollbars ? 'yes' : 'no') + ','
	+ 'toolbar=' + (isToolbar ? 'yes' : 'no') + ','
	+ 'location=' + (isLocation ? 'yes' : 'no') + ','
	+ 'fullscreen=' + (isFullscreen ? 'yes' : 'no') + ','
	+ 'titlebar=' + (isTitlebar ? 'yes' : 'no') + ','
	+ 'height=' + height + ',' + 'width=' + width + ','
	+ 'top=' + top + ',' + 'left=' + left);
}

//nghiahd

function showTearmCondition()
{   
    return showWindow(SubUrl + 'TearmConditions.htm', false, false, true, false, false, false, true, false, 720, 720, 0, 0); 
}

function showCurrency()
{     
    return showDialog(SubUrl + 'Currency.htm',700,780);
}

function showAskQuestionInfo()
{     
    return showDialog(SubUrl +'AskAlert.htm',700,490);        
}
function showGuidelines()
{     
    return showWindow(SubUrl + 'Guidelines.htm', false, false, true, false, false, false, true, false, 720, 720, 0, 0); 
}

// Write Time
function writeTime(s)
{
	var mydate=new Date(s)
	
	var year = mydate.getYear()
	if (year < 1000)
		year += 1900
	var month = mydate.getMonth()
	//if (month < 10)
	//	month = "0" + month
	var day = mydate.getDate()
	if (day < 10)
		day = "0" + day

	var dayw = mydate.getDay()
	
	var hour = mydate.getHours()
	
	var minute=mydate.getMinutes()
	if (minute < 10)
		minute = "0" + minute;
		
	if (hour < 10)
	{
	    hour = "0" + hour
	}	    	
	if (hour < 12)
    {
        minute = minute + " a.m."
    }
    else if (hour > 12)
    {
        hour = hour - 12;
        minute = minute + " p.m."  
    }	
    else if (hour = 12)
    {
        minute = minute + " p.m."
    }
		
	var monthname = new Array("January","February","March","April","May","Jun","July","August","September","October","November","December");	
	var dayarray  = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	
	document.write(hour+":"+minute+ " (GMT+7), "+dayarray[dayw]+", "+monthname[month]+" "+day+", "+year)
}

function getTimeString(s)
{
	document.write("<span class=LargeTime2><strong>Last Update:&nbsp;</strong>");
	writeTime(s);
	document.write("</span>")
}

function getTime(s)
{
	writeTime(s);
}

function Trim(s)
{
    var i = 0;
    while ((i < s.length) && (s.charCodeAt(i) == 32))
	    i++;

    var j = s.length - 1;
    while ((j > i) && (s.charCodeAt(j) == 32))
	    j--;

    return s.substr(i, j - i + 1);
}

function GetPostVariable(param, defval)
{
	var	s = '&' + location.search.substr(1);
	var	i = s.indexOf('&' + param + '=');
	if (i == -1) return defval;

	s = s.substr(i + param.length + 2);
	i = s.indexOf('&');
	if (i == -1) return s;

	return s.substr(0, i);
}

//nghiahd
function DisplaySearchPage(PageCount)
{
    document.write('Trang ');
    j = GetPostVariable('SearchPage', 1);
    for (i = 1; i <= PageCount; i++)
	    if (i != j)
		    document.write('<a class="SearchPage" href='+SubUrl+'Searcher.htm?SearchQuery=' + GetPostVariable('SearchQuery', 'Error') + '&SearchPage=' + i + '>' + i + '&nbsp;</a>');
	    else
		    document.write('[' + i + ']&nbsp;');
		    
    i = parseInt(j) + 1;
    
    if (j < PageCount)
	    document.write('&nbsp;<a class="SearchPage" href='+SubUrl+'Searcher.htm?SearchQuery=' + GetPostVariable('SearchQuery', 'Error') + '&SearchPage=' + i + '>Xem ti&#7871;p&nbsp;</a>');
}


function onLinkClick(link)
{
    if (isIE)	//Mozilla and FireFox don't support non-standard attribute
    {	
	    window.open(link.href, '', 
	    'toolbar='+link.toolbar+
	    ',location='+link.location+
	    ',status='+link.statusbar+
	    ',menubar='+link.menubar+
	    ',scrollbars='+link.scrollbars+
	    ',resizable='+link.resizable+
	    ',width='+link.width+
	    ',height='+link.height+
	    ',top='+link.top+
	    ',left='+link.left);
	    return false;
    }
}

function displayInfo(info)
{
	if (info == 'weather')
	    showDialog('http://www.baophunu.org.vn/transweb/rams.htm', 490, 690);
    if (info == 'forex')
	    showDialog('http://www.baophunu.org.vn/transweb/tygia.htm', 550, 440);
    if (info == 'gold')
	    showDialog('http://www.baophunu.org.vn/transweb/giavang.htm', 450, 280);
    //if (info == 'tvguide')
	    //showWindow('http://www.baophunu.org.vn/transweb/truyenhinh.htm', false, false, true, false, false, false, true, true, 450, 354, 0, 0);
    if (info == 'lottery')
	    showWindow('http://www.baophunu.org.vn/transweb/XoSo.html', false, false, true, false, false, false, true, true, 450, 600, 0, 0);
    if (info == 'stock_ho')
	    showDialog('http://www.baophunu.org.vn/transweb/chungkhoan_ho.htm', 1010, 750);
	if (info == 'stock_ha')
	    showDialog('http://www.baophunu.org.vn/transweb/chungkhoan_ha.htm', 900, 750);
}

function setHomePage(ctrl)
{
	ctrl.style.behavior='url(#default#homepage)';
	ctrl.setHomePage(location.host);
}

function switchDisplay(id)
{
	ctrl = document.getElementById(id);
	if (ctrl.style.display == "")
		ctrl.style.display = "none";
	else
		ctrl.style.display = "";
}

function onCmdSearchClick(Type)
{
    var s = location.search.split('&');    
    for(var i=0;i< s.length;i++)
    {
        if(s[i].indexOf('Type') != -1) 
        {            
            Type = s[i].substring(s[i].indexOf('Type')+5);
            break;
        }
    }     	
    var SearchQuery = document.getElementById("SearchQuery");

	//var q = Trim(document.SearchSubmit.SearchQuery.value);
	
	var q = Trim(SearchQuery.value);
	
	if (q == '')
		return false;
		
	if ((q.indexOf('AND') == -1) && (q.indexOf('OR') == -1) && (q.indexOf('"') == -1))
		q = '"' + q + '"';
	
	//document.SearchSubmit.SearchQuery.value = q;
	
	//nghiahd
	document.location.href = SubUrl + "Searcher.htm?SearchQuery=" + q +"&Type=" + Type;
	
	return false;
}

/***********************************
            String Cut
************************************/
function cutStrExper( head, length )
{
    if(head == "?")
    {
        document.write("");
		return;
    }
        
   	var strLen = head.length;
	if (strLen > length)
	{
		head = head.substr(0, length+5) ;
		
		var lastIndexOfSpace = head.lastIndexOf(' ');
		
		if (lastIndexOfSpace > 0)
        {
            head = head.substr(0, lastIndexOfSpace) ;
        }
        else 
        {
            head = head.substr(0, length) ;
        }
		
		document.write(head + " ...");
		return;
	}
	else
	{
		document.write(head);		
	}		
}

// Save Webpage
function doSaveAs()
{
    if ((document.execCommand) && isIE)
    {
       document.execCommand("SaveAs", true);
    }
    else
    {
        var fileName = location.href.toString();
        var X=new XMLHttpRequest
        var data="";
        X.open('GET', fileName, false );
        X.send('');	
        data = X.responseText;
        return window.open( "data:x-application/external;charset=utf-8;base64," + btoa(data));
    }
}
