// 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); });
	}
}

function checkExtensie(fisier, arrayExtensii)
{
	var flagGasit=false;
	var temp=fisier.split(".");
	var extensie=temp[temp.length-1].toLowerCase();
	if(typeof(arrayExtensii)=="object") // array
	{
		for(var i=0; i<arrayExtensii.length; i++)
		{
			if(extensie==arrayExtensii[i]) { flagGasit=true; break; }
		}
	}
	else if(typeof(arrayExtensii)=="string")
	{
		if(extensie==arrayExtensii) flagGasit=true;
	}
	return flagGasit;
}

function VerificareCNP(s){
	var suma=0;
	var mesaj="";
	if(s.length==13)	{
		suma=parseInt(s.charAt(0))*2+parseInt(s.charAt(1))*7+parseInt(s.charAt(2))*9+
		parseInt(s.charAt(3))*1+parseInt(s.charAt(4))*4+parseInt(s.charAt(5))*6+
		parseInt(s.charAt(6))*3+parseInt(s.charAt(7))*5+parseInt(s.charAt(8))*8+parseInt(s.charAt(9))*
		2+parseInt(s.charAt(10))*7+parseInt(s.charAt(11))*9;
		suma=suma%11;
		if(suma==10)
			suma=1;
		if(suma==parseInt(s.charAt(12)))
			return true;
		else{
			alert("CNP invalid !!!");
			return false;
			}
		}
	else{
		alert("CNP de lungime necorespunzatoare (<>13) !!! ");
		return false;
	}
}


//javascript echivalent function for php urlencode, si cu UTF-8
function urlencode( str ) {
    var hash_map = {}, unicodeStr='', hexEscStr='';
    var ret = (str+'').toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urldecode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
 
    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr];
        ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
}

function NumberFormat(numar, nr_zecimale){
	//determina punctul zecimal;
//	alert(nr_zecimale);
	var putere=1;
	for(i=0;i<nr_zecimale; i++) putere*=10;
//	alert(putere);
	return Math.round(numar*putere)/putere;
}
