/***************************/
function RateSeller(ID,vote){
   var url="ajax.pl?nocache="+Date()+"&action=RecordSellerVote&ID="+ID+"&vote="+vote;
   httpRequest("GET",url,true);
}


/***************************/
function CommentSeller(ID,comment){

   var url="ajax.pl?nocache="+Date()+"&action=RecordSellerComment&ID="+ID+"&comment="+comment;
   httpRequest("GET",url,true);

   var mySplitResult = ID.split("_");
   var ownerID = mySplitResult[1];
   document.getElementById('form_'+ownerID).style.display='none'; 
   document.getElementById('thank_you_'+ownerID).style.display='inline';

}



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



/***************************/
function RecordPropertyActivity(pid,type,factor){

   if (! pid || ! type){
      return;
   }

   var url="ajax.pl?nocache="+Date()+"&action=RecordPropertyActivity&pid="+pid+"&type="+type+"&factor="+factor;

   httpRequest("GET",url,true);

}


/***************************/
function ChangePropertyStatus(action2,nline){

   if (! action2){
      return;
   }

   var url="ajax.pl?nocache="+Date()+"&action=ChangePropertyStatus&action2="+action2;

   httpRequest("GET",url,true);

}


/***************************/
function DeleteEmailAlert(alertID){

   if (! alertID){
      return;
   }

   var url="ajax.pl?nocache="+Date()+"&action=DeleteEmailAlert&alertID="+alertID;

   httpRequest("GET",url,true);


}


/***************************/
function ProgressBar(file){

   if (! file){
      return;
   }

   //var url="ajax.pl?nocache="+Date()+"&action=ProgressBar&file="+file;

   httpRequest("GET",url,true);

}



/***************************/
function cityLookup(zip){

   if (! zip){
      return;
   }

   var url="ajax.pl?nocache="+Date()+"&action=cityLookup&zip="+escape(zip);

   httpRequest("GET",url,true);

}



/***************************/
function getLatLong(city){

   if (! city){
      return;
   }

   var url="ajax.pl?nocache="+Date()+"&action=getLatLong&city="+escape(escape(city));

   httpRequest("GET",url,true);
}


/***************************/
function Alert_Translate(text1,text2){
   if (! text1){
      return;
   }

   if (text2){  
      var url="ajax.pl?nocache="+Date()+"&action=translate&text1="+escape(text1)+"&text2="+escape(text2);
   }
   else {
      var url="ajax.pl?nocache="+Date()+"&action=translate&text1="+escape(text1);
   }

   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 == "CheckAdInstallation"){

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

               if (response[1] != "OK" ){
                  alert (response[4]);  
                  document.getElementById('response_short').innerHTML="<span class=\"color1\" style=\"font-weight:bold\">"+response[2]+"</span>";
                  document.getElementById('response').innerHTML="<span class=\"color1\" style=\"font-weight:bold\">"+response[3]+"</span>"; 

                  return;
               }

 
               document.getElementById('response_short').innerHTML="<span style=\"color:#04C01A; font-weight:bold;\">"+response[2]+"</span>";
               document.getElementById('response').innerHTML="<p style=\"color:#04C01A; font-weight:bold;\">"+response[3]+"</p>";

               return;

            }


            else if (CallingFunction == "ChangePropertyStatus"){
               var status = response[1];
               if (status != 'ok'){
                  alert(status);
                  return;
               }
               else {
                  var id = 'status_'+response[3];
                  if (response[2] == 'hold'){
                     alert(ConvertFromHTMLref(response[5]));
                     document.getElementById(id).innerHTML = response[4];
                  }
                  else if (response[2] == 'sold'){
                     alert(ConvertFromHTMLref(response[5]));
                     document.getElementById(id).innerHTML = response[4];
                  }
                  else if (response[2] == 'ok'){
                     alert(ConvertFromHTMLref(response[5]));
                     document.getElementById(id).innerHTML = response[4];
                  }
               }
               return;
            }

            if (CallingFunction == "RecordPropertyActivity"){
               return;
            }

            if (CallingFunction == "RecordSellerVote"){
               return;
            }

            if (CallingFunction == "RecordSellerComment"){
               return;
            }

            if (CallingFunction == "DeleteEmailAlert"){

               var alertID = response[1];
               //alert (alertID);
               document.getElementById('n_alerts').innerHTML = document.getElementById('n_alerts').innerHTML - 1;
               document.getElementById(alertID).style.display='none'; 

               return;

            }


            if (CallingFunction == "ProgressBar"){

               var count = response[1];
               var file = response[2];
      //         document.getElementById('progress_bar').innerHTML = count+' %';

               return;

            }

            if (CallingFunction == "cityLookup"){

               var error_message = response[1];

               if (error_message){
                  alert(ConvertFromHTMLref(error_message));

                  var HTML ='<select name="city" style="width:100%; font-size:1em; border:1px solid #CCCCCC; background:#FFFFFF; color:#333333;"><option value=""></option></select>';
                  document.getElementById('city_input').innerHTML = HTML;

                  return;
               }

               var cities = response[2];
               var cities = cities.split("|");

               var HTML ='<select name="city" style="width:100%; font-size:1em; border:1px solid #CCCCCC; background:#FFFFFF; color:#333333;" onChange="update_city_address(document.form1);">';

               for (i=0; i<cities.length; i++){
                  HTML = HTML +'<option value="'+cities[i]+'">'+cities[i]+'</option>';
               }

               HTML = HTML+'</select>';

               document.getElementById('city_input').innerHTML = HTML;
               update_city_address(document.form1);

               return;

            }


            if (CallingFunction == "getLatLong"){

               var lat = response[1];
               var long = response[2];
               var city = response[3];
               var zip = response[4];

               if (! lat || ! long || lat == "0" || long == "0"){
                  document.getElementById('locator').style.top = '0px';
                  document.getElementById('locator').style.right = '0px';
                  return;
               }


               lat_dot = parseInt((51.0871-lat) * 175/8.6517 +0.5) - 10;
            //   long_dot = parseInt((9.5636-long) * 193/14.3577 +0.5) + 10;
               long_dot = parseInt((9.5636-long) * 193/13.5 +0.5) + 0;

               new Effect.Move(document.getElementById('locator'), { x: 193-long_dot, y: lat_dot, mode: 'absolute'}); 

              document.getElementById('locator').innerHTML = '<a href="javascript:void(0);" onClick="javascript:help_open(\'locate.pl?lat='+lat+'&long='+long+'&city='+city+'&zip='+zip+'\',\'800\',\'600\')"><img src="map_marker_grey.gif" width="20" height="20" border="0" id="map_marker" onMouseOver="javascript:document.getElementById(\'map_marker\').src=\'map_marker2_grey.gif\'" onMouseOut="javascript:document.getElementById(\'map_marker\').src=\'map_marker_grey.gif\'"></a>';   


               return;

            }

            if (CallingFunction == "Alert_Translate"){
               response[1] = ConvertFromHTMLref(response[1]);  // custom_js.pl must be included in calling file for this to work

               if (response[2]){
                  response[2] = ConvertFromHTMLref(response[2]);  // custom_js.pl must be included in calling file for this to work
                  alert(response[1]+' '+response[2]);
               }
               else {
                  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); }

}







