function Browser()
{
	this.version	= navigator.appVersion;
	this.agent		= navigator.userAgent;
	this.dom		= document.getElementById?1:0;
	this.opera5		= this.agent.indexOf("Opera 5")>-1;
	this.ie5		= (this.version.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
	this.ie6		= (this.version.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie7		= (this.version.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4		= (document.all && !this.dom && !this.opera5)?1:0;
	this.ie			= this.ie4||this.ie5||this.ie6||this.ie7;
	this.mac		= this.agent.indexOf("Mac")>-1;
	this.ns6		= (this.dom && parseInt(this.version) >= 5) ?1:0;
	this.ns4		= (document.layers && !this.dom)?1:0;
	this.bw			= (this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5);
	return this;
}

browser = new Browser();

function verify(msg)
{
	if (!confirm(msg))
	{
		if(browser.ie)
		{
			event.cancelBubble = true;
		}

		return false;
	}
	return true;
}

function pressButton(inName)
{
	document.getElementById(inName).click();
}

function keyPress(myCall, event)
{
	var myKeyCode = event.keyCode;

	if(document.all) // Internet Explorer
	{
		event.cancelBubble = true;

		if(myKeyCode == 13)
		{
			event.returnValue = false;
		}
	}
	else // All Others
	{
		if(myKeyCode == 13)
		{
			event.preventDefault(true);
		}
	}

	if(myKeyCode == 13)
	{
		eval(myCall);
	}
}

function openWindow(inURL, inWidth, inHeight, inWindowName)
{
	if(!inWindowName)
	{
		inWindowName = 'newwindow';
	}

	window.open(inURL, inWindowName, 'width=' + inWidth + ',height=' + inHeight + ',menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
}

function gotoPage(inURL)
{
	window.location = inURL;
}

function doPageFocus()
{
	if(document.forms[0])
	if(document.forms[0].elements["email"])
	{
		document.forms[0].elements["email"].focus();
	}
}

function focusElement(inElementID)
{
	var element = document.getElementById(inElementID);

	if(element)
	{
		element.focus();
	}
}

function showHelp(inCode)
{
	url = "/kb";

	if(inCode && inCode.length > 0)
	{
		url = inCode;
	}

	var newWindow = window.open(url, "oit_help", "width=760,height=650,scrollbars");
	
	if(window.focus)
	{
		newWindow.focus();
	}
}

function showHelpTopic(inCode)
{
	parent.content.location = "/kb";
}

function toggleElement(inTitleID, inContentID, inTitle, inHideCharacter, inShowCharacter)
{
	var titleSpan = document.getElementById(inTitleID);
	
	var contentBlock = document.getElementById(inContentID);

	if(!contentBlock || !titleSpan)
	{
		alert('cannot find stuff');
		
		return;
	}
	
	if(contentBlock.style.display=="none")
	{
		contentBlock.style.display="block";
		
		titleSpan.innerHTML = inShowCharacter + " " + inTitle;
	}
	else
	{
		contentBlock.style.display="none";
		
		titleSpan.innerHTML = inHideCharacter + " " + inTitle;
	}
	
	return;
}

// ------------------------------

function __postForm(inTargetID)
{
	var element = $(inTargetID);
	
	if(!element)
	{
		alert("Element " + inTargetID + " not found.");
	}
	else
	{
		// Find containing form and submit
		var elements = element.ancestors();
		
		for(var i = 0; i < elements.length; i++)
		{
			ancestor = elements[i];
			
			if(ancestor.tagName == "FORM")
			{
				ancestor.insert("<input type='hidden' name='action' value='" + inTargetID + "'>");
				
				ancestor.submit();
				
				break;
			}
		}
	}
}

var modalWindow;

function showModal(inURL, inTitle, inWidth, inHeight)
{
	modalWindow = new Window({
		id: 'modal',
		title: inTitle, 
		url: inURL,
		width: inWidth, 
		height: inHeight, 
		resizable: false,
		closable: true,
		minimizable: false,
		maximizable: false,
		draggable: false,
		destroyOnClose: true, 
		recenterAuto:true
	}); 
	
	modalWindow.showCenter(true);
}

function closeModal(updateParent, inURL)
{
	Windows.closeAll();

	if(updateParent)
	{
		if(inURL != null && inURL.length > 1)
		{
			window.location = inURL;
		}
		else
		{	
			window.location.reload();
		}
	}
}

var tinyMCECallerWindow;
var tinyMCECallerField;

function customFileBrowser(field_name, url, type, win) 
{
	tinyMCECallerField = field_name;
	tinyMCECallerWindow = win;
	
	url = '/accountfiles/?bus_id=' + $('bus_id').value + '&picker=true';
	
	openWindow(url, 900, 600, 'filePicker');
}
	
function pickFile(inPath)
{
	var targetWindow = window.opener.tinyMCECallerWindow;
	var targetField = window.opener.tinyMCECallerField;
	
	var element = targetWindow.document.getElementById(targetField); // .value = inPath;
	
	if(element)
	{
		element.value = inPath;
	}
	else
	{
		alert('Cannot find field with ID [' + targetField + ']');
	}
	
	window.close();
}

