var statelist = "";

function store_checker()
{
var radio_choice = false;
for (counter = 0; counter < document.regf.STORE_NUMBER.length; counter++)
{
	if (document.regf.STORE_NUMBER[counter].checked)
	radio_choice = true; 
}
if (!radio_choice){
	return false;
}
	return true;
}

function errorHandler()
{  
    
   if(document.regf.states.selectedIndex == 0){
   	document.getElementById("listResults").innerHTML = "<font color='#ff0000'><b><ul><li>You must make a preference selection to proceed</li></ul></b></font>";
   	return false;
   } else if (document.regf.states.selectedIndex == 1){
	 return true;
   } else if(eval(document.regf.cities) && document.regf.cities.selectedIndex == 0){
   	document.getElementById("listResults").innerHTML = "<font color='#ff0000'><b><ul><li>You must make a city selection to proceed</li></ul></b></font>";
   	return false;
   } else if(eval(document.regf.STORE_NUMBER)){
	if(document.regf.STORE_NUMBER.length > 1 && store_checker())
	return true;
	else if(!document.regf.STORE_NUMBER.checked){	 
   	document.getElementById("listResults").innerHTML = "<font color='#ff0000'><b><ul><li>You must make a store selection to proceed</li></ul></b></font><br>" + document.getElementById("listResults").innerHTML ;
   	return false;
	} else
		return true;
   } else{
   	return true;
   }



}
function loadXMLDoc(url)
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            // req.setRequestHeader("content-type", "text/xml");
            req.send();
        }
    }
}

function handleAjax(response,state,newcity,oldState, oldCity){
	var cities = "";
	var states = response.getElementsByTagName("state");
	var searchResults = "";
	for (i = 0; i < states.length; i++){
		abbr = states[i].getAttribute("id");
		if(state && state == abbr){
			storeLocation = states[i].getElementsByTagName("city");
			for (j = 0; j < storeLocation.length; j++){
				city = states[i].getElementsByTagName("city")[j].firstChild.data;
				if(newcity && newcity == city){
				storeid = states[i].getElementsByTagName("storeid")[j].firstChild.data;
				mall = states[i].getElementsByTagName("mall")[j].firstChild.data;
				searchResults += "<input type='radio' name='STORE_NUMBER' value='"+storeid+"'>"+storeid + " " + mall+"<br/>"; 
				}
				else{
				if(cities.indexOf(city) == -1)
				cities +=  "<option value='"+city+"'>" + city + "</option>";
				}
			}

		if(newcity){
		document.getElementById("results").innerHTML = "Your Search Results:<br/> (select one)";
		document.getElementById("listResults").innerHTML = searchResults;
		oldState.selected;
		oldCity.selected;
		}
		else{
		
		document.getElementById("cityText").innerHTML = "Pick the City Nearest You:";
		document.getElementById("listings").innerHTML = "<select name='cities' onchange='OnChange(this,2,this.form.states)'><option>City</option>" +cities +"</select>";
		document.getElementById("results").innerHTML = "";
		document.getElementById("listResults").innerHTML = "";
		}
		return true;
		}	
		statelist += "<option value='"+abbr+"'>" + abbr + "</option>";
		
	}	
	
	document.getElementById("states").innerHTML = "<select name='states' onchange='OnChange(this,1)'><option>States</option><option>No Preference</option>" +statelist +"</select>";
	return true;		
}

function processReqChange()
{
    // only if req shows "complete"
    if (req.readyState == 4 ) {
        // only if "OK"
        if (req.status == 200) {	
          if (req.responseXML) {
            response  = req.responseXML.documentElement;
            handleAjax(response);
          } else {
            alert("Empty Dataset");
          }
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}
	function array_contains(array,val){
			debug_str = ""
			for( i in array){
				debug_str +=val+" - "+array[i] + "\n"
				if(array[i] == val){
					return true;
				}				
			}
			return false;
	}

function OnChange(dropdown,Ctype,selection)
{
	var myindex = "";
	var oldSelection="";
	var state = "";
	
	if(!selection)
	myindex = dropdown.selectedIndex;
	else{ 
	myindex = dropdown.selectedIndex;
	oldSelection = selection.selectedIndex;
	
	} 
	if(myindex == 0 && Ctype==1){ 
		document.getElementById("listings").innerHTML = "";
		document.getElementById("cityText").innerHTML = "";
		document.getElementById("results").innerHTML = "";
		document.getElementById("listResults").innerHTML = "";
		
	} else if(myindex ==1 && Ctype==1){
		document.getElementById("listings").innerHTML = "";
		document.getElementById("cityText").innerHTML = "";
		document.getElementById("results").innerHTML = "";
		document.getElementById("listResults").innerHTML = "You have selected that you do not have a store preference. <input type='hidden' name='STORE_NUMBER' value='000'> ";
		return false;

	}else if(myindex == 0 && Ctype==2){ 
		document.getElementById("results").innerHTML = "";
		document.getElementById("listResults").innerHTML = "";
		
	}
	if(Ctype==1){
			
		state = dropdown.options[myindex].value;
		handleAjax(response,state); 
	
	}
	else if(Ctype==2){
		state = selection.options[oldSelection].value;
		city = dropdown.options[myindex].value;
		handleAjax(response,state,city,selection.options[oldSelection],dropdown.options[myindex]);
	
    	}
	return true;
}