function initScripts(){
	var tag = document.getElementsByTagName("SCRIPT");
	for(var x=0;x< tag.length;x++){
			if(tag[x].hasChildNodes()){
				var script = tag[x].firstChild.nodeValue;
				eval(script);
			}
	}
}

function initLink(){
	var tag = document.getElementsByTagName("A");
	for(var x=0;x< tag.length;x++){
			if(tag[x].getAttribute("onclick")){
				if(tag[x].href.indexOf("#")>0){
					tag[x].href = tag[x].href.substring(tag[x].href.indexOf("#"));
				}else{
					tag[x].href = "#";
				}
			}
	}
}

function initLoading(){
    var corpo = document.getElementById("head");
    if(corpo){
        //<div id="loading"><span>Loading...</span></div>
        var loading = document.createElement('div');
        loading.style.display = "none";
        var span_loading = document.createElement('span');
		corpo.appendChild(loading);
        loading.id = "loading";
        loading.appendChild(span_loading);
        span_loading.appendChild(document.createTextNode("In caricamento..."));
    }
} 

function onLoading(){
	document.getElementById("loading").style.display = 'block';
}
function onLoaded(){
	document.getElementById("loading").style.display = 'none';
}
function getPath(){
	var arr_path = window.location.pathname.split("/");
	var temp = '';
	for(x=0;x<arr_path.length;x++){
		if(arr_path[x].indexOf("php")>0){
		}else{
			temp += arr_path[x] + "/";
		}
	}
	return temp;
}
function getContent(page, divtarget, params){
	
	if(!divtarget) divtarget = 'content';
	var ajax = new sack();
	var url = getPath();
	var uri = url + page + ".php";

	ajax.setVar("ajax", 1);
    ajax.setVar("target", divtarget.replace("container_",""));
    if(params){
     var param = params.split("&");
	 for(var x=0; x < param.length; x++){
    	var split_param = param[x].split("=");
   	    ajax.setVar(split_param[0], split_param[1]);
     }
    }
    ajax.method = "GET";
	ajax.requestFile = uri;
	ajax.element = divtarget;
	ajax.onLoading = onLoading;
	ajax.onLoaded = onLoaded;
    ajax.onCompletion = ajaxOnload;
    ajax.runAJAX();
    return false;
}
function readParameters(element){

	//var num = document.forms[0].elements.length;
	var num = element.elements.length;
	var url = "";
	//radio button 
	var j = 0;
	var a = 0;
	var radio_buttons = new Array();
	var nome_buttons = new Array();

	//var the_form = window.document.forms[0];
	var the_form = element;
	for(var i=0; i< the_form.length; i++){
		var temp = the_form.elements[i].type;
		if ( (temp == "radio") && ( the_form.elements[i].checked) ) { 
			nome_buttons[a] = the_form.elements[i].name;
			radio_buttons[j] = the_form.elements[i].value; 
			j++; 
			a++;
		}
	}
	for(var k = 0; k < radio_buttons.length; k++) {
		url += nome_buttons[k] + "=" + radio_buttons[k] + "&";
	}
	//checkbox
	var j = 0;
	var a = 0;
	var check_buttons = new Array();
	var nome_buttons = new Array();
	
	for(var i=0; i< the_form.length; i++){
		var temp = the_form.elements[i].type;
		if ( (temp == "checkbox") && ( the_form.elements[i].checked) ) { 
			nome_buttons[a] = the_form.elements[i].name;
			check_buttons[j] = the_form.elements[i].value; 
			j++; 
			a++;
		}
	}
	for(var k = 0; k < check_buttons.length; k++) {
		url += nome_buttons[k] + "=" + check_buttons[k] + "&";
	}
	
	for (var i = 0; i < num; i++){
		var chiave = the_form.elements[i].name;
		var valore = the_form.elements[i].value;
		var tipo = the_form.elements[i].type;
		//(tipo == "submit") ||
		if (  (tipo == "radio") || (tipo == "checkbox") ){}
		else {

			url += chiave + "=" + valore + "&";
		}
	}
	//alert(url);
	return url;	
/*	var parameters = url;
	url = FILE + "?" + url;
	if (METHOD == undefined) { METHOD = "GET"; 	}
	if (METHOD == "GET") { ahah(url, 'target', '', METHOD); }
	else { ahah(FILE, 'target', '', METHOD, parameters); }*/
	
}
function ajaxForm(element,page,target,mparams){
	var str_parameters = readParameters(element);
	if(str_parameters == false) return false;
	if(!target) target = 'content';
	var ajax = new sack();
	
	
	ajax.setVar("ajax", 1);
    ajax.setVar("target", target.replace("container_",""));

	if(mparams){
     var mparam = mparams.split("&");
	 for(var x=0; x < mparam.length; x++){
    	var split_mparam = mparam[x].split("=");
   	    ajax.setVar(split_mparam[0], split_mparam[1]);
     }
    }
    
    var param = str_parameters.split("&");

    for(var x=0; x < param.length; x++){
    	var split_param = param[x].split("=");
    	if(split_param[0] == 'page'){
    		page = split_param[1];
   	    }else{
   	    	ajax.setVar(split_param[0], split_param[1]);
   	    }
    }
    if(page){
    	ajax.setVar("page", page);
    	var url = getPath();
		var uri = url + page + ".php";
	}
    
    
    
    ajax.method = "POST";
	ajax.requestFile = uri;
	ajax.element = target;
	ajax.onLoading = onLoading;
	ajax.onLoaded = onLoaded;
    ajax.onCompletion = ajaxOnload;
    ajax.runAJAX();
    return false;	
}			
function setonAlt(index,elem){
		if(document.getElementById('alt'+elem+index)){
			document.getElementById('alt'+elem+index).style.display = "inline";
		}	
}
function setoffAlt(index,elem){
		if(document.getElementById('alt'+elem+index)){
			document.getElementById('alt'+elem+index).style.display = "none";
		}	
}
function diminuisciQuantita(quant,link){
	if(quant == 1){
		 if (confirm("Vuoi eliminare il prodotto dal carrello?"))
			window.location = link;
	}else{
		window.location = link;
	}
}
function noprivacy(){
	if (confirm("Non autorizzando il trattamento dati non sar? possibile procedere.")){
		document.getElementById("invia_registrazione").disabled = 'disabled';						 	
	}
}	
function enableReg(){
	document.getElementById("invia_registrazione").disabled = '';
}
function checkRegister(tipo){
	var Tipo = tipo;
	var pre = (Tipo == 'privato')?'P_':'A_';
	var t = (Tipo == 'privato')?'P':'A';
	//obbligatori
	//consenso
	if(!document.getElementById(pre + "trattamento_si").checked){
		alert("Per proseguire accettare il consenso al trattamento dei dati.");
		return false;
	}
	//username
	if(document.getElementById(pre + "Rusername").value == ""){
		alert("Per proseguire compilare il campo : username.");
		return false;
	}
	//password
	if(document.getElementById(pre + "Rpw").value == ""){
		alert("Per proseguire compilare il campo : password.");
		return false;
	}
	//ripeti password
	if(document.getElementById(pre + "Rpw2").value == ""){
		alert("Per proseguire compilare il campo : ripeti password.");
		return false;
	}
	//password
   	if(document.getElementById(pre + "Rpw").value != document.getElementById(pre + "Rpw2").value){
		 alert("Le password non corrispondono");
   		 return false;
   	}
	//nome
	if(document.getElementById(pre + "nome").value == ""){
		alert("Per proseguire compilare il campo : nome.");
		return false;
	}
	//cognome
	if(document.getElementById(pre + "cognome").value == ""){
		alert("Per proseguire compilare il campo : cognome.");
		return false;
	}
	
	//piva
	if(t == 'A'){
		if(document.getElementById(pre + "piva").value == ""){
			alert("Per proseguire compilare il campo : partita iva.");
			return false;
		}
		//piva caratteri
	  	if(document.getElementById(pre + "piva").value.length > 0){
	  	 	if(!(document.getElementById(pre + "piva").value.length == 11)){ 
	  	 		alert("La partita iva deve contenere 11 caratteri");
				return false;
	  	 	}
	  	}
	}
	//cf
	if(document.getElementById(pre + "cf").value == ""){
		alert("Per proseguire compilare il campo : codice fiscale.");
		return false;
	}
	if(t == 'A'){
		//cf caratteri
	  	if(document.getElementById(pre + "cf").value.length > 0){
	  	 	if(!(document.getElementById(pre + "cf").value.length == 16 || 
	  	 		(document.getElementById(pre + "cf").value.length == 11 && document.getElementById(pre + "cf").value == document.getElementById(pre + "piva").value)
	  	 	)){ 
	  	 		alert("Il codice fiscale deve contenere 16 caratteri o deve essere la ripetizione della partita iva");
				return false;
	  	 	}
	  	}	
	}else{
		//cf caratteri
	  	if(document.getElementById(pre + "cf").value.length > 0){
	  	 	if(!(document.getElementById(pre + "cf").value.length == 16)){ 
	  	 		alert("Il codice fiscale deve contenere 16 caratteri");
				return false;
	  	 	}
	  	}
	}
	//indirizzo
	if(document.getElementById(pre + "indirizzo").value == ""){
		alert("Per proseguire compilare il campo : indirizzo.");
		return false;
	}	
	//citta
	if(document.getElementById(pre + "citta").value == ""){
		alert("Per proseguire compilare il campo : citta.");
		return false;
	}	
	//cap
	if(document.getElementById(pre + "cap").value == ""){
		alert("Per proseguire compilare il campo : cap (sar? usato per calcolare l'eventuale costo di trasporto).");
		return false;
	}
	// cap se presente
  	if(document.getElementById(pre + "cap").value.length > 0){
  	 	if(document.getElementById(pre + "cap").value.length != 5){
	  	 	alert("Il Cap deve contenere 5 cifre");
			return false;
  	 	}
  	}
	//email
	if(document.getElementById(pre + "email").value == ""){
		alert("Per proseguire compilare il campo : email.");
		return false;
	}
	//email 
  	if(document.getElementById(pre + "email").value.indexOf("@") == -1 || document.getElementById(pre + "email").value.indexOf(".") == -1){
  	 	alert("Email non valida");
  		return false;
  	}
	//telefono
	if(document.getElementById(pre + "telefono").value == ""){
		alert("Per proseguire compilare il campo : telefono.");
		return false;
	}	
	//cellulare
	if(document.getElementById(pre + "cellulare").value == ""){
		alert("Per proseguire compilare il campo : cellulare.");
		return false;
	}	
	return true;
}

function checkLogin(){
	
	//obbligatori
	//username
	if(document.getElementById("username").value == ""){
		alert("Per proseguire compilare il campo : username.");
		return false;
	}
	//password
	if(document.getElementById("pw").value == ""){
		alert("Per proseguire compilare il campo : password.");
		return false;
	}
}

function checkTrasporto(){
	
	//obbligatori
	//cap
	if(document.getElementById("cap").value == ""){
		alert("Per proseguire compilare il campo : cap.");
		return false;
	}
	// cap se presente
  	if(document.getElementById("cap").value.length > 0){
  	 	if(document.getElementById("cap").value.length != 5){
	  	 	alert("Il Cap deve contenere 5 cifre");
			return false;
  	 	}
  	}
}

function checkDati(){
	
	//obbligatori
	//indirizzo
	if(document.getElementById("indirizzo").value == ""){
		alert("Per proseguire compilare il campo : indirizzo.");
		return false;
	}
	//citta
	if(document.getElementById("citta").value == ""){
		alert("Per proseguire compilare il campo : citta.");
		return false;
	}
	//cap
	if(document.getElementById("cap").value == ""){
		alert("Per proseguire compilare il campo : cap.");
		return false;
	}
	// cap se presente
  	if(document.getElementById("cap").value.length > 0){
  	 	if(document.getElementById("cap").value.length != 5){
	  	 	alert("Il Cap deve contenere 5 cifre");
			return false;
  	 	}
  	}
	//telefono
	if(document.getElementById("telefono").value == ""){
		alert("Per proseguire compilare il campo : telefono.");
		return false;
	}
}
function toggleDebug(){
	var el = document.getElementById('debugContainer');
	if(el.style.display == 'block'){
		el.style.display = 'none';
	}else{
		el.style.display = 'block';
	}
}
function toggleDump(){
	var el = document.getElementById('dumpContainer');
	if(el.style.display == 'block'){
		el.style.display = 'none';
	}else{
		el.style.display = 'block';
	}
}
function displayRegisterForm(){
	var html_privato_off = '<a onclick="displayRegisterForm(this, \'privato\');" href="#">Privato</a>';
	var html_privato_on = 'Privato';
	var html_azienda_off = '<a onclick="displayRegisterForm(this, \'azienda\');" href="#">Azienda</a>';
	var html_azienda_on = 'Azienda';
	
	var cont_privato = document.getElementById('cont_privato');
	var cont_azienda = document.getElementById('cont_azienda');
	
	var btn_privato = document.getElementById('butt_privato');
	var btn_azienda = document.getElementById('butt_azienda');
	
	var form_azienda = document.getElementById('form_azienda');	
	var form_privato = document.getElementById('form_privato');
	
	if(form_azienda.style.display == 'block'){
		form_azienda.style.display = 'none';
		form_privato.style.display = 'block';
		cont_privato.innerHTML = html_privato_on;
		cont_azienda.innerHTML = html_azienda_off;
		cont_azienda.className = '';
		cont_privato.className = 'cont_attiva';
	}else{
		form_azienda.style.display = 'block';
		form_privato.style.display = 'none';
		cont_privato.innerHTML = html_privato_off;
		cont_azienda.innerHTML = html_azienda_on;
		cont_azienda.className = 'cont_attiva';
		cont_privato.className = '';
	}
}