﻿function loadErrorImage(id, src) {

    if (id.getAttribute("loi") == null) {
        id.setAttribute("loi", "1");
    }
    else {
        id.setAttribute("loi", eval(id.getAttribute("loi")) + 1);
    }
    if (eval(id.getAttribute("loi")) >= 2) {
        var width = src.substr(src.lastIndexOf("=") + 1, src.length - src.lastIndexOf("="));
        id.onerror = null;
        id.src = "http://cafef3.vcmedia.vn/v2/images/no_image.jpg";
    }
    else {
        id.src = src;
    }
}

function MenuActive(id) {
    if (id == 39 || id == 51 || id == 52 || id == 53 || id == 54 || id == 55) {
        $('#divMenuHanghoa').show();
        document.getElementById('a_39').className = 'active';
    }
    else {
        $('#divMenuHanghoa').hide();
    }

    var aLink = document.getElementById("a_" + id);
    if (aLink != null) {
        if (id == 0) {
            aLink.className = "trangchuActive active";
        }
        else { aLink.className = "active"; }
    }
    else {
        //aLink.className = "";
    }


}

var hours;
var minutes;
var seconds;
var dn;
function GetDate() {
    monthname = new Array("tháng 1", "tháng 2", "tháng 3", "tháng 4", "tháng 5", "tháng 6", "tháng 7", "tháng 8", "tháng 9", "tháng 10", "tháng 11", "Tháng 12");
    now = new Date();
    date = now.getDate();
    monthnum = now.getMonth() + 1;
    month = monthname[monthnum];

    hours = now.getHours();
    minutes = now.getMinutes();
    seconds = now.getSeconds();
    dn = "PM";
    if (hours < 12)
        dn = "AM";
    if (hours > 12)
        hours = hours - 12;
    if (hours == 0)
        hours = 12;
    if (minutes <= 9)
        minutes = "0" + minutes;

    var divTime = document.getElementById("datetime");
    /*divTime.innerHTML = "Ngày "+date + " " + month +" năm " +now.getFullYear() + "<br/>" + hours+":"+minutes + " "+dn;*/

    divTime.innerHTML = hours + ":" + minutes + " " + dn + " | " + date + "." + monthnum + "." + now.getFullYear();
}



function LoadTieuDiem_DNN(idTD, idDNN, bl, adnn, atd) {
    var divDNN, divTieuDiem, aDNN, aTD;
    divTieuDiem = document.getElementById(idTD);
    divDNN = document.getElementById(idDNN);
    aDNN = document.getElementById(adnn);
    aTD = document.getElementById(atd);

    /* bl=true : load doc nhieu nhat va nguoc lai*/

    if (bl) {
        divDNN.style.display = "block";
        divTieuDiem.style.display = "none";
        aDNN.className = "aEEE-active";
        aTD.className = "aEEE";
    }
    else {
        divDNN.style.display = "none";
        divTieuDiem.style.display = "block";
        aDNN.className = "aEEE";
        aTD.className = "aEEE-active";
    }
}

function LoadTinMoiHeader(index) {
    $.get('/ajax/tinmoi.aspx?pageindex=' + index, function(data) {
        $('#divTinMoi').html(data);
    });
}
function NewsletterRegister(email) {
    $.get('/ajax/newsletter.aspx?mail=' + email, function(data) {
        var i = data.toString().indexOf('ok=');
        if (i >= 0) $('#newsletter-note').html(data.toString().substr(i + 3, data.toString().length - i - 3));
        else window.alert('Co loi xay ra - Lien he             04-39749300       - may le 234');
    });
}
function LoadTinMoiNext() {
    var hdpageindex = document.getElementById('hdPageIndex');
    var index = hdpageindex.value;
    index = eval(eval(index) + 1);
    LoadTinMoiHeader(index);
    hdpageindex.value = index;
}
function LoadTinMoiPre() {
    var hdpageindex = document.getElementById('hdPageIndex');
    var index = hdpageindex.value;
    index = eval(eval(index) - 1);
    if (index < 1)
        index = 1;
    LoadTinMoiHeader(index);
    hdpageindex.value = index;
}

function openWindow(sUrl, title) {
    window.open(sUrl, title, 'scrollbars,resizable=yes,status=yes');
}

function ShareWeb(type) {
    var url = '';
    switch (type) {
        case 1:
            url = "http://www.facebook.com/sharer.php?u=" + window.location.href + "&p[title]=" + cutoffTitle(document.title);
            break;
        case 2:
            url = "http://twitthis.com/twit?url=" + window.location.href + "&title=" + cutoffTitle(document.title);
            break;
    }
    var newWindow = window.open(url, '', '_blank,resizable=yes,width=800,height=450');
    newWindow.focus();
    return false;
}
function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}
function getOneCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return y; //unescape(y);
        }
    }
}
function saveasCookie(c_obj1, c_obj2) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_obj1) {
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + 100);
            document.cookie = c_obj2 + '=' + y + "; expires=" + exdate.toUTCString() + "; domain=.cafef.vn";
            return;
        }
    }
}
function getCookie(c_nameobj, c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_nameobj) {
            var arr = y.split("&");
            for (j = 0; j < arr.length; j++) {
                var value = arr[j].substr(0, arr[j].indexOf("="));
                var _re = arr[j].substr(arr[j].indexOf("=") + 1);
                value = value.replace(/^\s+|\s+$/g, "");
                if (value == c_name) {
                    return _re;
                }
            }
        }
    }
}

function LoadUser() {
    convertCookie();
    var userName = getCookie('cafef.user.v2', 'name');
    
    if (userName.indexOf('&name=') > 0) {
        userName = userName.substr(userName.indexOf('&name=') + 6, 50);
        if (userName.indexOf('&') > 0) {
            userName = userName.substr(0, userName.indexOf('&'));
        }
    }
    if (userName.indexOf('&') >= 0) { userName = '' }
    if (userName != null && userName != '' && userName != 'undefined') {
        //var ming = getCookie('cafef.user', 'ming');
        /*var divLogin = document.getElementById('useraccount');
        if (divLogin == null) return;*/
        /*if (userName.length > 30) userName = userName.substring(0, 30);*/
         $("#useraccount").html('<a href="/danh-muc-dau-tu.chn" style="color:red; font-weight:bold">&nbsp;' + userName + '&nbsp;</a><span>|</span><a href="/pages/signout.aspx">Thoát</a>');
        $.ajax({ url: "/ajax/user.aspx", success: function(data) { if (data == '' || data == '0') { minglogin(true); } } });
        closeMingNote(false);        
    } else {
        displayMingNote();
        $('#danhmucdautu').click(function(e) { e.preventDefault(); minglogin(false); });
    }
    $("#useraccount").show();
}
function cutoffTitle(title) {
    title = title.toString();
    if (title.indexOf('|') > 0) { title = title.substr(0, title.lastIndexOf('|') - 1); }
    if (title.indexOf('|') > 0) { title = title.substr(0, title.lastIndexOf('|') - 1); }
    return title;
} /*
$(document).ready(function() {
    $('#newsletter-email').keypress(function(e) { if (e.which == 13) { e.preventDefault(); $('#newsletter-button').click(); } });
});*/
/* ming */
var modalWindow = {
    parent: "body",
    force: false,
    windowId: null,
    content: null,
    width: null,
    height: null,
    close: function() {
        if (this.force) return;
        $(".modal-window").remove();
        $(".modal-overlay").remove();
    },
    open: function() {
        var modal = "";
        modal += "<div class=\"modal-overlay\"></div>";
        modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:100%; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px; z-index:100000;\">"; /* " + this.height + "px */
        modal += this.content;
        modal += "</div>";

        $(this.parent).append(modal);

        if (!this.force) { $(".modal-window").append("<a class=\"close-window\"></a>"); }
        $(".close-window").click(function() { modalWindow.close(); });
        //$(".modal-overlay").click(function(){modalWindow.close();});

    }
};
function minglogin(_force) {
    /*
    var _width = 610;
    var Xpos = ((screen.availWidth - _width) / 2);
    var _height = 470;
    var Ypos = ((screen.availHeight - _height) / 2);
    window.mingdmdt = window.open('/danh-muc-dau-tu/ming.chn','mingloginwindow','width=' + _width + ',height='	+ _height + ',toolbar=no,location=no,resizable=fixed,status=no,scrollbars=no,menubar=no,screenX=' + Xpos + ',screenY=' + Ypos);
    mingdmdt.focus();
    */
    openMyModal('/danh-muc-dau-tu/ming.chn', 610, 470, _force);
    return false;
}
function mingregister() {
    /*
    var _width = 610;
    var Xpos = ((screen.availWidth - _width) / 2);
    var _height = 470;
    var Ypos = ((screen.availHeight - _height) / 2);
    window.mingdmdt = window.open('/danh-muc-dau-tu/mingregister.chn', 'mingloginwindow', 'width=' + _width + ',height=' + _height + ',toolbar=no,location=no,resizable=fixed,status=no,scrollbars=no,menubar=no,screenX=' + Xpos + ',screenY=' + Ypos);
    mingdmdt.focus();
    */
    openMyModal('/danh-muc-dau-tu/mingregister.chn', 610, 470, false);
    return false;
}
function openMyModal(source, width, height, _force) {
    modalWindow.windowId = "myModal";
    modalWindow.width = width;
    modalWindow.height = height;
    modalWindow.force = _force;
    modalWindow.content = "<iframe frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "' style='width:100%;height:100%'></iframe>";
    modalWindow.open();
}
function closeMyModal() {
    modalWindow.close();
    window.location.href = window.location.href;
}
function refreshPage() {
    window.location.reload();
}
function refreshParent() {
    window.opener.location.href = window.opener.location.href;
    if (window.opener.progressWindow) {
        window.opener.progressWindow.close();
    }
    window.close();
}
function displayMingNote() {
    var loc = window.location.toString().toLowerCase();
    var closed = getOneCookie("mingnote");
    if ((loc == 'http://beta5.cafef.vn/' || loc == 'http://cafef.vn/' || loc.indexOf('/default.aspx') >= 0 || loc.indexOf('/home.chn') >= 0) && closed != 'closed') {
        $('body').css('background-position', 'left -126px');
        $('body').prepend('<div id="mingnote" style="background-color: #EEF3FA; color: red; height: 30px; text-align: center;line-height:30px;"><div style="margin: 0 auto; width: 980px;"><div style="float: right; padding-top: 5px;"><a href="javascript:void(0)" onclick="closeMingNote(true)"><img border="0" src="http://cafef3.vcmedia.vn/v2/images/btnmingnote.1.png" style=""></a></div><b>Từ ngày 23/11/2011, CafeF sử dụng <a href="http://id.ming.vn" target="_blank">Ming ID</a> để đăng nhập Danh mục đầu tư. <a href="/danh-muc-dau-tu/mingsupport.chn#mingsupport">Xem hướng dẫn</a>. Hỗ trợ : <a href="mailto:hotrokythuat@cafef.vn">hotrokythuat@cafef.vn</a> - <span style="color:#004370">(04) 3974 3410 </span> - máy lẻ : <span style="color:#004370">234</span>.</b></div></div>');
    }
}
function closeMingNote(_saved) {
    $('body').css('background-position', 'left -156px');
    $('#mingnote').remove();
    if (_saved) {
        setCookie("mingnote", "closed", 1);
    }
}
function convertCookie() {
    var converted = getOneCookie("converted");
    if (converted == 'converted') return;
    var userName = getCookie('cafef.user', 'name');
    if (userName != null && userName != '' && userName != 'undefined') {
        saveasCookie('cafef.user', 'cafef.user.v2');
        setCookie("converted", "converted", 1000);
    }
}
