﻿var myRequest = null;
var MustReloadPage = false;
var myResultsDestination = null;
var myLoadingBox = null;

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

 
 
function ShowLoading(TargetElement){

setTimeout(document.getElementById(TargetElement).innerHTML="<div id="+TargetElement+" class=divResultBox><img src=Images/loading.gif></img></div>",20);

}


function HideLoading(TargetElement){
if (TargetElement==null){TargetElement='divLoading'};

document.getElementById(TargetElement).innerHTML="<div id="+TargetElement+"></div>";
}



function CreateXmlHttpReq(handler) {
  var xmlhttp = null;
  xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = handler;
  return xmlhttp;
}

function myHandler() {
    var HTMLResult;
    var HTMLErrorText;
    
    if (myRequest.readyState == 4)
        
        {  
           HideLoading(myLoadingBox);
           
           if (myRequest.status == 200)
            
                {
                    HTMLResult=myRequest.responseText;
                    if (Left(HTMLResult,4)=='*** ')
                        {if  (MustReloadPage)
                            {
                                HTMLErrorText = HTMLResult.substring(4);
                                HTMLErrorText = HTMLErrorText.replace("<br/>",String.fromCharCode(10));
                                HTMLErrorText = HTMLErrorText.replace("<br/>",String.fromCharCode(10));
                                HTMLErrorText = HTMLErrorText.replace("<br/>",String.fromCharCode(10));
                                
                                alert(HTMLErrorText);
                                MustReloadPage=false;
                            }
                         else
                            {
                                document.getElementById(myResultsDestination).innerHTML= HTMLResult.substring(4);
                            }
                        
                        }
                    else
                        {document.getElementById(myResultsDestination).innerHTML= HTMLResult;}                    
                    
                    if (MustReloadPage){ window.location.reload(); };
                    if (document.getElementById("cmdRegister")!=null)
                        {document.getElementById("cmdRegister").disabled=false;
                        };
                    if (document.getElementById("cmdLogin")!=null)
                        {document.getElementById("cmdLogin").disabled=false;
                        };
                    if (document.getElementById("cmdLogout")!=null)
                        {document.getElementById("cmdLogout").disabled=false;
                        };
                    
                    }
                    
                else
                {window.location="SiteNotAvailable.aspx";}
        }
        
    
}


function CreateXmlHttpReq2(handler) {
  var xmlhttp = null;
  try {
    xmlhttp = new XMLHttpRequest();
  } catch(e) {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  xmlhttp.onreadystatechange = handler;
  return xmlhttp;
}



function Dispatcher(IdCaller,IdLanguage,IdParameter)
{
var String01;
var String02;
var StringToSend;
var StringAppo;
var answer;
var NumberOfSubscriptions;

var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);


if (IdCaller == 1){
    myLoadingBox="divLoading";
    ShowLoading("divLoading");
    
    MustReloadPage=true;
    
    StringToSend = "Cod=LOGIN&Page="+sPage+"&";
    
    StringAppo = document.getElementById("txtLogin").value;
    StringAppo = StringAppo.replace("&","__*xyz*___");
    StringToSend = StringToSend + "login=" + encodeURI(StringAppo)+"&";
    
    StringAppo = document.getElementById("txtPassword").value;
    StringAppo = StringAppo.replace("&","__*xyz*___");
    StringToSend = StringToSend + "password=" + encodeURI(StringAppo)+"&";
    
    StringAppo = document.getElementById("cboRememberMe").value;
    StringAppo = StringAppo.replace("&","__*xyz*___");
    StringToSend = StringToSend + "IdRememberMe=" + encodeURI(StringAppo);

    StringToSend = StringToSend + "&Test=1"
    
    myResultsDestination="BoxLogin_Content";

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
    }

if (IdCaller == 2)
    {
    myLoadingBox="divLoading";
    ShowLoading("divLoading");    
    MustReloadPage=true;
    
    if (IdLanguage==1){
        answer = confirm ("Vuoi chiudere la sessione?")
        if (answer)
            {
            StringToSend = "Cod=LOGOUT&Page="+sPage+"&ln=1&Test=1";


            myResultsDestination="BoxLogin_Content";

            myRequest=CreateXmlHttpReq2(myHandler);
            myRequest.open("POST","AJAXFunctions.aspx");
            myRequest.send(StringToSend);
            return false;
            }
    else
        {
            document.getElementById("cmdLogout").disabled=false;
            HideLoading();
            }
    }
     

    if (IdLanguage==2)
        {
        answer = confirm ("Do you want to close your session?")
        if (answer)
            {
            StringToSend = "Cod=LOGOUT&Page="+sPage+"&ln=2&Test=1";


            myResultsDestination="BoxLogin_Content";

            myRequest=CreateXmlHttpReq2(myHandler);
            myRequest.open("POST","AJAXFunctions.aspx");
            myRequest.send(StringToSend);
            return false;
            }
        else
            {
                document.getElementById("cmdLogout").disabled=false;
                HideLoading();
            }
        }

    }


if (IdCaller == 3){
    myLoadingBox="divLoading";
    ShowLoading("divLoading");
    MustReloadPage=false;
    StringToSend = "Cod=CASECAT&Page="+sPage+"&IdParameter="+IdParameter+"&Test=1";
    myResultsDestination="BoxContent_ColRightDiv";

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
     
    }

if (IdCaller == 4){
    myLoadingBox="divLoading";
    ShowLoading("divLoading");
    MustReloadPage=false;
    StringToSend = "Cod=FAQ&Page="+sPage+"&IdParameter="+IdParameter+"&Test=1";
    myResultsDestination="BoxContent_ColRightDiv";

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
     
    }

if (IdCaller == 5){
    myLoadingBox="divLoading";
    ShowLoading("divLoading");
    MustReloadPage=false;
    StringToSend = "Cod=PAPER&Page="+sPage+"&IdParameter="+IdParameter+"&Test=1";
    myResultsDestination="BoxContent_ColRightDiv";

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
     
    }


if (IdCaller == 6){
    myLoadingBox="DivResult";
    ShowLoading("DivResult");
    MustReloadPage=false;
    StringToSend = "Cod=PASSWORD&Page="+sPage+"&";
    
    StringAppo = document.getElementById("ctl00_CPH_CONTENT_CONTENT_txtLogin").value;
    StringAppo = StringAppo.replace("&","__*xyz*___");
    StringToSend = StringToSend + "login=" + encodeURI(StringAppo)+"&";
    
    StringAppo = document.getElementById("ctl00_CPH_CONTENT_CONTENT_txtEmail").value;
    StringAppo = StringAppo.replace("&","__*xyz*___");
    StringToSend = StringToSend + "email=" + encodeURI(StringAppo)+"&";

    StringAppo = document.getElementById("ctl00_CPH_CONTENT_CONTENT_txtTaxCode").value;
    StringAppo = StringAppo.replace("&","__*xyz*___");
    StringToSend = StringToSend + "taxcode=" + encodeURI(StringAppo);

    StringToSend = StringToSend + "&Test=1"
    
    myResultsDestination="DivResult";

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
     
    }

if (IdCaller == 7){
    myLoadingBox="divLoading";
    ShowLoading("divLoading");

    MustReloadPage=false;
    document.getElementById("DivResult").value='';
    StringToSend = "Cod=NEWCUSTOMER&Page="+sPage+"&IdParameter="+IdParameter+"&ln="+IdLanguage+"&Test=1";
    myResultsDestination="BoxContent_ColRightDiv";

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
     
    }
    
if (IdCaller == 8){
    myLoadingBox="DivResult";
    ShowLoading("DivResult");

    document.getElementById("DivResult").value='';
    document.getElementById("cmdRegister").disabled=true;
    if (document.getElementById("chkPrivacy").checked == false)
        {
            if (IdLanguage == 1)
                {   alert("Devi accettare le condizioni della privacy per poter procedere con la registrazione.");
                    document.getElementById("cmdRegister").disabled=false;
                    HideLoading();
                    return false;
                }
            if (IdLanguage == 2)
                {   alert("You must accept privacy terms to proceed with subscription.");
                    document.getElementById("cmdRegister").disabled=false;
                    HideLoading();
                    return false;
                }
        }

    if (document.getElementById("txtUserPassword").value != document.getElementById("txtPasswordVerify").value)
        {
            if (IdLanguage == 1)
                {   alert("La verifica della password è fallita.");
                    document.getElementById("cmdRegister").disabled=false;
                    HideLoading();
                    return false;
                }
            if (IdLanguage == 2)
                {   alert("Password verification failed.");
                    document.getElementById("cmdRegister").disabled=false;
                    HideLoading();
                    return false;
                }
        }

    MustReloadPage=false;
    StringToSend = ''
    
    StringAppo = document.getElementById("txtSurname").value;
    StringAppo = StringAppo.replace("&","__*xyz*___");
    StringToSend = StringToSend + "surname=" + encodeURI(StringAppo);

    if (document.getElementById("txtName")!=null)
        {
        StringAppo = document.getElementById("txtName").value;
        StringAppo = StringAppo.replace("&","__*xyz*___");
        StringToSend = StringToSend + "&name=" + encodeURI(StringAppo);
        }

    if (document.getElementById("cboGender")!=null)
        {
        StringAppo = document.getElementById("cboGender").value;
        StringAppo = StringAppo.replace("&","__*xyz*___");
        StringToSend = StringToSend + "&idgender=" + encodeURI(StringAppo);
        }
    else 
        {
        StringToSend = StringToSend + "&idgender=0";
        }

    if (document.getElementById("txtTaxCode")!=null)
        {
        StringAppo = document.getElementById("txtTaxCode").value;
        StringAppo = StringAppo.replace("&","__*xyz*___");
        StringToSend = StringToSend + "&taxcode=" + encodeURI(StringAppo);
        }

    if (document.getElementById("txtTelephone")!=null)
        {
        StringAppo = document.getElementById("txtTelephone").value;
        StringAppo = StringAppo.replace("&","__*xyz*___");
        StringToSend = StringToSend + "&telephone=" + encodeURI(StringAppo);
        }

    if (document.getElementById("txtBirthDate")!=null)
        {
        StringAppo = document.getElementById("txtBirthDate").value;
        StringAppo = StringAppo.replace("&","__*xyz*___");
        StringToSend = StringToSend + "&birthdate=" + encodeURI(StringAppo);
        }

    if (document.getElementById("txtParent_Login")!=null)
        {
        StringAppo = document.getElementById("txtParent_Login").value;
        StringAppo = StringAppo.replace("&","__*xyz*___");
        StringToSend = StringToSend + "&parentlogin=" + encodeURI(StringAppo);
        }

    if (document.getElementById("txtParent_Password")!=null)
        {
        StringAppo = document.getElementById("txtParent_Password").value;
        StringAppo = StringAppo.replace("&","__*xyz*___");
        StringToSend = StringToSend + "&parentpassword=" + encodeURI(StringAppo);
        }

    StringAppo = document.getElementById("txtEmail").value;
    StringAppo = StringAppo.replace("&","__*xyz*___");
    StringToSend = StringToSend + "&email=" + encodeURI(StringAppo);

    if (document.getElementById("txtUserLogin")!=null)
        {
        StringAppo = document.getElementById("txtUserLogin").value;
        StringAppo = StringAppo.replace("&","__*xyz*___");
        StringToSend = StringToSend + "&login=" + encodeURI(StringAppo);
        }

    if (document.getElementById("txtOldPassword")!=null)
        {
        StringAppo = document.getElementById("txtOldPassword").value;
        StringAppo = StringAppo.replace("&","__*xyz*___");
        StringToSend = StringToSend + "&oldpassword=" + encodeURI(StringAppo);
        }

    StringAppo = document.getElementById("txtUserPassword").value;
    StringAppo = StringAppo.replace("&","__*xyz*___");
    StringToSend = StringToSend + "&newpassword=" + encodeURI(StringAppo);

    StringAppo = document.getElementById("cboNewsletter").value;
    StringAppo = StringAppo.replace("&","__*xyz*___");
    StringToSend = StringToSend + "&idnewsletter=" + encodeURI(StringAppo);




    StringToSend = "Cod=REGISTER&Page=" + sPage + "&ln=" + IdLanguage + "&"+StringToSend + "&Test=1";
    myResultsDestination="DivResult";

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
     
    }


if (IdCaller == 9){
    myLoadingBox="DivResult"+IdParameter;
    ShowLoading("DivResult"+IdParameter);

    MustReloadPage=true;
    document.getElementById("DivResult"+IdParameter).value='';
    StringToSend = "Cod=ACTIVATEAPP&Page="+sPage+"&IdParameter="+IdParameter+"&ln="+IdLanguage+"&Test=1";
    myResultsDestination="DivResult"+IdParameter;

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
     
    }

if (IdCaller == 10){
    myLoadingBox="DivResult"+IdParameter;
    ShowLoading("DivResult"+IdParameter);
    NumberOfSubscriptions = document.getElementById("cboSubscriptions"+IdParameter).value;

    MustReloadPage=false;
    document.getElementById("DivResult"+IdParameter).value='';
    StringToSend = "Cod=SUBSCRIBECOURSE&Page="+sPage+"&IdParameter="+IdParameter+"&ln="+IdLanguage+"&subscriptions="+NumberOfSubscriptions+"&Test=1";
    myResultsDestination="DivResult"+IdParameter;

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
     
    }

if (IdCaller == 11){
    myLoadingBox="DivResultCourse"+IdParameter;
    ShowLoading("DivResultCourse"+IdParameter);

    MustReloadPage=true;
    document.getElementById("DivResultCourse"+IdParameter).value='';
    StringToSend = "Cod=UNSUBSCRIBECOURSE&Page="+sPage+"&IdParameter="+IdParameter+"&ln="+IdLanguage+"&Test=1";
    myResultsDestination="DivResultCourse"+IdParameter;

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
     
    }

if (IdCaller == 12){
    myLoadingBox="divLoading";
    ShowLoading("divLoading");
    MustReloadPage=false;
    StringToSend = "Cod=SCREENS&Page="+sPage+"&IdParameter="+IdParameter+"&Test=1";
    myResultsDestination="BoxContent_ColRightDiv";

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
     
    }

if (IdCaller == 14){
    myLoadingBox="divLoading";
    ShowLoading("divLoading");
    MustReloadPage=false;
    StringToSend = "Cod=SERVICE&Page="+sPage+"&IdParameter="+IdParameter+"&Test=1";
    myResultsDestination="BoxContent_ColRightDiv";

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
     
    }

if (IdCaller == 15){
    myLoadingBox="divLoading";
    ShowLoading("divLoading");
    MustReloadPage=false;
    StringToSend = "Cod=SERVICESEL&Page="+sPage+"&IdParameter="+IdParameter+"&Test=1";
    myResultsDestination="BoxContent_ColRightDiv";

    myRequest=CreateXmlHttpReq2(myHandler);
    myRequest.open("POST","AJAXFunctions.aspx");
    myRequest.send(StringToSend);
    return false;
     
    }


}


