// JavaScript Document

document.write('\n<script type="text/javascript" src="/js/spry/SpryEffects.js"></script>');

function ChooseRegion(){
	
	var _readyToSubmit = false;
	var _isIE  = (navigator.appVersion.toLowerCase().indexOf("msie") != -1) ? true : false;
	var states = {"AL":new Object(),"AZ":new Object(),"CO":new Object(),"CA":new Object(),"FL":new Object(),"NM":new Object(),"TX":new Object()};
	states.AL.optionLabels = ["Birmingham","Mobile","Montgomery","Huntsville","Other Alabama"];
	states.AL.optionValues = ["AL.Birmingham","AL.Mobile","AL.Montgomery","AL.Huntsville","AL.Other"];
    states.AZ.optionLabels = ["Phoenix","Tucson","Other Arizona"];
	states.AZ.optionValues = ["AZ.Phoenix","AZ.Tucson","AZ.Other"];
	states.CA.optionLabels = ["Southern California","Northern California","San Diego"];
	states.CA.optionValues = ["CA.Southern","CA.Northern","CA.San_Diego"];
	states.CO.optionLabels = ["Not needed, click Go"];
	states.CO.optionValues = ["CO"];
	states.FL.optionLabels = ["Ft. Walton","Jacksonville","Gainesville/Ocala","Pensacola"];
	states.FL.optionValues = ["FL.Panhandle","FL.Jacksonville","FL.Gainesville","FL.Pensacola"];
	states.NM.optionLabels = ["Albuquerque","South New Mexico"];
	states.NM.optionValues = ["NM.Albuquerque","NM.South"];
	states.TX.optionLabels = ["Austin","Bryan/College Station","Dallas/Ft. Worth","El Paso","Houston","Laredo","San Antonio","Waco","Central Texas","East Texas","West Texas","South Texas"];
	states.TX.optionValues = ["TX.Austin","TX.College_Station","TX.Dallas","TX.El_Paso","TX.Houston","TX.Laredo","TX.San_Antonio","TX.Waco","TX.Central","TX.East","TX.West","TX.South"];
	
	this.init = init;
	function init(){
		$('state').selectedIndex = 0;
		$('city').disabled = true;
		if(_isIE){
			$('city').style.backgroundColor = '#CCCCCC';
		}
	}
	
	this.populateCitySelect = populateCitySelect;
	function populateCitySelect(state){
		if(state && state.length > 0){$state = state;}else{$state = null;}
		$('city').options[0] = new Option("","");
		setTimeout(_populateCitySelect,0);
	}
	
	var $state;
	function _populateCitySelect(){
		errorMsg(false);
		var state = ($state) ? $state : $('state').options[$('state').selectedIndex].value;
		if(state.length == 0){
			_readyToSubmit = false;
			enableCitySelect(false); 
			$('customer_region').value = ''; 
			$('city').options[0] = new Option("Choose Your City","");
			return;
		}
		var citySelect = $('city');
		citySelect.options.length = 0;
		var optionLabels = states[state].optionLabels;
		var optionValues = states[state].optionValues;
		for(var i=0; i<optionLabels.length; i++){
			citySelect.options[i] = new Option(optionLabels[i],optionValues[i]);
		}
		_readyToSubmit = true;
		enableCitySelect(true);
		chooseCity();	
	}
	
	this.chooseCity = chooseCity;
	function chooseCity(){
		$('customer_region').value = $('city').options[$('city').selectedIndex].value;
	}
	
	function enableCitySelect(enable){
		if(enable){
			$('city').disabled = false;
			$('city').style.backgroundColor = '#FFFFFF';
		}
		else{
			$('city').options.length = 0;
			$('city').options[0] = new Option("Choose Your City","");
			$('city').disabled = true;
			$('city').style.backgroundColor = '#CCCCCC';
		}
	}
	
	function errorMsg(show){
	    if(show){
		    $('msg').style.visibility = 'visible';	
		}
		else{
			$('msg').style.visibility = 'hidden';	
		}
	}
	
	this.doSubmit = doSubmit;
	function doSubmit(){
		if(! _readyToSubmit){	
			errorMsg(true);
			 return;
		}
		document.form1.submit();
	}
	
	this.preselect = preselect;
	function preselect(sessionRegion,cookieRegion){
		 var region;
		 var city;
		 var state;
		 if(! cookieRegion){ cookieRegion = ""; }
		 if(sessionRegion.length == 0 && cookieRegion.length == 0){_log('no region'); return;}
		 region = (sessionRegion.length == 0) ? cookieRegion : sessionRegion;
		 if(region.indexOf('.') == -1){
			 state = region;
			 city = 'none';
		 }
		 else{ 
		     state = region.substring(0,region.indexOf('.'));
		 }
		 populateCitySelect(state);
		 var drop = $('state');
		 for(var i=0; i< drop.options.length; i++){
			 if(drop.options[i].value == state){
				 drop.options[i].selected = true;
				 break;
			 }
		 }
		 if(city != 'none'){
		 	 setTimeout(function(){drop = $('city');
			 for(var i=0; i< drop.options.length; i++){
				 if(drop.options[i].value == region){
					 enableCitySelect(true);
					 drop.options[i].selected = true;
					 break;
				 }
			 }},10);
		 }
		 setTimeout(function(){$('customer_region').value = $('city').options[$('city').selectedIndex].value;},20);
	}

	function $(id){ if(typeof(id) == 'string'){return document.getElementById(id);}else{return id} }
	function _log(msg){ if(typeof(console) != 'undefined'){console.log(msg);}}
}

var ChooseRegion = new ChooseRegion();
