//  Copyright 2003 Narragansett Technologies.  All Rights Reserved.
//**Start Encode**

var changed    = false;
var HTMLFields = false;// Do we have HTML Fields on the Panel !
var saved      = false;
var firstList="";// Name of First List on a Page 
var homeSite="../";
//Global page name
var strPage = document.location.pathname.substring( document.location.pathname.lastIndexOf( "/" ) + 1, document.location.pathname.lastIndexOf( "." ) );
var fullStrPage = document.location.pathname;
var localUrl = window.location.href;
	localUrl = localUrl.substr(0,localUrl.indexOf(strPage));

if (strPage!="templateHome")//disable help on logon page
	document.onhelp = F1Help;

// see if changes have been saved
function saveDoc(){
if (!saved && changed){
	saved=true;
	document.forms[0].submit();
	//FIX FOR IE5
	if (event) event.returnValue=false;}
}
 
// RefreshList Contents
function refreshPage(){
document.forms[0].submit();

}

function Paging(aId,aDir){
ListPostBack(aId,'',aDir);
window.event.cancelBubble=true;
return false;
}
//List Post back function
function ListPostBack(aControl1,sortField, pageDirection){
	var aControl = aControl1.replace(/\t/,""); 
document.all(aControl + "_Sort").value = sortField;
document.all(aControl + "_Direction").value = pageDirection;
document.all(aControl + "_Active").value = "Y";
saved = true;
//if scrolling on list replace action to ensure that last action does not get executed again
document.forms[0].action = document.forms[0].action.replace("Request","xOequest");
document.forms[0].submit();
window.event.cancelBubble=true;
//FIX FOR IE5
	if (event) event.returnValue=false;

//alert(1);
}
 
function hideFilter(aControl){
if (document.all(aControl + "_Filter").className == "showPanel")
	document.all(aControl + "_Filter").className = "hidePanel";
else
	document.all(aControl + "_Filter").className = "showPanel";
}

// Set All HTMLFields with their Text
function setHTMLFields(){
HTMLFields=true;
for (var ix=0; ix<document.all.length; ix++){
	if (document.all(ix).HTMLField){
		document.all(document.all(ix).HTMLField).innerHTML = document.all(ix).value;
		}
	if (document.all(ix).HTMLDiv){
		document.all(ix).unselectable = "off";
		//document.all(ix).onkeypress = checkEnter;
		}
	//else
	//	document.all(ix).unselectable = "on";
	}
}

function checkEnter(){
var sel = null;
var keyCode = event.keyCode;
if (keyCode ==13){// Cancel enter if pressed
	activeHtmlBox.focus();
	event.keyCode="\r";
	//var sel = document.selection.createRange();
	//if (sel.pasteHTML){
	//	sel.pasteHTML("<br>\r");
	//	//event.cancelBubble=true;
	//	event.returnValue=false;
	//	}
	}
}

// Copy All HTMLFields with their Text
function copyHTMLFields(){
for (var ix=0; ix<document.all.length; ix++){
	if (document.all(ix).HTMLField){
		if (document.all(ix).textMode){
			if (document.all(ix).textMode == '1'){
				document.all(document.all(ix).HTMLField).innerHTML=document.all(document.all(ix).HTMLField).innerText;
				document.all(ix).textMode = '0';// Return To HTML
				}
			}
		document.all(ix).value = document.all(document.all(ix).HTMLField).innerHTML.replace('vid :','').replace(localUrl+"{","{").replace(localUrl+"/{","{");
		document.all(document.all(ix).name + "1").value = document.all(document.all(ix).HTMLField).innerText;
		}
	}
}

//function takes URL and submits to server
function reSubmit(url)
{	//Call client validation if it exista
	//alert(url);
	if (typeof(Page_ClientValidate) == 'function') 
	{
		var Ok =Page_ClientValidate();
		if	(Ok)
		{
			if (url.length >0)
				document.forms[0].action = url;
			if (HTMLFields)
				copyHTMLFields();// Copy HTML Fields to Hidden Fields
			document.forms[0].submit();
			
		}
	}
	else
	{
		if (url.length >0)
				document.forms[0].action = url;
			if (HTMLFields)
				copyHTMLFields();// Copy HTML Fields to Hidden Fields
			document.forms[0].submit();
	}
	//FIX FOR IE5
	if (event) event.returnValue=false;
	return false;
}
//function takes URL and goes to new location
function newLocation(url)
{	
		location.href =url;
		if (event) event.returnValue=false;
		return false;
}
//function takes URL and goes to new location
function newLocationWithMsg(url,msg)
{	
	if (msg.length>0)
	{
		if (!confirm(msg))
			return;
	}
	newLocation(url);
}
//function takes URL and submits to server
function reSubmitWithMsg(url,msg)
{	
	if (msg.length>0)
	{
		if (!confirm(msg))
			return;
	}
	reSubmit(url);
}
//open page in windowd
function reSubmitWithMsgOpenWindow(url,msg,iWidth,iHeight,aPageID)
{
	var sWindowInfo="";
	sWindowInfo = "resizable=yes,width=" +iWidth+ ",height=" + iHeight + ",top=" + (window.screenTop+10) + ",left=" + (window.screenLeft+10)  + ",toolbar=no, status=yes, scrollbars=yes"
	if (msg.length>0)
		{
			if (!confirm(msg))
				return;
		}
		var aWin = window.open(url, aPageID, sWindowInfo);
		aWin.focus();
		return aWin;
}


//List Post back function Called for Filters to find if Enter is Pressed
function filterEnterKey(aControl){
if (window.event.keyCode == 13) {// Enter Key
	window.event.cancelBubble=true;
	window.event.keyCode = 0;
	ListPostBack(aControl,'','FILTER')
	}
}

//Delete a Specific Item
function deleteItem(url,aItem)
{	

window.event.cancelBubble=true;
if (window.confirm("Delete(" + aItem + ") ?")){
	document.forms[0].action = url;
	document.forms[0].submit();
	//FIX FOR IE5
	if (event) event.returnValue=false;
	}
}

//Include or Exclude a Specific Item
function ItemClick(url)
{	
	document.forms[0].action = url;
	saved = true;
	document.forms[0].submit();
	//FIX FOR IE5
	if (event) event.returnValue=false;
	
}

// Highlight a Row Color
function rowHighlight(aObj){
aObj.style.backgroundColor ='#f0f0f0';
}

// Revert a Row Color
function rowNormal(aObj){
aObj.style.backgroundColor ='';
}

function promptValue(aControl,aValue){
document.all(aControl).value = aValue;
if (document.all(aControl).onchange)
	document.all(aControl).onchange();
}

function helpAbout(){
window.open('../help/about.aspx','About','width=400, height=300, toolbar=no, status=no, scrollbars=no, resizeable=no');
}

function Print(ReportId){
window.open('../System/Print.aspx?ReportId' + ReportId ,'Print','width=300, height=243, toolbar=no, status=no, scrollbars=no, resizeable=no');
}

function F1Help(aControl){
	window.event.cancelBubble=true;
	window.event.keyCode = 0;
	openHelp();
	return false;
}

function openHelp() {
	//var strPage = document.location.pathname.substring( document.location.pathname.lastIndexOf( "/" ) + 1, document.location.pathname.lastIndexOf( "." ) );
	var fullStrPage1='';
	try
	{
	fullStrPage1 = fullStrPage.substring(1 , fullStrPage.lastIndexOf( "/" ) );
	fullStrPage1 = fullStrPage1.substring(fullStrPage1.lastIndexOf("/") + 1 , fullStrPage1.length );
	//alert(fullStrPage);
	}
	catch (e){fullStrPage1='';}
	window.open('../help/RunHelp.aspx?PageName='+strPage+'&SDir='+fullStrPage1,'Help','width=500, height=550, toolbar=no, status=no, scrollbars=yes, resizeable=yes')
}

function openHelpIndex() {
	window.open('../help/RunHelp.aspx?PageName=index','Help','width=500, height=550, toolbar=no, status=no, scrollbars=yes, resizeable=yes')
}

function getPageName() {
	var strPage = document.location.pathname.substring( document.location.pathname.lastIndexOf( "/" ) + 1, document.location.pathname.lastIndexOf( "." ) );
	return strPage;
 }

function openWindow(aPage,aPageID,iWidth,iHeight) {
	var sWindowInfo="";
	sWindowInfo = "resizable=yes,width=" +iWidth+ ",height=" + iHeight + ",top=" + (window.screenTop+10) + ",left=" + (window.screenLeft+10)  + ",toolbar=no, status=yes, scrollbars=yes"
	var aWin = window.open(aPage, aPageID, sWindowInfo);
	aWin.focus();
	return aWin;
}

function evalMessage() {
	alert('This software has a time-limited license key. Please contact your software vendor for a permanent key.')
}


// =========================== Numeric Helpers =======================================================
function formatMoney(aVal1,decSep,groupSep){
var aVal = aVal1.toString().replace(groupSep,'');
var aVal = aVal.replace(decSep,'.');
var res = "";
if (!isNaN(aVal) && aVal.length > 0){
	var aNum = Math.round(aVal*100)/100;
	var intPart = parseInt(aNum);
	var dec = parseInt(Math.round((aNum-intPart)*100));
	var intStr = intPart.toString();
	var iy=0;
	var aComma = "";
	for (var ix=1;ix<=intStr.length;ix++){
		if (iy==3){iy=0;aComma= groupSep;} else {iy++;aComma=""}
		res =  intStr.substr(intStr.length-ix,1) + aComma + res;
		}
	if (dec.toString().length == 1)
	   res = res + decSep + "0" + dec.toString();
	else
	   res = res + decSep + dec.toString();
	}
else
	res = aVal1;
return res;
}

function formatInt(aVal1,decSep,groupSep){
var aVal = aVal1.toString().replace(groupSep,'');
var aVal = aVal.replace(decSep,'.');
var res = "";
if (!isNaN(aVal) && aVal.length > 0){
	var aNum = Math.round(aVal*100)/100;
	var intPart = parseInt(aNum);
	var intStr = intPart.toString();
	var iy=0;
	var aComma = "";
	for (var ix=1;ix<=intStr.length;ix++){
		// if (iy==3){iy=0;aComma= groupSep;} else {iy++;aComma=""}
		res =  intStr.substr(intStr.length-ix,1) + aComma + res;
		}
	}
else
	res = aVal1;
return res;
}
// ========================================================================================================


// ============================= Calendar Routines ============================================

function calendarClick(aField,aFormat) {
	var dateParts;
	var fmt;
	var found = false;
	var aDate = new Date();
	var aMonth = aDate.getMonth()+1,aYear=aDate.getFullYear(),aDay = aDate.getDate();//Default Values
	var aDateStr = "";
	
document.all('cal').currElement=document.all(aField);
if ((event.clientY+10 + 240) < document.body.clientHeight) {
   document.all('cal').style.top=event.clientY+10;
   }
else {
   document.all('cal').style.top=document.body.clientHeight - 240;
   }
document.all('cal').style.position='absolute';
if ((event.clientX+10 + 220) < document.body.clientWidth) {
   document.all('cal').style.left=event.clientX;
   }
else {
   document.all('cal').style.left=document.body.clientWidth-225;
   }


var aFormat1 = aFormat.toLowerCase();
var aValue = document.all(aField).value;

if (aValue.length > 0){
	if (aFormat.search("/") != -1) {dateParts = aValue.split("/");fmt = aFormat1.split("/");found=true;};
	if (aFormat.search("-") != -1) {dateParts = aValue.split("-");fmt = aFormat1.split("-");found=true;};
	if (aFormat.search(":") != -1) {dateParts = aValue.split(":");fmt = aFormat1.split(":");found=true;};
	if (aFormat.search(/\\/) != -1) {dateParts = aValue.split("\\");fmt = aFormat1.split("\\");found=true;}; 
	if (aFormat.search("\ ") != -1) {dateParts = aValue.split("\ ");fmt = aFormat1.split("\ ");found=true;};
	if (aFormat.search("[\.]") != -1) {dateParts = aValue.split("\.");fmt = aFormat1.split("\.");found=true;}; 
	if (found)
	for (var  ix=0;ix < fmt.length && ix < dateParts.length ;ix++){
		if (!isNaN(dateParts[ix]))
		switch (fmt[ix]){
			case "m": case "mm":
				aMonth = parseInt(dateParts[ix],10);
				break;
			case "yyyy": case "yy":
				aYear = parseInt(dateParts[ix],10);
				break;
			case "d": case "dd":
				aDay = parseInt(dateParts[ix],10);
				break;
			default:
			break;
			}
		}
	//var aDate = new Date(document.all(aField).value);
	}
aDateStr =  '' + aYear.toString() + '/' + aMonth.toString() + '/' + aDay.toString();
document.all('cal').aFormat= aFormat1;
document.all('cal').value=aDateStr;
document.all('cal').style.display='';
document.all('cal').children(0).focus();
try
{
if (hideCalFields())
	hideCalFields();
}
catch (e) {}
}

// ============================================================================================
