﻿// JScript File

function ShowException(ex)
{
    if (!ex)
        return;
        
    var message = ex.name + ': ' + ((ex.message) ? ex.message : ex.description);
    alert(message);
}

function GetRadioButtonValue(name)
{
    var radioElements = document.getElementsByName(name);
    if (!radioElements)
    {
        alert('No radio button "' + name + '"elements were received.');
        return '';
    }

    for (var index = 0; index < radioElements.length; index++)
    {
        if (('radio' == radioElements[index].type) &&
            (radioElements[index].checked))
            return radioElements[index].value;
    }
    
    return '';
}

function ResetRadioButtonValue(name)
{
    var radioElements = document.getElementsByName(name);
    if (!radioElements)
    {
        alert('No radio button "' + name + '"elements were received.');
        return;
    }

    for (var index = 0; index < radioElements.length; index++)
    {
        if (('radio' == radioElements[index].type) &&
            (radioElements[index].checked))
            radioElements[index].checked = false;
    }
}

function SetVisible(id, visible)
{
    var obj = document.getElementById(id);
    if (null != obj)
    {
        if (visible)
            obj.style.display = "";
        else
            obj.style.display = "none";
    }
    else
        alert('Called SetVisible(' + id + ', ' + visible + ') on invalid object.');
}

function SetStyleColor(id, color)
{
    var obj = document.getElementById(id);
    if (null != obj)
        obj.style.color = color;
    else
        alert('Called SetStyleColor(' + id + ', ' + color + ') on invalid object.');
}

function SetStyleBackColor(id, color)
{
    var obj = document.getElementById(id);
    if (null != obj)
        obj.style.backgroundColor = color;
    else
        alert('Called SetStyleColor(' + id + ', ' + color + ') on invalid object.');
}

function SetDisabled(id, disabled)
{
    var obj = document.getElementById(id);
    if (null != obj)
        obj.disabled = disabled;
    else
        alert('Called SetDisabled(' + id + ', ' + disabled + ') on invalid object.');
}

function SetInnerHTML(id, value)
{
    var obj = document.getElementById(id);
    if (null != obj)
        obj.innerHTML = value;
    else
        alert('Called SetInnerHTML(' + id + ', ' + value + ') on invalid object.');
}

function SetFocus(id)
{
    var obj = document.getElementById(id);
    if (null != obj)
    {
        try
        {
            if (!obj.disabled && (obj.style.display.toLowerCase() != 'none'))
                obj.focus();
        }
        catch (ex)
        {
        }
    }
    else
        alert('Called SetFocus(' + id + ') on invalid object.');
}

function SetValue(id, value)
{
    var obj = document.getElementById(id);
    if (null != obj)
        obj.value = value;
    else
        alert('Called SetValue(' + id + ', ' + value + ') on invalid object.');
}

function GetValue(id)
{
    var obj = document.getElementById(id);
    if (null != obj)
        return obj.value;
    else
        alert('Called GetValue(' + id + ') on invalid object.');
}

function EscapePath(path)
{
    return path.replace(/\\/g, "\\\\").split("'").join("\\'");
}

function PasteFromClipboard(id)
{
    var obj = document.getElementById(id);
    if (!obj)
    {
        alert('Called PasteFromClipboard(' + id + ') on invalid object.');
        return;
    }
        
    var textRange = obj.createTextRange();
    textRange.select();
    textRange.execCommand('Paste');
    textRange.collapse();   
}

var controlsForDisabling = new Array();

function RegisterForDisableOnSumbit(control)
{
    if(controlsForDisabling != null && controlsForDisabling != "undefined"
        && control != null && control != "undefined")
    {        
        controlsForDisabling[controlsForDisabling.length] = control;
    }
}

function DisableOnSumbit()
{
    if(controlsForDisabling != null && controlsForDisabling != "undefined")
    {
        for(var index = 0; index < controlsForDisabling.length; index++)
        {
           SetDisabled(controlsForDisabling[index],true);            
        }
    }
}

var objHttp = null;
if (window.ActiveXObject)
	objHttp = new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest)
	objHttp = new XMLHttpRequest();

var submitOjbect = null;

function InitSubmitButton(button, url)
{
	submitOjbect = button;
    if (!objHttp) {
		alert("Unable to create Microsoft.XMLHTTP object");
		return;
	}
	
	SetDisabled(submitOjbect, true);

	objHttp.open("POST", url + "?checkavailability=true", true);
	objHttp.onreadystatechange = InitCompleted;
	objHttp.send();
}


function InitCompleted()
{
	if ((4 == objHttp.readyState) && (200 == objHttp.status))
	{
	    var availabilityResponse = objHttp.responseText;
		SetDisabled(submitOjbect, availabilityResponse == "false");
	}
}
