function genFields(nr,objID,tip)
{
	if(isNaN(nr))
	{
		alert("Sunt permise doar caractere numerice!");
		if(objID=="cuvinte")
		{
			document.getElementById("inputNrCuv").value="";
		}
		else if(objID=="cuvintecomp")
		{
			document.getElementById("inputNrCuvComp").value="";
		}
		else if(objID=="emails")
		{
			document.getElementById("inputNrEmails").value="";
		}
		return false;
	}
	if (objID == "cuvinte" && (nr < 0 || nr > 20)) 
		alert("Numarul de cuvinte poate fi doar intre 0 si 20!");
	else 
		if (objID == "cuvintecomp" && (nr < 0 || nr > 5)) 
			alert("Numarul de cuvinte compuse poate fi doar intre 0 si 5!");
		else 
			if (objID == "emails" && (nr < 1 || nr > 50)) 
				alert("Numarul de e-mailuri poate fi doar intre 1 si 50!");
			else {
				var obj = document.getElementById(objID);
				var i = 0;
				if(tip=="comp")
					var head = "<input class=\"inputFieldC\" onkeydown=\"return (event.keyCode != 9)\" onkeyup=\"calcularePret()\" type=\"text\" id=\"" + tip;
				else if(tip=="email") 
					var head = "<input class=\"inputFieldC\" onkeydown=\"return (event.keyCode != 9)\" type=\"text\" id=\"" + tip;
				else
					var head = "<input class=\"inputField\" onkeydown=\"return (event.keyCode != 9)\" type=\"text\" id=\"" + tip;
				var middle = "\" name=\"" + tip;	
				var end = "\"> ";
				obj.innerHTML = "<br>";
				for (i = 1; i <= nr; i++) {
					obj.innerHTML += head + i + middle + i+ end;
					if (i % 3 == 0 || tip != "cuv") 
						obj.innerHTML += "<br>";
				}
			}
}

function genOre(nr, objID)
{
	if(isNaN(nr))
	{
		alert("Sunt permise doar caractere numerice!");
		document.getElementById("inputNrOre").value="";
		return false;
	}
	if(nr>4 || nr<1) alert("Numarul de monitorizari zilnice poate fi doar intre 1 si 4!");
	else{
		var obj = document.getElementById(objID);
		var i;
		var head = "<select id=\"selectOre\" name=\"ore";
		var end = "\">";
		for (i = 0; i < 24; i++) {
			end += "<option value=\"" + i + ":00\">" + i + ":00</option>";
			end += "<option value=\""+i+":30\">"+i+":30</option>";
		}
		end += "</select>";
		obj.innerHTML="<br>";
		for (i=1; i<=nr; i++)
		{
			obj.innerHTML += head + i + end;
		}
	}
}

function checkRegisterForm()
{
	if(document.registerForm.email.value=="")
	{
		alert("Va rugam completati campul pentru e-mail!");
		return false;
	}
	if(document.registerForm.user.value=="")
	{
		alert("Va rugam completati campul pentru username!");
		return false;
	}
	if(document.registerForm.pass.value=="")
	{
		alert("Va rugam completati campul pentru parola!");
		return false;
	}
	if(document.registerForm.conditii.checked==false)
	{
		alert('Va rugam bifati "Am luat la cunostinta Conditiile de functionare si utilizare a website-ului Monitorizarestiri.ro si sunt de acord cu acestea"!');
		return false;
	}
	if(document.registerForm.pass_confirm.value=="")
	{
		alert("Va rugam completati campul pentru confirmarea parolei!");
		return false;
	}
	if(document.registerForm.pass_confirm.value!=document.registerForm.pass.value)
	{
		alert("Parola si confirmarea parolei sunt diferite!");
		return false;
	}
	return true;
}
function checkEditForm()
{
	if(document.editForm.email.value=="")
	{
		alert("Va rugam completati campul pentru e-mail!");
		return false;
	}
	if(document.editForm.user.value=="")
	{
		alert("Va rugam completati campul pentru username!");
		return false;
	}
	if(document.editForm.pass_confirm.value!=""||document.editForm.pass.value!=""||document.editForm.oldpass.value!="")
	{
		if(document.editForm.pass_confirm.value=="")
		{
			alert("Va rugam completati si campul pentru confirmarea parolei noi!");
			return false;
		}
		if(document.editForm.pass.value=="")
		{
			alert("Va rugam completati si campul pentru parola noua!");
			return false;
		}
		if(document.editForm.oldpass.value=="")
		{
			alert("Va rugam completati si campul pentru parola veche!");
			return false;
		}
		if(document.editForm.pass_confirm.value!=document.editForm.pass.value)
		{
			alert("Parola noua si Confirmarea parolei noi sunt diferite!");
			return false;
		}
	}
	return true;
}

function checkLoginForm()
{
	if(document.loginForm.user.value=="")
	{
		alert("Va rugam completati campul pentru username!");
		return false;
	}
	if(document.loginForm.pass.value=="")
	{
		alert("Va rugam completati campul pentru parola!");
		return false;
	}
	return true;
}

function checkEditLoginForm()
{
	if(document.loginForm.oldpass.value=="")
	{
		alert("Va rugam completati campul pentru parola veche!");
		return false;
	}
	if(document.loginForm.newpass.value=="")
	{
		alert("Va rugam completati campul pentru parola noua!");
		return false;
	}
	if(document.loginForm.confnewpass.value=="")
	{
		alert("Va rugam completati campul pentru confirmarea parolei noi!");
		return false;
	}
	if(document.loginForm.newpass.value!=document.loginForm.confnewpass.value)
	{
		alert("Valoarea parolei noi si confirmarii parolei noi difera!");
		return false;
	}
	return true;
}

function checkAddURL()
{
	if(document.addURL.link.value=="")
	{
		alert("Va rugam completati campul pentru link!");
		return false;
	}
	if(document.addURL.nume.value=="")
	{
		alert("Va rugam completati campul pentru nume!");
		return false;
	}
	if(document.addURL.linkgen.value=="")
	{
		alert("Va rugam completati campul pentru link-ul general!");
		return false;
	}
	return true;
}

function checkIt(str1,objid)
{
	var el = document.getElementById(objid);
	var n1 = str1.split(" ").length;
	var arr = el.value.split(" ");
	var n2 = arr.length;
	var finaly = "";
	if (n2>n1) 
		{
			for(i=0;i<n1;i++)
				finaly += arr[i] + ' ';
			el.value = finaly;
		}
}

function checkNewRaportForm()
{
	if(document.newRaport.nume.value=="")
	{
		alert("Va rugam completati campul pentru numele raportului!");
		return false;
	}
	if(document.newRaport.nrcuv.value=="" || document.newRaport.nrcuv.value<0 || document.newRaport.nrcuv.value>20)
	{
		alert("Va rugam completati campul pentru numarul de cuvinte cu o valoare intre 0 si 20!");
		return false;
	}
	if(document.newRaport.nrcuvcomp.value==""||document.newRaport.nrcuvcomp.value<0||document.newRaport.nrcuvcomp.value>5)
	{
		alert("Va rugam completati campul pentru numarul de structuri de cuvinte compuse cu o valoare intre 0 si 5!");
		return false;
	}
	if(document.newRaport.nremails.value==""||document.newRaport.nremails.value<=0||document.newRaport.nremails.value>50)
	{
		alert("Va rugam completati campul pentru numarul de e-mailuri cu o valoare intre 1 si 50!");
		return false;
	}
	if(document.newRaport.nrore.value==""||document.newRaport.nrore.value<=0||document.newRaport.nrore.value>4)
	{
		alert("Va rugam completati campul pentru numarul de ore cu o valoare intre 0 si 4!");
		return false;
	}
	if(document.newRaport.nrcuv.value==0&&document.newRaport.nrcuvcomp.value==0)
	{
		alert("Numarul de cuvinte si numarul de structuri compuse nu pot fi simultan 0!");
		return false;
	}
        if (document.newRaport.nrcuv.value!="")
            {
                var cuvs = document.newRaport.nrcuv.value;
                for (var icuv=1; icuv<=cuvs; icuv++)
                    {
                        var cuvID=document.getElementById("cuv" + icuv);
                        if(cuvID.value=="")
                            {
                                alert("Va rugam completati campul " + icuv + " pentru cuvinte simple!");
                                return false;
                            }
                    }
            }
            if (document.newRaport.nrcuvcomp.value!="")
            {
                var cuvs = document.newRaport.nrcuvcomp.value;
                for (var icuv=1; icuv<=cuvs; icuv++)
                    {
                        var cuvID=document.getElementById("comp" + icuv);
                        if(cuvID.value=="")
                            {
                                alert("Va rugam completati campul " + icuv + " pentru cuvinte compuse!");
                                return false;
                            }
                    }
            }
             if (document.newRaport.nremails.value!="")
            {
                var cuvs = document.newRaport.nremails.value;
                for (var icuv=1; icuv<=cuvs; icuv++)
                    {
                        var cuvID=document.getElementById("email" + icuv);
                        if(cuvID.value=="")
                            {
                                alert("Va rugam completati campul " + icuv + " pentru email-uri!");
                                return false;
                            }
                    }
            }
	if(document.newRaport.conditii.checked==false)
	{
		alert('Va rugam bifati "Am luat la cunostinta Conditiile de functionare si utilizare a website-ului Monitorizarestiri.ro si sunt de acord cu acestea"!');
		return false;
	}
	return true;
}

function checkContactForm()
{
	if(document.contactForm.nume.value=="")
	{
		alert("Va rugam completati campul pentru nume!");
		return false;
	}
	if(document.contactForm.email.value=="")
	{
		alert("Va rugam completati campul pentru e-mail!");
		return false;
	}
	if(document.contactForm.subiect.value=="")
	{
		alert("Va rugam completati campul pentru subiect!");
		return false;
	}
	if(document.contactForm.mesaj.value=="")
	{
		alert("Va rugam completati campul pentru mesaj!");
		return false;
	}
	return true;
}

function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function positiontip(e){
		if (enabletip){
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20
		
		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000
		
		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<tipobj.offsetWidth)
		//move the horizontal position of the menu to the left by it's width
		tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
		else if (curX<leftedge)
		tipobj.style.left="5px"
		else
		//position the horizontal position of the menu where the mouse is positioned
		tipobj.style.left=curX+offsetxpoint+"px"
		
		//same concept with the vertical position
		if (bottomedge<tipobj.offsetHeight)
		tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
		else
		tipobj.style.top=curY+offsetypoint+"px"
		tipobj.style.visibility="visible"
		}
}

		function ietruebody(){
		return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
		}
		
		function ddrivetip(thetext, thecolor, thewidth){
			tipobj = document.getElementById("infobox");
		if (ns6||ie){
		if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
		if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
		tipobj.innerHTML=thetext
		enabletip=true
		return false
		}
		}
		
		
		
		function hideddrivetip(){
			tipobj = document.getElementById("infobox");
		if (ns6||ie){
		enabletip=false
		tipobj.style.visibility="hidden"
		tipobj.style.left="-1000px"
		tipobj.style.backgroundColor=''
		tipobj.style.width=''
		}
		}

function checkIfInRange(range, val)
{
	var n = range.length;
	//alert(range + " " + val);
	if(range.charAt(n-1)=='+') 
		{
			if(val >= parseInt(range.substr(0,n-1))) return true;
			else return false;
		}
	else
		{
			var i = range.indexOf('-');
			var from = parseInt(range.substr(0,i));
			var to = parseInt(range.substr(i+1));
			if(from <= val && val <= to) return true;
			else return false;
		}
}
	
function calcularePret()
{
	if((document.newRaport.nrcuv.value!=0&&document.newRaport.nrcuv.value!=""||
	   document.newRaport.nrcuvcomp.value!=0&&document.newRaport.nrcuvcomp.value!="")&&
	   document.newRaport.nremails.value!=0&&document.newRaport.nremails.value!=""&&
	   document.newRaport.nrore.value!=0&&document.newRaport.nrore.value!="")
	   {
	   	var nrcuv = 0;
		if(document.newRaport.nrcuv.value!=0&&document.newRaport.nrcuv.value!="") nrcuv += parseInt(document.newRaport.nrcuv.value);
		if (document.newRaport.nrcuvcomp.value != 0 && document.newRaport.nrcuvcomp.value != "") {
			for (k = 1; k <= document.newRaport.nrcuvcomp.value; k++) 
				nrcuv += document.getElementById("comp" + k).value.split(" ").length;
		}
		var nremails = document.newRaport.nremails.value;
		var nrore = document.newRaport.nrore.value;
		var capC = "capTabelc" + nrore;
		for(i=1; i<=4; i++)
		{
			var el = document.getElementById(capC + i);
			var val = el.innerHTML;
			if(checkIfInRange(val,nremails))
			{
				break;
			}
		}
		var idxe = i;
		var capT = "capTabel" + nrore;
		for(i=1; i<=4; i++)
		{
			var el = document.getElementById(capT + i);
			var val = el.innerHTML;
			if(checkIfInRange(val,nrcuv))
			{
				break;
			}
		}
		document.getElementById("pretulCorect").innerHTML = "Pret: " + document.getElementById("val"+nrore+i+idxe).innerHTML;
	   }
}

function resetColor(id){
 if(document.getElementsByTagName){  
   var table = document.getElementById(id);  
   var rows = table.getElementsByTagName("tr");  
   for(i = 1; i < rows.length; i++){          
 //manipulate rows
     if(i % 2 == 0){
       rows[i].id = "altrow";
     }else{
       rows[i].id = "";
     }      
   }
 }
}
