


/////////////////////////// AJAX FUNCTIONS //////////////////////////////


///////////////////// Public Variables  ////////////////////////

 var errorRetryInterval = 10;
 // Ajax Master Array Variables //
 var xmlHttp = new Array();
 var xmlHttpPhpUrl = new Array();
 var xmlHttpresponse = new Array();
 var xmlHttpReturnType = new Array();
 var xmlHttpDisplayFunction = new Array();
 var debugmode = 1;



////////////////////// Error Handling //////////////////////////

function displayError($message,functionName)
{
   alert("Error!! Will retry in " +
   errorRetryInterval + "Seconds." +
   (debugmode ? "<br />" + $message : ""));
   if (functionName != 'none')
   {
      setTimeout("" + functionName + "();", errorRetryInterval * 1000);
   }
}

////////////////////// Ajax Functions //////////////////////////

function createXMLHttpRequestObject(xmlHttpId)
{
 try {
 	xmlHttp[xmlHttpId] = new XMLHttpRequest();
 } 
 catch (e) {
 
 	var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0", 
																	"MSXML2.XMLHTTP.5.0", 
																	"MSXML2.XMLHTTP.4.0", 
																	"MSXML2.XMLHTTP.3.0", 
																	"MSXML2.XMLHTTP", 
																	"Microsoft.XMLHTTP");
																	
 	for (var i = 0; i < xmlHttpVersions.length && !xmlHttp[xmlHttpId]; i++) {
 		try {
 			xmlHttp[xmlHttpId] = new ActiveXObject(xmlHttpVersions[i]);
 		} 
 		catch (e) {
 		}
 	}
	
 }
      if (!xmlHttp[xmlHttpId]) {
         alert("Error creating the xmlHttpRequest Object" + e);
      }else{
         return xmlHttp[xmlHttpId];
      }
 }


  /////////////////// Internal Object Ajax Process //////////////////////

  // main call : processAjax('myphp.php?var1=foo', 'txt or xml', 'unique-Id', 'display-Function-Name');

 function processAjax(phpAddressStr, returnType, xmlHttpId, displayFunction)
  {
   xmlHttp[xmlHttpId] = createXMLHttpRequestObject(xmlHttpId);
   xmlHttpPhpUrl[xmlHttpId] = phpAddressStr;
   xmlHttpReturnType[xmlHttpId] = returnType;
   xmlHttpDisplayFunction[xmlHttpId] = displayFunction;
   

   if (xmlHttp[xmlHttpId])
   {
      try
      {
         xmlHttp[xmlHttpId].onreadystatechange = function(){processReturn(xmlHttpId);};
         xmlHttp[xmlHttpId].open("GET", xmlHttpPhpUrl[xmlHttpId], true);
         
         xmlHttp[xmlHttpId].send(null);
      }
      catch(e)
      {
         displayError(e.toString(),'none');
      }
   }
 }


 function processReturn(xmlHttpId)
 {

     if (xmlHttp[xmlHttpId].readyState == 4)
     {

          if (xmlHttp[xmlHttpId].status == 200)
          {
              //try
                // {
                         if(xmlHttpReturnType[xmlHttpId] == 'txt')
                        {
                           getResponseTxt(xmlHttpId);
                        }
                        else
                        {
                           getResponseXML(xmlHttpId);
                        }
                 //}
                // catch(e)
                 //{
                 //   displayError(e.toString(), 'none');
                 //}
          }
          else
          {
             displayError(xmlHttp[xmlHttpId].statusText, 'none');

          }
     }
     
 }


 function getResponseTxt(xmlHttpId){

  xmlHttpresponse[xmlHttpId] = xmlHttp[xmlHttpId].responseText;

  if (xmlHttpresponse[xmlHttpId].indexOf("ERRNO") >= 0
   || xmlHttpresponse[xmlHttpId].indexOf("error") >= 0
   || xmlHttpresponse[xmlHttpId].length == 0)
    throw(xmlHttpresponse[xmlHttpId].length == 0 ? "Response Server Error." : xmlHttpresponse[xmlHttpId]);

    var displayFunction = xmlHttpDisplayFunction[xmlHttpId] + '(' + xmlHttpId + ')';
    eval(displayFunction);
    clearAjaxArrays(xmlHttpId);
 }


 function getResponseXML(xmlHttpId){

  xmlHttpresponse[xmlHttpId] = xmlHttp[xmlHttpId].responseXML;

 // if (xmlHttpresponse[xmlHttpId].indexOf("ERRNO") >= 0
  // || xmlHttpresponse[xmlHttpId].indexOf("error") >= 0
  // || xmlHttpresponse[xmlHttpId].length == 0)
  //  throw(xmlHttpresponse[xmlHttpId].length == 0 ? "Response Server Error." : xmlHttpresponse[xmlHttpId]);

    var displayFunction = xmlHttpDisplayFunction[xmlHttpId] + '(' + xmlHttpId + ')';
    eval(displayFunction);

    clearAjaxArrays(xmlHttpId);
 }


 function clearAjaxArrays(xmlHttpId){
      // clean up arrays to stop memory leaks //
      delete xmlHttp[xmlHttpId];
      delete xmlHttpPhpUrl[xmlHttpId];
      delete xmlHttpresponse[xmlHttpId];
      delete xmlHttpReturnType[xmlHttpId];
      delete xmlHttpDisplayFunction[xmlHttpId];
 }

 ///////////////////////// End Internal Ajax Processes /////////////////////////





///////////////////////////////////////////////////////////////////////////////
/////                                                                     /////
/////           START ALL OF THE AJAX ROUTINES FROM HERE                  /////
/////                                                                     /////
///////////////////////////////////////////////////////////////////////////////



//// <---------------  Load Calendar Views in Property Pages ---------------> ////


function loadCal(id,month,year,x){
          
      var calendarPage = "/scripts/availability.php";
      var randomNumb = Math.floor(Math.random()*1000);
      var getUrl = calendarPage + "?id=" + id + "&month=" + month + "&year=" + year + "&x=" + x;
      
      processAjax(getUrl, 'txt', randomNumb, 'getCalendarDetails');
}

function getCalendarDetails(xmlHttpId){

    var responseTxt  = xmlHttpresponse[xmlHttpId];
    var container = document.getElementById("calendar_wrapper");
	  container.innerHTML = responseTxt;
		container.style.zIndex = '1';
}

// <-------------------------------------------------------------------------> //


















////-------------------------- HELPERS -------------------------///

 function testForObject(Id, Tag){
  var o = document.getElementById(Id);
  if (o){
    if (Tag){
      if (o.tagName.toLowerCase() == Tag.toLowerCase()){
        return o;
      }
    }else{
      return o;
    }
  }
  return null;
}
 
 