function getXmlHttpObject(){
  var xmlHttp;
  try {
      // Firefox, Opera 8.0+, Safari, IE 7+
      xmlHttp = new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer 6 or older
    try
    {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e)
                    {
                        alert("Your browser does not support AJAX!");
                        return false;
                    }
         }
     }
     return xmlHttp;
}

function callAndFill(xmlHttp,url,elementToFill,outputType){
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4){
          if (xmlHttp.status==200) {
                if (outputType == "innerHTML"){
                    elementToFill.innerHTML = xmlHttp.responseText;
                }
                else if (outputType == "value"){
                    elementToFill.value =  xmlHttp.responseText;
                }
          } else {
              alert("Wrong AJAX execution \n Status: " + xmlHttp.status + '\n Status text:' + xmlHttp.statusText);
          }
      }
    }
    var calledScript;
    var stamp = new Date();
    calledScript = url + '&executionTimeToPreventCaching=' + stamp.getYear()+stamp.getMonth()+stamp.getDay()+stamp.getHours()+stamp.getMinutes()+stamp.getSeconds();
    // alert(calledScript);
    xmlHttp.open('GET',calledScript,true);
    xmlHttp.send(null);
    return xmlHttp.responseText;
}

function sendRequest() {
            var oForm = document.forms[0];
            var sBody = getRequestBody(oForm);

            var oXHR = zXmlHttp.createRequest();
            oXHR.open("post", oForm.action, true);
            oXHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

            oXHR.onreadystatechange = function () {
                if (oXHR.readyState == 4) {
                    if (oXHR.status == 200) {
                        saveResult(oXHR.responseText);
                    } else {
                        saveResult("An error occurred: " + oXHR.statusText);
                    }
                }
            };
            oXHR.send(sBody);
}

function encodeNameAndValue(sName, sValue) {
            var sParam = encodeURIComponent(sName);
            sParam += "=";
            sParam += encodeURIComponent(sValue);
            return sParam;
}

function getRequestBody(oForm) {

            //array to hold the params
            var aParams = new Array();

            //get your reference to the form
            //var oForm = document.forms[0];

            //iterate over each element in the form
            for (var i=0 ; i < oForm.elements.length; i++) {

                //get reference to the field
                var oField = oForm.elements[i];

                //different behavior based on the type of field
                switch (oField.type) {

                    //buttons - we don't care
                    case "button":
                    case "submit":
                    case "reset":
                             break;

                    //checkboxes/radio buttons - only return the value if the control is checked.
                    case "checkbox":
                    case "radio":
                        if (!oField.checked) {
                            break;
                        } //End: if

                    //text/hidden/password all return the value
                    case "text":
                    case "hidden":
                    case "password":
                        aParams.push(encodeNameAndValue(oField.name, oField.value));
                        break;

                    //everything else
                    default:

                        switch(oField.tagName.toLowerCase()) {
                            case "select":
                                aParams.push(encodeNameAndValue(oField.name, oField.options[oField.selectedIndex].value));
                                break;
                            default:
                                aParams.push(encodeNameAndValue(oField.name, oField.value));
                        }
                }

            }

            return aParams.join("&");
        }

function saveResult(sMessage) {
            var divStatus = document.getElementById("divStatus");
            divStatus.innerHTML = "Request completed: " + sMessage;
}

function submitForm(oXml,formId,onSuccess,onFailure){
    var oForm = getEl(formId);
    var params = getRequestBody(oForm);
    var type = 'POST';
    var stamp = new Date();
    var url = '';
   if (oForm.action.indexOf('?')==-1){
        url = oForm.action + '?' + 'executionTimeToPreventCaching=' + stamp.getYear()+stamp.getMonth()+stamp.getDay()+stamp.getHours()+stamp.getMinutes()+stamp.getSeconds();
    } else {
        url = oForm.action + '&' + 'executionTimeToPreventCaching=' + stamp.getYear()+stamp.getMonth()+stamp.getDay()+stamp.getHours()+stamp.getMinutes()+stamp.getSeconds();
    }
    oXml.open(type, url, true);
    oXml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    // alert('Connection opened and header set');

    oXml.onreadystatechange = function () {
        if (oXml.readyState == 4) {
            if (oXml.status == 200) {
                onSuccess();
            } else {
                onFailure();
            }
        }
    };
    // alert(url);
    // alert(params);
    oXml.send(params);
}

function addTimeToUrl(url){
    var stamp = new Date();
    if (url.indexOf('?')==-1){
        url += '?' + 'timeToPreventCaching=' + stamp.getYear()+stamp.getMonth()+stamp.getDay()+stamp.getHours()+stamp.getMinutes()+stamp.getSeconds();
    } else {
        url += '&' + 'timeToPreventCaching=' + stamp.getYear()+stamp.getMonth()+stamp.getDay()+stamp.getHours()+stamp.getMinutes()+stamp.getSeconds();
    }
    return url;
}

function ajaxPost(xmlHttp,url,params,onSuccess,onFailure){
        url = addTimeToUrl(url);
        xmlHttp.open("POST", url, true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.onreadystatechange = function() {
            if(xmlHttp.readyState == 4){
                    if (xmlHttp.status == 200) {
                        onSuccess();
                    } else {
                        onFailure();
                    }
            } 
        }
        xmlHttp.send(params);
}

function ajaxGet(xmlHttp,url,onSuccess,onFailure){
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4){
          if (xmlHttp.status==200) {
                onSuccess();
          } else {
                onFailure();
          }
      }
    }
    url = addTimeToUrl(url);
    xmlHttp.open('GET',url,true);
    xmlHttp.send(null);
}

function ajaxLoad(xmlHttp,url,element,outputType){
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4){
          if (xmlHttp.status==200) {
                if (outputType == "innerHTML"){
                    element.innerHTML = xmlHttp.responseText;
                }
                else if (outputType == "value"){
                    element.value =  xmlHttp.responseText;
                }
          } else {
              alert("Wrong AJAX execution \n Status: " + xmlHttp.status + '\n Status text:' + xmlHttp.statusText);
          }
      }
    }
    url = addTimeToUrl(url);
    // alert(calledScript);
    xmlHttp.open('GET',url,true);
    xmlHttp.send(null);
}
