/**
 * 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;

//*******************************************************************
// 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();
		handleKeyPress( k.keyCode );
	}
}

//*******************************************************************
// 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 false;
}

//*******************************************************************
// 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;
	}
}

// -->
