var global_loc_code = "";
var global_loc_name = "";

function changeVal(supplier) {
   // Arrays with location info included in main file from get_location_arr.js (generated by get_location_array.php)   

   word = new String(document.step1.myText.value); // Get the value from the text box
   word = word.toLowerCase();
   var i = 0;
   var found = "";
   
   // If there is a word typed and it has more than 1 character
   if (word != "" && word.length > 1) {
      if (supplier=="all") {
      // Loop through each array and create a string with all details to search through
         while (i < loc_name_arr.length) {
            var str2=loc_name_arr[i].toLowerCase() + " " + loc_code_arr[i].toLowerCase() + " " + loc_region_arr[i].toLowerCase();
            var word_check = str2.indexOf(word); //Check if the array word has the word the user typed in
           
            // If there was a match, ad it to the found str
            if (word_check != -1) {
               var loc_name=loc_name_arr[i];
               var loc_code=loc_code_arr[i];
               
               var img = ""
               if (loc_name.indexOf("Airport")!=-1) //If an airport, put in icon
               {
                      img = "<img src='/images/airplane.png'  class=\"noborder\" />";
               }
              else
              {
                      img = "<img src='/images/blank_image.png'  class=\"noborder\" />";
              }
             if (document.step1.airport_pu.checked == true) //Only Airports
             {      
                    if (loc_name.indexOf("Airport")!=-1) 
                   {
                          if (found == "")
                          {
                              global_loc_code = loc_code;
                              global_loc_name = loc_name;
                              found ="<table class=\"div_link\"><tr><td>"+  img + "</td><td><a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\">" + loc_name + "</a></td></tr></table>";
                          }
                          else
                          {
                              found = found + "<table class=\"div_link\"><tr><td>"+img +"</td><td> <a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                          }
                    }
             }
             else //Everywhere
             {               
                  if (found == "")         
                  {
                        found =  "<table class=\"div_link\"><tr><td>"+ img +"</td><td><a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                  }
                  else
                  {
                       found =  found + "<table class=\"div_link\"><tr><td>"+ img +"</td><td><a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                  }
             }

          } // if

            i++;
        
         } // end while
      } else if (supplier=="avis"){
      
      
         // Loop through each array and create a string with all details to search through
         while (i < loc_name_arr_avis.length) {
            var str2=loc_name_arr_avis[i].toLowerCase() + " " + loc_code_arr_avis[i].toLowerCase() + " " + loc_region_arr_avis[i].toLowerCase();
            var word_check = str2.indexOf(word); //Check if the array word has the word the user typed in
      
            // If there was a match, ad it to the found str
            if (word_check != -1) {
               var loc_name=loc_name_arr_avis[i];
               var loc_code=loc_code_arr_avis[i];
            
               var img = ""
               if (loc_name.indexOf("Airport")!=-1) //If an airport, put in icon
               {
                      img = "<img src='/images/airplane.png'  class=\"noborder\" />";
               }
              else
              {
                      img = "<img src='/images/blank_image.png'  class=\"noborder\" />";
              }
             if (document.step1.airport_pu.checked == true) //Only Airports
             {      
                    if (loc_name.indexOf("Airport")!=-1) 
                   {
                          if (found == "")
                          {
                              global_loc_code = loc_code;
                              global_loc_name = loc_name;
                              found ="<table class=\"div_link\"><tr><td>"+  img + "</td><td><a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\">" + loc_name + "</a></td></tr></table>";
                          }
                          else
                          {
                              found = found + "<table class=\"div_link\"><tr><td>"+img +"</td><td> <a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                          }
                    }
             }
             else //Everywhere
             {               
                  if (found == "")         
                  {
                        found =  "<table class=\"div_link\"><tr><td>"+ img +"</td><td><a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                  }
                  else
                  {
                       found =  found + "<table class=\"div_link\"><tr><td>"+ img +"</td><td><a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                  }
             }
            } // if
      
            i++;
         
         } // end while
      
      
      
      
      } else if (supplier=="budget"){
      
      
         // Loop through each array and create a string with all details to search through
         while (i < loc_name_arr_budget.length) {
            var str2=loc_name_arr_budget[i].toLowerCase() + " " + loc_code_arr_budget[i].toLowerCase() + " " + loc_region_arr_budget[i].toLowerCase();
            var word_check = str2.indexOf(word); //Check if the array word has the word the user typed in
      
            // If there was a match, ad it to the found str
            if (word_check != -1) {
               var loc_name=loc_name_arr_budget[i];
               var loc_code=loc_code_arr_budget[i];
            
               var img = ""
               if (loc_name.indexOf("Airport")!=-1) //If an airport, put in icon
               {
                      img = "<img src='/images/airplane.png'  class=\"noborder\" />";
               }
              else
              {
                      img = "<img src='/images/blank_image.png'  class=\"noborder\" />";
              }
             if (document.step1.airport_pu.checked == true) //Only Airports
             {      
                    if (loc_name.indexOf("Airport")!=-1) 
                   {
                          if (found == "")
                          {
                              global_loc_code = loc_code;
                              global_loc_name = loc_name;
                              found ="<table class=\"div_link\"><tr><td>"+  img + "</td><td><a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\">" + loc_name + "</a></td></tr></table>";
                          }
                          else
                          {
                              found = found + "<table class=\"div_link\"><tr><td>"+img +"</td><td> <a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                          }
                    }
             }
             else //Everywhere
             {               
                  if (found == "")         
                  {
                        found =  "<table class=\"div_link\"><tr><td>"+ img +"</td><td><a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                  }
                  else
                  {
                       found =  found + "<table class=\"div_link\"><tr><td>"+ img +"</td><td><a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                  }
             }
            } // if
      
            i++;
         
         } // end while
      
      

      
      } else if (supplier=="hertz"){
      
      
         // Loop through each array and create a string with all details to search through
         while (i < loc_name_arr_hertz.length) {
            var str2=loc_name_arr_hertz[i].toLowerCase() + " " + loc_code_arr_hertz[i].toLowerCase() + " " + loc_region_arr_hertz[i].toLowerCase();
            var word_check = str2.indexOf(word); //Check if the array word has the word the user typed in
      
            // If there was a match, ad it to the found str
            if (word_check != -1) {
               var loc_name=loc_name_arr_hertz[i];
               var loc_code=loc_code_arr_hertz[i];
            
               var img = ""
               if (loc_name.indexOf("Airport")!=-1) //If an airport, put in icon
               {
                      img = "<img src='/images/airplane.png'  class=\"noborder\" />";
               }
              else
              {
                      img = "<img src='/images/blank_image.png'  class=\"noborder\" />";
              }
             if (document.step1.airport_pu.checked == true) //Only Airports
             {      
                    if (loc_name.indexOf("Airport")!=-1) 
                   {
                          if (found == "")
                          {
                              global_loc_code = loc_code;
                              global_loc_name = loc_name;
                              found ="<table class=\"div_link\"><tr><td>"+  img + "</td><td><a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\">" + loc_name + "</a></td></tr></table>";
                          }
                          else
                          {
                              found = found + "<table class=\"div_link\"><tr><td>"+img +"</td><td> <a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                          }
                    }
             }
             else //Everywhere
             {               
                  if (found == "")         
                  {
                        found =  "<table class=\"div_link\"><tr><td>"+ img +"</td><td><a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                  }
                  else
                  {
                       found =  found + "<table class=\"div_link\"><tr><td>"+ img +"</td><td><a href=\"javascript:g_location('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                  }
             }
            } // if
      
            i++;
         
         } // end while
      
      
      
      
      } // end if hertz
      
     found = found+"</table>"; 

     document.getElementById("myDiv").innerHTML = found; // Write the chosen locations to the div
     document.getElementById("div_fix1").style.display = "block";
     document.getElementById("myDiv").style.display = "block"; // Display the div

   } else {
      document.getElementById("myDiv").innerHTML = ""; // If there is nothing in the box don't show anything in the div
      document.getElementById("div_fix1").style.display = "none";
      document.getElementById("myDiv").style.display = "none";
   }// end if word
} // end function







// When the user selects a location write it to the text box and hidden field and clear results div
function g_location(loc_code,loc_name) {
   document.step1.myText.value = loc_name; 
   document.getElementById("myDiv").innerHTML = "";
   document.getElementById("myDiv").style.display = "none";
   document.getElementById("div_fix1").style.display = "none";
   document.step1.PULOC.value = loc_code;  
}

function removeText() { 

   if (document.step1.myText.value == 'Type in a City Location...') {
      document.step1.myText.value = '';
   }
   
}

// These functions are for the drop off location, just writing to different boxes
function changeVal2() {
   // Arrays with location info included in main file from get_location_arr.js (generated by get_location_array.php)   

   word = new String(document.step1.myText2.value); // Get the value from the text box
   word = word.toLowerCase();
   var i = 0;
   var found = "";
   
   // If there is a word typed and it has more than 1 character
   if (word != "" && word.length > 1) {

      // Loop through each array and create a string with all details to search through
      while (i < loc_name_arr.length) {
         var str2=loc_name_arr[i].toLowerCase() + " " + loc_code_arr[i].toLowerCase() + " " + loc_region_arr[i].toLowerCase();
         var word_check = str2.indexOf(word); //Check if the array word has the word the user typed in
      
         // If there was a match, ad it to the found str
         if (word_check != -1) {
            var loc_name=loc_name_arr[i];
            var loc_code=loc_code_arr[i];
            var img = ""
            if (loc_name.indexOf("Airport")!=-1) //If an airport, put in icon
            {
                    img = "<img src='/images/airplane.png'  />";
            }
           else
           {
                    img = "<img src='/images/blank_image.png'  />";
            }

             if (document.step1.airport_do.checked == true) //Only Airports
             {      
                    if (loc_name.indexOf("Airport")!=-1) //If an airport, put in icon
                   {
                          if (found == "") //highlight the first link in blue
                          {
                               found =  "<table class=div_link><tr><td>"+img +"</td><td><a href=\"javascript:g_location2('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                          }
                          else
                          {
                                found = found + "<table class=div_link><tr><td>"+img +"</td><td><a href=\"javascript:g_location2('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                          }
                    }
              }
              else //Everywhere
              {               
                  if (found == "") //highlight the first link in blue
                  {
                       found =  "<table class=div_link><tr><td>"+img +"</td><td><a href=\"javascript:g_location2('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                  }
                  else
                  {
                       found = found + "<table class=div_link><tr><td>"+img +"</td> <td><a href=\"javascript:g_location2('" + loc_code + "','" + loc_name + "');\" >" + loc_name + "</a></td></tr></table>";
                  }
              }

         } // if
      
         i++;
         
      } // end while
     found = found 
     document.getElementById("myDiv2").innerHTML = found; // Display the div
     document.getElementById("div_fix2").style.display= "block";
     document.getElementById("myDiv2").style.display = "block"; // Write the chosen locations to the div

     
   } else {

      document.getElementById("div_fix2").style.display= "none";
      document.getElementById("myDiv2").innerHTML = ""; // If there is nothing in the box don't show anything in the div
      document.getElementById("myDiv2").style.display = "none";

   }// end if word
} // end function

// When the user selects a location write it to the text box and hidden field and clear results div
function g_location2(loc_code,loc_name) { 
   document.step1.myText2.value = loc_name; 
   document.getElementById("myDiv2").innerHTML = "";
   document.getElementById("myDiv2").style.display = "none";
   document.getElementById("div_fix2").style.display = "none";


   document.step1.DOLOC.value = loc_code; 
}

function removeText2() {

   if (document.step1.myText2.value == 'Same as Pick Up') {
      document.step1.myText2.value = '';
   }
}

// Show all the locations
function pop_puloc() {
   if (document.step1.myText2.value == 'Same as Pick Up') {
      document.step1.myText2.value = '';
   }
}


//check if the user has clickoutside the drop down location menu
function checkDropdown()
{

       document.getElementById("myDiv").style.display = "none" 
       document.getElementById("myDiv2").style.display = "none" 
       document.getElementById("div_fix1").style.display = "none" 
       document.getElementById("div_fix2").style.display = "none"
}

//if the user clicks within the locations div, keep it there
function keepPopUp()
{ 
       document.getElementById("div_fix1").style.display = "inline" 
       document.getElementById("div_fix2").style.display = "inline" 
       document.getElementById("myDiv").style.display = "inline" 
       document.getElementById("myDiv2").style.display = "inline" 
}


//Stops the form submitting if the user hits enter
function stopEnter(e)
{
    var key = window.event ? e.keyCode : e.which;
    if (key == 13) //enter
   {
       return false;
   }
}

//If the field is emtpy, replace with standard text
function ifEmpty()
{ 
    if (document.step1.myText.value == "")
     {  
         document.step1.myText.value = 'Type in a City Location...'
     }
    if (document.step1.myText2.value == "")
    {
         document.step1.myText2.value = 'Same as Pick Up'
    }
} 


