var Tools = {};

Tools.$ = function()
{
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) 
	{
		var element = arguments[i];
		if (typeof element == 'string')
		{
			element = document.getElementById(element);
		}
		if (arguments.length == 1)
		{
			return element;
		}
		elements.push(element);
	}
	return elements;
}
Tools.showDeleteWarning = function()
{
	return window.confirm('You are about to delete this record.\r\n\r\n  Press OK to delete record\r\n  Press Cancel to quit.');
}

Tools.showTip = function(event, eobj, disobj)
{
	if(event == null) event = window.event;
	var el = Tools.$(disobj)
	if(el == null) return;
	
	Tools.addEvent(Tools.$(eobj), "mouseout", Tools.delayHide, false);
	
	el.style.left = Tools.getX(event) +25;
	el.style.top = Tools.getY(event) -20;
	
	el.style.display = "block";
	Tools.updateDisplay(event);
}
Tools.getX = function(event)
{
	if(event == null) event = window.event;
	if(event.clientX) return event.clientX;	
	if(event.pageX)	{return event.pageX;}
}
Tools.getY = function(event)
{
	if(event == null) event = window.event;
	if(event.clientY)
	{
		Tools.ScrollY = event.clientY;
		return event.clientY;
	}
	if(event.pageY)
	{
		Tools.ScrollY = event.pageY;
		return event.pageY;
	}
}

Tools.copyToTextbox = function(txt1, txt2)
{
	var frm = document.forms[0];
	txt1value = frm.elements[txt1].value;
	arr = txt1value.split(" ");
	
	var newval = "";
	for(i=0; i< arr.length; i++)
	{
		newval += arr[i];
	}
	
	frm.elements[txt2].value = newval;
}

Tools.buildUsername = function(txt1, txt2, txttarget)
{
	var frm = document.forms[0];
	txt1value = frm.elements[txt1].value.substring(0,1);
	
	txt2value = frm.elements[txt2].value.toLowerCase();
	arr = txt2value.split(" ");
	
	var newval = "";
	for(i=0; i< arr.length; i++)
	{
		newval += arr[i];
	}
	
	frm.elements[txttarget].value = txt1value.toLowerCase() + newval.toLowerCase();
}

Tools.clearTexBox = function(txtbox)
{
	if(typeof(txtbox) == 'object')
	{
		txtbox.value = "";
	}
	
	if(typeof(txtbox) == 'string')
	{
		document.forms[0].elements[txtbox].value = "";
	}
}


Tools.updateDisplay = function(event)
{
	var display = "<table class='tipBox'>"
	+"<tr><td >x:</td><td>"+ Tools.getX(event) +"</td></tr>"
	+"<tr><td>y:</td><td>"+ Tools.getY(event) +"</td></tr>"
	+"</table>"
	Tools.$("divDisplay").innerHTML = display;
}
Tools.copyTo = function(fromBox, toBox)
{
	var frm = document.forms[0];
	
	if(frm.elements[toBox] != null && frm.elements[fromBox] != null)
	{
		frm.elements[toBox].value = frm.elements[fromBox].value;
	}
	
}
Tools.delayHide = function()
{
	setTimeout(Tools.hide, 300);
}
Tools.hide = function()
{
	var el = document.getElementById("divDisplay");
	el.style.display = "none";
	Tools.detachEvent(el, "mouseout", Tools.hide);
}
Tools.addEvent = function(el, evt, fnc, useCapture)
{
	if (el.addEventListener) el.addEventListener(evt, fnc, useCapture); 
	if (el.attachEvent)el.attachEvent("on"+ evt, fnc); 
	el['on' + evt] = fnc;
}
Tools.detachEvent = function(el, evt, fnc)
{
	if (el.removeEventListener) el.removeEventListener(evt, fnc);
	if (el.detachEvent)el.detachEvent("on"+ evt, fnc);
}
Tools.ScrollY = 0;
Tools.ScrollX = 0;
Tools.getScrollPos = function()
{
	/*
	var scrollX, scrollY;
    if (document.all)
    {
     	if (document.documentElement.scrollLeft)
	  	{
        	Tools.ScrollX = document.documentElement.scrollLeft;
        	Tools.ScrollY = document.documentElement.scrollTop;
		}
      	else
		{
			Tools.ScrollX = document.body.scrollLeft;
			Tools.ScrollY = document.body.scrollTop;
		}
    }
    else
    {
      Tools.ScrollX = window.pageXOffset;
      Tools.ScrollY = window.pageYOffset;
    }
    */
    
    //alert(Tools.ScrollY);
	document.forms[0].elements["txtY"].value = Tools.ScrollY;
}

Tools.setScroll = function()
{
	//alert("Setting scroll position.");
	//window.scrollTo(0,Tools.ScrollY);
}
Tools.displayCoords = function(event)
{
	var ds = "<table bgcolor='#ffffff'>"
	+"<tr><td >x:</td><td>"+ Tools.getX(event) +"</td></tr>"
	+"<tr><td>y:</td><td>"+ Tools.getY(event) +"</td></tr>"
	+"</table>"
	
	//Tools.$("divCoords").innerHTML = ds;
}
Tools.toggleDisplay = function(obj)
{
	var theObj = Tools.$(obj);
	if(theObj == null)
	{
		alert("The object you're lookin for is null");
		return;
	} 
	
	theObj.style.display = (theObj.style.display == 'none') ? 'block'	: 'none';
}
Tools.switchDisplay = function(obj1, obj2) 
{
	var theObj1 = Tools.$(obj1);
	var theObj2 = Tools.$(obj2);
	if(theObj1 == null || theObj2 == null)
	{
		alert("One of your objects is null: Object1-"+theObj1+" Object2-"+theObj2);
	}
	if(theObj1.style.display == "block" || theObj1.style.display == "")
	{
		theObj1.style.display = "none";
		
		theObj2.style.display = "block";
		return;
	}
	
	theObj1.style.display = "block";
	
	theObj2.style.display = "none";
}

Tools.switchEditCancel = function(objLnk, obj1, obj2) 
{
	var theLink = Tools.$(objLnk);
	var theObj1 = Tools.$(obj1);
	var theObj2 = Tools.$(obj2);
		
	if(theObj1 == null || theObj2 == null)
	{
		alert("One of your objects is null: Object1-"+theObj1+" Object2-"+theObj2);
	}
	if(theObj1.style.display == "block" || theObj1.style.display == "")
	{
		theLink.innerHTML = "[ x Cancel ]";
		theObj1.style.display = "none";
		
		theObj2.style.display = "block";
		theObj2.style.background = "#ebebeb";
		theObj2.style.border = "1px solid #cccccc";
		return;
	}
	
	theLink.innerHTML = "<img src=\"/akbuyers/webui/_images/icoUpdate.gif\" id=\"img\"/> ";
	theObj1.style.display = "block";
	
	theObj2.style.display = "none";
}
if(this.window.captureEvents)
{
	this.window.captureEvents(Event.MOUSEMOVE);
	this.window.onmousemove= Tools.displayCoords;
}
else
{
	this.document.onmousemove = Tools.displayCoords;
}

this.window.onscroll = Tools.getScrollPos;
this.window.onresize = Tools.getScrollPos;
this.window.onload = Tools.setScroll;
this.window.onload = Tabs.doSetup;