var globalObject;
var globalEntity;
var globalUsername;

var oldThumbId='';
var oldThumbContent='';
var firstSwitch = true;

var globalHTMLHolder1, globalHTMLHolder2;
var mainObjectName, addonObjectName;

function genSwitcherBox(entity, username, objName, objType)
{
	var par = document.getElementById(objName);

	if (par == null)
		return "";

	globalObject = "edit_"+objName;
	globalEntity = entity;
	globalUsername = username;

	//save main object and addon inner HTML 
	globalHTMLHolder1 = document.getElementById(objName).innerHTML;
	globalHTMLHolder2 = document.getElementById(objName+"_addon").innerHTML;

	mainObjectName = objName;
	addonObjectName = objName+"_addon";
	

	document.getElementById(objName+"_addon").innerHTML = "";

	var insideValue = par.innerHTML;
	if (objType == 'text')
		par.innerHTML = "<input size=25 onKeyPress='keyPressAction(event)' style='height: 22px; font-size: 9pt; font-family: Verdana; border: 1px solid white; color: white; background-color:#272E34;' type='text' id='edit_"+objName+"' value='"+insideValue+"'>";
	else
		par.innerHTML = "<textarea cols=110 rows=3 onKeyPress='keyPressAction(event)' style='margin-left: 25px; height: 46px; font-size: 9pt; font-family: Verdana; border: 1px solid white; color: white; background-color:#272E34;'  id='edit_"+objName+"'>"+insideValue+"</textarea>";
		
	

}


function keyPressAction(event)
{
	if (event.keyCode==13) 
	{
		changeValue(globalObject);
	}
	else if (event.keyCode==27)
	{
		document.getElementById(addonObjectName).innerHTML = globalHTMLHolder2;
		document.getElementById(mainObjectName).innerHTML = globalHTMLHolder1;
	}
}


function changeValue(obj)
{
	var newTitle = document.getElementById(obj).value;

	var attr= obj+"="+newTitle;
		attr += "&username="+globalUsername;
		attr += "&path="+globalEntity;

	var myAjax = new Ajax.Request(
                    '/proj/PHP/ajax/dispatch.gallery.php',
                    {
                        method: 'post',
                        parameters: attr,
                        asynchronous: true,
                        onComplete: showResponse,
                        contentType:'application/x-www-form-urlencoded',
                        encoding: 'UTF-8'
                    });

	function showResponse(originalRequest)
        { 
		var result = originalRequest.responseText;	
		document.getElementById(addonObjectName).innerHTML = globalHTMLHolder2;
		document.getElementById(mainObjectName).innerHTML = newTitle;
        }	
}


function prepareUploaders()
{
	var x = document.getElementsByTagName('input');
	var oneVal = '';
	for (var i=0;i<x.length;i++) 
	{
		if (trim(x[i].id)!='')
		{
			var exp = x[i].id.split('_');
			if (exp[0] == 'real')
			{
				$("fake_"+exp[1]).value = x[i].value.replace(/^.*\\/, '');
			}
		}

	}
}


function addUploader(howMany)
{
	var attr= 'addElem=true';
		attr += "&howMany="+howMany;

	var myAjax = new Ajax.Request(
                    '/proj/PHP/ajax/dispatch.macros.php',
                    {
                        method: 'post',
                        parameters: attr,
                        asynchronous: true,
                        onComplete: showResponse,
                        contentType:'application/x-www-form-urlencoded',
                        encoding: 'UTF-8'
                    });

	function showResponse(originalRequest)
        { 
		var result = originalRequest.responseText;	
		document.getElementById('file_uploader').innerHTML = result;
		if (howMany > 0)
			$("sendButton").style.display = 'block';	
		else
			$("sendButton").style.display = 'none';	
        }	
}


function loadReqPhotos(selValue)
{
	if (typeof justChanged != undefined)
		if (justChanged == false)
		{
			if (document.getElementById("selLocations") != null)
			{
				alert(crMsgMustChoose);
				document.getElementById("selLocations").focus();
				return;
			}
		}

	if (document.getElementById("selCityId")!=null)
	{
		if ($("selCityId").value == -1)
		{
			alert(crMsgMustChoose);
			return;
		}
	}

	if (document.getElementById("galCrCityId")!=null)
	{
		if ($("galCrCityId").value == -1)
		{
			alert(crMsgMustChoose);
			return;
		}
	}


	var x = document.addArtForm.elements;
	var i = 0;
	var hasElem = false;
	for (i=0;i<x.length;i++)
	{
		if (trim(x[i].name)!='')
		{
			var exp = x[i].name.split("_");
			if (exp[0]=='real')
				if (x[i].value != '')
				{
					hasElem = true;
					break;
				}
		}
	}

	if (hasElem)
	{
		if (!confirm("Atentie! Schimbarea numarului de fotografii acum \n      va duce la pierderea selectie curente\n                  Doriti sa continuati?"))
			return "";
	}
	addUploader(selValue);
}

function genCountryLoader(except)
{
	var attr= 'selAllCountries=true';
		attr += "&except="+except;
		attr += "&evt=true";
		attr += "&name=genCountrySel";

	var myAjax = new Ajax.Request(
                    '/proj/PHP/ajax/dispatch.macros.php',
                    {
                        method: 'post',
                        parameters: attr,
                        asynchronous: true,
                        onComplete: showResponse,
                        contentType:'application/x-www-form-urlencoded',
                        encoding: 'UTF-8'
                    });

	function showResponse(originalRequest)
        { 
		var result = originalRequest.responseText;	
		document.getElementById('genCtrCity').innerHTML = result;
        }	
}

//if otherAction is true try to insert data in the user profile
function genCityLoader(countryId, otherAction)
{
	$("genCtrCity").innerHTML = "<img src='/STATIC_IMGS/ajxl/plswait.gif' border=0>";
	var attr= 'selAllCities=true';
		attr += "&countryId="+countryId;
		attr += "&evt=true";
		attr += "&name=genCitySel";
		attr += "&saveParent="+otherAction;

	var myAjax = new Ajax.Request(
                    '/proj/PHP/ajax/dispatch.macros.php',
                    {
                        method: 'post',
                        parameters: attr,
                        asynchronous: true,
                        onComplete: showResponse,
                        contentType:'application/x-www-form-urlencoded',
                        encoding: 'UTF-8'
                    });

	function showResponse(originalRequest)
        { 
		var result = originalRequest.responseText;	
		document.getElementById('genCtrCity').innerHTML = result;
        }	
}



function switchThumb(obj, newPhoto, imgStyle)
{
	if (document.getElementById(oldThumbId) != null)
	{
		if (obj.id == oldThumbId && firstSwitch)
		{
			obj.innerHTML = oldThumbContent;
			firstSwitch = false;
			return;
		}
		else
		{
			document.getElementById(oldThumbId).innerHTML = oldThumbContent;
		}
	}
		
	oldThumbContent = obj.innerHTML;
	oldThumbId = obj.id;
	firstSwitch = true;
	obj.innerHTML = "<img alt='Fotografia se incarca' src='"+newPhoto+"' style='"+imgStyle+"' border=0>";
}

function countChars(lim)
{
	if (document.getElementById("comment_text").value.length > lim)
	{
		alert("Atentie! Mesajul va fi scurtat. Lungimea maxima este de "+lim+" de caractere");
		document.getElementById("comment_text").value = document.getElementById("comment_text").value.substring(0, lim);
		return;
	}

	document.getElementById("charCount").innerHTML = document.getElementById("comment_text").value.length+1;
}

function simpleCountChars(lim, field)
{
	document.getElementById("charCount").innerHTML = document.getElementById(field).value.length;
}


function borderize(obj, borderColor)
{
	$(obj).style.border = "2px solid black";
}

