/**
*
*
*
* @params - product type 
* Called from 
*    - onload() - when page is loaded (or reloaded)
*    - select product search radio button
*   
*
*/
function doPageServices(productType)
{
    jsonrpc = new JSONRpcClient("/Jsp/JSON-RPC");
    //jsonrpc.pageServices.getNumBasketItems(doNumBasketItems);
    if(productType=="hotel") {
        jsonrpc.pageServices.getSearchInputValues(doHotelSearchInput, "HOTEL");
    } else 
    if(productType=="car") {
        jsonrpc.pageServices.getSearchInputValues(doCarSearchInput, "CAR");
    } else
    if(productType=="flight") {
        jsonrpc.pageServices.getSearchInputValues(doFlightSearchInput, "FLIGHT");
    }
}

function doHotelSearchInput(result, exception) {
    if(exception) {alert(exception.message);}
    var searchInputValues = result;
    //alert(searchInputValues.map.length);
    // TODO - need to put length on this hashmap for javascript to read!
    //if(searchInputValues.map.length != 0) {
        //alert(objectToString(searchInputValues,'JSON result Object'));
        // set search input values
        if (searchInputValues.map.destinationName!=undefined) { 
            document.getElementById('dest').value = searchInputValues.map.destinationName;
        }
         if (searchInputValues.map.checkInDay!=undefined) { 
            document.getElementById('checkInDay').value = searchInputValues.map.checkInDay;
         }
         if (searchInputValues.map.checkInMonthYear!=undefined) { 
             document.getElementById('checkInMonthYear').value = searchInputValues.map.checkInMonthYear;
         }
         if (searchInputValues.map.checkOutDay!=undefined) { 
             document.getElementById('checkOutDay').value = searchInputValues.map.checkOutDay
         }
         if (searchInputValues.map.checkOutMonthYear!=undefined) { 
             document.getElementById('checkOutMonthYear').value = searchInputValues.map.checkOutMonthYear
         }
         if (searchInputValues.map.numOfRooms!=undefined) { 
             document.getElementById('numOfRooms').value = searchInputValues.map.numOfRooms
         }
    //}
}
function doCarSearchInput(result, exception) {
    if(exception) {alert("exception="+exception.message);}
    var searchInputValues = result;
    if (searchInputValues.map.driverAge!=undefined) { 
        document.getElementById('DriverAge').value = searchInputValues.map.driverAge;
    }
    if (searchInputValues.map.destination1!=undefined) { 
        document.getElementById('destination1').value = searchInputValues.map.destination1;
    }
    if (searchInputValues.map.pickDate!=undefined) { 
           document.getElementById('pickDate').value = searchInputValues.map.pickDate;
    }
    if ((searchInputValues.map.pickYear!=undefined)||(searchInputValues.map.pickMonth!=undefined)) { 
        document.getElementById('pickMonth').value = searchInputValues.map.pickYear+searchInputValues.map.pickMonth;
    }
    if (searchInputValues.map.pickHour!=undefined) { 
        document.getElementById('pickHour').value = searchInputValues.map.pickHour;
    }
    if (searchInputValues.map.dropDate!=undefined) { 
        document.getElementById('dropDate').value = searchInputValues.map.dropDate;
    }
    if ((searchInputValues.map.dropYear!=undefined)||(searchInputValues.map.dropMonth!=undefined)) { 
        document.getElementById('dropMonth').value = searchInputValues.map.dropYear+searchInputValues.map.dropMonth;
    }
    if (searchInputValues.map.dropHour!=undefined) { 
        document.getElementById('dropHour').value = searchInputValues.map.dropHour;
    }
}
function doFlightSearchInput(result, exception) {
    if(exception) {alert("exception="+exception.message);}
    
    var searchInputValues = result;
    
    if(searchInputValues.map.destinationAirport != null) {
        document.getElementById('destinationAirport').value = searchInputValues.map.destinationAirport;
    }
    
    if(searchInputValues.map.destinationAirport != null) {
        document.getElementById('departureAirport').value = searchInputValues.map.departureAirport;
    }
    
    if(document.getElementsByName('tripType').tripType1.value == searchInputValues.map.tripType) {
          document.getElementsByName('tripType').tripType1.checked = true;
    } else 
    if(document.getElementsByName('tripType').tripType2.value == searchInputValues.map.tripType) {
         document.getElementsByName('tripType').tripType2.checked = true;
    }
    if(searchInputValues.map.departureDay != null) {
     document.getElementById('departureDay').value = searchInputValues.map.departureDay;
    }
    
    if(searchInputValues.map.departureMonth != null) {
    document.getElementById('departureMonth').value = searchInputValues.map.departureMonth;
    }
    if(searchInputValues.map.PDayOfWeek != null) {
    document.getElementById('PDayOfWeek').value = searchInputValues.map.PDayOfWeek;
    }
    if(searchInputValues.map.departureTime != null) {
    document.getElementById('departureTime').value = searchInputValues.map.departureTime;
    }
    if(searchInputValues.map.returnDay != null) {
    document.getElementById('returnDay').value = searchInputValues.map.returnDay;
    }
    if(searchInputValues.map.returnMonth != null) {
    document.getElementById('returnMonth').value = searchInputValues.map.returnMonth;
    }
    if(searchInputValues.map.DDayOfWeek != null) {
    document.getElementById('DDayOfWeek').value = searchInputValues.map.DDayOfWeek;
    }
    if(searchInputValues.map.returnTime != null) {
    document.getElementById('returnTime').value = searchInputValues.map.returnTime;
    }
    if(searchInputValues.map.maxConnections != null) {
    document.getElementById('maxConnections').value = searchInputValues.map.maxConnections;
    }
    if(searchInputValues.map.carriageClass != null) {
    document.getElementById('carriageClass').value = searchInputValues.map.carriageClass;
    }
    if(searchInputValues.map.airlineCode != null) {
    document.getElementById('airlineCode').value = searchInputValues.map.airlineCode;
    }
    if(searchInputValues.map.numAdults != null) {
    document.getElementById('numAdults').value = searchInputValues.map.numAdults;
    }
    if(searchInputValues.map.numChildren != null) {
    document.getElementById('numChildren').value = searchInputValues.map.numChildren;
    }
    if(searchInputValues.map.numInfants != null) {
    document.getElementById('numInfants').value = searchInputValues.map.numInfants;
    }
    calcDay();
}
 
/**
  * Outputs the contents of ANY object as a string.  WARNING if it 
  * contains recursive references, this function WILL get stuck...
  * Objects are enclosed within '{...}'  keys (this is essentially 
  * a hashmap remember) are denoted by '[...]' and the values are 
  * denoted by '"..."' if they are a literal, and '{...}' if they 
  * are themselves an object.
  *
  * @param anObject   - The object to iterate over
  * @param objectName - Any string you wish to use to identify that
  *                     object passed in (not required)
  */
 function objectToString(anObject,objectName)
 {
   var retVal = ""; // this will be our string to return eventually
   var value;
 
   if(null != objectName)
   {
     retVal += "["+objectName +"] {";
   }
 
   // loop through each item in the object
   for(memberObjKey in anObject)
   {
     retVal += "["+memberObjKey+"]";
     value = anObject[memberObjKey];
     
     if('object' == typeof value)
     {
       retVal += " {"+objectToString(value)+"}";
     }
     else
     {
       retVal += " '"+value+"' ";
     }
     retVal += " ";
   }
 
   if(null != objectName)
   {
     retVal += " }";
   }
 
   return retVal;
 }