/**************************************************************************
	File: move_node.js
	
	Purpose: 
		Move DOM nodes up and down, above or below like elements. 
		This is good for moving table rows up or down.

	Parameters:
		nodeRef		either an element reference, or it's ID
		direction	"up" or "down"
		stopId		optional...the ID of a like element that you do not want
					to move above or below.
		swapClass	optional - if set to true, then the classes of the node moving, and the one below or above will be swapped
		resetHTML	optional - if set to true, then the node's parentNode will have it's innerHTML property set to itself. This will
					reorder the nodes so that form fields can post in the order that they appear in the HTML. This will reset any field values
					that were entered after the page loaded.
					
	History:
		File added on 01/21/2005 for v2.0.0
	
**************************************************************************/

function moveNode(moveNode, direction, stopId, swapClass) {

	// first, grab the reference to the node that we will move, if not specified
	if (typeof (moveNode) == 'text') {
		moveNode = document.getElementById('nodeRef');
	}
	
	if (moveNode) {
		/******************************************
			Move up
		******************************************/
		if (direction == 'up' && moveNode.previousSibling) {		
			// grab ref to element above this one
			prevSib		= getPreviousElement(moveNode);
			
			if (prevSib) {
				if (stopId) {
					if (prevSib.id == stopId) var dontMove = true;
				}
				
				if (!dontMove) {
					// grab containing element
					parentNode	= moveNode.parentNode;
				
					// remove the node that we are going to move
					removedNode	= parentNode.removeChild(moveNode);
				
					// insert it above it's prev. sib
					if (parentNode.insertBefore(removedNode, prevSib)) {
						// swap class, if passed
						if (swapClass) {
							var oldClass = removedNode.className;
							removedNode.className =prevSib.className;
							prevSib.className = oldClass;						
						}

						return true;
						// reset innerHTML so browsers other than IE will pass the fields to the server in the order they are laid out on the page
						//parentNode.innerHTML = parentNode.innerHTML;
					}
				}
			}
		/******************************************
			Move down
		******************************************/
		} else if(direction == 'down' && moveNode.nextSibling) {

			// grab ref to element below this one
			//nextSib		= moveNode.nextSibling.nodeName == moveNode.nodeName ? moveNode.nextSibling : moveNode.nextSibling.nextSibling;
			nextSib = getNextElement(moveNode);
			
			if (nextSib) {
				if (stopId) {
					if (nextSib.id == stopId) var dontMove = true;
				}
				
				if (!dontMove) {
					// grab containing element
					parentNode	= moveNode.parentNode;
				
					// grab the element we will insert before
					beforeNode = getNextElement(nextSib);
					
					if (beforeNode) {
						// remove the node that we are going to move
						removedNode	= parentNode.removeChild(moveNode);
					
						// swap class, if passed
						if (swapClass) {
							var oldClass = removedNode.className;
							removedNode.className =nextSib.className;
							nextSib.className = oldClass;						
						}

						parentNode.insertBefore(removedNode, beforeNode);
						return true;
					} else {
						// remove the node that we are going to move
						removedNode	= parentNode.removeChild(moveNode);
						parentNode.appendChild(removedNode);
						return true;
					} 
				}
			}
			
		}
	}
	
}


/*********************************************************************
	This function takes a form field, and swaps its ID, name, and Value
	with either the field above or below it. This function was
	written to be called right after moveNode is called, in order
	to properly track position of fields in Safari :(
	
	Parameters:
		elemRef			ref. to field element OR it's ID
		direction		up/down (or FALSE if targetField is sent)
		targetField 	optional...ref. to target field, OR it's ID
*********************************************************************/
function swapValues(elemRef,direction,targetField) {
	if (typeof(elemRef) == "string") elemRef = document.getElementById(elemRef);
	
	// set the targetField
	if (targetField) {
		if (typeof(targetField) == "string") targetField = document.getElementById(targetField);
	} else {
		var targetField = direction == 'down' ? getNextElement(elemRef) : getPreviousElement(elemRef);
	}
	
	var srcVal 		= elemRef.value;
	var srcId 		= elemRef.id;
	var srcName		= elemRef.name
	var targetVal 	= targetField.value;
	var targetId 	= targetField.id;
	var targetName 	= targetField.name;

	// this is for Safari...it destroys the targetField if elemRef.id is set to it's id and they are both the same
	targetField.id 		= 'temporayblahblah';
	
	elemRef.value 		= targetVal;
	elemRef.id 			= targetId;
	elemRef.name		= targetName;
	
	targetField.value 	= srcVal;
	targetField.id 		= srcId;
	targetField.name	= srcName;
}





/******************************************************************************
	Function trackOptionPos()
	
	Purpose: 
		Used when editing records that have product options. Tracks the
		position of the option using hidden <input> fields that correspond
		to each option.
	
	Parameters:
		fieldRef		reference or id of the hidden field
		direction		up/down/bottom - corresponds to the direction that
						the option was just moved. "bottom" is used when the
						option is chosen, as it is moved to the last position
	
******************************************************************************/
function trackOptionPos(fieldRef, direction) {
	
	// grab ref. to field, if the ID was passed
	if (typeof(fieldRef) == "string") fieldRef = document.getElementById(fieldRef);
		
	var foundTarget = false, firstRun = true, targetRef;
	
	// if the "state" attribute is not set to ON to indicate that
	// it is visibile on the page, then keep iterating up
	// until we find the 1st field that does have state set to ON
	while(!foundTarget) {
		
		checkField = firstRun? fieldRef : targetFieldRef;
		
		// grab the field either above, below, or at the bottom of the div this one, depending on the direction var.
		if (direction == "up") {
			targetFieldRef = getPreviousElement(checkField);
		} else if (direction == "down") {
			targetFieldRef = getNextElement(checkField);
		} else if (direction == "bottom") {
			
			//targetFieldRef = firstRun ? getLastChildElement(fieldRef.parentNode) : getPreviousElement(checkField);
			// if moving to the bottom, we need to swap values on every field from the current one down
			
			while(swapField = getNextElement(checkField)) {
				swapValues(checkField,false,swapField);
				checkField = swapField;
			}
			return;
		}
		
		if (targetFieldRef) {
			if (targetFieldRef.getAttribute('state') == "on") foundTarget = true;
		} else {
			break;
		}
		
		firstRun = false;
	}
	

	if (targetFieldRef) {
		swapValues(fieldRef, false, targetFieldRef);
		// set state attribute to ON, just in case it isn't there or set to off
		targetFieldRef.setAttribute('state', 'on');
		// set field name
		targetFieldRef.name = 'options[positions][]';
	}
}