// JavaScript Document
var tipobj;


$(document).ready(function() { 
   rearange();
   });

$(window).bind('resize', function() {
	rearange();
});

/////////////////////////////////////////////////////////////////
function rearange(){
	if (getElem('cat_list')) rearange_index();
	else if (getElem('listing')) rearange_listing();

	$("#tablist a").bind("click", function(){
			setTab($(this).get(0).id.substr(1));
	return false;
	});

	var body_width = $("body").width();
	if (body_width<1050){
//		$("#main_menu li a").css("font-size", "12px");
		$("#main_menu li a").css("margin-right", "7px");
	}else {
//		$("#main_menu li a").css("font-size", "13px");
		$("#main_menu li a").css("margin-right", "12px");
	}

//	if (getElem('cat_list')) rearange_index();
}

/////////////////////////////////////////////////////////////////
function setTab(tab_id){
$('#tablist a').removeClass('tab_on');
$('#t'+tab_id).addClass('tab_on');
$('#tabContent div').hide();
$('#c'+tab_id).show();
}

/*
/////////////////////////////////////////////////////////////////
function rearange_listing(){
	var container_width = $('#listing').width();
	var nr_liuri = Math.floor(container_width/200);
	var li_width = (container_width-nr_liuri)/nr_liuri +'px';
	var maxH = 0;
	$('#listing li').css({width: li_width});
	$("#listing li").css({borderRight:"1px solid #c9e1f4"});
	for(i=1, n=$('#listing li').length; i<=n; i++)
	{	currentH = $("#listing li:eq(" + (i-1) + ")").height();
		if(i%nr_liuri == 0)	$("#listing li:eq(" + (i-1) + ")").css({borderRight:"0"});
	}
}
*/

/////////////////////////////////////////////////////////////////
function rearange_listing(){
	if (!tipobj) tipobj = getElem("dhtmltooltip");	
	
	$("#listing li a.produs").css({height:"auto"});
	$("#listing li p").css({height:"auto"});

//	var container_width = $('#listing').width();
	var container_width = $("body").width()-232;
	var nr_liuri = Math.floor(container_width/200);
	var li_width = Math.round((container_width-nr_liuri-1)/nr_liuri) +'px';
	var maxProdus = 0; var maxP = 0;
//	console.log(container_width+" "+nr_liuri+" "+li_width+" : "+$("body").width());
	$('#listing li').css({width: li_width});
	$("#listing li").css({borderRight:"1px solid #c9e1f4"});
	for(i=1, n=$('#listing li').length; i<=n; i++)
	{	curentProdus = $("#listing li:eq(" + (i-1) + ") a.produs").height();
		currentP = $("#listing li:eq(" + (i-1) + ") p").height();
//		console.log(curentProdus);
		if (maxProdus<curentProdus) maxProdus = curentProdus;
		if (maxP<currentP) maxP = currentP;
		if(i%nr_liuri == 0){
			$("#listing li:eq(" + (i-1) + ")").css({borderRight:"0"});
			for (j=i-nr_liuri; j<i; j++){
				$("#listing li:eq(" + (j) + ") a.produs").css({height:maxProdus});
//				console.log('maxProdus: '+maxProdus);
				$("#listing li:eq(" + (j) + ") p").css({height:maxP});
			}
			maxProdus = 0; maxP = 0;
		}				
	}
	
	if (i%nr_liuri > 0){
		for (j=Math.floor((i-1)/nr_liuri)*nr_liuri; j<i-1; j++){
			$("#listing li:eq(" + (j) + ") a.produs").css({height:maxProdus});
			$("#listing li:eq(" + (j) + ") p").css({height:maxP});
		}
	}
	
}


/////////////////////////////////////////////////////////////////
function rearange_index(){
	var container_width = $('#cat_list').width();
	var nr_liuri = Math.floor(container_width/190);
//	console.log(container_width+":"+nr_liuri);
	var li_width = Math.round((container_width-nr_liuri)/nr_liuri-21) +'px';
//	console.log(li_width);
	var nr_liuri_cat = Math.floor(container_width/220);
//	console.log(container_width+":"+nr_liuri_cat);
	var li_width_cat = Math.round((container_width-nr_liuri_cat)/nr_liuri_cat) +'px';
//	console.log(li_width_cat);

	$('#cat_list li').css({width: li_width_cat});
	$('#content .lista_produse li').css({width: li_width});
	fit_produse_index("prod_rec", nr_liuri);
	fit_produse_index("prod_last", nr_liuri);
}

/////////////////////////////////////////////////////////////////
function fit_produse_index(id, nr_liuri){
	$("#"+id+" li ").css({borderRight:"1px solid #c9e1f4"});
	var maxP = 0; var maxS = 0;
	for(i=1, n=$('#'+id+' li').length; i<=n; i++){	
		currentP = $("#"+id+" li:eq(" + (i-1) + ") .logo_lista").height() + $("#"+id+" li:eq(" + (i-1) + ") .recomandat").height() + $("#"+id+" li:eq(" + (i-1) + ") h3").height() + 116;
		currentPret = $("#"+id+" li:eq(" + (i-1) + ") .preturi").height();
		if (currentP+currentPret>maxP) maxP = currentP+currentPret;
		if (currentP>maxS) maxS = currentP;
		if(i%nr_liuri == 0 || i==n){
			$("#"+id+" li:eq(" + (i-1) + ")").css({borderRight:"0"});
			if (i==n && i%nr_liuri > 0) start = Math.floor(n/nr_liuri) * nr_liuri;
			else start = i-nr_liuri;
			for (j=start; j<i; j++){ $("#"+id+" li:eq(" + (j) + ")").css({height:maxP+40}); $("#"+id+" li:eq(" + (j) + ") .prod_container").css({height:maxS+10});}
//			console.log(start+" / "+i+" / "+nr_liuri+" / "+n);
			maxP = 0; maxS = 0;
		}
	}

}

/*
function initial(){
	if (!tipobj) tipobj = getElem("dhtmltooltip");

	if (document.images)
	{
		preload_image = new Image(25,25); 
		preload_image.src = root+"img/menu_r.gif"; 
	}

}
*/

/////////////////////////////////////////////////////////////////
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;
	}
	
	return true;
}

/////////////////////////////////////////////////////////////////
function validate_nota(){
	if (frm.mesaj.value == ""){
		alert('Introduceti nota !');
		frm.mesaj.focus();
		return false;
	}
	
	return true;
}

/////////////////////////////////////////////////////////////////
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.submit();
}

/////////////////////////////////////////////////////////////////
function validate_comentariu(frm){
	if (frm.nume.value == ""){
		alert('Introduceti numele!');
		frm.nume.focus();
		return false;
	}else if (frm.comentariu.value == ""){
		alert('Introduceti comentariul!');
		frm.comentariu.select();
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////
function checkEmail(email){
	var regex = /^[\w\.-]+@[\w\.-]+\.[a-zA-Z]{2,4}$/ 
	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 caracteristica!");
			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.nume.value==""){
		alert("Introduceti numele dumneavoastra.");
		frm.nume.focus();
		return false;
	}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 validate_comanda_rapida(frm){
	var tip = "";
	if (frm.tip[0].checked) tip="PF";
	else if (frm.tip[1].checked) tip="PJ";
	
	if (frm.nume_comanda.value == ""){
		alert('Introduceti numele !');
		frm.nume_comanda.focus();
		return false;
	}else if (frm.prenume.value == ""){
		alert('Introduceti prenumele !');
		frm.prenume.focus();
		return false;
	}else 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.telefon.value == ""){
		alert('Introduceti telefonul!');
		frm.telefon.focus();
		return false;
	}else if (tip==""){
		alert('Alegeti modul de facturare');
		return false;
	}else if (tip=="PF" && frm.cnp.value == ""){
		alert('Introduceti CNP-ul!');
		frm.cnp.focus();
		return false;
	}else if (tip=="PJ" && frm.firma.value == ""){
		alert('Introduceti firma!');
		frm.firma.focus();
		return false;
	}else if (tip=="PJ" && frm.cod_fiscal.value == ""){
		alert('Introduceti codul fiscal!');
		frm.cod_fiscal.focus();
		return false;
	}else if (tip=="PJ" && frm.reg_com.value == ""){
		alert('Introduceti numarul de inregistrare la registrul comertului!');
		frm.reg_com.focus();
		return false;
	}else if (tip=="PJ" && frm.iban.value == ""){
		alert('Introduceti codul IBAN!');
		frm.iban.focus();
		return false;
	}else if (frm.adresa.value == ""){
		alert('Introduceti adresa!');
		frm.adresa.focus();
		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 pop_harta(){
	window.open('pop_up_image.php?harta=1','pop_img','scrollbars=no,height=460,width=490,resizable=yes');
}

/////////////////////////////////////////////////////////////////
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 = "";
		}
	}
//	getElem('cumpara_acum').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){
	root = $("base").attr("href");
	window.open(root+'pop_up_image.php?IDProdus='+IDProdus+'&IDImagine='+IDImagine,'pop_img','scrollbars=no,height=250,width=350,resizable=yes');
}

///////////////////////////////////////////////////////////////
function open_mostre(IDProdus, IDMostra){
	root = $("base").attr("href");
	window.open(root+'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" && thewidth!="") 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 validate_cautare(frm){
	if (frm.key.value.length < 2){ 
		alert ("Introduceti un cuvant de cel putin 2 caractere!");
		frm.key.select();
		return false;
	}
	frm.submit();
}

/////////////////////////////////////////////////////////////
function startList() {
	if (!getElem("filtru")) return;
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("filtru");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
					this.childNodes[2].style.display = "block";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
					this.childNodes[2].style.display = "none";
				}
			}
		}
	}
}

/////////////////////////////////////////////////////////////
function select_panel(nr){
	max_tab = 10;
	var n = 0;
	for (i=1; i<=max_tab; i++){
		if (obj = getElem('tool_'+i)){
			n = i;
			obj.className = "";
			$('#panel_'+i).hide();
		}
	}
	
	if (nr<=n){
		getElem('tool_'+nr).className = "selected";
		$('#spate,#panel_'+nr).show();
		$('#spate').height($("body").height());
	}
	//spate
}

/////////////////////////////////////////////////////////////
function reset_form(frm){
	n = frm.elements.length;
	for (i=0; i<n; i++){
		if (frm.elements[i].type == "text" || frm.elements[i].type == "textarea" || (frm.elements[i].type=="hidden" && frm.elements[i].name!='page')) frm.elements[i].value = "";
		else if (frm.elements[i].type=="select-one"){
			obj = frm.elements[i];
			obj.value = obj.options[0].value;
		}else if (frm.elements[i].type=="radio"){
			obj = frm.elements[i];
			obj.checked = false;
		}
	}
}

/////////////////////////////////////////////////////////////
function select_all(frm, ch_state){
	var n = frm.elements.length;
	for (i=0; i<n; i++){
		if (frm.elements[i].type == 'checkbox'){
		frm.elements[i].checked = ch_state;
		}
	}
}

/////////////////////////////////////////////////////////////
function set_ordonare(val){
	if (val=="asc") $.cookie('pret_ordonare', null, {path: '/'});
	else if (val=="desc") $.cookie('pret_ordonare', "desc", {path: '/'});
	location.href = location.href;
}

/////////////////////////////////////////////////////////////
function set_afisare(grupa, val){
	if (val=="tot") $.cookie('prod_afisare', null, {path: '/'});
	else $.cookie('prod_afisare', grupa+"_"+val, {path: '/'});
	location.href = location.href;
}

/////////////////////////////////////////////////////////////
function set_filter(obj){
	if (obj.checked)	location.href = $(obj).parent().children("a").attr("href");
	else location.href = $(obj).parent().parent().children("li.sterge").children("a").attr("href");
	
}

/***********************************************
* 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" && thewidth!="") 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


//window.onload=initial;