// JavaScript Document
ron = 10000;
var tipobj;

function initial(){
	if (!tipobj) tipobj = getElem("dhtmltooltip");
}

/////////////////////////////////////////////////////////////////
function getElem(elemID){
	var obj;
	if (document.all){
		obj = document.all(elemID)
	}else if (document.getElementById){
		obj = document.getElementById(elemID)
	}else if (document.layers){
		obj = document.layers[elemID]
	}
	return obj;
}

/////////////////////////////////////////////////////////////////
function validate_login_main(frm){
	if (frm.email.value == ""){
		alert('Introduceti adresa de email !');
		frm.email.focus();
		return false;
	}else if (!checkEmail(frm.email.value)){
		alert('Adresa de email este invalida!');
		frm.email.select();
		return false;
	}else if (frm.parola.value == ""){
		alert('Introduceti parola!');
		frm.parola.focus();
		return false;
	}
	
	frm.action = "login.php";
}

/////////////////////////////////////////////////////////////////
function validate_news(frm){
	if (frm.email_news.value == ""){
		alert('Introduceti adresa de email !');
		frm.email_news.focus();
		return false;
	}else if (!checkEmail(frm.email_news.value)){
		alert('Adresa de email este invalida!');
		frm.email_news.select();
		return false;
	}
	frm.action = "newsletter.php";
}

/////////////////////////////////////////////////////////////////
function checkEmail(email){
	var regex = /^[\w\.-]+@[\w\.-]+\.[a-zA-Z]{2,3}$/ 
	if (!regex.test(email)) return false; 
	return true; 
} 	

/////////////////////////////////////////////////////////////////
function validare_parere(frm,n){
	for (i=1; i<=n; i++){
		id_criteriu = getElem("id_"+i).value;
		suma = 0;
		for (j=0; j<5; j++) suma += frm.elements[id_criteriu][j].checked;
		if (suma==0){
			alert("Selectati o nota pentru fiecare criteriu!");
			return false;
		}
	}
	
	if (frm.nume.value == ""){
		alert('Introduceti numele !');
		frm.nume.focus();
		return false;
	}else if (frm.email.value != "" && !checkEmail(frm.email.value)){
		alert('Adresa de email este invalida !');
		frm.email.select();
		return false;
	}else if (frm.parere.value == ""){
		alert('Scrieti parerea dumneavoastra !');
		frm.parere.select();
		return false;
	}
	
	return true;
}

/////////////////////////////////////////////////////////////////
function resize(){
	width = getElem('poza').width;
	getElem('td_poza').width = width;
}

/////////////////////////////////////////////////////////////////
function validare_recomanda(frm){
	if (frm.email_vizitator.value==""){
		alert("Introduceti adresa dumneavoastra de email.");
		frm.email_vizitator.focus();
		return false;
	}else if (!checkEmail(frm.email_vizitator.value)){
		alert('Adresa de email este invalida!');
		frm.email_vizitator.select();
		return false;
	}else if (frm.email_prieten.value==""){
		alert("Introduceti adresa de email.");
		frm.email_prieten.focus();
		return false;
	}else if (!checkEmail(frm.email_prieten.value)){
		alert('Adresa de email este invalida!');
		frm.email_prieten.select();
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////

	function calcul_rate2(){
		frm = document.rate2;
		
		//CREDIT RON
		credit_ron = checkPrice(frm.suma_ron.value - frm.avans_ron.value) * 1.099;
		factor_ron = 1;
		for (i=0; i<frm.rate_ron.value; i++) factor_ron *= 1 + 21/1200;

		val_rata_ron = credit_ron * factor_ron * 21/1200/(factor_ron-1);
		frm.valoare_ron.value = Math.round(val_rata_ron*100)/100;
//		frm.dae_ron.value = Math.round(dae(checkPrice(frm.suma_ron.value - frm.avans_ron.value), frm.valoare_ron.value, frm.rate_ron.value)*10)/10;
		frm.dae_ron.value = frm.rate_ron.options[frm.rate_ron.selectedIndex].title;
		creditare();
	}
/////////////////////////////////////////////////////////////////

	function checkPrice(price){
		var regex = /^[\d]+(\.[\d]{1,2})?$/ 
		if (!regex.test(price)) return 0; 
		return price; 
	} 	
/////////////////////////////////////////////////////////////////
	
	function dae(C, rata_lunara, n){
		rmin=0; rmax=128;
		for (j=0; j<15; j++){
			r = (rmax+rmin)/2;
			s = 0;
			for (i=1; i<=n; i++){
				s = s + rata_lunara / Math.pow(1+r/100, i/12);
			}
			if (C-s < 0) rmin = r;
			else rmax = r;
		}
		return r;
	}

/////////////////////////////////////////////////////////////////

function creditare(){
	frm = document.rate2;
	venit = -frm.venit.value - frm.venit_sot.value;
	rate_consum = -frm.credit_consum.value - frm.credit_consum_sot.value;
	rate_imob = -frm.credit_imob.value - frm.credit_imob_sot.value;
	rata_lunara = -frm.valoare_ron.value;
	

	getElem('creditare_40').style.display="none";
	getElem('creditare_30').style.display="none";
	getElem('creditare_1').style.display="none";

	if ((rate_consum + rate_imob + rata_lunara) * (-1) > -0.4 * venit) getElem('creditare_40').style.display="";
	else if ((rate_consum + rata_lunara) * (-1) > -0.3 * venit) getElem('creditare_30').style.display="";
	else getElem('creditare_1').style.display="";;
}	

if (document.rate2 && checkPrice(document.rate2.suma_ron.value)>0){
	calcul_rate2();
}
/////////////////////////////////////////////////////////////////

function pop_harta(){
	window.open('harta.htm', 'popup', 'width=517, height=350, resizable=no, scrollbars=no, menubar=no');
}

/////////////////////////////////////////////////////////////////
function show_pagina(nr){
	var n = 6;
	for (i=1; i<=n; i++){
		if (i==nr){
			if (getElem('pagina_'+i)) showByID('pagina_'+i);
			if (getElem('tag_'+i)) getElem('tag_'+i).className = "del_ales";
		}else{
			if (getElem('pagina_'+i)) hideByID('pagina_'+i);
			if (getElem('tag_'+i)) getElem('tag_'+i).className = "";
		}
	}
	
	obj = getElem('cumpara_acum');
	if (nr==2){
		obj.onclick = function(){
			frm = document.accesorii;
			if (validare_accesorii(frm)) frm.submit();
			return false;
		}
	}else{
		obj.onclick = function(){return true;}
	}
	obj.focus();
}

///////////////////////////////////////////////////////////////
function hideByID(ID){
	if (getElem(ID)){getElem(ID).style.display = "none";}
	else if (ID){
		d = ID.split(",");
		for (i=0,n=d.length; i<n; i++){
			if (getElem(d[i])){getElem(d[i]).style.display = "none";}
		}
	}
}

///////////////////////////////////////////////////////////////
function showByID(ID){
	if (getElem(ID)){getElem(ID).style.display = "";}
	else if (ID){
		d = ID.split(",");
		for (i=0,n=d.length; i<n; i++){
			if (getElem(d[i])){getElem(d[i]).style.display = "";}
		}
	}
}

///////////////////////////////////////////////////////////////
function open_imagini(IDProdus, IDImagine){
	window.open('pop_up_image.php?IDProdus='+IDProdus+'&IDImagine='+IDImagine,'pop_img','scrollbars=no,height=250,width=350,resizable=yes');
}

///////////////////////////////////////////////////////////////
function open_mostre(IDProdus, IDMostra){
	window.open('pop_up_image.php?IDProdus='+IDProdus+'&IDMostra='+IDMostra,'pop_img','scrollbars=no,height=250,width=350,resizable=yes');
}

/***********************************************
* Cool DHTML tooltip script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var offsetxpoint=0; //Customize x offset of tooltip
var offsetypoint=20; //Customize y offset of tooltip
var ie=document.all;
var ns6=document.getElementById && !document.all;
var enabletip=false;

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

/////////////////////////////////////////////////////////////
function ddrivetip(thetext, thecolor, thewidth){
	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 positiontip(e){
	if (enabletip){
		var curX=(ns6)?e.pageX : event.x+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.y+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 hideddrivetip(){
	if (ns6||ie){
		enabletip=false;
		tipobj.style.visibility="hidden";
		tipobj.style.left="-1000px";
//		tipobj.style.backgroundColor='';
		tipobj.style.width='';
		}
}

document.onmousemove=positiontip


/////////////////////////////////////////////////////////////
function open_compara(IDProdus){
	window.open('compara.php?IDProdus='+IDProdus,'compara','scrollbars=yes,height=600,width=800,resizable=yes');
}

/////////////////////////////////////////////////////////////
function validare_accesorii(frm){
	var n = frm.elements.length;
	var counter = 0;
	for (i=0; i<n; i++){
		obj = frm.elements[i];
		if (obj.type == "checkbox" && obj.checked) counter++;
		if (obj.type == "text" && !checkPrice(obj.value)){
			alert("Cantitatea trebuie sa fie o valoare numerica !");
			obj.select();
			return false;
		}
	}
	
	if (!counter){
		alert("Selectati cel putin un produs prin bifarea produselor dorite !");
		return false;
	}
	return true;
}