/* Common functions */

function SwapImage(image, imagePath)
{
    image.src = imagePath;
}

function HighLight(row)
{
    row.className = "Hover";
}

function UnHighLight(row)
{
    row.className = "Inactive";
}

function Browse(url)
{
    if (url != null && url != "")
    {
        if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1)
        {
            window.open("http://" + url);
        }
        else
        {
            window.open(url);
        }
    }
}

function MailTo(url)
{
    if (url != null && url != "")
    {
        if (url.indexOf("mailto:") == -1)
        {
            window.open("mailto:" + url);
        }
        else
        {
            window.open(url);
        }
    }
}

function ZoomImage(title, imagePath)
{
    var image = new Image();

    image.src = imagePath;
    image.onload = function()
    {
        ShowImage(title, image);
    }

    if (image.complete)
    {
        ShowImage(title, image);
    }
}

function ShowImage(title, image)
{
    if (image.width > 800)
    {
        image.height = image.height * 800 / image.width;
        image.width = 800;
    }

    if (image.height > 600)
    {
        image.width = image.width * 600 / image.height;
        image.height = 600;
    }

    var left = (screen.availWidth / 2) - (image.width / 2);
    var top = (screen.availHeight / 2) - (image.height / 2);
    var settings = "toolbar=no,status=no,help=no,resizable=no,left=" + left + ",top=" + top + ",width=" + image.width + ",height=" + image.height;

    var imageWindow = window.open(image.src, "", settings);
    var html = "<html><head><title>" + title + "</title></head><body style='margin: 0 0 0 0'><img src='" +
	    image.src + "' width=" + image.width + "height=" + image.height + "></body></html>";

    imageWindow.document.write(html);
    imageWindow.resizeBy(image.width - imageWindow.document.body.clientWidth, image.height - imageWindow.document.body.clientHeight);
    imageWindow.focus();
}

function OnKeyDown(e, btnButton)
{ 
    var intKey = -1; 

    if (e && e.which)
    {
        intKey = e.which;    // NS
    }
    else 
    {
        if (window.event && window.event.keyCode)
        {
            intKey = window.event.keyCode;  // IE
        }
    }
    
    if (intKey == 13) 
    { 
         document.getElementById(btnButton).click(); 
         return false; 
    } 

    return true; 
}

function IsMaxLength(textBox, maxLength, e)
{
    var key = (window.event ? e.keyCode : e.which);
    var validKeys = "8,33,34,35,36,37,38,39,40,46";

    if (validKeys.indexOf(key) == -1)
    {
        return textBox.value.length <= maxLength;
    }

    return true;
}

function TruncTextBoxValue(textBox, maxLength)
{
    if (textBox.value.length > maxLength)
    {
        textBox.value = textBox.value.substring(0, maxLength);
        textBox.scrollTop = textBox.scrollHeight;
    }
}

function CheckRadioButton(nameregex, current)
{
    re = new RegExp(nameregex);

    for (i = 0; i < document.forms[0].elements.length; i++)
    {
        elm = document.forms[0].elements[i];

        if (elm.type == 'radio')
        {
            if (re.test(elm.name))
            {
                elm.checked = false;
            }
        }
    }

    current.checked = true;
}

function XDiv(id)
{
    var div = document.getElementById(id);

    if (div)
    {
        var mode = "no" + "ne";

        div.style.display = mode;
    }
}

function XCheck(checkBox, groupName)
{
    var inputs = document.getElementsByTagName("input");

    for (var index = 0; index < inputs.length; index++)
    {
        if (inputs[index].parentNode != null && inputs[index].parentNode.getAttribute("GroupName") == groupName && inputs[index].id != checkBox.id)
        {
            inputs[index].checked = false;
        }
    }
}

function SetCaretPosition(textBoxID, position)
{
    var textBox = document.getElementById(textBoxID);

    if (textBox != null)
    {
        if (textBox.createTextRange)
        {
            var range = textBox.createTextRange();
            
            range.move('character', position);
            range.select();
        }
        else
        {
            if (textBox.selectionStart)
            {
                textBox.focus();
                textBox.setSelectionRange(position, position);
            }
            else
            {
                textBox.focus();
            }
        }
    }
}

String.prototype.trim = function ()
{
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

/* Popups */

function Popup(strUrl, strName, intWidth, intHeight)
{
    return XPopup(strUrl, strName, intWidth, intHeight, "no");
}

function XPopup(strUrl, strName, intWidth, intHeight, strScrollbars)
{
    var str = "menubar=no,status=no,scrollbars=" + strScrollbars + ",toolbar=no,location=no,directories=no,resizable=no," +
        "height=" + intHeight + ",innerHeight=" + intHeight +
        ",width=" + intWidth + ",innerWidth=" + intWidth;

    if (window.screen)
    {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;

        var xc = (aw - intWidth) / 2;
        var yc = (ah - intHeight) / 2;

        str += ",left=" + xc + ",screenX=" + xc;
        str += ",top=" + yc + ",screenY=" + yc;
    }

    return window.open(strUrl, strName, str);
}

/* WebService calls */

var _xmlHttpRequest = null;

function SendPostRequest(url, params, onCompleted)
{
    if (window.XMLHttpRequest)
    {
        _xmlHttpRequest = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        _xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }

    _xmlHttpRequest.open("POST", url, true);
    _xmlHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    _xmlHttpRequest.setRequestHeader("Content-Length", params.length);
    
    if (onCompleted != null)
    {
        _xmlHttpRequest.onreadystatechange = onCompleted;
    }
    
    _xmlHttpRequest.send(params);
}

function SendSoapRequest(url, params, onCompleted)
{
    var soapEnvelope = "";
    
    SendPostRequest(url, soapEnvelope, onCompleted);
}
