﻿/*
 * 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), March 2005
 * Additional bugfixes by Mark Pruett (mark.pruett@comcast.net), 12th July 2005
 * Multi-select added by Craig Anderson (craig@sitepoint.com), 24th August 2006
 *
 * Version 1.3
*/

/**
 * Serializes the data from all the inputs in a Web form
 * into a query-string style string.
 * @param docForm -- Reference to a DOM node of the form element
 * @param formatOpts -- JS object of options for how to format
 * the return string. Supported options:
 *    collapseMulti: (Boolean) take values from elements that
 *    can return multiple values (multi-select, checkbox groups)
 *    and collapse into a single, comman-delimited value
 *    (e.g., thisVar=asdf,qwer,zxcv)
 * @returns query-string style String of variable-value pairs
 */
function formData2QueryString(formName, formatOpts){
	var docForm = document.forms[formName];
	var opts = formatOpts || {};
	var str = '';
	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':
				formElem.value = formElem.value.replace("&"," and ");
				str += formElem.name + '=' + encodeURI(formElem.value) + '&';
				break;
			case 'hidden':
			case 'password':
			case 'textarea':
				formElem.value = formElem.value.replace("&"," and ");
				str += formElem.name + '=' + encodeURI(formElem.value) + '&';
				break;
			case 'select-one':
				str += formElem.name + '=' + encodeURI(formElem.value) + '&';
				break;

			// Multi-option select
			case 'select-multiple':
				var isSet = false;
				for(var j = 0; j < formElem.options.length; j++){
					var currOpt = formElem.options[j];
					if(currOpt.selected){
						if (opts.collapseMulti){
							if (isSet){
								str += ',' + encodeURI(currOpt.value); }
							else{
								str += formElem.name + '=' + encodeURI(currOpt.value);
								isSet = true;
							}
						}
						else{
							str += formElem.name + '=' + encodeURI(currOpt.value) + '&';
						}
					}
				}
				if (opts.collapseMulti){
					str += '&';
				}
				break;

			// Radio buttons
			case 'radio':
				if (formElem.checked){
					str += formElem.name + '=' + encodeURI(formElem.value) + '&';
				}
				break;

			// Checkboxes
			case 'checkbox':
				if (formElem.checked){
					// Collapse multi-select into comma-separated list
					if (opts.collapseMulti && (formElem.name == lastElemName)){
						// Strip of end ampersand if there is one
						if (str.lastIndexOf('&') == str.length-1){
							str = str.substr(0, str.length - 1);
						}
						// Append value as comma-delimited string
						str += ',' + encodeURI(formElem.value);
					}
					else{
						str += formElem.name + '=' + encodeURI(formElem.value);
					}
					str += '&';
					lastElemName = formElem.name;
				}
				break;

		}
	}
	// Remove trailing separator
	str = str.substr(0, str.length - 1);
	return str;
}

function xmlhttpPost(strURL, strSubmit, strResultFunc, bolFileUpload, strOptParams1, strOptParams2) 
{
	OpenAjaxCalls++;
	if(document.getElementById("PleaseWait")){
		document.getElementById("PleaseWait").style.display = "";
	}
	var xmlHttpReq = false;

// New code which IS IE7-compliant
if (window.XMLHttpRequest)
	// If IE7, Mozilla, Safari, etc: Use native object
	{var xmlHttpReq = new XMLHttpRequest();}
else{
	if (window.ActiveXObject)
		// ...otherwise, use the ActiveX control for IE5.x and IE6
		{var xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");}
}

	// Old Code which is NOT IE7-compliant
/*	if (window.XMLHttpRequest){
		xmlHttpReq = new XMLHttpRequest();
		xmlHttpReq.overrideMimeType('text/xml');
	}
	// IE
	else if (window.ActiveXObject){
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}*/

	xmlHttpReq.open('POST', strURL, true);	
	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	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 (strResultFunc){ 
							if (strOptParams1 && strOptParams2){ 
								eval(strResultFunc + '(strResponse,' + strOptParams1 + ', ' + strOptParams2 + ');'); }
							else if (strOptParams1){ 
								eval(strResultFunc + '(strResponse,' + strOptParams1 + ');'); }
							else{ 
								eval(strResultFunc + '(strResponse);'); }
						}
					//}
					break;
			}
		}
	}
	xmlHttpReq.send(strSubmit);
}
function DoNothing()
{
	OpenAjaxCalls--;
	if(OpenAjaxCalls == 0 && document.getElementById("PleaseWait"))
		document.getElementById("PleaseWait").style.display = "none";
	var nothing = 0;
}

function handleErrFullPage(strIn){
	if(userUID == "joe.strollo"){
		OpenWindow=window.open("", "newwin", "toolbar=no,scrollbars="+scroll+",menubar=no");
		OpenWindow.document.write(strIn);
		OpenWindow.document.close();
	}else{
		alert('An error has occurred in this application. Do not continue and please contact Joe regarding this message');
		var sPath = document.referrer; 
		var sPage = sPath.substring(33,sPath.lastIndexOf('?'));
	
		handleErrFullPage = null;
		xmlhttpPost('http://www.pdelena.com/admin/SendError.asp', 'strIn='+escape(strIn)+'&referringPage='+sPage, 'DoNothing',false);
	}
}

