// Creates & returns an ajax object
function newAjaxObject()
{
	var xmlhttp = null;
	
	try
	{
		// Mozilla, Opera, Safari, Konqueror and everything else uses this method
		xmlhttp = new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			// IE lower than 6 implements Msxml lib (only 5.xx versions,
			// versions below 5.0 does not support asynchronous queries
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				// For IE 6 and later
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				// No asynchronous support provided by this browser...
				alert("Your browser doesn't support Ajax Requests\n\nTherefore, certain features may not be available to you.")
			}
		}
	}
	return xmlhttp;
}

// Init some new ajax objects
var ajax = newAjaxObject(); // this is globally used
var ajaxBusy = false;

// Makes a query using the ajax object ajaxObj to the page serverPage
// using GET or POST (sendMethod) and puts the response into the div with the id divID
// If the POST method is prefered, the params to be sent must be given as a string of this form:
// field1=value1&field2=value2&field3=value3 (etc.)
function makequery( ajaxObj, serverPage, divID, sendMethod, params )
{
	// Get the container's handler
	var div = document.getElementById( divID );
	
	if( ajaxBusy )
	{
		ajaxObj.onreadystatechange = function() {}
		ajaxObj.abort();
	}
	
	if( ajaxObj )
	{
		if( sendMethod == "GET" )
		{
			ajaxObj.open( "GET", serverPage, true );
			ajaxBusy = true;
			ajaxObj.onreadystatechange = function()
			{
				if( ajaxObj.readyState == 4 && ajaxObj.status == 200 )
				{
					div.innerHTML = ajaxObj.responseText;
				}
				ajaxBusy = false;
			}
			ajax.send( null );
		}
		else if( sendMethod == "POST" )
		{
			ajaxObj.open( "POST", serverPage, true );
			ajaxBusy = true;
			ajaxObj.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" );
			ajaxObj.onreadystatechange = function()
			{
				if( ajaxObj.readyState == 4 && ajaxObj.status == 200 )
				{
					div.innerHTML = ajaxObj.responseText;
				}
				ajaxBusy = false;
			}
			ajaxObj.send( params );
		}
		else
		{
			// No other valid known methods for passing the data...
			alert( "Invalid or no method passed to the function!" );
			return;
		}
	}
}

// show hide mainDet content
function show(divID)
{
	if (document.getElementById(divID))	document.getElementById(divID).style.display='block';
}
function hide(divID)
{
	if (document.getElementById(divID))	document.getElementById(divID).style.display='none';
}
function showHide(divID) 
{
	document.getElementById(divID).style.display == 'none' ? document.getElementById(divID).style.display = 'block' : document.getElementById(divID).style.display = 'none';
}

// Checks if a field is empty, and if so, resets the input's content to the default value
function checkField( obj, value )
{
	// Match any amount of white spaces
	if( obj.value == "" )
	{
		obj.value = value;
	}
	
	return false;
}

// Clears an input's content
function clearField( obj, value )
{
	if( obj.value == value )
	{
		obj.value = "";
	}
	
	return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function reloadWindow()
{
	window.location=window.location;
	
	return;
}

function obiect(id)
{
	if (document.getElementById(id)) return document.getElementById(id); else return false;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function markFields(campuri)
{
	var vector = new Array;
	vector = campuri.split("%");
	for (i=0;i<vector.length;i++)
	if (document.getElementById(vector[i]))
	{
		document.getElementById(vector[i]).style.backgroundColor = '#F9E5E5';
	}
}
