// ---------------------------------------------------------------------------
// --- Name:    Easy DHTML Treeview                                         --
// --- Original idea by : D.D. de Kerf      D.deKerf@bodegro.com            --
// --- Updated by Jean-Michel Garnier, garnierjm@yahoo.fr                   --
// ---------------------------------------------------------------------------

function toggle(node) {
	
	var nextDIV = node.nextSibling;
	
	// find the next DIV
	while(nextDIV.nodeName != "DIV") {
		nextDIV = nextDIV.nextSibling;
	}
	
	// Unfold the branch if it isn't visible
	if (nextDIV.style.display == 'none') {

		// Change the image (if there is an image)
		if (node.childNodes.length > 0) {

			if (node.childNodes.item(0).nodeName == "IMG") {
				node.childNodes.item(0).src = "minus.gif";
			}
		}

		nextDIV.style.display = 'block';
	}
	// Collapse the branch if it IS visible
	else {

		// Change the image (if there is an image)
		if (node.childNodes.length > 0) {
			if (node.childNodes.item(0).nodeName == "IMG") {
				node.childNodes.item(0).src = "plus.gif";
			}
		}
		nextDIV.style.display = 'none';
		
	}
}


function stringExtract( st, position, separator ) {
	var array;
	var result = new String('');
	var s = new String(st);
	if (s != '' ) {
		array = s.split( separator);
		result = array[position];
	}
	return result;
}


function jsTrim(TXT) {
    	return TXT.replace(/(^\s+)|(\s+$)/g,"");
}

// This function
// Parameters :
function findObj(n, d) {
  var p, i, x;
  if (!d)
    d = document;
  if ( (p=n.indexOf("?") )>0 && parent.frames.length ) {
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0,p);
  }
  if (!(x=d[n])&& d.all )
	x = d.all[n];
  for (i=0; !x && i < d.forms.length; i++)
	x = d.forms[i][n];
  for (i=0; !x && d.layers && i<d.layers.length; i++)
	x = findObj(n, d.layers[i].document);
	
  return x;
}

/* This functions returns true if the list box already contains value v */
function isInSelectInput(v, select_input) {
	for(var i=0; i<select_input.options.length; i++) {
		if (select_input.options[i].value == v) {
			return true;
		}
	}
	return false;
}

function selectOption(select_input, v_value) {
	var i, nb_item;
	nb_item = select_input.options.length;
	for (i = 0; i < nb_item ; i++) {
		if ( select_input.options[i].value == v_value )
			select_input.options[i].selected = true;
	}
}

/* This Function deletes the selected options  */
function selectRemoveSelectedOption(select_input) {
    for(var i=0; i<select_input.options.length; i++) {
        if ( select_input.options[i].selected ) {
  			select_input.options[i] = null;
   		}
	}
	select_input.selectedIndex = -1;
}

/* This Function deletes all rows  */
function selectRemoveAll(select_input) {
    var linesNumber;
    linesNumber = select_input.options.length;
    for(var i=0; i < linesNumber; i++) {
        select_input.options[i] = null;
    }
	select_input.selectedIndex = -1;
}

function buildXMLSource(xmlSource_name) {

    var obj, file_extension;
    obj = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
    obj.async = false;
    obj.resolveExternals = false;

    file_extension = stringExtract(xmlSource_name, 1, ".");
    // if there is a file extension, then load the file
    if (file_extension == "xml" || file_extension == "xslt" ) {
        obj.load(xmlSource_name);
    }
    else {
        // else load the XML String
        obj.loadXML(xmlSource_name);
    }
    
    return obj;
}

function transform(xmlSource, xsltSource) {

    var xslt;
    var xslProc, paramName, paramValue;

    // Create XLST
    xslt = new ActiveXObject("Msxml2.XSLTemplate");
    xslt.stylesheet = xsltSource;

    // Add parameters
    xslProc = xslt.createProcessor();

    xslProc.input = xmlSource;
    
    // add parameters if present
    if (arguments.length >2 && arguments.length % 2 == 0){
        for (var i=0; i < Math.floor((arguments.length)/2)-1; i++){
            paramName = arguments[2*i+2];
            paramValue = arguments[2*i+3];
            xslProc.addParameter(paramName, paramValue);
        }
    }
       
    xslProc.transform();
    return xslProc.output;
}

