var getZipcodes;

function get_zipcodes(){
	if (window.XMLHttpRequest) { return new XMLHttpRequest(); }
	if (window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); }
	return null;
}

function search_zipcodes(x){
	getZipcodes=get_zipcodes();
	if (getZipcodes==null){ 
		alert ("AJAX not supported");
  		return;
  	}
	
	var o_o="/tasks/zipcodes.php";
	
	o_o=o_o+"?city="+x;
	
	o_o=o_o+"&sid="+Math.random();

	getZipcodes.onreadystatechange=select_zipcodes;
	
	getZipcodes.open("GET",o_o,true);
	getZipcodes.send(null);
}

function select_zipcodes(){
	if (getZipcodes.readyState==4){
		x = getZipcodes.responseText;
		choose_zip(x);
		//document.getElementById("select_zipcodes").innerHTML=getZipcodes.responseText;
	}
}

function choose_zip(x){
	document.getElementById('zip-code-home').value = x;
	document.getElementById('zipcodes_box').style.display = 'none';
}

////////////////////

var getCities;

function get_cities(){
	if (window.XMLHttpRequest) { return new XMLHttpRequest(); }
	if (window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); }
	return null;
}

function search_cities(x){
	getCities=get_cities();
	if (getCities==null){ 
		alert ("AJAX not supported");
  		return;
  	}

	if(x != ''){
		var o_o="/tasks/cities.php";
		
		o_o=o_o+"?state="+x;
		
		o_o=o_o+"&sid="+Math.random();
	
		getCities.onreadystatechange=select_cities;
		
		getCities.open("GET",o_o,true);
		getCities.send(null);
	}else{
		document.getElementById("select_cities").innerHTML='<select class="select_field" disabled="disabled"> <option>- City -</option> </select>';
	}
	
	//document.getElementById("select_zipcodes").innerHTML='<select class="select_field" disabled="disabled"> <option>- Zipcode -</option> </select>';
}

function select_cities(){
	if (getCities.readyState==4){
		document.getElementById("select_cities").innerHTML=getCities.responseText;
	}
}

function toggle_zippercoder(x){
	if(x == 'close'){
		document.getElementById('zipcodes_box').style.display = 'none';
	}else if(x == 'open'){
		document.getElementById('zipcodes_box').style.display = 'block';
	}
}

/////////////////////////////////////

var getCZ;

function get_cities_zips(){
	if (window.XMLHttpRequest) { return new XMLHttpRequest(); }
	if (window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); }
	return null;
}

function search_cities_zips(x,y){
	getCZ=get_cities_zips();
	if (getCZ==null){ 
		alert ("AJAX not supported");
  		return;
  	}

	if(x != ''){
		var o_o="/tasks/cities_zip.php";
		
		o_o=o_o+"?state="+x;
		o_o=o_o+"&extra="+y;
		
		o_o=o_o+"&sid="+Math.random();
	
		getCZ.onreadystatechange=cities_zips;
		
		getCZ.open("GET",o_o,true);
		getCZ.send(null);
	}else{
		document.getElementById('cities_zips').innerHTML = '<select class="selections" id="' + y + 'city" disabled="disabled"> <option value=""></option> </select>';
	}
}

function cities_zips(){
	if (getCZ.readyState==4){
		document.getElementById("cities_zips").innerHTML=getCZ.responseText;
	}
}

////////////////////////////////////////

function know_thy_width(){
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		winW = window.innerWidth;
		winH = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		winW = document.documentElement.clientWidth;
		winH = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		winW = document.body.clientWidth;
		winH = document.body.clientHeight;
	}
	
	document.getElementById('lit_box_back').style.width = winW + 'px';
	document.getElementById('lit_box_back').style.height = winH + 'px';
}

function close_lit_box(){
	document.getElementById('lit_box').style.display = 'none';
	document.getElementById('lit_box_back').style.display = 'none';
	//document.getElementById('disable_scroll_for_now').style.overflow='auto';
	document.documentElement.style.overflow='auto';
	document.body.scroll='yes';
}

function pass_to_lit_box(x){
	var moving_from = document.getElementById('zip-code-home').value;
	var moving_to_vars = document.getElementById(x + 'city').value;

	var ck_zipcode = /^[0-9-\s]{5,11}$/;

	if(moving_from == '' || moving_to_vars == ''){
		alert('Please provide your zipcode and the city and state you\'ll be moving to');
	}else if(!ck_zipcode.test(moving_from)){
		alert('The zip code you provided is invalid');
	}else{
		moving_to_vars = moving_to_vars.split('-');
		city_id = moving_to_vars[0];
		state_id = moving_to_vars[1];
		zipcode = moving_to_vars[2];
		city_name = moving_to_vars[3];
		state_code = moving_to_vars[4];
		document.getElementById('moving_from').innerHTML = moving_from;
		document.getElementById('moving_to').innerHTML = city_name + ', ' + state_code + ' ' + zipcode;

		document.form.moving_from_zip.value = moving_from;
		document.form.moving_to_zip.value = zipcode;
		
		document.getElementById('lit_box').style.display = 'block';
		document.getElementById('lit_box_back').style.display = 'block';
		//document.getElementById('disable_scroll_for_now').style.overflow='hidden';
		document.documentElement.style.overflow='hidden';
		document.body.scroll='no';
		window.scrollTo(0,0);
	}
}

