﻿// JScript File

function HighlightTab(id)
{
    var tag = document.getElementById(id);
    if(!tag) return;
    tag.className = "SelectedTabText";
}

function CreatePager(container, pageCount, currentPage, url)
{
    container = document.getElementById(container);
    if(!container) return;
    var select = document.createElement("SELECT");
    select.className = "Pages";
    for(var i = 0; i < pageCount; i++)
    {
        var option = document.createElement("OPTION");
        option.value = i;
        option.innerHTML = i + 1;
        select.appendChild(option);
    }
    select.selectedIndex = currentPage;
    select.onchange = function()
        {
            location.href = url.replace("{page}", this.selectedIndex);
        }
    container.appendChild(select);
}

function ShowImage(image, largeImage)
{
    if(!image) return;
    image.style.display = "";
    if(largeImage)
    {
        image.style.cursor = "pointer";
        image.onclick = function()
        {
            PopupImage(largeImage);
        }
    }
}

function TextboxOnBlur(textbox)
{
    if(!textbox) return;
    if(!textbox.valueChanged && textbox.value != textbox.defaultValue)
    {
        textbox.value = textbox.defaultValue;
    }
}

function ShowDate(container, date)
{
    if(!date) date = new Date();
    var dateNames = new Array("Chủ nhật", "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy");
    var dateTemplate = "{date}, {day}/{month}/{year}"
    dateTemplate = dateTemplate.replace("{date}", dateNames[date.getDay()]);
    dateTemplate = dateTemplate.replace("{day}", date.getDate());
    dateTemplate = dateTemplate.replace("{month}", date.getMonth() + 1);
    dateTemplate = dateTemplate.replace("{year}", date.getFullYear());
    if(!container)
        document.write(dateTemplate);
    else
    {
        container = document.getElementById(container);
        if(container) container.innerHTML = dateTemplate;
    }
}

function InitDefaultValueTextbox(textbox)
{
    if(!textbox) return;
    if(textbox.initialized) return;
    textbox.initialized = true;
    textbox.onfocus = function()
        {
            if(!this.valueChanged)
            {
                this.defaultValue = textbox.value;
                this.value = "";
            }
        }
    textbox.onchange = function()
        {
            this.valueChanged = true;
        }
    
    textbox.onblur = function()
        {
            if(this.value)
            {
                if(!this.valueChanged && this.value != this.defaultValue)
                {
                    this.value = this.defaultValue;
                }
            }
            else
            {
                this.valueChanged = false;
                this.value = this.defaultValue;
            }
        }
        
    textbox.onfocus();
}

function createAd(directOutput, tooltip, source, flashSource, popupSource, link, isBlock, isSWF, width, height)
{
    var html = "";
    if(isSWF)
    {
        html = "<a href='" + link + "' target='_blank'><embed quality='High' style='margin: 0px; margin-bottom: -2px; padding: 0px;' wmode='transparent' border='0' src='" + flashSource + "' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' " + (width == "auto"?"":"width='" + width) + "' " + (height == "auto"?"":"height='" + height) + "' /></a>";
    }
    else
    {
        html = "<a href='" + link + "' target='_blank'><img src='" + source + "' alt='" + tooltip + "' " + (width == "auto"?"":"width='" + width) + "' " + (height == "auto"?"":"height='" + height) + "'/></a>";
    }
    if(!directOutput)
    {
        var container = null;
        if(isBlock)
            container = document.createElement("SPAN");
        else
            container = document.createElement("DIV");
        container.innerHTML = html;
        return container;
    }
    else
    {
        if(isBlock)
            html = "<div>" + html + "</div>";
        else
            html = "<span>" + html + "</span>";
        document.write(html);
    }
}

function PopupImage(url)
{
    if(!window.popupWindow)
    {
        window.popupWindow = document.createElement("IMG");
        
        window.popupWindow.className = "PopupImage";
        window.popupWindow.onclick = function()
            {
                this.parentNode.removeChild(this);
                window.popupWindow = null;
            }
        window.popupWindow.onload = function()
            {
                var top, left;
                top = (typeof(window.pageYOffset) == "undefined") ? document.documentElement.scrollTop : window.pageYOffset;
                left = document.body.clientWidth - this.offsetWidth - 5;
                this.style.left = left + "px";
                this.style.top = top + "px";
                this.style.visibility = "visible";
            }

        document.body.appendChild(window.popupWindow);
    }
    window.popupWindow.style.visibility = "hidden";
    window.popupWindow.src = url;
    window.popupWindow.alt = "Nhấn vào đây để đóng lại";
    
    return;
}