﻿function buttonSubmit(btn, text, otherbtn)
{
	btn = $(btn);
	if (btn.attr("disabled")) return true;
	
	// now disable it...
	if (text != null) btn.val(text);
	btn.attr("disabled", true);

	// if an alternate button is supplied, disable it
	if (otherbtn != null) $(otherbtn).attr("disabled", true);
}

function fireDefaultLinkButton(e, id)
{
	if (!e) e = event;
	if (!e) e = window.event;
	if (e.keyCode != 13) return;
	
	// cancel the event
	e.returnValue = false;
	e.cancelBubble = false;
	
	window.location.href = $get(id).href;
}

//**********************************************************************
// Finish Box Functions
//**********************************************************************
function hideFinishBox(id, delay)
{
	var box = $get(id);
	if (!box) return;
	
	// if another action was scheduled, clear it
	if (box._timeout) window.clearTimeout(box._timeout);
	
	// send it off to be hidden after the delay
	box._timeout = window.setTimeout("hideFinishBoxEnd('" + id + "');", delay);
}

function hideFinishBoxEnd(id)
{
	var box = $get(id);
	if (!box) return;
	
	box._timeout = null;
	box.style.display='none';
}

function cancelEvent(e)
{
	if (!e) e = event;
	if (!e) e = window.event;

	// cancel the event
	e.returnValue = false;
	e.cancelBubble = false;
	
	try { e.preventDefault(); } catch(e) {}
	try { e.stopPropagation(); } catch (e) { }
	return false;
}

//**********************************************************************
// Rad Window Functions
//**********************************************************************
function openRadWindow(url, title, width, height, onCloseHandler)
{
	var win = GetRadWindowManager().open(url, null)
	if (title != null) win.SetTitle(title);
	win.setSize(width, height);
	win.set_modal(true);
	win.center();
	
	// attach close handler if any
	if (onCloseHandler != null) win.add_close(onCloseHandler);
	window.document.body.__radWindowResult = null;
}

function closeRadWindow(result)
{
	// must only be called from the page loaded 
	// in the radWindow, not the calling page.
	var win = getRadWindow();
	
	// store the dialog result to the host page can read it
	win.BrowserWindow.document.body.__radWindowResult = result;
	
	// close the window
	win.close();
}

function getRadWindow()
{
	// must only be called from the page loaded 
	// in the radWindow, not the calling page.
	var oWindow = null;
	if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
	else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;//IE (and Moz az well)
	
	return oWindow;
}

function radWindowResult()
{
	return window.document.body.__radWindowResult;
}

function	updateAfterRadWindowClose()
{
	var panel = radWindowResult();
	if (!panel || panel == '') return;
	__doPostBack(panel,'')
}

function redirectFromRadWindow(url)
{
	// must only be called from the page loaded 
	// in the radWindow, not the calling page.
	var win = getRadWindow();

//	// close the window first
//	win.close()
		
	// now redirect to the target url
	win.BrowserWindow.location.href = url;
}

function reloadFromRadWindow()
{
	getRadWindow().BrowserWindow.location.reload();
}

//**********************************************************************
// RadEditor Functions
//**********************************************************************
function EditorClientCommand(editor, args)
{
	var name = args.get_name();
	var val = args.get_value();
	if (name == "Emoticons")
	{
		editor.pasteHtml("<img src='" + val + "' alt='' />");

		//Cancel the further execution of the command as such a 
		// command does not exist in the editor command list      
		args.set_cancel(true);
	}
}	

//*********************************************************************
// Page Request Manager Hook
//*********************************************************************
function setupPageManager()
{
	 Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(handleBeginRequest);
}

function handleBeginRequest()
{
	 // kill the scroll logic during async postbacks
	 Sys.WebForms.PageRequestManager.getInstance()._scrollPosition = null;
}
