function showCitiesHeader(current_state, current_city) {
  createRegionsLoading();	
  state = current_state[current_state.selectedIndex].value;
  xmlhttp=GetXmlHttpObjectHeader();
  if (xmlhttp==null) {
    alert ("Browser does not support HTTP Request");
    return;
  }
  var url="includes/get_cities.php";
  url=url+"?state="+state+"&city="+current_city+"&element_id=city_header";
  xmlhttp.onreadystatechange=stateChangedHeader;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

function createRegionsLoading(){
	$('#select_state_header').html('');
	$("#select_state_header").append( 'Loading...' );
}

function stateChangedHeader() {
  if (xmlhttp.readyState==4) {
    document.getElementById("select_state_header").innerHTML=xmlhttp.responseText.replace("form_select","FormFieldSelect");
  }
}

function GetXmlHttpObjectHeader() {
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject) {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  return null;
}

function showRegionsHeader(current_state){
	var state = current_state[current_state.selectedIndex].value;
	$.ajax({
		   type: "POST",
		   url: "includes/get_regions.php",
		   data: "state=" + state,
		   success: function(html){
				createRegions(html);
		   }
	});
}

function createRegions(html){
	if(html !== ''){
		var regions_html_top = '<tr id="region_name"><td class="Upper"><div align="left">Regions:</div></td></tr><tr id="region_select"><td><div align="left"><div>';
		var regions_html_bottom = '</div></td></tr>';
		$("#region_name").remove( );
		$("#region_select").remove( );
		$("#regions").append( regions_html_top + html + regions_html_bottom );
	}else{
		$("#region_name").remove( );
		$("#region_select").remove( );
		$("#regions").append( " " );		
	}
}

function getRegionCities(current_region){
	createRegionsLoading();
	var region = current_region[current_region.selectedIndex].value;
	var current_state = document.getElementById("state_header");
	var state = current_state[current_state.selectedIndex].value;
	$.ajax({
		   type: "POST",
		   url: "includes/get_region_cities.php",
		   data: "region=" + region + '&state=' + state + '&city_element_id=city_header',
		   success: function(html){
				createCities(html);
		   }
	});
}

function createCities(html){
	$('#select_state_header').html('');
	$("#select_state_header").append( html );
}



function showCities(current_state, current_city) {
	  state = current_state[current_state.selectedIndex].value;
	  xmlhttp=GetXmlHttpObject();
	  if (xmlhttp==null) {
	    alert ("Browser does not support HTTP Request");
	    return;
	  }
	  var url="includes/get_cities.php";
	  url=url+"?state="+state+"&city="+current_city;
	  xmlhttp.onreadystatechange=stateChanged;
	  xmlhttp.open("GET",url,true);
	  xmlhttp.send(null);
	}

	function stateChanged() {
	  if (xmlhttp.readyState==4) {
	    document.getElementById("select_state").innerHTML=xmlhttp.responseText;
	  }
	}

	function GetXmlHttpObject() {
	  if (window.XMLHttpRequest) {
	    // code for IE7+, Firefox, Chrome, Opera, Safari
	    return new XMLHttpRequest();
	  }
	  if (window.ActiveXObject) {
	    // code for IE6, IE5
	    return new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return null;
	}

	function check_form(theForm) {
	  var error = false;
	  var text = new Array();
	  if(theForm['statement_agreement'].checked===false) {
	    error = true;
	    text[text.length] = "Read & submit statement agreement";
	  }
	    
	  if(error === true) {
	    var b = "Please, check the following:";
	    for(i=0; i<text.length; i++) {
	      b = b + "\n    " + text[i];
	    }
	    alert(b);
	    return false;
	  }
	  else {
	    return true;
	  }
	}
	
	function showRegions(current_state){
		var state = current_state[current_state.selectedIndex].value;
		$.ajax({
			   type: "POST",
			   url: "includes/get_regions.php",
			   data: "state=" + state + '&style_class=form_select&js_function=getRegionCitiesProperty(this)',
			   /*data: "state=" + state + '&style_class=form_select&js_function=',*/
			   success: function(html){
					createRegionProperty(html);
			   }
		});
	}
	
	function createRegionProperty( html ){	
		$('#regions_tr').html('');
		if(html!=''){
			var top_code = "<td align='left' valign='top' width='20%'><b>Regions</b></td><td align='left'>";
			var bottom_code = "</td>";
			$("#regions_tr").append( top_code + html + bottom_code );
		}
	}


	function getRegionCitiesProperty(current_region){
		var region = current_region[current_region.selectedIndex].value;
		var current_state = document.getElementById("current_state");
		var state = current_state[current_state.selectedIndex].value;
		$.ajax({
			   type: "POST",
			   url: "includes/get_region_cities.php",
			   data: "region=" + region + '&state=' + state + '&style_class=form_select',
			   success: function(html){
					createCitiesProperty(html);
			   }
		});
	}

	function createCitiesProperty(html){
		$("#select_state").html('');
		$("#select_state").append( html );
	}

	
	function showCitiesSearch(current_state, current_city) {
		  state = current_state[current_state.selectedIndex].value;
		  xmlhttp=GetXmlHttpObjectHeader();
		  if (xmlhttp==null) {
		    alert ("Browser does not support HTTP Request");
		    return;
		  }
		  var url="includes/get_cities.php";
		  url=url+"?state="+state+"&city="+current_city;
		  xmlhttp.onreadystatechange=stateChangedSearch;
		  xmlhttp.open("GET",url,true);
		  xmlhttp.send(null);
	}

	function stateChangedSearch() {
		  if (xmlhttp.readyState==4) {
		    document.getElementById("select_state").innerHTML=xmlhttp.responseText.replace("form_select","FormFieldSelect");
		  }
	}

	
	
	
	function showStates(current_country){
		var country = current_country[current_country.selectedIndex].value;
		$.ajax({
			   type: "POST",
			   url: "includes/get_states.php",
			   data: "country=" + country + '&style_class=form_select&js_function=showCities(this); showRegions(this);',
			   success: function(html){
					createStatesProperty(html);
			   }
		});
	}
	
	function showCitiesByCountry(current_country){
		$.ajax({
			   type: "POST",
			   url: "includes/locations.php",
			   data: 'country='+current_country.value+'&action=get_cities_for_select_by_country',
			   success: function( html_cities ){
					createCitiesLocationList( html_cities, '#city' );
			   }
		});
	}
	
	function showCitiesByCountryDetail(current_country, country_id){
		$.ajax({
			   type: "POST",
			   url: "includes/locations.php",
			   data: 'country='+current_country.value+'&action=get_cities_for_select_by_country',
			   success: function( html_cities ){
					createCitiesLocationList( html_cities, '#'+country_id );
			   }
		});
	}
	
	function createCitiesLocationList( html_cities, city_id ){
		$(city_id).html( html_cities );
	}
	
	// v 1.1
/*	
	function createStatesProperty(html){
		$("#current_state").html('');
		$("#current_state").append( html );
	
		$("#city").html('');
		$("#regions_tr").html('');
	}
*/
	
	// v 1.2
		function createStatesProperty( html ){
			/*
			$("#current_state").html('');
			$("#current_state").append( html );
			$("#city").html('');
			$("#regions_tr").html('');
			*/
			
			$("#states_tr").html('');			
			$("#regions_tr").html('');
			
			if(html.length > 0){
				$('#states_tr').html('<td><b>State</b></td><td><select onchange="showCities(this); showRegions(this);" class="form_select" id="current_state" name="state">' + html +'</select></td>');
			}
		}
	
	
	function showStatesHeader(current_country){
		var country = current_country[current_country.selectedIndex].value;
		$.ajax({
			   type: "POST",
			   url: "includes/get_states.php",
			   data: "country=" + country + '&style_class=form_select&js_function=showCities(this); showRegions(this);',
			   success: function(html){
					createStatesPropertyHeader(html);
			   }
		});
	}
	
	function createStatesPropertyHeader(html){
		$("#states_tr_header").html('');
		
		if( html.length > 0 ){
			$("#states_tr_header").html('<tr><td class="Upper"><div align="left">State/Province/Region: </div></td></tr><tr><td id="states_tr_header"><div align="left"><select name="state" id="state_header" class="FormFieldSelect" onchange="showCitiesHeader(this); showRegionsHeader(this);">' + html + '</select></div></td>');
		}
		
		$("#city_header").html('<option value="">...</option>');
		$("#regions").html('');
	}
	
/*	
	function createStatesPropertyHeader(html){
		$("#state_header").html('');
		$("#state_header").append( html );
		
		//$("#city").html('<option value="">...</option>');
		$("#city_header").html('<option value="">...</option>');
		//$("#region_select").html('');
		$("#regions").html('');
	}
*/
	function showCitiesByRegion( region_id, city_id ){
		$.ajax({
			   type: "POST",
			   url: "includes/locations.php",
			   data: 'city='+city_id+'&region='+region_id.value+'&action=get_cities_for_select_by_region',
			   success: function( html_cities ){
					createCitiesLocationList( html_cities, '#city' );
			   }
		});
	}
	
	function showCitiesByCountryTwo( country_id, city_id ){
		$.ajax({
			   type: "POST",
			   url: "includes/locations.php",
			   data: 'city='+city_id+'&country='+country_id.value+'&action=get_cities_for_select_by_country',
			   success: function( html_cities ){
					createCitiesLocationList( html_cities, '#city' );
			   }
		});
	}
	








