// JavaScript Document

function doLogin() {

    var username = document.forms['login'].username.value;
    var password = document.forms['login'].password.value;
    var reqPage = document.forms['login'].reqPage.value;

    jx.load('users.php?username=' + username + '&password=' + password, function (data) {

        if (data == "loginok") {
            parent.parent.document.location.href = reqPage;
        } else {
            document.getElementById("logError").style.visibility = "visible";
        }

    },
    'text', 'post');

    return false;
}

function checkFormValues(form) {
	
	for(i=0; i<form.elements.length; i++) {
		if (form.elements[i].type == 'text' && form.elements[i].value != "") return true;
		if (form.elements[i].type == 'select-one' && form.elements[i].value != "scegli") return true;
	}
	return false;
}


function checkFormRicerca(form, trackInfoPage) {
	
	//debugger;
	var trackInfo = "";
	
	if (!checkFormValues(form)) {
		
		alert("Inserisci almento un parametro di ricerca!");
		return false;
	} else {
		
		if (form.sezione != null && form.sezione.value != "scegli") {		
			form.action = form.sezione.value;
			trackInfo += "_sezione_" + form.sezione.value;
		}
		
		for(i=0; i<form.elements.length; i++) {
			if (form.elements[i].type == 'text' && form.elements[i].value != "") {
				trackInfo += "_" + form.elements[i].name + "_" + form.elements[i].value + "/";
			}
			
			if (form.elements[i].type == 'select-one') {
				if (form.elements[i].value == "scegli") {
					form.elements[i].value = "";
				} else {
					trackInfo += "_" + form.elements[i].name + "_" + form.elements[i].value + "/";	
				}
			}
		}		
	}

	Track(trackInfoPage + "DS" + trackInfo);
	return true;
}

function checkFormFT(form, trackInfo) {

	if (form.ftsearch.value == "") {
		
		alert("Inserisci il testo di ricerca nel campo!");
		return false;
	}
	Track(trackInfo + "FT_" + form.ftsearch.value + "/");
	return true;	
}

function submitFormFT(ftKey, page) {

	document.forms["ricercaftcms"].ftsearch.value = ftKey;
	document.forms["ricercaftcms"].pagina.value = page;
	
	document.forms["ricercaftcms"].submit();
}

//Le funzioni qui sotto riportate sono di servizio e servono per effettuare l'unique dei valori di ritorno del JSON e lo split delle informazioni contenute nell'array JJSON
function unique(a, key) {
    var tmp = eval("[{" + key + ":\"scegli\"}];")
    for (i = 0; i < a.length; i++) {
        if ((!contains(tmp, a[i], key)) && (trim(a[i][key]) != "")) {
            tmp.push(a[i])
        }
    }
    return tmp;
}



function contains(a, e, key) {
    for (j = 0; j < a.length; j++)
    if (a[j][key] == e[key]) return true;
    return false;
}



function splitvalue(a, key) {
    var tmp = eval("[{" + key + ":\"scegli\"}];")
    var vettore = [];
    for (i = 0; i < a.length; i++) {
        vettore = a[i][key].split(",")
        for (j = 0; j < vettore.length; j++) {
            var nuovovettore = "[{" + key + ":\" " + vettore[j] + "\"}]";
            var vettoreeval = eval(nuovovettore);
            tmp.push(vettoreeval[0]);
        }
    }
    return tmp;
}



function trim(stringa) {
    while (stringa.substring(0, 1) == ' ') {
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length - 1, stringa.length) == ' ') {
        stringa = stringa.substring(0, stringa.length - 1);
    }
    return stringa;
}

function editStyle(id, styleName, value) {
	
	obj = getObject(id);
	if (obj != null) obj.style[styleName] = value;
}

function setVisible(id, visible) {
	if (id != null && id.length > 0) {
		if (visible) {
			editStyle(id, "display", "block");
		} else {
			editStyle(id, "display", "none");		
		}
	}
}

function switchVisible(id2Show, id2Hide) {
	
	setVisible(id2Show, true);
	setVisible(id2Hide, false);	
	return id2Show;
}

function getObject(id) {

    var object = null;
    if (document.layers) {
        object = document.layers[id];
    } else if (document.all) {
        object = document.all[id];
    } else if (document.getElementById) {
        object = document.getElementById(id);
    }
    return object;
}

function switchTabs(idTab, idContenuto, indice, numeroTabs, stileTab) {

	for (i = 1; i < numeroTabs + 1; i++) {
        getObject(idContenuto + i).style.display = "none";
        getObject(idTab + i).className = stileTab;
    }
	
    getObject(idTab + indice).className = stileTab + "_sel";
    dojo.fadeIn({
        node: idContenuto + indice,
        duration: 500,
        beforeBegin: function () {
            var node = dojo.byId(idContenuto + indice);
            dojo.style(node, "opacity", 0);
            dojo.style(node, "display", "block");
        }
    }).play();	
}

function changeFrameContent(frameId, url) {
	var frame=getObject(frameId);
	if (frame != undefined) frame.src= url;
}

function correggifoto(stringaimg) {
    alert(0);
    document.write(stringaimg.replace("\"", ""));
}



function addLoadEvent(func) {
	
	/* ESEMPIO addLoadEvent */
	//addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
	
	//addLoadEvent(function () {
		/* more code to run on page load */
	//});


	var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function () {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function LoadAdvFrame() {
	var bannerleft=getObject("b120x600");
	if (bannerleft != undefined) bannerleft.src="webadsBanner.php?size=120x600&pos=LEFT&sezione=PROVE";
	var bannerright=getObject("b300x250");
	if (bannerright != undefined) bannerright.src="webadsBanner.php?size=300x250&pos=RIGHT&sezione=PROVE";
	var bannertop=getObject("b728x90");
	if (bannertop != undefined) bannertop.src="webadsBanner.php?size=728x90&pos=TOP&sezione=PROVE";
}
			
function LoadAdvAndTrack(trkInfo) {
	pageTracker._trackPageview(trkInfo);
	LoadAdvFrame();
}

function Track(trkInfo) {
	pageTracker._trackPageview(trkInfo);
}

function marcaUpdated(track) {

	marca = document.forms["ricerca"].marca.value;
	if (marca != "scegli") {
		LoadAdvAndTrack('/Ricerca' + track + 'marca_' + marca);

		if (getObject("layer_modello") != null) jx.bind({'url':'cmbData.php?what=modello&marca=' + marca + '&track=' + track, 'update':"layer_modello"});
		if (getObject("layer_anno") != null) jx.bind({'url':'cmbData.php?what=anno&marca=*&track=' + track, 'update':"layer_anno"});

	}
}

function modelloUpdated(track) {

	modello = document.forms["ricerca"].modello.value;	
	if (modello != "scegli") {
		marca = document.forms["ricerca"].marca.value;
		LoadAdvAndTrack('/Ricerca' + track + 'modello_' + modello);	

		if (getObject("layer_anno") != null) jx.bind({'url':'cmbData.php?what=anno&marca=' + marca + '&modello=' + modello + '&track=' + track, 'update':"layer_anno"});
	}
}

function annoUpdated(track) {

	anno = document.forms["ricerca"].anno.value;	
	if (anno != "scegli") {
		modello = document.forms["ricerca"].modello.value;	
		marca = document.forms["ricerca"].marca.value;
		LoadAdvAndTrack('/Ricerca' + track + 'anno_' + anno);	
	}
}
