// AJAX handlers - thanks to http://www.xml.com/pub/a/2005/02/09/xml-http-request.html
var req;

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.send();
        }
    }
}

function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
// uncomment alert to debug request			
//		alert(req.responseText);
      		response = req.responseXML.documentElement;
      		method = response.getElementsByTagName('method')[0].firstChild.data;
//			if (response.getElementsByTagName('recipient')) {
//				recipient = response.getElementsByTagName('recipient')[0].firstChild.data;
//			}
//			result = response.getElementsByTagName('result')[0].firstChild.data;

//      		eval(method + "('','','" + recipient + "', '', '" + result + "')");
			eval(method);
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

// other functions

function make_options(sel,optionArray) {
	f = document.getElementById(sel);
	clear_options(sel);

for (z=0; z<optionArray.length; z++) {	// create new options
		e = document.createElement("OPTION");
		vals = optionArray[z].split("|");
		e.value = vals[0];
		if (vals.length > 1) {
			e.text = vals[1];
		} else {
			e.text = vals[0];
		}		
		f.options.add(e);
	}	
}

function clear_options(sel) {
	f = document.getElementById(sel);
	if (sel.indexOf("_") > -1) {	
		leave = 2;		// edit page, leave two options (please select, suggest new)
	} else {
		leave = 1;
	}
	while (f.options.length > leave) {	// clear options, but leave first option alone
		if (f.options.remove) {
			f.options.remove(f.options.length-1);
		} else {
			f.options[f.options.length-1] = null;	// for Mozilla
		}	
	}
}


// PHP clones

function urlencode( str ) {
    
    ret = str.toString();
    ret = encodeURIComponent(ret);
    ret = ret.replace(/%20/g, '+');
 
    return ret;
}

function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}
