function GetStoresByZip() {
	var zip = document.getElementById('zip').value;
	
	new Ajax.Request('/storefinder.php?zip='+zip, {
		method:'post',
		onSuccess: function(transport){
			var json = transport.responseText.evalJSON();
			handleJSON(json);
		 }
	});
       
}

function GetStoresByState(state) {
    new Ajax.Request('/storefinder.php?state='+state, {
        method:'post',
        onSuccess: function(transport){
            var json = transport.responseText.evalJSON();
            handleJSON(json);
         }
    }); 
}

function handleJSON(json) {
    
    var r = 0;
    var c = 0;
    var result = "";
    var tbl = document.getElementById('results');
    
    while(tbl.rows.length > 0) {
		tbl.deleteRow(0);
    }

    var stores = json.stores;    
    
    if(stores.length > 0) { var row = tbl.insertRow(-1); }
    else { var row = tbl.insertRow(-1);
        cell = row.insertCell(-1);
        cell.align = "center";
        cell.innerHTML = '<h3>Sorry, no retailers found</h3>';
    }
    
    document.getElementById('resultsFor').innerHTML = "Found "+stores.length+" for "+json.searchedFor;
    
   	for(var i = 0;i < stores.length; i++) { 
   		if(c == 3) { var row = tbl.insertRow(-1);c = 0; }
        var cell = row.insertCell(-1);
        
        if(stores[i].name != null) { 
            var storeString = '<h3 style="margin:0;">'+stores[i].name+'</h3>';
	        if(stores[i].phone != null) storeString += stores[i].phone+'<br/>';
	        if(stores[i].addressOne != null) storeString += stores[i].addressOne+'<br/>';
	        if(stores[i].zip < 10000) stores[i].zip = "0"+stores[i].zip;
	        if(stores[i].city != null) storeString += stores[i].city+', '+stores[i].state+' '+stores[i].zip+'<br/>';
	        if(stores[i].url != null) storeString += '<a href="http://'+stores[i].url+'" style="text-decoration:underline;">'+stores[i].url+'</a>';
	        
	        cell.vAlign = "top";
	        cell.style.width = "33%";
	        cell.innerHTML = storeString;
	        
	        c++;
        }
	}
	
	displayDiv();
}

function displayDiv() {
    document.getElementById('dataDiv').style.visibility = "visible";
}

function hideDiv() {
    document.getElementById('dataDiv').style.visibility = "hidden";
}