﻿function $(id)
{
    return document.getElementById(id);
}

function $$(obj, tag)
{
    return (obj ? obj.getElementsByTagName(tag) : null);
}

function message(message, options)
{
    var msg = $("YBConfirmMessage");
    
    if(!msg)
    {
        msg = document.createElement("div");
        msg.id = "YBConfirmMessage";
        document.body.appendChild(msg);
    }
    
    msg.innerHTML = "";

    msg.appendChild(document.createElement("div")).innerHTML = message;

    var buttonPanel = msg.appendChild(document.createElement("div"));

    buttonPanel.style.marginTop = "20px";
    buttonPanel.style.textAlign = "center";

    if(options)
    {
        for(var property in options)
        {
            var button = document.createElement("input");

            button.type = "button";
            button.value = property;
            button.className = "Button";
            
            if(options[property] == "{CANCEL}")
            {
                button.onclick = function()
                {
                    document.body.removeChild($("YBConfirmMessage"));
                }
            }
            else
            {
                button.__clickEvent = options[property];
                button.onclick = function()
                {
                    this.__clickEvent();
                    document.body.removeChild($("YBConfirmMessage"));
                }
            }

            buttonPanel.appendChild(button);
            buttonPanel.appendChild(document.createTextNode(" "));
        }
    }
    else
    {
        var button = document.createElement("input");

        button.type = "button";
        button.value = "ok";
        button.className = "Button";
        button.onclick = function()
        {
            document.body.removeChild($("YBConfirmMessage"));
        }

        buttonPanel.appendChild(button);
    }
    
    var size = getBrowserSize();
    
    msg.style.position = "absolute";
    msg.style.width = "300px";
    msg.style.left = (size.width / 2 - msg.offsetWidth / 2 + getScrollX()) + "px";
    msg.style.top = (size.height / 2 - msg.offsetHeight / 2 + getScrollY()) + "px";
}

function getScrollX()
{
    var x = 0;
    
    if(typeof(window.pageYOffset) == "number")
    {
        //Netscape compliant
        x = window.pageXOffset;
    }
    else if(document.body && (document.body.scrollLeft || document.body.scrollTop))
    {
        //DOM compliant
        x = document.body.scrollLeft;
    }
    else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
    {
        //IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
    }
    return x;
}

function getScrollY()
{
    var y = 0;

    if(typeof(window.pageYOffset) == "number")
    {
        //Netscape compliant
        y = window.pageYOffset;
    }
    else if(document.body && (document.body.scrollLeft || document.body.scrollTop))
    {
        //DOM compliant
        y = document.body.scrollTop;
    }
    else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
    {
        //IE6 standards compliant mode
        y = document.documentElement.scrollTop;
    }

    return y;
}

function getBrowserSize()
{
    if(window.innerWidth)
        return { width:window.innerWidth, height:window.innerHeight };
    else if(document.documentElement.clientWidth)
        return { width:document.documentElement.clientWidth, height:document.documentElement.clientHeight };
    else if(document.body.offsetWidth)
        return { width:document.body.offsetWidth, height:document.body.offsetHeight };
    else
        return { width:null, height:null };
}

function ajaxSubmitForm(form, url, frameId, callback, debug)
{
    // Create HTTP POST frame
    var frameHolder = document.createElement("div");
    
    frameHolder.innerHTML = "<iframe id=\"" + frameId + "\" name=\"" + frameId + "\"></iframe>";
    
    if(!debug)
    {
        frameHolder.style.width = "1px";
        frameHolder.style.height = "1px";
        frameHolder.style.position = "absolute";
        frameHolder.style.left = "-100px";
        frameHolder.style.overflow = "hidden";
    }

    document.body.appendChild(frameHolder);

    // Setup HTTP POST frame complete event handler.
    var frame = document.getElementById(frameId);

    // IE form loaded function
    frame.onreadystatechange = function()
    {
        if(this.readyState.toString() == "complete")
        {
            var result = "";

            for(var i=0; i<document.frames.length; i++)
            {
                if(document.frames[i].name == frameId)
                {
                    result = document.frames[i].document.body.innerHTML.trim();
                    break;
                }
            }

            if(!debug) document.body.removeChild(frame.parentNode);
            if(typeof callback == "function") callback(result);
        }
    }

    // Others form loaded function
    frame.onload = function()
    {
        var result = this.contentDocument.body.innerHTML.trim();

        function removeUploadFrame()
        {
            if(!debug) document.body.removeChild(frame.parentNode);
            if(typeof callback == "function") callback(result);
        }
        
        setTimeout(removeUploadFrame, 100);
    }

    form.action = url;
    form.target = frameId;
    form.submit();
}

topPosition = function(obj)
{
    var top = obj.offsetTop;
    var container = obj.offsetParent;
    
    while(container)
    {
        top += container.offsetTop;
        container = container.offsetParent;
    }
    
    return top;
}

bottomPosition = function(obj)
{
    var top = obj.offsetTop;
    var container = obj.offsetParent;
    
    while(container)
    {
        top += container.offsetTop;
        container = container.offsetParent;
    }
    
    return (top + obj.offsetHeight);
}

leftPosition = function(obj)
{
    var left = obj.offsetLeft;
    var container = obj.offsetParent;
    
    while(container)
    {
        left += container.offsetLeft;
        container = container.offsetParent;
    }
    
    return left;
}

rightPosition = function(obj)
{
    var left = obj.offsetLeft;
    var container = obj.offsetParent;
    
    while(container)
    {
        left += container.offsetLeft;
        container = container.offsetParent;
    }
    
    return (left + obj.offsetWidth);
}

function addEvent(object, eventType, eventFunction)
{
    if (object.attachEvent)
    {
        object["e" + eventType + eventFunction] = eventFunction;
        object[eventType + eventFunction] = function() { object["e" + eventType + eventFunction]( window.event ); };
        object.attachEvent("on" + eventType, object[eventType + eventFunction]);
    }
    else
    {
        object.addEventListener(eventType, eventFunction, false);
    }
}

function removeEvent(object, eventType, eventFunction)
{
    if(object.detachEvent)
    {
        object.detachEvent("on" + eventType, object[eventType + eventFunction]);
        object[eventType + eventFunction] = null;
        object["e" + eventType + eventFunction] = null;
    }
    else
    {
        object.removeEventListener(eventType, eventFunction, false);
    }
}

function pagination(totalPage, currentPage, ajaxUrl)
{
    var pageOnDisplay = 5;
    
    totalPage = parseInt(totalPage);
    currentPage = parseInt(currentPage);

    if (currentPage < 1)
    {
        currentPage = 1;
    }
    else if (totalPage > 0 && currentPage > totalPage)
    {
        currentPage = totalPage;
    }

    var startPage = currentPage - 2;
    var endPage = currentPage + 2;

    if (startPage < 1) startPage = 1;
    if (endPage > totalPage) endPage = totalPage;

    var freeSpace = (pageOnDisplay - 1) - (endPage - startPage);
    if (freeSpace > 0 && endPage < totalPage)
    {
        if (totalPage - endPage < freeSpace) freeSpace = totalPage - endPage;
        endPage += freeSpace;
    }

    freeSpace = (pageOnDisplay - 1) - (endPage - startPage);
    if (freeSpace > 0 && startPage > 1)
    {
        if (startPage - 1 < freeSpace) freeSpace = startPage - 1;
        startPage -= freeSpace;
    }

    var html = "<div>";

    html += "page ";
    for (var i = startPage; i <= endPage; i++)
    {
        html += (i != currentPage ? "<a href=\"javascript:;\"" + (ajaxUrl != null && ajaxUrl.length > 0 ? " onclick=\"" + ajaxUrl.format(i) + "\"" : "") + ">" + i + "</a> " : "<span style=\"font-weight:bold;\">" + i + "</span> ");
    }

    html += (currentPage > 1 ? "<a href=\"javascript:;\"" + (ajaxUrl != null && ajaxUrl.length > 0 ? " onclick=\"" + ajaxUrl.format(currentPage - 1) + "\"" : "") + ">previous</a> | " : "<span>previous</span> | ");
    html += (currentPage < totalPage ? "<a href=\"javascript:;\"" + (ajaxUrl != null && ajaxUrl.length > 0 ? " onclick=\"" + ajaxUrl.format(currentPage + 1) + "\"" : "") + ">next</a>" : "<span>next</span>");

    html += "</div>";
    
    return html;
}

Array.prototype.indexOf = function(object)
{
    for(var i=0; i<this.length; i++)
    {
        if(object == this[i]) return i;
    }
    
    return -1;
}

String.prototype.ltrim = function() 
{
    return this.replace(new RegExp("^[\\s]+", "ig"), "");
}

String.prototype.rtrim = function() 
{
    return this.replace(new RegExp("[\\s]+$", "ig"), "");
}

String.prototype.trim = function()
{
    return this.ltrim().rtrim();
}

String.prototype.startsWith = function(match)
{
    if(match.length == 0 || match.length > this.length) return false;
    
    return (this.substring(0, match.length) == match);
}

String.prototype.urlEncode = function()
{
    return escape(this).replace(new RegExp("\\+", "g"), "%2B");
}

String.prototype.htmlEncode = function()
{
    var div = document.createElement("div");
    var text = document.createTextNode(this);

    div.appendChild(this);
    return div.innerHTML;
}

String.prototype.toInt = function()
{
    return parseInt(this);
}

String.prototype.toFloat = function()
{
    return parseFloat(this);
}

String.prototype.format = function()
{
    var formatted = this;
    
    for(var i=0; i<arguments.length; i++)
    {
        formatted = formatted.replace(new RegExp("[{][" + i + "][}]", "ig"), arguments[i]);
    }
    
    return formatted;
}
