// $Id: urpm.js 1103 2008-05-09 10:06:09Z dmf $
//Modified the initLists and fillBaseList functions to handle values in addition to text - Aug, 2001
//Added the getDefaultListItems() function - Aug 10, 2001

var v = false;

function get(elt)
{
   // loop thru document.forms property and exit w/ current element index

   var num = -1;
   for (var i = 0; i < elt.form.elements.length; i++) {
      if (elt.form.elements[i].name == elt.name) {
        num = i;  // save element index
        break;
      }
   }
   return num;  // returns current element index
}


function sindex(num, offset, elt)
{  //***modified next line to get the right element index***
   // sel finds selected index or value of num + offset's form element
   var sel = elt.form.elements[num+offset].selectedIndex;
   if (sel < 0) sel = 0;

   return sel;
}

function relate(elt, tree, depth)
{ // relate submenus based on sel of form - calls update to redef submenus
   if (v) {
      var num = get(elt); // fetch the current elt index
      var a = tree;        // set a to be the tree array

      while (a != null && --depth != -1) {
        // traverse menu tree until we reach the corresponding option record
        a = a[sindex(num, -depth, elt)];
      }

      // at depth 3, should end up w/ something like a[i][j][k]
      // where each index holds the value of s(elected )index in related form select elts
      if (a != null && a.length) {
         // if a array exists and it has elements,
         // feed update() w/ this record reference
         update(num, elt, a);
         return;
      }
   }
   // if a hasn't any array elements or new Option unsupported then end up here ;)
   //jmp(form, elt); // make like a live popup
}


function update(num, elt, m)
{
   // updates submenus - element(num)'s menu options, and all submenus
   // if refd element exists

   if (num != -1) {
	  do {
	      ++num;
      // reference the very next SELECT form element (warrenski modified this code from assuming it followed in HTML)
      //***modified next line to refer to elements within the same form***
	  } while (!elt.form.elements[num].type || elt.form.elements[num].type.toString().substr(0,6) != "select");

      with (elt.form.elements[num]) {

	     // null out options in reverse order (bug work-around)
         for (var i = options.length - 1; 0 < i; i--) options[i] = null;

         // fill up next menu's items
         for (var i = 0; i < m.length; i++)
           options[i] = (m[i].value != null) ? new Option(m[i].text, m[i].value) : new Option(m[i].text);

         options[0].selected = true; // default to 1st menu item, windows bug kludge
         options[0].defaultSelected = true;
      }
      if (m[0].length != 0) {
         update(num, elt, m[0]); // update subsequent form if any grandchild menu exists
      }
    }
}

// Internet Explorer 4+ bug fix:
// IE4+ remembers the index of each SELECT but NOT the CONTENTS of each SELECT,
// so it gets it wrong.

function resetIE() {
   for (var i = 0; i < document.forms.length; i++) {
      document.forms[i].reset();
   }
}

if (document.all)
   window.onload = resetIE;

function initLists() {

  if (!v) return false;

  var args = initLists.arguments;
  var argLen = args.length - 1;
  var temp = new Array();
  var baseList = args[0];

  fillBaseList( baseList, args[1] );

  //this loop iterates through each 2nd argument (arrays)
  for(var arg=argLen; arg>2; arg-=2) {
    var list = args[arg-1];
    var a = args[arg];
    var newSubI = 0, oldSubI = 0;
    var subMenu = null;

    for(var i=0; i<a.length;i++) {
     if (a[i].length) {
        //create a new Option with the appropriate submenu
        subMenu = arg==argLen ? null : m[oldSubI++];
        temp[temp.length] = typeof(a[i]) == "object"
                            ? new O(a[i][0], a[i][1], subMenu )
           	                : new O(a[i]   , null   , subMenu );
	}
      else {
//        temp = temp.sort(compare);
        m[newSubI++] = temp;
        temp = new Array();
      }
    }
    //reset the length of array
    m.length = newSubI;
  }

  //set up the listboxes
  relate(baseList, m, 1);

  return true;
}

function fillBaseList(elt, a) {
  if (v) {
    //create a new Option for each array element
    for(var i=0; i<a.length; i++)
    {
       a[i] = typeof(a[i]) == "object" ? new O( a[i][0], a[i][1], null ) : new O( a[i], null, null );
    }
    //fill the list
    // null out options in reverse order (bug work-around)
    for (var i = elt.options.length - 1; 0 < i; i--)
      elt.options[i] = null;

    // fill up menu's items
    for (var i = 0; i < a.length; i++)
      elt.options[i] = a[i].value != null
                       ? new Option(a[i].text, a[i].value)
                       : new Option(a[i].text);

    elt.options[0].selected = true; // default to 1st menu item, windows bug kludge
    elt.options[0].defaultSelected = true;
  }
}

//this function is used to size the listboxes in Netscape
function getDefaultListItems()
{ 
//  alert (navigator.appName); Opera lies even to JavaScript!
//  if (v && navigator.appName == 'Netscape')
  {
    var options = ""
    for (var i=0; i<10; i++) options += "<option>......................................</option>";

    return options;
  }
}

// Internet Explorer 4+ bug fix:
// IE4+ remembers the index of each SELECT but NOT the CONTENTS of each SELECT,
// so it gets it wrong.
//
// Thanks to Peter Belesis (pbel@internet.com) for pointing this out.

function resetIE() {
   for (var i = 0; i < document.forms.length; i++) {
      document.forms[i].reset();
   }
}


// DMF's presetting functions

function findIndex (elt, selectedValue) {
	var num = -1;
	for (var i = 0; i < elt.length; i++) {
		if (elt.options[i].value == selectedValue) {
			num = i;  // save element index
			break;
		}
	}
	return num;  // returns current element index
}

// Umbrella presetting function.  Needs some global variables
// since setTimeout operates from the perspective of the window object
// so the arguments to the function have to get parked somewhere.
// The need for a delay after onLoad and after the first relate
// cropped up in NS6 where the deselection of options[0] fails
// completely if you do it too quickly after populating the select.
fE = sE = new Object;
var fV, sV;
function presetURPM (firstElt, firstVal, secondElt, secondVal) {
	if (typeof firstVal != "object") {
		presetURPMsingle (firstElt, firstVal, secondElt, secondVal)
	} else {
		fE = firstElt;
		sE = secondElt;
		fV = firstVal;
		sV = secondVal;
		setTimeout("presetURPMmulti1 (fE, fV, sE, sV)", 100);
	}
}

// Handles presetting for single selection boxes
function presetURPMsingle (firstElt, firstVal, secondElt, secondVal) {
	var firstIndex = findIndex(firstElt, firstVal);
	if (firstIndex > -1) {
		firstElt.options[0].selected = false; 
		firstElt.options[0].defaultSelected = false; 
		firstElt.options[firstIndex].selected = true;
		firstElt.options[firstIndex].defaultSelected = true;
		relate (firstElt, m, 1);
		var secondIndex = findIndex(secondElt, secondVal);
		if (secondIndex > -1) { 
			secondElt.options[0].selected = false; 
			secondElt.options[0].defaultSelected = false; 
			secondElt.options[secondIndex].selected = true; 
			secondElt.options[secondIndex].defaultSelected = true; 
		}
	}
}

// Does (possibly) multiple selections in the first box and, if
// required, schedules execution of an update for the second box.
function presetURPMmulti1 (firstElt, firstVal, secondElt, secondVal) {
	for (var i=0; i < firstVal.length; i++) {
		var firstIndex = findIndex(firstElt, firstVal[i]);
		if (firstIndex > -1) { 
			firstElt.options[firstIndex].selected = true; 
			firstElt.options[firstIndex].defaultSelected = true; 
		}
	}
	if (firstIndex) { 
		firstElt.options[0].selected = false; 
		firstElt.options[0].defaultSelected = false; 
	}
	if (firstVal.length == 1) {
		relate (firstElt, m, 1);
		setTimeout("presetURPMmulti2 (sE, sV)", 100);
	} else {
		update(get(firstElt), firstElt, m[0]);
	}
}

// Does (possibly) multiple selections in the second box
// Executed, after a delay, by presetURPMmulti1
function presetURPMmulti2 (secondElt, secondVal) {
	var secondIndex;
	for (var i=0; i < secondVal.length; i++) {
		secondIndex = findIndex(secondElt, secondVal[i]);
		if (secondIndex > -1) { 
			secondElt.options[secondIndex].selected = true; 
			secondElt.options[secondIndex].defaultSelected = true; 
		}
	}
	if (secondIndex) { 
		secondElt.options[0].selected = false; 
		secondElt.options[0].defaultSelected = false; 
	}
}

if (document.all)
   window.onload = resetIE;

