var resubmit;

function saveSubmitPage() {
    var tddl = window.document.getElementById("targetDDL");
    var target = tddl.options[tddl.selectedIndex].text;
    if (target != null) setCookie("SELECTED_CONTEXT", target, null, null, null, null);
    saveQuery();
}
function saveQuery() {
    var f = getqry(false);
    if (f == null) return;
    //alert("SAVE\n"+f);
    loadXMLDoc('output/svqry.aspx', 'USQL=' + f);
}
function loadSubmitPage() {
    var context = getCookie("SELECTED_CONTEXT");
    if (context != null && !resubmit) {
        var tddl = window.document.getElementById("targetDDL");
        for (var x = 0; x < tddl.options.length; x++) {
            if (tddl.options[x].text == context)
                tddl.selectedIndex = x;
        }
    }
    if (!resubmit) {
        loadXMLDoc('output/svqry.aspx', '');
        //alert("LOAD: "+req.responseText);
        var tmp = req.responseText;
        lastQry = tmp;
    } else {
        lastQry = decodeURIComponent(document.getElementById('rsqry').innerHTML);
    }
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return decodeURIComponent(dc.substring(begin + prefix.length, end));
}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + encodeURIComponent(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/**
* based on example from
* http://developer.apple.com/internet/webcontent/xmlhttpreq.html
* post stuff from http://www.devx.com/DevX/Tip/17500
*/
var req;
function loadXMLDoc(url, param) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        //req.onreadystatechange = processReqChange;
        req.open("POST", url, false);
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.send(param);
        //alert(req.responseText);
        // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            //req.onreadystatechange = processReqChange;
            req.open("POST", url, false);
            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            req.send(param);
            //alert(req.responseText);
        }
    }
}