var space123 = '\
north:Best of Kashmir:Best of Kashmir|\
north:Desert Extravaganza:Desert Extravaganza|\
north:Hallmarks of Himalayan:Hallmarks of Himalayan|\
north:Mewar Special:Mewar Special|\
north:Himalayan Extravaganza:Himalayan Extravaganza|\
north:North India Highlights:North India Highlights|\
north:Tiger Trails:Tiger Trails|\
north:India Golden Triangle:India Golden Triangle|\
south:Amazing Coorg With Mysore:Amazing Coorg With Mysore|\
south:Best of Southern Hills:Best of Southern Hills|\
south:Hydrebad A.P.Tirupathi:Hydrebad A.P.Tirupathi|\
south:Maharaja Experience:Maharaja Experience|\
south:Mystique Wayanad With Mysore:Mystique Wayanad With Mysore|\
south:Queen of Hills With Mysore:Queen of Hills With Mysore|\
south:Romantic Hilly South:Romantic Hilly South|\
south:Southern Splendor:Southern Splendor|\
south:South At A Stretch:South At A Stretch|\
south:Tranquil At South:Tranquil At South|\
east:Eastern Golden Triangle:Eastern Golden Triangle|\
east:Enchanting Himalayas:Enchanting Himalayas|\
east:Orossa & Alpine Himalayan:Orossa & Alpine Himalayan|\
east:Orissa & North East:Orissa & North East|\
west:Colours of Gujarat:Colours of Gujarat|\
west:Royal Textile Tours:Royal Textile Tours|\
west:Best of Gujarat:Best of Gujarat|\
west:Majestic Coast Tours:Majestic Coast Tours|\
orissa:Adventure Tour-Orissa:Adventure Tour-Orissa|\
orissa:Ethnic Galore-Orissa:Ethnic Galore-Orissa|\
orissa:Ethnic Tribal Tour Orissa:Ethnic Tribal Tour Orissa|\
orissa:Explore the Virgin Beach:Explore the Virgin Beach|\
orissa:Golden Triangle-Chilka Lake:Golden Triangle-Chilka Lake|\
orissa:Golden Triangle of Orissa:Golden Triangle of Orissa|\
orissa:Heritage tour to Orissas:Heritage tour to Orissa|\
orissa:Regalia Tour-Orissa & Kolkata:Regalia Tour-Orissa & Kolkata|\
orissa:Spiritual & Conventional Tour:Spiritual & Conventional Tour|\
orissa:Taj With Konark:Taj With Konark|\
orissa:Tribal Gateway:Tribal Gateway|\
';
var purpose = '\
north:North India Packages|\
south:South India Packages|\
east:North East Packages|\
west:West India Packages|\
orissa:Orissa Packages|\
';
// Save the purpose & space field names
var purposeFieldCfgArray = document.getElementById('cs_config_purpose_field').value.split(' ');
var spaceFieldCfgArray   = document.getElementById('cs_config_space_field').value.split(' ');

// Save the names of the fields that hold the purpose & space default values
var purposeDefaultCfgArray = document.getElementById('cs_config_purpose_default').value.split(' ');
var spaceDefaultCfgArray   = document.getElementById('cs_config_space_default').value.split(' ');

var defaultSpace = false;
var defaultPurpose = false;

function TrimString(sInString) {
   
   if ( sInString ) {

      sInString = sInString.replace( /^\s+/g, "" );// strip leading
      return sInString.replace( /\s+$/g, "" );// strip trailing
   }
}
// Populates the purpose select with the counties from the purpose list
//
function populatePurpose(idName) {

   var purposeLineArray = purpose.split('|');      // Split into lines

   var selObj = document.getElementById( idName );

   selObj.options[0] = new Option('Select Package','');
   selObj.selectedIndex = 0;

   for (var loop = 0; loop < (purposeLineArray.length-1); loop++) {

      lineArray = purposeLineArray[loop].split(':');

      purposeCode  = TrimString(lineArray[0]);
      purposeName  = TrimString(lineArray[1]);
   
      if ( purposeCode != '') {

         selObj.options[loop + 1] = new Option(purposeName, purposeCode);
      }

      if ( defaultPurpose == purposeCode ) {

         selObj.selectedIndex = loop + 1;
      }
   }
}
function populateSpace( spacespaceIdName, purposeIdName ) {

   var selObj = document.getElementById( spaceIdName );
   var foundSpace = false;

   // Empty options just in case new drop down is shorter
   //
   if ( selObj.type == 'select-one' ) {

      selObj.options.length = 0;

      selObj.options[0] = new Option('Package Required','');
      selObj.selectedIndex = 1;
   }
   // Populate the drop down with spaces from the selected purpose
   //
   var spaceLineArray   = space123.split("|");        // Split into lines

   var optionCntr = 1;

   for (var loop = 0; loop < spaceLineArray.length; loop++) {

      lineArray = spaceLineArray[loop].split(":");

      purposeCode  = TrimString(lineArray[0]);
      spaceCode    = TrimString(lineArray[1]);
      spaceName    = TrimString(lineArray[2]);

      if ( document.getElementById( purposeIdName ).value == purposeCode && purposeCode != '' ) {

         // If it's a input element, change it to a select
         //
 if ( selObj.type == 'text' ) {

            parentObj = document.getElementById( spaceIdName ).parentNode;
            parentObj.removeChild(selObj);

            var inputSel = document.createElement("SELECT");
            inputSel.setAttribute("name","space123"); 
            inputSel.setAttribute("id", spaceIdName ); 

            parentObj.appendChild(inputSel) ;

            selObj = document.getElementById( spaceIdName );
            selObj.options[0] = new Option('Package Required','');
            selObj.selectedIndex = 1;
         }
   
         if ( spaceCode != '' ) {

            selObj.options[optionCntr] = new Option(spaceName, spaceCode);
         }
         // See if it's selected from a previous post
         //
         if ( spaceCode == defaultSpace && purposeCode == defaultPurpose ) {

            selObj.selectedIndex = optionCntr;
         }
         foundSpace = true;
         optionCntr++
      }
   }
   // If the purpose has no spaces, change the select to a text box
   //
   if ( ! foundSpace ) {

      parentObj = document.getElementById( spaceIdName ).parentNode;
      parentObj.removeChild(selObj);
 
      // Create the Input Field
      var inputEl = document.createElement("INPUT");

      inputEl.setAttribute("id",  spaceIdName ); 
      inputEl.setAttribute("type", "text"); 
      inputEl.setAttribute("name", "space123"); 
      inputEl.setAttribute("size", 20); 
      inputEl.setAttribute("value", defaultSpace); 
      parentObj.appendChild(inputEl) ;
   }
   
}
// Called when space drop down is changed
// 
function updateSpace( purposeIdNameIn ) {

   for (var loop = 0; loop < purposeFieldCfgArray.length; loop++) {
   
      purposeIdName  = purposeFieldCfgArray[loop];
      spaceIdName    = spaceFieldCfgArray[loop];

      // Read the default value hidden fields
      defaultPurpose = document.getElementById( purposeDefaultCfgArray[loop] ).value;
      defaultSpace   = document.getElementById( spaceDefaultCfgArray[loop] ).value;

      if ( purposeIdNameIn == purposeIdName ) {

         populateSpace( spaceIdName, purposeIdName );
      }
   }
}
// Initialize the drop downs
// 
function initPurpose() {

   for (var loop = 0; loop < purposeFieldCfgArray.length; loop++) {
   
      purposeIdName  = purposeFieldCfgArray[loop];
      spaceIdName    = spaceFieldCfgArray[loop];

      // Read the default value hidden fields
      defaultPurpose = document.getElementById( purposeDefaultCfgArray[loop] ).value;
      defaultSpace   = document.getElementById( spaceDefaultCfgArray[loop] ).value;

      populatePurpose( purposeIdName);
      populateSpace( spaceIdName, purposeIdName );
   }
}

