var site_url = "http://www.bormasina.ro/";

function schimba_clase_producatori() {

	elemente = getElementsByClassName(document,"*","pic_prod");

	for ( i=0;i<elemente.length;i++ ) {

		elemente[i].onmouseover = function() { this.className = 'pic_prod_over'}

		elemente[i].onmouseout = function() { this.className = 'pic_prod'}

	}

}

function AttachEvent(elementObj, eventName, eventHandlerFunctionName)

{

  if (elementObj.addEventListener) 

  { 

    elementObj.addEventListener(eventName, eventHandlerFunctionName, false);		

  } 

  else if (elementObj.attachEvent) 

  { 

    elementObj.attachEvent('on' + eventName, eventHandlerFunctionName);

  } 

  else 

  { 

    var currentEventHandler = elementObj['on' + eventName];

    if (currentEventHandler == null) 

    {

      elementObj['on' + eventName] = eventHandlerFunctionName;

    } 

    else 

    {

      elementObj['on' + eventName] = function(e) { currentEventHandler(e); eventHandlerFunctionName(e); }

    }

  }

}

function getElementsByClassName(oElm, strTagName, strClassName){

    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);

    var arrReturnElements = new Array();

    strClassName = strClassName.replace(/\-/g, "\\-");

    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");

    var oElement;

    for(var i=0; i<arrElements.length; i++){

        oElement = arrElements[i];      

        if(oRegExp.test(oElement.className)){

            arrReturnElements.push(oElement);

        }   

    }

    return (arrReturnElements)

}

function captcha_reload(id) {

	theImg=document.getElementById(id);

	theImg.src +='?'+ Math.round(Math.random()*100000);

}

function getPageSize(){

	

	var xScroll, yScroll;

	

	if (window.innerHeight && window.scrollMaxY) {	

		xScroll = document.body.scrollWidth;

		yScroll = window.innerHeight + window.scrollMaxY;

	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac

		xScroll = document.body.scrollWidth;

		yScroll = document.body.scrollHeight;

	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari

		xScroll = document.body.offsetWidth;

		yScroll = document.body.offsetHeight;

	}

	

	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer

		windowWidth = self.innerWidth;

		windowHeight = self.innerHeight;

	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode

		windowWidth = document.documentElement.clientWidth;

		windowHeight = document.documentElement.clientHeight;

	} else if (document.body) { // other Explorers

		windowWidth = document.body.clientWidth;

		windowHeight = document.body.clientHeight;

	}	

	

	// for small pages with total height less then height of the viewport

	if(yScroll < windowHeight){

		pageHeight = windowHeight;

	} else { 

		pageHeight = yScroll;

	}



	// for small pages with total width less then width of the viewport

	if(xScroll < windowWidth){	

		pageWidth = windowWidth;

	} else {

		pageWidth = xScroll;

	}





	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 

	return arrayPageSize;

}

function nimic(){

}

function MM_swapImgRestore() { //v3.0

  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;

}

function MM_preloadImages() { //v3.0

  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 MM_findObj(n, d) { //v4.01

  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {

    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}

  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];

  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);

  if(!x && d.getElementById) x=d.getElementById(n); return x;

}

function MM_swapImage() { //v3.0

  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)

   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}

}

function confirm_delete(url,question) {

	test = confirm(question);

	if ( test == true ){

		document.location = url;

		return false;

	}

}

var handlerFunc = function(t) {

    alert(t.responseText);

}

var handlerXML = function(t) {

    alert(t.responseXML);

}

var errFunc = function(t) {

    alert('Error ' + t.status + ' -- ' + t.statusText);

}

var schimba_producator = function(nume_producator) {

	nume = nume_producator.split("|");

	if ( nume[0] != 0 ) window.location = site_url+"producatori/"+nume[0]+'-'+nume[1];

	else  window.location = site_url+"producatori/";

}

var open_window = function(url,height,width) {

	

	window.open(url,'','scrollbars=yes,menubar=no,height='+height+',width='+width+',resizable=yes,toolbar=no,location=no,status=no');

}

function resizeOuterTo(w,h) {

	if (parseInt(navigator.appVersion)>3) {

		if (navigator.appName=="Netscape") {

			top.outerWidth=w;

			top.outerHeight=h;

		}

		else top.resizeTo(w,h);

	}

}

function moveWindowTo(w,h) {

	sw = screen.width;

	sh = screen.height;

	l = (sw - 800) / 2;

	t = (sh - 800) / 2;

	moveTo(l,t)

}



var change_large = function(id,path) {

	acts =  getElementsByClassName(document, "*", "img_active");

	for ( i=0;i<acts.length;i++) {

		acts[i].className = "img_none";

	}

	$(id).className = "img_active";

	$('main_pic').src = path;

}

var erori_inregistrare = function(t) {

	alert(raspuns);

	raspuns = t.responseText;

	$('error_display').innerHTML = raspuns;

	new Effect.Fade('error_display');

}

var arata_erori = function(r) {

	rasp = r.responseText;

	//$('error_display').innerHTML = rasp;

	status = rasp.substr(0,7);

	if ( status == "success" ) {

		if ( $('add_review') ) new Effect.Fade('add_review');

		$('error_display').innerHTML = rasp.substr(8,rasp.length);

	}

	else {

		$('error_display').innerHTML = rasp;

		if ( $('lnk_securitycode') ) {

			captcha_reload('captcha','lnk_securitycode');

			$('lnk_securitycode').value="";

		}

	}

	new Effect.ScrollTo('error_display',{offset: -50});

	new Effect.Appear('error_display');

}

var arata_erori_login = function(r) {

	rasp = r.responseText;

	if ( rasp.substr(1,7) != "success" ) {

		$('error_display_login').innerHTML = rasp + "<br>";

	}

	else {

		window.location = window.location;

	}

	new Effect.Appear('error_display_login');

}

var arata_opinie = function(r) {

	rasp = r.responseText;

	$('error_display').innerHTML = rasp;

	if ( rasp.substr(1,7) == "success" ) {

		if ( $('add_review') ) {

			new Effect.Fade('add_review');

		}

		$('error_display').innerHTML = rasp.substr(9,rasp.length);

		$('adaugare_opinie').style.display = "none";

	}

	else {

		$('error_display').innerHTML = rasp;

		if ( $('lnk_securitycode') ) {

			captcha_reload('captcha','lnk_securitycode');

			$('lnk_securitycode').value="";

		}

	}

	new Effect.ScrollTo('error_display',{offset: -50});

	new Effect.Appear('error_display', {afterFinish:function() {new Effect.Highlight('error_display',{duration:0.8});} });

	

}

function update_status(com_id,status) {

	new Ajax.Request(site_url+'admin/produse_functions.php',{parameters:'act=update_comanda&com_id='+com_id+'&status='+status ,asynchronous:true,onComplete:function(t){$('update_status').innerHTML=t.responseText; new Effect.Highlight('update_status');}});

}

function update_dist_activ(did,status) {

	new Ajax.Request(site_url+'admin/produse_functions.php',{parameters:'act=update_distribuitor&did='+did+'&status='+status ,asynchronous:true,onComplete:function(t){$('update_status').innerHTML=t.responseText; new Effect.Highlight('update_status');}});

}

var form_do_login = function(r) {

	rasp = r.responseText;

	if ( rasp != 1 ) {

		new Effect.Shake('login_form');

	}

	else {

		new Effect.Fade('login_form',{afterFinish:function() {

			new Ajax.Updater('div_info', site_url+'functii.php',{parameters:'act=arata_login_box' ,asynchronous:true,onComplete:function(){

				new Effect.Appear('div_info');

				document.location = document.location;

			}});

		}});

		

	}

}

function arata_produse_categorie(val) {

	new Ajax.Request(site_url+'admin/newsletter_functions_xml.php', {parameters:'act=arata_produse&cat_id='+val, onSuccess:populeaza_produse_categorie, onFailure:errFunc});

}

function populeaza_lista_produse(valoare) {

	if ( valoare != "-" ) {

		new Ajax.Request(site_url+'admin/newsletter_functions_xml.php', {parameters:'act=arata_produse&cat_id='+valoare, onSuccess:populeaza_produse_accesorii, onFailure:errFunc});

	}

}

function get_detalii(pid,container) {

	new Ajax.Updater(container, 'produs_functii.php',{parameters:'act=detalii_produs&pid='+pid ,asynchronous:true, evalScripts:true,onComplete:function(){ new Effect.Highlight(container,{startcolor:'#EEEEEE',endcolor:'#FFFFFF'});}});

}

var populeaza_produse_accesorii = function(a) {

	rasp = a.responseXML;

	var xmldoc=rasp;

	if (!xmldoc) return false;

	var selObj = $('acc_lista_produse');

	var opts = xmldoc.getElementsByTagName('options')[0];

	if (!opts) {

		selObj.length=0;

		selObj[0]=new Option('---------','-1');

		return false;

	}

	var elems = opts.getElementsByTagName('option');

	selObj.length=0;

	var val=-1;

	var txt='error';

	var opt;

	for(i=0;i<elems.length;i++) {

		val=elems[i].getAttribute("value");

		txt=elems[i].getAttribute("text");

		sel=elems[i].getAttribute("select");

		dis=elems[i].getAttribute("disable");

		selObj[i]=new Option(txt,val,dis,sel);

	}

	$('prod_list').style.display = "";

}

var populeaza_produse_categorie = function(a) {

	rasp = a.responseXML;

	var xmldoc=rasp;

	if (!xmldoc) return false;

	var selObj = $('prod_list');

	var opts = xmldoc.getElementsByTagName('options')[0];

	if (!opts) {

		selObj.length=0;

		selObj[0]=new Option('---------','-1');

		return false;

	}

	var elems = opts.getElementsByTagName('option');

	selObj.length=0;

	var val=-1;

	var txt='error';

	var opt;

	for(i=0;i<elems.length;i++) {

		val=elems[i].getAttribute("value");

		txt=elems[i].getAttribute("text");

		sel=elems[i].getAttribute("select");

		dis=elems[i].getAttribute("disable");

		selObj[i]=new Option(txt,val,dis,sel);

	}

	$('prod_list').style.display = "";

}

function toggle_div(id) {

	if ( $(id).style.display == "none")  $(id).style.display = "";

	else $(id).style.display = "none";

}

var erori_newsletter = function(t) {
	rasp = t.responseText;
	if ( rasp.substr(1,7) == "success" ) {
		new Effect.Fade('tbl_newsletter', { afterFinish: function() { 
			$('newsletter_response').innerHTML = rasp.substr(9,rasp.length);
			new Effect.Appear('newsletter_response');													
		}});		
	}
	else{	
		$('newsletter_response').innerHTML = rasp.substr(9,rasp.length);
		new Effect.Appear('newsletter_response');
		new Effect.Shake('newsletter_response');
		setTimeout("new Effect.Appear('tbl_newsletter');",5000);
	}
}
var erori_recomanda = function(t) {
	rasp = t.responseText;
	if ( rasp.substr(1,7) == "success" ) {
		new Effect.Fade('tbl_recomanda', { afterFinish: function() { 
			$('recomanda_response').innerHTML = rasp.substr(9,rasp.length);
			new Effect.Appear('recomanda_response');													
		}});		
	}
	else{	
		$('recomanda_response').innerHTML = rasp.substr(9,rasp.length);
		new Effect.Appear('recomanda_response');
		new Effect.Shake('recomanda_response');
		setTimeout("new Effect.Appear('tbl_recomanda');",5000);
	}
}
var change_newsletter_type = function(disp_what,h1) {

	$(h1).style.display = "none";

	if ( disp_what == "newsletter_produse_list" ) {

		new Ajax.Updater('newsletter_produse_list',site_url+'admin/newsletter_functions.php',{asynchronous:true,evalScripts:true,parameters:'act=listare_produse'});

	}

	$(disp_what).style.display = "";	

}

var arata_categori = function(tip,id) {

	if ( tip == 'produs' ) {

		$('listare_filtre').innerHTML="";

		new Ajax.Updater('listare_filtre',site_url+'admin/produse_functions.php',{asynchronous:true,evalScripts:true,parameters:'act=arata_filtre&id_produs='+id,onComplete:function(){$('listare_filtre').style.display="";if ( $('listare_accesorii') ) $('listare_accesorii').style.display="";}});

	}

	else {

		$('listare_filtre').style.display="none";

		if ( $('listare_accesorii')) $('listare_accesorii').style.display="none";

	}

	/*new Ajax.Updater('categorii_produs',site_url+'admin/produse_functions.php',{asynchronous:true,evalScripts:true,parameters:'act=arata_categorii&tip='+tip});

	*/

}

function enable_menu_top_left() {

	elemente = $('menu_top_left').getElementsByTagName("td");

	for ( i=0;i<elemente.length;i++) {

		this.clasa = elemente[i].className;

		elemente[i].onmouseover = function() { this.className = this.className + " buton_over" ;}

		elemente[i].onmouseout  = function() { classes = this.className.split(" "); this.className =  classes[0] ;} 

	}

}

function show_login() {

	new Effect.Appear('show_messages',{duration:0.3});

	new Draggable('show_messages',{revert:false,handle:'show_messages_handle'});

}

function close_message() {

	new Effect.Shrink('show_messages',{duration:0.5});

}

function enable_login() {

	if ( $('mtl_4') ) {

		$('mtl_4').onclick = function() {

			show_login();

			return false;

		}

	}

}

function switch_products(tip) {

	acts =  getElementsByClassName(document, "*", "tab_activ");

	if ( acts[0] ) {

	acts[0].className = "tab_pasive";

	$(tip).className = "tab_activ";

	id = tip.split("_");

	new Ajax.Updater('listare_produse',site_url+'products_functions.php',{asynchronous:true,evalScripts:true,parameters:'act=arata_produse&tip_produse='+id[1]+'&url='+window.location.href});

	}

}

function init_switch_products() {

	if ( $('lista_taburi') ) {

		taburi = $('lista_taburi').getElementsByTagName("table");

		for ( i=0;i<taburi.length;i++) {

			taburi[i].onclick = function() {

				switch_products(this.id);

				return false;

			}

		}

	}

}

function switch_info(tip,pid) {

	acts =  getElementsByClassName(document, "*", "tab_activ");

	acts[0].className = "tab_pasive";

	$(tip).className = "tab_activ";

	id = tip.split("_");

	new Ajax.Updater('detalii_produs',site_url+'products_functions.php',{asynchronous:true,evalScripts:true,parameters:'act=arata_info&info='+id[2]+'&pid='+pid});

}

function init_tabs_product(pid) {

	if ( $('lista_taburi_produs') ) {

		taburi = $('lista_taburi_produs').getElementsByTagName("table");

		for ( i=0;i<taburi.length;i++) {

			taburi[i].pid = pid;

			taburi[i].onclick = function(pid) {

				switch_info(this.id,this.pid);

				return false;

			}

		}

	}

}

function update_prd_compara(pid,exclude) {

	new Ajax.Updater('product_to_compare',site_url+'products_functions.php',{asynchronous:true,evalScripts:true,parameters:'act=arata_compara&pid='+pid+'&exclude='+exclude});

}

function schimba_tip_livrare(tip) {

	if ( tip == "magazin" ) {

		$('pic_livrare_curierat').style.display = "none";

		$('pic_livrare_magazin').style.display = "";

		$('arata_tip_livrare').innerHTML = "Adresa magazinului";

		new Ajax.Updater('tbl_livrare',site_url+'cart_functions.php',{asynchronous:true,evalScripts:true,parameters:'act=arata_adresa'});

		$('persoana_fizica').style.display = "";

		inputs = $('date_facturare').getElementsByTagName("input");

		for ( i=0;i<inputs.length;i++) {

			inputs[i].disabled = false;

		}

		$('firma_adresa').disabled= false;

		check_comanda();

	}

	if ( tip == "curierat" ) {

		$('pic_livrare_magazin').style.display = "none";

		$('pic_livrare_curierat').style.display = "";

		$('arata_tip_livrare').innerHTML = "Adresa de livrare";

		new Ajax.Updater('tbl_livrare',site_url+'cart_functions.php',{asynchronous:true,evalScripts:true,parameters:'act=arata_formular'});

		$('persoana_fizica').style.display = "";

		inputs = $('date_facturare').getElementsByTagName("input");

		for ( i=0;i<inputs.length;i++) {

			inputs[i].disabled = false;

		}

		$('firma_adresa').disabled= false;

		check_comanda();

	}

}

function tip_factura(tip) {

	if ( tip == "fizica" ) {

		$('persoana_juridica').style.display = "none";

		$('persoana_fizica').style.display = "";

		new Ajax.Updater('tip_factura',site_url+'cart_functions.php',{asynchronous:true,evalScripts:true,parameters:'act=factura_fizica'});

		

	}

	if ( tip == "juridica" ) {

		$('persoana_fizica').style.display = "none";

		$('persoana_juridica').style.display = "";

		new Ajax.Updater('tip_factura',site_url+'cart_functions.php',{asynchronous:true,evalScripts:true,parameters:'act=factura_juridica'});

	}

}

function check_comanda() {

	if ( ( $('tip_livrare_curierat').checked == true || $('tip_livrare_magazin').checked == true ) ) {

		$('trimite_comanda').disabled = false;

	}

	else {

		$('trimite_comanda').disabled = true;

	}

}

function update_localitati(judet,localitate) {

	//new Ajax.Updater('localitate',site_url+'cart_functions.php',{asynchronous:true,evalScripts:true,parameters:'act=update_localitati&judet='+judet+'&localitate='+localitate});

	new Ajax.Request(site_url+'cart_functions_xml.php',{parameters:'act=update_localitati&judet='+judet+'&localitate='+localitate ,asynchronous:true,onComplete:populeaza_localitati});

}

var populeaza_localitati = function(t) {

	rasp = t.responseXML;

	var xmldoc=rasp;

	if (!xmldoc) return false;

	var selObj = $('localitate');

	var opts = xmldoc.getElementsByTagName('options')[0];

	if (!opts) {

		selObj.length=0;

		selObj[0]=new Option('---------','-1');

		return false;

	}

	var elems = opts.getElementsByTagName('option');

	selObj.length=0;

	var val=-1;

	var txt='error';

	var opt;

	for(i=0;i<elems.length;i++) {

		val=elems[i].getAttribute("value");

		txt=elems[i].getAttribute("text");

		sel=elems[i].getAttribute("select");

		dis=elems[i].getAttribute("disable");

		selObj[i]=new Option(txt,val,dis,sel);

	}

	

}

var procesare_plata = function(r) {

	rasp = r.responseText;

	if ( rasp.substr(1,7) == "success" ) {

		new Effect.Fade('div_formular_comanda');

		$('raspuns_procesare').innerHTML = rasp.substr(9,rasp.length)


	}

	else {

		$('raspuns_procesare').innerHTML = rasp;

	}

	new Effect.ScrollTo('raspuns_procesare',{offset: -50});

	new Effect.Appear('raspuns_procesare');

}



function setOpacity(id, opac) {

  if(opac<0){opac=0;} if(opac>100){opac=100;}

  var c=opac/100;

  var d=document.getElementById(id);

  if(typeof(d.style.filter)=='string'){d.style.filter='alpha(opacity:'+opac+')';}

  if(typeof(d.style.KHTMLOpacity)=='string'){d.style.KHTMLOpacity=c;}

  if(typeof(d.style.MozOpacity)=='string'){d.style.MozOpacity=c;}

  if(typeof(d.style.opacity)=='string'){d.style.opacity=c;}

}

function fadeIn(id, opac, step) {

    document.getElementById(id).style.display="block";

    setOpacity(id, opac);    

    if (opac<100) {

        setTimeout("fadeIn('"+id+"',"+(opac+step)+","+step+")", 30);

    }

}

function fadeOut(id, opac, step) {

    setOpacity(id, opac);    

    if (opac>0) {

        setTimeout("fadeOut('"+id+"',"+(opac-step)+","+step+")", 30);

    } else {

        document.getElementById(id).style.display="none";

    }

}

function sterge_comanda(id_com,tip) {

	sterg = confirm('Esti sigur ca vrei sa stergi comanda nr. '+id_com+' ?');

	if (sterg) {

		window.location = 'comenzi_delete.php?com_id='+id_com+'&tip='+tip;

	}

}



function update_price(id,value) {
	new Ajax.Request('/admin/preturi_produse.php',{parameters:'act=update_price&pid='+id+'&pret='+value,asynchronous:true,onComplete:function(t){
		raspuns = t.responseText;
		//alert(raspuns);
		if ( raspuns.substr(1,1) == '1'  ) {
			raspuns = raspuns.split('~');
			$('update_'+id).innerHTML = raspuns[2];
			$('valoare_pret_'+id).innerHTML = raspuns[1];
			new Effect.Highlight('update_'+id,{startcolor:'#FFA22F',endcolor:'#FFFFFF'});
			new Effect.Highlight('valoare_pret_'+id,{startcolor:'#FFA22F',endcolor:'#FFFFFF'});
		}
		else {
			$('update_'+id).innerHTML = '';
		}

	}});
}
function do_product_search(value) {
	if ( value.length >= 3) {
		new Ajax.Updater('product_listing', '/admin/preturi_produse.php',{parameters:'act=cautare_produse&name='+value ,asynchronous:true,onComplete:function(){
			new Effect.Highlight('product_listing',{startcolor:'#EEEEEE',endcolor:'#FFFFFF'});
		}});
	}
}
function schimba_adaos(val,pid) {
	if ( val ==1 ) {
		$('show_custom_'+pid).style.display = '';
	}
	else {
		$('show_custom_'+pid).style.display = 'none';
	}
	new Ajax.Request('/admin/preturi_produse.php',{parameters:'act=schimba_adaos&pid='+pid+'&tip='+val,asynchronous:true,onComplete:function()
	{
		update_price(pid,$('pret_'+pid).value);
	}});
}
function update_adaos_custom(valoare,pid) {
	new Ajax.Request('/admin/preturi_produse.php',{parameters:'act=update_adaos_custom&pid='+pid+'&adaos='+valoare,asynchronous:true,onComplete:function()
	{
		update_price(pid,$('pret_'+pid).value);
	}});
}