/*
Webstensible - THE Extensible Application Layer Development Toolset - http://www.webstensible.com
Copyright (c) 2005 - SDC Solutions, Inc. ( http://www.sdcsol.com ) - All rights reserved.
Support: support@webstensible.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions 
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
DEALINGS IN THE SOFTWARE.
*/

function newLocation(sNewURL, sTarget, iNewWindow, sScrollbars, iWidth, iHeight, sWindowPosition, sStatusBar) {
	var iWinPos = 0;

	//Replace special characters used to prevent HTML from blowing up
	sNewURL = sNewURL.replace("`", "'");	//restore single quotes
	//Allow variables to be optional by ensuring they are always initialized
	sWindowPosition += "";
	sStatusBar += "";
	//Determine if a new frame or window is needed or not
	if (sTarget.length == 0) {
		//Same frame/window, just show new page
		document.location.href = sNewURL;
		}
	else {
		if (iNewWindow == 0) {
			//Just send to new frame in current window (will create a new window if frame doesn't exist)
			//NOTE: not handling a different frame/window as the target yet
			window.document.location.href = sNewURL;
			}
		else {
			//Create, or display page in, new window using specified parameters
			if (sScrollbars.length > 0) { sFeatures = "scrollbars=" + sScrollbars.toLowerCase(); }

			//Set up height and width
			if (iHeight > screen.availHeight) {iHeight = screen.availHeight;}
			if (iWidth > screen.availWidth) {iWidth = screen.availWidth;}
			sFeatures += ", width=" + iWidth;
			sFeatures += ", height=" + iHeight;
			//if (iWidth > 99) { sFeatures += ", width=" + iWidth; }
			//if (iHeight > 99) { sFeatures += ", height=" + iHeight; }
			
			//Set up status bar display
			if (sStatusBar.length > 0) { 
				sFeatures += ", status=" + sStatusBar.toLowerCase(); 
				}
			else {
				sFeatures += ", status=yes";
			} 
			
			//Set up window positioning
			//Determine top and left relative to screen for window positioning
			if (iHeight == 0 && iWidth == 0)	//No height and width means _RESIZE will provide info
			{
				sFeatures += ", top=0, left=" + (screen.availWidth + 1);
			}
			else
			{
				//Convert text values to numeric for use with switch statement
				sWindowPosition = sWindowPosition.toLowerCase();
				if (sWindowPosition == "topleft") { iWinPos = 1; }
				if (sWindowPosition == "topright") { iWinPos = 2; }
				if (sWindowPosition == "bottomleft") { iWinPos = 3; }
				if (sWindowPosition == "bottomright") { iWinPos = 4; }
				switch ( iWinPos )
				{
					case 1:
						sFeatures += ", top=0, left=0";
						break;
					case 2:
						sFeatures += ", top=0, left=" + (screen.availWidth - iWidth);
						break;
					case 3:
						sFeatures += ", top=" + (screen.availHeight - iHeight) + ", left=0";
						break;
					case 4:
						sFeatures += ", top=" + (screen.availHeight - iHeight) + ", left=" + (screen.availWidth - iWidth);
						break;
					default:	//center by default (iWinPos = 0)
						sFeatures += ", top=" + ((screen.availHeight - iHeight)/2) + ", left=" + ((screen.availWidth - iWidth)/2);
						break;
				}
			}
			
			//Create the new window with the specified features
			sFeatures += ", resizable=yes, toolbar=no, location=no, directories=no, copyhistory=no";
			objWindow = window.open(sNewURL, sTarget, sFeatures);
			
			//make sure the new window has the focus
			objWindow.focus();
		}
	
	}
}

function resizeWindow(iWidth, iHeight, sWindowPosition, iRepositionOnly)
{
	var iTop = 0, iLeft = 0, iWinPos = 0;
	
	//Allow sWindowPosition and iRepositionOnly to be optional by ensuring they are always initialized
	sWindowPosition += "";
	iRepositionOnly = iRepositionOnly + 0;
	if (isNaN(iRepositionOnly)) {iRepositionOnly = 0;}
	//Resize the window if applicable
	if (iRepositionOnly == 0)
	{
		//Resize the window if height and width values provided
		//Make sure the window fits on the screen
		if (iHeight > screen.availHeight) {iHeight = screen.availHeight;}
		if (iWidth > screen.availWidth) {iWidth = screen.availWidth;}
		//Handle being in a frameset
		window.top.resizeTo(iWidth, iHeight);
	}
	
	//Reposition the window; Center by default
	//Determine top and left relative to screen for window positioning
	//Convert text values to numeric for use with switch statement
	if (sWindowPosition != "")
	{
		sWindowPosition = sWindowPosition.toLowerCase();
	}
	if (sWindowPosition == "topleft") { iWinPos = 1; }
	if (sWindowPosition == "topright") { iWinPos = 2; }
	if (sWindowPosition == "bottomleft") { iWinPos = 3; }
	if (sWindowPosition == "bottomright") { iWinPos = 4; }
	switch ( iWinPos )
	{
		case 1:
			//Top and left are already set
			break;
		case 2:
			iLeft = screen.availWidth - iWidth;
			break;
		case 3:
			iTop = screen.availHeight - iHeight;
			break;
		case 4:
			iTop = screen.availHeight - iHeight;
			iLeft = screen.availWidth - iWidth;
			break;
		default:	//center by default (iWinPos = 0)
			iTop = (screen.availHeight - iHeight) / 2;
			iLeft = (screen.availWidth - iWidth) / 2;
			break;
	}
	//alert(iWinPos + "|LEFT" + iLeft + "|TOP" + iTop)
	//alert("HEIGHT" + screen.availHeight + "|WIDTH" + screen.availWidth)
	window.moveTo(iLeft, iTop);
	//window.focus();
	return true;
}
	
function getWindow(locationURL,winName,x,y) {
  locationURL = locationURL;
  features='menubar=no,scrollbars=yes,resizable=yes,width=' + x + ',height=' + y;
  window.open(locationURL,winName,features);
}

function closeWindow() {
	window.event.cancelBubble = true;
	window.top.close();
	if (window.top.opener && !window.top.opener.closed){window.top.opener.focus();}
}

/*
function confirmLink(sMessage, sURL){
	if(eval(confirm(sMessage))){
		location.href = sURL;
	}
}
*/

/*
function updatePickListFields(sFieldList, sFieldValues) {
	var vNameArray, vValueArray;
	var iIndex;
	
	//Replace special characters used to prevent HTML from blowing up
	sFieldValues = sFieldValues.replace("`", "'");	//restore single quotes

	vNameArray = sFieldList.split("^");
	vValueArray = sFieldValues.split("^");
	//Loop through the field names provided and update the parent screen with the corresponding values
	for(iIndex = 0; iIndex < vNameArray.length; iIndex++)
		if (vNameArray[iIndex].length != 0) {
			window.opener.document[0].elements[vNameArray[iIndex]].value = vValueArray[iIndex];
			}
}
*/

function swapText(sFieldName, sNewText) {
//	elem = document.getElementsByName(sFieldName).item(0)
	elem = getObj(sFieldName)
	if (elem){
		elem.obj.innerText = sNewText
	}
}

function returnObj(name)
{
  var tmp;
  if (document.getElementById)
  {
  	tmp = document.getElementById(name);
  }
  else if (document.all)
  {
	tmp = document.all[name];
  }
  else if (document.layers)
  {
	tmp = getObjNN4(document,name);
  }
  
  return tmp;
}


function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
	this.obj = getObjNN4(document,name);
	this.style = this.obj;
  }
}

function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
//	for (var i=0; i&lt; x.length;i++)
	for (var i=0; x.length; i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function rowColorChange(row,color) {
	var overRow = row.substr(3,2);
	document.all[row].bgColor = color;
}

function calendarPicker(calendarPage, windowFeatures)
{
	window.open(calendarPage, 'calendarPopup', windowFeatures);
}
