// Vot articol, restaurant
function doVote(vote_url, id, vot, module_name)
{
	// show loading
	new Ajax.Request(vote_url,
	{
		method: "get",
		parameters: {"id": id, "vot": vot, "module_name": module_name },
		onSuccess: function(transport)
		{
			// aici tratam on success
			var json=transport.responseText.evalJSON(true);//alert(transport.responseText.evalJSON(true));
			if (json==false)
			{
				alert("Eroare comunicare AJAX!");
			} else
			{
				if (!json.err)
				{
					$("medie_voturi").update(json.medie_voturi);
					$("rating_stars").update(json.rating_stars);
				} else
				{
					alert("Eroare: "+json.msg);
				}
			}
		},
		onFailure: function()
		{
			alert("Eroare AJAX !");
		}
		
	});
}
// color stars
function selectedStars(n,start)
{
	for (var i=start; i<=5; i++)
	{
		//alert(i+"--"+n);
		if (i<=n)
		{
			if ($("star"+i).className=="unselected")
			{
				$("star"+i).className=$("star"+i).className.replace("unselected", "selected");
			}
		}
	}	
}
// uncolor stars
function unselectedStars(id)
{
	var root = $(id);
	li = root.getElementsByTagName('A');
	for (var i=0; i<li.length; i++)
	{
		if (li[i].className=="selected")
			{
				li[i].className=li[i].className.replace("selected", "unselected");
			}
	}
}
// color all stars to the one selected
function doRate(id,selected_star)
{
	var root = $(id);
	li = root.getElementsByTagName('A');
	for (var i=selected_star; i<=4; i--)
	{
		li[i].className=li[i].className.replace(li[i].className, "selected");
	}
}
function createCookie(name, id, 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+id+"="+value+expires+"; path=/";
}

function readCookie(name,id) 
{
	var nameEQ = name+ id + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function change_image(id)
{
	$("reteta_img").src="/images/"+id+".jpg";
}

// Show or hide an element
function clickedElement(id)
{
	if ($(id).style.display == "block")
	{	
		$(id).style.display = "none";
	}	
	else
	{
		$(id).style.display = "block";
	}
}
// Focus in an form
function focusForm(form_name, elem)
{
  $(form_name).focusFirstElement(elem);
}
function validateEmail(id)
{
	email_expr= /^[^@]+@[^@]+.[a-z]{2,}$/i;
	id_error=id+'_error';
	if ($(id).value.search(email_expr)==-1) 
	{
		$(id_error).style.display="block";
		$(id_error).innerHTML="Introduceti o adresa de email valida!";
		return false;
	}
	else
	{ 
		$(id_error).style.display="none";
		return true;
	}
}
function validateString(id)
{
	string=/^[a-zA-Z]+.\s*.+[^0-9]+$/;
	id_error=id+'_error';
	if ($(id).value.search(string)==-1) 
	{
		$(id_error).style.display="block";
		$(id_error).innerHTML="Introduceti un nume valid!";
		return false;
	}
	else 
	{
		$(id_error).style.display="none";
		return true;
	}
}
// Validare formular newsletter
function validateNewsletter(f_name)
{
	with (f_name)
	{
		if ((f_name["nume_ab"].value=="")&&(f_name["prenume_ab"].value=="")&&(f_name["email_ab"].value==""))
		{
			alert("Toate campurile cu * trebuiesc completate!");
			return false;
		}
		if (!validateString(f_name["prenume_ab"].id)) return false;
		if (!validateString(f_name["nume_ab"].id)) return false;
		if(!validateEmail(f_name["email_ab"].id)) return false
		if (!f_name["adress_book"].checked)
		{
			$("coolinaria_adress_book").style.display = "block";
			$("coolinaria_adress_book").innerHTML = "Va rugam sa adaugati info@coolinaria.ro in Address Book-ul dvs."
			return false;
		}
		else 
		{
			$("coolinaria_adress_book").style.display = "none";
		}
		if ((!f_name["sfaturi"].checked)&&(!f_name["retete"].checked)) 
		{
			$("newsletter_error").style.display = "block";
			$("newsletter_error").innerHTML = "Trebuie sa selectati cel putin un newsletter";
			return false;
		}
		else 
			if((f_name["sfaturi"].checked)||(f_name["retete"].checked)) 
			{
				$("newsletter_error").style.display = "none";
			}
	}
	return true;
} 
// Validare formular forgot password
function validateForgot(f_name)
{
	with (f_name)
	{
		if ((f_name["nickname_forgot"].value=="") && (f_name["email_forgot"].value=="")) 
		{
			$("nickname_forgot_error").style.display = "block";
			$("nickname_forgot_error").innerHTML = "Unul din cele 2 campuri trebuie completat!";
			return false;
		}
		else
		{
			if ((f_name["nickname_forgot"].value!="") || (f_name["email_forgot"].value!=""))
			{
				$("nickname_forgot_error").style.display = "none";
				if (f_name["nickname_forgot"].value!="")
				{
					if (!validateString(f_name["nickname_forgot"].id)) 
					{
						return false;
					}
					else
					{
						$(f_name["nickname_forgot"].id+"_error").style.display = "none";
					}
				}
				if(f_name["email_forgot"].value!="")
				{
					if(!validateEmail(f_name["email_forgot"].id)) 
					{
						return false;
					}
					else
					{
						$(f_name["email_forgot"].id+"_error").style.display = "none";
					}
				}
			}
		}		
	}	
	
	return true; 
}
function validateSend()
{
	if (($("nume_expeditor").value== "")||($("nume_prieteni").value== "")||($("email_expeditor").value== "")||($("email_prieteni").value== "")||($("mesaj").value== ""))
	{
		alert('Toate campurile trebuiesc completate!!!');
		return false;
	}
	if ((validateString('nume_expeditor')=='true')||(validateString('nume_prieteni')=='true')||(validateEmail('email_expeditor')=='true')||(validateEmail('email_prieteni')=='true'))
	{
		return true;
	}
	
	else
	{
		return false;
	} 
}
// Validare formular concurs
function validateConcurs()
{
	if($("raspuns").value=="")
	{
		alert("Trebuie sa raspunzi la intrebare pentru a participa la concurs!");
		return false;
	}
	return true;
}
// Validare formular adaugare comentariu
function validateAddComment(key)
{
	if (($("email").value=="")||($("fullname").value=="")||($("comment").value==""))
	{
		alert('Toate campurile cu * trebuiesc completate!!!');
		return false;
	}
	if ($("cod").value=="")
	{
		alert('Mai sunt campuri obligatorii de completat!');
		$("cod").style.border = "1px solid red";
		return false;
	}
	if (!validateEmail("email")) return false;
	if (!validateString("fullname")) return false;
	if (readCookie(MD5("coolinaria__captcha_code_" + key), "") != MD5($("cod").value.toUpperCase()))
	{	
		$("cod_error").style.display = "block"; 
		$("cod_error").innerHTML = "Codul introdus nu e corect!!!";
	 	return false;
	}
	
	return true;
}
// Validare formular reply
function validateReply(f_name,key)
{
	with (f_name)
	{
		if ((f_name["r_email"].value=="")||(f_name["name"].value=="")||(f_name["c_reply"].value==""))
		{
			alert('Toate campurile cu * trebuiesc completate!!!');
			return false;
		}
		if (f_name["r_cod_"+key].value=="")
		{
			alert('Mai sunt campuri obligatorii de completat!');
			$("r_cod_"+key).style.border = "1px solid red";
			return false;
		}
		if(!validateEmail(f_name["r_email"].id)) return false;
		if (!validateString(f_name["name"].id)) return false;
			 
		if (readCookie(MD5("coolinaria__captcha_code_" + key), "") != MD5($("r_cod_"+key).value.toUpperCase())) 
		{
			
			$("r_cod_"+key+"_error").style.display = "block"; 
			$("r_cod_"+key+"_error").innerHTML = "Codul introdus nu e corect!!!";
	 		return false;
	 	}
	}
	return true;
}
// Validare formular abonare newsletter
function validFormAbonare(f_name)
{
	if (f_name["inscriere"].value=="") 
	{	
		alert("Campul email trebuie completat!");
		return false;
	}
	else
	{
		if (!validateEmail(f_name["inscriere"].id)) return false; 
	}
	return true;
}
function addCommentElement()
{
  var ni = $("comentarii");
  var numi = document.getElementById('val');
  var num = (document.getElementById('val').value -1)+ 2;
  numi.value = num;
  var divId = 'new_comment'+num;
  var div = new Element('div',{'id': divId,'class': 'comment_style'});
  var p1 = new Element('p', {'class': 'author_comment'}).update($("fullname").value);
  var span = new Element('span', {'id': 'data'});
  var p2 = new Element ('p').update($("comment").value);
  p1.appendChild(span);
  div.appendChild(p1);
  div.appendChild(p2);
  ni.appendChild(div);
 
}
function addElementRetete()
{
  var root = $("upload");
  var nr_files = $("nr_files").value;
  nr_files++;
  var id = 'file'+nr_files;
  var id_a = 'a_'+nr_files;
  var id_br1 = 'br1_'+nr_files;
  var id_br2 = 'br2_'+nr_files;
  
  var input = new Element('input', { 'class': 'file_tag', 'id': id, 'name': id, type: 'file' });
  var br1 = new Element('br', {'id': id_br1});
  var br2 = new Element('br', {'id': id_br2});
  var a = new Element('a', {'id': id_a, 'href': 'javascript:removeElementRetete("'+id+'","'+id_a+'","'+id_br1+'","'+id_br2+'");'}).update('Remove');
  var div = new Element('div', {'class': 'noFloat'})
  root.appendChild(input);
  root.appendChild(div);
  root.appendChild(a);
  root.appendChild(br1);
  root.appendChild(br2);
  
  $("nr_files").value = nr_files;
}
function removeElementRetete(id, id_a, id_br1,id_br2) 
{
  var root = $("upload");
  var olddiv = $(id);
  var old_a = $(id_a);
  var old_br1 = $(id_br1);
  var old_br2 = $(id_br2);
  
  root.removeChild(olddiv);
  root.removeChild(old_a);
  root.removeChild(old_br1);
  root.removeChild(old_br2);
  
  var nr_files = $("nr_files").value;
  nr_files--;
  $("nr_files").value = nr_files;
}
function addElement()
{
  var ni = $("send_t");
  var tbody = $("send_table"); 
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;
  var tdId = 'friend'+num;
 	var tr = new Element('tr',{'id': tdId});
 	var input1 = new Element('input', {'class': 'add_input','name': 'prieten'+num});
 	var input2 = new Element('input', {'class': 'add_input','name': 'email_prieten'+num});
 	var a = new Element('a', {'class': 'add_friend','href': 'javascript:removeElement("'+tdId+'");'}).update('Remove');
 	var td = new Element ('td');
 	td.appendChild(input1);
 	td.appendChild(input2);
 	td.appendChild(a);
 	tr.appendChild(td);
 	tbody.appendChild(tr);
 	ni.appendChild(tbody);
}
function removeElement(tdNum) 
{
  var d = $("send_table");
  var olddiv = $(tdNum);
  d.removeChild(olddiv);
}
// Meniu subcategorii din pagina de afisare categorie
function menuSubcategorii(assoc,current_id)
{
	var root = $("lista_subcat");
	var a = root.getElementsByTagName('A');
	var id = current_id.substring(current_id.indexOf(assoc+"_") + String(assoc+"_").length);
	current_description = $("description_" + id);
	for (var i=0; i<a.length; i++)
	{ 
		a[i].className=a[i].className.replace("item_selected", "");
		id_s = a[i].id.substring(a[i].id.indexOf(assoc+"_") + String(assoc+"_").length);
		$("description_" + id_s).hide();
	}
	$(current_id).className = "item_selected";
	current_description.style.display = 'block';
}
function menuSubcategorii2(current_item,type,item_type,id,max)
{
	var root = $("lista_subcat");
	var a = root.getElementsByTagName('A');
	for (var i=0; i<a.length; i++)
	{ 
		a[i].className=a[i].className.replace("item_selected", "");
	}
	$(current_item).className = "item_selected";
	
	param = "type=" + type + "&item_type=" + item_type + "&id=" + id + "&max=" + max;
	new Ajax.Updater(
		{success: 'container_items'},
		"/sectiune/items",
		{method: 'get', 
		 parameters: param,
		 onFailure: 'error',
		 onSuccess: function (oXHR, oJson) {$("loading_gif").hide();},
		 onLoading: $("loading_gif").update('<div id="loading" style="position: relative;text-align:right;top: 68px;right: 135px;padding:10px 0px 10px 0px;"><img src="/images/ajax-loader.gif" title="Loading" /></div>')},
		{insertion: Insertion.Bottom}
	);
}
// Selectarea tagurilor  din pagina de adaugare reteta
function moveSelectedOptions(from,to) 
{
	// Unselect matching options, if required
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
			to.options[index] = new Option( o.text, o.value, false, false);
			}
		}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
}
function hasOptions(obj) 
{
	if (obj!=null && obj.options!=null) { return true; }
	return false;
}
var tags = new Array();
function moveSelectedOptions(from,to) 
{
	// Unselect matching options, if required
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if ((from.name=="tag_list1") && (to.name=="tag_list2"))
			{
				tags.push(o.value); 
			}
			else
			{
				var ret = new Array();
				for (var j=0; j<tags.length; j++)
				{
					if (tags[j]!=o.value) ret.push(tags[j]);
				}
				tags=ret; 
			}
			if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
			to.options[index] = new Option( o.text, o.value, false, false);
			}
		}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	document.frm_flash_thumb.tag_values.value=tags;
}
function sortSelect(obj) 
{
	var o = new Array();
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
}
function addCommentJson(request, json, comm_url, id_reteta)
{
	var root = document.getElementById('add_comment_new');
	var numi = document.getElementById('theValue');
	var num = (document.getElementById('theValue').value -1)+ 2;
	numi.value = num;
	var divId = 'comment_nou_'+num; 
	var div = new Element('div', {'id': divId});
	root.appendChild(div);
	comment = document.addCommentform.comment.value;
	fullname = document.addCommentform.fullname.value;
	email = document.addCommentform.email.value;
	url = document.addCommentform.url.value;
	new Ajax.Request(comm_url,
	{
		method: "get",
		parameters: {"id_reteta": id_reteta, "comment": comment, "fullname": fullname, "email": email, "url": url },
		onSuccess: function(transport)
		{
			// aici tratam on success
			var json=transport.responseText.evalJSON(true);
			if (json==false)
			{
				alert("Eroare comunicare AJAX!");
			} else
			{
				if (!json.err)
				{
					$(divId).update(json.comment);			
					$("add_comment").hide();			
				} else
				{
					alert("Eroare: "+json.msg);
				}
			}
			
				
		},
		onFailure: function()
		{
			alert("Eroare AJAX !");
		}
		
	});
}  
function lista_select_tag(lista, url, param)
{
	for(i=0;i<lista.options.length;i++)
	{
		if(lista.options[i].selected)
		{
			chosen_item = lista.options[i].value;
		}
	}
	url +="/"+chosen_item+"/"+param;
	document.location.href = url;
}
function lista_select_condimente(lista, url)
{
	for(i=0;i<lista.options.length;i++)
	{
		if(lista.options[i].selected)
		{
			chosen_item = lista.options[i].value;
		}
	}
	if(chosen_item.indexOf(","))
	{
		params = chosen_item.split(",");
	}
	id_condiment = params[0];
	nume_condiment = params[1].replace(" ","-"); 
	url +="/"+id_condiment+"/"+nume_condiment+".html";
	document.location.href = url;
}
function lista_select_orase(lista, url, param)
{
	for(i=0;i<lista.options.length;i++)
	{
		if(lista.options[i].selected)
		{
			chosen_item = lista.options[i].value;
		}
	}
	url +="/"+chosen_item+"/10/1";
	document.location.href = url;
}
// Meniu brand-uri
scrollStep=8
timerLeft=""
timerRight=""
function scrollDivLeft(id){
  clearTimeout(timerRight) 
  document.getElementById(id).scrollLeft+=scrollStep
  timerRight=setTimeout("scrollDivLeft('"+id+"')",5)
}
function scrollDivRight(id){
  clearTimeout(timerLeft)
  document.getElementById(id).scrollLeft-=scrollStep
  timerLeft=setTimeout("scrollDivRight('"+id+"')",5)
}
function stopMe(){
  clearTimeout(timerRight) 
  clearTimeout(timerLeft)
}
// Galerie imagini din pagina de afisare a unei galerii
var currentImg=0;
function picturesGallery(id_galerie)
{
	var lista = $("lista_pics").getElementsByTagName("A");
	var imgs = $("lista_pics").getElementsByTagName("IMG");
	var descrieri = $("descrieri_poza").getElementsByTagName("P");
	var total = $("total_nr").innerHTML;
	
	if($("default_image"))
	{
		$("default_image").id+=$("default_image").id.replace("default_image", "opacityNone");
		$("gallery_info").style.display="none";
		$("gallery_details").style.display = "block";
		$("total_pages").innerHTML ="1/"+lista.length;
		if($("galerie" + id_galerie + "_1"))
		{
			$("galerie" + id_galerie + "_1").style.display = "block";
		}
	}
	
	document.getElementById('gallery_right_arrow').onclick = function(){
		document.getElementById('gallery_left_arrow').style.display = "none";
		for (var i=0;i<lista.length-1;i++)
		{
			if ((lista[i].className.indexOf('showImage'))!=-1)	
			{
				lista[i].className=lista[i].className.replace("showImage", " ");
				i++;
				changeOpac(40,imgs[i].id);
				lista[i].className+="showImage";
				x=i+1;
				$("total_pages").innerHTML = x+"/"+lista.length;
				document.getElementById('gallery_left_arrow').style.display = "inline";
				$("g_big_img").style.margin = "0px 0px 0px 0px";
				for(var j=0;j<descrieri.length;j++)
				{
					$(descrieri[j].id).style.display = "none";
				}
				if($("galerie" + id_galerie + "_" + x)){
					$("galerie" + id_galerie + "_" + x).style.display = "block";
				}
			}
			if (i==lista.length-1)
			{
				document.getElementById('gallery_right_arrow').style.display = "none";
			}
			
		}
	}
	
	document.getElementById('gallery_left_arrow').onclick = function(){
		for ( var i=lista.length-1;i>0;i--)
		{
			if ((lista[i].className.indexOf('showImage'))!=-1)	
			{
				lista[i].className=lista[i].className.replace("showImage", " ");
				i--;
				changeOpac(40,imgs[i].id);
				lista[i].className+="showImage";
				x = i+1;
				$("total_pages").innerHTML = x+"/"+lista.length;
				document.getElementById('gallery_right_arrow').style.display = "inline";
				for(var j=0;j<descrieri.length;j++)
				{
					$(descrieri[j].id).style.display = "none";
				}
				if($("galerie" + id_galerie + "_" + x)){
					$("galerie" + id_galerie + "_" + x).style.display = "block";
				}
			}
			if (i==0)
			{
				document.getElementById('gallery_left_arrow').style.display = "none";
				$("g_big_img").style.margin = "0px 0px 0px 28px";
				$("g_big_img").style.display = "inline";
			}
		}
	}
}
// Fade pt imagini
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	opacity+=10;
	object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
    setTimeout("changeOpac("+opacity+",'" + id + "')",10);
}
function showOpac(elem)
{
	el = $(elem).style;
	el.opacity=1;
	el.filter = "alpha(opacity=100)";
}
function restoreOpac(elem)
{
	el = $(elem).style;
	el.opacity=0.4;
	el.filter = "alpha(opacity=60)";
}
// Nr caractere pt comentariu(textarea tag)
var rac_int = null;
var rac_ta = null;
function refreshAvailableChars()
{
	end_rac();
	var ta_available = (rac_ta.previousSibling.nodeType == 1 ? rac_ta.previousSibling : rac_ta.previousSibling.previousSibling);
	ta_available.innerHTML = "Caractere disponibile: " + (500 - rac_ta.value.length);
}
function end_rac()
{
	clearInterval(rac_int);
	rac_int = null;
}
function refreshTextarea(ta)
{
	if (ta.value.length > 500) ta.value = ta.value.substring(0,500);
	end_rac();
	rac_ta = ta;
	rac_int = setInterval(refreshAvailableChars, 250);
}
// Verifica adresa de email pt newsletter 
function newsletter_checkValue(el)
{
	if (el.value=="adresa de mail")
	{
			el.value="";
	} else
	if (el.value=="")
	{
		el.value="adresa de mail";
	}
}
// Verifica daca un camp e gol
function emptyField(id)
{
	if ($(id).value!="")
	{
		$(id+"_error").style.display = "none";
		return true;
	}
	else
	{
		$(id+"_error").style.display = "block";
		$(id+"_error").innerHTML = "Campul trebuie completat";
		return false;
	}
}
// Formular search pe home page (header)
function validateSearchForm(id)
{
	
	if ($(id).value!="")
	{
		$(id+"_error").style.display = "none";
		$("login_form").style.padding = "35px 0px 0px 10px";
		$("cautare_avansata").style.margin = "5px 0px 0px 10px";
		return true;
	}
	else
	{
		$(id+"_error").style.display = "block";
		$(id+"_error").innerHTML = "Campul trebuie completat";
		$("login_form").style.padding = "20px 0px 0px 10px";
		$("cautare_avansata").style.margin = "20px 0px 0px 10px";
		return false;
	}
}
// Galerie imagini articol
function showBigImage(id,type,nr)
{
	$("image_zoom").src = "/images/"+type+"/"+id+"/"+id+"_b"+nr+".jpg";
}
// Change a class for an element
function changeClassName(id,old_class,new_class)
{
	if (($(id).className.indexOf(old_class))!=-1)	
	{
		$(id).className=$(id).className.replace(old_class, new_class);
	}
}
function flash_thumb_resize(newW, newH)
{
 newW = 450;
 if (document.all && !document.getElementById)
 {
  document.all['flash_thumb'].style.pixelWidth = newW;
  document.all['flash_thumb'].style.pixelHeight = newH;
 } else {
  document.getElementById('flash_thumb').style.width = newW + "px";
  document.getElementById('flash_thumb').style.height = newH + "px";
 }
}
 
function updateFormVariable(sForm, sName, sValue)
{
 document.forms[sForm][sName].value = sValue;
}
function drawPercentBar(width, percent, color, background) 
{ 
var pixels = width * (percent / 100); 
if (!background) { background = "none"; }

document.write("<div style=\"position: relative;color: #534741;float: left;line-height: 1em; background-color: " 

               + background + "; border: 1px solid #a0824b; width: " 
               + width + "px\">"); 
document.write("<div style=\"height: 1.5em;color: #534741;width: " + pixels + "px; background-color: "
               + color + ";\"></div>"); 
document.write("<div style=\"position: absolute;color: #534741;text-align: center; padding-top: .25em; width: " 
               + width + "px; top: 0; left: 0\">" + percent + "%</div>"); 

document.write("</div>"); 
} 
// dsiplay all fields in form inregistrare
function addAllFields(holder)
{
	if(($(holder).style.display=="")||($(holder).style.display=="none"))
	{
		$(holder).style.display="block";
		$("img_fields_all").src = '/images/btn_down_arrow.jpg';
		$("text_fields_all").update("<b>Completez alta data datele din profilul meu</b>");
	}
	else
	{
		$(holder).style.display="none";
		$("img_fields_all").src = '/images/btn_up_arrow.jpg';
		$("text_fields_all").update("<b>Vreau sa imi completez profilul cu toate datele</b>");
	}
}
// js comment
/*function toggleReplyForm(holder)
{
	var item;
	var root=$("commentsRoot");
	var c_id_form = holder.substring(holder.indexOf("comments_holder_") + String("comments_holder_").length);
	current_reply_form = $("replyComment_" + c_id_form);
	current_name = "replyCommentContainer_" + c_id_form;
	root_childs = $("commentsRoot").getElementsByTagName('DIV');
	for (var i=0;i<root_childs.length;i++)
	{
		item = root_childs[i]; 
		if (typeof(item.id ) != "undefined")
		{
			if (item.id.indexOf("replyComment_") >= 0)
			{
				if (item.id==current_reply_form.id)
				{
					item.style.display="block";
					$(current_name).focusFirstElement('c_reply');
				}
				else
				{
					item.style.display="none";
				}
			}
		}
	}
	
}
*/
function toggleReplyForm(reply_url, holder, id_update,id_comentariu, parent_id, id, url)
{
	var item;
	var root=$("commentsRoot");
	
	root_childs = $("commentsRoot").getElementsByTagName('DIV');
	for (var i=0;i<root_childs.length;i++)
	{
		item = root_childs[i]; 
		if (typeof(item.id ) != "undefined")
		{
			if (item.id.indexOf("replyComment_") >= 0)
			{
				if (item.id==id_update)
				{	
					if(item.style.display=="block") {
						item.style.display="none";
					} else {
						item.style.display="block";
					}
				}
				else
				{
					item.style.display="none";
				}
			}
		}
	}
	param = "id_comentariu="+id_comentariu+"&parent_id="+parent_id+"&url="+url+"&id="+id; 
	new Ajax.Updater(
		{success: id_update},
		reply_url,
		{method: 'get', 
		 parameters: param,
		 onFailure: 'error',
		 onSuccess: function (oXHR, oJson) {
			$("loading_gif_"+id_comentariu).hide();
		 },
		 onComplete: function (oXHR, oJson) {
			 focusForm("replyCommentContainer_"+id_comentariu, "name_"+id_comentariu)
		 },
		 onLoading: $("loading_gif_"+id_comentariu).update('<div id="loading" style="position: relative;text-align:right;top: 0px;right: 200px;padding:10px 0px 10px 0px;"><img src="/images/ajax-loader.gif" title="Loading" /></div>')},
		{insertion: Insertion.Bottom}
	);
	
}
function toggleReplyClose(current_reply)
{
	$(current_reply).style.display="none";
}
//home page gallery
function clickGallery(id) 
	{
	var getSpan = document.getElementById('gallery').getElementsByTagName("SPAN");
	var getEls = document.getElementById('gallery').getElementsByTagName("DIV");
	var getLi = document.getElementById('square_list').getElementsByTagName("LI");
    var getH = document.getElementById('gallery').getElementsByTagName("H3");
		getLi[id].onclick=function() {
			for (var j=0; j<getLi.length; j++) {
				getEls[j].className=getEls[j].className.replace("chosen", "");
				getEls[j].className=getEls[j].className.replace("on", "off");
				getLi[j].className=getLi[j].className.replace("on_square", " ");
				getH[j].className=getH[j].className.replace("on_h", "off_h");
			}
			getLi[id].className+=" on_square";
			getEls[id].className+=" chosen";
			getEls[id].className=getEls[id].className.replace("off", "on");
			getH[id].className=getH[id].className.replace("off_h", "on_h");
			if (id==0)  
			{
				document.getElementById('previous').style.cursor='text';
				$("previous").className=$("previous").className.replace("previous_image", "previous_image_selected");	
			}
			else 
			{
				document.getElementById('previous').style.cursor='pointer';
				$("previous").className=$("previous").className.replace("previous_image_selected", "previous_image");	
			}
			if (id==getEls.length-1) 
			{
				 document.getElementById('next').style.cursor='text';
				 $("next").className=$("next").className.replace("next_image", "next_image_selected");	
			}					
			else 
			{
				document.getElementById('next').style.cursor='pointer';
				$("next").className=$("next").className.replace("next_image_selected", "next_image");	
			}
	}
	
	for (var i=0; i<getSpan.length; i++) {
		getSpan[i].onclick=function() {
			if ((this.className.indexOf('previous_image'))!=-1) 
			{
				for (var x=getEls.length-1;x>0; x--) 
						{
							if ((getEls[x].className.indexOf('chosen'))!=-1 && (getLi[x].className.indexOf('on_square'))!=-1)	
							{
							
								getEls[x].className=getEls[x].className.replace("chosen", "");
								getEls[x].className=getEls[x].className.replace("on", "off");
								getLi[x].className=getLi[x].className.replace("on_square", " ");
								getH[x].className=getH[x].className.replace("on_h", "off_h");
								x--;
								getEls[x].className+=" chosen";
								getLi[x].className+=" on_square";
								getEls[x].className=getEls[x].className.replace("off", "on");
								getH[x].className=getH[x].className.replace("off_h", "on_h");

								if (x==0) { 
									document.getElementById('previous').style.cursor='text';
									$("previous").className=$("previous").className.replace("previous_image", "previous_image_selected");
						   
								}
								else {
									$("previous").className=$("previous").className.replace("previous_image_selected", "previous_image");	
									document.getElementById('previous').style.cursor='pointer';
								}
								document.getElementById('next').style.cursor='pointer';
								$("next").className=$("next").className.replace("next_image_selected", "next_image");	
							}
				}
						
			}	
			
			
			if ((this.className.indexOf('next_image'))!=-1) 
			{
						for (var x=0; x<getEls.length-1; x++) 
						{
							if ((getEls[x].className.indexOf('chosen'))!=-1 && (getLi[x].className.indexOf('on_square'))!=-1)	
							{
								getEls[x].className=getEls[x].className.replace("chosen", "");
								getEls[x].className=getEls[x].className.replace("on", "off");
								getLi[x].className=getLi[x].className.replace("on_square", " ");
								getH[x].className=getH[x].className.replace("on_h", "off_h");
								x++;
								getEls[x].className+=" chosen";
								getLi[x].className+=" on_square";
								getEls[x].className=getEls[x].className.replace("off", "on");
								getH[x].className=getH[x].className.replace("off_h", "on_h");
								if (x==getEls.length-1) { 
									document.getElementById('next').style.cursor='text';
									$("next").className=$("next").className.replace("next_image", "next_image_selected");	
								}
								else {
									
									document.getElementById('next').style.cursor='pointer';
								}
								document.getElementById('previous').style.cursor='pointer';
								$("previous").className=$("previous").className.replace("previous_image_selected", "previous_image");	
							}
						}
						
			}	
	}
	}
}

   	/* *** START Image slideshow *** */
   	
	var displayWaitMessage=true;	// Display a please wait message while images are loading?
  	
   		
	var activeImage = false;
	var imageGalleryLeftPos = false;
	var imageGalleryWidth = false;
	var imageGalleryObj = false;
	var maxGalleryXPos = false;
	var slideSpeed = 40;
	var imageGalleryCaptions = new Array();
	function startSlide(e)
	{
		if(document.all)e = event;
		var id = this.id;
		if(this.id=='arrow_right'){
			slideSpeedMultiply = Math.floor((e.clientX - this.offsetLeft) / 5);
			slideSpeed = -1*slideSpeedMultiply;
			slideSpeed = Math.max(-30,slideSpeed);
		}else{			
			slideSpeedMultiply = 20 - Math.floor((e.clientX - this.offsetLeft) / 5);
			slideSpeed = 1*slideSpeedMultiply;
			slideSpeed = Math.min(25,slideSpeed);
			if(slideSpeed<0)slideSpeed=40;
		}
	}
	
	function releaseSlide()
	{
		var id = this.id;
		slideSpeed=0;
	}
		
	function gallerySlide()
	{
		if(slideSpeed!=0){
			var leftPos = imageGalleryObj.offsetLeft;
			leftPos = leftPos/1 + slideSpeed;
			if(leftPos>maxGalleryXPos){
				leftPos = maxGalleryXPos;
				slideSpeed = 0;
				
			}
			if(leftPos<minGalleryXPos){
				leftPos = minGalleryXPos;
				slideSpeed=0;
			}
			
			imageGalleryObj.style.left = leftPos + 'px';
		}
		setTimeout('gallerySlide()',10);
		
	}
	
	function showImage()
	{
		
		activeImage = this;	
	}
	
	function initSlideShow()
	{
		document.getElementById('arrow_left').onmousemove = startSlide;
		document.getElementById('arrow_left').onmouseout = releaseSlide;
		document.getElementById('arrow_right').onmousemove = startSlide;
		document.getElementById('arrow_right').onmouseout = releaseSlide;
		
		imageGalleryObj = document.getElementById('theImages');
		imageGalleryLeftPos = imageGalleryObj.offsetLeft;
		imageGalleryWidth = document.getElementById('galleryContainer').offsetWidth - 80;
		maxGalleryXPos = imageGalleryObj.offsetLeft; 
		minGalleryXPos = imageGalleryWidth - document.getElementById('slideEnd').offsetLeft;
		var slideshowImages = imageGalleryObj.getElementsByTagName('IMG');
		for(var no=0;no<slideshowImages.length;no++){
			slideshowImages[no].onmouseover = showImage;
		}
		
		gallerySlide();
	}
	
	function showPreview(imagePath,imageIndex){
		var subImages = document.getElementById('previewPane').getElementsByTagName('IMG');
		if(subImages.length==0){
			var img = document.createElement('IMG');
			document.getElementById('previewPane').appendChild(img);
		}else img = subImages[0];
		
		if(displayWaitMessage){
			document.getElementById('waitMessage').style.display='inline';
		}
		document.getElementById('largeImageCaption').style.display='none';
		img.onload = function() { hideWaitMessageAndShowCaption(imageIndex-1); };
		img.src = imagePath;
		
	}
	
	function changeByResolution()
	{
	    var w = screen.width;
	    if((w=="800")||(w=="1024"))
	    {
	       	$("left_corner").hide();
	    	$("right_corner").hide();
	    	$("content").style.width = "930px";
	    }
	}
	
	function start()
	{
		changeByResolution();
		initSlideShow();
	}
	
	
	window.onload = start;

	/* *** END Image slideshow *** */
	
// Vot articol, restaurant
function triedReteta(url, id_reteta)
{
	// show loading
	new Ajax.Request(url,
	{
		method: "get",
		parameters: {"id_reteta": id_reteta },
		onSuccess: function(transport)
		{
			// aici tratam on success
			var json=transport.responseText.evalJSON(true);//alert(transport.responseText.evalJSON(true));
			if (json==false)
			{
				alert("Eroare comunicare AJAX!");
			} else
			{
				if (!json.err)
				{
					$("tried").update(json.tried);
					$("btn_tried").hide();
					$("btn_print").style.margin = 0;
				} else
				{
					alert("Eroare: "+json.msg);
				}
			}
		},
		onFailure: function()
		{
			alert("Eroare AJAX !");
		}
		
	});
}

function showTooltip(tooltip_url, top_margin, left_margin, id_container, id_element, id_update) 
{
	var tagLeft = Position.page($(id_element))[0];
	var tagTop = Position.page($(id_element))[1];//alert(tagLeft + "---" + tagTop);
	var containerLeft = Position.page($(id_container))[0];
	var containerTop = Position.page($(id_container))[1];//alert(containerLeft + "---" + containerTop);
	var hPosition = tagLeft - containerLeft;
	var vPosition = tagTop - containerTop;
	new Ajax.Request(tooltip_url,
	{
		method: "get",
		onSuccess: function(transport)
		{
			// aici tratam on success
			var json=transport.responseText.evalJSON(true);//alert(transport.responseText.evalJSON(true));
			if (json==false)
			{
				alert("Eroare comunicare AJAX!");
			} else
			{
				if (!json.err)
				{
					$(id_update).style.display = "block";
					$(id_update).style.top = (vPosition - top_margin) + "px";
					$(id_update).style.left = (hPosition - left_margin) + "px";
					$(id_update).update(json.result);
				} else
				{
					alert("Eroare: "+json.msg);
				}
			}
		},
		onFailure: function()
		{
			alert("Eroare AJAX !");
		}
		
	});
}
function hideTooltip(id_element) 
{
	$(id_element).hide();
}
// show google map
function showGoogleMap() {
	latlng = new google.maps.LatLng(44.4157871, 26.1094058);
    myOptions = {
      zoom: 15,
      center: latlng,
      mapTypeControl: true,
      mapTypeControlOptions: {
          style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
          position: google.maps.ControlPosition.TOP_RIGHT
      },
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map($("map_canvas"), myOptions);
    x = document.getElementById("x_adr_restaurant").innerHTML;
    y = document.getElementById("y_adr_restaurant").innerHTML;
    address = new google.maps.LatLng(x, y);
	if (address) {
        map.setCenter(address);
        marker = new google.maps.Marker({
            map: map, 
            position: address,
            icon: '/images/markers/ball_green_simple.png'
        });
        str = $("tooltip_harta_detalii").innerHTML;
        info = new google.maps.InfoWindow({  
			content: str 
		}); 
        google.maps.event.addListener(marker, 'mouseover', function() {
			info.open(map, marker);
			marker.setIcon('/images/markers/ball_yellow_simple.png');
		});
		google.maps.event.addListener(marker, 'mouseout', function() {
			info.close();
			marker.setIcon('/images/markers/ball_green_simple.png');
		});
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    
  }

function callAction(url, params, id_update, id_loading, css, gif) 
{
	if(css == null) css = "position: relative;text-align:right;top: 68px;right: 175px;padding:10px 0px 10px 0px;";
	if(gif == null) gif = "ajax-loader.gif";
	new Ajax.Updater(
			{success: id_update},
			url,
			{method: 'get', 
			 parameters: params,
			 onFailure: 'error',
			 onSuccess: function (oXHR, oJson) {$(id_loading).hide();},
			 onLoading: $(id_loading).update('<div id="loading" style="' + css + '"><img src="/images/' + gif + '" title="Loading" /></div>')},
			{insertion: Insertion.Bottom}
		);
}

function showBoxTabs(url, id, id_update, id_loading) 
{
	param = "id=" + id;
	callAction(url, param, id_update, id_loading,'','ajax-loader2.gif');
}

function showAll(id, tag, old_class, new_class, id_to_hide) 
{
	items = $(id).getElementsByTagName(tag);
	for(i=0;i<items.length;i++)
	{
		items[i].className=items[i].className.replace(old_class, new_class);
	}
	$(id_to_hide).update('');
}

function combinareIngrediente(form, field_name) 
{
	var checkboxes = form['ingrediente'];
	var state = true;
	params = "ingrediente=";
	i=0;
	for(k=0;k<checkboxes.length;k++)
	{
		if (checkboxes[k].checked)
		{
			params = params + checkboxes[k].value +  ","; 
			i++;
		}
    }
	params = params.slice(0, -1);
	if(i>0)
	{
		callAction('/retete/combinareIngrediente', params, 'retete_combinare', 'combinare_loading', 'position: relative;text-align:right;top: 30px;right: 175px;padding:10px 0px 10px 0px;', 'ajax-loader2.gif');
	}
	else
	{
		alert("Trebuie sa selectati ingrediente!");
	}
}

function selectIngredient (ingrediente, value)
{
	for (k = 0; k < ingrediente.length; k++)
	{
		if (ingrediente[k].value == value && ingrediente[k].checked == false) 
		{
			ingrediente[k].checked = true;	
		}
		else if (ingrediente[k].value == value && ingrediente[k].checked == true) 
		{
			ingrediente[k].checked = false;	
		}
	}
}

function checkreqperm(rp)
{
	var perms = rp.split(',');
							    	
	for(var i = 0; i < perms.length; i++)
	{
		if(perms[i] == "email")
		{
			return true;			
		}
	}
							    	
	return false;							    	
}

