
// This must agree with the list in AZ::Magazine::Store_Directory_Control_Data::Multiple
var division = new Array("Grouping", "Country", "State", "City");

// i is the first thingy that we have to get.
function URL_for_get(i)
{
    return 'http://scrapbooking.com/cgi-bin/store_directory_ctldata.pl/' + URL_args_for_get(i);
}

function URL_args_for_get(i)
{
    url = '';
    for (j = 0; j < i; j++) {
	obj = document.forms[0][division[j] + '_pulldown'];
        if (obj.type != 'select-one') {
            // radio group doesn't have a type?
	    for (k = 0; k < obj.length; k++)
		if (obj[k].checked)
	            url += obj[k].value + '/';
	} else
            url += obj.value + '/';
    }
    return url;
}

function get_pulldown_data(i)
{
// Don't do anything if City changed
    if (division.length - 1 < i) return;
    var url = URL_for_get(i);
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.onreadystatechange = processResponse;
    req.open("GET", url);
    req.send(null);
}

// Replace any menu's options with what we get back.
function processResponse()
{
    if (req.readyState == 4) {
        if (req.status == 200) {
	    for (i = 0; i < division.length; i++) {
	        opts = req.responseXML.getElementsByTagName(division[i]);
		if (opts && 0 < opts.length) {
		    pd_name = division[i] + '_pulldown';
		    pulldown = document.forms[0][pd_name];
		    for (j = pulldown.length - 1; 0 <= j; j--) {
		        pulldown.removeChild(pulldown.item(j));
		    }
		    for (j = 0; j < opts[0].childNodes.length; j++) {
	                pulldown.options[j] = new Option(opts[0].childNodes[j].firstChild.nodeValue,
		                                         opts[0].childNodes[j].getAttribute("value"));
		        if (opts[0].childNodes[j].getAttribute("selected") == "selected") {
			    pulldown.options[j].selected = true;
                            }
	            }
		}
            }
        } else {
            alert('request failed: ');
        }
    }
}

function get_stores()
{
    window.location = 'http://scrapbooking.com/directory/' + URL_args_for_get(division.length);
    return false;
}


function get_by_zip(distance)
{
    var zip = document.forms[0]['zip'].value;
    var reZIP = /^\d\d\d\d\d$/;
    var reCPC6 = /^[a-zA-Z]\d[a-zA-Z][- ]?\d[a-zA-Z]\d$/;
    if (!(reZIP.test(zip) || reCPC6.test(zip))) {
        alert("Please enter a valid 5-digit US ZIP or 6-character Canadian Postal Code");
	return false;
    }
    window.location = 'http://scrapbooking.com/directory/'
	    + URL_args_for_get(1) + '/'
             + zip + '/'
	     + distance + '/';
    return false;
}

function zip_enter(myfield,e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13) {
	return get_by_zip('5');
    } else
	return true;
}

function show_layout(sURL, sName, sFeature)
{
    msgWindow=window.open(sURL, sName, sFeature)
}

