// holds an instance of XMLHttpRequest
//var xmlHttp = createXmlHttpRequestObject();
// holds the remote server address and parameters
var tourAddress = "/transactions/includes/tour-trips.inc.php";
var tourWebsiteAddress = "/transactions/includes/tour-dates.inc.php";
var roomAddress = "/transactions/includes/rooms.inc.php";
var maxChildAddress = "/transactions/includes/maxchild.inc.php";
// variables that establish how often to access the server 
var updateInterval = 5; // how many seconds to wait to get new message
var errorRetryInterval = 30; // seconds to wait after server error
// when set to true, display detailed error messages
var debugMode = true;

var currentSelectBox = '';
var currentEditBox = '';
var currentVal = '';
var currentCount = 0;
 
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

/******************* trips *************************/
function showTrips(selectBox, t, tt, defaultValue, childEditBox, defChildCount, weekendTour)
{
  xmlHttpTour = createXmlHttpRequestObject();
  if (xmlHttpTour)
  {
    try
    {
 		currentSelectBox = selectBox;
	  	currentVal = defaultValue;
		
		currentEditBox = childEditBox;
		currentCount = defChildCount;

		if (weekendTour)
	      	xmlHttpTour.open("GET", tourWebsiteAddress+'?t='+t+'&tt='+tt, true);
		else
			xmlHttpTour.open("GET", tourAddress+'?t='+t+'&tt='+tt, true);

	    xmlHttpTour.onreadystatechange = function() 
		   {
			  idArray = new Array();
			  nameArray = new Array();
			
			  if (xmlHttpTour.readyState == 4) 
			  {
			  	if (xmlHttpTour.status == 200) 
				{
					response = xmlHttpTour.responseXML.documentElement;
				    if (response.childNodes.length)
					{
						idArray = xmlToArray(response.getElementsByTagName("id")); 
						nameArray =  xmlToArray(response.getElementsByTagName("name")); 
					}
					  
					if (currentSelectBox)
					{
						while (currentSelectBox.options.length>0)
							currentSelectBox.remove(0);
					
						for (i=0;i<idArray.length;i++)
						{
							//isSelected = (idArray[i] == currentRoomType);
							var newOpt = new Option(nameArray[i], idArray[i]);
			  				var selLength = currentSelectBox.length;
			  				currentSelectBox.options[selLength] = newOpt;
						}
						
						if (currentVal != "")
							currentSelectBox.value = currentVal;
					}
				}
			  }			
			  
			  // show the children count now
			  showChildren(childEditBox, t, tt, defChildCount);
		} /* end ready func */
	    xmlHttpTour.send(null);
    }
    catch(e)
    {
      displayError(e.toString());
    }
  }

}

// handles the processTrips received from the server
/*function processTrips()
{
  idArray = new Array();
  nameArray = new Array();

  if (xmlHttp.readyState == 4) 
  {
  	if (xmlHttp.status == 200) 
	{
		response = xmlHttp.responseXML.documentElement;
	    if (response.childNodes.length)
		{
			idArray = xmlToArray(response.getElementsByTagName("id")); 
			nameArray =  xmlToArray(response.getElementsByTagName("name")); 
		}
		  
		if (currentSelectBox)
		{
			while (currentSelectBox.options.length>0)
				currentSelectBox.remove(0);
		
			alert(idArray.length);
			for (i=0;i<idArray.length;i++)
			{
				//isSelected = (idArray[i] == currentRoomType);
				var newOpt = new Option(nameArray[i], idArray[i]);
  				var selLength = currentSelectBox.length;
  				currentSelectBox.options[selLength] = newOpt;
			}
			
			if (currentVal != "")
				currentSelectBox.value = currentVal;
		}
	}
  }
}*/
/******************* trips *************************/


/******************* children *************************/

function showChildren(editBox, h, r, defaultValue)
{
  xmlChildrenHttp = createXmlHttpRequestObject();
  if (xmlChildrenHttp)
  {
    try
    {
 		currentEditBox = editBox;
	  	currentCount = defaultValue;
      	xmlChildrenHttp.open("GET", maxChildAddress+'?h='+h+'&r='+r, true);
	    xmlChildrenHttp.onreadystatechange = function() {
			  maxArray = new Array();
			  if (xmlChildrenHttp.readyState == 4) 
			  {
			  	if (xmlChildrenHttp.status == 200) 
				{
					response = xmlChildrenHttp.responseXML.documentElement;
				    if (response.childNodes.length)
						maxArray = xmlToArray(response.getElementsByTagName("max")); 
					  
					if (currentEditBox)
					{
						currentEditBox.value = '';
						for (i=0;i<maxArray.length;i++)
						{
							currentEditBox.value = maxArray[i];
						}
						
						if (currentCount != "")
							currentEditBox.value = currentCount;
					}
				}
			  }		
		} /* end onready func */
	    xmlChildrenHttp.send(null);
    }
    catch(e)
    {
      displayError(e.toString());
    }
  }
}

function processChildren()
{
  maxArray = new Array();
  if (xmlHttp.readyState == 4) 
  {
  	if (xmlHttp.status == 200) 
	{
		response = xmlHttp.responseXML.documentElement;
	    if (response.childNodes.length)
			maxArray = xmlToArray(response.getElementsByTagName("max")); 
		  
		if (currentEditBox)
		{
			currentEditBox.value = '';
			for (i=0;i<maxArray.length;i++)
			{
				currentEditBox.value = maxArray[i];
			}
			
			if (currentVal != "")
			{
				currentEditBox.value = currentVal;
			}
		}
	}
  }
}

/******************* children *************************/

function xmlToArray(resultsXml)
{
  // initiate the resultsArray
  var resultsArray= new Array();  
  // loop through all the xml nodes retrieving the content  
  for(i=0;i<resultsXml.length;i++)
  {
    resultsArray[i]=resultsXml.item(i).firstChild.data;
  }
  
  // return the node's content as an array
  return resultsArray;
}

function displayError(message)
{
  // display error message, with more technical details if debugMode is true
  alert("Error accessing the server! "+
        (debugMode ? "\n" + message : ""));
}

/******************* OTHER *************************/

function updateDoubleSingle(numAdults, doubleField, singleField)
{
	NumPeople = numAdults;
	NumDbl = Math.floor(numAdults / 2);
	NumSingle = numAdults-(NumDbl*2);
			
	doubleField.value = NumDbl;
	singleField.value = NumSingle;		
}

/***************************** ROOM FOR HOTEL *******************/
function populateRooms(selectBox, h)
{
  xmlHttpRoom = createXmlHttpRequestObject();
  
  if (xmlHttpRoom)
  {
    try
    {
 		currentSelectBox = selectBox;
		
      	xmlHttpRoom.open("GET", roomAddress+'?h='+h, true);
	    xmlHttpRoom.onreadystatechange = function() 
		   {
			  idArray = new Array();
			  nameArray = new Array();
			
			  if (xmlHttpRoom.readyState == 4) 
			  {
			  	if (xmlHttpRoom.status == 200) 
				{
					response = xmlHttpRoom.responseXML.documentElement;
				    if (response.childNodes.length)
					{
						idArray = xmlToArray(response.getElementsByTagName("id")); 
						nameArray =  xmlToArray(response.getElementsByTagName("name")); 
					}
					  
					if (selectBox)
					{
						while (selectBox.options.length>0)
							selectBox.remove(0);
					
						for (i=0;i<idArray.length;i++)
						{
							//isSelected = (idArray[i] == currentRoomType);
							var newOpt = new Option(nameArray[i], idArray[i]);
			  				var selLength = currentSelectBox.length;
			  				selectBox.options[selLength] = newOpt;
						}
						
						if (currentVal != "")
							selectBox.value = currentVal;
					}
				}
			  }			
			  
			  
			  // show the children count now
			 // showChildren(childEditBox, t, tt, defChildCount);
			  
		} /* end ready func */
	    xmlHttpRoom.send(null);
    }
    catch(e)
    {
      displayError(e.toString());
    }
  }
}
