$.showLoading1 = false;
$.showLoading2 = false;

/******Jquery extra function ********/
$.fn.swapClass = function(c1, c2){return this.each(function(){
	var t = jQuery(this);
	(!t.is('.'+c1)) ?
	t.addClass(c1).removeClass(c2) : t.addClass(c2).removeClass(c1);
	}
);};
/******************************/

$.fn.replace = function(o) { return this.after(o).remove(); }; 

$(
  function(){
	  $('#selState').change( function() {
									  
									  $.get("/saveState.asp?st="+$('#selState').attr("value"));
									  });
	  fetchCities($('#selState').attr("value"),'');
				$("#loading").ajaxStart(function(){if($.showLoading1)$(this).fadeIn();}).ajaxStop(function(){$(this).fadeOut();$.showLoading1 = false;});
				$("#loading2").ajaxStart(function(){if($.showLoading2)$(this).fadeIn();}).ajaxStop(function(){$(this).fadeOut();$.showLoading2 = false;});
				
				
				$('#selCity').change( function() {fetchRegions($(this).val(),'');});
				
				$('#selState2').change( function() {});
				//$('#selCity2').change( function() {fetchRegions($(this).val(),'2');});
fetchCities($('#selState2').val(),'2');
				$('a#submitRegion').click(function(){ if($('#selRegion').attr('disabled')||$('#selRegion').val()==''){alert('Please choose a region.');}else{$('#findLocal form').submit();}});
				
				$('a#submitContact').click(function(){
													
											var $form = $('#contactForm form');	
											
											var valid_form = true;
											
											var elms = $form.find(' input, select');
											
											for(i=0;i<elms.length;i++){
												
												if($(elms[i]).val()==''){ 
												
													alert('Please fill all of the fields');
													$(elms[i]).addClass('fillMe').focus().click( function(){ $(this).removeClass('fillMe'); });;
													valid_form = false;
													break;
													}
												}
											
											
											if(valid_form) {
										//		var obj = $('#selState2').get(0);
//												var opt = obj.options[obj.selectedIndex];
//												var txt = opt.text;
//												opt.value = txt;
														
												obj = $('#selCity2').get(0);
												opt = obj.options[obj.selectedIndex];
												txt = opt.text;
												opt.value = txt;
												$form.submit();
												}
												
												});
				
			
				$('#gray, #gray2').treeview({control: '#treecontrol',collapsed: true});
				

			}
			
 );

function fetchCities(xpar_id,indx){
	if(indx=='') $.showLoading= true;
	$('#selRegion'+indx).attr('disabled',true);
	$('#selCity'+indx).load('/includes/_fetchLoc.asp',{level:'city',par_id:xpar_id},function(){$(this).attr('disabled',(this.options.length==1))});
	}
	
function fetchRegions(xinf_typ,indx){
	$.showLoading1 = true;
	$('#selRegion'+indx).load('/includes/_fetchLoc.asp',{level:'region',inf_typ:xinf_typ},function(){$(this).attr('disabled',(this.options.length==1))});	
	}



	
	
/*	
//---------- City lists-------------/
var cities = new Array();

cities['United States'] = new Array();
cities['United States']['California'] = new Array('Los Angeles','San Francisco');
cities['United States']['Florida']    = new Array('Miami','Orlando');
cities['United States']['New York']   = new Array('Buffalo','New York');



function setCities() {
  $.showLoading2 = true;
  cntrySel = 'United States';//document.getElementById('country');
  stateSel = document.getElementById('selState2');
  cityList = cities[cntrySel][stateSel.value];
  changeSelect('selCity2', cityList, cityList);
}

function changeSelect(fieldID, newOptions, newValues) {
  $.showLoading2 = true;
  selectField = document.getElementById(fieldID);
  selectField.options.length = 0;
  for (i=0; i<newOptions.length; i++) {
    selectField.options[selectField.length] = new Option(newOptions[i], newValues[i]);
  }
}*/



	

