var arrAJAXResponse = new Array();
var xmlhttpobject;
var citycodehelpercallby;//which ID last call the attachcitycodehelper()
var existingEvt;
var lastXML; //the last xml data that was accessed.  This is to help with find the citycode for a cityname.
var okcolor, failcolor;
okcolor = 'white'; //textbox background color.
failcolor = 'red';

//write the select to the page.
if (document.getElementById('citycodehelper'))
{}else{    document.write('<select name="citycodehelper" class="citycodehelper" id="citycodehelper" style="display:none;overflow:hidden;z-index:1000;" onclick="ClearCarLocations();setcitycode();" onkeydown="setselectedhelpercity(event);" onblur="HideHelperSelect();"></select>'); }

if (document.getElementById('viewalldiv'))
{}else{    document.write('<div id="viewalldiv" class="dialog" style="margin: 0;z-index:1000; padding: 0; display: none; height: 500px; width: 800px; overflow: auto;"></div>');}


    
    existingEvt = document.onclick;
    
    document.onclick = 
    function hidecitycodehelper()
    {
        if (document.getElementById('citycodehelper'))
        {   
//            if (document.getElementById('citycodehelper').style.display == 'none')
//            {//it has been hidden...don't to anything else
//                return;
//            }
            document.getElementById('citycodehelper').style.display = 'none'; 
        }
        setcityname();
        setcitycode();
        if (existingEvt){ existingEvt();}
    }
    
    function setcityname()
    {
        if (citycodehelpercallby)
        {
            if (document.getElementById('citycodehelper').selectedIndex >= 0)
            {
               //citycodehelpercallby.value = document.getElementById('citycodehelper').value;   
               citycodehelpercallby.value = document.getElementById('citycodehelper').options[document.getElementById('citycodehelper').selectedIndex].text;   
               //de select items now
               document.getElementById('citycodehelper').selectedIndex = -1;
            }
            if (citycodehelpercallby.style)
                citycodehelpercallby.style.borderBottomWidth='1px';
        }
    }
    function setcitycode()
    {
        var desthelperid, selcityname, citycode, i,j;
        var nameid, codeid, destnum, checkprodtrans;
         try
         {   
            checkprodtrans = '';
            destnum  = 0;
            //get the IDs of the textbox holding the name and the code  (airport name vs airport code)
            if (citycodehelpercallby)
            {
                nameid         = document.getElementById(citycodehelpercallby.getAttribute('nameid'));
                codeid         = document.getElementById(citycodehelpercallby.getAttribute('codeid'));
                destnum        = parseInt('0' + citycodehelpercallby.getAttribute('currentdestnum'));
                checkprodtrans = citycodehelpercallby.getAttribute('checkinterdesttransportation'); //Y/N
                if (!checkprodtrans || checkprodtrans == null || checkprodtrans == 'undefined'){   checkprodtrans =''; }
                if (nameid && codeid)
                {
                    desthelperid = document.getElementById('citycodehelper');
                                    
                    if (nameid.value == '' || !desthelperid){ return;}
                    //clear was one there before.
                    codeid.value = '';
                    //all good....get the citycode.
                    i = lastXML.toUpperCase().indexOf('|' + nameid.value.toUpperCase() + '|')
                    if (i < 0)
                    {
                        //invalid city name.
                        citycodehelpercallby.style.backgroundColor = failcolor;                    
                        return;    
                    }
                    j = lastXML.lastIndexOf('**',i);
                    citycode = lastXML.substring(j+2,i).split('|')[0];                
                    codeid.value = citycode;
                    if (citycodehelpercallby.style)
                        citycodehelpercallby.style.backgroundColor = okcolor;
                    
                    //are we checking inter destination transportation options?
                    if (destnum <= 1)
                    {   if (document.getElementById('interdest' + destnum))
                        {
                            document.getElementById('interdest' + destnum).style.display = 'none';
                        }
                    }
                    else if (checkprodtrans.toUpperCase() == 'Y' && destnum > 1)
                    {
                        //hard code how to get the previous citycode......damn it!!!!
                        var prevcitycode;
                        prevcitycode = ''
                        if (document.getElementById('destination' + (destnum-1)))
                        {   prevcitycode = document.getElementById('destination' + (destnum-1)).value;  }
                        //alert('checkprodtrans: ' + checkprodtrans + ' destnum: ' + destnum + ' prevcitycode: ' + prevcitycode + ' citycode: ' + citycode);
                        if (prevcitycode.length >= 3 && citycode.length >= 3)
                        {
                            //call the function to set the interdestination transportation.
                            SetInterDestTransportation(destnum, citycode.substring(0,3), prevcitycode.substring(0,3));
                        }                    
                    }
                }
            }
            return true;
        }
        catch (ex)
        {
            //alert('setcitycode() failed. ' + ex.message);
        }
    }
    function SetInterDestTransportation(destnum, citycode, precitycode)
    {
        //show the interdestination section for this dest.
        //why did I do this in 3 IFs? for clarity and I like it like that!  Can't I have anything my way damn it!!!
        if (citycode && citycode != null & citycode != 'undefined' & citycode.length > 0)        
        {
            if (precitycode && precitycode != null & precitycode != 'undefined' & precitycode.length > 0)
            {
                if (destnum && destnum != null & destnum != 'undefined' & parseInt(destnum) > 1)
                {
                    //show the inter destination section.
//                    document.getElementById('interdest' + destnum).style.display = '';
                    //set the Product Transportation data for these two destination.
                    var strData, bcar, btra, bair, btrf, bnone;
                    strData = '';
                    strData = xmlhttpResponse('GETPRODUCTTRANSPORTATION', '', '', '', '&FromCity=' + precitycode + '&ToCity=' + citycode, precitycode+citycode)
                    //alert('GETPRODUCTTRANSPORTATION Data: ' + strData);
                    //Date structure.
                    //**|AIR:Y|TRA:Y||**    only Y (yes/allow) transportation type are return.  format: **CAR:Y|AIR:Y|TRA:Y|TRF:Y**
                    strData = strData.toUpperCase();
                    bcar  = (strData.indexOf('CAR:Y') > -1);
                    btra  = (strData.indexOf('TRA:Y') > -1);
                    bair  = (strData.indexOf('AIR:Y') > -1 && document.getElementById('trip_type').value.toUpperCase().indexOf('AIRF') > -1);
                    btrf  = (strData.indexOf('TRF:Y') > -1);
                    bnone = (strData.indexOf(':')     <  0);
                    
                    if (bair == false)
                    {//check for bnone again....it maybe that only 'AIR:Y is available but trip type is not air inclusive.
                        bnone = (strData.replace('AIR:Y','').indexOf(':')     <  0);
                    }
                    
                    //for debugging purposes...drop a ID=test textarea somewhere to see
                    //document.getElementById('test').value = 'GETPRODUCTTRANSPORTATION Data: ' + strData + '\nbcar: ' + bcar + '\nbtra: ' + btra + '\nbair: ' + bair + '\ntrf: ' + btrf + '\nbnone: ' + bnone + '\ndestnum: ' + destnum;

                    //hide/show the td.                    
                    if (document.getElementById('tdairf'        + destnum)){ document.getElementById('tdairf'       + destnum).style.display = ((bair == true) ? '' : 'none');   }
                    if (document.getElementById('tdrail'        + destnum)){ document.getElementById('tdrail'       + destnum).style.display = ((btra == true) ? '' : 'none');   }
                    if (document.getElementById('tdtrf'         + destnum)){ document.getElementById('tdtrf'        + destnum).style.display = ((btrf == true) ? '' : 'none');   }
                    if (document.getElementById('tdcar'         + destnum)){ document.getElementById('tdcar'        + destnum).style.display = ((bcar == true) ? '' : 'none');   }
                    if (document.getElementById('tdnone'        + destnum)){ document.getElementById('tdnone'       + destnum).style.display = ((bnone == true) ? 'none' : '');   }
                    
//                    //show/hide the no services available between destination
//                    if (document.getElementById('noservicemsg'  + destnum)){ document.getElementById('noservicemsg' + destnum).style.display = ((bnone == true) ? '' : 'none');   }
//                    
//                    //these two IDs are hardcoded in Input_FitMultiIndiv.htm template.
//                    if (document.getElementById('noserviceheader'  + destnum))
//                    {
//                        document.getElementById('noserviceheader'  + destnum).innerHTML = 'I\'d like to go from ' + document.getElementById('destination' + (destnum-1)).options[document.getElementById('destination' + (destnum-1)).selectedIndex].text.replace('&nbsp;', '') + ' to ' + document.getElementById('destination' + (destnum)).options[document.getElementById('destination' + (destnum)).selectedIndex].text.replace('&nbsp;', '') + ' by';
//                    }
//                    if (document.getElementById('noservicemsg'  + destnum))
//                    {
//                        document.getElementById('noservicemsg'  + destnum).innerHTML = 'We do not have any services between these destinations.  You must go from ' + document.getElementById('destination' + (destnum-1)).options[document.getElementById('destination' + (destnum-1)).selectedIndex].text.replace('&nbsp;', '') + ' to ' + document.getElementById('destination' + (destnum)).options[document.getElementById('destination' + (destnum)).selectedIndex].text.replace('&nbsp;', '') + ' by your own means.';
//                    }
                }
            }
        }
    }
    function ChangeAJAXLastCallID(newid)
    {
        if (document.getElementById(newid))
        {
            citycodehelpercallby = document.getElementById(newid);
        }
    }
    function checkArrowDown(e)
    {
      var keyCode;
      if (e)
      {
        keyCode = e.keyCode;
      }
      else
      {
        keyCode = e.which;
      }
      if (keyCode == 40)
      {
        document.getElementById('citycodehelper').focus();
      }
    }
    function HideHelperSelect()
    {
        if (document.getElementById('citycodehelper')){document.getElementById('citycodehelper').style.display='none';}
    }
    function setselectedhelpercity(e)
    {
        var keyCode;
        if (e){ keyCode = e.keyCode;    }
        else {  keyCode = e.which;  }
        if (keyCode == 9)
        {
            //select the current item.
            document.getElementById('citycodehelper').selectedIndex = document.getElementById('citycodehelper').options.selectedIndex;
            //turn the key into a enter key
            keyCode = 13;            
        }
        if (keyCode == 13)
        {
            setcityname();
            setcitycode();
            document.getElementById('citycodehelper').style.display='none';
            //if has car...show locations            
            if (document.getElementById('trip_type'))
            {
                if (document.getElementById('trip_type').value.toUpperCase().indexOf('CAR') > -1)
                {   SetCarLocations();  }
            }
        }
        else if (keyCode == 40 || keyCode == 38)
        {
            //do nothing...user just go up(40) and down(38)
        }
        else
        {
			//give control back to the current textbox
            citycodehelpercallby.focus();
        }
    }
    function GetLastCityCode(currdestnum)
    {
        if (currdestnum && currdestnum != null && currdestnum != 'undefined')
        {
            if (document.getElementById('destination' + (parseInt(currdestnum)-1)))
            {   return document.getElementById('destination' + (parseInt(currdestnum)-1)).value; }
        }
        return '';
    }
    /*
    tid: the id of the element that the list of avail cities should popup under.
    datatype: type of data that you're looking for  , FROMCITY, TOCITY, PRODUCTCODE, OR CARLOCATION....see xmlhttpResponse() below for more detail
    brandid: the current brandid
    predest: the previous destination...for multicities FIT shops.
    */
    function attachcitycodehelper(tid, datatype, brandid, predest, triptype, param)
    {
        var txtid, helpid, x, y, z, h, w, dom, strXML;
        citycodehelpercallby = tid
        x = getX(tid);
        y = getY(tid);
        h = tid.offsetHeight;
        w = tid.offsetWidth;        
        helpid = document.getElementById('citycodehelper');
        dom = null;
        var now, end, total, debug;
        
        //don't trust the typetype passed in.  get it from the page to get the most current one.
        if (document.getElementById('trip_type')){  triptype = document.getElementById('trip_type').value;  }
        
        //if the previous citycode is not passed in, attempt to find it.
        if (predest == null || predest == 'undefined' || predest == '')
        {
            predest = GetLastCityCode(tid.getAttribute('currentdestnum'));
        }
        
        //get the xml
        now = new Date();
        end = new Date();
        debug = '';
        strXML = xmlhttpResponse(datatype, brandid, predest, triptype, param);
        //remember this xml data as the last one that was accessed.
        lastXML = strXML;
        
        end = new Date();
        debug = 'GetData: ' + (end - now)/1000 + ' s';
        now = new Date();
        var i, j, seektext, seeklen, city, code, country, total, maxcitylen, strtemp, pos, lpos, rpos, allcitiesarr;
        seektext = tid.value.toUpperCase();
        seeklen = seektext.length;
        total = 0;
        maxcitylen = 0;
        helpid.options.length = 0;
        helpid = document.getElementById('citycodehelper');
        begdate = new Date();
        pos = 0;
        lpos = 0;
        rpos = 0;
        
        //format of data.
        //**CityCode|ProductDescription|CityDescription,Country.Description|ProductProductCode
        //WARNING: do not search by cityCode (exclude cityCode when matching) because we have multiple product that point to the same citycode.  This is because we have products in cities which do not have airport and must land at other city's airport)
        //so do not search by cityCode when matching on product.
                
		///SEARCHING WITH STRING
		if (datatype.toUpperCase() == 'PRODUCTCODE')
		{
            pos = strXML.toUpperCase().indexOf('|' + seektext,pos);
            if (pos = -1) { pos = strXML.toUpperCase().indexOf(seektext,pos+5);} //+5 to not include the **CITYCODE in matching.
        }else
        {
            pos = strXML.toUpperCase().indexOf('|' + seektext,pos);
            if (pos = -1) { pos = strXML.toUpperCase().indexOf('**' + seektext,pos);}
            if (pos = -1) { pos = strXML.toUpperCase().indexOf(seektext,pos);}
        }
        
        while (pos >-1)
        {
            lpos = strXML.lastIndexOf('**',pos);
            rpos = strXML.indexOf('**',pos);
            
            if (lpos > -1 && rpos > lpos)
            {
                lpos = lpos + 2;
                strtemp = strXML.substr(lpos, (rpos - lpos));
                if (strtemp.split('|').length >= 3)
                {
                    if (datatype.toUpperCase() == 'FROMCITYCODE')
                    {
                        code = strtemp.split('|')[0];
                        city = strtemp.split('|')[1].replace(/&amp;/g,'&');
                        country = strtemp.split('|')[2].replace(/&amp;/g,'&');
                    }
                    else
                    {
                        code = strtemp.split('|')[0];
                        city = strtemp.split('|')[2].replace(/&amp;/g,'&');
                        country = strtemp.split('|')[3].replace(/&amp;/g,'&');
                    }                        
                    //match add to options.
                    try
                    {   helpid.add(new Option(city, code))  }
                    catch(e)
                    {   helpid.add(new Option(city, code), null)    }
                    total += 1;
                    //if (city.length > maxcitylen){ maxcitylen = city.length }
                }
            }
            if (rpos > 0)
            {
                if (datatype.toUpperCase() == 'PRODUCTCODE')
		        {
                    pos = strXML.toUpperCase().indexOf('|' + seektext,rpos);
                    if (pos = -1) { pos = strXML.toUpperCase().indexOf(seektext,rpos+5);} //+5 to not include the **CITYCODE in matching.
                }else
                {
                    pos = strXML.toUpperCase().indexOf('|' + seektext,rpos);
                    if (pos = -1) { pos = strXML.toUpperCase().indexOf('**' + seektext,rpos);}
                    if (pos = -1) { pos = strXML.toUpperCase().indexOf(seektext,rpos);}
                }
//                pos = strXML.toUpperCase().indexOf('|' + seektext,rpos);
//                if (pos = -1) { pos = strXML.toUpperCase().indexOf('**' + seektext,rpos);}
//                if (pos = -1) { pos = strXML.toUpperCase().indexOf(seektext,rpos);}
            }else
            {
                //exit while
                pos = -1
            }
        }
            
        if (total > 0)
        {
            helpid.style.display = '';
            helpid.selectedIndex = -1;
            helpid.setAttribute('size',10);
            tid.style.borderBottomWidth='0px';
            helpid.style.borderTopWidth = '0px';
            helpid.style.left = x + 'px';
            helpid.style.top = (y+h) + 'px';
            helpid.style.position= 'absolute';
        }
        end = new Date();
        debug = debug + 'Build: ' + (end-now)/1000 + ' s';        
        //enable for ajax stats
        //alert(debug);
    }
    
function viewalldest(tid, datatype, brandid, predest, triptype, destnum, nameid, codeid, param)
{
    
    var strXML, strOut, viewallid, divcontainer, divcontainerid, tdcount, record, pos1, pos2, strlastcountry;
    var country, cityname, citycode, nameid, codeid;
    var cityliststarted, linkdivstarted;

    //don't trust the typetype passed in.  get it from the page to get the most current one.
    if (document.getElementById('trip_type')){  triptype = document.getElementById('trip_type').value;  }
    
    //build the table and then show it.    
    //if the previous citycode is not passed in, attempt to find it.
    if (predest == null || predest == 'undefined' || predest == '')
    {
        predest = GetLastCityCode(destnum);
    }
    strXML = xmlhttpResponse(datatype, brandid, predest, triptype, param);
    //save the last XML data that was used.
    lastXML = strXML;

    divcontainerid = document.getElementById('viewalldiv');
    
    citycodehelpercallby = tid;
    
    strOut = '';
    strOut += '         <table width="100%" bgcolor="white" border="0" cellpadding="5" align="center" style="border: 1px solid #000000;">'
    strOut += '             <tr bgcolor="white">'
    strOut += '                 <td nowrap colspan="3">'
    if (datatype.toUpperCase() == 'FROMCITYCODE')
    {
        strOut += '                     <span class="Header">Departure Cities</span>'
    }
    else
    {
        strOut += '                     <span class="Header">Destinations</span>'
    }
    strOut += '                 </td>'
    strOut += '                 <td width="100%" align="right">'
    strOut += '                     <a href="#" onclick="HideOverlay(\'viewalldiv\');"> <img src="./' + brandid + '/Pics/close.gif" width="15" height="15" alt="" border="0" align="absmiddle" title="Closed"/></a>&nbsp;'
    strOut += '                 </td>'
    strOut += '             </tr>'
    
    cityliststarted = false;    
    country = '';
    strlastcountry = '';
    tdcount = 0;
    pos1 = strXML.indexOf('**')
    var closedtd = false;
    while (pos1 > -1)
    {
        pos2 = strXML.indexOf('**', pos1+1);
        if (pos2 > pos1)
        {
            record = strXML.substring(pos1 + 2, pos2);
            pos1 = pos2;
        }
        else
        {
            //end ...set condition to exit.
            pos1 = -1;
        }
        
        if (record != '')
        {
            if (datatype.toUpperCase() == 'FROMCITYCODE')
            {
                citycode = record.split("|")[0];
                cityname = record.split("|")[1];
                country  = record.split("|")[2];
            }else
            {
                citycode = record.split("|")[0];
                cityname = record.split("|")[2];
                country  = record.split("|")[3];
            }
            if (!country || country == null || country == 'undefine'){  country = '';}
            if (strlastcountry == '')
            {
                //the very very first time through the loop.
                strOut += '<tr> <!-- FIRST -->'                
            }
            
            if (strlastcountry != country && tdcount == 4)
            {
                //close the tr                                
                strOut += '</tr>'
                cityliststarted = false;
                tdcount = 0;
            }
            
            if (tdcount == 0 && strlastcountry != '' && country != strlastcountry)
            {
                //after all TDs are created.
                strOut += '<tr> <!-- NEW COUNTRY -->'
            }

            if (country.toUpperCase() != strlastcountry.toUpperCase())
            {
                if (cityliststarted)
                {   strOut += '</td>';  }
                //increase td and build it.
                tdcount += 1;
                strOut += '<td nowrap valign="top" width="20%">'
                strOut += ' <strong>' + country + '</strong><br/>'              
	            cityliststarted = true;
	        }
	        
	        if (datatype.toUpperCase() == 'FROMCITYCODE')
	        {
	            strOut += '<a href="#" class="regularlink" onclick="document.getElementById(\'' + nameid + '\').style.backgroundColor=\'' + okcolor + '\';document.getElementById(\'' + nameid + '\').value=\'' + cityname + '\';document.getElementById(\'' + codeid + '\').value=\'' + citycode + '\';HideOverlay(\'viewalldiv\');">' + cityname + '</a><br/>\n'
	        }	        
	        else if (triptype.toUpperCase() == 'CAR')
	        {
	            //pull the car location list after setting destination name and code.
	            strOut += '<a href="#" class="regularlink" onclick="document.getElementById(\'' + nameid + '\').style.backgroundColor=\'' + okcolor + '\';document.getElementById(\'' + nameid + '\').value=\'' + cityname + '\';document.getElementById(\'' + codeid + '\').value=\'' + citycode + '\';SetCarLocations();HideOverlay(\'viewalldiv\');">' + cityname + '</a><br/>\n'
	        }
	        else if (triptype.toUpperCase() == 'TRA')
	        {
	            strOut += '<a href="#" class="regularlink" onclick="document.getElementById(\'' + nameid + '\').style.backgroundColor=\'' + okcolor + '\';document.getElementById(\'' + nameid + '\').value=\'' + cityname + '\';document.getElementById(\'' + codeid + '\').value=\'' + citycode + '\';HideOverlay(\'viewalldiv\');">' + cityname + '</a><br/>\n'
	        }
	        else
	        {
	            strOut += '<a href="#" class="regularlink" onclick="document.getElementById(\'' + nameid + '\').value=\'' + cityname + '\';document.getElementById(\'' + codeid + '\').value=\'' + citycode + '\';ChangeAJAXLastCallID(\'destcity\'+' + destnum + '); setcitycode();SetCarLocations();HideOverlay(\'viewalldiv\');">' + cityname + '</a><br/>\n'
	        }
            //strOut += '<!-- Build Info: tdcount: ' + tdcount + ' - CityListStarted: ' + cityliststarted + ' - Country: ' + country + ' - LastCountry: ' + strlastcountry + '-->\n';
            strlastcountry = country;
        }    
    }
    
    //if no data found...say so
    if (strXML.indexOf('**') < 0)
    {
        if (predest.length > 0 && document.getElementById(nameid).value != 'undefined' && document.getElementById(nameid).value.length > 0)
        {
            strOut += '<tr><td colspan="4">We\'re sorry.  There are no available city coming from ' + document.getElementById(nameid).value + ' for this destination.'
            //also remove the cities currently in this destination box (if any)
            document.getElementById(nameid).value='';
            document.getElementById(codeid).value='';
        }
        else
        {   
            strOut += '<tr><td colspan="4">We\'re sorry.  There are no available city for this destination.'            
        }
    }
    
    //Close the last TD and TR
    strOut += '</td></tr>';
    //Close the table
    strOut += '</table>';

    divcontainerid.innerHTML = strOut;
    ShowOverlay('viewalldiv');
}

//Function that will set the dropdown correctly based on the previous destination, etc.
function SetDestDropdown(DestNum, datatype, brandid, predest, triptype, param)
{
    var pos, lpos, rpos;
    var strtemp,strprevcountry;
    var optGroup, optOption;
    
    strprevcountry = '';
    //don't trust the typetype passed in.  get it from the page to get the most current one.
    if (document.getElementById('trip_type')){  triptype = document.getElementById('trip_type').value;  }
    
    strXML = xmlhttpResponse(datatype, brandid, predest, triptype, param);
    
    //Clear out the destination dropdown
    document.getElementById('destination' + DestNum).options.length = 0;
    var sel = document.getElementById('destination' + DestNum);
    while (sel.firstChild) 
    {
        sel.removeChild(sel.firstChild);
    }
    document.getElementById('destination' + DestNum).options[0] = new Option('Select a Destination', '');
    
    pos = strXML.indexOf('**')
    while (pos >-1)
    {
        lpos = strXML.indexOf('**', pos+1);
        if (lpos > pos)
        {
            strtemp = strXML.substring(pos + 2, lpos);
            pos = lpos;
        }
        else
        {
            //end ...set condition to exit.
            pos = -1;
            strtemp = '';
        }
        
        if (strtemp != '')
        {
            if (strtemp.split('|').length >= 3)
            {
                code = strtemp.split('|')[0];
                city = strtemp.split('|')[2].replace(/&amp;/g,'&');
                country = strtemp.split('|')[3].replace(/&amp;/g,'&');
                
                //Is this a new country?
                if (strprevcountry != country)
                {
                    //If not the first country, add the group
                    if (strprevcountry != '')
                        document.getElementById('destination' + DestNum).appendChild(optGroup);
                    //Create the new Group
                    optGroup = document.createElement('optgroup');
                    optGroup.label = country;
                }
                
                //match add to options.
                //If there's a - in the code, add an extra tab to the city
                if (code.indexOf('-') > 0)
                    city = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + city;
                //document.getElementById('destination' + DestNum).options[optCount]= new Option(city, code)
                //Create the new option and add it to the Group
                optOption = document.createElement('option');
                optOption.innerHTML = city;
                optOption.value = code;
                optGroup.appendChild(optOption);
                
                strprevcountry = country;
            }
        }
    }
    //Add the last group
    if (optGroup != null)
        document.getElementById('destination' + DestNum).appendChild(optGroup);
}

function ClearCarLocations()
{
    var strXML, strtemp, strCode, strName;
    var lpos, rpos, optCount;
    
    strXML = '';
    strtemp = '';
    strCode = '';
    strName = '';
    lpos = 0;
    rpos = 0;
    optCount = 1;
    
    try
    {
        //Empty out the dropdowns first
        document.getElementById('selpickup').options.length = 0;
        document.getElementById('seldropoff').options.length = 0;
        
        document.getElementById('selpickup').options[0] = new Option('>>Select a City<<', 0)
        document.getElementById('seldropoff').options[0] = new Option('>>Select a City<<', 0)
    }
    catch(ex)
    {
        //alert('SetCarLocations() Error: ' + ex.message);
    }
}

//Sets the pickup and dropoff location dropdowns based on the city selected
function SetCarLocations()
{
    var strXML, strtemp, strCode, strName, strcarparam, citycode;
    var lpos, rpos, optCount;
    
    strXML = '';
    strtemp = '';
    strCode = '';
    strName = '';
    lpos = 0;
    rpos = 0;
    optCount = 1;
    
    try
    {
        //Empty out the dropdowns first
        document.getElementById('selpickup').options.length = 0;
        document.getElementById('seldropoff').options.length = 0;
        
        if (document.getElementById('destination1').value != '')
        {
            document.getElementById('selpickup').options[0] = new Option('>>Select a Location<<', 0)
            document.getElementById('seldropoff').options[0] = new Option('>>Select a Location<<', 0)
            
            
            //function xmlhttpResponse(strRequestType, brandid, predest, triptype, param, cacheid)
            //xmlhttpobject.send('Action=GETCARLOCATIONS&CityCode=' + document.getElementById('destination1').value.substring(0, 3));
            
            citycode = document.getElementById('destination1').value.substring(0, 3);            
            //Now get the locations
            strXML = xmlhttpResponse('CARLOCATIONS',null ,null ,null , '&Citycode=' + citycode, citycode);
            
            lpos = strXML.indexOf('**') + 2;
            //Loop through the response to load the dropdowns
            while (lpos > -1)
            {
                rpos = strXML.indexOf('**', lpos);
                
                if (rpos > lpos)
                {
                    strtemp = strXML.substr(lpos, (rpos - lpos));
                    if (strtemp.split('|').length >= 2)
                    {
                        strCode = strtemp.split('|')[0];
                        strName = strtemp.split('|')[1].replace(/&amp;/g,'&');
                        
                        //Add to the dropdown list
                        //document.getElementById('selpickup').add(new Option(strName, strCode))
                        //document.getElementById('seldropoff').add(new Option(strName, strCode))
                        //FIC:2009-12-09 - The above statement ONLY works in IE.
                        document.getElementById('selpickup').options[optCount]= new Option(strName, strCode)
                        document.getElementById('seldropoff').options[optCount]=new Option(strName, strCode)
                        optCount++;
                    }
                }
                if (rpos > 0)
                {
                    lpos = strXML.indexOf('**', rpos);
                    if (lpos > -1)
                        lpos += 2;
                }
                else
                {
                    //exit while
                    lpos = -1
                }
            }
            //NOW if car is to be drop off at a different city....pull a different drop off locations for that cities.
            //Now get the locations            
            if (document.getElementById('docarcitycode1'))
            {
                citycode = document.getElementById('docarcitycode1').value.substring(0,3).replace(/ /g, '');
            }
            if (citycode.length == 3)
            {
                strXML = xmlhttpResponse('CARLOCATIONS',null ,null ,null , '&CityCode=' + citycode, citycode);
                document.getElementById('seldropoff').options.length = 0;
                document.getElementById('seldropoff').options[0] = new Option('>>Select a Location<<', 0)
                optCount = 1;
                
                lpos = strXML.indexOf('**') + 2;
                //Loop through the response to load the dropdowns
                while (lpos > -1)
                {
                    rpos = strXML.indexOf('**', lpos);
                    
                    if (rpos > lpos)
                    {
                        strtemp = strXML.substr(lpos, (rpos - lpos));
                        if (strtemp.split('|').length >= 2)
                        {
                            strCode = strtemp.split('|')[0];
                            strName = strtemp.split('|')[1].replace(/&amp;/g,'&');
                            
                            //Add to the dropdown list
                            //document.getElementById('selpickup').add(new Option(strName, strCode))
                            //document.getElementById('seldropoff').add(new Option(strName, strCode))
                            //FIC:2009-12-09 - The above statement ONLY works in IE.
                            document.getElementById('seldropoff').options[optCount]=new Option(strName, strCode)
                            optCount++;
                        }
                    }
                    if (rpos > 0)
                    {
                        lpos = strXML.indexOf('**', rpos);
                        if (lpos > -1)
                            lpos += 2;
                    }
                    else
                    {
                        //exit while
                        lpos = -1
                    }
                }
            }
            ///set setting dropoff locations
            
            
        }
        else
        {
            document.getElementById('selpickup').options[0] = new Option('>>Select a City<<', 0)
            document.getElementById('seldropoff').options[0] = new Option('>>Select a City<<', 0)
        }
    }
    catch(ex)
    {
        //alert('SetCarLocations() Error: ' + ex.message);
    }
}


function getX(tid)
{
    var x,op;
    x = 0;
    //x = tid.offsetRight;
    x = tid.offsetLeft;
    while (tid.offsetParent)
    {
        //alert('tid: ' + tid.id + ' - OffsetParent: ' + tid.offsetParent.offsetLeft);
	    op = tid.offsetParent;	// Get container parent
	    x = x + op.offsetLeft;	// Add this element's offset x in pixels
	    tid = tid.offsetParent;	// Update current container
    }
    return x;
}
function getY(tid)
{
    var y,op;
    y = 0;
    y = tid.offsetTop;
    while (tid.offsetParent)
    {
	    op = tid.offsetParent;	// Get container parent
	    y = y + op.offsetTop;		// Add this element's offset y in pixels
	    tid = tid.offsetParent;	// Update current container
    }
    return y;
}
    
    
//AJAX stuff here.
function GetXMLHTTPObject()
{
    var xmlHTTPobj;
    xmlHTTPobj = null;
    //if (xmlhttpobject && xmlhttpobject  == 'undefined')
    //{return;/*object already recreated.*/}

	if(window.XMLHttpRequest)
	{
		try{xmlHTTPobj=new XMLHttpRequest;}
		catch(d){}
	}
	else if(window.ActiveXObject)
	{
		for(var i=0,xmlp;xmlp=["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"][i++];)
		{
			try{xmlHTTPobj=new ActiveXObject(xmlp);break}
			catch(d){}
		}
	}
	return xmlHTTPobj;
}

function getVacBuilderAmenities(brandid, productcode, supplierid, serviceid, seq, destnum)
{
    var strXML, strSuppID, strServID, strSuppName, strImage, strRating, strDesc;
    var strtemp, strTable, strID, strValue, strNewValue;
    var lpos, rpos;
    
    //First get the Amenities response
    strXML = xmlhttpResponse('GETVACBUILDERAMEN',null ,null ,null , '&productcode=' + productcode, productcode);
    
    lpos = strXML.indexOf('**') + 2;
    //Loop through the response to load the dropdowns
    while (lpos > -1)
    {
        rpos = strXML.indexOf('**', lpos);
        
        if (rpos > lpos)
        {
            strtemp = strXML.substr(lpos, (rpos - lpos));
            if (strtemp.split('|').length >= 6)
            {
                strSuppID = strtemp.split('|')[0];
                strServID = strtemp.split('|')[1];
                
                //Check SupplierID, ServiceID
                if (strSuppID.toUpperCase() == supplierid.toUpperCase() && (strServID.toUpperCase() == serviceid.toUpperCase() || strServID == '0' || strServID == ''))
                {
                    strSuppName = strtemp.split('|')[2].replace(/&amp;/g,'&');
                    strImage = strtemp.split('|')[3].replace(/&amp;/g,'&');
                    strRating = strtemp.split('|')[4];
                    strDesc = strtemp.split('|')[5].replace(/&amp;/g,'&');
                    
                    //If there is no Image, use the default
                    if (strImage == '')
                        strImage = './USA/Pics/NoImage.gif';
                    //If there is no rating, set it to 0
                    if (strRating == '')
                        strRating = '0';
                    
                    //Now build the table
                    strTable = '<table class="box3" cellspacing="0" width="100%">\n';
                    strTable = strTable + '<tr>\n<td class="box3-title" valign="top" style="width:100%">\n';
                    strTable = strTable + 'Selected Hotel - ' + strSuppName + '\n</td>\n</tr>\n';
                    strTable = strTable + '<tr>\n<td class="box3-row" style="width:100%"><img src="' + strImage + '" align="left" border="0" width="200" class="photo"><img src="./USA/Pics/star' + strRating + '.gif" border="0"><br/><br/>' + strDesc + '<br />';
                    strTable = strTable + '<a href="/stw/STWDescription.aspx?mode=hotel&theme=' + brandid + '&shopform=N&stype=HTL&sid=' + strSuppID + '&svcID=' + serviceid + '&ProductCode=' + productcode + '" target="_blank">[Hotel Detail]</a></td>\n</tr>\n</table>\n';
                    
                    //Set the main description html
                    document.getElementById('mainhoteldescription' + destnum).innerHTML = strTable;
                    
                    //Now set the room type radio button
                    strID = document.getElementsByName('radioroom' + destnum).item(0).id;
                    strValue = document.getElementsByName('radioroom' + destnum).item(0).value;
                    
                    //Set the new value
                    strNewValue = strValue.substring(0, strValue.indexOf('{SupplierID}') + 12);
                    strNewValue = strNewValue + supplierid;
                    strNewValue = strNewValue + strValue.substring(strValue.indexOf('{/SupplierID}'), strValue.indexOf('{Seq}') + 5);
                    strNewValue = strNewValue + seq;
                    strNewValue = strNewValue + strValue.substring(strValue.indexOf('{/Seq}'), strValue.length);
                    
                    //Set the radio button's value                    
                    document.getElementById(strID).value = strNewValue;
                    
                    //Set the new class
                    document.getElementById('tdradiohotel' + productcode + supplierid).className = 'pkg-results-active-left';
                }
            }
        }
        if (rpos > 0)
        {
            lpos = strXML.indexOf('**', rpos);
            if (lpos > -1)
                lpos += 2;
        }
        else
        {
            //exit while
            lpos = -1
        }
    }
}

function xmlhttpResponse(strRequestType, brandid, predest, triptype, param, cacheid)
{
    var strResponse, strbrandid, strpredest, strtriptype;
    strResponse = '';    
    strbrandid = '';
    strpredest = '';
    strtriptype = '';
    
    //don't trust the typetype passed in.  get it from the page to get the most current one.
    if (document.getElementById('trip_type')){  triptype = document.getElementById('trip_type').value;  }
        
    if (!param || param == null || param.toUpperCase() == 'UNDERFINED')
    {//param is not passed in.  set it to empty.
        param = '';
    }
    if (!cacheid || cacheid == null || cacheid.toUpperCase() == 'UNDERFINED')
    {//param is not passed in.  set it to empty.
        cacheid = '';
    }
    
    if (brandid && brandid != null && brandid != '' && brandid.toUpperCase() != 'UNDERFINED')
    {
        param += '&Theme=' + brandid;
        strbrandid = brandid;
    }
    //previous destination.
    if (predest && predest != null && predest != '' && predest.toUpperCase() != 'UNDERFINED')
    {
        param += '&previousdest=' + predest;
        strpredest = predest;
    }
    //what type of product are we looking for?  opt, sst, htl, rail    
    if (triptype && triptype != null && triptype != '' && triptype.toUpperCase() != 'UNDERFINED')
    {
        param += '&triptype=' + triptype;
        strtriptype = triptype;
    }
    //replace the first & char with ? 
    if (param != '')    
    {   param = param.replace('&','?');    }
    
    //check if we already the the response for this RequestType.
    if (strRequestType != 'GETVACBUILDERAMEN')
    {
        strResponse = arrAJAXResponse[strRequestType + strbrandid + strpredest + strtriptype + cacheid];
        if (strResponse && strResponse != '' && strResponse != 'undefined') // && strRequestType.toUpperCase() != 'CARLOCATIONS'
        {
            //we already have it.
            return strResponse;
        }
    }
    else
    {
        //Vacation Builder Amenities uses the param to cache
        strResponse = arrAJAXResponse[strRequestType + param];
        if (strResponse && strResponse != '' && strResponse != 'undefined') // && strRequestType.toUpperCase() != 'CARLOCATIONS'
        {
            //we already have it.
            return strResponse;
        }
    }
    
    xmlhttpobject = GetXMLHTTPObject();
    //if (!xmlhttpobject || xmlhttpobject == 'undefined')
    //{ alert('failed to get xmlhttpobject');return '';  }
    switch (strRequestType.toUpperCase())
    {
        case 'FROMCITYCODE':
            //alert('open and sending request');
            xmlhttpobject.open("POST",'./AJAX.aspx' + param, false);
            xmlhttpobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpobject.send('Action=GETFROMCITYCODES');
            //xmlhttpobject.onreadystatechange = new function statechangehandler()
            //{
                if (xmlhttpobject.readyState == 4 && xmlhttpobject.status == 200)
                {
                    //alert('data is ready');
                    strResponse = xmlhttpobject.responseText;
                    //call function to build dropdown.
                    //attachcitycodehelper(citycodehelpercallby);
                    
                    //save the result for this call
                    arrAJAXResponse[strRequestType + strbrandid + strpredest + strtriptype + cacheid] = xmlhttpobject.responseText;
                }
            //}
            break;   
         case 'TOCITYCODE':
            //alert('open and sending request');
            xmlhttpobject.open("POST",'./AJAX.aspx' + param, false);
            xmlhttpobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpobject.send('Action=GETTOCITYCODES');
            //xmlhttpobject.onreadystatechange = new function statechangehandler()
            //{
                if (xmlhttpobject.readyState == 4 && xmlhttpobject.status == 200)
                {
                    //alert('data is ready');
                    strResponse = xmlhttpobject.responseText;
                    //call function to build dropdown.
                    //attachcitycodehelper(citycodehelpercallby);
                    
                    //save the result for this call
                    arrAJAXResponse[strRequestType + strbrandid + strpredest + strtriptype + cacheid] = xmlhttpobject.responseText;
                }
            //}
            break;   
        case 'PRODUCTCODE':
            //alert('open and sending request');
            xmlhttpobject.open("POST",'./AJAX.aspx' + param, false);
            xmlhttpobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpobject.send('Action=GETPRODUCTCODES');
            //xmlhttpobject.onreadystatechange = new function statechangehandler()
            //{
                if (xmlhttpobject.readyState == 4 && xmlhttpobject.status == 200)
                {
                    //alert('data is ready');
                    strResponse = xmlhttpobject.responseText;
                    //call function to build dropdown.
                    //attachcitycodehelper(citycodehelpercallby);
                    //save the result for this call
                    arrAJAXResponse[strRequestType + strbrandid + strpredest + strtriptype + cacheid] = xmlhttpobject.responseText;
                }
            //}
            break;
        case 'GETPRODUCTTRANSPORTATION':
                    //alert('open and sending request');
            xmlhttpobject.open("POST",'./AJAX.aspx' + param, false);
            xmlhttpobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpobject.send('Action=GETPRODUCTTRANSPORTATION');            
            if (xmlhttpobject.readyState == 4 && xmlhttpobject.status == 200)
            {
                //alert('data is ready');
                strResponse = xmlhttpobject.responseText;
                //call function to build dropdown.
                //attachcitycodehelper(citycodehelpercallby);
                //save the result for this call
                arrAJAXResponse[strRequestType + strbrandid + strpredest + strtriptype + cacheid] = xmlhttpobject.responseText;
            }
            break;
        case 'CARLOCATIONS':            
            xmlhttpobject.open("POST",'./AJAX.aspx' + param, false);
            xmlhttpobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpobject.send('Action=GETCARLOCATIONS');
            //xmlhttpobject.send('Action=GETCARLOCATIONS&CityCode=' + document.getElementById('destination1').value.substring(0, 3));
            
            if (xmlhttpobject.readyState == 4 && xmlhttpobject.status == 200)
            {
                strResponse = xmlhttpobject.responseText;
                //save the result for this call
                //for car save result per from destination
                arrAJAXResponse[strRequestType + strbrandid + strpredest + strtriptype + cacheid] = xmlhttpobject.responseText;
            }
            break;
        case 'GETVACBUILDERAMEN':
            xmlhttpobject.open("POST", './AJAX.aspx' + param, false);
            xmlhttpobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpobject.send('Action=GETVACBUILDERAMEN');
            
            if (xmlhttpobject.readyState == 4 && xmlhttpobject.status == 200)
            {
                strResponse = xmlhttpobject.responseText;
                //save the result forthis call
                //for amenities, save using the Param (hold the productcode)
                arrAJAXResponse[strRequestType + param] = strResponse;
            }
            break;
        default:
        //do nothing
        break;
    }
    //alert('returning data');
    return strResponse;    
}
