/********** GLOBAL VARIABLES ********************/
var formUpdateWindow = null;
var disablePopups = false;    //if a form is disabled, also disable the popups


/********** ADDS OPTION TO THE OTHER SELECT BOX ********************/

function addItem(theElementFrom,theElementTo) {
	for (i=0; i < theElementFrom.options.length; i++) {
		if(theElementFrom.options[i].selected == true) {
			var theValue = theElementFrom.options[i].value;
			var theText = theElementFrom.options[i].text;
			theElementTo.options[theElementTo.options.length] = new Option(theText, theValue);
			theElementFrom.options[i] = null;
			i--;
		} // end if
	} // end for
} // end addItem()



/********** ADDS MULTIPLE OPTIONS TO THE OTHER SELECT BOX ********************/
function addMultipleItems(theElementFrom,theElementTo) {
	var theStorage = new Array(theElementFrom.options.length);
	var k = 0;
	for (i=0; i < theElementFrom.options.length; i++) {
		if(theElementFrom.options[i].selected == true) {
			theStorage[k] = new Array();
			theStorage[k][0] = theElementFrom.options[i].value;
			theStorage[k][1] = theElementFrom.options[i].text;
			k++;
			theElementFrom.options[i--] = null;
		} // end if
	} // end for
	for (j=0; j < k; j++) {
		theElementTo.options[theElementTo.options.length] = new Option(theStorage[j][1], theStorage[j][0]);
	}
} // end addMultipleItems()


/********** DELETES OPTION FROM THE SELECT BOX ********************/
function removeItem(theElementFrom,theElementTo) {
	for (i=0; i < theElementFrom.options.length; i++) {
		if(theElementFrom.options[i].selected == true) {
			var theValue = theElementFrom.options[i].value;
			var theText = theElementFrom.options[i].text;
			theElementTo.options[theElementTo.options.length] = new Option(theText, theValue);
			theElementFrom.options[i] = null;
			i--;
		} // end if
	} // end for
} // end removeItem()


/********** MOVES OPTION UP IN THE SELECT BOX ********************/
function moveUp(theElement) {
	// break if already at the top index
	if (theElement.options[0].selected == true) return;

	for (i=0; i < theElement.options.length; i++) {
		if(theElement.options[i].selected == true) {
			var tempValue = theElement.options[i].value;
			var tempText = theElement.options[i].text;

			// execute the swap
			theElement.options[i].value = theElement.options[i-1].value;
			theElement.options[i].text = theElement.options[i-1].text;
			theElement.options[i-1].value = tempValue;
			theElement.options[i-1].text = tempText;
			
			// put focus on the swapped item and break
			theElement.selectedIndex = i-1;
			return;
		} // end if
	} // end for
} // end moveUp()


/********** MOVES OPTION DOWN IN THE SELECT BOX ********************/
function moveDown(theElement) {
	// break if already at the bottom index
	if (theElement.options[theElement.options.length-1].selected == true) return;
	
	for (i=0; i < theElement.options.length; i++) {
		if(theElement.options[i].selected == true) {
			var tempValue = theElement.options[i].value;
			var tempText = theElement.options[i].text;

			// execute the swap
			theElement.options[i].value = theElement.options[i+1].value;
			theElement.options[i].text = theElement.options[i+1].text;
			theElement.options[i+1].value = tempValue;
			theElement.options[i+1].text = tempText;
			
			// put focus on the swapped item and break
			theElement.selectedIndex = i+1;
			return;
		} // end if
	} // end for
} // end moveDown()


/***** MAKES SELECTED OPTION THE PRIMARY VALUE IN THE SELECT BOX *****/
function makeSelectedPrimary(theElement) {
	// break if already at the top index
	if (theElement.options[0].selected == true) {
		alert('Selected Item is already Primary');
		return;
	} // end if

	for (i=1; i < theElement.options.length; i++) {
		if(theElement.options[i].selected == true) {
			// move selected option's value and text to temp variable
			var tempValue = theElement.options[i].value;
			var tempText = theElement.options[i].text;

			for (var j=i-1; j>-1; j--) {
				// execute the swap
				theElement.options[i].value = theElement.options[j].value;
				theElement.options[i].text = theElement.options[j].text;
				i--;
			} // end nested for

			// move original selected value and text into slot 0 and focus
			theElement.options[0].value = tempValue;
			theElement.options[0].text = tempText;
			theElement.selectedIndex = 0;
			return;
		} // end if
	} // end for
} // end makeSelectedPrimary()


/********** OPEN NEW POPUP WINDOW ********************/
function openPopup(theURL, divName, fieldName, w, h) {

	//exist early if this is a disabled form
	if(disablePopups == true) return false;

	//set global variables in this window that will be accessible by the window being opened
	divToUpdate = eval(divName);
	fieldToUpdate = eval(fieldName);
	theLocation = "";

	//because IE caches popups and ignores HTTP header requests not to cache, trick them by appending a query string
	var now = new Date();
	var x = now.getTime();
	if(theURL.indexOf('?') == -1) theURL=theURL+"?ignoreDateStamp="+x;
	else theURL=theURL+"&ignoreDateStamp="+x;

    //set window dimensions and placement--arbitrary--change if desired
	var x=10,y=10;

	//RULE:  Only allows one child window to be opened by the current window
	if (formUpdateWindow!=null) {
		if(formUpdateWindow.closed==true) formUpdateWindow=window.open(theURL,"","top="+y+",left="+x+",width="+w+",height="+h+theLocation+",scrollbars=no");
		else formUpdateWindow.focus();
	}
    else formUpdateWindow=window.open(theURL,"","top="+y+",left="+x+",width="+w+",height="+h+theLocation+",scrollbars=no");
} // end openPopup()


/*********** HIGHLIGHT TABLE ROW ON MOUSEOVER ************/
function selectRow(src,hiLiteColor) {
	if (!src.contains(event.fromElement)) {
		src.bgColor = hiLiteColor;
	}
}

/************ DESELECT TABLE ROW ON MOUSEOUT *************/
function deselectRow(src,originalColor) {
	if (!src.contains(event.toElement)) {
		src.bgColor = originalColor;
	}
}


/********** CLOSE CURRENT OPEN WINDOW ********************/
function closeWin() {
	if (!arguments[0]) window.close();
	else {
		var theWindow = eval(arguments[0]);
		theWindow.close();
	}
} // end closeWin()


/********** CANCELS ACTION ********************/
function cancelForm() {
	var cancelMessage = "Are you sure want to cancel? All data will not be saved.";
	if (confirm(cancelMessage))	history.go(-1);
} // end cancelForm()


/********** CANCELS POPUP *********************/
function cancelPopup() {
	var cancelMessage = "Are you sure want to cancel? All data will not be saved.";
	if (confirm(cancelMessage))	top.window.close();
} // end cancelPopup()


/** DELETES FIRST OPTION IN SELECT FIELD **
There is an empty option in certain select fields. This empty option is there
to expand the field to the proper size because NN does not do this by default.
IE can do this with CSS. As soon as the page is loaded, this function is called
and the empty option is deleted. The select field then retains the size that
it was drawn in at the page load. :ML:
*/

function clearSelect(selectName) {
	selectName.options[0] = null;
} // end clearSelect()

// CHECKS ALL CHECKBOXES ON A FORM, MUST HAVE FORM NAME AND ARRAY OF CHECKBOXES NAME	

function checkAll(field)
	{
	for (i = 0; i < field.length; i++)
		field[i].checked = true ;
	}


// #######################################################################
// ###################### MULTIPLE SELECT BOX MOVER ######################
// #######################################################################

function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function moveOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }

  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }

}

function refresh()
{
 
    window.location.reload( false );
}

/******* REMOVE APPLICATION_SPECIFIC FUNCTIONS FROM GLOBAL STYLESHEET *******/
// OPENS DIFFERENT POPUPS, BASED ON USER CHOICE OF INSTALLMENT OPTIONS FOR BILLING

function showInstallTable(arg) {
	
		if ( arg == 'On') {
			document.getElementById('installmentTable').style.display = 'block';
		}
		else if ( arg == 'Off'){
			document.getElementById('installmentTable').style.display = 'none';		
		}
}

function installmentChoice() {

	var inst1 = document.getElementById('installments1');
	var inst2 = document.getElementById('installments2');
	var inst3 = document.getElementById('installments3');

	for (i = 1; i <= 3; i++) {
		//no installs
		if(inst1.checked == true) {
			openPopup('invoiceItem_createNew1.html','','',850,490);
		}
		//entire form
		else if(inst2.checked == true) {
			openPopup('invoiceItem_createNew2.html','','',850,490);
		}
		//indiv. line items
		else if(inst3.checked == true) {
			openPopup('invoiceItem_createNew3.html','','',850,490);
		}
	}
}
