/*--- ΕΔΩ ΔΗΛΩΝΕΤΑΙ ΤΟ ΣΚΑΦΟΣ ΤΗΣ ΚΑΘΕ ΚΑΤΗΓΟΡΙΑΣ ΜΕ ΤΗΝ ΚΑΤΑΛΛΗΛΗ ΜΕΤΑΒΛΗΤΗ ---*/
/*
 * RB: (δεν έχει σκάφη)
 * WF: (δεν έχει σκάφη)
*/

var state = '\
SY:Atlantic 55:Atlantic 55|\
SY:GIANETTI 65:GIANETTI 65|\
SY:Skipper 63:Skipper 63|\
SY:Atlantic 61:Atlantic 61|\
SY:Jeanneau Sun Odyssey 54 DS:Jeanneau Sun Odyssey 54 DS|\
SY:Jeanneau Sun Odyssey 52.2:Jeanneau Sun Odyssey 52.2|\
SY:Jeanneau Sun Odyssey 49+49DS:Jeanneau Sun Odyssey 49+49DS|\
SY:Beneteau 57:Beneteau 57|\
SY:Jeanneau Sun Odyssey 43:Jeanneau Sun Odyssey 43|\
SY:Beneteau Oceanis Clipper 52.3:Beneteau Oceanis Clipper 52.3|\
SY:Jeanneau Sun Odyssey 40.3:Jeanneau Sun Odyssey 40.3|\
SY:Beneteau Cyclades 50,5:Beneteau Cyclades 50,5|\
SY:Beneteau Oceanis Clipper 46.1:Beneteau Oceanis Clipper 46.1|\
SY:Jeanneau Sun Odyssey 32:Jeanneau Sun Odyssey 32|\
SY:Beneteau Oceanis 46:Beneteau Oceanis 46|\
SY:Beneteau Oceanis 440:Beneteau Oceanis 440|\
SY:Beneteau Cyclades 43.3:Beneteau Cyclades 43.3|\
SY:Beneteau Oceanis 40:Beneteau Oceanis 40|\
SY:Ocean Star 56.1:Ocean Star 56.1|\
SY:Ocean star 51.2:Ocean star 51.2|\
SY:Jeanneau Sun Odyssey 36:Jeanneau Sun Odyssey 36|\
SY:Jeanneau Sun Odyssey 32+32i:Jeanneau Sun Odyssey 32+32i|\
SY:Jeanneau Sun Odyssey 49i:Jeanneau Sun Odyssey 49i|\
SY:Jeanneau Sun Odyssey 39i:Jeanneau Sun Odyssey 39i|\
SY:Jeanneau Sun Odyssey 42i:Jeanneau Sun Odyssey 42i|\
SY:Jeanneau Sun Odyssey 45:Jeanneau Sun Odyssey 45|\
SY:Jeanneau Sun Odyssey 36i:Jeanneau Sun Odyssey 36i|\
CY:SUNREEF 62:SUNREEF 62|\
CY:Lagoon 500:Lagoon 500|\
CY:Lagoon 470:Lagoon 470|\
CY:Lagoon 440:Lagoon 440|\
CY:Lagoon 420:Lagoon 420|\
CY:Lagoon 410:Lagoon 410|\
CY:Lagoon 380:Lagoon 380|\
CY:Nautitech 47:Nautitech 47|\
CY:Nautitech 44:Nautitech 44|\
CY:Nautitech 435:Nautitech 435|\
CY:Nautitech 40:Nautitech 40|\
CY:FOUNTAINE PAJOT Belize 43:FOUNTAINE PAJOT Belize 43|\
MY:OURANOS TOO:OURANOS TOO|\
RY:Liana H:Liana H|\
TY:Prestige 46:Prestige 46|\
';

/*--- ΕΔΩ ΔΗΛΩΝΕΤΑΙ Η ΚΑΘΕ ΚΑΤΗΓΟΡΙΑ ΜΕ ΤΗΝ ΑΝΤΙΣΤΟΙΧΗ ΜΕΤΑΒΛΗΤΗ ---*/

var country = '\
SY:Sailing Yachts|\
CY:Catamaran Yachts|\
MY:MEGA Yachts|\
RY:Motorsailer Yachts|\
TY:Motor Yachts|\
RB:Rib Boats|\
WF:Traditional Greek Wooden Fishboats|\
';

// Save the country & state field names
var countryFieldCfgArray = document.getElementById('clientcountry').value.split(' ');
var stateFieldCfgArray   = document.getElementById('clientstate').value.split(' ');

// Save the names of the fields that hold the country & state default values
var countryDefaultCfgArray = document.getElementById('clientcountry_default').value.split(' ');
var stateDefaultCfgArray   = document.getElementById('clientstate_default').value.split(' ');

var defaultState = false;
var defaultCountry = false;

function TrimString(sInString) {
   
   if ( sInString ) {

      sInString = sInString.replace( /^\s+/g, "" );// strip leading
      return sInString.replace( /\s+$/g, "" );// strip trailing
   }
}
// Populates the country select with the counties from the country list
//
function populateCountry(idName) {

   var countryLineArray = country.split('|');      // Split into lines

   var selObj = document.getElementById( idName );

   selObj.options[0] = new Option('select Boat Type','');
   selObj.selectedIndex = 0;

   for (var loop = 0; loop < countryLineArray.length; loop++) {

      lineArray = countryLineArray[loop].split(':');

      countryCode  = TrimString(lineArray[0]);
      countryName  = TrimString(lineArray[1]);
   
      if ( countryCode != '' ) {

         selObj.options[loop + 1] = new Option(countryName, countryCode);
      }

      if ( defaultCountry == countryCode ) {

         selObj.selectedIndex = loop + 1;
      }
   }
}
function populateState( statestateIdName, countryIdName ) {

   var selObj = document.getElementById( stateIdName );
   var foundState = 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('select Boat','');
      selObj.selectedIndex = 0;
   }
   // Populate the drop down with states from the selected country
   //
   var stateLineArray   = state.split("|");        // Split into lines

   var optionCntr = 1;

   for (var loop = 0; loop < stateLineArray.length; loop++) {

      lineArray = stateLineArray[loop].split(":");

      countryCode  = TrimString(lineArray[0]);
      stateCode    = TrimString(lineArray[1]);
      stateName    = TrimString(lineArray[2]);

      if ( document.getElementById( countryIdName ).value == countryCode && countryCode != '' ) {

         // If it's a input element, change it to a select
         //
         if ( selObj.type == 'text' ) {

            parentObj = document.getElementById( stateIdName ).parentNode;
            parentObj.removeChild(selObj);

            var inputSel = document.createElement("SELECT");
            inputSel.setAttribute("name","state"); 
            inputSel.setAttribute("id", stateIdName ); 

            parentObj.appendChild(inputSel) ;

            selObj = document.getElementById( stateIdName );
            selObj.options[0] = new Option('select Boat','');
            selObj.selectedIndex = 0;
         }
   
         if ( stateCode != '' ) {

            selObj.options[optionCntr] = new Option(stateName, stateCode);
         }
         // See if it's selected from a previous post
         //
         if ( stateCode == defaultState && countryCode == defaultCountry ) {

            selObj.selectedIndex = optionCntr;
         }
         foundState = true;
         optionCntr++
      }
   }
   // If the country has no states, change the select to a text box
   //
   if ( ! foundState ) {

      parentObj = document.getElementById( stateIdName ).parentNode;
      parentObj.removeChild(selObj);
 
      // Create the Input Field
      var inputEl = document.createElement("INPUT");

      inputEl.setAttribute("id",  stateIdName ); 
      inputEl.setAttribute("type", "text"); 
      inputEl.setAttribute("name", "state"); 
      inputEl.setAttribute("size", 20); 
      inputEl.setAttribute("value", defaultState); 
      parentObj.appendChild(inputEl) ;
   }
   
}
// Called when state drop down is changed
// 
function updateState( countryIdNameIn ) {

   for (var loop = 0; loop < countryFieldCfgArray.length; loop++) {
   
      countryIdName  = countryFieldCfgArray[loop];
      stateIdName    = stateFieldCfgArray[loop];

      // Read the default value hidden fields
      defaultCountry = document.getElementById( countryDefaultCfgArray[loop] ).value;
      defaultState   = document.getElementById( stateDefaultCfgArray[loop] ).value;

      if ( countryIdNameIn == countryIdName ) {

         populateState( stateIdName, countryIdName );
      }
   }
}
// Initialize the drop downs
// 
function initCountry() {

   for (var loop = 0; loop < countryFieldCfgArray.length; loop++) {
   
      countryIdName  = countryFieldCfgArray[loop];
      stateIdName    = stateFieldCfgArray[loop];

      // Read the default value hidden fields
      defaultCountry = document.getElementById( countryDefaultCfgArray[loop] ).value;
      defaultState   = document.getElementById( stateDefaultCfgArray[loop] ).value;

      populateCountry( countryIdName);
      populateState( stateIdName, countryIdName );
   }
}
