
/* Tooltip */

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  obj.style.filter = "alpha(opacity:"+opacity+")";   // IE/Win
  obj.style.KHTMLOpacity = opacity/100;  // Safari<1.2, Konqueror
  obj.style.MozOpacity = opacity/100;  // Older Mozilla and Firefox
  obj.style.opacity = opacity/100;  // Safari 1.2, newer Firefox and Mozilla, CSS3
}

function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 90) {
      setOpacity(obj, opacity);
      opacity += 30;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}

tooltip = {
	name : "qTip",
	offsetX : -30,
	offsetY : 26,
	tip : null
}

tooltip.init = function () {
	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";
	if(!tipContainerID){ var tipContainerID = "qTip";}
	var tipContainer = document.getElementById(tipContainerID);

	if(!tipContainer) {
	  tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		tipContainer.setAttribute("id", tipContainerID);
	  document.getElementsByTagName("body").item(0).appendChild(tipContainer);
	}

	if (!document.getElementById) return;
	this.tip = document.getElementById (this.name);
	if (this.tip) document.onmousemove = function (evt) {tooltip.move (evt)};

	var a, sTitle;
	var anchors = document.getElementsByTagName ("a");

	var anch = document.getElementsByTagName ("img");

	for (var i = 0; i < anchors.length; i ++) {
		a = anchors[i];
		sTitle = a.getAttribute("title");
		if(sTitle) {
			a.setAttribute("tiptitle", sTitle);
			a.removeAttribute("title");
			a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
			a.onmouseout = function() {tooltip.hide()};
		}
	}

	for (var i = 0; i < anch.length; i ++) {
		a = anch[i];
		sTitle = a.getAttribute("alt");
		if(sTitle) {
			a.setAttribute("tipalt", sTitle);
			a.removeAttribute("alt");
			a.onmouseover = function() {tooltip.show(this.getAttribute('tipalt'))};
			a.onmouseout = function() {tooltip.hide()};
		}

	}
}

tooltip.move = function (evt) {
	var x=0, y=0;
	if (document.all) {// IE
		x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		x += window.event.clientX;
		y += window.event.clientY;
		
	} else {// Mozilla
		x = evt.pageX;
		y = evt.pageY;
	}
	this.tip.style.left = (x + this.offsetX + 40) + "px";
	this.tip.style.top = (y + this.offsetY - 15) + "px";
}

tooltip.show = function (text) {
	if (!this.tip) return;
	this.tip.innerHTML = text;
	this.tip.style.display = "block";
	if(!tipContainerID){ var tipContainerID = "qTip";}
    fadeIn(tipContainerID,0);
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
}

window.onload = function () {
	tooltip.init ();
}

/* Toggle function */
function toggle(id) {
	var el = document.getElementById(id);
	if ( el.style.display != 'none' ) {
		Slide(id).up();
	} else {
		Slide(id).down();
	}
}

/* Homepage tabs functions */
function tabs_albume(id) {
	var ultimele = document.getElementById('albume_ultimele');
	var votate = document.getElementById('albume_votate');

	var active = 'undefined';

	if ( ultimele.style.display != 'none' ) {
		active = 'albume_ultimele';
	} else if ( votate.style.display != 'none' ) {
		active = 'albume_votate';
	}

	if ( id != active ) {
		ultimele.style.display = 'none';
		votate.style.display = 'none';

		toggle(id);
	}
}

function tabs_categorii(id) {
	var firme = document.getElementById('categorii_firme');
	var produse = document.getElementById('categorii_produse');

	var active = 'undefined';

	if ( firme.style.display != 'none' ) {
		active = 'categorii_firme';
	} else if ( firme.style.display != 'none' ) {
		active = 'categorii_produse';
	}

	if ( id != active ) {
		firme.style.display = 'none';
		produse.style.display = 'none';

		toggle(id);
	}
}

function tabs_regiuni(id) {
	var firme = document.getElementById('regiuni_firme');
	var produse = document.getElementById('regiuni_produse');

	var active = 'undefined';

	if ( firme.style.display != 'none' ) {
		active = 'regiuni_firme';
	} else if ( firme.style.display != 'none' ) {
		active = 'regiuni_produse';
	}

	if ( id != active ) {
		firme.style.display = 'none';
		produse.style.display = 'none';

		toggle(id);
	}
}

/* Toggle interactive section elements */
function interactive(id) {

	var holder = document.getElementById('interactive_holder');

	var cautare = document.getElementById('interactive_cautare');
	var intra = document.getElementById('interactive_intra');
	var creare = document.getElementById('interactive_creare');
	var contul = document.getElementById('interactive_contul');

	var active = 'undefined';

	if ( cautare.style.display != 'none' ) {
		active = 'interactive_cautare';
	} else if ( intra.style.display != 'none' ) {
		active = 'interactive_intra';
	} else if ( creare.style.display != 'none' ) {
		active = 'interactive_creare';
	} else if ( contul.style.display != 'none' ) {
		active = 'interactive_contul';
	}

	if ( id == active || ( active =='undefined' && holder.style.display == 'none' ) ) {
		toggle('interactive_holder');
		toggle(id);
	} else {
		cautare.style.display = 'none';
		intra.style.display = 'none';
		creare.style.display = 'none';
		contul.style.display = 'none';

		toggle(id);
	}
}

/* Slide function */
var slideInUse = new Array();
function Slide(objId, options) {
	this.obj = document.getElementById(objId);
	this.duration = 1;
	this.height = parseInt(this.obj.style.height);

	if(typeof options != 'undefined') { this.options = options; } else { this.options = {}; }
	if(this.options.duration) { this.duration = this.options.duration; }
		
	this.up = function() {
		this.curHeight = this.height;
		this.newHeight = '1';
		if(slideInUse[objId] != true) {
			var finishTime = this.slide();
			window.setTimeout("Slide('"+objId+"').finishup("+this.height+");",finishTime);
		}
	}
	
	this.down = function() {
		this.newHeight = this.height;
		this.curHeight = '1';
		if(slideInUse[objId] != true) {
			this.obj.style.height = '1px';
			this.obj.style.display = 'block';
			this.slide();
		}
	}
	
	this.slide = function() {
		slideInUse[objId] = true;
		var frames = 30 * duration; // Running at 30 fps

		var tIncrement = (duration*400) / frames;
		tIncrement = Math.round(tIncrement);
		var sIncrement = (this.curHeight-this.newHeight) / frames;

		var frameSizes = new Array();
		for(var i=0; i < frames; i++) {
			if(i < frames/2) {
				frameSizes[i] = (sIncrement * (i/frames))*4;
			} else {
				frameSizes[i] = (sIncrement * (1-(i/frames)))*4;
			}
		}
		
		for(var i=0; i < frames; i++) {
			this.curHeight = this.curHeight - frameSizes[i];
			window.setTimeout("document.getElementById('"+objId+"').style.height='"+Math.round(this.curHeight)+"px';",tIncrement * i);
		}
		
		window.setTimeout("delete(slideInUse['"+objId+"']);",tIncrement * i);
		
		if(this.options.onComplete) {
			window.setTimeout(this.options.onComplete, tIncrement * (i-2));
		}
		
		return tIncrement * i;
	}
	
	this.finishup = function(height) {
		this.obj.style.display = 'none';
		this.obj.style.height = height + 'px';
	}
	
	return this;
}

/* Forms functions */

function toggle_interactive_creare(tip) {

	var cnp = document.getElementById('cnp');
	var firma = document.getElementById('firma');
	var cod_fiscal = document.getElementById('cod_fiscal');

	if ( tip == 'pf' ) {
		cnp.style.display = '';
		firma.style.display = 'none';
		cod_fiscal.style.display = 'none';
	} else {
		cnp.style.display = 'none';
		firma.style.display = '';
		cod_fiscal.style.display = '';
	}
}

function validare_cautare_avansata() {

	alert('am chemat functia');

	if(document.cautare_avansata.search.value == '') {
		alert("Termenii de cautare trebuie sa contina cel putin 3 caractere");
		document.cautare_avansata.search.focus();
		return false;
	}
	return true;
}

function validare_creare() {

	alert('am chemat functia');

}

// login form

function validare_cont_nou() {

	if( document.cont_nou.email.value == '' ) {
		alert("Nu ati introdus adresa de email");
		return false;
	}

	if( document.cont_nou.parola1.value == '' ) {
		alert("Nu ati introdus parola");
		return false;
	}

	if( document.cont_nou.parola2.value == '' ) {
		alert("Nu ati introdus parola");
		return false;
	}

	if( document.cont_nou.parola1.value != document.cont_nou.parola2.value ) {
		alert("Parolele introduse nu coincid.");
		return false;
	}

	if( document.cont_nou.judet.value == '' ) {
		alert("Nu ati introdus judetul");
		return false;
	}

	if( document.cont_nou.persoana_contact.value == '' ) {
		alert("Nu ati introdus persoana de contact.");
		return false;
	}

	if( document.cont_nou.termeni.checked == false ) {
		alert("Trebuie sa fiti de acord cu termenii si conditiile pentru a putea continua.");
		return false;
	}

	return true;
}

function validare_login() {

	if( document.login.email.value == '' ) {
		alert("Nu ati introdus adresa de email");
		return false;
	}
	if( document.login.parola.value == '' ) {
		alert("Nu ati introdus parola");
		return false;
	}

	return true;
}

function validare_produs_nou() {

	if( document.produs_nou.titlu.value == '' ) {
		alert("Nu ati introdus titlul.");
		return false;
	}

	if( document.produs_nou.cat.value == '' ) {
		alert("Nu ati ales categoria.");
		return false;
	}

	if( document.produs_nou.subcat.value == '' ) {
		alert("Nu ati ales subcategoria.");
		return false;
	}

	if( document.produs_nou.continut.value == '' ) {
		alert("Nu ati completat descrierea.");
		return false;
	}

	var regiune = "";
	for ( var i=0; i < document.produs_nou.regiuni.length; i++ ) {
		if(document.produs_nou.regiuni[i].checked) {
			regiune +=document.produs_nou.regiuni[i].value;
		}
	}
	if ( regiune == "" ) {
		alert("Alegeti cel putin o regiune.");
		return false;
	}

	return true;
}

function validare_album_nou() {

	if( document.album_nou.titlu.value == '' ) {
		alert("Nu ati introdus titlul.");
		return false;
	}

	if( document.album_nou.regiune.value == '' ) {
		alert("Nu ati introdus regiunea.");
		return false;
	}

	if( document.album_nou.localitate.value == '' ) {
		alert("Nu ati introdus localitatea.");
		return false;
	}

	if( document.album_nou.data.value == '' ) {
		alert("Nu ati introdus data.");
		return false;
	}
}

function confirm_delete() {
	
	var answer = confirm("Esti sigur ca vrei sa stergi aceasta inregistrare?");
	
	if ( answer ) {
		return true;
	} else {
		return false;
	}
}

