function parse_search_word(max,maxstr) {
	/*maxstr < max - ca sa aiba relevanta codul*/
	var inpt;
	var fw;
	inpt = document.getElementById("cauta_what");
	fw=inpt.value;
	fw = word_no_spaces(fw,maxstr);
	fw = get_paragraph(fw,max,maxstr);
	document.getElementById("cauta_what").value = document.getElementById("cauta_what").value.substring(0,fw.length);
}
function word_no_spaces(str,maxstr) {
	var lsp;
	lsp = str.lastIndexOf(" ");
	if( lsp==-1 && str.length>maxstr) str=str.substring(0,maxstr);
	return str;
}
function get_paragraph(str,max,maxstr) {
	var chk;
	var lsp;
	if(str.length>max) {
		chk=str.substring(0,max);
		chk=word_no_spaces(chk,maxstr);
		if(chk.length>=max+1 && str[max+1]==" ") str=chk;
		else {
			lsp = chk.lastIndexOf(" ");
			if(lsp!=-1) {
				chk=chk.substring(0,lsp);
				str=chk;
			}
		}
		
	}
	return str;
}
function submit_cautare(form_h){
    form_what = form_h.cauta_what.value;
    form_what=form_what.replace('/',' ')
    form_what=form_what.replace('&',' ')
	form_h.action = '/cauta/' + form_what + '/';
	if (form_what=='') alert('Introduceti termenii cautarii');
	else form_h.submit();
}
function addToShop(reteta_id) {
	var log=$('shopbox');
	var req = new Request({
		url: 'http://'+window.location.hostname+'/add-to-shop/'+reteta_id+ '/',
		method: 'get',
		onComplete: function(response) {
			alert(response);
			log.set('html',response);
		}
	}).send();
	return false;
} 
function ClickHereToPrint(){
	try{
		var oIframe = document.getElementById('ifrmPrint');
		var oContent = document.getElementById('divToPrint').innerHTML;
		var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
		if (oDoc.document) oDoc = oDoc.document;
		oDoc.write("<head><title>SiteDeBucate.ro</title>");
		oDoc.write('<link href="/css/style.css" rel="stylesheet" type="text/css" />');
		oDoc.write("</head><body onload='this.focus(); this.print();'>");
		oDoc.write(oContent + "</body>");
		oDoc.close();
	}
	catch(e){
		self.print();
	}
	return false;
}
function restoreRating(rating) {
	rating=parseFloat(rating);
	floor=Math.floor(rating);
	ceil=Math.ceil(rating);
	for(i=1;i<=floor;i++) {
		$('rating'+i).set('class','sprite stelutaGalbena');
	}
	if(floor!=rating) {
		$('rating'+ceil).set('class','sprite stelutaJumatate');
	}
	for(i=(ceil+1);i<=5;i++) {
		$('rating'+i).set('class','sprite stelutaGri');
	}
}
function setVoteRatingColor(item) {
	item=parseInt(item);
	for(i=1;i<=item;i++) {
		$('rating'+i).set('class','sprite stelutaGalbena');
	}
	for(i=(item+1);i<=5;i++) {
		$('rating'+i).set('class','sprite stelutaGri');
	}
}
function voteazaReteta(rating,url) {
	var req = new Request({
		method: 'get',
		url: 'http://'+window.location.hostname+'/voteaza_retete/'+rating+'/'+url+'/',
		onRequest: function() { 
			//alert('Request made. Please wait...'); 
		},
		onComplete: function(response) {
			var r = eval('(' + response + ')');
			if(r.nota!=undefined){
				$$('#ratingGiver a').each(function (item) {
					item.removeClass('stelutaGalbena');
					item.removeClass('stelutaJumatate');
					item.removeClass('stelutaGri');
					var numarItem=(item.get('id')).replace('rating','');
					if(numarItem<=r.nota) item.addClass('stelutaGalbena');
					else if((numarItem-1)>=r.nota) item.addClass('stelutaGri');
					else item.addClass('stelutaJumatate');
				});
				$('ratingGiver').set('onmouseout','restoreRating("'+r.nota+'")');
				$('ratingGeneral').set('html',r.nota);
				$('ratingVotes').set('html',r.nr_note);
			}
			

			alert('Response: ' + r.mesaj); 
		}
    }).send();
	return false;
}
