function ShowWaitGif(status)
{
    var img = GetElementByID('gearimg');
    if (typeof (img) != "undefined") 
    { 
        if (status) img.src = img.src //restore animation (exception in treeview expand JS) 
        img.style.display = status ? "inline" : "none";
    } 
}

function GetElementByID(elm)  
{  
    if (document.layers) {  
        if (typeof elm == "string") {  
            // just one layer deep  
            return document.layers[elm]  
        }  
    }  
  
    if (document.all) {  
        if (typeof elm == "string") {  
            return document.all(elm)  
        }  
    }  
  
    if (document.GetElementByID) {  
        if (typeof elm == "string") {  
            return document.GetElementByID(elm);  
        }  
    }  
      
    return null  
} 

function ChangeVisibility(control)
{
	var c = GetElementByID (control)
	if (c == null) return

	c.style.display = c.style.display == "none" ? "inline" : "none"
}

function CBChecked (checkbox)
{
	var cb = GetElementByID(checkbox)
	if (cb == null) return false
	return cb.checked
}

function CopyValues (source)
{
	if (arguments.length < 2) return
	var from = GetElementByID(source)
	if (from == null) return
	
	var from_value = ""
	switch (from.tagName)
	{
		case "SPAN":
			from_value = from.innerHTML
			break
		case "INPUT":
			from_value = from.value
			break
	}
	if (from_value == "") return

	for (var i = 1; i < arguments.length; i++)
	{	
		var to = GetElementByID(arguments[i])
		if (to == null || to.value != "") continue
		to.value = from_value
	}
}

function SetTimeDropDown(textbox, dropdown)
{
	var tb = GetElementByID(textbox)
	var ddl = GetElementByID(dropdown)
	
	if (tb == null || ddl == null) return
	
	try
	{
		if (! /^(([0]?[1-9]|1[0-2])(:)([0-5][0-9]))$/.test(tb.value)) throw "not time";
		var hour = tb.value.match(/^[0-1]?[0-9]/)
		ddl.selectedIndex = (hour >= 6 && hour <= 12) ? 0 : 1
	}
	catch (e)
	{
		ddl.selectedIndex = 0
	}
}

function ShowDialog (web_root, width, height, url, scroll)
{
	var url = web_root + "/_dialogs/" + url
	
	if (typeof(scroll) == "undefined") scroll = "no"

	if (/\?/.test(url))
		url += '&rnd=' + Math.random() * 100000
	else
		url += '?rnd=' + Math.random() * 100000

	window.showModalDialog (url, 
		'popupcal', 
		'dialogWidth=' + width + 'px; dialogHeight=' + height + 'px; help=no; status=no; resizable=yes; scroll=' + scroll + ';');
}

function ClickIfEnter (buttonID)
{
	if (event.keyCode == 13) 
	{
		GetElementByID(buttonID).click()
		return false;
	}
}

function EnableIfNotEmpty (sourceID)
{
	for (var i = 1; i < arguments.length; i++)
	{
		try
		{
			GetElementByID(arguments[i]).disabled = GetElementByID(sourceID).value == ""
		}
		catch (e) {}
	}
}

function OpenPrintPage (data_id, xsl_id)
{
	var url = web_root + "/Reports/Print/PrintPage.aspx?data=" + data_id + "&xsl=" + xsl_id;
	window.open (url);
}

function SetFocus (elem_id)
{
	try
		{
			GetElementByID(elem_id).focus();
		}
	catch (e) {}
}

function OpenDialog(dlgName, dialogWidth, dialogHeight, resInput, dialogParams, dialogParamsClientFunction, dialogParamsClientFunctionParams)
{
	if (typeof(dialogParamsClientFunction) != 'undefined')
		dialogParams = dialogParamsClientFunction(dialogParams, dialogParamsClientFunctionParams);
	
	var x = (screen.width - dialogWidth) / 2;
	var y = (screen.height - dialogHeight) / 2;

    
    var formName = dlgName.indexOf('http://') == -1 ?
        web_root +'/_dialogs/' + dlgName
        :
        dlgName 
       
	var rnd = Math.random()
	formName += formName.indexOf('?') == -1 ? '?' : '&'
	formName += 'random=' + rnd;

	if (dialogParams != "undefined") formName += '&' + dialogParams;

	var params = 'dialogWidth=' + dialogWidth + 'px; dialogHeight=' + dialogHeight + 'px; dialogLeft=' + 
			x + 'px; dialogTop=' + y + 'px; border=thin; help=no; status=no; center=yes; resizable=yes; scroll=no;';
	
	var res = window.showModalDialog(formName, '', params);
	
	if (resInput != null && resInput != "undefined") 
		GetElementByID(resInput).value = res
	else
		return false;
	
	return res;
}

function SetDialogResult (res)
{
	window.returnValue = res
	window.close()
}


var keyBuf = "";
var MYES = "yes-m\r"
var MNO = "no-m\r"
var AYES = "yes-a\r"
var ANO = "no-a\r"
var SAVE = "save\r"
var CANCEL = "cancel\r"

function ProcessScans()
{
	var last = event.keyCode;
	
	if (event.keyCode >= 48 && event.keyCode <= 57 && GetElementByID (tbInput) != null && !GetElementByID (tbInput).disabled)
		GetElementByID (tbInput).value += String.fromCharCode (event.keyCode)
	else
		keyBuf += String.fromCharCode (event.keyCode)
		
	event.keyCode = 0;
	
	if (last == 13)
	{
		switch (keyBuf.toLowerCase())
		{
			case MYES:
				if (GetElementByID (AMYes) != null) GetElementByID (AMYes).checked = true;
				break;
			case MNO:
				if (GetElementByID (AMNo) != null) GetElementByID (AMNo).checked = true;
				break;
			case AYES:
				if (GetElementByID (PMYes) != null) GetElementByID (PMYes).checked = true;
				break;
			case ANO:
				if (GetElementByID (PMNo) != null) GetElementByID (PMNo).checked = true;
				break;
			case SAVE:
				if (GetElementByID (btnSave) != null) GetElementByID (btnSave).click()
				break;
			case CANCEL:
				if (GetElementByID (btnCancel) != null) GetElementByID (btnCancel).click()
				break;
			default:
				if (GetElementByID (btnLoad) != null) GetElementByID (btnLoad).click()
				break;
		}
		
		keyBuf = ""
	}
}

function RefreshPostData()
{
    __theFormPostData = ""
    WebForm_InitCallback()
}

function prepareCallBackArgument(arg)
{
    return arg + '_' + Math.random() * 100000
}
