//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// helpers for wiring-up event handlers
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function addEventHandler_OnLoad(oFunc)
{
	var oldHandler = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = oFunc;
	} else {
		window.onload = function() {
			oldHandler();
			oFunc();
		}
	}
}

function addEventHandler_OnUnload(oFunc)
{
	var oldHandler = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = oFunc;
	} else {
		window.onunload = function() {
			oldHandler();
			oFunc();
		}
	}
}

function addEventHandler_OnLoadAndUnload(oFunc)
{
	addEventHandler_OnLoad(oFunc);
	addEventHandler_OnUnload(oFunc);
}

function addEventHandler_OnSubmit(oForm,oFunc)
{
	var oldHandler = oForm.onsubmit;
	if (typeof oForm.onsubmit != 'function') {
		oForm.onsubmit = oFunc;
	} else {
		oForm.onsubmit = function() {
			oldHandler();
			oFunc();
		}
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// input-event handlers
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function ClickOnCrKeyPress(e,button)
{
	if(getkey(e)==13)
	{
		button.click();
		return false;
	}
	else return true;
}

function DoOnCrKeyPress(e,oFunc)
{
	if(getkey(e)==13)
	{
		oFunc();
		return false;
	}
	else return true;
}

function VoidOnCrKeyPress(e)
{
	return (getkey(e)!=13);
}

function getkey(e) {
	if (window.event)
	   return window.event.keyCode;
	else if (e)
	   return e.which;
	else
	   return null;
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// general form functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function addHiddenInputToForm(form, name, value)
{
	var newInput = document.createElement('INPUT');
	if(document.all) {
		newInput.type = 'hidden';
		newInput.id  = name;
		newInput.name = name;
		newInput.value = value;
	} else {
		newInput.setAttribute('type','hidden');
		newInput.setAttribute('id',name);
		newInput.setAttribute('name',name);
		newInput.setAttribute('value',value);
	}
	form.appendChild(newInput);
}

function ClearDefaultValue(o)
{
	if(o && o.value && o.defaultValue && o.value.toLowerCase()==o.defaultValue.toLowerCase()) o.value = '';
}

function formFocus(strFormname, strElement) 
{
	var objE = eval('document.forms.' + strFormname + '.' + strElement);
	if (objE) objE.focus();
}

function InlineDelete_Submit(sender,keyfield,id)
{
	var row = document.getElementById(id.toString());
	var strClassName = '';
	if(row)
	{
		strClassName = row.className;
		row.className = 'delitem';
	}
	
	if(confirm('Are you sure you want to delete the selected record?\t'))
	{
		eval('sender.form.'+keyfield).value = id;
		return true;
	}
	else
	{
		if(row) row.className = strClassName;
		return false;
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// check-list functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function CheckALL(objCheckbox)
{
	if (objCheckbox)
	{
		var len = objCheckbox.length;
		
		if (len > 0)
		{
			var i=0;
			for (i=0 ; i<len ; i++)
			{
				objCheckbox[i].checked=true;
			}
		} else {
			objCheckbox.checked=true;
		}
	}
	return false;
}

function UnCheckALL(objCheckbox)
{
	if (objCheckbox)
	{
		var len = objCheckbox.length;
		
		if (len > 0)
		{
			var i=0;
			for (i=0 ; i<len ; i++)
			{
				objCheckbox[i].checked=false;
			}
		} else {
			objCheckbox.checked=false;
		}
	}
	return false;
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// option-list functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getSelectedText(elmnt)
{
	if(elmnt && elmnt.options) {
		return elmnt.options[elmnt.selectedIndex].text;
	}
	return '';
}

function getSelectedTextById(elementId)
{
	var elmnt = document.getElementById(elementId);
	return getSelectedText(elmnt);
}

function getSelectedValue(elmnt)
{
	if(elmnt && elmnt.options) {
		return elmnt.options[elmnt.selectedIndex].value;
	}
	return '';
}

function getSelectedValueById(elementId)
{
	var elmnt = document.getElementById(elementId);
	return getSelectedValue(elmnt);
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// text field functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function InitTextCounter(FormName,InputName,MaxLen)
{
	addEventHandler_OnLoad(function(){TextCounter_Window_OnLoad(FormName,InputName,MaxLen);});
}

function textCounter(field,cntfield,maxlimit)
{
	if (field.value.length > maxlimit) {
		// if too long trim it!
 		field.value = field.value.substring(0, maxlimit);
	}
	// update 'characters left' counter
	cntfield.value = (maxlimit - field.value.length);
}

function TextCounter_Window_OnLoad(FormName,InputName,MaxLen)
{
	var form = eval('document.'+FormName);
	var TextInput = eval('form.'+InputName);
	
	TextInput.onkeyup = function(){
		textCounter(this,eval('this.form.'+InputName+'_Counter'),MaxLen);
	}
	
	eval('form.'+InputName+'_Counter').value = (MaxLen - TextInput.value.length);
}

function textTrimmer(field,maxlimit)
{
	if (field.value.length > maxlimit) {
		// if too long trim it
  		field.value = field.value.substring(0, maxlimit);
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// window functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var m_blnRefreshWindow = false;

function clickOpenerButton(buttonId,openerUrl)
{
	var openerForm;
	var buttonToClick;
	
	if(opener)
	{
		openerForm = opener.document.forms[0];
		if(openerForm) buttonToClick = eval('openerForm.'+buttonId);
	}
	if(buttonToClick)
	{
		buttonToClick.click();
	}
	else
	{
		openerLocation(openerUrl);
	}
	closePopup();
}

function closePopup()
{
	window.close(self);
	return false;
}

function focusOpener()
{
	if (windowIsOpen(opener)) {
		opener.focus();
	}
}

function focusPopup(objPopup,theURL,winName,features,width,height)
{
	if (!windowIsOpen(objPopup)) {
		objPopup = returnPopup(theURL,winName,features,width,height);
	}
	 objPopup.focus();
	 return objPopup;
}

function openPopup(theURL,winName,features,width,height)
{
	var objPopup = returnPopup(theURL,winName,features,width,height);
	objPopup.focus();
	return false;
}

function openerLocation(strLocation)
{
	if (windowIsOpen(opener)) {
		opener.location.href=strLocation;
		opener.focus();
	} else {
		window.open(strLocation);
		window.close(self);
	}
}

function refreshOpener(strDefaultURL,blnCloseMe)
{
	var blnExists = false;
	
	if (windowIsOpen(opener)) {
	 	if (opener.m_blnRefreshWindow==true) {
			opener.location.reload();
			opener.focus();
		} else {
			blnCloseMe = false;
		}
		blnExists = true;
	}
	
	if (blnExists==false) {
		window.open(strDefaultURL);
		blnCloseMe = true;
	}
	
	if (blnCloseMe==true) window.close(self);
}

function returnPopup(theURL,winName,features,width,height)
{
	var winWidth	= width;
	var winHeight	= height;
	var strWinSize	= ",width=" + winWidth + ",height=" + winHeight;

	if (window.screen) {
		var winPosL = (screen.availWidth - winWidth) / 2;
		var winPosT = (screen.availHeight - winHeight) / 2;
		strWinSize += ",left=" + winPosL + ",screenX=" + winPosL + ",top=" + winPosT + ",screenY=" + winPosT;
	}	
	
	return window.open(theURL,winName,features + strWinSize);
}

function setOpenerRefresh()
{
	if (windowIsOpen(opener)) {
		opener.m_blnRefreshWindow = true;
	}
}

function setWinStatus(value)
{
	window.status=value;
	return true;
}

function windowIsOpen(objWindow)
{
	var blnIsOpen = false;
	
	if (typeof(objWindow)=='object') {
		if (!objWindow.closed) {
			blnIsOpen = true;
		}
	}
	return blnIsOpen;
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// display functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function setCssDisplay(domId,value)
{
	document.getElementById(domId).style.display = value;
}

function getCssDisplayCookie(domId)
{
	if(document.cookie)
	{
		var CssStyle = document.getElementById(domId).style;
		if((CssStyle)&&(document.cookie.toString().indexOf(domId+'DisplayOn')<0))
		{
			CssStyle.display = 'none';
		}
		else
		{
			CssStyle.display = '';
		}
	}
}

function setCssDisplayCookie(domId)
{
	if(document.cookie)
	{
		var CssStyle = document.getElementById(domId).style;
		if((CssStyle)&&(CssStyle.display==''))
		{
			document.cookie = domId+'Display='+domId+'DisplayOn';
		}
		else
		{
			document.cookie = domId+'Display='+domId+'DisplayOff';
		}
	}
}

function switchCssDisplay(domId)
{
	var CssStyle = document.getElementById(domId).style;
	if(CssStyle)
	{
		CssStyle.display = (CssStyle.display=='') ? 'none' : '';
		setCssDisplayCookie(domId);
	}
	return false;
}

function toggleChildDisplay(parentId,childOn,childOff)
{
	var CssStyle = document.getElementById(parentId).style;
	if((CssStyle)&&(CssStyle.display==''))
	{
		document.getElementById(childOff).style.display = '';
		document.getElementById(childOn).style.display = 'none';
	}
	else
	{
		document.getElementById(childOff).style.display = 'none';
		document.getElementById(childOn).style.display = '';
	}
	return false;
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// privacy functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function CharShiftDecrypt(strInVal, shiftval, shiftCharSet)
{	// shift all the characters in the inval by shiftval characters from the charset
	// example:  "cat", 2, "abcdefghijklmnopqrstuvwxyz" would become "ecv"
	// if a character is not found in charset, it is untouched
	// if a shift operation goes out of bounds, it will roll to the other side of charset
	var strInString = new String(strInVal);
	var intInString = strInString.length;
	var strCharSet = new String(shiftCharSet);
	var intCharSetLen = strCharSet.length;
	var strOutVal = new String('');
	
	var nextchar, nextindex, i;
	
	// for each character
	for (i=0 ; i < intInString ; i++)
	{	// grab the next character to decrypt
		nextchar = strInString.substr(i, 1);
		// look it up in charset
		nextindex = strCharSet.indexOf(nextchar, 0);
		if (nextindex >= 0)
		{	// found it, modulo it so we can stay in bounds for next operation
			nextindex = (nextindex  - shiftval) % intCharSetLen;
			// check bounds of nextindex
			if (nextindex < 0)
			{	// wrap around to high end of charset
				nextindex = nextindex + intCharSetLen;
			}
			else if (nextindex >= intCharSetLen)
			{	// this wont happen btw, becuase of modulo, but anyway
				nextindex = nextindex - intCharSetLen;
			}
			strOutVal += strCharSet.charAt(nextindex);
		}	
		else
		{	// char not found in set, so add it as is
			strOutVal += nextchar;
		}
	}
	
	return strOutVal;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//XML/HTTP funtions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function setInnerHtmlFromHttpRequest(Id, Url)
{
	var xmlhttp = null;
	if (window.XMLHttpRequest)
	{	//code for Mozilla, etc.
		xmlhttp = new XMLHttpRequest()
	} else if(window.ActiveXObject)
	{	//code for IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	if(xmlhttp)
	{	//wire up the event to handle the response on successful load
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState==4 && xmlhttp.status==200) {
				var container = document.getElementById(Id);
				container.innerHTML = xmlhttp.responseText;
				container.style.display = '';
			}
		}
		
		xmlhttp.open("GET",Url,true)
		xmlhttp.send(null)
	}
}