// DOM ready

function clickable()
{
    jQuery("#regionSelector").bind('change', function() {
          var getState = jQuery(this).val();
          
          if(getState != '')
          {
              var stateBox = jQuery(document.getElementById(getState)).clone( true ); 
              jQuery("#stateArea").empty();
              stateBox.attr("id", "location");
              stateBox.attr("name", "location");
              stateBox.appendTo('#stateArea');
          }
    }); 
    
    //the 2 is for home page newletter area so it does not conflict with the popup
    jQuery("#regionSelector2").bind('change', function() {
      var getState = jQuery(this).val();

      if(getState != '')
      {
          var stateBox = jQuery(document.getElementById(getState + '-2')).clone( true ); 
          jQuery("#stateArea2").empty();
          stateBox.attr("id", "location2");
          stateBox.attr("name", "location2");
          stateBox.appendTo('#stateArea2');
      }
    }); 
    
    jQuery("#requestmore, #request-quote").bind('click', function() {    
        jQuery("#backdrop").css("display","");

        jQuery('#contact-form-part1').dialog( {
              close: function(event, ui) {
                  jQuery("#backdrop").css("display","none");
              }
        });

        jQuery("#contact-form-part1").dialog( "option", "width", 600 );
        jQuery("#contact-form-part1").dialog( "option", "height", 500 );
        jQuery('#contact-form-part1').dialog('option', 'position', 'center');
    }); 
    
     jQuery(".storeClicker").bind('click', function() { 
        var selectedStore = jQuery(this).text();
        var selectedState = jQuery(this).attr("value");
        
        jQuery('#contact-form-part1').dialog('close');
         
        jQuery("#backdrop").css("display","");
        
        jQuery('#contact-form-part2').dialog( {
              close: function(event, ui) {
                  jQuery("#backdrop").css("display","none");
              }
        });

        jQuery("#contact-form-part2").dialog( "option", "width", 600 );
        jQuery("#contact-form-part2").dialog( "option", "height", 500 );
        jQuery('#contact-form-part2').dialog('option', 'position', 'center');

       var regionSelector = document.getElementById("regionSelector");

       for(var i = 0; i < regionSelector.length; i++)
       {
          if(regionSelector.options[i].text == selectedState)
          {
                  regionSelector.selectedIndex = i;
                  break;
          }
              
          
       }
         
        jQuery("#regionSelector").trigger('change'); //let the system know we changed the region
         
         jQuery("#location").val(selectedStore);
         
         //now set the selected location
         var location = document.getElementById("location");

           for(var i = 0; i < location.length; i++)
           {
              if(location.options[i].text == selectedStore)
              location.selectedIndex = i;
           }
         
    }); 
}

jQuery(document).ready(function () {
    clickable();
    var backdrop = jQuery("#backdrop_temp").clone( true )
    backdrop.attr("id","backdrop");
    jQuery("body").prepend(backdrop);
}); 
