// Bind getMouseXY to the mouse move event for NN and IE, so we always know where the pointer is.
// TODO:  This needs to be checked to make sure X,Y are relative to document position, not screen position
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0;
var submitted = false;

function noDblClick() {
//    if (submitted == false) {
//      submitted = true;
//      return true;
//    }
//    return false;
return true;
}
function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
	tempX = event.clientX + document.body.scrollLeft;
	tempY = event.clientY + document.body.scrollTop;
	}
	else {  // grab the x-y pos.s if browser is NS
	tempX = e.pageX;
	tempY = e.pageY;
	}
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}
	return true;
}                                                                                      
function showTip(theDiv) {
	var tehObj = getStyleObject(theDiv);
                                                                                                                    
        if (tehObj)
        {
                tehObj.display="block";
                tehObj.top=tempY;
                tehObj.left=tempX;
                tehObj.visibility="visible";
        }
}
function hideTip(theDiv) {
        var tehObj = getStyleObject(theDiv);
                                                                                                             
        if (tehObj)
        {
                tehObj.display="none";
                tehObj.top="0";
                tehObj.left="0";
                tehObj.visibility="hidden";
        }
}
function highlightFocus(theControl)
{
	theControl.focus();
	if (theControl.type == 'text')
	{
		theControl.select();
		theControl.select();
	}
	return true;
}

function shipperDiv(index)
{
	// Hide all of the shipper* divs
	var regEx=/^shipper/;
	var divList=document.getElementsByTagName("div");
	for (var x = 0; x < divList.length;x++)
	{
		var thisDiv=divList.item(x);
		var thisID=thisDiv.id;
		if (regEx.test(thisID))
		{
			var myDiv=getStyleObject(thisID);
			myDiv.display='none';
		}
	}
	// Show the shipper(index) div
	var obj=getStyleObject('shipper'+index);
	obj.display='block';
}

function switchDiv(this_div, next_div, theControl)
{
  if (getStyleObject(this_div) && getStyleObject(next_div)) {
    changeObjectVisibility(this_div, "hidden","none");
    changeObjectZindex(this_div,"200","fixed","9999","9999");
    changeObjectVisibility(next_div, "visible","block");
    changeObjectZindex(next_div,"1","relative","0","0");
  }
  if (theControl)
  {
	highlightFocus(theControl);
  }
  return true;
}
function changeObjectZindex(objectId, newZindex,newPos,newTop,newLeft) {
	var tehObj = getStyleObject(objectId);

	if (tehObj) {
		tehObj.zindex = newZindex;
		tehObj.position = newPos;
		tehObj.left = newLeft;
		tehObj.top = newTop;
		return true;
	} else {
		return false;
	}
}
function changeObjectVisibility(objectId, newVisibility, newDisplay) {
    // first get the object's stylesheet
    var styleObject = getStyleObject(objectId);

    // then if we find a stylesheet, set its visibility
    // as requested
    //
    if (styleObject) {
	styleObject.visibility = newVisibility;
	styleObject.display = newDisplay;
	return true;
    } else {
	return false;
    }
}
function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else {
	return false;
   }
}
function getObject(objectId)
{
   if(document.getElementById && document.getElementById(objectId)) {
        return document.getElementById(objectId);
   }
   else if (document.all && document.all(objectId)) {
        return document.all(objectId);
   }
   else if (document.layers && document.layers[objectId]) {
        return document.layers[objectId];
   } else {
        return false;
   }
}
function doSubmit(divName,nextDivName,spanName,formName,controlName,validatorFunction,spanText)
{
	if (validatorFunction && validatorFunction != null)
	{
		var theValid = validatorFunction(formName);
		if (theValid == false)
		{
			return false;
		}
	}

	var newVal=controlName.value;
	var mySpan=document.getElementById(spanName);
	if (mySpan)
	{
		if (newVal != '' || spanText != '') 
		{	
			if (spanText)
			{
				mySpan.innerHTML=spanText;
			}
			else
			{
				mySpan.innerHTML=newVal;	
			}
		}
		else
		{
			mySpan.innerHTML='Not Set';
		}
	}

	if (divName && nextDivName)
	{
		switchDiv(divName,nextDivName);
	}

	formName.submit();
}
function changeFace(imgTag, faceName, formName, facePath)
{
	var myImage = document.getElementById(imgTag);
	if (myImage)
	{
		myImage.src = facePath + faceName;
		formName.iconuri.value = faceName;
		formName.submit();
	}
}
function reloadSubmit(event, form)
{
	if (IE)
	{
		if (window.event && window.event.keyCode == 13)
                {
			alert('you hit enter');
                	form.submit();
			return false;
		} else {
                        return true;
                }
        }
        else {
                if (event && event.which == 13)
                {
			alert('joo hitxored teh enter key');
                	form.submit();
			return false;
		}
                else
                {
                        return true;
                }
        }
}
function checkSubmit(event,divName,nextDivName,spanName,formName,controlName, validatorFunction)
{
	if (IE)
	{
		if (window.event && window.event.keyCode == 13)
		{
			doSubmit(divName, nextDivName, spanName, formName, controlName, validatorFunction);
		} else {
			return true;
		}
	}
	else {
		if (event && event.which == 13)
		{
			doSubmit(divName, nextDivName, spanName, formName, controlName, validatorFunction);
		}
		else
		{
			return true;
		}
	}
}
////// Cookie Functions
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
function getCookie (name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
        var j = i + alen;
                if (document.cookie.substring(i, j) == arg)
                return getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
                if (i == 0) break; 
        }

	return null;
}
function getCookieVal (offset) {
        var endstr = document.cookie.indexOf (";", offset);
                if (endstr == -1)
        endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
}
function deleteCookie (name) {
	var exp = new Date(2000,11,24,18,0,0);
	var delCookie = name + "=; expires=" + exp.toGMTString() + "; path=/";
	document.cookie = delCookie;
}
////// Tree-view functions
function toggle(theDivID)
{
	var styleObject = getStyleObject(theDivID);
	var imageID = theDivID + 'i';
	var imageObject = getObject(imageID);

	if (styleObject)
	{
		if (styleObject.display == 'none')
		{
			styleObject.display = 'block';
			imageObject.src = '/images/minus.gif';
		}
		else
		{
			styleObject.display = 'none';
			imageObject.src = '/images/plus.gif';
		}
	}

	// Store Tree State
	                                                                                                                                                                                              
	var trees = document.getElementsByName("tree");

	var cookieStr;

	for (var i=0;i<trees.length;i++)
	{
		if (trees[i].style.display == 'block')
		{
			if (cookieStr)
			{
				cookieStr = cookieStr + ":" + trees[i].id;
			}
			else
			{
				cookieStr = trees[i].id;
			}
		}
	}
	setCookie('treestate',cookieStr,null,'/');
	return;
}
function restoreTree()
{
	var restore = getCookie('treestate');

	if (restore)
	{
		var divArray = restore.split(":");

		for (var i=0; i < divArray.length; i++)
		{
			var styleObject = getStyleObject(divArray[i]);
			var imgId = divArray[i] + 'i';
			var imgObj = getObject(imgId);
			imgObj.src = '/images/minus.gif';
			styleObject.display = 'block';
		}
	}
	return;
}
////// Error Functions
function showError(badField)
{
	var errDiv=badField.name + 'error';
	var theDiv=getStyleObject(errDiv);

	badField.style.border = '1px solid red';
	badField.focus();
	theDiv.display = 'block';

	return;
}
////// Multipage Functions
function getRange(theSelect,theCookieValue)
{
	var ubound=theSelect.length;
	var theValue = getCookie(theCookieValue);

	for (var i=0;i<ubound;i++)
	{
		if (theSelect[i].value == theValue)
		{
			theSelect[i].selected = true;
		}
	}
	return;
}
////// Validators
function checkpassword(theForm)
{
	if (theForm.password.value == "" || theForm.checkpass.value == "")
	{
		alert("You must enter a password in both of the boxes!  Please try again.");
		theForm.password.value = "";
                theForm.checkpass.value = "";
                theForm.password.focus();
                return false;
	}

	if (theForm.password.value != theForm.checkpass.value)
	{
		alert("The passwords you entered do not match!  Please reenter and try again.");
		theForm.password.value = "";
		theForm.checkpass.value = "";
		theForm.password.focus();
		return false;
	}
}
//////// Popup Code
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
                                                                                                                             
                                                                                                                             
function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
         return rnd.seed/(233280.0);
}
                                                                                                                             
                                                                                                                             
function rand(number) {
        return Math.ceil(rnd()*number);
}
                                                                                                                             
                                                                                                                             
function pop(URL, width, height, specname)
{
                        var name = rand(19302);
                        if (specname) { name = specname; }
                        var popup = new Object();
                        // get center of browser window
                        var winleft = (screen.width - width) / 2;
                        var wintop = (screen.height - height) / 2;
                        popup = window.open(URL, name,'width='+width+',height='+height+',top='+wintop+',left='+winleft+',resizable, dependant, scrollbars=yes')
                        popup.focus()
}

