
$(document).ready(function(){

     $.ajaxSetup({timeout: 1200000});

     //change events for location dropdowns
     $('#Country').change(function() {
        getStateList($(this).val());
     });
     
     $('#State').change( function() {
         getLocationList($(this).val());
     });
     


} )//END ON DOC LOAD ==========================


/* ==========================================
 * Delete custom background from server
*/
function getStateList(country)
{

   
    if (country){
        $('#SiteID').hide();
        $('#location-note').text('Select the country and state or province above').show(); 
        $('#State').hide();
        //kill any error tips for items we'll be changing           
        if ($('#State').data('qtip'))
        {    
            $('#State').qtip("destroy");
            
         } 
         if ( $('#SiteID').data('qtip') )
         {
            $('#SiteID').qtip("destroy");
         } 
        
             
        if (country == 'ca'){
            $('label[for=State]').text('Province');
                        
        } else {
            $('label[for=State]').text('State');      
        }
        
        if (country=='bm' || country=='vi'){
            //hide state selector for these countries 
            $('label[for=State]').parent().hide();
             $('#State').val('');             
            getLocationList(country);
        }
        else {
             $('#state-spinner').show(); 
             $('label[for=State]').parent().show(); 
        
            $.ajax({
                    type: "POST",
                    url:'/register',
                    data: "task=getStateList&country=" + country,
                    dataType: "html",
                    success: function(returnHtml) {
                        $('#State').replaceWith(returnHtml);
                        $('#State').change( function() {
                            getLocationList($(this).val());
                        });
                        $('#state-spinner').hide();

                     },
                     failure: function() {
                        showPageError("Failed to get states or provinces","error");
                        $('#state-spinner').hide();     
                     }
            });
        }
    }
}

/* ==========================================
 * Delete custom background from server
*/
function getLocationList(state)
{

    if (state){
        $('#location-note').hide();
        $('#location-spinner').show(); 
        
        if ( $('#SiteID').data('qtip') ){
            $('#SiteID').qtip("destroy").remove();
        }
        
        $.ajax({
                type: "POST",
                url:'/register',
                data: "task=getLocationList&state=" + state,
                dataType: "html",
                success: function(returnHtml) {
                    if (returnHtml == '0'){
                         $('#SiteID').hide();
                         $('#location-note').text('No locations were found').show();
                    } else {
                    $('#SiteID').unbind();
                        //returnHtml = '<p class="note">No locations were found</p>';
                        if ($('#SiteID').length)
                            $('#SiteID').replaceWith(returnHtml).show();
                        else
                            $('label[for=Location]').after(returnHtml);
                    }   
                     $('#location-spinner').hide(); 

                 },
                 failure: function() {
                    showPageError("Failed to get locations","error");
                    $('#location-spinner').hide(); 
                 }
        });
    }
}
