// 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
      	{
      		eSel.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 deselectOptions(selectElementID, optionValue)
{
	//if optionValue == null, clear all
	var elSel = document.getElementById(selectElementID);
	var i;
	for (i = 0; i< elSel.length; i++)
	{
		if (optionValue == null)
		{
			elSel.options[i].selected = false;
		}
		else
		{
			if (elSel.options[i].value == optionValue)
			{
				elSel.options[i].selected = false;
			}
		}
	}
}

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 formatDate(date, format)
{
	var formatted = "";
	
	if (date != null)
	{
		var curr_date = date.getDate();
		var curr_month = date.getMonth() + 1;
		var curr_year = date.getFullYear();
		formatted = curr_month + "/" + curr_date + "/" + curr_year;
	}
	
	return formatted;
}

function getParameter(parameterName)
{
	var queryString = window.top.location.search.substring(1);


	// Add "=" to the parameter name (i.e. parameterName=value)
	var parameterName = parameterName + "=";
	
	if ( queryString.length > 0 )
	{
		// Find the beginning of the string
		begin = queryString.indexOf ( parameterName );
		// If the parameter name is not found, skip it, otherwise return the value
		if ( begin != -1 )
		{
			// Add the length (integer) to the beginning
			begin += parameterName.length;
			// Multiple parameters are separated by the "&" sign
			end = queryString.indexOf ( "&" , begin );
			if ( end == -1 ) {
				end = queryString.length
			}
			// Return the string
			return unescape ( queryString.substring ( begin, end ) );
		}
		// Return "null" if no parameter has been found
		return "null";
	}
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function toggleActive(element, bolActive)
{
	//alert(element);
	var disabled = !bolActive;
	//alert(bolActive + ' ' + disabled);
	try {
		element.disabled = disabled;
	}
	catch(E){
	
	}
	if (element.childNodes && element.childNodes.length > 0) {
		for (var x = 0; x < element.childNodes.length; x++) {
			toggleActive(element.childNodes[x], bolActive);
		}
	}
}


//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.";
  }
}


