/**
 * AUTHOR
 * Andre Nicholson
 *
 * PURPOSE
 * Administration page JavaScript functions available to all scrtips within.
 *
 * TODO
 * 2005-01-06 By Andre Nicholson
 *  o Handling keyboard capture in all scripts needs to be tested - RT #1404.
 *
 * HISTORY
 */

var __KEY_TAB__	= 9;
var __KEY_F3__	= 114;
var movecount = 0;
var overMenu = false;
var hideNavDivTimeout = null;
//*******************************************************************
// ADMIN INIT
// Global JavaScript initialization function, called by every script in
// the administration website on the oninit() property of the body tag.
//*******************************************************************
function adminInit(){
	return;
	document.onkeypress = adminKeyPress;
	document.onkeydown = adminKeyDown;
}

//*******************************************************************
// POPUP
//*******************************************************************
function popup( url, width, height, feats ){
	var title = "Popup";
	var features = "height=" + height + ",width=" + width + ",";

	if( false != feats ){
		features += feats;
	} else {
		features += "toolbars=no,location=no,menubar=no,status=yes,resizable=yes,scrollbars=yes";
	}

	var hWindow = window.open( url, title, features );
	hWindow.focus();
}

//*******************************************************************
// FORM FOCUS
// Function accepts either a form element's name or form element index number.
//*******************************************************************
function formFocus( formNo, inputName, isElement ){
	var objForm = eval( "document.forms[" + formNo + "];" );

	// If inputName is a name of an element and not forcing the use of elements, proceed.
	if( isNaN(inputName) && "boolean" != typeof isElement ){

		eval( "objForm." + inputName + ".focus();" );

	// Otherwise inputName is an integer representing the element index in the form.
	} else {

		eval( 'objForm.elements["' + inputName + '"].focus();' );

	}
}

//*******************************************************************
// GET FORM ELEMENT
// HTML element that is specified as a _POST array -- "nItemPrice[x]"
// while valid HTML, is not as an object of the forms[] array -- cannot
// be referenced by name, must use the elements array to reference the
// desired form input.
//*******************************************************************
function getFormElement( formNo, needle ){
	for( var i = 0; i < document.forms[formNo].elements.length; i++ ){
		var objName = document.forms[formNo].elements[i].name;

		if( -1 != objName.indexOf(needle) ){
			return i;
		}
	}

	return null;
}

//*******************************************************************
// TOP LEVEL
//*******************************************************************
function topLevel(){
	if( self != top ){
		top.location = self.location;
	}
}

//*******************************************************************
// TOGGLE ORDER DATE FIELD
//*******************************************************************
function toggleOrderDateField(){
	var dateOrderInput = document.forms[0].nDOrder;

	dateOrderInput.disabled = !dateOrderInput.disabled;
}

//*******************************************************************
// COPY VALUES
//*******************************************************************
function copyValues( a, b ){
	b.value = a.value;
}

//*******************************************************************
// COPY BILLING ADDRESS
// edit_customer
//*******************************************************************
function copyBillingAddress(){
	theForm = document.customer_form;

	copyValues( theForm.nBill_name, theForm.nShip_name );
	copyValues( theForm.nBill_contact, theForm.nShip_contact );
	copyValues( theForm.nBill_address, theForm.nShip_address );
	copyValues( theForm.nBill_address2, theForm.nShip_address2 );
	copyValues( theForm.nBill_city, theForm.nShip_city );
	copyValues( theForm.nBill_state, theForm.nShip_state );
	copyValues( theForm.nBill_zip, theForm.nShip_zip );
	copyValues( theForm.nBill_phone, theForm.nShip_phone );
	copyValues( theForm.nBill_fax, theForm.nShip_fax );
	copyValues( theForm.nBill_email, theForm.nShip_email );
}

//*******************************************************************
// ADMIN KEY PRESS
//*******************************************************************
function adminKeyPress( e ){
	var k;
	var ns = false;

	if( e ){
		k = e;
		ns = true;
	} else {
		e = event;
		k = e.keyCode;
	}

	if( ns && 0 < parent.frames.length && parent.frames[2].handleKeyPress ){
		e.preventDefault();
		return handleKeyPress( k.keyCode );
	}
    return true; // not handled
}

//*******************************************************************
// ADMIN KEY DOWN
//*******************************************************************
function adminKeyDown( e ){
	if( !e ){
		var k = e.keyCode;
		e = event;

		if( 0 < parent.frames.length && parent.frames[2].handleKeyPress ){
			return( handleKeyPress(k) );
		}
/*
		if( 114 == k ){
			e.keyCode = 0;
			window.location.href = "edit_item.php?part_number=$part_number";
			return false;
		}
*/

    }
    return true;
}

//*******************************************************************
// SEND FORM
//*******************************************************************
function sendForm( formNo, doWhat ){
	// Append a submit input value for PHP to look for.
	appendFormInput( document.forms[formNo], document, "hidden", doWhat, "defaultValue" );

	// Submit the form.
	document.forms[formNo].submit();
}

//*******************************************************************
// APPEND FORM INPUT
// Appending elements to objects in IE requires to be done at the document.body level.
//*******************************************************************
function appendFormInput( theForm, domObj, type, name, value ){
	// Need to create the new element in the same DOM level.
	// If creating a new element from a popup on a form in the opening window, need to
	// create the element in the opening window's DOM.
	var objInput = domObj.createElement( "input" );

	objInput.setAttribute( "type", type );
	objInput.setAttribute( "name", name );
	objInput.setAttribute( "value", value );

	theForm.appendChild( objInput );
}

//*******************************************************************
// SET DOCUMENT TITLE
// Changes the top frame's (ie, the browser's) title.
//*******************************************************************
function setDocumentTitle( name ){
	parent.document.title = name;
}

//*******************************************************************
// RELOAD FRAME
// Triggers a selected frame to reload itself.
//*******************************************************************
function reloadFrame( frameName ){
	if( document.images ){
		parent.frames[frameName].location.reload();
	} else {
		parent.frames[frameName].location.href = parent.frames[frameName].location.href;
	}
}

function moveNavDiv() {
   movecount++;
//   top.content.status = 'moving...' + movecount;
   if (top.content.document.getElementById('navdiv')) {

      // if non clone is visible, then abort

      var navdiv = top.content.document.getElementById('navdiv');
      var yOff = '';
      if (top.content.pageYOffset) {
         yOff = top.content.pageYOffset;
      } else {
         yOff = top.content.document.body.scrollTop;
      }

      if (yOff <= navdiv.style.top) {
//         top.content.status = 'NOT moving... non clone on screen' + movecount;
         return;
      }
      if (!top.content.document.getElementById('navDivClone')) {
         navDivClone = navdiv.cloneNode(true);
         navDivClone.style.zIndex = '0';
         navDivClone.id = 'navDivClone';
         navDivClone.top = '-500px';
         navDivClone.left = '-500px';
         navDivClone.display = 'none';
         navdiv.parentNode.insertBefore(navDivClone, navdiv);
//         top.content.status = 'cloned ' + movecount;
/*
         for (i=0; i<navDivClone.childNodes.length; i++) {
             node = navDivClone.childNodes[i];
//             if (node.nodeName=="LI") {
                 node.onhover=keepNavDiv;
//             }
         }
*/
      }
//      top.status=yOff;
      if (top.content.document.getElementById('navDivClone')) {
//         top.content.status = 'navDivClone found ' + movecount;
         var navDivClone = top.content.document.getElementById('navDivClone');
         navDivClone.style.position = "absolute";
         navDivClone.style.top = (yOff) + 'px';
         navDivClone.style.left = '0px';
         navDivClone.display = 'block';
         navDivClone.style.zIndex = '500';
         navDivClone.onmouseover = mouseOverMenu;
         navDivClone.onmouseout = mouseOutMenu;
         navDivClone.onhover = mouseOverMenu;
         if (hideNavDivTimeout != null) {
            clearTimeout(hideNavDivTimeout);
         }
         hideNavDivTimeout = top.setTimeout('top.hideNavDiv();', 2000);
      } else {
//         top.content.status = 'navDivClone not found ' + movecount;
      }
   }
}

function mouseOverMenu() {
   overMenu = true;
   keepNavDiv();
}
function mouseOutMenu() {
   overMenu = false;
}
function keepNavDiv() {
   movecount++;
//   top.content.status = 'keeping...' + movecount;
   if (hideNavDivTimeout != null) {
      clearTimeout(hideNavDivTimeout);
   }
   hideNavDivTimeout = top.setTimeout('top.hideNavDiv();', 2000);
}

function hideNavDiv() {
   movecount++;
   if (top.content.document.getElementById('navDivClone')) {
      var navDivClone = top.content.document.getElementById('navDivClone');
      if (overMenu) {
         keepNavDiv();
         return;
      }
//      top.content.status = 'hiding navDivClone found ' + movecount;
      navDivClone.style.position = "absolute";
      navDivClone.style.top = '-500px';
      navDivClone.style.left = '-500px';
      navDivClone.display = 'none';
   } else {
//      top.content.status = 'hiding navDivClone not found ' + movecount;
   }
}


// -->
