// JavaScript Document

function del_confirm(lnk)
{
	if (confirm("Esti sigur ca vrei sa stergi?"))
		document.location=lnk;
}

function js_confirm(lnk, text)
{
	if (confirm(""+text))
		document.location=lnk;
}

function getkey(e)
{
	if (window.event)
	   return window.event.keyCode;
	else if (e)
	   return e.which;
	else
	   return null;
}


function goodchars(e, goods)
{
	var key, keychar;
	key = getkey(e);
	if (key == null)
		return true;
	
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods = goods.toLowerCase();
	
	if (goods.indexOf(keychar) != -1)
		return true;
	
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	   return true;
	
	return false;
}

function toggle(control)
{
	obj=document.getElementById(control);
	if (obj.style.display=="none")
		obj.style.display="block";
	else
		obj.style.display="none";
}

marimeFontDefault = 12;
marimeFontMinim = 9;
marimeFontMaxim = 15;

function maresteFont()
{
	theDiv=document.getElementById('enunt_articol');
	marimeFontDefault = marimeFontDefault+1;
	if (marimeFontDefault > marimeFontMaxim) {marimeFontDefault = marimeFontMaxim;}
	theDiv.style.fontSize=marimeFontDefault+"px";
}

function micsoreazaFont()
{
	theDiv=document.getElementById('enunt_articol');
	marimeFontDefault = marimeFontDefault-1;
	if (marimeFontDefault < marimeFontMinim) {marimeFontDefault = marimeFontMinim;}
	theDiv.style.fontSize=marimeFontDefault+"px";
}

function popup(adresa, latime, inaltime, is_scroll)
{
	if (is_scroll=="scroll")
		completare=", SCROLLBARS=YES"
	else
		completare="";
	var newWin=window.open(adresa+"", "_blank", "WIDTH="+latime+", HEIGHT="+inaltime+completare);
	
	var X=(screen.width-latime)/2;
	var Y=(screen.height-inaltime)/2;
	newWin.moveTo(X,Y)
}

function isMouseOver(theDivX)
{
	var theDiv=document.getElementById(theDivX);
	var isOpera = (navigator.userAgent.indexOf('Opera') != -1);
	var isIE = (!isOpera && navigator.userAgent.indexOf('MSIE') != -1);
	
	if (!e) var e = window.event;
	
	var mouseXPos = 0;
	var mouseYPos = 0;
	
	if (e.pageX || e.pageY)
	{
		mouseXPos = e.pageX;
		mouseYPos = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		mouseXPos = e.clientX;
		mouseYPos = e.clientY;
		if (isIE)
		{
			mouseXPos += document.documentElement.scrollLeft;
			mouseYPos += document.documentElement.scrollTop;
		}
	}
	
	var theDivLeftPos=parseFloat(theDiv.offsetLeft);
	var theDivRightPos=theDivLeftPos+parseFloat(theDiv.style.width);
	var theDivTopPos=parseFloat(theDiv.offsetTop);
	var theDivBottomPos=theDivTopPos+parseFloat(theDiv.style.height);
	alert(mouseXPos+"/"+mouseYPos+" intre ("+theDivLeftPos+":"+theDivRightPos+") si ("+theDivTopPos+":"+theDivBottomPos+")?");
	if (theDivLeftPos<mouseXPos && mouseXPos<theDivRightPos && theDivTopPos<mouseYPos && mouseYPos<theDivBottomPos)
	{
		//alert("da");
		return true;
	}
	else
	{
		//alert("nu");
		return false;
	}
}

function hideRMenu()
{
	theDivID='nimic';
	theDiv1=document.getElementById(theDivID);
	if(theDiv1)
	{
		if(theDiv1.style.display=="block")
		{
			if(!isMouseOver(theDivID))
			{
				//alert("Mouse over");
				//alert("Mouse: "+event.clientX+"/"+event.clientY+"\n"+"coords: "+theDiv1.offsetLeft+"-"+(theDiv1.offsetLeft+parseFloat(theDiv1.style.width))+"-"+theDiv1.offsetTop+"-"+(theDiv1.offsetTop+parseFloat(theDiv1.style.height)));
				theDiv1.style.display="none";
			}
			else
			{
				//alert("Mouse off");
				//alert("Mouse: "+event.clientX+"/"+event.clientY+"\n"+"coords: "+theDiv1.offsetLeft+"-"+(theDiv1.offsetLeft+parseFloat(theDiv1.style.width))+"-"+theDiv1.offsetTop+"-"+(theDiv1.offsetTop+parseFloat(theDiv1.style.height)));
			}
		}
		return true;
	}
	return false;
}

var gAutoPrint = true;
function printFriendly() 
{ 
	if (document.getElementById!= null) 
	{ 
		var html = '<html>\n<head>\n'; 
		if (document.getElementsByTagName!= null) 
		{ 
			var headTags = document.getElementsByTagName("head"); 
			if (headTags.length > 0) 
				html += headTags[0].innerHTML; 
		} 
		html += '\n</he' + 'ad>\n<body style="margin:5px; font-size:12px;">\n'; 
		var printPageElem = document.getElementById("continutPrintabil"); 
		
		if (printPageElem!= null) 
		{ 
			html += printPageElem.innerHTML; 
		} 
		else 
		{ 
			alert("Nu am gasit sectiunea printabila."); 
			return; 
		} 

		html += '\n</bo' + 'dy>\n</ht' + 'ml>'; 
		
		var printWin = window.open("","continutPrintabil"); 
		printWin.document.open(); 
		printWin.document.write(html); 
		printWin.document.close(); 
		if (gAutoPrint) 
			printWin.print(); 
	} 
	else 
	{ 
		alert("Aceasta functie poate fi utilizata numai\nde browserele cu JavaScript activat."); 
	}
}

function getFileExtension(inputId)
{
	var fileinput=document.getElementById(inputId);
	if(!fileinput) return "";
	var filename=fileinput.value;
	if(filename.length==0) return "";
	var dot=filename.lastIndexOf(".");
	if(dot==-1) return "";
	var extension=filename.substr(dot+1,filename.length);
	return extension;
}

function inchideDivuri(prefix)
{
	var divuri=document.getElementsByTagName('div');
	for(i=0; i<divuri.length; i++)
	{
		if(divuri[i].id.search(prefix)!=-1)
		{
			divuri[i].style.display='none';
		}
	}
}

function toggleSpecial(prefix, id)
{
	if(obj=document.getElementById(prefix+id))
	{
		if (obj.style.display=="none")
		{
			inchideDivuri(prefix);
			obj.style.display="block";
		}
		else
			obj.style.display="none";
	}
}

function ajaxFunction()
{
	var xmlHttp;
	try { xmlHttp=new XMLHttpRequest(); }
	catch (e) {
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) {
			try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (e) { alert("Your browser does not support AJAX!"); return false; }
		}
	}
	return xmlHttp;
}

function turnEditable(obj, val, wid, tabela, prefix, empty)
{
	objNume=obj.id;
	objId=objNume.substring(5, objNume.length);
	objValue=obj.innerHTML;
	theDiv=obj.parentNode;
	theDiv.innerHTML="<input type='text' style='width:"+wid+"px;' value='"+objValue+"'id='field_"+objId+"' class='editInPlace' />";
	newField=document.getElementById('field_'+objId);
	newField.focus();
	newField.onkeypress=function(e)
	{
		var KeyPress ;
		if(e && e.which) { e=e; KeyPress=e.which; }
		else { e=event; KeyPress=e.keyCode; }
		if(KeyPress==13) { newField.blur(); }
	}
	newField.onblur=function()
	{
		if(empty=="nu" && newField.value=="")
		{
			theDiv.innerHTML="<span id='camp_"+objId+"' onclick=\"turnEditable(this,'"+val+"',"+wid+",'"+tabela+"','"+prefix+"','"+empty+"');\" class='editable' title='Click pentru a edita pe loc!'>"+objValue+"</span>";
		}
		else
		{
			if(newField.value!=objValue)
			{
				try { http=ajaxFunction(); } catch(e) {}
				var myurl="ajax/updateEditInPlace.php";
				myRand=parseInt(Math.random()*99999999);						// cache buster
				myurl+="?rand="+myRand;
				myurl+="&var="+escape(val)+"&val="+escape(newField.value)+"&id="+escape(objId)+"&tabela="+escape(tabela)+"&prefix="+escape(prefix);
				http.open("GET", myurl, true);
				http.onreadystatechange=function()
				{
					if (http.readyState==4)
					{
						theDiv.innerHTML="<span id='camp_"+objId+"' onclick=\"turnEditable(this,'"+val+"',"+wid+",'"+tabela+"','"+prefix+"','"+empty+"');\" class='editable' title='Click pentru a edita pe loc!'>"+http.responseText+"</span>";
						$("#msgUpdate").text("Actualizare OK!").show().fadeOut(2000);
					}
				}
				http.send(null);
			}
			else
			{
				theDiv.innerHTML="<span id='camp_"+objId+"' onclick=\"turnEditable(this,'"+val+"',"+wid+",'"+tabela+"','"+prefix+"','"+empty+"');\" class='editable' title='Click pentru a edita pe loc!'>"+objValue+"</span>";
			}
		}
	}
}

function openDialog(loc, subId)
{
	if(loc=="meniu")
	{
		jQuery(function(){ setTimeout(function(){ tb_show('Muta subcategoria',
				'muta-subcategorie.php?keepThis=true&id='+subId+'&TB_iframe=true&height=120&width=300&modal=true', null); }, 200); });
	}
	else if(loc=="articol")
	{
		jQuery(function(){ setTimeout(function(){ tb_show('Schimba subcategoria',
				'schimba-subcategorie.php?keepThis=true&id='+subId+'&TB_iframe=true&height=120&width=300&modal=true', null); }, 200); });
	}
}