/**
 * aduce valoarea elementului cu id-ul id . se presupune ca exista un asemenea element in documentul curent 
 * si ca i se poate intoarce valoarea
 */
function getValue(id)
{
	return getElem(id).value;
}
/**
 * seteaza valoarea elementului cu id-ul id la valoarea val
 * si ca i se poate intoarce valoarea
 */
function setValue(id, val)
{
	getElem(id).value=val;
}
/**
 * Presupune existenta unui caz foarte specializat dar foarte des intalnit
 * seteaza valoarea elementului cu id-ul action la valoarea val
 */
function setAction(val)
{
	setValue('action',val);
}
/**
 * Presupune existenta unui caz foarte specializat dar foarte des intalnit
 * seteaza valoarea elementului cu id-ul action la valoarea val
 */
function setAction2(val)
{
	setValue('action2',val);
}
/**
 * aduce elementul cu id-ul id . se presupune ca exista un asemenea element in documentul curent 
 */
function getElem(id)
{
	return document.getElementById(id);
}
/**
 * aduce elementul cu id-ul id . se presupune ca exista un asemenea element in documentul ferestrei parinte
 */
function getParentElem(id)
{
	return window.parent.document.getElementById(id);
}
/**
 * aduce elementul cu id-ul id . se presupune ca exista un asemenea element in documentul ferestrei parinte
 */
function getOpenerElem(id)
{
	return window.opener.document.getElementById(id);
}
/**
 * face submit la elementul cu id-ul id . Se presupune ca e vorba de un formular
 */
function doSubmit(id)
{
	getElem(id).submit();
}

/**
 * Concateneaza toate bucatile si intre ele baga glue
 * @param String glue
 * @param Array pieces array de stringuri
 * @return String 
 */
function implode(glue,pieces)
{
	var ret=new String();
	
	for (i=0;i<pieces.length;i++)
	{
		if (i>0) ret = ret.concat(glue);
		ret = ret.concat(pieces[i]);
	}
	return ret;
}

/**
 * Sparge stringul in bucati dupa separator si intoarce un array cu bucatile
 * @param String separator
 * @param String stringul de explodat
 * @return Array bucatile rezultate dupa explozie
 */
function explode(separator, string)
{
	var ret=new Array();
	
	//de scris 
	
	return ret;
}
/**
 * arata divul
 */
function showElem(elem_id, display_style)
{
	switch (display_style)
	{
		case 'table-row':
			getElem(elem_id).style.display='block';
			try{
				getElem(elem_id).style.display='table-row';
			}catch (err){}	
		break;
		case 'inline':
			getElem(elem_id).style.display='inline';
		break;		
		default:
			getElem(elem_id).style.display='block';
		break;
	}
	
}
/**
 * ascunde divul
 */
function hideElem(elem_id)
{
	getElem(elem_id).style.display='none';
}
/**
 * 
 */
function isElemHidden(elem_id)
{
	return getElem(elem_id).style.display=='none';
}
function showHideElem(elem_id, display_style)
{
	if (isElemHidden(elem_id)) showElem(elem_id, display_style);
	else hideElem(elem_id);	
}
/**
 * arata divul si incrementeaza newi
 */
function showNewDiv(newi, div_new, limit)
{
	if (getValue(newi)==limit) return true;
	
	setValue(newi, parseInt(getValue(newi))+1);
	showDiv(div_new+getValue(newi),'table-row');
}
/**
 * schimba imaginea pozei img_id, si a hiddenului select_id
 */
function selUnselItem(select_id, imgitem_id)
{
	if (getValue(select_id)==0)//ma duc pe un item neselectat ( deci bifez prima data)
	{
		setValue(select_id,'1');//setez starea
		getElem(imgitem_id).src='images/design/tree/itemsel.gif';//setez poza 
	}
	else //ma duc pe unul selectat ( deci anulez )
	{
		setValue(select_id,'0');//setez starea
		getElem(imgitem_id).src='images/design/tree/item.gif';//setez poza 
	}	
}
function isEmail(str) 
{
	var lastdot = str.lastIndexOf(".");
	var at = str.indexOf("@");
	var lastat = str.lastIndexOf("@");
	
	return (at>0 && at==lastat && lastdot > 2 && lastdot<(str.length-2));
}
/**
 * Trims the leading spaces of a string 
 * @param String thestring
 * @return String trimmed
 */
function ltrim(thestring)
{
	return thestring.replace(/^\s+/,'');
}
/**
 * Trims the ending spaces of a string 
 * @param String thestring
 * @return String trimmed
 */
function rtrim(thestring)
{
	return thestring.replace(/\s+$/,'');
}
/**
 * Trims both the leading and the ending spaces
 * @param String thestring
 * @return String
 */
function trim(thestring)
{
	return ltrim(rtrim(thestring));
}

function updateDiv(sDiv_id, sUrl, sMethod, onComplete)
{
	var method = sMethod;
	if (method==null || method=='GET') method = 'GET';
	else method = 'POST';
	
	var div = getElem(sDiv_id);
	try{
		if (getElem(sDiv_id+'_loading')!=null)
		{
			hideElem(sDiv_id);
			showElem(sDiv_id+'_loading');
		}
	}
	catch (e){}
	
	var handleSuccess = function(o){
		if(o.responseText !== undefined){
			div.innerHTML = o.responseText;
			try{
				if (getElem(sDiv_id+'_loading')!=null)
				{
					hideElem(sDiv_id+'_loading');
					showElem(sDiv_id);
				}
			}catch (e){}
			if (onComplete!=null) onComplete();
		}
	}
	
	var handleFailure = function(o){
		if(o.responseText !== undefined){
			div.innerHTML = "Ajax error";
			try{hideElem(sDiv_id+'_loading');}catch (e){}
		}
	}
	
	var handleUpload = function(o){
		if(o.responseText !== undefined){
			div.innerHTML = o.responseText;
			try{
				if (getElem(sDiv_id+'_loading')!=null)
				{
					hideElem(sDiv_id+'_loading');
					showElem(sDiv_id);
				}
			}catch (e){}
			if (onComplete!=null) onComplete();
		}
	}
	
	var callback =
	{
		success:handleSuccess,
		failure: handleFailure,
		upload: handleUpload
	};

	YAHOO.util.Connect.asyncRequest(method, sUrl, callback); 
}
function isHexcode(string)
{
	if (string==null || string.length<3) return false;
	var pattern = /^[a-f\d]{3,6}$/i;
		
	return string.match(pattern);
}
function redirectToLogin(type)
{
	self.location = type+'_login.php';
}
function toggleTextareaHeight(elem, h1, h2)
{
	if (elem.style.height==(''+h1+'px')) elem.style.height = ''+h2+'px';
	else if (elem.style.height==(''+h2+'px')) elem.style.height = ''+h1+'px';
}
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}