﻿// File JScript

function xmlhttpPost(strURL,formname,responsediv,responsemsg,callback) {
    var xmlHttpReq = false;
    var self = this;
    // Xhr per Mozilla/Safari/Ie7
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // per tutte le altre versioni di IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    //self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
			// Quando pronta, visualizzo la risposta del form
            updatepage(self.xmlHttpReq.responseText,responsediv,callback);
            
        }
		else{
			// In attesa della risposta del form visualizzo il msg di attesa
			updatepage(responsemsg,responsediv,null);
		}
    }
    self.xmlHttpReq.send(getquerystring(formname));
}

function getquerystring(formname) {
    var form = document.forms[formname];
    var qstr = "";

    function GetElemValue(name, value) {
        qstr += (qstr.length > 0 ? "&" : "")
            + escape(name).replace(/\+/g, "%2B") + "="
            + escape(value ? value : "").replace(/\+/g, "%2B");
			//+ escape(value ? value : "").replace(/\n/g, "%0D");
    }
	
	var elemArray = form.elements;
    for (var i = 0; i < elemArray.length; i++) {
        var element = elemArray[i];
        var elemType = element.type.toUpperCase();
        var elemName = element.name;
        if (elemName) {
            if (elemType == "TEXT"
                    || elemType == "TEXTAREA"
                    || elemType == "PASSWORD"
					|| elemType == "BUTTON"
					|| elemType == "RESET"
					|| elemType == "SUBMIT"
					|| elemType == "FILE"
                    || elemType == "HIDDEN")
                GetElemValue(elemName, element.value);
            else if (elemType == "CHECKBOX" && element.checked)
                GetElemValue(elemName, 
                    element.value ? element.value : "On");
            else if (elemType == "RADIO" && element.checked)
                GetElemValue(elemName, element.value);
            else if (elemType.indexOf("SELECT") != -1)
                for (var j = 0; j < element.options.length; j++) {
                    var option = element.options[j];
                    if (option.selected)
                        GetElemValue(elemName,
                            option.value ? option.value : option.text);
                }
        }
    }
    return qstr;
}

function updatepage(str,responsediv,call){
    document.getElementById(responsediv).innerHTML = str;
    callback(call);
}

function callback(call) {
    eval(call);
}


function xmlhttpPostCascade(arrPages,arrDivs,Ind,formname,responsemsg,arrCallBack)    {
    Ind = Ind +1  
    if (Ind<arrPages.length) {
        var xmlHttpReq = false;
        var self = this;
        // Xhr per Mozilla/Safari/Ie7
        if (window.XMLHttpRequest) self.xmlHttpReq = new XMLHttpRequest();
        // per tutte le altre versioni di IE
        if (window.ActiveXObject) self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
         
        self.xmlHttpReq.open('POST', arrPages[Ind], true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
       
        self.xmlHttpReq.onreadystatechange = function() {
            if (self.xmlHttpReq.readyState == 4) {
			    // Quando pronta, visualizzo la risposta del form
			    UpdatePageCascade(self.xmlHttpReq.responseText,arrDivs[Ind],arrCallBack[Ind]);
                 
                if (Ind<arrPages.length) xmlhttpPostCascade(arrPages,arrDivs,Ind,formname,responsemsg,arrCallBack)
               
            } else {
			    // In attesa della risposta del form visualizzo il msg di attesa
			    if (Ind<arrPages.length) UpdatePageCascade(responsemsg,arrDivs[Ind]);
		    }
        }
        self.xmlHttpReq.send(getquerystring(formname));
    }
}
function UpdatePageCascade(str,responsediv,call){
    if (document.getElementById(responsediv)) document.getElementById(responsediv).innerHTML = str;
    callback(call);
}

function xmlhttpPostAdv(strURL, formname, responsediv, responsedivloader, responsemsg, callback) {
    var xmlHttpReq = false;
    var self = this;
    // Xhr per Mozilla/Safari/Ie7
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // per tutte le altre versioni di IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    //self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            // Quando pronta, visualizzo la risposta del form
            updatepage(self.xmlHttpReq.responseText, responsediv, callback);
            if (document.getElementById(responsedivloader)) document.getElementById(responsedivloader).innerHTML = '&nbsp;';

        }
        else {
            // In attesa della risposta del form visualizzo il msg di attesa			
            if (responsedivloader.length == '') {
                updatepage(responsemsg, responsediv, null);
            } else {
                updatepage(responsemsg, responsedivloader, null);
            }
        }
    }
    self.xmlHttpReq.send(getquerystring(formname));
}

