/*

	Please keep the following lines visible, in recognition of my work...

	************************	
	Author: Max Holman <max@blueroo.net>
	Date  : Sun, 21 Jan 2001
	************************

	This functions lets users type in letters to select an option in your SELECT form fields.

	Usually the browser only takes notice of single keystrokes and switches to the first Option that
	begins with that letter.

	This scripts buffers the users input and compares it against the OPTIONs in the SELECT field, 
	choosing the closest match as you type


	Cut and Paste this text into your HTML file or into a separate .js file for inclusion on your site.

	Usage:	<SELECT onKeyPress = "return shiftHighlight(event.keyCode, this);">	

	Platform: Only tested on IE5 (Win) - will not work on Netscape


*/

var timerid     = null;
var matchString = "";
var mseconds    = 1500;	// Length of time before search string is reset

var DialogWindow = null;

function shiftHighlight(keyCode,targ)
{
	keyVal      = String.fromCharCode(keyCode); // Convert ASCII Code to a string
	matchString = matchString + keyVal; // Add to previously typed characters

	elementCnt  = targ.length - 1;	// Calculate length of array -1

	for (i = elementCnt; i > 0; i--)
	{
		selectText = targ.options[i].text.toLowerCase(); // convert text in SELECT to lower case
		if (selectText.substr(0,matchString.length) == 	matchString.toLowerCase())
		{
			targ.options[i].selected = true; // Make the relevant OPTION selected
		}
	}
	clearTimeout(timerid); // Clear the timeout
	timerid = setTimeout('matchString = ""',mseconds); // Set a new timeout to reset the key press string
	
	return false; // to prevent IE from doing its own highlight switching
}

function FormFocusFirst(oForm, szName)
{
	if (oForm)
	{
		var oElement=null;
		
		if (szName!=null && szName!="")
		{
			oElement = oForm.elements[szName];
		}
		else
		{
			for (i = 0; i < oForm.elements.length; i++)
			{
				oElement = oForm.elements[i] ;
				if (!oElement.isDisabled) //ajs 12/9/02
				{
					if (oElement.type == 'text' || oElement.type == 'textarea' || oElement.type == 'select-one' || oElement.type == 'checkbox' || oElement.type == 'select-multiple' || oElement.type == 'radio')
					{
						break;
					}
				}
			}
		}
		if (oElement!=null && oElement.type != 'hidden' && !oElement.isDisabled ) // ajs 12/9/02
		{
			oElement.focus();
		}
	}
}

function TabStrip_Next(oTS)
{
	if (oTS.selectedIndex < oTS.numTabs)
		oTS.selectedIndex++;
}

function TabStrip_Previous(oTS)
{
	if (oTS.selectedIndex > 0)
		oTS.selectedIndex--;
}


function expandCollapseDiv(divId, imageId, szExpand, szCollapse, szExpandAlt, szCollapseAlt)
{
	var div,img,oMultiChildAttributes
	
	if (document.all)
	{
		div = document.all[divId];
		img = document.all[imageId];
		oMultiChildAttributes = document.all["MultiChildAttributes"];
	}
	else if (document.getElementById)
	{
		div = document.getElementById(divId);
		img = document.getElementById(imgId);
		oMultiChildAttributes = document.getElementById("MultiChildAttributes");
	}
		
	if (div != null && img != null)
	{
		if (div.style.display == "none") 
		{
			div.style.display = "inline";
			img.src = szCollapse;
			if (szCollapseAlt!=null)
			{img.alt = szCollapseAlt;}
		}
		else
		{
			div.style.display = "none";		
			img.src = szExpand;	
			if (szExpandAlt!=null)
			{img.alt = szExpandAlt;}
		}
		if (oMultiChildAttributes != null)
		{
			szAttribute = "["+divId+"]";
			szAttributes = oMultiChildAttributes.value;
			if (div.style.display == "inline")
			{
				if (szAttributes.indexOf(szAttribute) < 0)
					szAttributes += szAttribute;
			}
			else
			{
				if (szAttributes.indexOf(szAttribute) >= 0)
					szAttributes = szAttributes.replace(szAttribute,"");
			}
			oMultiChildAttributes.value = szAttributes;
		}
	}
}
function KeyPressMasked(szMaskType)
{
// Supported Mask Types:
//	Currency
//	Date
//	Phone
//	Integer
//	Float
//	Alpha
	if (szMaskType != "")
	{
		szChar = String.fromCharCode(window.event.keyCode)
		szMaskType = szMaskType.toLowerCase();
		if (szMaskType == "currency")
		{
			if ((szChar < "0" || szChar > "9") && "$,.-".indexOf(szChar) < 0)
				window.event.keyCode = 0;
		}
		else if (szMaskType == "date")
		{
			if ((szChar < "0" || szChar > "9") && szChar != "/")
				window.event.keyCode = 0;
		}
		else if (szMaskType == "phone")
		{
			if ((szChar < "0" || szChar > "9") && "()-/".indexOf(szChar) < 0)
				window.event.keyCode = 0;
		}
		else if (szMaskType == "integer")
		{
			if ((szChar < "0" || szChar > "9") && szChar != "-")
				window.event.keyCode = 0;
		}
		else if (szMaskType == "float")
		{
			if ((szChar < "0" || szChar > "9") && ".-".indexOf(szChar) < 0)
				window.event.keyCode = 0;
		}
		else if (szMaskType == "alpha")
		{
			if ((szChar < "a" || szChar > "z") && (szChar < "A" || szChar > "Z"))
				window.event.keyCode = 0;
		}
	}
}

function ListGridRowClicked()
{
	szHref = event.srcRow.children[0].children[0].href;
	szHref = szHref.substr(szHref.indexOf("'")+1);
	szHref = szHref.substr(0,szHref.indexOf("'"));
	__doPostBack(szHref,'');
}

function ConfirmDelete()
{
	if (!confirm("Are you sure you want to delete?"))
	{
		event.returnValue = false;
		return false;
	}
	return true;
}

function Confirm(msg)
{
	if (!confirm(msg))
	{
		event.returnValue = false;
		return false;
	}
	return true;
}

function DeleteChildRow(szFormKey,szRowKey)
{
	if (ConfirmDelete())
	{
		if (document.forms[0].MultiChildAttributes != null)
		{
			document.forms[0].MultiChildAttributes.value += "DCKey["+szFormKey+"]DCRKey["+szRowKey+"]";
			document.forms[0].submit();
		}
		/*
		szURL = window.document.URL;
		if (szURL.indexOf("&DCKey=") > 0)
			szURL = szURL.substr(0,szURL.indexOf("&DCKey="));
		szURL += "&DCKey="+szFormKey+"&DCRKey="+szRowKey;
		window.document.URL = szURL;
		*/
	}
}

function ParentWindowRefresh(szURL)
{
	if (opener)
	{
		if (window.opener.__doPostBack)
			window.opener.__doPostBack("","");
		else if (opener.document.forms[0])
			opener.document.forms[0].submit();
		else if (szURL != "")
			opener.location.assign(szURL);
		else
			opener.location.reload(false);
	}
}

function ImageSrcChange(szImageSrc)
{
	var oImage = null;
	if (event.srcElement.children[0] != null && event.srcElement[0].tagName == "IMG")
		oImage = event.srcElement[0];
	else if (event.srcElement.parentElement != null && event.srcElement.parentElement.children[0] != null && event.srcElement.parentElement.children[0].tagName == "IMG")
		oImage = event.srcElement.parentElement.children[0];
	if (oImage != null)
	{
		oImage.src = szImageSrc;	
	}
}

function TextLimit(field, maxlen)
{
	if (field.value.length > maxlen) 
	{
		field.value = field.value.substring(0, maxlen);
		alert('your input has been truncated!');
	}
}





function checkField (objField, cInputMask) 
{
	InputMaskDelimiters = "/"
	bDelimiter = false;
	if( objField.value != "")
	{
		for (var i=0; i<objField.value.length; i++)
		{
			cChar = objField.value.charAt(i)
			if (InputMaskDelimiters.indexOf(cChar)>-1)
			{
				bDelimiter = true;
				break;
			}
		}

		if ( !bDelimiter )
		{
			objField.value = reformatInputMask(objField.value, cInputMask, InputMaskDelimiters)
		}
	}
	return true;
}

function reformat (s)
{   
	var arg;
    var sPos = 0;
    var resultString = "";
	var nStringLength = s.length
    for (var i = 1; i < reformat.arguments.length ; i++) {
		if (sPos <= nStringLength)
		{
			arg = reformat.arguments[i];
			if (i % 2 == 1) resultString += arg;
			else
			{
				resultString += s.substring(sPos, sPos + arg);
				sPos += arg;
			}
		}
    }

    return resultString;
}

function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}




function reformatInputMask (cValue,cInputMask, InputMaskDelimiters)
{   
	creformatFunction = "reformat (cValue, "
	nChar = 0
	for (i=0;i<cInputMask.length;i++)
	{
		if (InputMaskDelimiters.indexOf(cInputMask.charAt(i))==-1)
		{
			nChar=nChar+1
			if (i==0) //first character
				{creformatFunction = creformatFunction + "'', "}
			else
				{
				if (nConcatMask)
					{creformatFunction = creformatFunction + "', "}
				}				
			nConcatMask = false
		}	
		else
		{
			if (i==0)
				{creformatFunction = creformatFunction + "'"+cInputMask.charAt(i)}
			else
			{
				if (nChar==0)
				{
					if (nConcatMask)
						{creformatFunction = creformatFunction + cInputMask.charAt(i)}
					else
						{creformatFunction = creformatFunction + "'"+cInputMask.charAt(i)}
				}
				else
				{
					if (nConcatMask)
						{creformatFunction = creformatFunction + cInputMask.charAt(i)}
					else
						{creformatFunction = creformatFunction + " "+nChar+", '"+cInputMask.charAt(i)}
				}
			}
			nConcatMask = true
			nChar = 0
		}
	}
	if (nChar > 0)
	{
		creformatFunction = creformatFunction + " "+nChar+", "
	}
	//remove last two characters ", " and add close parenthesis at the end.
	creformatFunction = creformatFunction.substring(0,creformatFunction.length-2)
	creformatFunction = creformatFunction + ")"
	//alert(creformatFunction)
return ( eval(creformatFunction) )
}



function CheckCreditDebit()
{
	oSrcElement = event.srcElement;
 iIndex = oSrcElement.name.indexOf('JDG');
   iIndex = oSrcElement.name.indexOf('_', iIndex+1); 
 iIndex = oSrcElement.name.indexOf('_', iIndex+1); 
 oRowID = oSrcElement.name.substr(iIndex+1,1); 
if ( oSrcElement.name.indexOf('JournalDataGrid') >= 0 &&
	oSrcElement.type == 'text' &&
	(oSrcElement.name.indexOf('Debit') >= 0 || oSrcElement.name.indexOf('Credit') >= 0) &&
	oSrcElement.value > 0 )
	{
	if (oSrcElement.name.indexOf('Debit') >= 0) Opposite = 'Credit';
	if (oSrcElement.name.indexOf('Credit') >= 0) Opposite = 'Debit';
	for (i = 0; i < document.forms[0].elements.length; i++)
	{
		oElement = document.forms[0].elements[i];
		if (oElement.type == 'text' &&
			oElement.name.indexOf('JournalDataGrid') >= 0 && 
			oElement.name.indexOf('JDG_'+Opposite+'_' + oRowID) >= 0 )
{    
		document.forms[0].elements[i].disabled = true;
		if (Opposite == 'Credit')
		document.forms[0].elements[i+1].focus();
		if (Opposite == 'Debit')
		document.forms[0].elements[i+2].focus();
		break;
	}
	}
	}
}

function SetCookie(szName, szValue)
{
	document.cookie = szName + "=" + escape(szValue) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
	if (0==1)
	{
	}
	var szDummy = GetCookie(szName);
	if (0==1)
	{
	}
}

function GetCookie(szName)
{
	// cookies are separated by semicolons
	var szReturnValue = null;
	var aCookies = document.cookie.split("; ");
	for (var i=0; i < aCookies.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCookie = aCookies[i].split("=");
		if (szName == aCookie[0]) 
			szReturnValue = unescape(aCookie[1]);
	}
	return szReturnValue;
}

function DelCookie(szName)
{
  date = new Date();
  document.cookie = szName + "=" + "; expires=" + date.toGMTString();
}

function OpenNewWindow(szURL)
{
	//window.onfocus = "reload();";
	//openDialog(szURL,screen.width/2,screen.height/2);
	//debugger
	openDialog(szURL,50,50);
}

function CloseWindow()
{
	close();
}

function openDialog(url, width, height)
{
	DialogWindow = null;
	DialogWindow = new Object();
	if (!DialogWindow.win || (DialogWindow.win && DialogWindow.win.closed)) {
		//DialogWindow.returnFunc = returnFunc;
		//DialogWindow.returnedValue = "";
		//DialogWindow.args = args;
		DialogWindow.url = url;
		DialogWindow.width = width;
		DialogWindow.height = height;
		DialogWindow.name = (new Date()).getSeconds().toString();
		DialogWindow.left = 5000; //(screen.width - width) / 2;
		DialogWindow.top = 5000; // (screen.height - height) / 2;
		var attr = "left=" + DialogWindow.left + ",top=" + 
			DialogWindow.top + ",resizable=yes,status=yes,scrollbars=yes,width=" 
			+ DialogWindow.width + ",height=" + DialogWindow.height;
		DialogWindow.win = window.open(DialogWindow.url, DialogWindow.name, attr);
		var nLeftOffset = DialogWindow.left-DialogWindow.win.screenLeft;
		var nTopOffset = DialogWindow.top-DialogWindow.win.screenTop;
		SetCookie("WindowTopLeftOffset",nTopOffset+","+nLeftOffset);
	}
	DialogWindow.win.focus()
}

function openDialogAbsolute(url, width, height)
{
	DialogWindow = null;
	DialogWindow = new Object();
	if (!DialogWindow.win || (DialogWindow.win && DialogWindow.win.closed)) {
		DialogWindow.url = url;
		DialogWindow.width = width;
		DialogWindow.height = height;
		DialogWindow.name = (new Date()).getSeconds().toString();
		DialogWindow.left = (screen.width - width) / 2;
		DialogWindow.top = (screen.height - height) / 2;
		var attr = "left=" + DialogWindow.left + ",top=" + 
			DialogWindow.top + ",resizable=yes,status=yes,scrollbars=yes,width=" 
			+ DialogWindow.width + ",height=" + DialogWindow.height;
		DialogWindow.win = window.open(DialogWindow.url, DialogWindow.name, attr);
	}
	DialogWindow.win.focus()
}

function WindowBlockEvents()
{
	window.onfocus = WindowCheckModal
}

function WindowCheckModal()
{	
	if (DialogWindow != null)
	{
		if (DialogWindow.win && !DialogWindow.win.closed)
		{
			DialogWindow.win.focus()	
		}
	}
}

function DialogBlockParent()
{
	if (opener) 
	{
		opener.WindowBlockEvents();
	}
}

function SetWindowSize(bRemember)
{

	//debugger
	
	if (! bRemember || ! ResizeByCookie())
	{
		/*
		var nOffsetWidth = screen.width / 2;
		var nOffsetHeight = screen.height / 2;
		var oDesignedDiv = document.all["DesignedDiv"];
		if (document.forms[0] != null && document.forms[0].id == "WizardForm")
		{
			nOffsetWidth = screen.width-200;
			nOffsetHeight = screen.height-200;
		}
		else if (oDesignedDiv != null)
		{
			nOffsetWidth = oDesignedDiv.offsetWidth+50;
			nOffsetHeight = oDesignedDiv.offsetHeight+150;
		}
		else
		{
			var oDataFormTable = document.all["DataFormTable"];
			if (oDataFormTable != null)
			{
				nOffsetWidth = oDataFormTable.offsetWidth;
				nOffsetHeight = oDataFormTable.offsetHeight+75; // 75 to account for buttons..
			}
		}
		if (nOffsetWidth > screen.width)
			nOffsetWidth = screen.width;
		if (nOffsetHeight > screen.height)
			nOffsetHeight = screen.height;
		window.resizeTo(nOffsetWidth,nOffsetHeight);
		window.moveTo(((screen.width-nOffsetWidth) / 2),((screen.height-nOffsetHeight) / 2));
		window.scrollTo(0,0);
		*/
		w = (document.layers)?document.width:document.body.scrollWidth;
		h = (document.layers)?document.height:document.body.scrollHeight;
		w += 40;
		h += 60;
		if (w > screen.width)
		w = screen.width;
		if (h > screen.height)
		h = screen.height;
		window.resizeTo(w,h);
		window.moveTo(((screen.availWidth-w) / 2),((screen.availHeight-h) / 2));
		window.scrollTo(0,0);
	}
}

function ResizeByCookie()
{
	var bResized = false;
	var szCookie = GetCookieName(document.location.search);
	
	if (szCookie == "")
		szCookie = GetFileName(document.location.pathname);
	
	if (szCookie != "")
	{
		var szCoordinates = GetCookie(szCookie);
		if (szCoordinates != null)
		{
			var nTopOffset = 0;
			var nLeftOffset = 0;
			var szOffsetCookie = GetCookie("WindowTopLeftOffset");
			if (szOffsetCookie != null)
			{
				var aOffsets = szOffsetCookie.split(",");
				nTopOffset = parseInt(aOffsets[0]);
				nLeftOffset = parseInt(aOffsets[1]);
			}
			var aCoordinates = szCoordinates.split(",");
			var nTop = parseInt(aCoordinates[0])+nTopOffset;
			var nLeft = parseInt(aCoordinates[1])+nLeftOffset;
			var nHeight = parseInt(aCoordinates[2])-(nTopOffset*2);
			var nWidth = parseInt(aCoordinates[3])-(nLeftOffset*2);
			window.resizeTo(nWidth,nHeight);
			window.moveTo(nLeft,nTop);
			window.scrollTo(0,0);
			bResized = true;
		}
	}
	return bResized;
}

function GetCookieName(szQueryString)
{
	var szCookie = "";
	if (szQueryString.indexOf("WizardKey=") >= 0)
		szCookie = szQueryString.substr(szQueryString.indexOf("WizardKey=")+10);
	else if (szQueryString.indexOf("FormKey") >= 0)
		szCookie = szQueryString.substr(szQueryString.indexOf("FormKey=")+8);
	if (szCookie != "")
	{
		if (szCookie.indexOf("&") >= 0)
			szCookie = szCookie.substring(0,szCookie.indexOf("&"));
	}
	return szCookie;
}

function GetFileName(szQueryString)
{
	var szCookie = "";
	if (szQueryString.indexOf(".") >= 0)
		szCookie = szQueryString.substr(0,szQueryString.indexOf("."));
	while (szCookie.indexOf("/") >= 0)
		szCookie = szCookie.substr(szCookie.indexOf("/")+1);
	if (szCookie != "")
		szCookie = "FORM_"+szCookie;
	return szCookie;
}

function BeforeUnloadWindow()
{
	//debugger
	var szCookie = GetCookieName(document.location.search);
	if (szCookie == "")
		szCookie = GetFileName(document.location.pathname);
	if (szCookie != "")
	{
		var szCookieValue = window.screenTop+","+window.screenLeft+","+document.body.offsetHeight+","+document.body.offsetWidth;
		SetCookie(szCookie,szCookieValue);
	}
}

function ConfirmDialog(szMessage, szButtonID)
{
	if (window.confirm(szMessage))
	{
		if (document.forms[0].action)
			document.forms[0].action += "&Confirm=ByPass";

		if (szButtonID==null || szButtonID=='')
			szButtonID='ButtonSave';
		oButton = eval('document.forms[0].'+szButtonID);
		if (oButton)
			oButton.click();
	}
}

function ClearConfirmByPass()
{
	if (document.forms[0].action && document.forms[0].action.indexOf("&Confirm=ByPass") > 0)
		document.forms[0].action = document.forms[0].action.replace("&Confirm=ByPass","");
}

function AlertDialog(szMessage)
{
	window.alert(szMessage);
}

// H. Stechl - add options to dropdown from opener
function DropDownAddOption(oDropDown, szValue, szText)
{
	oDropDown.options[oDropDown.length] = new Option((szText==null)?szValue:szText, szValue);
}
