function hideAddress(i)
{   
    if (i == 'addaddress') {
    //  jQuery('form')[0].reset();
    $('#add-address').hide();
    } else {
    $('#address-container-'+i).hide();
     }
   if (i == 'add-area') {
   // jQuery('form')[0].reset();
    $('#add-area').hide();
    jQuery('#cities').removeAttr('disabled');
    jQuery('#whole').val('');
    }
   if (i == 'edit-area') {
   //jQuery('form')[0].reset();
    $('#edit-area').hide();
   // jQuery('form')[0].reset();
    }  
    
   if (i == 'edit-testimonial') {
   //jQuery('form')[0].reset();
    $('#edit-testimonial').hide();
     jQuery('form')[0].reset();
   // jQuery('form')[0].reset();
    }  
    
       if (i == 'add-testimonial') {
   //jQuery('form')[0].reset();
    $('#add-testimonial').hide();
     jQuery('form')[0].reset();
   // jQuery('form')[0].reset();
    }  
    
    if (i == 'add-town') {

    $('.add_town_form').hide();
    jQuery('#remove_town').removeAttr('disabled');
    jQuery('#whole').removeAttr('disabled');
        jQuery('#add_town').attr('checked', false);
           jQuery('#cities').removeAttr('disabled', 'disabled');
       jQuery('#counties').removeAttr('disabled', 'disabled');
       //jQuery('#save').attr("onclick","saveArea('close')");
       //jQuery('#cancel').attr("onclick","hideAddress('add-area')");
       //jQuery("#save").removeClass("disabled-link");
       //jQuery("#cancel").removeClass("disabled-link");
	   jQuery('#save').remove();
	   jQuery('#cancel').remove();
	   jQuery('#remove-link').html(jQuery("<a onclick=\"saveArea('close');\" id=\"save\" class=\"display\" >save</a>&nbsp;| <a class=\"display\" id=\"cancel\" onclick=\"hideAddress('add-area')\">close</a>"));
   // jQuery('form')[0].reset();
    }  
    
    if (i == 'remove-town') {
    $('.remove_town_form').hide();
    jQuery('#add_town').removeAttr('disabled');
    jQuery('#whole').removeAttr('disabled');
    jQuery('#remove_town').attr('checked', false);
                      //enable save elements
       jQuery('#cities').removeAttr('disabled', 'disabled');
       jQuery('#counties').removeAttr('disabled', 'disabled');
       //jQuery('#save').attr("onclick","saveArea('close')");
       //jQuery('#cancel').attr("onclick","hideAddress('add-area')");
       //jQuery("#save").removeClass("disabled-link");
       //jQuery("#cancel").removeClass("disabled-link");
	   jQuery('#save').remove();
	   jQuery('#cancel').remove();
	   jQuery('#remove-link').html(jQuery("<a onclick=\"saveArea('close');\" id=\"save\" class=\"display\" >save</a>&nbsp;| <a class=\"display\" id=\"cancel\" onclick=\"hideAddress('add-area')\">close</a>"));
    }  
     
}

function updateAreasOnClose()
{   

    $('#edit-area').hide();
    var tmp = new Array('trader_account_id', 'counties', 'cities' ); 
    var data_string = getDataStringByIds( tmp ) ;
    //alert(data_string);
    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/getCountiesAndCities",
      success: function( ret_val) {
            if (ret_val) {
                     jQuery('ul.test1').html(jQuery(ret_val));
                      initClickAdd();
                }
      }
    });
     
}

function getDataStringByIds(arr)
{
  var documentData = '';
  var add_zn = '';
  for(var obj in arr){
    if ( obj == 0 ) {
       add_zn ='';
    } else {
       add_zn ='&'; 
    }
    documentData += add_zn+arr[obj]+'='+jQuery('#'+arr[obj]).val();    
  } 
  return documentData;
}


function showAddress(i)
{   

    jQuery('#address-container-'+i).show();

}

function saveAddress(i)
{   
     
    var tmp = new Array( 'trader_account_id', 'address_id', 'users_id', 'address1-add', 'address2-add', 'city-add', 'county-add', 'GETcode-add'); 
    
    var data_string = getDataStringByIds( tmp ) ;
    
    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/saveData",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
                     jQuery('#add-address').hide();
                     jQuery('ul.test').html(jQuery(ret_val));
                     jQuery('form')[0].reset();
                     initClickAdd();
            }
 
      }
    });
     
}

function saveArea(i)
{    

    var no_close = i;
    var tmp = new Array('trader_account_id', 'counties', 'cities', 'whole', 'hidden'); 
    var data_string = getDataStringByIds( tmp ) ;
    var county_id = jQuery('#counties').val();
    var city_id = jQuery('#cities').val();
    var whole = jQuery('#whole').val();
    if(!city_id && !county_id || county_id == 'county' || county_id == 'country-england' || county_id == 'country-scotland' || county_id == 'country-wales') {
    
    alert('Please select a county and a town');
    return;
    
    }
    
    if(!city_id && county_id && county_id != 'county' && county_id != 'country-england' && county_id != 'country-scotland' && county_id != 'country-wales' && !whole) {
    
    alert('Please select a city/town');
    return;
    
    }
    
    
    if(city_id && !county_id) {
    
    alert('Please select a county');
    return;
    
    }
    
    if(city_id && county_id == 'country-england' || county_id == 'country-scotland' || county_id == 'country-wales') {
    
    alert('Please select a county');
    return;
    
    }
    
    if(!city_id && county_id == 'country-england' || county_id == 'country-scotland' || county_id == 'country-wales') {
    
    alert('Please select a county');
    return;
    
    }
    
   // alert(data_string);
    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/saveArea",
      success: function( ret_val) {
            if (ret_val) {
                    if(ret_val == 'This town already exists') {
                     //alert(ret_val);
                     }
                     jQuery('#cities').removeAttr('disabled');
                     if(no_close == 'close') {
                    // jQuery('#add-area').hide();
                     } 
                     if(no_close == 'no-close') {
                      //alert(ret_val);
                     jQuery('#cities').html(jQuery(ret_val));
                     jQuery('#cities').children().each( function() {
                      jQuery(this).attr('selected', false);
                       });
                      }
                     jQuery('.add_town').hide();
                     if(ret_val != 'This town already exists') {
                     jQuery('ul.test1').html(jQuery(ret_val));
                     var test = jQuery('#areas').val();
                     if (test == 'on') { 
                     jQuery('#areas').val('off');
                     }
                     }
                     jQuery('#whole').val('');
                     jQuery('#whole').attr('checked', false);
                     jQuery('#add_town').removeAttr('disabled');
                     jQuery('#remove_town').removeAttr('disabled');
                     initClickAdd();
                }
      }
    });
     
}

function saveAddTown()
{    
    var tmp = new Array('trader_account_id', 'counties_add', 'new_town'); 
    var data_string = getDataStringByIds( tmp ) ;
    var county_id = jQuery('#counties').val();
    var county_id_remove = jQuery('#counties_remove').val();
    var county_id_add = jQuery('#counties_add').val();
    var new_town = jQuery('#new_town').val();
    if(!new_town && !county_id_add) {
    
    alert('Please select a county and enter a name of a Town');
    return;
    
    }
    
    if(!new_town && county_id_add) {
    
    alert('Please enter a name of a city/town');
    return;
    
    }
    
    
    if(new_town && !county_id_add) {
    
    alert('Please select a county');
    return;
    
    }
    
    if(new_town && county_id_add == 'country-england' || county_id_add == 'country-scotland' || county_id_add == 'country-wales') {
    
    alert('Please select a county');
    return;
    
    }
    
    if(!new_town && county_id_add == 'country-england' || county_id_add == 'country-scotland' || county_id_add == 'country-wales') {
    
    alert('Please select a county');
    return;
    
    }

    //alert(data_string);
    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/saveAddTown",
      success: function( ret_val) {
           if (ret_val) {
                     //alert(ret_val);
                     if (ret_val == 'This town already exists') {
                     alert('This town already exists');
                     }
                     if (ret_val != 'This town already exists') {
                     alert('This town has been added');
                     }
                     if(county_id_add == county_id) {
                     jQuery('#cities').html(jQuery(ret_val));
                     jQuery('#cities').children().each( function() {
                      jQuery(this).attr('selected', false);
                       });
                      }

                     if(county_id_remove == county_id_add) {
                      jQuery('#cities_remove').html(jQuery(ret_val));
                      jQuery('#cities_remove').children().each( function() {
                      jQuery(this).attr('selected', false);
                       });
                      }
                     initClickAdd();
              }
      }
    });
     
}

function saveRemoveTown()
{   


    
    var tmp = new Array('counties_remove','cities_remove','trader_account_id'); 
    var data_string = getDataStringByIds( tmp ) ;
    var county_id = jQuery('#counties').val();
    var county_id_remove = jQuery('#counties_remove').val();
    var cities_remove = jQuery('#cities_remove').val();
        //alert(data_string);
    if(!cities_remove && !county_id_remove || county_id_remove == 'county' || county_id_remove == 'country-england' || county_id_remove == 'country-scotland' || county_id_remove == 'country-wales') {
    
    alert('Please select a county and a town');
    return;
    
    }
    
    if(!cities_remove && county_id_remove && county_id_remove != 'county' && county_id_remove != 'country-england' && county_id_remove != 'country-scotland' && county_id_remove != 'country-wales') {
    
    alert('Please enter a name of a city/town');
    return;
    
    }
    
    
    if(cities_remove && !county_id_remove) {
    
    alert('Please select a county');
    return;
    
    }
    
    if(cities_remove && county_id_remove == 'country-england' || county_id_remove == 'country-scotland' || county_id_remove == 'country-wales') {
    
    alert('Please select a county');
    return;
    
    }
    
    if(!cities_remove && county_id_remove == 'country-england' || county_id_remove == 'country-scotland' || county_id_remove == 'country-wales') {
    
    alert('Please select a county');
    return;
    
    }
    
    var test = window.confirm('Are you sure?');
	if (test) {
    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/saveRemoveTown",
      success: function( ret_val) {
           if (ret_val) {
                    // alert(ret_val);
                     jQuery('#cities_remove').html(jQuery(ret_val));
                     jQuery('#cities_remove').children().each( function() {
                      jQuery(this).attr('selected', false);
                       });
                     if(county_id_remove == county_id) {
                     jQuery('#cities').html(jQuery(ret_val));
                     jQuery('#cities').children().each( function() {
                      jQuery(this).attr('selected', false);
                       });
                      }
                     initClickAdd();
                     listAddressesOnDelete();
              }
      }
    });
    }
     
}

function listAddressesOnDelete()
{   

    var tmp = new Array('trader_account_id'); 
    var data_string = getDataStringByIds( tmp ) ;

    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/listAddresses",
      success: function( ret_val) {
            if (ret_val) {
          //  alert(ret_val);
                     jQuery('ul.test1').html(jQuery(ret_val));
                     initClickAdd();
            }
 
      }
    });
     
}

function deleteAddress(i)
{   
    var tmp = new Array( 'trader_account_id', 'users_id'); 
    var data_string_tmp = getDataStringByIds( tmp ) ;
    var data_string = data_string_tmp + '&address_id='+i;
    
    //alert('Confirm deletion');


    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/deleteData",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
                     jQuery('ul.test').html(jQuery(ret_val));
                     initClickAdd();
            }
 
      }
    });
     
}
/*
function deleteArea(i)
{   
    var tmp = new Array( 'trader_account_id'); 
    var data_string_tmp = getDataStringByIds( tmp ) ;
    var data_string = data_string_tmp + '&id='+i;
    
    //alert('Confirm deletion');


    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/deleteArea",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
                     jQuery('ul.test1').html(jQuery(ret_val));
                     initClickAdd();
            }
 
      }
    });
     
}

*/

function deleteAreaCity(a,b)
{   

    var tmp = new Array('trader_account_id'); 
    var data_string_tmp = getDataStringByIds( tmp ) ;
    var data_string = data_string_tmp + '&id='+a+'&county_id='+b;
    
    //alert(data_string);

    var test = window.confirm('Are you sure?');


    if (test) {
    
    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/deleteAreaCity",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
                     jQuery('ul.test3').html(jQuery(ret_val));
                     if(ret_val == '1') {
                      jQuery('#edit-area').hide();
                     }
                    listAddressesOnDelete();
                    initClickAdd();

            }
 
      }
    });
     }

}

function deleteAreaCounty(i)
{   

    var tmp = new Array('trader_account_id'); 
    var data_string_tmp = getDataStringByIds( tmp ) ;
    var data_string = data_string_tmp + '&id='+i;
    
    //alert(data_string);
    var test = window.confirm('Are you sure?');


    if (test) {

    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/deleteAreaCounty",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
                    /* jQuery('ul.test3').html(jQuery(ret_val));*/
                       if(ret_val == '1') {
                      jQuery('#edit-area').hide();
                     }
                     listAddressesOnDelete();
                     initClickAdd();

            }
 
      }
    });
     }
}

function editArea(i)
{   

    jQuery('.edit-area').show();
    var tmp = new Array('trader_account_id'); 
    var data_string_tmp = getDataStringByIds( tmp ) ;
    var data_string = data_string_tmp + '&county_id='+i;
    
    //alert(data_string);


    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/editArea",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
                     jQuery('ul.test3').html(jQuery(ret_val));
                      initClickAdd();

            }
 
      }
    });
     
}
function editTestimonial(i)
{   

    jQuery('.edit-testimonial').show();
    jQuery('#saveTestimonial').html('<a id="saveTestimonial" class="display" onClick="saveTestimonial('+i+');">save</a>');
    jQuery('#deleteTestimonial').html('<a id="deleteTestimonial" class="display" onClick="deleteTestimonial('+i+');">delete</a>');
	
    var tmp = new Array('trader_account_id'); 
    var data_string_tmp = getDataStringByIds( tmp ) ;
    var data_string = data_string_tmp + '&testimonial_id='+i;
    
    //alert(data_string);

    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/editTestimonial",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
                      jQuery('div.testimonial-container').html(jQuery(ret_val));
                      jQuery('form')[0].reset();
                      initClickAdd();

            }
      }
      
      
    });
  
}

function saveTestimonial(i)
{   
    var testimonial = jQuery('#testimonial').val();
    var name = jQuery('#name').val();
    var testimonial_city = jQuery('#city_testimonial').val();
    var tmp = new Array('trader_account_id'); 
    var data_string_tmp = getDataStringByIds( tmp ) ;
    var data_string = data_string_tmp +'&testimonial='+testimonial+'&name='+name+'&city='+testimonial_city+'&testimonial_id='+i;
    if (!testimonial) {
    alert('Please enter a testimonial');
    return;
    }
    //alert(data_string);


    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/saveTestimonial",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
                    jQuery('ul.test5').html(jQuery(ret_val));
                      jQuery('#edit-testimonial').hide();
                      jQuery('form')[0].reset();
                      initClickAdd();
                      

            }
 
      }
    });
     
}

function deleteTestimonial(i)
{   
    var tmp = new Array('trader_account_id'); 
    var data_string_tmp = getDataStringByIds( tmp ) ;
    var data_string = data_string_tmp + '&testimonial_id='+i;
    
    //alert(data_string);
	if (i) {
    var test = window.confirm('Are you sure?');
	}

    if (test) {
    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/deleteTestimonial",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
                    jQuery('ul.test5').html(jQuery(ret_val));
                      $('#edit-testimonial').hide();
                      jQuery('form')[0].reset();
                      initClickAdd();

            }
 
      }
    });
     }
}

function addTestimonial()
{   

    var testimonial = jQuery('#testimonial-add').val();
    var name = jQuery('#name-add').val();
    var testimonial_city = jQuery('#city_testimonial-add').val();
    var tmp = new Array('trader_account_id'); 
    var data_string_tmp = getDataStringByIds( tmp ) ;
    var data_string = data_string_tmp +'&testimonial='+testimonial+'&name='+name+'&city='+testimonial_city;
    if (!testimonial) {
    alert('Please enter a testimonial');
    return;
    }
    //alert(data_string);


    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/addTestimonial",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
                    jQuery('ul.test5').html(jQuery(ret_val));
                      $('#add-testimonial').hide();
                       jQuery('form')[0].reset();
                      initClickAdd();

            }
 
      }
    });
     
}

function initClickAdd()  {
	  
    jQuery('#show_address_form').click( function() {
        jQuery('#add-address').show();
     });  
     
    jQuery('#show_testimonial_form').click( function() {
        jQuery('.add-testimonial').show();
        //showCounties(1);
       // showCities(1);
     });  
     

    jQuery('#show_area_form').click( function() {
        jQuery('.area').show();
        //showCounties(1);
       // showCities(1);
     });  
     
       jQuery('#add_town').click( function() {
       if (jQuery('#add_town').attr('checked') ) {
       jQuery('#add_town').attr('checked', true);
       jQuery('.add_town_form').show();
       jQuery('#whole').attr('disabled', 'disabled');
       jQuery('#remove_town').attr('disabled', 'disabled');
       //disable save elements
       jQuery('#cities').attr('disabled', 'disabled');
       jQuery('#counties').attr('disabled', 'disabled');
              //disable save elements
       jQuery('#cities').attr('disabled', 'disabled');
       jQuery('#counties').attr('disabled', 'disabled');
	   jQuery('#save').remove();
	   jQuery('#cancel').remove();
	   jQuery('#remove-link').html(jQuery("<a id=\"save\" class=\"disabled-link\" >save</a>&nbsp;| <a class=\"disabled-link\" id=\"cancel\">close</a>"));
       //jQuery('#save').removeAttr("onclick"); 
       //jQuery('#cancel').removeAttr("onclick");
      // jQuery("#save").addClass("disabled-link");
      // jQuery("#cancel").addClass("disabled-link"); 
     } else {
        jQuery('.add_town_form').hide();
        jQuery('#whole').removeAttr('disabled', 'disabled');
       jQuery('#remove_town').removeAttr('disabled', 'disabled');
       //enable save elements
       jQuery('#whole').removeAttr('disabled');
       //jQuery("#save").attr("onclick","saveArea('no-close')");/*needed?*/
                  //enable save elements
       jQuery('#cities').removeAttr('disabled', 'disabled');
       jQuery('#counties').removeAttr('disabled', 'disabled');
      // jQuery('#save').attr("onclick","saveArea('close')");
	   jQuery('#save').remove();
	   jQuery('#cancel').remove();
	   jQuery('#remove-link').html(jQuery("<a onclick=\"saveArea('close');\" id=\"save\" class=\"display\" >save</a>&nbsp;| <a class=\"display\" id=\"cancel\" onclick=\"hideAddress('add-area')\">close</a>"));
      //jQuery('#cancel').attr("onclick","hideAddress('add-area')");
	   //jQuery('#cancel').html(jQuery(""));
       //jQuery("#save").removeClass("disabled-link"); 
       //jQuery("#cancel").removeClass("disabled-link"); 
     }
     });  
     
     
     jQuery('#manage_towns').click( function() {
    if (jQuery('#manage_towns').attr('checked') ) {
       jQuery('#manage_towns').attr('checked', true);
       jQuery('.manage_towns').show();
      
     } else {
        jQuery('.manage_towns').hide();
     }
     });  
     
     
     jQuery('#remove_town').click(function(){
     if (   jQuery('#remove_town').attr('checked') ) {
       jQuery('#remove_town').attr('checked', true);
        jQuery('.remove_town_form').show();
       jQuery('#hidden').val(1);
       jQuery("#save").attr("onclick","saveArea('no-close')");
       jQuery('#whole').attr('disabled', 'disabled');
        jQuery('#add_town').attr('disabled', 'disabled');
        //disable save elements
       jQuery('#cities').attr('disabled', 'disabled');
       jQuery('#counties').attr('disabled', 'disabled');
       //jQuery('#save').removeAttr("onclick");
       //jQuery('#cancel').removeAttr("onclick");
       //jQuery("#save").addClass("disabled-link");
       //jQuery("#cancel").addClass("disabled-link");
	    jQuery('#save').remove();
	    jQuery('#cancel').remove();
	    jQuery('#remove-link').html(jQuery("<a id=\"save\" class=\"disabled-link\" >save</a>&nbsp;| <a class=\"disabled-link\" id=\"cancel\">close</a>"));
     } else {
           jQuery('#hidden').val('');
           jQuery('.remove_town_form').hide();
           jQuery('#whole').removeAttr('disabled');
           jQuery('#add_town').removeAttr('disabled');
           jQuery("#save").attr("onclick","saveArea('no-close')");
                  //enable save elements
       jQuery('#cities').removeAttr('disabled', 'disabled');
       jQuery('#counties').removeAttr('disabled', 'disabled');
       //jQuery('#save').attr("onclick","saveArea('close')");
       //jQuery('#cancel').attr("onclick","hideAddress('add-area')");
       //jQuery("#save").removeClass("disabled-link");
       //jQuery("#cancel").removeClass("disabled-link");
	   jQuery('#save').remove();
	   jQuery('#cancel').remove();
	   jQuery('#remove-link').html(jQuery("<a onclick=\"saveArea('close');\" id=\"save\" class=\"display\" >save</a>&nbsp;| <a class=\"display\" id=\"cancel\" onclick=\"hideAddress('add-area')\">close</a>"));
     }
                            
                                            
     });
           
     
}


 function showCities(value,i)  {

    // alert(i);
    //var tmp = new Array( 'trader_account_id', 'users_id'); 
    //var data_string_tmp = getDataStringByIds( tmp ) ;
    //var data_string = data_string_tmp + '&address_id='+i;
    data_string = "id="+value;
    //alert(data_string);


    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/getCities",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
                     if(i != 'remove_select_box') {
                     jQuery('#cities').html(jQuery(ret_val));
                     jQuery('#cities').children().each( function() {
                     jQuery(this).attr('selected', false);
                       });
                       } else {
                      jQuery('#cities_remove').html(jQuery(ret_val));
                      jQuery('#cities_remove').children().each( function() {
                      jQuery(this).attr('selected', false);
                        });
                     }
                     initClickAdd();
            }
 
      }
    });
}    


     function deleteImage(i)
{   
    var tmp = new Array('trader_account_id'); 
    var data_string_tmp = getDataStringByIds( tmp ) ;
    var data_string = data_string_tmp + '&image_id='+i;
    
    //alert(data_string);
    var test = window.confirm('Are you sure?');


    if (test) {

    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/deleteImage",
      success: function( ret_val) {
            if (ret_val) {
            alert(ret_val);
                    jQuery('ul.test9').html(jQuery(ret_val));
                      initClickAdd();

            } else {
                jQuery('ul.test9').html("");
            }
 
      }
    });
     }
}

jQuery(document).ready( function() {
jQuery('#whole').val('');
     jQuery('#whole').click(function(){
jQuery('#cities').val('null');

     if (   jQuery('#whole').attr('checked') ) {
       jQuery('#whole').attr('checked', true);
       jQuery('#whole').val(1);
       jQuery('#cities').attr('disabled', 'disabled');
        jQuery('#add_town').attr('disabled', 'disabled');
       jQuery('#remove_town').attr('disabled', 'disabled');
      
     } else {
           jQuery('#whole').val('');
           jQuery('#whole').removeAttr('checked');
           jQuery('#cities').removeAttr('disabled');
           jQuery('#add_town').removeAttr('disabled', 'disabled');
           jQuery('#remove_town').removeAttr('disabled', 'disabled');
     }
                            
                                            
     });
    data_string = "id="+1;
    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/getCities",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
            
                     jQuery('#cities').html(jQuery('<option value="">Select County</option>'));
                     jQuery('#cities').children().each( function() {
                     jQuery(this).attr('selected', false);
                      });
                      
                      jQuery('#cities_remove').html(jQuery('<option value="">Select County</option>'));
                      jQuery('#cities_remove').children().each( function() {
                      jQuery(this).attr('selected', false);
                      });
    
                     initClickAdd();
            }
 
      }
    });
 
 
 
 jQuery('#counties').change( function() {
     var value = jQuery('#counties').val();
     var error = jQuery('#counties').val();
     if (error == 'country-england' || error == 'country-wales' || error == 'country-scotland') {
     alert('Please select a county');
              jQuery('#cities').html(jQuery('<option value="">Select County</option>'));
                                  jQuery('#cities').children().each( function() {
                      jQuery(this).attr('selected', false);
                      });
     }
     if (value == 'county') {
     
         jQuery('#cities').html(jQuery('<option value="">Select County</option>'));
                                  jQuery('#cities').children().each( function() {
                      jQuery(this).attr('selected', false);
                      });
     }

     showCities(value);
     
     }); 
     
      jQuery('#counties_add').change( function() {
     var error = jQuery('#counties_add').val();
     if (error == 'country-england' || error == 'country-wales' || error == 'country-scotland') {
     alert('Please select a county');
              jQuery('#cities_add').html(jQuery('<option value="">Select County</option>'));
                                  jQuery('#cities_add').children().each( function() {
                      jQuery(this).attr('selected', false);
                      });
     }

     
     }); 
     
 jQuery('#counties_remove').change( function() {
      var error = jQuery('#counties_remove').val();
     if (error == 'country-england' || error == 'country-wales' || error == 'country-scotland') {
     alert('Please select a county');
              jQuery('#cities_remove').html(jQuery('<option value="">Select County</option>'));
                                  jQuery('#cities_remove').children().each( function() {
                      jQuery(this).attr('selected', false);
                      });
     }
     var i = 'remove_select_box';
     var value = jQuery('#counties_remove').val();
     
     if (value == 'county') {
      jQuery('#cities_remove').html(jQuery('<option value="">Select County</option>'));
                         jQuery('#cities_remove').children().each( function() {
                      jQuery(this).attr('selected', false);
                      });
     
     }
     
     showCities(value,i);
     
     });       
     
       
       

 initClickAdd();

 
    /*  jQuery('#delete').click( function() {
      confirm('Are you sure?');
      return false;

     });  
     */
});


/*******************public end ***********************************/
 function showCountiesByCountry(value)  {

    // alert(i);
    //var tmp = new Array( 'trader_account_id', 'users_id'); 
    //var data_string_tmp = getDataStringByIds( tmp ) ;
    //var data_string = data_string_tmp + '&address_id='+i;
    data_string = "id="+value;
    //alert(data_string);


    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/getCountiesByCountry",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
 
                     jQuery('#counties').html(jQuery(ret_val));
                     jQuery('#county-hide').show();
                     //jQuery('#county-hide').css('display','table-row');
                     jQuery('#test').attr('selected', 'selected');
                     //initClickAdd();
            }
 
      }
    });
}    
     
 function showCitiesByCounty(value)  {

    // alert(i);
    var tmp = new Array('counties'); 
    var data_string_tmp = getDataStringByIds( tmp ) ;
    var county = jQuery('#counties').val();
    var data_string = data_string_tmp + '&id=' + county;
    //alert(data_string);


    jQuery.ajax({
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/getCitiesByCountyPublic",
      success: function( ret_val) {
            if (ret_val) {
            //alert(ret_val);
                     jQuery('#cities_public').html(jQuery(ret_val));
                      jQuery('#cities_public').children().each( function() {
                      jQuery(this).attr('selected', false);
                       });
                     //jQuery('#city-hide').css('display','table-row');
                     //jQuery('#whole-county-hide').css('display','table-row');
                     // jQuery('#tip-hide').css('display','table-row');
                     jQuery('#city-hide').show();
                     jQuery('#whole-county-hide').show();
                     jQuery('#tip-hide').show();
                     initClickAdd();
            }
 
      }
    });
}    
          
jQuery(document).ready( function() {


     jQuery('#whole').click(function(){
jQuery('#cities_public').val('null');

     if (   jQuery('#whole').attr('checked') ) {
       jQuery('#whole').attr('checked', true);
       jQuery('#whole').val(1);
       jQuery('#cities_public').attr('disabled', 'disabled');
      
     } else {
           jQuery('#whole').val('');
           jQuery('#whole').removeAttr('checked');
           jQuery('#cities_public').removeAttr('disabled');
     }
                            
                                            
     });



     var previous_value = null;
     var previous_county = null;
     jQuery('#countries').change( function() {
     var value = jQuery('#countries').val();
     
     if  (value != 'country') {
     jQuery('#city-hide').hide();
     jQuery('#whole-county-hide').hide();
     jQuery('#tip-hide').hide();
     previous_value = value;
     }
     var error = jQuery('#countries').val();
     if (error == 'country') {
     alert('Please select a Country');
     // jQuery('#city-hide').hide();
     //jQuery('#whole-county-hide').hide();
     // jQuery('#tip-hide').hide();
     jQuery("#countries option[value=" + previous_value + "]").attr("selected",true); 
     if (previous_county) {
    // showCountiesByCountry(previous_value);
     //alert(previous_county);
     jQuery("#counties option[value=" + previous_county + "]").attr("selected",true); 
     var check = 1;
     }
    
     // return false;
     }
     if (!check) {

     showCountiesByCountry(value);
     }
     //alert(value);
     
     
     }); 
jQuery('#countries').trigger('change');


     jQuery('#counties').change( function() {
     var counties = jQuery('#counties').val();
     if  (counties != 'counties') {
     previous_county = counties;
     }
     if (counties == 'counties') {
      alert('Please select a County');
      jQuery("#counties option[value=" + previous_county + "]").attr("selected",true); 
      return false;
     }     
     var value = jQuery('#cities').val();
     showCitiesByCounty(value);
     });   

     jQuery('#submit').click( function() {
     var countries = jQuery('#countries').val();
     var counties = jQuery('#counties').val();
 
     var cities = jQuery('#cities_public').val();
     var whole = jQuery('#whole').val();

     if (countries == 'country') {
      alert('Please select a Country');
      return false;
     }
     
     if (counties == 'counties') {
      alert('Please select a County');
      return false;
     }
     

      if (!cities && whole != 1) {
      alert('Please select a City or select the "all Towns/Cities option"');
      return false;
     }
    
    // jQuery('#find_trader').submit.disabled = true;
     });        
     
});

/**************Live Search ********************/
$(document).ready(function(){
/*
pm5 = {
  t: 0,
  doit: function() {
    $('#pm5-spinner').css('visibility','visible');
    $('#pm5-form').form('#pm5-q-msg',  function(){
      $('#pm5-spinner').css('visibility','hidden');
    });
  },
  init: function(){
    $('#pm5-q').set('autocomplete', 'off');
    $('#pm5-q').hover(function(){
      this.focus();
      $(this).css('border','2px solid #0a0');
      $(this).keyup(function(){
        if (this.t) clearTimeout(this.t);
        this.t = setTimeout("pm5.doit()", 1000);
      });
    });
    $('#pm5-q').blur(function(){
      $(this).css('border','2px solid #ccc');
      $('#pm5-spinner').css('visibility','hidden');
    });
  }
}

*/

function add() {
    jQuery('#search-form').show();
}

$("p.search-directory").toggle(function(){
  jQuery('#search-form').show();
  $(this).addClass("hide");
},function(){
  $(this).removeClass("hide");
  jQuery('#search-form').hide();
});


die = null;



function trim(stringValue)
{
return stringValue.replace(/(^\s*|\s*$)/, "");
}
$("#productslivesearch").keyup( function() {
 var charLength = $(this).val().length;
 if (charLength < 1) {
 jQuery('#productslivesearch').val("");
 jQuery('#results').hide();
 die = 1;
 return false;
 } else {
 
 die=null;
 
 }
 
    var val = jQuery('#productslivesearch').val();
    var data_string =  'name='+trim(val);
	var val = jQuery('#productslivesearch').val();
	if (!val) {
	data_string="";
	jQuery('#results').hide();
	return;
	}
  if(!die){
	//alert(data_string);
	  jQuery.ajax({
	
      type: "GET",
      data: data_string,
      url: "/ajax.php/directory/tradersLiveSearch",
      success: function( ret_val) {
		jQuery('ul.results').html(jQuery(ret_val));
		jQuery('#results').show();
		}});
	}
		
	});

});





