// The var form should be a reference to a <form>
function queryString(form) {
    return queryString(form, null);
}

// The var form should be a reference to a <form>, elementsToInclude is an array
// builds a query string that can be used in call to xmlhttpPost
function queryString(form, elementsToInclude) {

	var strSubmitContent = '';
	var formElem;
	var strLastElemName = '';

    if (elementsToInclude && !elementsToInclude.length) {
       elementsToInclude = elementsToInclude.split(",");
    }

	
	for (i = 0; i < form.elements.length; i++) {
		formElem = form.elements[i];
        if (elementsToInclude && elementsToInclude.length) {
           skip = true;
           for (i = 0; i < elementsToInclude.length; i++) {
              if (elementsToInclude[i] == formElem.name) {
                 skip = false;
                 break;
              }
           }
           if (skip) continue;
        }

        var value = elementValue(formElem);
		if (formElem.name == strLastElemName) {
		   strSubmitContent += ',' + escape(value);
        } else {
           if (i > 0) {
             strSubmitContent += '&';
           }
		   strSubmitContent += formElem.name + '=' + escape(value);
        }
        strLastElemName = formElem.name;
	}

	return strSubmitContent;
}


function elementValue(formElem) {
        var value = '';
		switch (formElem.type) {
			// Text fields, hidden form elements
			case 'text':
			case 'hidden':
			case 'password':
			case 'textarea':
			case 'select-one':
				value = formElem.value;
				break;
				
			// Radio buttons
			case 'radio':
			case 'checkbox':
				if (formElem.checked) {
                    value = formElem.value;
				}
				break;
				
		}

        return value;
}
//strURL is the url to post to
//strSubmit is a query string, which can be generated by queryString
//strResultFunc is the name of the callback function
function xmlhttpPost(strURL, strSubmit, strResultFunc) {
        var xmlHttpReq = false;
        if (window.ActiveXObject) {  // IE
           xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        } else
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
           xmlHttpReq = new XMLHttpRequest();
           xmlHttpReq.overrideMimeType('text/xml');
        }

        xmlHttpReq.open('POST', strURL, true);
        xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlHttpReq.onreadystatechange = function() {
        document.body.style.cursor="default";
        if (xmlHttpReq.readyState == 4) {
           strResponse = xmlHttpReq.responseText;
           switch (xmlHttpReq.status) {
                   // Page-not-found error
                   case 404:
                           alert('Error: Not Found. The requested URL ' + 
                                   strURL + ' could not be found.');
                           break;
                   // Display results in a full window for server-side errors
                   case 500:
                           handleErrFullPage(strResponse);
                           break;
                   default:
                           // Call JS alert for custom error or debug messages
                           if (strResponse.indexOf('Error:') > -1 || 
                                   strResponse.indexOf('Debug:') > -1) {
                                   alert(strResponse);
                           }
                           // Call the desired result function
                           else {
                                   eval(strResultFunc + '(strResponse);');
                           }
                           break;
           }
        }

        };
        xmlHttpReq.send(strSubmit);
        document.body.style.cursor="wait";
}

function handleErrFullPage(strIn) {

        var errorWin;

        // Create new window and display error
        try {
                errorWin = window.open('', 'errorWin');
                errorWin.document.body.innerHTML = strIn;
        }
        // If pop-up gets blocked, inform user
        catch(e) {
                alert('An error occurred, but the error message cannot be' +
                        ' displayed because of your browser\'s pop-up blocker.\n' +
                        'Please allow pop-ups from this Web site.');
        }
}


