// JavaScript Document
var http_request = false;
var MyPostSend = "";

	function setForm(formName){
		var ff=document.forms[formName];
		//alert(ff.elements.length);
		for(var i=0;i<ff.elements.length;i++){
			if(ff.elements[i].type != "submit" && ff.elements[i].type != "button"){
				var InputName = ff.elements[i].id;
				if(InputName != ""){
					if(i == 0){
						MyPostSend += InputName+"="+document.getElementById(InputName).value;
					} else {
						MyPostSend += "&"+InputName+"="+document.getElementById(InputName).value;
					}//endif
				}//endif
			}//endif
		}//endfor
		return true;
	}//endfunction

    function makeRequestPost(url) {

        http_request = false;
		//MyPostSend = "pippo=" + escape(document.getElementById('pippo').value);

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        //http_request.onreadystatechange = alertContents;
        http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // sending it as encoded formdata
		http_request.setRequestHeader("Connection","close"); // Connection is to be closed after transfer
        http_request.send(MyPostSend);		
    }
	
	function makeRequestGet(url) {

        http_request = false;
		//MyPostSend = "pippo=" + escape(document.getElementById('pippo').value);

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        //http_request.onreadystatechange = alertContents;
        http_request.open('GET', url, true);
		http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // sending it as encoded formdata
		http_request.setRequestHeader("Connection","close"); // Connection is to be closed after transfer
        http_request.send(null);		
    }

    function alertContents() {
	 	if (http_request.readyState > 0 && http_request.readyState <4) {
			document.getElementById('allerta').style.display = 'none';
		} else {
			document.getElementById('allerta').style.display = 'block';
		}
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                alert(http_request.responseText);
				/*var xmldoc = http_request.responseXML;
				var root_node = xmldoc.getElementsByTagName('root').item(0);
				alert(root_node.firstChild.data);
				document.getElementById('writer').innerHTML += "<br>"+root_node.firstChild.data;
				//alert(http_request.responseText);
				document.getElementById('allerta').style.display = 'none';*/
            } else {
                alert('There was a problem with the request.');
            }
        }

    }
	
	function showVenditoriEE_x(){
		MyPostSend = "nazione="+document.getElementById('nazione').value;
		MyPostSend += "&codcat="+document.getElementById('codcat').value;
		//document.getElementById('addlog').style.display = "block";
		makeRequestPost('/include/rivenditori_esteri.php');
		http_request.onreadystatechange = show_list;	
	}

	function showVenditoriEE(nazione,codcat){
		MyPostSend = "nazione=" + nazione;
		MyPostSend += "&codcat=" + codcat;
		//document.getElementById('addlog').style.display = "block";
		makeRequestPost('/include/rivenditori_esteri.php');
		http_request.onreadystatechange = show_list;	
	}

	
	function showVenditori(){
		MyPostSend = "prov="+document.getElementById('prov').value;	
		//document.getElementById('addlog').style.display = "block";
		makeRequestPost('/include/rivenditori.php');
		http_request.onreadystatechange = show_list;	
	}
	
	function show_list(){
		if (http_request.readyState == 0) {
			document.getElementById('reseller_box').innerHTML = "Sto caricando i dati";
		}
		if (http_request.readyState == 1) {
			document.getElementById('reseller_box').innerHTML = "Sto caricando i dati";
		}
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				document.getElementById('reseller_box').innerHTML = http_request.responseText;
				new Effect.Highlight('reseller_box',{duration:1.5}); 
				return false;
			} else {
				alert('There was a problem with the request.');
			}
		}
	}
