/**
 * 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);
}
/**
 * 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';
}
/**
 * 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)
{
	var method = sMethod;
	if (method==null) method = 'GET';
	var div = getElem(sDiv_id);

	
	var handleSuccess = function(o){
		if(o.responseText !== undefined){
			div.innerHTML = o.responseText;
		}
	}
	
	var handleFailure = function(o){
		if(o.responseText !== undefined){
			div.innerHTML = "Ajax error";
		}
	}
	
	var handleUpload = function(o){
		if(o.responseText !== undefined){
			div.innerHTML = o.responseText;
		}
	}
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure,
	  upload: handleUpload
	};
	var request = YAHOO.util.Connect.asyncRequest(method, sUrl, callback); 
}
function changeLoginFormLinks(type)
{
	var newaccount = getElem('login_newaccount');
	var forgotpass = getElem('login_forgotpass');
	
	if (type=='user')
	{
		newaccount.href = 'register_user.html';
		forgotpass.href = 'user_password_recovery.html';
	}
	else
	{
		newaccount.href = 'register_vendor.html';
		forgotpass.href = 'vendor_password_recovery.html';
	}
}
function redirectToLogin(type)
{
	self.location = type+'_login.php';
}