
/*
 * Copyright 2005 Matthew Eernisse (mde@fleegix.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Original code by Matthew Eernisse (mde@fleegix.org)
 * Additional bugfixes by Mark Pruett (mark.pruett@comcast.net)
 *
*/

function formData2QueryString(formName) {
	var docForm = document.forms[formName];
	var submitContent = '';
	var formElem;
	var lastElemName = '';

	for (i = 0; i < docForm.elements.length; i++) {
		
		formElem = docForm.elements[i];
		switch (formElem.type) {
		// Text fields, hidden form elements
		case 'text':
		case 'hidden':
		case 'password':
		case 'textarea':
		case 'select-one':
			submitContent += formElem.name + '=' + escape(formElem.value) + '&'
			break;
		
			//strSubmitContent += formElem.name + '=' + escape(formElem[formElem.selectedIndex].value) + '&'
			//break;
		// Radio buttons
		case 'radio':
			if (formElem.checked) {
			submitContent += formElem.name + '=' + escape(formElem.value) + '&'
			}
			break;
			
		// Checkboxes
		case 'checkbox':
			if (formElem.checked) {
			// Continuing multiple, same-name checkboxes
			if (formElem.name == lastElemName) {
				// Strip of end ampersand if there is one
				if (submitContent.lastIndexOf('&') == submitContent.length-1) {
				submitContent = submitContent.substr(0, submitContent.length - 1);
				}
				// Append value as comma-delimited string
				submitContent += ',' + escape(formElem.value);
			}
			else {
				submitContent += formElem.name + '=' + escape(formElem.value);
			}
			submitContent += '&';
			lastElemName = formElem.name;
			}
			break;	
		}
	}
	// Remove trailing separator
	submitContent = submitContent.substr(0, submitContent.length - 1);
	return submitContent;
}


function xmlhttpPost(strURL, strSubmit, strResultFunc, strProcessingFunc, displayDiv, processDiv) {

	var xmlHttpReq = false;
	
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		var xmlHttpReq = new XMLHttpRequest();
		//xmlHttpReq.overrideMimeType('text/xml');
	}
	// IE
	else if (window.ActiveXObject) {
		var xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlHttpReq.open('POST', strURL, true);
	xmlHttpReq.setRequestHeader('Content-Type', 
		 'application/x-www-form-urlencoded; charset=UTF-8');
	xmlHttpReq.onreadystatechange = function() {
		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 {
						
						if(displayDiv){
							eval(strResultFunc + '(strResponse, displayDiv)');
						}else {
							eval(strResultFunc + '(strResponse, "displayDiv")');
						}
					}
					break;
			}
		} else if (xmlHttpReq.readyState < 4) {
			//eval(strProcessingFunc + '(xmlHttpReq.readyState)');
		}
	}
	xmlHttpReq.send(strSubmit);
	
}

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.');
	}
}
