// JavaScript Document

function AjaxUpdate (formEl) {
	//alert (formEl.target+', '+formEl.action+', '+formEl.method+', '+formToString(formEl));
	var myAjax = new Ajax.Updater (
			formEl.target, 
			formEl.action, 
			{ 
				method:			formEl.method, 
				parameters:		formToString(formEl),
				evalScripts:	true
			});
	return false;
}

function formToString (docForm) {
	var strSubmitContent = '';
	var formElem;
	var strLastElemName = '';
	
	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':
				strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&';
				break;
				
			// Radio buttons
			case 'radio':
				if (formElem.checked) {
					strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&';
				}
				break;
				
			// Checkboxes
			case 'checkbox':
				if (formElem.checked) {
					// Continuing multiple, same-name checkboxes
					if (formElem.name == strLastElemName) {
						// Strip of end ampersand if there is one
						if (strSubmitContent.lastIndexOf('&') == strSubmitContent.length-1) {
							strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
						}
						// Append value as comma-delimited string
						strSubmitContent += ',' + escape(formElem.value);
					}
					else {
						strSubmitContent += formElem.name + '=' + escape(formElem.value);
					}
					strSubmitContent += '&';
					strLastElemName = formElem.name;
				}
				break;
				
		}
	}
	
	// Remove trailing separator
	strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
	// Preserve + symbols
	strSubmitContent = strSubmitContent.replace(/\+/g,'%2B');
	// Return formatted GET-style string
	return strSubmitContent;
}

function displayTagById (displayStyle, tagType, tagId, flagPrefixSuffix) {
	// get all elements of specified tag type, usually div
	var el = document.getElementsByTagName(tagType);
	// loop through each element
	for (i=0;i<el.length;i++) {
		if (
			(flagPrefixSuffix==null && el[i].id==tagId) || // exact match
			(flagPrefixSuffix=='prefix' && el[i].id.substr(0,tagId.length)==tagId) || // prefix match
			(flagPrefixSuffix=='suffix' && el[i].id.substr((el[i].id.length-tagId.length),el[i].id.length)==tagId) // suffix match
		) {
			// displayStyle can be any valid css display property or 'toggle'
			if (displayStyle=='toggle') {
				if (document.getElementById(el[i].id).style.display=='none') displayStyle='block';
				else displayStyle='none';
			}
			// update displayStyle on matched element
			document.getElementById(el[i].id).style.display=displayStyle;
		}
	}
}