// JavaScript Document
// tw
util={};
util.popup=function(params)
{
	var w = window.open(params.uri, Math.round(Math.random()*100), 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width='+params.width+',height='+params.height);
	if(w){ try {w.focus();}catch(excpt){} } //make window show, err control for ie.
};
function setDebugTxt(txt)
{
if(g_EBI('debugBox')) g_EBI('debugBox').innerHTML = txt;
}


function disableEnter(event)
{
	var e = (typeof event != "undefined") ? event : window.event;
	
	if(e && e.keyCode==13)
	{
		e.returnValue = false;
		e.cancelBubble = true;
	}
}


function g_EBI(id)
{ //element by id
	var el = document.getElementById(id)
	if(el)
		return el;
	else
		return;
}
function el_getValue(id)
{ //get element value
	var el = g_EBI(id);
	if(el)
		return el.value;
	else
		return "";
}
function el_setValue(id, val)
{ //set element value
	var el = g_EBI(id);
	if(el)
		el.value = val;
}
function el_setDisplay(id, way)
{//set way: 1 - display=''; 0 - 'none'
	var el = g_EBI(id);
	if(el)
	{
		if(el.style)
		{
			if(way==0)
				el.style.display='none';
			else
				el.style.display='';
		}
	}
}
function el_setInnerHTML(id, ht)
{ //set innerhtml for value, if we can
	var el = g_EBI(id);
	if(el && el.innerHTML)
		el.innerHTML = ht;
}

function el_focus(id)
{ //focus the element
	var el = g_EBI(id);
	if(el)
		el.focus();
	else
		return;
}


//find the argument in the script tag and return, used to modularize the js files and pass items as arguments.
//usage: var blah = getJSArgVal("doc.js", "argument");
function getJSArgVal(docID, argID)
{
	var sEls=document.getElementsByTagName("script");
	var docStr = "/"+docID;
	var matchStr = "[?&]"+argID+"=[0-9A-Za-z]*";
	var matchStrIndex=0, matchStrLen=0;
	var retStr = "";
	for(i=0;i<sEls.length;i++)
	{
		if(sEls[i].src.match(docStr)) //only use from correct file.
			if(sEls[i].src.match(matchStr))
			{
				matchStrIndex=sEls[i].src.indexOf(sEls[i].src.match(matchStr));
				matchStrLen=sEls[i].src.match(matchStr).length;
				retStr = sEls[i].src.substring(matchStrIndex+argID.length+2); //value truncs out the [?&]varname= expression to just get the return value
				if(retStr.indexOf("&")==0) retStr = ""; //if var is empty
				else if(retStr.indexOf("&")>0) retStr = retStr.substring(0, retStr.indexOf("&")); //pull until first &
			}
	}
	return retStr;
}

//disable all input elements that can perform actions (button, submit, image)
function disableInputElements()
{
	var iEls=document.getElementsByTagName("input");
	var iElsType;
	for(i=0;i<iEls.length;i++)
	{
		iElsType = iEls[i].type.toLowerCase();
		if(
			iElsType=='image' || 
			iElsType=='button' || 
			iElsType=='submit'
			)  iEls[i].disabled=true;
	}
}


//ref help: http://www.quirksmode.org/js/doctypes.html
function getScrollTop()
{
	var retVal;
	if(document.body.scrollTop)
		retVal = document.body.scrollTop;
	else if(document.documentElement && document.documentElement.scrollTop)
		retVal = document.documentElement.scrollTop;
	if(!retVal) retVal = 0; //force num
	return retVal;
}
function getScrollLeft()
{
	var retVal;
	if(document.body.scrollLeft)
		retVal = document.body.scrollLeft;
	else if(document.documentElement && document.documentElement.scrollLeft)
		retVal = document.documentElement.scrollLeft;
	if(!retVal) retVal = 0; //force num
	return retVal;
}

function getBrowserWindowHeight()
{
	var retVal;
	if(document.body.clientHeight)
		retVal = document.body.clientHeight;
	else if(document.documentElement && document.documentElement.clientHeight)
		retVal = document.documentElement.clientHeight;
	if(!retVal) retVal = 0; //force num
	return retVal;
}
function getBrowserWindowWidth()
{
	var retVal;
	if(document.body.clientWidth)
		retVal = document.body.clientWidth;
	else if(document.documentElement && document.documentElement.clientWidth)
		retVal = document.documentElement.clientWidth;
	if(!retVal) retVal = 0; //force num
	return retVal;
}


function trimMultispaces(textEl)
{
	//trim multispaces on input
	if(textEl)
		while(textEl.value.indexOf("  ")>0)
			textEl.value = textEl.value.replace(/  /g, ' ');
}


//appends instead of using document.write, which will clear the page if drawn directly in the document
function addHTML (html) {
  if (document.all)
    document.body.insertAdjacentHTML('beforeEnd', html);
  else if (document.createRange) {
    var range = document.createRange();
    range.setStartAfter(document.body.lastChild);
    var docFrag = range.createContextualFragment(html);
    document.body.appendChild(docFrag);
  }
  else if (document.layers) {
    var l = new Layer(window.innerWidth);
    l.document.open();
    l.document.write(html);
    l.document.close();
    l.top = document.height;
    document.height += l.document.height;
    l.visibility = 'show';
  }
}


function clickCheckbox(id)
{ //primary use to be from a label (for ie) to check a box when the text is clicked
	var el_Chkbox = document.getElementById(id);
	if(el_Chkbox.checked == true)
		el_Chkbox.checked = false;
	else
		el_Chkbox.checked = true;
}



function elPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function elPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }


//*** This code is copyright 2003 by Gavin Kistner, gavin@refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
//*** Reuse or modification is free provided you abide by the terms of that license.
//*** (Including the first two lines above in your source code satisfies the conditions.)


//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);
function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
}

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}
function MyFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}

//apply both 'onscroll' and 'onresize' for items - this is used primarily for popup-div placements 
//runs the AttachEvent function twice.
function AttachScrollResizeEvents(obj, fnc, useCapture)
{
	AttachEvent(obj, "resize", fnc, useCapture);
	AttachEvent(obj, "scroll", fnc, useCapture);
}

//center a div on the screen
function centerDiv(obj, offsetT, offsetL, baseWidth, baseHeight)
{
	if(!g_EBI(obj)) return false;
	obj = g_EBI(obj);
	
	var screenSize = getBrowserVisibleRect();
	var screenW = screenSize.width;
	var screenH = screenSize.height;
//	var scrollT = getScrollTop();
	var objWOffset = obj.offsetWidth / 2;
	var objHOffset = obj.offsetHeight / 2;
	if(baseWidth > 0) objWOffset = baseWidth / 2;
	if(baseHeight > 0) objHOffset = baseHeight / 2;
	
	var putToLeft = ( screenW / 2 ) +offsetL + getScrollLeft() - ( objWOffset );
	var putToTop = ( screenH / 2 ) +offsetT + getScrollTop() - ( objHOffset );
	if(putToLeft < 0) putToLeft = getScrollLeft() + 20; //force to be at least the buffer
	if(putToTop < 0) putToTop = getScrollTop() + 20; //force to be at least the buffer
	obj.style.left = putToLeft + "px";
	obj.style.top = putToTop + "px";
}

function getBrowserVisibleRect()
{
	var ret = new Object();
	var viewportwidth;
	var viewportheight;
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	}
	
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined'
	&& typeof document.documentElement.clientWidth !=
	'undefined' && document.documentElement.clientWidth != 0)
	{
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	}
	
	// older versions of IE
	else
	{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	ret.width = viewportwidth;
	ret.height = viewportheight;
	return ret;
}





var acrobat=new Object();
acrobat.installed=false;
acrobat.version='0.0';

function checkForAcrobat()
{
	if (navigator.plugins && navigator.plugins.length)
	{
		for (x=0; x<navigator.plugins.length; x++)
		{
			if (navigator.plugins[x].description.indexOf('Adobe') != -1 && navigator.plugins[x].description.indexOf('PDF') != -1)
			{
				/*
				acrobat.version=parseFloat(navigator.plugins[x].description.split('Version ')[1]);
				if (acrobat.version.toString().length == 1) acrobat.version+='.0';
				*/
				
				acrobat.installed=true;
				acrobat.version = 1; //set to 1 since ffx has just "Adobe PDF for Firefox"
				break;
			}
		}
	}
	else if (window.ActiveXObject)
	{
		for (x=2; x<10; x++)
		{
			try
			{
				oAcro=eval("new ActiveXObject('PDF.PdfCtrl."+x+"');");
				if (oAcro)
				{
					acrobat.installed=true;
					acrobat.version=x+'.0';
				}
			}
			catch(e) {}
		}
		
		try
		{
			oAcro4=new ActiveXObject('PDF.PdfCtrl.1');
			if (oAcro4)
			{
				acrobat.installed=true;
				acrobat.version='4.0';
			}
		}
		catch(e) {}
		
		try
		{
			oAcro7=new ActiveXObject('AcroPDF.PDF.1');
			if (oAcro7)
			{
				acrobat.installed=true;
				acrobat.version='7.0';
			}
		}
		catch(e) {}
	
	}
}

//set the tab activation based on type and which tab, useable for 2 tabs only (added/available)
function tabTop_SetActiveTabSet(tabsetStr, tabSet)
{
	var tab_0_L = tabsetStr + "_Tab_0_L"; var tab_0_C = tabsetStr + "_Tab_0_C"; var tab_0_R = tabsetStr + "_Tab_0_R";
	var tab_1_L = tabsetStr + "_Tab_1_L"; var tab_1_C = tabsetStr + "_Tab_1_C"; var tab_1_R = tabsetStr + "_Tab_1_R";
	
	if(tabSet==0) //set to tab 0 as the selected
	{
		if(g_EBI(tab_0_L)) g_EBI(tab_0_L).style.backgroundImage= "url(../core/images/tab/tab_top_corner_left_selected.gif)";
		if(g_EBI(tab_0_C)) g_EBI(tab_0_C).style.backgroundImage= "url(../core/images/tab/tab_top_slice_selected.gif)";
		if(g_EBI(tab_0_C)) g_EBI(tab_0_C).innerHTML = "<img src='../core/images/tab/tab_top_addedItems_selected.gif' alt='' border='0' />";
		if(g_EBI(tab_0_R)) g_EBI(tab_0_R).style.backgroundImage= "url(../core/images/tab/tab_top_corner_right_selected.gif)";

		if(g_EBI(tab_1_L)) g_EBI(tab_1_L).style.backgroundImage= "url(../core/images/tab/tab_top_corner_left_deselected.gif)";
		if(g_EBI(tab_1_C)) g_EBI(tab_1_C).style.backgroundImage= "url(../core/images/tab/tab_top_slice_deselected.gif)";
		if(g_EBI(tab_1_C)) g_EBI(tab_1_C).innerHTML = "<img src='../core/images/tab/tab_top_availableItems_deselected.gif' alt='' border='0' />";
		if(g_EBI(tab_1_R)) g_EBI(tab_1_R).style.backgroundImage= "url(../core/images/tab/tab_top_corner_right_deselected.gif)";
	}
	else
	{
		if(g_EBI(tab_0_L)) g_EBI(tab_0_L).style.backgroundImage= "url(../core/images/tab/tab_top_corner_left_deselected.gif)";
		if(g_EBI(tab_0_C)) g_EBI(tab_0_C).style.backgroundImage= "url(../core/images/tab/tab_top_slice_deselected.gif)";
		if(g_EBI(tab_0_C)) g_EBI(tab_0_C).innerHTML = "<img src='../core/images/tab/tab_top_addedItems_deselected.gif' alt='' border='0' />";
		if(g_EBI(tab_0_R)) g_EBI(tab_0_R).style.backgroundImage= "url(../core/images/tab/tab_top_corner_right_deselected.gif)";

		if(g_EBI(tab_1_L)) g_EBI(tab_1_L).style.backgroundImage= "url(../core/images/tab/tab_top_corner_left_selected.gif)";
		if(g_EBI(tab_1_C)) g_EBI(tab_1_C).style.backgroundImage= "url(../core/images/tab/tab_top_slice_selected.gif)";
		if(g_EBI(tab_1_C)) g_EBI(tab_1_C).innerHTML = "<img src='../core/images/tab/tab_top_availableItems_selected.gif' alt='' border='0' />";
		if(g_EBI(tab_1_R)) g_EBI(tab_1_R).style.backgroundImage= "url(../core/images/tab/tab_top_corner_right_selected.gif)";
	}
}


function AttachMouseWheelEvent(funct)
{
	if (window.addEventListener)	window.addEventListener('DOMMouseScroll', funct, false); //moz

	window.onmousewheel = document.onmousewheel = funct; //ie, opera
}


function fixImageToSize(imgID, sizeWay, size)
{ //take the image and resize it to the dimension in question
	//sizeWay: width/height

	if(!g_EBI(imgID)) return 1;

	var objImg = new Image()
	objImg.src = g_EBI(imgID).src;
	
	var currImgWidth = objImg.width;
	var currImgHeight = objImg.height;
	
	if(sizeWay.toLowerCase()=="width")
	{
		setToWidth = size;
		setToHeight = size * ( currImgHeight / currImgWidth );
	}
	else //height
	{
		setToHeight = size;
		setToWidth = size * ( currImgWidth / currImgHeight );
	}
	if(setToWidth <= 0) setToWidth = size;
	if(setToHeight <= 0) setToHeight = size;

	g_EBI(imgID).width=Math.round(setToWidth);
	g_EBI(imgID).height=Math.round(setToHeight);

	//show just in case it was hidden
	el_setDisplay(imgID, 1);
}

function fixImageToSize_PreloadDelay(imgID, sizeWay, size)
{ //make sure the image is loaded before we deal with it's rect
	var objImg = new Image();
	objImg.src = g_EBI(imgID).src;
	if(!objImg.complete) //wait
		setTimeout("fixImageToSize_PreloadDelay('" + imgID + "', '" + sizeWay + "', " + size + ");", 200);
	else //run fix
		fixImageToSize(imgID, sizeWay, size);
}



/* mask functions */
//requires window_display.js for setSelectBoxVisible_SansForm, setOpacity
function setVisibleMaskResize()
{
	//redraw mask size if we can
	if(!g_EBI('div_MaskGrey')) return false;
	if(g_EBI('div_MaskGrey').style.display=='')
	{
		g_EBI('div_MaskGrey').style.width=getBrowserWindowWidth() + "px";
		g_EBI('div_MaskGrey').style.height=getBrowserWindowHeight() + "px";
	}
}
function forceCreateMask()
{
	if(!g_EBI('div_MaskGrey')) //add the mask!
	{
		var maskStr = "<div id='div_MaskGrey' style='background-color:#3D557C;display:none;top:0px;left:0px;width:1px;height:1px;position:absolute;z-index:2;'></div>"
		addHTML(maskStr);
		setOpacity(g_EBI('div_MaskGrey'), 0);
	}
}
function showMask()
{
	forceCreateMask();
	
	if(g_EBI('div_MaskGrey') && g_EBI('div_MaskGrey').style.display=='' ) return false; //already shown so bypass this
	
	setSelectBoxVisible_SansForm(0); //hide boxes since ie makes thems funny... hides ALL boxes with the canHide tag!

	setOpacity(g_EBI('div_MaskGrey'), 0);
	if(g_EBI('div_MaskGrey')) g_EBI('div_MaskGrey').style.display='';
	setVisibleMaskResize();
	fadeMaskIn(0, 40);
}
function fadeMaskIn(currOpac, finalOpac)
{
	var delayTimer = 30;
	var opacIncr = 5;
	if(currOpac!=finalOpac) //work!
	{
		setOpacity(g_EBI('div_MaskGrey'), currOpac+opacIncr);
		setTimeout("fadeMaskIn(" + (currOpac+opacIncr) + ", " + finalOpac + ");", delayTimer);
	}
}
function hideMask()
{
	setSelectBoxVisible_SansForm(1); //show boxes if hidden... shows ALL boxes with the canHide tag!
	if(!g_EBI('div_MaskGrey')) return false;
	g_EBI('div_MaskGrey').style.display='none';
}
/* end mask functions */
