/***************************/
function CheckAdInstallation(url,userID,password,mode){
   var url="checkAdInstallation.pl?nocache="+Date()+"&url="+escape(url)+"&userID="+userID+"&password="+password+"&mode="+mode;
   httpRequest("GET",url,true);
}


/***************************/
function CheckCodeInstallation(url,alias,db,n,user,site){
   var url="check_code.pl?nocache="+Date()+"&url="+escape(url)+"&alias="+alias+"&db="+db+"&n="+n+"&user="+user+"&site="+site;
   httpRequest("GET",url,true);
}

/***************************/
function CheckUserIdEligibleForAd(userID){
   if (! userID){
      return;
   }
   var url="ajax.pl?nocache="+Date()+"&action=CheckUserIdEligibleForAd&user="+userID;
   httpRequest("GET",url,true);
}


/***************************/
function GetHTML(url){
   httpRequest("GET",'www.web-stat.com/faq.pl',true);
}




/***************************/
function CheckAlpha(object,var_name){
   if (object.value && (! object.value.match(/^\w+$/))){
      alert (var_name+" can only contain letters, numbers or underscores");
      object.focus(); 
   }
}


/***************************/
function CheckURL(object,var_name){
//   if (object.value && object.value != "http://" && (! object.value.match(/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/))){
//      alert (object.value+" is not a valid URL");
//      object.focus(); 
//   }
}

/***************************/
function CheckEmail(object){
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = object.value;
   if(address && reg.test(address) == false) {
      alert ("The email address you entered is invalid. An email address should be in the format xxx@xxx.xx Please double-check.");
      object.focus(); 
   }
}


/***************************/
function CheckUserID(userID){
   if (! userID){
      return;
   }
   var url="ajax.pl?nocache="+Date()+"&action=CheckUserID&user="+userID;
   httpRequest("GET",url,true);
}



/***************************/
function CheckUserIDExists(userID){
   if (! userID){
      return;
   }
   var url="ajax.pl?nocache="+Date()+"&action=CheckUserIDExists&user="+userID;
   httpRequest("GET",url,true);
}


/***************************/
function CheckPassword(password,userID){

  if (! password || ! userID){
     return;
  }

   var url="ajax.pl?nocache="+Date()+"&action=CheckPassword&user="+userID+"&password="+password;
   httpRequest("GET",url,true);
}


/***************************/
function RemoveReportFromFavs(report,userID){
   var url="ajax.pl?nocache="+Date()+"&action=RemoveReportFromFavs&user="+userID+"&report="+report;
   httpRequest("GET",url,true);
}


/***************************/
function AddReportToFavs(report,userID){
   var url="ajax.pl?test=2&nocache="+Date()+"&action=AddReportToFavs&user="+userID+"&report="+report;
   httpRequest("GET",url,true);
}








/***************************************
   Wrapper function for constructing a Request object.
   Parameters:
   reqType: The HTTP request type such as GET or POST.
   url: The URL of the server program.
   asynch: Whether to send the request asynchronously or not.

/***************************************/
function httpRequest(reqType,url,asynch){

    //Mozilla-based browsers
    if(window.XMLHttpRequest){
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject){
        request=new ActiveXObject("Msxml2.XMLHTTP");
        if (! request){
            request=new ActiveXObject("Microsoft.XMLHTTP");
        }
     }
    //the request could still be null if neither ActiveXObject
    //initializations succeeded
    if(request){
       initReq(reqType,url,asynch);
    }  else {
        alert("Your browser does not permit the use of all of this application's features!");}
}



/*********************************/
function initReq(reqType,url,bool){
    try{
        /* Specify the function that will handle the HTTP response */
        request.onreadystatechange=handleResponse;
        request.open(reqType,url,bool);
        request.send(null);
    } catch (err) {
        alert("The application cannot contact the server at the moment. Please try again in a few seconds. Send caused the prob.");
    }
}



/************************/
function handleResponse(){
   var statusMsg="";
   try{
      if(request.readyState == 4){
         if(request.status == 200){
            // here we get different responses, depending on what called this function

            try { 
               var response = request.responseText.split("::");
            } catch(e) { 
               alert('1 '+e);
               return;
            } 

            var CallingFunction = response[0];


            if (CallingFunction == "CheckCodeInstallation"){

               document.getElementById('submit_button1').style.display='inline'; 
               document.getElementById('spinner1').style.display='none';
               document.getElementById('all_ok1').style.display='none';
               document.getElementById('response_short1').innerHTML = '';
               document.getElementById('response1').innerHTML = '';

               try {
               document.getElementById('submit_button2').style.display='inline'; 
               document.getElementById('spinner2').style.display='none';
               document.getElementById('all_ok2').style.display='none';
               document.getElementById('response_short2').innerHTML = '';
               document.getElementById('response2').innerHTML = '';

               document.getElementById('submit_button3').style.display='inline'; 
               document.getElementById('spinner3').style.display='none';
               document.getElementById('all_ok3').style.display='none';
               document.getElementById('response_short3').innerHTML = '';
               document.getElementById('response3').innerHTML = '';
               } catch (err)   {}


               if (response[1] == "no_occurrence"){
                  alert (response[4]);  
                  document.getElementById('response_short'+response[2]).innerHTML="<span style=\"font-weight:bold; color:#FF0000\">"+response[3]+"</span>";
                  document.getElementById('response'+response[2]).innerHTML="<div style=\"border:3px dashed #FF0000; padding:10px;\"><span style=\"font-weight:bold; color:#FF0000\">"+response[4]+"</span><p>"+response[5]+"</p></div>"; 

                  return;
               }

               else if (response[1] == "empty_url"){
                  alert (response[4]);  
                  document.getElementById('response_short'+response[2]).innerHTML="<span style=\"font-weight:bold; color:#FF0000\">"+response[3]+"</span>";
                  document.getElementById('response'+response[2]).innerHTML="<div style=\"border:3px dashed #FF0000; padding:10px;\"><span style=\"font-weight:bold; color:#FF0000\">"+response[4]+"</span></div>"; 

                  return;
               }

               else if (response[1] == "invalid_url"){
                  alert (response[4]);  
                  document.getElementById('response_short'+response[2]).innerHTML="<span style=\"font-weight:bold; color:#FF0000\">"+response[3]+"</span>";
                  document.getElementById('response'+response[2]).innerHTML="<div style=\"border:3px dashed #FF0000; padding:10px;\"><span style=\"font-weight:bold; color:#FF0000\">"+response[4]+"</span></div>"; 

                  return;
               }

               else if (response[1] == "timeout"){
                  alert (response[4]); 
                  document.getElementById('response_short'+response[2]).innerHTML="<span style=\"font-weight:bold; color:#FF0000\">"+response[3]+"</span>";
                  document.getElementById('response'+response[2]).innerHTML="<div style=\"border:3px dashed #FF0000; padding:10px;\"><span style=\"font-weight:bold; color:#FF0000\">"+response[4]+"</span></div>"; 

                  return;
               }
               else if (response[1] == "no_url"){
                  alert (response[4]+"\n\n"+response[5]);  
                  document.getElementById('response_short'+response[2]).innerHTML="<span style=\"font-weight:bold; color:#FF0000\">"+response[3]+"</span>";
                  document.getElementById('response'+response[2]).innerHTML="<div style=\"border:3px dashed #FF0000; padding:10px;\"><span style=\"font-weight:bold; color:#FF0000\">"+response[4]+"</span><p>"+response[5]+"</p></div>"; 

                  return;
               }
               else if (response[1] == "no_match"){
                  alert (response[4]);  
                  document.getElementById('response_short'+response[2]).innerHTML="<span style=\"font-weight:bold; color:#FF0000\">"+response[3]+"</span>";
                  document.getElementById('response'+response[2]).innerHTML="<div style=\"border:3px dashed #FF0000; padding:10px;\"><span style=\"font-weight:bold; color:#FF0000\">"+response[4]+"</span><p>"+response[5]+"</p></div>"; 

                  return;
               }

               else {
                  document.getElementById('response_short'+response[1]).innerHTML="<span style=\"color:#04C01A; font-weight:bold;\">Code OK</span>";
                  document.getElementById('all_ok'+response[1]).style.display='block';

                  return;
               }

            }



            else if (CallingFunction == "CheckAdInstallation"){

               document.getElementById('submit_button').style.display='inline'; 
               document.getElementById('spinner').style.display='none';

               if (response[1] == "no_user"){
                  alert (response[2]+" : there is no existing account with this user ID in the system. Please check the spelling and case.\n\nIf you can not remember your user ID, use the 'retrieve login info' link on this page."); 
                  document.getElementById('response_short').innerHTML="<span class=\"emphasize\">Error (see below)</span>";
                  document.getElementById('response').innerHTML="<span class=\"emphasize\">Error - please check User ID</span>"; 
                  return;
               }

               if (response[1] == "wrong_password"){
                  alert ("the password you entered is invalid."); 
                  document.getElementById('response_short').innerHTML="<span class=\"emphasize\">Error (see below)</span>";
                  document.getElementById('response').innerHTML="<span class=\"emphasize\">Error - the password you entered is invalid</span>"; 
                  return;
               }
               if (response[1] == "not_eligible"){
                  alert (response[2]+" : this account is not eligible (only free trials please).");  
                  document.getElementById('response_short').innerHTML="<span class=\"emphasize\">Error (see below)</span>";
                  document.getElementById('response').innerHTML="<span class=\"emphasize\">Error - account not eligible (only free trials please)</span>"; 
                  return;
               }
               if (response[1] == "already_free"){
                  alert (response[2]+" : this account is already free."); 
                  document.getElementById('response_short').innerHTML="<span class=\"emphasize\">Error (see below)</span>";
                  document.getElementById('response').innerHTML="<span class=\"emphasize\">Error - account already free</span>"; 
                  return;
               }

               if (response[1] == "invalid_url"){
                  alert (response[3]);  
                  document.getElementById('response_short').innerHTML="<span class=\"emphasize\">"+response[2]+"</span>";
                  document.getElementById('response').innerHTML="<span class=\"emphasize\">"+response[3]+"</span>"; 

                  return;
               }

               if (response[1] == "query_string"){
                  alert ("Error : please use a URL with no query string");  
                  document.getElementById('response_short').innerHTML="<span class=\"emphasize\">Error (see below)</span>";
                  document.getElementById('response').innerHTML="<span class=\"emphasize\">Error - no query string (?) in URL please</span>"; 
                  return;
               }
               if (response[1] == "myspace"){
                  alert ("Error : not compatible with Myspace");  
                  document.getElementById('response_short').innerHTML="<span class=\"emphasize\">Error (see below)</span>";
                  document.getElementById('response').innerHTML="<span class=\"emphasize\">Error - unfortunately Myspace will not let you copy the advert 'as is', and as a result our validator can not check your installation</span>"; 
                  return;
               }
               if (response[1] == "timeout"){
                  alert ("Error : timeout, the URL did not respond. Please verify and validate again.");  
                  document.getElementById('response_short').innerHTML="<span class=\"emphasize\">Error (see below)</span>";
                  document.getElementById('response').innerHTML="<span class=\"emphasize\">Error - timeout</span>"; 
                  return;
               }
               if (response[1] == "no_url"){
                  alert ("Error : your server responded with an error. Please verify the URL and validate again. Here is the error detail.\n\n"+response[2]);  
                  document.getElementById('response_short').innerHTML="<span class=\"emphasize\">Error (see below)</span>";
                  document.getElementById('response').innerHTML="<span class=\"emphasize\">Error - server returned "+response[2]+"</span>"; 
                  return;
               }
               if (response[1] == "no_match"){
                  alert ("Error : the advertisement was not found on the URL you sepcified or the code was not identical to what we issued. Please double-check your installation and try again.");  
                  document.getElementById('response_short').innerHTML="<span class=\"emphasize\">Error (see below)</span>";
                  document.getElementById('response').innerHTML="<p class=\"emphasize\">Error - advert code not found on page or not identical to code above.</p><p>Please go to your URL and look at the source code (right-click on any text portion of the page - ie not on an image - and select 'view source' to see your page's source code) and make sure that the code we issued has not been modified in any way. If you need help with this, please use the 'contact us' link at the bottom of this page."; 
                  return;
               }
               if (response[1] == "url_already_used"){
                  alert ("Error : there is already an advert on this URL for account '"+response[2]+"'. You can only use one free account for a given site.");
                  document.getElementById('response_short').innerHTML="<span class=\"emphasize\">Error (see below)</span>";
                  document.getElementById('response').innerHTML="<span class=\"emphasize\">Error - URL already used for free account '"+response[2]+"'</span>"; 
                  return;
               }

               document.getElementById('response_short').innerHTML="<span style=\"color:#04C01A; font-weight:bold;\">Code validated (see below)</span>";

               document.getElementById('response').innerHTML="<p style=\"color:#04C01A; font-weight:bold;\">OK - the advert was found on "+response[1]+" and your account is now running for free.</p><p>Note that we check every night that the advert is still on your page. If it is not found you will get two warning messages. Following this, your account will revert to 'expired free trial' status</p>";

               return;

            }

            else if (CallingFunction == "CheckUserID"){
               if (response[1] != ""){
                  alert (response[2]+" : This user ID is already in use on our system. Please choose a different user ID");  
                  // document.form1.user.focus(); 
               }
               return;
            }

            else if (CallingFunction == "CheckUserIDExists"){
               if (response[1] == ""){
                  alert (response[2]+" : there is no existing account with this user ID in the system. Please check the spelling and case.\n\nIf you can not remember your user ID, use the 'forgot login' link on the stats login page and we will email it to you.");  
                 // document.form1.existing_userID.focus(); 
               }
               return;
            }
            else if (CallingFunction == "CheckUserIdEligibleForAd"){
               if (response[1] == ""){
                  alert (response[2]+" : there is no existing account with this user ID in the system. Please check the spelling and case.\n\nIf you can not remember your user ID, use the 'retrieve login info' link on this page.");  
                 // document.form1.existing_userID.focus(); 
               }
               if (response[1] == "x"){
                  alert (response[2]+" : this account is not eligible (only free trials please).");  
               }
               return;
            }

            else if (CallingFunction == "CheckPassword"){
               if (response[1] != "Password OK"){
                  alert (response[1]);  
                  // document.form1.existing_password.focus(); 
               }
               return;
            }

            else if (CallingFunction == "RemoveReportFromFavs"){
               //   alert ("done"+response[1]+":"+response[2]+":"+response[3]); 
               return; 
            }

            else if (CallingFunction == "AddReportToFavs"){
               alert (response[1]);  
               return;
            }


             else {

               alert ("unknown function call 1 ("+CallingFunction +")");  
               return;

            }
            
            // end of functions list
         } 
         else {
            //alert("A problem occurred with communicating between the XMLHttpRequest object and the server program. ");
         }
      }//end outer if

   } catch (err)   {alert("It does not appear that the server is available for this application. Please try again very soon. Error: "+err.message); }

}


