
/* get -----------------------------*/
function getId(e_id) { var e; if (document.getElementById) { e = document.getElementById(e_id); } else { e = document.all[e_id]; } return e; };
function getIdEval(e_id) { var e; if(document.getElementById) { e = 'document.getElementById("'+e_id+'")'; } else { e = 'document.all["'+e_id+'"]'; } return e;};

/* ENLARGE -----------------------------*/
function enlarge(e) { var e = getId(e); if (e.className=="visibile") { e.className="invisibile"; } else { e.className="visibile"; } }

/* HIGHLIGHT  -----------------------------*/
function Highlight(e,c){ var e = getId(e); e.style.backgroundColor = c; }
function UnHighlight(e,c){ var e = getId(e); e.style.backgroundColor = c; }

/* CONFIRM -----------------------------*/
function Confirm(link,text) { if (confirm(text)) { window.location=link } }

/* STARTUPLOAD() -----------------------------*/
function startUpload(){ var e = getId('loading'); e.style.display = 'block'; return true; }


/* CHECK -----------------------------*/

function ckeck_uncheck_all() {	
	var frm = document.seleziona;
	for (var i=0;i<frm.elements.length;i++) {
		var elmnt = frm.elements[i];
		if (elmnt.type=='checkbox') {
			if(frm.master_box.checked == true){ elmnt.checked=false; }
			else { elmnt.checked=true; }
		}
	}

	if(frm.master_box.checked == true){ frm.master_box.checked = false; }
	else { frm.master_box.checked = true; }
}

/* POP UP - newWindow
-----------------------------
function newWindow(pagina,w,h,scroll,resizable){
  var win;   
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0; 
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;                         
  settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable='+resizable;
  win = window.open(pagina,'Gestione Foto',settings);
  //win.focus();                        
} 
*/

/* POPUP -----------------------------*/

function popup(pagina){
  var po = null;
  var w = 780; var h = 500; 
  var L = Math.floor((screen.width-w)/2);
  var T = Math.floor((screen.height-h)/2);                       
  var settings = 'height='+h+',width='+w+',top='+L+',left='+T+',scrollbars=yes,resizable=yes';
  po = window.open(pagina,"foto",settings); 
  po.focus();           
} 
 
/* CAMBIASELECT -----------------------------
 * html -> <select onchange="cambiaSelect(this.value)">
 *
 */
 
function cambiaSelect(param){ this.location.href=param; }

/* INSERT -----------------------------*/

function insert(nome,campo,setclose) {
  eval('top.opener.document.form.'+campo+'.value =""'); eval('top.opener.document.form.'+campo+'.value += nome');
  if (setclose == "yes") { eval('top.opener.document.form.'+campo+'.focus()'); top.window.close(); }
}

function insertIMG(id,campo,posizione,url_img,url_up_dir,setclose) {
  var e = getIdEval(campo); var img = '<div><img class="deco" src='+url_up_dir+posizione+'/ico/'+url_img+' /></div>';
	var sostituisci = '<div><ul class="alrButtonA"><li><a class="alrButtonA1" href="immagini.php?campo=img" onClick="popup(this.href); return false;" ><span>Sostituisci &raquo;</span></a></li><li><a class="alrButtonA2" href="#" onClick="delIMG(\''+posizione+'\',\''+campo+'\'); return false;" ><span>Elimina &raquo;</span></a></li></ul></div><div class="clear"><!-- --></div>';
	var inp = '<input type="hidden" name="'+campo+'" value="'+id+'" />';
	var txt = img+sostituisci+inp;
  eval('top.opener.'+e+'.innerHTML = txt');
  if (setclose == "yes") { top.window.close(); }
}

function delIMG(posizione,campo) {
  var e = getIdEval(campo); var img = '<div>Nessuna Immagine Selezionata<div>';
  var sostituisci = '<div><ul class="alrButtonA"><li><a class="alrButtonA2" href="'+posizione+'.php?campo='+campo+'" onClick="popup(this.href); return false;"><span>Inserisci &raquo;</span></a></li></ul></div><div class="clear"><!-- --></div>';
	var inp = '<input type="hidden" name="'+campo+'" value="" />';
	var txt = img+sostituisci+inp;
  eval(e+'.innerHTML = txt');
}


/* FAST AJAX ----------------------------- */

function getXmlHttpObject() {
  var xmlHttp = null;
  try { xmlHttp = new XMLHttpRequest(); } // Firefox, Opera 8.0+, Safari
  catch (e) { // Internet Explorer 
    try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	}
  return xmlHttp;
}


/*  LISTA  -----------------------------*/

function addOption(select,value,text,sel) {
	var option = document.createElement("option");
	if (sel == 1) { option.setAttribute('selected','selected'); }
	option.value = value,
	option.text = text;
	try {
		select.add(option, null);
	} catch(e) {
		select.add(option);	// Per Internet Explorer
	}
}

function getSelected(select) {
	return select.options[select.selectedIndex].value; // Ritorna il valore dell'elemento <option> selezionato in una lista
}

function loadList( tb , id , marc , sel , tipo ) {
  
var xmlHttp = getXmlHttpObject();

xmlHttp.open( 'GET' , 'resp_lista.php?table='+tb+'&id='+id+'&marc='+marc+'&sel='+sel+'&tipo='+tipo , true );
xmlHttp.onreadystatechange = function() {
	if(xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			
			var resp = xmlHttp.responseText;
			
			if (resp) {
				var values = resp.split(';'); // Le coppie di valori nella striga di risposta sono separate da ;
				var listId = values.shift(); // Il primo elemento è l'ID della lista.
				var select = getId(listId);
				
				while (select.options.length) { select.remove(0); } // Elimina i valori precedenti
				
				if (listId == 'regioni') { addOption(select, -1, '---> Seleziona Regione <---', '0'); }
				if (listId == 'marca') { addOption(select, -1, '---> Seleziona Marca <---', 0); }
				if (listId == 'modello') { addOption(select, -1, '---> Seleziona Modello <---', 0); }
				if (listId == 'prodotto') { addOption(select, '-1', '---> Seleziona Prodotto <---', '0'); }
				
				var limit = values.length;
				
				for(i=0; i < limit; i++) {
					var pair = values[i].split('|');
					addOption(select, pair[0], pair[1] , pair[2]);
				}
			}
		} else {
			alert(xmlHttp.responseText);
		}
	}
}
xmlHttp.send(null);
}

/* TAB PANEL -----------------------------*/

function tabEasy(obj,active){

    this.active = (active) ? active : 1,
    this.timeout = null,
    this.tabclass = 'tab',
    this.activeclass = 'active',

    this.getTabs = function(){

        var retnode = [];
        var elem = document.getElementById(obj).childNodes; // modified for IE 5.x support
        for (var i = 0; i < elem.length; i++) {
        if (elem[i].className==this.tabclass) retnode[retnode.length]=elem[i];
        }

        return retnode;

    },

    this.links = document.getElementById(obj+'-nav').getElementsByTagName('a'),
    this.tabs = this.getTabs();

    this.show = function(number){

        for (var i = 0; i < this.tabs.length; i++) {
        this.tabs[i].style.display = ((i+1)==number) ? 'block' : 'none';
        this.links[i].className = ((i+1)==number) ? this.activeclass : '';
        }

    },

    this.rotate = function(interval){

        this.show(this.active);
        this.active++;

        if(this.active > this.tabs.length) this.active = 1;

        var self = this;
        this.timeout = setTimeout(function(){self.rotate(interval);}, interval*1000);

    },

    this.init = function(interval){

        this.show(this.active);

        var self = this;
        for (var i = 0; i < this.links.length; i++) {
        this.links[i].customindex = i+1;
        this.links[i].onclick = function(){ if (self.timeout) clearTimeout(self.timeout); self.show(this.customindex); return false; };
        }

        if (interval) this.rotate(interval);

    };

};

