// 2008 Austin Michael Internet Solutions Common JavaScript

function submitForm(formID, action)
{
	var submitForm = document.getElementById(formID);
	var oldActionElement = document.getElementById("frmAction");
	if (oldActionElement != null)
		submitForm.removeChild(oldActionElement);
	
 	var actionElement = document.createElement("input");
    actionElement.setAttribute("id", "frmAction");
    actionElement.setAttribute("name", "Action");
    actionElement.setAttribute("type", "hidden");
    actionElement.setAttribute("value", action);
    submitForm.appendChild(actionElement);
    submitForm.submit();
}

function showHide(elementID, state) {

	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval( "document.all." + elementID + ".style.visibility = '" + state + "'");

	}
				
	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[elementID].visibility = state;
	}
				
	if (document.getElementById && !document.all) {
		maxwell_smart = document.getElementById(elementID);
		maxwell_smart.style.visibility = state;
	}
}

function show(elementID) {

	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval( "document.all." + elementID + ".style.visibility = 'visible'");

	}
				
	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[elementID].visibility = 'visible';
	}
				
	if (document.getElementById && !document.all) {
		maxwell_smart = document.getElementById(elementID);
		maxwell_smart.style.visibility = 'visible';
	}
}


function hide(elementID) {

	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval( "document.all." + elementID + ".style.visibility = 'hidden'");

	}
				
	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[elementID].visibility = 'hidden';
	}
				
	if (document.getElementById && !document.all) {
		maxwell_smart = document.getElementById(elementID);
		maxwell_smart.style.visibility = 'hidden';
	}
}
			
			
function insertHTML(elementID, html)
{
	document.getElementById(elementID).htmlContent = html;
	document.getElementById(elementID).innerHTML = html;
}

function checkUncheckAll(form)
{
	var SelectAll = form.selectAll;
	for ( var i=0; i < form.elements.length; i++ )
	{
		var e = form.elements[i];
		if (SelectAll.checked)
		{
			e.checked = true;
		}
		else
		{
			e.checked = false;
		}
	}
}

function turnOnLabel(elementID, className)
{
	if (elementID == null)
		return false;
	
	var targetElement =   document.getElementById(elementID);
	
	if (targetElement.className != className)
	{
		targetElement.className = className;
  	} 
	return true;
} 

function turnOffLabel(elementID)
{
	if(elementID == null)
		return false;
	
	var targetElement =   document.getElementById(elementID);
	
	if (targetElement.className != '')
	{
		targetElement.className = '';
	} 
  	return true;
}

function changeLabel(elementID, className)
{
	if(elementID == null)
		return false;
    
	var targetElement =   document.getElementById(elementID);

	if (targetElement.className == className)
	{
		targetElement.className = '';
	}
	else
	{
		targetElement.className = className;
	}

	return true;
} 

function removeSelectOptions(selectElementID, bolSelectedOnly)
{
	var elSel = document.getElementById(selectElementID);
	var i;
	for (i = elSel.length - 1; i>=0; i--)
	{
    	if (bolSelectedOnly && elSel.options[i].selected)
    	{
      		elSel.remove(i);
      	}
      	else
      	{
      		elSel.remove(i);
      	}
    }
}

function addSelectOption(elementID, name, value)
{
	var itemList = document.getElementById(elementID);
	itemList.options[itemList.options.length] = new Option(name,value);
				
}
			
function clearSelectOptions(elementID)
{
			
	var itemList = document.getElementById(elementID);
	itemList.options.length = 0;
}


function appendSelectOption(selectElementID, optionName, optionValue)
{
	var elOptNew = document.createElement('option');
	elOptNew.text = optionName;
	elOptNew.value = optionValue;
	var elSel = document.getElementById(selectElementID);

	try {
    	elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
    } catch(ex) {
    	elSel.add(elOptNew); // IE only
  	}
}

function selectOption(selectElementID, optionValue)
{
	var elSel = document.getElementById(selectElementID);
	var i;
	for (i = 0; i< elSel.length; i++)
	{
		if (elSel.options[i].value == optionValue)
		{
			elSel.selectedIndex = i;
			break;
		}
    }

}

function getJSDateObject(sqlDate)
{
	if (sqlDate.split('-') != "")
	{
		arr = sqlDate.split('-');
	}
	else if (sqlDate.split('/') != "")
	{
		arr = sqlDate.split('/');
	}
	
	return new Date(parseInt(arr[0]), parseInt(arr[1], 10) - 1, parseInt(arr[2].substr(0,2), 10));
		
}

function urlEncode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function urlDecode(str) {
	return unescape(str.replace(/\+/g," "));
}


//only works for same hostname includes
function clientSideInclude(id, url) {
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
 var element = document.getElementById(id);
 if (!element) {
  alert("Bad id " + id + 
   "passed to clientSideInclude." +
   "You need a div or span element " +
   "with this id in your page.");
  return;
 }
  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    element.innerHTML = req.responseText;
  } else {
    element.innerHTML =
   "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }
}


