///////////////////////////////////////////////////////////////////////
//     This Button Script was designed by Erik Arvidsson for WebFX   //
//                                                                   //
//     For more info and examples see: http://webfx.eae.net/         //
//     or send mail to erik@eae.net                                  //
//                                                                   //
//     Feel free to use this code as lomg as this disclaimer is      //
//     intact.                                                       //
///////////////////////////////////////////////////////////////////////

document.onmouseover = doOver;
document.onmouseout  = doOut;
document.onmousedown = doDown;
document.onmouseup   = doUp;

// document.onmouseover = checkOver
// document.onmouseout = checkOut


function doOver() { 
	checkOver();
	var toEl = getReal(window.event.toElement, "className", "coolButton");
	var fromEl = getReal(window.event.fromElement, "className", "coolButton");
	if (toEl == fromEl) return;
	var el = toEl;
	
//	alert(el);
	
//	var cDisabled = el.getAttribute("cDisabled");
	var cDisabled = el.cDisabled;
//	alert(cDisabled);
	cDisabled = (cDisabled != null); // If CDISABLED atribute is present
	
	if (el.className == "coolButton")
		el.onselectstart = new Function("return false");
	
	if ((el.className == "coolButton") && !cDisabled) {
		makeRaised(el);
		makeGray(el,false);
	}

}

function doOut() {
	var toEl = getReal(window.event.toElement, "className", "coolButton");
	var fromEl = getReal(window.event.fromElement, "className", "coolButton");
	if (toEl == fromEl) return;
	var el = fromEl;

//	var cDisabled = el.getAttribute("cDisabled");
	var cDisabled = el.cDisabled;
	cDisabled = (cDisabled != null); // If CDISABLED atribute is present

	var cToggle = el.cToggle;
	toggle_disabled = (cToggle != null); // If CTOGGLE atribute is present

	if (cToggle && el.value) {
		makePressed(el);
		makeGray(el,true);
	}
	else if ((el.className == "coolButton") && !cDisabled) {
		makeFlat(el);
		makeGray(el,true);
	}
	checkOut();
}

function doDown() {
	el = getReal(window.event.srcElement, "className", "coolButton");
	
	var cDisabled = el.cDisabled;
	cDisabled = (cDisabled != null); // If CDISABLED atribute is present
	
	if ((el.className == "coolButton") && !cDisabled) {
		makePressed(el)
	}
}

function doUp() {
	el = getReal(window.event.srcElement, "className", "coolButton");
	
	var cDisabled = el.cDisabled;
	cDisabled = (cDisabled != null); // If CDISABLED atribute is present
	
	if ((el.className == "coolButton") && !cDisabled) {
		makeRaised(el);
	}
}


function getReal(el, type, value) {
	temp = el;
	while ((temp != null) && (temp.tagName != "BODY")) {
		if (eval("temp." + type) == value) {
			el = temp;
			return el;
		}
		temp = temp.parentElement;
	}
	return el;
}

function findChildren(el, type, value) {
	var children = el.children;
	var tmp = new Array();
	var j=0;
	
	for (var i=0; i<children.length; i++) {
		if (eval("children[i]." + type + "==\"" + value + "\"")) {
			tmp[tmp.length] = children[i];
		}
		tmp = tmp.concat(findChildren(children[i], type, value));
	}
	
	return tmp;
}

function disable(el) {

	if (document.readyState != "complete") {
		window.setTimeout("disable(" + el.id + ")", 100);	// If document not finished rendered try later.
		return;
	}
	
	var cDisabled = el.cDisabled;
	
	cDisabled = (cDisabled != null); // If CDISABLED atribute is present

	if (!cDisabled) {
		el.cDisabled = true;
		
		el.innerHTML = '<span style="background: buttonshadow; width: 100%; height: 100%; text-align: center;">' +
						'<span style="filter:Mask(Color=buttonface) DropShadow(Color=buttonhighlight, OffX=1, OffY=1, Positive=0); height: 100%; width: 100%%; text-align: center;">' +
						el.innerHTML +
						'</span>' +
						'</span>';

		if (el.onclick != null) {
			el.cDisabled_onclick = el.onclick;
			el.onclick = null;
		}
	}
}

function enable(el) {
	var cDisabled = el.cDisabled;
	
	cDisabled = (cDisabled != null); // If CDISABLED atribute is present
	
	if (cDisabled) {
		el.cDisabled = null;
		el.innerHTML = el.children[0].children[0].innerHTML;

		if (el.cDisabled_onclick != null) {
			el.onclick = el.cDisabled_onclick;
			el.cDisabled_onclick = null;
		}
	}
}

function addToggle(el) {
	var cDisabled = el.cDisabled;
	
	cDisabled = (cDisabled != null); // If CDISABLED atribute is present
	
	var cToggle = el.cToggle;
	
	cToggle = (cToggle != null); // If CTOGGLE atribute is present

	if (!cToggle && !cDisabled) {
		el.cToggle = true;
		
		if (el.value == null)
			el.value = 0;		// Start as not pressed down
		
		if (el.onclick != null)
			el.cToggle_onclick = el.onclick;	// Backup the onclick
		else 
			el.cToggle_onclick = "";

		el.onclick = new Function("toggle(" + el.id +"); " + el.id + ".cToggle_onclick();");
	}
}

function removeToggle(el) {
	var cDisabled = el.cDisabled;
	
	cDisabled = (cDisabled != null); // If CDISABLED atribute is present
	
	var cToggle = el.cToggle;
	
	cToggle = (cToggle != null); // If CTOGGLE atribute is present
	
	if (cToggle && !cDisabled) {
		el.cToggle = null;

		if (el.value) {
			toggle(el);
		}

		makeFlat(el);
		
		if (el.cToggle_onclick != null) {
			el.onclick = el.cToggle_onclick;
			el.cToggle_onclick = null;
		}
	}
}

function toggle(el) {
	el.value = !el.value;
	
	if (el.value)
		el.style.background = "URL(/images/tileback.gif)";
	else
		el.style.backgroundImage = "";

//	doOut(el);	
}


function makeFlat(el) {
	with (el.style) {
		background = "";
		border = "1px solid buttonface";
		padding      = "1px";
	}
}

function makeRaised(el) {
	with (el.style) {
		borderLeft   = "1px solid buttonhighlight";
		borderRight  = "1px solid buttonshadow";
		borderTop    = "1px solid buttonhighlight";
		borderBottom = "1px solid buttonshadow";
		padding      = "1px";
	}
}

function makePressed(el) {
	with (el.style) {
		borderLeft   = "1px solid buttonshadow";
		borderRight  = "1px solid buttonhighlight";
		borderTop    = "1px solid buttonshadow";
		borderBottom = "1px solid buttonhighlight";
		paddingTop    = "2px";
		paddingLeft   = "2px";
		paddingBottom = "0px";
		paddingRight  = "0px";
	}
}

function makeGray(el,b) {
	var filtval;
	
	if (b)
		filtval = "gray()";
	else
		filtval = "";

	var imgs = findChildren(el, "tagName", "IMG");
		
	for (var i=0; i<imgs.length; i++) {
		imgs[i].style.filter = filtval;
	}

}
	
var actual_button=null;

// cell functions
function mOver(cell)
{
   if (!cell.contains(event.fromElement))
   {
		if (actual_button!=cell) {	
			cell.bgColor = '#aaaaaa';
			cell.style.cursor = 'hand';			
		} else {
			cell.style.cursor = 'default';	
			cell.bgColor = '#000000';
		}
   }
}

function mOut(cell)
{
   if (!cell.contains(event.toElement))
   {
	   	if (actual_button!=cell) {	
	      cell.style.cursor = 'default';
	   	   cell.bgColor = '#D3D3D3'; }
	   	 else  {
	   	   cell.bgColor = '#000000'; 
	   	}
   }
}

function mClick(cell,link_def)
{
   if(event.srcElement.tagName=='TD')
   {
		cell.style.cursor = 'default';	
   	   if (actual_button!=cell) {
   	   		if (actual_button!=null) {
   	   			actual_button.bgColor = '#D3D3D3';
   	   			actual_button.style.color = '#000000';
   	   		}
   	   		cell.bgColor = '#000000';
   	   		cell.style.color = '#ffffff';
 		   actual_button=cell;
 		}
 		if (link_def) parent.body.location=link_def;
   }
}

function mClickSelf(cell,link_def)
{
   if(event.srcElement.tagName=='TD')
   {
		cell.style.cursor = 'default';	
   	   if (actual_button!=cell) {
   	   		if (actual_button!=null) {
   	   			actual_button.bgColor = '#D3D3D3';
   	   			actual_button.style.color = '#000000';
   	   		}
   	   		cell.bgColor = '#000000';
   	   		cell.style.color = '#ffffff';
 		   actual_button=cell;
 		}
 		if (link_def) document.location=link_def;
   }
}

function mClickSubmit(cell)
{
   if(event.srcElement.tagName=='TD')
   {
		cell.style.cursor = 'default';	
   	   if (actual_button!=cell) {
   	   		if (actual_button!=null) {
   	   			actual_button.bgColor = '#D3D3D3';
   	   			actual_button.style.color = '#000000';
   	   		}
   	   		cell.bgColor = '#000000';
   	   		cell.style.color = '#ffffff';
 		   actual_button=cell;
 		}
		document.forms[0].submit();
   }
}


function mClickClose()
{
	top.window.close();
}


function mClickSubmitClose()
{
	document.forms[0].submit();
	if (window==top.window) {	top.window.close();}
}



function mClickEditSubmit(cell)
{
   if(event.srcElement.tagName=='TD')
   {
		cell.style.cursor = 'default';	
   	   if (actual_button!=cell) {
   	   		if (actual_button!=null) {
   	   			actual_button.bgColor = '#D3D3D3';
   	   			actual_button.style.color = '#000000';
   	   		}
   	   		cell.bgColor = '#000000';
   	   		cell.style.color = '#ffffff';
 		   actual_button=cell;
 		}
		submitHandler_ASPRichEdit();
		document.forms[0].submit();

   }
}

function mClickPopUp(cell,link_def,h,w,wname,wtitle)
{
   if(event.srcElement.tagName=='TD')
   {
		xc=openPopUP(wname,h, w, link_def, wtitle)
		xc.focus();
   }	
}

function mClick2PopUp(cell,link_def,h,w,wname,wtitle)
{
		xc=openPopUP(wname,h, w, link_def, wtitle)
		xc.focus();

}



function openPopUP(wname,h, w, url, winname) {
  if (navigator.appVersion.indexOf('4') != -1) {
      xpos = screen.width/2 - (w/2);
      ypos = screen.height/2 - (h/2);
      return window.open(url, wname, 'height='+h+',width='+w+',scrollbars=0,resizable=0,menubar=0,toolbar0,status=0,location=0,directories=0,left=' + xpos + ',top=' + ypos + ''); 
  } else {
      return window.open(url, wname, 'height='+h+',width='+w+',scrollbars=0,resizable=0,menubar=0,toolbar=0,status=0,location=0,directories=0,left=100,top=150');
  }
}
	
	
	
	
var iDelay = 350 // Delay to hide in milliseconds
var iNSWidth=100 // Default width for netscape
var sDisplayTimer = null, oLastItem

function getRealPos(i,which) {
  iPos = 0
  while (i!=null) {
    iPos += i["offset" + which]
    i = i.offsetParent
  }
  return iPos
}

function showDetails(sDest,itop,ileft,iWidth) {
  if (document.all!=null) {
    var i = window.event.srcElement
    stopTimer()
    dest = document.all[sDest]
    if ((oLastItem!=null) && (oLastItem!=dest))
      hideItem()
    if (dest) {
      // Netscape Hack
      if (i.offsetWidth==0) 
        if (iWidth)
          i.offsetWidth=iWidth
        else
          i.offsetWidth=iNSWidth;
      if (ileft) 
        dest.style.pixelLeft = ileft +40
      else
        dest.style.pixelLeft = getRealPos(i,"Left") + 40
      if (itop)
        dest.style.pixelTop = itop+16
      else
        dest.style.pixelTop = getRealPos(i,"Top")+16
      dest.style.visibility = "visible"
    }
  oLastItem = dest
  }
}

function stopTimer() {
  clearTimeout(sDisplayTimer)
}

function startTimer(el) {
  if (!el.contains(event.toElement)) {
    stopTimer()
    sDisplayTimer = setTimeout("hideItem()",iDelay)
  }
}

function hideItem() {
  if (oLastItem)
    oLastItem.style.visibility="hidden"
}

function checkOver() {
  if ((oLastItem) && (oLastItem.contains(event.srcElement)))
    stopTimer()
}

function checkOut() {
  if (oLastItem==event.srcElement)
    startTimer(event.srcElement)
}





document.write("<style>");
document.write(".coolBar	{background: buttonface;border-top: 1px solid buttonhighlight;	border-left: 1px solid buttonhighlight;	border-bottom: 1px solid buttonshadow; border-right: 1px solid buttonshadow; padding: 2px; font: menu;}");
document.write(".coolButton {border: 1px solid buttonface; padding: 1px; text-align: center; cursor: default;}");
document.write(".coolButton IMG	{filter: gray();}");
document.write("</style>");



// ************************************************************************************
// ************************************************************************************
// ************************************************************************************
// ************************************************************************************
// ************************************************************************************
// ************************************************************************************


var sOffBackColor = "";
var sOffTextColor = "menutext"
var sOffBorderColor = "threedface";
var sOffPadding = "1px 1px 1px 1px";

var sOverBackColor = "";
var sOverTextColor = "menutext"
var sOverBorderColor = "threedhighlight threedshadow threedshadow threedhighlight";
var sOverPadding = "1px 1px 1px 1px";

var sDownBackColor = ""
var sDownTextColor = "menutext"
var sDownBorderColor = "threedshadow threedhighlight threedhighlight threedshadow";
var sDownPadding = "2px 0px 0px 2px";




var sActiveButtonName = "";
var eActiveButton = null;



function SetButtonPadding(eButton,sPaddingStyle)
{
  eButton.rows[0].cells[0].style.padding = sPaddingStyle;
  if (eButton.rows[0].cells[1]) {
  	eButton.rows[0].cells[1].style.padding = sPaddingStyle;
  }
}

function SetButtonImage(eButton,sState)
{
  var eImg = eButton.rows[0].cells[0].children[0];
  if (eImg)
  {
    var sImgSrc = eImg.src;
    
  }
}


function button_over(eButton)
{
  // IF WE'RE JUST MOUSING ABOUT WITHIN THE BUTTON, EXIT WITHOUT DOING A THING

  if (window.event && eButton.contains(window.event.fromElement)) return false;

  // OTHERWISE CHANGE BUTTON STYLE

  eButton.style.backgroundColor = sOverBackColor;
  eButton.style.color = sOverTextColor;
  if (eActiveButton != eButton)
  {
    eButton.style.borderColor = sOverBorderColor;
    SetButtonImage(eButton,"over");
  }
}



function button_out(eButton)
{
  // IF WE'RE JUST MOUSING ABOUT WITHIN THE BUTTON, EXIT WITHOUT DOING A THING

  if (window.event && eButton.contains(window.event.toElement)) return false;

  // OTHERWISE CHANGE BUTTON STYLE

  if (eActiveButton != eButton)
  {
    eButton.style.backgroundColor = sOffBackColor;
    eButton.style.borderColor = sOffBorderColor;
    eButton.style.color = sOffTextColor;
    SetButtonPadding(eButton,sOffPadding);
    SetButtonImage(eButton,"off")
  }
  else
  {
    eButton.style.backgroundColor = sDownBackColor;
    eButton.style.borderColor = sDownBorderColor;
    eButton.style.color = sDownTextColor;
  }
}


function button_down(eButton)
{
  eButton.style.borderColor = sDownBorderColor;
  SetButtonPadding(eButton,sDownPadding);
}


function button_up(eButton){
	button_over(eButton);
}
function button_up2(eButton)

{
  // IF ANOTHER BUTTON IS ACTIVE, RESET IT

  if (null != eActiveButton && eButton != eActiveButton)
  {
    eActiveButton.style.backgroundColor = sOffBackColor;
    eActiveButton.style.borderColor = sOffBorderColor;
    eActiveButton.style.color = sOffTextColor;
    SetButtonImage(eActiveButton,"off");
    SetButtonPadding(eActiveButton,sOffPadding);
    eActiveButton = null;

    // insert code here for button resetting
    window.status = window.defaultStatus;
  }

  // IF THE BUTTON WE CLICKED IS NOT THE ACTIVE BUTTON, MAKE IT ACTIVE

  if (eButton != eActiveButton)
  {
    eActiveButton = eButton;
    eActiveButton.style.borderColor = sDownBorderColor;
    eActiveButton.style.color = sDownTextColor;
    SetButtonPadding(eActiveButton,sDownPadding);

    // insert code here for button activating
    sActiveButtonName = eActiveButton.id.replace(/tbl(.*)Button/,"$1");
    window.status = "Now showing: " + sActiveButtonName;
  }

  // OTHERWISE, RESET IT

  else
  {
    eActiveButton.style.backgroundColor = sOverBackColor;
    eActiveButton.style.borderColor = sOverBorderColor;
    eActiveButton.style.color = sOverTextColor;
    SetButtonPadding(eActiveButton,sOverPadding);
    eActiveButton = null;

    // insert code here for button resetting
    window.status = window.defaultStatus;
  }
}

