var xmlHttp;

//Will attempt to crate XMLHttpRequest object if the browser supports it.
function verifyBrowser()
{  
	try
	{ 
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();    
	}
	catch (e)
	{    
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
			}
			catch (e)
			{        
				alert("Your browser does not support AJAX!");        
				return false;        
			}     
		}    
	} 
}

/* adds a function to the onload event handler */
function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

/*	Detects the browser */
function detectBrowser()
{
	var browser = navigator.appName;
	return browser;
}

//Will adjust the height of an element
function adjustHeightRealTime(element)
{
	if(typeof(element) != "object")
		element = document.getElementById(element);
	
	element.style.height = element.scrollHeight+"px";
}

function changeClass(element,class_name)
{
	//Accept objects and id's of objects
	if(typeof(element) != "object")
		element = document.getElementById(element);
	
	element.className = class_name;
}

//Performs the click method on a search button.
//Called when enter is pressed when the focus is on the search field.
function doSearch(event,searchButtonID)
{
	//13 is the keycode for enter key
	if(event.keyCode == 13)
	{
		var searchButton = document.getElementById(searchButtonID);
		if(searchButton)
		{
			searchButton.click();
		}
	}
}

function submitForm(form,action,target, validated)
{
	if(!validated) return;
	
	form.action = action;
	form.target = target;
	form.submit();
}


//Calendar reated

var windowREF; //A ref to a newly opened window.
var interval; //An interval of checking of the newly opened window closed.
//The URL to open = openwindowURL;
//whenclosesDest - the URL to go to when openwindowSrc closes.
function openWindow(openwindowURL,whenclosesURL)
{
	windowREF = window.open(openwindowURL,'mywin','left=100,top=100,width=800,height=600,toolbar=0,resizable=0,scrollbars=1');
	interval = setInterval(function(){checkForClosedWindow(whenclosesURL)}, 1000);
}
function checkForClosedWindow(destination)
{
	if(!windowREF.closed) return;
	clearInterval(interval);
	window.location=destination;
}

//Switches the page to the desired page selected in the page input field.s
function switchPage(goPage,args)
{
	window.location = "?page=" + goPage + "&" + args;
}

//Changes the background color for the referenced element.
//elementID: the element we want to change the bg color of
//color: the new color
function changeBG(element,color)
{
	
	if(typeof(element) != "object")
		element = document.getElementById(element);
	
	if(detectBrowser() == "Microsoft Internet Explorer")
	{
		element.style.backgroundColor = color;
	}
	else
	{
		element.style.setProperty("background-color",color,"");
	}
}

function changeBorder(element,border)
{
	if(typeof(element) != "object")
		element = document.getElementById(element);
	
	if(detectBrowser() == "Microsoft Internet Explorer")
	{
		element.style.border = border;
	}
	else
	{
		element.style.setProperty("border",border,"");
	}
}

//Will display a confirmation box about an action and if
//approved will redirect to the locatiom which needs to 
//be executed
function confirmAction(action,script)
{
	var doAction = confirm(action);
	if (doAction == true)
	{
		window.target = "_blank";
		window.location=script;
 	}
}
