function getObj(name) {
	if (document.getElementById) {
		if(document.getElementById(name)) {
			this.obj = document.getElementById(name);
			this.style = document.getElementById(name).style;
		}
	}
	else if (document.all) {
		if(document.all[name]) {
			this.obj = document.all[name];
			this.style = document.all[name].style;
		}
	}
	else if (document.layers) {
		if (document.layers[name]) {
			this.obj = document.layers[name];
			this.style = document.layers[name];
		}
	}
}

function overlayPop(popupObj, overlayObj, containerObj) {
	newObj_1 = new getObj(containerObj);
	newObj_2 = new getObj(popupObj);
	newObj_3 = new getObj(overlayObj);
	
	newObj_2.style.height = newObj_3.style.height = newObj_1.obj.offsetHeight + 'px';
}

function columnMatch(column_1, column_2, column_3) {
	oCol_1 = new getObj(column_1);
	oCol_2 = new getObj(column_2);
	oCol_3 = new getObj(column_3);
	
	if( !oCol_1 || !oCol_2 ) { return; }
	if( !oCol_1.obj || !oCol_2.obj ) { return; }
	if( !oCol_2 || !oCol_3 ) { return; }
	if( !oCol_2.obj || !oCol_3.obj ) { return; }
	
	iHeight_1 = oCol_1.obj.offsetHeight;
	iHeight_2 = oCol_2.obj.offsetHeight;
	iHeight_3 = oCol_3.obj.offsetHeight;

	iNewHeight = Math.max(iHeight_1, iHeight_2, iHeight_3);
	oCol_1.obj.style.height = iNewHeight + 'px';
	oCol_2.obj.style.height = iNewHeight + 'px';
	oCol_3.obj.style.height = iNewHeight + 'px';
}
function columnMatch2(column_1, column_2) {
	oCol_1 = new getObj(column_1);
	oCol_2 = new getObj(column_2);
	
	if( !oCol_1 || !oCol_2 ) { return; }
	if( !oCol_1.obj || !oCol_2.obj ) { return; }
	
	iHeight_1 = oCol_1.obj.offsetHeight;
	iHeight_2 = oCol_2.obj.offsetHeight;

	iNewHeight = Math.max(iHeight_1, iHeight_2);
	oCol_1.obj.style.height = iNewHeight + 'px';
	oCol_2.obj.style.height = iNewHeight + 'px';
}
function showHide(divID) {
	document.getElementById(divID).style.display == 'none' ? document.getElementById(divID).style.display = 'block' : document.getElementById(divID).style.display = 'none';
}

function submenus(ObjOpen,ObjClose) {
	document.getElementById(ObjOpen).style.display = 'block';
	document.getElementById(ObjClose).style.display = 'none';
}

function hide() {
	this.style.display = 'none';
}

function popup(pagina,lungime,inaltime) {
	window.open(pagina,"_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width="+lungime+", height="+inaltime+"")
}

function expandCollapse(titleLink,objID) {
	if(document.getElementById(objID).style.display == '' || document.getElementById(objID).style.display=='none'){
		titleLink.style.backgroundImage='url(http://www.autoklass.ro/images/default/bullet_minus.gif)';
		document.getElementById(objID).style.display='block';
	}
	else {
		titleLink.style.backgroundImage='url(http://www.autoklass.ro/images/default/bullet_plus.gif)';
		document.getElementById(objID).style.display='none';
	}
}

function openPopup( url )
{
	window.open( url, "Download", "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=500,width=650" );
	
	return;
}

function openEmployeePopup( url )
{
	window.open( url, "Download", "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=450,width=430" );
	
	return;
}

function setCookie(name,value) {
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 3600000 * 24 * 999 ); // set expire time after 999 days (never)
	document.cookie = name + "=" + escape(value) + ";expires=" + expire.toGMTString() + ";path=/"; // set the cookie
}

function getCookie(name) {
	var c_name = name;
	
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length+1;
			c_end = document.cookie.indexOf(";",c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	else
	{
		return false;
	}
}

/* --- Ajax functions --- */
var mainWebsiteURL = "http://www.autoklass.ro/";

// 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 a new ajax object
var ajaxUsername = newAjaxObject(); // this is globally used
var ajaxCaptcha = newAjaxObject();

// 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
	div = document.getElementById( divID );
	
	if( ajaxObj )
	{
		if( sendMethod == "GET" )
		{
			ajaxObj.open( "GET", serverPage, true );
			ajaxObj.onreadystatechange = function()
			{
				if( ajaxObj.readyState == 4 && ajaxObj.status == 200 )
				{
					div.innerHTML = ajaxObj.responseText;
				}
			}
			ajaxObj.send( null );
		}
		else if( sendMethod == "POST" )
		{
			ajaxObj.open( "POST", serverPage, 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;
				}
			}
			ajaxObj.send( params );
		}
		else
		{
			// No other valid known methods for passing the data...
			alert( "Invalid or no method passed to the function!" );
			return;
		}
	}
}

// Generates & reloads the captcha image & code
function generate_new_captcha( divID )
{	
	// Build the url to be queried
	the_url = mainWebsiteURL + "index.php";
	
	//makequery( ajaxCaptcha, the_url, divID, "GET" ); // WILL NOT WORK ON IE!
	
	makequery( ajaxCaptcha, the_url, divID, "POST", "act=Pictures&do=captcha" );
	
	return;
}

// Tests the captcha code
function check_captcha_code( inp )
{	
	val = inp.value;
	val = removeSpaces( val );
	
	the_url = mainWebsiteURL + "index.php?act=Pictures&cd=" + val + "&do=test-captcha";
	
	if( val != '' )
	{
		ajaxCaptcha.open( "GET", the_url, true );
		ajaxCaptcha.onreadystatechange = function()
		{
			if( ajaxCaptcha.readyState == 4 && ajaxCaptcha.status == 200 )
			{
				if( ajaxCaptcha.responseText != "invalid" )
				{
					inp.style.background = "url(" + mainWebsiteURL + "misc/register_ok.gif) right center no-repeat";
				}
				else
				{
					inp.style.background = "url(" + mainWebsiteURL + "misc/register_bad.gif) right center no-repeat";
					inp.focus();
				}
			}
		}
		ajaxCaptcha.send( null );
	}
	else
	{
		inp.style.background = "url(" + mainWebsiteURL + "misc/register_bad.gif) right center no-repeat";
		inp.focus();
	}
	
	return;
}

function checkUsername( inp )
{
	user = inp.value;
	user = removeSpaces( user );
	
	msg = '';
	
	divHandle = document.getElementById("usernameResponse");
	
	if( user != '' )
	{
		var the_url = mainWebsiteURL + "index.php?act=Pictures&do=username-check&username=" + user;
		
		ajaxUsername.open( "GET", the_url );
		ajaxUsername.onreadystatechange = function()
		{
			if( ajaxUsername.readyState == 4 && ajaxUsername.status == 200 )
			{
				divHandle.innerHTML = ajaxUsername.responseText;
				
				if( ajaxUsername.responseText != "Numele de utilizator ales contine caractere nepermise!" && ajaxUsername.responseText != "Numele de utilizator ales nu este liber." && ajaxUsername.responseText != "Numele trebuie sa aiba cel putin 4 caractere." )
				{
					inp.style.background = "url(" + mainWebsiteURL + "misc/register_ok.gif) right center no-repeat";
				}
				else
				{
					inp.style.background = "url(" + mainWebsiteURL + "misc/register_bad.gif) right center no-repeat";
					inp.focus();
				}
			}
		}
		ajaxUsername.send( null );
	}
	else
	{
		divHandle.innerHTML = "Va rugam introduceti un nume.";
		
		inp.style.background = "url(" + mainWebsiteURL + "misc/register_bad.gif) right center no-repeat";
		inp.focus();
	}
	
	return;
}

// Removes all spaces from a string
function removeSpaces( string )
{
	var temp = "";
	string = '' + string;
	
	// Break the string after spaces
	splitstring = string.split( " " );
	
	for( i = 0;i < splitstring.length;i++ )
	{
		temp += splitstring[i];
	}
	
	return temp;
}

function process_login()
{
	var ajax = newAjaxObject();
	
	var user = removeSpaces( document.getElementById('username').value );
	var pass = removeSpaces( document.getElementById('password').value );
	
	makequery( ajax, mainWebsiteURL + 'index.php', 'controlPanel', 'POST', 'act=Login&do=login&user=' + user + '&pass=' + pass );
}

function process_logout()
{
	var ajax = newAjaxObject();
	
	makequery( ajax, mainWebsiteURL + 'index.php', 'controlPanel', 'POST', 'act=Login&do=logout' );
}

function checkFile( file )
{
	var regex = /\.jpg|\.jpeg|\.gif|\.png/i;
	var filename = file.value;
	
	if( filename.search( regex ) == -1 )
	{
		alert( "Fisierul trebuie sa fie de tip JPG, JPEG, GIF sau PNG!" );
		file.value = '';
		return false;
	}
	
	return true;
}

function doVote( rating, picID, url )
{
	var ajax = newAjaxObject();
	
	ajax.open( "POST", mainWebsiteURL + 'index.php' );
	ajax.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" );
	ajax.onreadystatechange = function()
	{
		if( ajax.readyState == 4 && ajax.status == 200 )
		{
			window.location = unescape( url );
			//document.getElementById('test').innerHTML = ajax.responseText;
		}
	}
	ajax.send( 'act=vote&v=' + rating + '&p=' + picID + '&r=' + url );
}