﻿self.setInterval('loadData()', 10000)
    
var obj1, obj2, obj3;
var loading = "<div style='font-family:Verdana; font-size:11px; width: 130px'><img style='margin: 2px; vertical-align: middle' src='Images/Loading.gif' />Đang tải dữ liệu ...</div>"

function getText() {
    var valCode = false;
    var tblData = document.getElementById("tblData");
    tblData.style.visibility = "visible";
    tblData.style.position = "relative";
    
    var check = document.body.getElementsByTagName("input");
    var value = ""
    for(i=0; i<check.length; i++)
	    if(check(i).type=="checkbox" && check(i).checked) {
	        if (check(i).name.substr(0,7)=="dltCode") {
		        value += check[i].value;
		        value += "|"
		        valCode = true;
		    }
	    }
    
    if (valCode == false)
        alert("Phải chọn công ty chứng khoán");
    else {
        var c = new CookieHandler();
        c.setCookie('stockChoice', value, 60*60*24);
        c.deleteCookie('induChoice');
        
        var tdDataList = document.getElementById("tdDataList");
        tdDataList.style.visibility = "hidden";
        tdDataList.style.position = "absolute";
    }
}

function getTextIndu() {
    var valIndu = false;
    var tblData = document.getElementById("tblData");
    tblData.style.visibility = "visible";
    tblData.style.position = "relative";
    
    var check = document.body.getElementsByTagName("input");
    var value = ""
    for(i=0; i<check.length; i++)
	    if(check(i).type=="checkbox" && check(i).checked) {
	        if (check(i).name.substr(0,7)=="dltIndu") {
		        value += check[i].value;
		        value += "|"
		        valIndu = true;
		    }
	    }
    
    
    if (valIndu == false)
        alert("Phải chọn nghành nghề");
    else {
        var c = new CookieHandler();
        c.setCookie('induChoice', value, 60*60*24);
        c.deleteCookie('stockChoice');
        
        var tdDataListIndu = document.getElementById("tdDataListIndu");
        tdDataListIndu.style.visibility = "hidden";
        tdDataListIndu.style.position = "absolute";
    }
}

function getTime() {
    var rightnow= new Date();

    var thehours= rightnow.getHours();
    var themins= rightnow.getMinutes();
    var theseconds= rightnow.getSeconds();
    
    if (thehours < 11)
        var ampm = "Sáng";
    else if (thehours < 14)
        var ampm = "Trưa";
    else if (thehours < 18)
        var ampm = "Chiều";
    else
        var ampm = "Tối";
    if (thehours == 0)
        thehours = 12;
    if (thehours >= 13)
        thehours -= 12;
    if (thehours < 10)
        thehours = "0" + thehours;
    if (themins < 10)
        themins = "0" + themins;
    if (theseconds < 10)
        theseconds = "0" + theseconds;
        
    return thehours + " giờ " + themins + " phút " + theseconds + " giây (" + ampm + ")"
}

function CheckAll(chk) {
	var check = document.body.getElementsByTagName("input");
	var val = chk;
	var i;
	for (i=0; i<check.length; i++)
		if (check(i).type=="checkbox" && !check(i).disabled)
			check(i).checked = val;
}

function getHeight() {
    return (document.body.clientHeight-100);
}

function loadData() {
    var tblData = document.getElementById("tblData");

    tblData.style.visibility = "visible";
    tblData.style.position = "relative";
    
    var url = "AjaxTable.aspx";
    
    if (document.cookie.indexOf("stockChoice") != -1) {
        url = "AjaxTable.aspx?type=code";
    }
    else if (document.cookie.indexOf("induChoice") != -1) {
        url = "AjaxTable.aspx?type=indu";
    }
        
    var xRequest=null;

    if (window.XMLHttpRequest)
        xRequest = new XMLHttpRequest();
    else if (typeof ActiveXObject != "undefined")
        xRequest = new ActiveXObject("Microsoft.XMLHTTP");

    obj1 = xRequest;

    if (obj1 != null) {
        obj1.onreadystatechange = function() {
            if (obj1.readyState < 4)
                window.status = "Đang tải dữ liệu ...";

            if (obj1.readyState == 4)
                if (obj1.status == 200) {
                    window.status = "Bảng giá cập nhật lúc : " + getTime();
                    var retval=obj1.responseText;
                    if (document.getElementById("resultsData") != null)
	                    document.getElementById("resultsData").innerHTML = retval;
                    else
	                    alert("Error retrieving data!");
                }
        }
        obj1.open("GET",url,true);
        obj1.send(null);
    }
}

function loadDataList(code) {
    var check = document.body.getElementsByTagName("input");
    var tdDataListIndu = document.getElementById("tdDataListIndu");
    var tdDataList = document.getElementById("tdDataList");
    var type = "";
    var url;
    for(i=0; i<check.length; i++)
	    if(check(i).type=="radio" && check(i).checked)
		    if (check[i].value == 'rbnIndu') {
		        url = "AjaxDataList.aspx?type=indu&search=" + encodeURI(code);
		        type = "indu";
		    }
		    else {
		        url = "AjaxDataList.aspx?type=code&search=" + code;
		        type = "code";
		    }
        
    var xRequest=null;

    if (window.XMLHttpRequest)
        xRequest = new XMLHttpRequest();
    else if (typeof ActiveXObject != "undefined")
        xRequest = new ActiveXObject("Microsoft.XMLHTTP");

    obj2 = xRequest;

    if (obj2!=null) {
        obj2.onreadystatechange = function() {
	        if (obj2.readyState < 4)
                document.getElementById("Loading").innerHTML = loading;

            if (obj2.readyState == 4)
                if (obj2.status == 200) {
                    document.getElementById("Loading").innerHTML = "";
	                var retval=obj2.responseText;
	                if (type == "code") {
	                    tdDataList.style.visibility = "visible";
                        tdDataList.style.position = "relative";
                        
	                    if (document.getElementById("resultsDataList") != null)
		                    document.getElementById("resultsDataList").innerHTML = retval;
	                    else
		                    alert("Error retrieving data!");
		            }
		            else {
	                    tdDataListIndu.style.visibility = "visible";
                        tdDataListIndu.style.position = "relative";

	                    if (document.getElementById("resultsDataListIndu") != null)
		                    document.getElementById("resultsDataListIndu").innerHTML = retval;
	                    else
		                    alert("Error retrieving data!");
		            }
                }
        }

        obj2.open("GET",url,true);
        obj2.send(null);
    }
}

function OnChange(val) {
    var tblCode = document.getElementById("tblCode");
    var tblIndu = document.getElementById("tblIndu");
    var tdDataListIndu = document.getElementById("tdDataListIndu");
    var tdDataList = document.getElementById("tdDataList");
    var c = new CookieHandler();
        
    var check = document.body.getElementsByTagName("input");
    for(i=0; i<check.length; i++)
	    if(check(i).type=="checkbox" && check(i).checked)
		        check[i].checked = false;
	    
    if (val=="Code") {
        c.setCookie('checkStock', 'true', 60*60*24);
        tblCode.style.visibility = "visible";
        tblCode.style.position = "relative";
        tblIndu.style.visibility = "hidden";
        tblIndu.style.position = "absolute";
        tdDataListIndu.style.visibility = "hidden";
        tdDataListIndu.style.position = "absolute";
    }
    else {
        c.setCookie('checkStock', 'false', 60*60*24);
        tblCode.style.visibility = "hidden";
        tblCode.style.position = "absolute";
        tdDataList.style.visibility = "hidden";
        tdDataList.style.position = "absolute";
        tblIndu.style.visibility = "visible";
        tblIndu.style.position = "relative";
    }
}

function CookieHandler() {
 
	this.setCookie = function (name, value, seconds) {
 
		if (typeof(seconds) != 'undefined') {
			var date = new Date();
			date.setTime(date.getTime() + (seconds*1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else {
			var expires = "";
		}
 
		document.cookie = name+"="+value+expires+"; path=/";
	}
    
    this.GetCookieVal = function (name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
	        var c = ca[i];
	        while (c.charAt(0)==' ') c = c.substring(1,c.length);
	        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }
 
	this.deleteCookie = function (name) {
		this.setCookie(name, "", -1);
	}
}

function loadBegin() {
    var tblCode = document.getElementById("tblCode");
    var tblIndu = document.getElementById("tblIndu");
    var check = document.body.getElementsByTagName("input");
    var c = new CookieHandler();
    
    if (c.GetCookieVal('checkStock') == 'false') {
        for (i=0; i<check.length; i++)
        if (check(i).type == "radio") {
            if (check(i).value == "rbnIndu") {
	            check[i].checked = true;
	            tblCode.style.visibility = "hidden";
                tblCode.style.position = "absolute";
                tblIndu.style.visibility = "visible";
                tblIndu.style.position = "relative";
	        }
        }
    }
    else if (c.GetCookieVal('checkStock') == 'true') {
        for (i=0; i<check.length; i++)
        if (check(i).type == "radio") {
            if (check(i).value == "rbnCode") {
	            check[i].checked = true;
	            tblCode.style.visibility = "visible";
                tblCode.style.position = "relative";
                tblIndu.style.visibility = "hidden";
                tblIndu.style.position = "absolute";
	        }
        }
    }
    else {
        c.setCookie('checkStock', 'true', 60*60*24);
        tblIndu.style.visibility = "hidden";
        tblIndu.style.position = "absolute";    
    }
}

function getIndu(indu) {
    var tdDataListIndu = document.getElementById("tdDataListIndu");
 
    tdDataListIndu.style.visibility = "hidden";
    tdDataListIndu.style.position = "absolute";   
    loadDataList(indu);
}

function getIndu1(keysearch) {
    keysearch = document.getElementById('txtNameIndu').value;
    var tdDataListIndu = document.getElementById("tdDataListIndu");
 
    tdDataListIndu.style.visibility = "hidden";
    tdDataListIndu.style.position = "absolute";   
    loadDataList(keysearch);
}

function getCode(code) {
    var tdDataList = document.getElementById("tdDataList");
 
    tdDataList.style.visibility = "hidden";
    tdDataList.style.position = "absolute";   
    loadDataList(code);
}