﻿  
//Dung de xoa Session khi browser close
function PageUnload()
{ 
    var url = "../Session.aspx";
    
    if (event.altKey==true && event.keyCode==0 ){ //capturing ALT + F4
        document.location.href=url;
    }
    var Xwidth=document.body.offsetWidth-event.clientX
    var YHeight=event.clientY
    if(Xwidth<=30 && YHeight<0){ //capturing close button
        document.location.href=url;
    }
}

function getKey(el) 
    {
        if(event.which || event.keyCode){
            if ((event.which == 13) || (event.keyCode == 13)) {
                el.click();
            }
        }
    }

function features(isStatus, isResizeable, isScrollbars, isToolbar, isLocation, 
isFullscreen, isTitlebar, isCentered, width, height, top, left){
    if (isCentered)
	{
		top = (screen.height - height) / 2;
		left = (screen.width - width) / 2;
	}
    return '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;
}

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;
	}
	
	return open(url, '_blank', 
	    features(false, false, false, false, false, false, true, true, width, height, 0, 0));
}

function showDialog(url, width, height)
{
	return showWindow(url, false, false, true, false, false, false, true, true, width, height, 0, 0);
}

function ShowVideo(videoURL)
{
    var url = "/dipeditor/showvideo.aspx?id=" + videoURL;
	showWindow(url, false, false, true, false, false, false, true, true, 500, 375, 0, 0);
}

function OpenDialog(urlPath){
    var frm = window.showModalDialog(urlPath);   
    return false;
}         

function openBrower(url, textID, imgID) 
{ 
     var frm = window.showModalDialog(url, "center", "dialogHeight: 450px; dialogWidth: 500px"); 
     if (frm!=null){ document.getElementById(textID).value = frm; 
     if(imgID!='') document.getElementById(imgID).src = frm; } 
     return false; 
}
        
//------------------------------------------------------------------------------------------------------------

function EnabledControl(Check, el){
        if(document.getElementById(Check).checked)
            document.getElementById(el).disabled = "";
        else
            document.getElementById(el).disabled = "disabled";
}
    
function FormatNumber(str){
	 var strTemp = GetNumber(str);
	 if(strTemp.length <= 3)
		  return strTemp;
	 strResult = "";
	 for(var i =0; i< strTemp.length; i++)
		  strTemp = strTemp.replace(",", "");
	 for(var i = strTemp.length; i>=0; i--)
	 {
		  if(strResult.length >0 && (strTemp.length - i -1) % 3 == 0)
			   strResult = "," + strResult;
		  strResult = strTemp.substring(i, i + 1) + strResult;
	  }	
	  if(strResult=="0") strResult="";
	  return strResult;
}

function GetNumber(str)
{
	for(var i = 0; i < str.length; i++)
	{	
		var temp = str.substring(i, i + 1);		
		if(!(temp == "," || temp == "." || (temp >= 0 && temp <=9)))
		{
			  alert("Vui lòng nhập số (0-9)!");
			  return str.substring(0, i);
		}
		if(temp == " "){
		      return str.substring(0, i);
		}
	}
	return str;
}
            
function ConvertPriceText(strTemp)
{
	strTemp        = strTemp.replace(/,/g, "");
	var priceTy    = parseInt(strTemp/1000000000,0)
	var priceTrieu = parseInt((strTemp % 1000000000)/1000000,0)
	var priceNgan  = parseInt(((strTemp % 1000000000))%1000000/1000,0)
	var priceDong  = parseInt(((strTemp % 1000000000))%1000000%1000,0)
	var strTextPrice = ""      
	if(priceTy > 0 && parseInt(strTemp,0) > 900000000)
		strTextPrice = strTextPrice  + "<b>" + priceTy + "</b> tỷ "
	if(priceTrieu > 0)
		strTextPrice = strTextPrice  + "<b>" + priceTrieu + "</b> triệu "
	if(priceNgan > 0)
		strTextPrice = strTextPrice  + "</b>" + priceNgan + "</b> ngàn "
    if(document.getElementById("tiente").value == "vnd1")
    {
         if(priceTy > 0 || priceTrieu > 0 || priceNgan > 0 || priceDong > 0)
	         strTextPrice = strTextPrice  + "<b>VNĐ</b>"
    }
    if(document.getElementById("tiente").value == "sjc")
    {
        if(priceDong > 0)
           strTextPrice = strTextPrice + priceDong
        if(priceTy > 0 || priceTrieu > 0 || priceNgan > 0 || priceDong > 0)
		   strTextPrice = FormatNumber(strTemp) + "<b> lượng SJC</b>"
    }
    if(document.getElementById("tiente").value == "usd")
    {
        if(priceDong > 0)
            strTextPrice = strTextPrice + priceDong
        if(priceTy > 0 || priceTrieu > 0 || priceNgan > 0 || priceDong > 0)
		    strTextPrice = FormatNumber(strTemp) + "<b> USD</b>"
    }
    document.getElementById("priceText").innerHTML = strTextPrice
}
           
function FocusObj(name)
{
    document.getElementById(name).focus()
}


//Moi nhat
function changeCurr(price, priceType, Index){
    document.getElementById('vnd'+Index).className='';
    document.getElementById('usd'+Index).className='';
    document.getElementById('sjc'+Index).className='';
    switch(priceType){
        case 'vnd':
            document.getElementById('vnd'+Index).className='ac';
            break
        case 'usd': 
            document.getElementById('usd'+Index).className='ac';
            break;
        default:
            document.getElementById('sjc'+Index).className='ac';
            break;   
    }    
    document.getElementById('price'+Index).innerHTML=price;
}
function showPopup(el, width, height, header, url){
    var ran = Math.random();
    var reURL = url.indexOf('?')>-1? url + '&ran=' + ran:url+'?ran='+ran;
    el.SetSize(width, height);
    el.SetHeaderText(header);
    el.SetContentUrl(reURL);
    el.Show();
}

function ShowPopup(width, height, header, url){
    var ran = Math.random();
    var reURL = url.indexOf('?')>-1? url + '&ran=' + ran:url+'?ran='+ran;
    Popup.SetSize(width, height);
    Popup.SetHeaderText(header);
    Popup.SetContentUrl(reURL);
    Popup.Show();
}

function showPopupAtID(el, elementID, width, height, header, url){
    var ran = Math.random();
    var reURL = url.indexOf('?')>-1? url + '&ran=' + ran:url+'?ran='+ran;
    el.SetSize(width, height);
    el.SetHeaderText(header);
    el.SetContentUrl(reURL);
    el.Hide();
    el.ShowAtElementByID(elementID);
}

function Checkbox_CheckAll(checkboxID, countItem){
    var checkAll = document.getElementById(checkboxID + '_0').checked;
    for(var i=1; i<countItem; i++){
        document.getElementById(checkboxID + '_' + i.toString()).checked=checkAll;
    }
}

function Checkbox_SingleCheck(checkboxID, countItem){
    var checkAll = document.getElementById(checkboxID + '_0').checked;
    if(checkAll)
        document.getElementById(checkboxID + '_0').checked = false;
    else{
        checkAll = true;
        for(var i=1; i<countItem; i++){
            if(!document.getElementById(checkboxID + '_' + i.toString()).checked){
                checkAll = false;
                break;
            }
        }
        document.getElementById(checkboxID + '_0').checked = checkAll;
    }    
}

//Yeu cau dich vu
function cmdTinhTrangYCDV(Params)
{
    grvDV.PerformCallback(dv, tt);
}

function cmdYeuCauDichVu(ldv, dv){
    var Url = '/sanbds/khachhang/yeucaudichvu.aspx?ldv=' + ldv + '&dv=' + dv; 
    ShowPopup(830, 360, 'Yêu cầu dịch vụ', Url);
}

//====================Cookie================================
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) { endstr = document.cookie.length; }
  return unescape(document.cookie.substring(offset, endstr));
  }

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) {
      return getCookieVal (j);
      }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
    }
  return null;
  }

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
  }

function SetCookie (name,value,path,domain,secure) {
    var today = new Date();
    var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape (value) +
    ((expiry) ? "; expires=" + expiry.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
  }
  
//var a = window.external.IsSearchProviderInstalled('infoland.com.vn');
function addOpenSearch() {
    try { 
        var cook = GetCookie("InfolandSearchProvider");
        if(cook == null){
            SetCookie("InfolandSearchProvider", '1');
            window.external.AddSearchProvider('http://infoland.com.vn/opensearch.xml');             
        }
    }
    catch (e) {   }
}

function InstalledOpenSearch() {
    try { 
        window.external.AddSearchProvider('http://infoland.com.vn/opensearch.xml'); 
    }
    catch (e) {
        alert("Bạn cần sử dụng Internet Explorer (7.0 hoặc mới hơn) hoặc Firefox (2.0 hoặc mới hơn) đển cài đặt tiền ích OpenSearch của Infoland.");
        var isMSIE = navigator.userAgent.toLowerCase().indexOf("msie") != -1;
        var link = ( isMSIE ) ?  "http://www.microsoft.com/windows/internet-explorer/" : "http://www.getfirefox.com/";
        open(link);
    }
}
