//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// site-specific declarations
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
var m_blnDoBodyUnload = false;
var m_winAddressManager;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// site-specific functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
function WriteAdminNavBar(LinkedMemberExists, HasMemberAuthID, MemberName)
{
	try{if(m_blnSuppressAdminNavBar) return;}catch(e){};
	
	document.writeln('<style type="text/css">body { margin:20px 0px 0px 0px }');
	document.writeln('#AdminNavBar {');
	document.writeln('	background:#FAEB86; border-bottom:solid 1px #915131; color:Black; width:100%; height:20px;');
	document.writeln('	font-size:11px; line-height:20px; text-align:center; text-transform:uppercase;');
	document.writeln('	overflow:hidden; position:absolute; top:0; left:0 }');
	document.writeln('#AdminNavBar a { color:#CC0000; text-decoration:none }');
	document.writeln('#AdminNavBar a:hover { color:#CC0000; text-decoration:underline }');
	document.writeln('</style><div id="AdminNavBar">');
	
	if(HasMemberAuthID) {
		document.writeln('<div style="float:left">&nbsp;&nbsp;You are currently signed in as community member "'+ MemberName +'"&nbsp;&nbsp;</div>');
		document.writeln('<div style="float:right">&nbsp;');
		document.writeln('<a href="/general/logout.asp">Sign Out of the Community</a>&nbsp;');
	} else if(LinkedMemberExists) {
		document.writeln('<div style="float:center">&nbsp;');
		document.writeln('<a href="/general/login_admin_as_linked_member.asp">Sign In to the Community</a>&nbsp;');
	} else {
		document.writeln('<div style="float:center">');
	}
	
	document.writeln('&nbsp;&nbsp;<a href="/admin/">Return to Secure Backend</a>&nbsp;&nbsp;</div></div>');
}

function CheckCityStateZip(oForm,sCityField,sZipField,sStateField,sStateList)
{
	if(eval('oForm.'+sCityField).value=='') return false;
	if(eval('oForm.'+sZipField).value=='') return false;
	if(eval('oForm.'+sStateField).value=='')
	{
		var oStateList = eval('oForm.'+sStateList);
		if(oStateList.options[oStateList.selectedIndex].value=='') return false;
	}
	
	return true;
}

function CheckMultipartForms(blnAllowSubmit)
{
	if(blnAllowSubmit) return;
	if(!document.forms.length>0) return;
	
	var blnFoundOne = false;
	for(var i = 0; i<document.forms.length; i++)
	{
		if(document.forms[i].enctype.toLowerCase()=='multipart/form-data')
		{
			blnFoundOne = true;
			document.forms[i].onsubmit = function(){return false;}
		}
	}
	
	if(blnFoundOne) alert('We\'\re sorry, file uploads and forms containing file uploads\nhave been temporarily disabled. Please check back in a few minutes.');
}

function DoUploadProgress(sender)
{	//does the hidden field exist for storing the progress-id?
	if(!sender.ProgressID) return true;
	
	//are there any files being uploaded?
	var fields = sender.elements;
	var bHasFile = false;
	FieldLoop: for(var i=0; i<fields.length; i++) {
		if(fields[i].type.toLowerCase()=='file' && fields[i].value!='') {
			bHasFile = true;
			break FieldLoop;
		}
	}
	
	if(!bHasFile) return true;
	
	var sPid = sender.ProgressID.value;
	var sAction = sender.action;
	
	var rx = new RegExp("\\?.+=","i");
	if(rx.test(sAction)) {
		sAction +='&';
	} else {
		rx.compile("\\?$","i");
		if(!rx.test(sAction)) sAction +='?';
	}
	
	sAction +='pid='+ sPid;
	sender.action = sAction;
	
	openPopup('/general/upload_progress.asp?pid='+ sPid,'UploadProgress','status,toolbar=false',250,120);
	
	return true;
}

function openCsvExport(unicode,alternateUrl,extraQueryArgs)
{
	var Url = alternateUrl==null ? '/admin/database/csv_export.asp' : alternateUrl;
	if(extraQueryArgs==null) extraQueryArgs = '';
	openPopup(Url +'?unicode='+ unicode.toString() +'&'+ extraQueryArgs.toString(),'CsvExport','resizable,status,toolbar=false',350,150);
	return false;
}

//address-manager window functions
function openAddressManager(AddrMgrUrl,MasterElementId,QueryArgs)
{
	m_winAddressManager = returnPopup(AddrMgrUrl+'?OpenerMasterElementId='+MasterElementId+'&'+QueryArgs,'AddressManager','resizable,scrollbars,status,toolbar=false',660,475);
	m_winAddressManager.focus()
	return false;
}

function closeAddressManager()
{
	try{m_winAddressManager.close();}catch(e){};
}

function openMugshotPopup(Url)
{
	return openPopup(Url,'MemberMugshot','resizable,scrollbars,status,toolbar=false',537,405);
}

//begin dynamic help functions
var m_blnInlineHelpIsOn = false;

function switchInlineHelpDisplay()
{
	if(m_blnInlineHelpIsOn)
	{
		doInlineHelpOff();
	} else {
		doInlineHelpOn();
	}
	return false;
}

function doInlineHelpOn()
{
	//show inline help areas
	setInlineHelpDisplay('');
	//switch toggle link
	dhtmlDisplay('InlineHelpLinkShow','none');
	dhtmlDisplay('InlineHelpLinkHide','');
	//set current state
	m_blnInlineHelpIsOn = true;
	//preserve current state in cookie
	setInlineHelpCookie();
}

function doInlineHelpOff()
{
	//hide inline help areas
	setInlineHelpDisplay('none');
	//switch toggle link
	dhtmlDisplay('InlineHelpLinkShow','');
	dhtmlDisplay('InlineHelpLinkHide','none');
	//set current state
	m_blnInlineHelpIsOn = false;
	//preserve current state in cookie
	setInlineHelpCookie();
}

function setInlineHelpCookie()
{
	if(document.cookie)
	{	//set cookie expiry at 1 year
		var datExpires = new Date();
		datExpires.setTime(datExpires.getTime()+31536000000);
		if(m_blnInlineHelpIsOn)
		{
			document.cookie = 'InlineHelpDisplay=InlineHelpDisplayOn; expires='+datExpires.toGMTString()+'; path=/';
		} else {
			document.cookie = 'InlineHelpDisplay=InlineHelpDisplayOff; expires='+datExpires.toGMTString()+'; path=/';
		}
	}
}

function setInlineHelpDisplay(value)
{
	setInlineHelpDisplayItems(document.anchors,value);
	setInlineHelpDisplayItems(document.links,value);
}

function setInlineHelpDisplayItems(items,value)
{
	for(var i=0;i<items.length;i++)
	{
		if(items[i].className.toLowerCase()=='inlinehelp')
		{
			items[i].style.display = value.toString();
		}
	}
}

function initInlineHelpDisplay()
{
	if((document.cookie)&&(document.cookie.toString().indexOf('InlineHelpDisplayOff')>=0))
	{
		doInlineHelpOff();
	} else {
		doInlineHelpOn()
	}
}

function mediaPopup(theURL) {
	openPopup(theURL,'winMediaPopup','status,toolbar=false',622,496);
	return false;
}

function AlertAppUpdate()
{
	if(confirm('There have been updates to the\nsystem since your last visit!\n\nClick OK to review the latest features, \nenhancements and bug fixes.'))
		location.href='/admin/client_services/updates.asp';
}

function ConfirmSignIn(strMemberName)
{
	return confirm('You will now be signed in as '+ strMemberName +'. \n\nAre you sure you want to continue? ');
}

//search form functions
function SearchForm_q_OnKeyPress(e,sender)
{
	if(getkey(e)==13) {
		return SearchForm_Validate(sender);
	}
	return true;
}

function SearchForm_Validate(sender)
{
	var sVal = sender.form.q.value;
	if(sVal=='' || /^[\s]+$/.test(sVal)) {
		alert('Please enter some search criteria. ');
		return false;
	} else {
		return true;
	}
}

function StartNewSearch(FormId)
{
	var SearchForm = document.getElementById(FormId);
	if(SearchForm)
	{
		SearchForm.q.value = '';
		SearchForm.style.display = '';
		SearchForm.q.focus();
	}
	return false;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// common functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
function fixnewlines(val)
{             
	// Adjust newlines so can do correct character counting for MySQL. MySQL counts a newline as 2 characters.
	if (val.indexOf('\r\n')!=-1)
	; // this is IE on windows. Puts both characters for a newline, just what MySQL does. No need to alter
	else if (val.indexOf('\r')!=-1)
		val = val.replace ( /\r/g, "\r\n" ); // this is IE on a Mac. Need to add the line feed
	else if (val.indexOf('\n')!=-1)
		val = val.replace ( /\n/g, "\r\n" ); // this is Firefox on any platform. Need to add carriage return
	else 
	; // no newlines in the textarea  
	return val;
}

function isEmpty(val)
{
	return (!val || val==null || val.toString()=='');
}

function replaceAccents(sVal)
{
	var RetVal = new String(sVal);
	
	var regExps = [ /[\xC0-\xC5]/g,
		/[\xE0-\xE5]/g,
		/[\xC8-\xCB]/g,
		/[\xE8-\xEB]/g,
		/[\xCC-\xCF]/g,
		/[\xEC-\xEF]/g,
		/[\xD2-\xD6]/g,
		/[\xF2-\xF6]/g,
		/[\xD9-\xDC]/g,
		/[\xF9-\xFC]/g,
		/\xDD/g, /\xFD/g,
		/\xC7/g, /\xE7/g,
		/\xD1/g, /\xF1/g ];
	
	var repChar = ['A','a','E','e','I','i','O','o','U','u','Y','y','C','c','N','n'];
	
	for(var i=0; i<regExps.length; i++)
		RetVal = RetVal.replace(regExps[i],repChar[i]);
	
	return RetVal;
}

function replaceReturns(sVal)
{
	var RetVal = fixnewlines(sVal.toString());
	return RetVal.replace(/\r\n/g,'<br/>');
}

function stripHtml(sVal)
{
	return sVal.toString().replace(/<[\?\/!A-Za-z]+[^<>]*>?/ig,' ');
}

function urlDecode(sVal)
{
	if(isEmpty(sVal)) return sVal;
	return decodeURIComponent(sVal.toString()).replace(/\+/g, ' ');
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// 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();
		}
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// window functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
var blnRefreshWindow = false;

function windowIsOpen(objWindow)
{
	var blnIsOpen = false;
	
	if (typeof(objWindow)=='object') {
		if (!objWindow.closed) {
			blnIsOpen = true;
		}
	}
	return blnIsOpen;
}

function setOpenerRefresh()
{
	if (windowIsOpen(opener)) {
		opener.blnRefreshWindow = true;
	}
}

function reloadSelf()
{
	var now = new Date();
	var newHref = location.protocol +'//'+ location.hostname + location.pathname;
	var qString = location.search.replace(/\&?reloadtime=[^\&]*\&?/,'');
	
	if(qString.length==0) {
		qString = '?'
	} else if(qString!='?') {
		qString += '&';
	}
	
	newHref += qString +'reloadtime='+ now.getTime();
	
	location.href = newHref;
}

function refreshOpener(strDefaultURL,blnCloseMe)
{
	var blnExists = false;
	
	if (windowIsOpen(opener)) {
	 	if (opener.blnRefreshWindow==true) {
			opener.location.reload();
			opener.focus();
		}
		blnExists = true;
	}
	
	if (blnExists==false) {
		window.open(strDefaultURL);
		blnCloseMe = true;
	}
	
	if (blnCloseMe==true) window.close(self);
}

function openerLocation(strLocation,blnCloseMe)
{
	if (windowIsOpen(opener)) {
		opener.location.href=strLocation;
		if(blnCloseMe) opener.focus();
	} else {
		window.open(strLocation);
	}
	
	if(blnCloseMe) window.close(self);
}

function focusPopup(objPopup,theURL,winName,features,width,height)
{
	var blnIsOpen = true;
	if (typeof(objPopup) != 'object') {
		blnIsOpen = false;
	} else if (objPopup.closed) {
		blnIsOpen = false;
	}
	if (blnIsOpen== false) {
		objPopup = returnPopup(theURL,winName,features,width,height);
	}
	 objPopup.focus();
	 return objPopup;
}

function goToUrlOnClick(Url)
{
	location.href = Url;
	return false;
}

function openPopup(theURL,winName,features,width,height)
{
	var objPopup = returnPopup(theURL,winName,features,width,height);
	objPopup.focus();
}

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 closePopup()
{
	window.close(self);
}

function setWinStatus(value)
{
	window.status=value;
	return true;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// form functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
function addOptionToSelectList(DomID, value)
{
	var SelectList = document.getElementById(DomID);
	SelectList.options[SelectList.length] = new Option(value,value);
}

function removeSelectedOption(DomID)
{
	var SelectList = document.getElementById(DomID);
	SelectList.options[SelectList.selectedIndex] = null;
}

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 checkRadioByValue(oRadio, value)
{
	if(oRadio) {
		if(oRadio.length>0)	{
			for(var i=0; i<oRadio.length; i++)
				oRadio[i].checked = (oRadio[i].value==value.toString());
		} else {
			oRadio.checked = (oRadio.value==value.toString());
		}
	}
}

function getCheckedRadioValue(oRadio)
{
	if(oRadio) {
		if(oRadio.length>0)	{
			for(var i=0; i<oRadio.length; i++) {
				if(oRadio[i].checked) return oRadio[i].value;
			}
		} else if(oRadio.checked) {
			return oRadio.value;
		}
	}
	return null;
}

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);
}

function selectOptionByValue(oSelect, value)
{
	if(oSelect) {
		for(var i=0; i<oSelect.length; i++) {
			if(oSelect.options[i].value==value.toString()) {
				oSelect.selectedIndex = i;
				break;
			}
		}
	}
}

function verifyMsg(jsStrURL, jsStrMsg) 
{
	if (confirm (jsStrMsg)) 
	{		
		this.window.location=jsStrURL;
		return true;
	}
}

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;
		}
	}
}

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;
		}
	}
}

function formFocus(strFormname, strElement) 
{
	var objE = eval('document.' + strFormname + '.' + strElement);
	if (objE) objE.focus();
}

function buildHumanSQL(objElement, strHeadline) {

	var inputLocal	= objElement;
	var strSQLHuman	= '<b>' + strHeadline + '</b>\n';
		strSQLHuman	+='<ul>\n';
	
	if (inputLocal) {
		var len = inputLocal.length;
		var i=0;
		for (i=0 ; i<len ; i++) {
		
			if (inputLocal.options[i].selected) {
			
				strSQLHuman += '<li>' + inputLocal.options[i].text + '<br></li>\n';
			}
		}
	}
	
	strSQLHuman += '</ul>'
	objElement.form.txt_sqlHuman.value = strSQLHuman;
	return true;
}

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 SubmitOnCrKeyPress(e,sender)
{
	if(getkey(e)==13)
	{
		sender.form.submit();
		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;
}

function confirmDelete(objForm, strElement, strValue)
{
	var ItemRow = document.getElementById(strValue.toString());
	var strPrevClass = '';
	
	if(ItemRow)
	{
		strPrevClass = ItemRow.className;
		ItemRow.className = 'delitem';
	}
	
	if(confirm('Are you sure you want to delete the selected item? '))
	{
		eval('objForm.'+ strElement).value = strValue;
		return true;
	}
	else
	{
		if(ItemRow) ItemRow.className = strPrevClass;
		return false;
	}
}

function InlineDelete_Submit(sender,keyfield,id)
{
	return confirmDelete(sender.form,keyfield,id);
}

function InlineItem_Delete(sender,ItemID)
{
	return confirmDelete(sender.form,'ItemID',ItemID);
}

//adds a new option to a userlist element
function UserListAdd(sender, ID, AllowComma, DefaultValue)
{
	var SelectList = eval('sender.form.'+ ID +'_select');
	var UserInput = eval('sender.form.'+ ID +'_input');
	
	if(SelectList && UserInput) {
		var val = stripHtml(UserInput.value.toString());
		if(!AllowComma) val = val.replace(/,/g,'');
		if(val!='') {
			SelectList.options[SelectList.length] = new Option(val,val);
			RebuildUserList(sender.form, ID);
		}
		if(DefaultValue!=null) UserInput.value = DefaultValue;
		try{ //IE throws an exception if the focus() method of an invisible object is called
			UserInput.focus();
		}catch(e){};
	}
}

//removes the selected option from a userlist element
function UserListRemove(sender, ID)
{
	var SelectList = eval('sender.form.'+ ID +'_select');
	if(SelectList) {
		SelectList.options[SelectList.selectedIndex] = null;
		RebuildUserList(sender.form, ID);
	}
}

//rebuilds the hidden value of a userlist element
function RebuildUserList(form, ID)
{
	var HiddenInput = eval('form.'+ ID);
	if(!HiddenInput) {
		addHiddenInputToForm(form, ID, '');
		HiddenInput = eval('form.'+ ID);
	}
	HiddenInput.value = '';
	
	var SelectList = eval('form.'+ ID +'_select');
	if(SelectList) {
		for(var i=0; i<SelectList.length; i++)
			HiddenInput.value += SelectList.options[i].value +'\r\n';
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// DHTML display functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
function ul_onclick(jsObj)
{
   var i;
   var style;
  
   for (i = 0; i < jsObj.children.length; i++)
   {
       style = jsObj.children[i].style;
       if (style.display == "none")
       {
           style.display = "";
       }
       else
       {
           style.display = "none";
       }
   } 
}

function setDisplay(DomId, Value)
{
	var element = document.getElementById(DomId);
	if(element) element.style.display = Value;
	return false;
}

function switchDisplay(strDomID)
{
	var CssStyle = document.getElementById(strDomID).style;
	if(CssStyle)
	{
		if(CssStyle.display=='')
		{
			CssStyle.display = 'none';
		}
		else
		{
			CssStyle.display = '';
		}
		setCssDisplayCookie(strDomID);
	}
	return false;
}

function switchDisplayToggle(parentId,childOn,childOff)
{
	var CssStyle = document.getElementById(parentId).style;
	if((CssStyle)&&(CssStyle.display==''))
	{
		dhtmlDisplay(childOff,'');
		dhtmlDisplay(childOn,'none');
	}
	else
	{
		dhtmlDisplay(childOff,'none');
		dhtmlDisplay(childOn,'');
	}
	return false;
}

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 textCounter(field,cntfield,maxlimit)
{
	var val = fixnewlines(field.value.toString());
	var len = val.length;
	
	//if aready too long, trim it before making any adjustments
	if(len > maxlimit) {
 		val = val.substring(0, maxlimit);
		len = val.length;
		field.value = val;
	}
	
	//update 'characters left' counter
	cntfield.value = (maxlimit - len);
}

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);
	}
	
	textCounter(TextInput,eval('form.'+InputName+'_Counter'),MaxLen);
}

function InitTextCounter(FormName,InputName,MaxLen)
{
	addEventHandler_OnLoad(function(){TextCounter_Window_OnLoad(FormName,InputName,MaxLen);});
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// DTHML edit functions 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
function dhtmlDisplay(domID,dVal) {
	var element = document.getElementById(domID);
	if(element) element.style.display = dVal;
}

function dhtmlFormEdit(objForm,domLEN,domID) {

	var styleView;
	var styleEdit;
	var i=1;
	for (i=1 ; i<=domLEN ; i++) {
	
		var styleView = document.getElementById('view'+i.toString()).style;
		var styleEdit = document.getElementById('edit'+i.toString()).style;
		
		if ((i.toString()==domID.toString())&&(styleView.display=="")) {
			styleView.display = "none";
			styleEdit.display = "";
			intLastOpenRow = i.toString();
		} else {
			styleView.display = "";
			styleEdit.display = "none";
		}
	}
	
	objForm.reset();
	return false;

}

function dhtmlFormSubmit(objForm1,objForm2,domID,sysAction) {

	var i=0;
	for (i=0 ; i<objForm2.length ; i++) {
	
		var objE = eval('objForm1.' + objForm2[i].name + domID);
				
		if (objE) {
			if (objE.type == 'checkbox'|objE.type == 'radio') {
				if (objE.checked) {
					objForm2[i].value = objE.value;
				}
			} else if (objE.type == 'select') {
				var j=0;
				for (j=0 ; j<objE.length ; j++) {
					if (objE.options[i].selected) {
						objForm2[i].value = objE.options[j].value;
					}
				}
			} else {
				objForm2[i].value = objE.value;
			}
		}
	}
	
	objForm2.sys_id.value = domID;
	objForm2.sys_action.value = sysAction;
	
	var blnSubmit;
	if (sysAction == 'delete') {
		blnSubmit = confirm('Click OK to delete this record.');
	} else {
		blnSubmit = true;
	}
	
	if (blnSubmit) objForm2.submit();
	return false;

}

function MaximizeScrollingBlock(DomId)
{
	var o = document.getElementById(DomId);
	
	if(o)
	{
		o.style.overflow = 'visible';
		o.style.height = 'auto';
	}
	
	return false;
}

function RestoreScrollingBlock(DomId, Height)
{
	var o = document.getElementById(DomId);
	
	if(o)
	{
		o.style.overflow = 'auto';
		o.style.height = Height;
	}
	
	return false;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// "AJAX" functions ;)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
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 && xmlhttp.responseText!='') {
				var container = document.getElementById(Id);
				container.innerHTML = xmlhttp.responseText;
				container.style.display = '';
			}
		}
		
		xmlhttp.open("GET",Url,true)
		xmlhttp.send(null)
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// 3rd-party functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

// Countdown in Java Script .. Cameron Gregory http://www.bloke.com/
// permission to use and modify as long as you leave these 4 comment
// lines in tact and unmodified.
// http://www.bloke.com/javascript/Countdown/
var clockForm, clockTime, clockTimeout, clockFormat, clockTid, clockRefresh;

function doDate() {

		dt = new Date();
		
		if (clockTime != 0) {			
			v1 = Math.round(( clockTime - dt )/1000);
			//add timeout value to time
			v1 += Math.round(clockTimeout * 60);
			if (v1 <= 0) {
				clockForm.date.value = "expired";
				//dont set the timer again
			}
			else {
				if (clockFormat == 1)
					clockForm.date.value = v1;
				else if (clockFormat ==2) {
					sec = v1%60;
					v1 = Math.floor(v1/60);
					min = v1 %60 ;
					hour = Math.floor(v1/60);
					if (sec < 10 ) sec = "0"+sec;
					if (min < 10 ) min = "0"+min;
					if (hour > 0) {
				        clockForm.date.value = hour+"h "+min+"m "+sec+"s";
						} else {
						clockForm.date.value = min+"m "+sec+"s";
					}
				}
				else if (clockFormat ==3) {
					sec = v1%60;	
					v1 = Math.floor( v1/60);
					min = v1 %60 ;
					v1 = Math.floor(v1 / 60);
					hour = v1 %24 ;
					day = Math.floor(v1 / 24);
					if (sec < 10 ) sec = "0"+sec;
					if (min < 10 ) min = "0"+min;
					if (hour < 10 ) hour = "0"+hour;
					clockForm.date.value = day+"d "+hour+"h "+min+"m "+sec+"s";
				}
				else if (clockFormat ==4 ) {
					sec = v1%60;
					v1 = Math.floor( v1/60);
					min = v1 %60 ;
					v1 = Math.floor(v1 / 60);
					hour = v1 %24 ;
					day = Math.floor(v1 / 24);
					clockForm.date.value = day+(day==1?"day ":"days ")+hour+(hour==1?"hour ":"hours ")+min+(min==1?"min ":"mins ")+sec+(sec==1?"sec ":"secs ")
				}
				else {
					clockForm.date.value = "invalid format";
				}
			 	clockTid=window.setTimeout("doDate()",clockRefresh);
			}
		}
		else
			clockForm.date.value = "error";
}

function startCountdown(objForm,time,timeout,format) {
	clockForm = objForm;
	clockTime = new Date(time);
	clockTimeout = timeout;
	clockFormat = format;
	clockTid = 0;
	clockRefresh = 1000;
	if (Math.round((clockTime - new Date())/1000) < -60) {
		//clock is too far out of sync
		clockForm.date.value = "unknown";
	}
	else {
		clockTid=window.setTimeout("doDate()",clockRefresh);
	}
}

//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
function CharShiftDecrypt(strInVal, shiftval, shiftCharSet) {
	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, ascii_nextchar, i;
	
	//for each character
	for (i=0 ; i < intInString ; i++) {
		// grab the next character to encrypt
		nextchar = strInString.substr(i, 1);

		//look it up in charset
		nextindex = strCharSet.indexOf(nextchar,0)
		if (nextindex >= 0){ //found it
			nextindex = (nextindex  - shiftval) % intCharSetLen // modulo this so can stay in bounds for next operation
			
			//check bounds of nextindex
			if (nextindex < 0) {
				nextindex = nextindex + intCharSetLen //wrap around to high end of charset
			}
			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;

}

