/**
 * $Header: //depot/Projects/CIMIT/website/web/lib/leftNavOver.js#12 $
 *
 * COPYRIGHT:
 *
 * This software is Copyright (c) 2006-2008 Marc Davignon
 *                         <mpdavig@users.sourceforge.net>
 *
 * LICENSE:
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of Version 2 of the GNU General Public License as published by the
 * Free Software Foundation.
 *
 * The functions contained within this document replace the standard MacroMedia
 * (now Adobe) JavaScript functions. The renamed functions will be ignored by
 * Dreamweaver and not be re-added when updating the templates. In particular
 * <body onload="MM_preloadImages()"> will not be added.
 */


/**
 * The JavaScript Anthology Pg. 15
 * Allows any number of load event handlers:
 *  addLoadListener(firstFunction);
 *  addLoadListener(secondFunction);
 *  addLoadListener(twentyThirdFunction);
 */
function addLoadListener(fn) {
  if (typeof window.addEventListener != 'undefined') {
    window.addEventListener('load', fn, false);
  } else if (typeof document.addEventListener != 'undefined') {
    document.addEventListener('load', fn, false);
  } else if (typeof window.attachEvent != 'undefined') {
    window.attachEvent('onload', fn);
  } else {
    var oldfn = window.onload;
    if (typeof window.onload != 'function') {
      window.onload = fn;
    } else {
      window.onload = function() {
        oldfn();
        fn();
      };
    }
  }
} // function addLoadListener(fn)


/**
 *
 */
function leftNavOverTest(){
  return true;
} // function leftNavOverTest()


/**
 *
 */
function swfobjectTest(){
  return true;
} // function swfobjectTest()


/**
 *
 */
function MPD_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MPD_p) d.MPD_p=new Array();
    var i,j=d.MPD_p.length,a=MPD_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MPD_p[j]=new Image; d.MPD_p[j++].src=a[i];}}
} // function MPD_preloadImages()


/**
 *
 */
function MPD_swapImgRestore() { //v3.0
  var i,x,a=document.MPD_sr;
  for (i=0; a && i<a.length && (x=a[i]) && x.oSrc;i++) {
    x.src=x.oSrc;
    MPD_matchParent(a[i].id, "restore");
  }
} // function MPD_swapImgRestore()


/**
 *
 */
function MPD_findObj(n, d) { //v4.01
  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=MPD_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
} // function MPD_findObj(n, d)


/**
 *
 */
function MPD_swapImage() { //v3.0
  var i,j=0,x,a=MPD_swapImage.arguments;
  document.MPD_sr=new Array;
  for (i=0;i<(a.length-2);i+=3) {
   if ((x=MPD_findObj(a[i]))!=null) {
    document.MPD_sr[j++]=x;
    if(!x.oSrc) x.oSrc=x.src;
    // Use path information from the original image to determine swap image location.
    // In other words, any part of the swap image path can be crap, except for the image name.
    // This does however require the original path to be correct and both images be located in the same location.
    var swapImg = x.src.substring(0, x.src.lastIndexOf('/')) + a[i+2].substring(a[i+2].lastIndexOf('/'));
    x.src=swapImg;
    MPD_matchParent(a[i], "swap");
   }
  }
} // function MPD_swapImage()


/**
 *
 */
function MPD_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MPD_pgW=innerWidth; document.MPD_pgH=innerHeight; onresize=MPD_reloadPage; }}
  else if (innerWidth!=document.MPD_pgW || innerHeight!=document.MPD_pgH) location.reload();
} // function MPD_reloadPage(init)
MPD_reloadPage(true);


/**
 *
 */
function MPD_goToURL() { //v3.0
  var i, args=MPD_goToURL.arguments; document.MPD_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
} // function MPD_goToURL()


/**
 * This script is intended to extend some standard MacroMedia (now Adobe)
 * JavaScript functions.
 *
 * Using the example below:
 *
 *  <li class="leftnav grayborder">
 *   <a href="about-keyfacts.html" class="leftnavdarkblue"  onmouseover="MPD_swapImage('sidebarimage1','','images/arrowbluesmall.gif',1);" onmouseout="MPD_swapImgRestore()">
 *    <div class="inlineText">Key Facts</div>
 *    <img src="images/arrowgraysmall.gif" class="arrowsmall" id="sidebarimage1" />
 *   </a>
 *  </li>
 *
 * We will add the ability to:
 *  1. Match the "img" tag by the given "id" value.
 *  2. Find the "img" tag parent and check for an "a" tag name.
 *  3. Parse the "a" tag class name to determine the color.
 *  4. Find the "a" tag parent and check for an "li" tag name.
 *  5. Swap the "li" tag class border value.
 */
function MPD_matchParent(id, action) {
    var i, newClassValue = [], startElement = document.getElementById(id);
    var aParent = startElement.parentNode;
    if (aParent.nodeName.toLowerCase() == "a") {
	// Dynamically obtain the new border color
	var matches = aParent.className.match(/^leftnav(.*)$/);
	if (matches && matches[1] != 'undefined') {
	    var newBorder = matches[1] + "border";
	    var liParent = aParent.parentNode;
	    if (liParent.nodeName.toLowerCase() == "li") {
		// Only modify the border class, leave all others alone
		var classes = liParent.className.split(" ");
		for (i=0; i < classes.length; i++) {
		    if (/^.*border$/.test(classes[i])) {
			if (action == "swap") {
			    newClassValue[i] = newBorder;
			    // Save the old border color for "restore"
			    document.MPD_oldBorder = classes[i];
			} else if (action == "restore") {
			    newClassValue[i] = document.MPD_oldBorder;
			}
		    } else {
			newClassValue[i] = classes[i];
		    }
		}
		liParent.className = newClassValue.join(" ");
		//alert(liParent.className); // Testing
	    }
	}
    }
} // function MPD_matchParent(id, action)

var MPD_oldClass;

function MPD_switchClasses(divId, classToAdd) {
  if (document.getElementById(divId)) {
    var divObject = document.getElementById(divId);
    if (classToAdd) {
      MPD_oldClass = divObject.className;
      divObject.className = divObject.className + " " + classToAdd;
    } else {
      divObject.className = MPD_oldClass;
    }
  }
}


/**
 * Donation page functions / variables
 *
 * For CIMIT do not hide but disable the input text box
 */
var storeInputValue=new Array();

function enableInputText(inputID, textID) {
    // Check the status of the controlling input element
    if (document.getElementById(inputID).checked) {
	// Return the stored value, should support multiple hidden input fields
	if (storeInputValue[textID] && storeInputValue[textID].length) { document.getElementById(textID).value = storeInputValue[textID]; }
	//document.getElementById(textID).style.display = "";
	document.getElementById(textID).disabled = false;
    } else {
	disableInputText(textID);
    }
} // function enableInputText(inputID, textID)


function disableInputText(textIDList) {
    var textID = textIDList.split(",");
    for (var ti=0;ti < textID.length;ti++) {
        if (document.getElementById(textID[ti])) {
	    // Only store the value when last status was display
	    //if (document.getElementById(textID[ti]).style.display == "") {
	    if (document.getElementById(textID[ti]).disabled == false) {
                storeInputValue[textID[ti]] = document.getElementById(textID[ti]).value;
	    }
            // Disable the given input text element
            //document.getElementById(textID[ti]).style.display = "none";
            document.getElementById(textID[ti]).disabled = true;
            document.getElementById(textID[ti]).value = "";
        }
    }
} // function disableInputText(textIDList)


/**
 * Only allow numeric keys to be pressed, including the 101+ keyboard number
 * pad keys while retaining special keys like backspace, tab, home, end, arrows,
 * etc.
 *
 * This particular function version also allows commas and periods:
 * commas: || key == 188 || key == 44
 * periods: || key == 190 || key == 46 || key == 110
 *
 * Note: "key == 0" is required for onkeypress which struggles with special keys
 */
function checkForNumeric(e) {
    var key = window.event ? e.keyCode : e.which;

    //alert("'" + key + "'"); // Testing
    if (e.shiftKey == 1) {
        return key == 0
            || key == 9
    } else {
        return key == 0
            || key == 8
            || key == 9
            || (key > 34 && key < 41)
            || (key > 47 && key < 58)
            || (key > 95 && key < 106)
            || key == 190 || key == 46 || key == 110
    }
} // function checkForNumeric(e)


/**
 * Function for submitting forms using javascript
 * Parameters:
 *  form - name of form to submit
 *  submitvalue - sets a value to differentiate the type of submit
 */
function submitform(form, submitvalue) {
    // Hidden arguments 3 and 4 ([2] and [3]) and beyond set a form element by
    // id and value before the form is submitted.

    if (typeof document.forms[form][0].length != 'undefined') {
        // Get the form by "name", Opera and IE will return an array of forms
        // with the same name.
        formToSubmit = document.forms[form][0];
        //alert('first'); // Testing
    } else if (typeof document.forms[form].length != 'undefined') {
        // Get the form by "name", Firefox will only return the first form with
        // the matching name even if there is more than one.
        formToSubmit = document.forms[form];
        //alert('second'); // Testing
    } else {
        // Get the form by "id"
        formToSubmit = document.getElementById(form);
        //alert('third'); // Testing
    }

    for (var i=2;i < arguments.length;i=i+2) {
        //alert(arguments[i] + " " + arguments[i+1]); // Testing
        if (arguments[i] != null && arguments[i+1] != null)

            if (formToSubmit.elements[arguments[i]]) {
                // Look for elements by "name" within the given form "name"
                //alert('Trying to use element found by "name"!'); // Testing
                formToSubmit.elements[arguments[i]].value = arguments[i+1];
            } else if (document.getElementById(arguments[i]) && formToSubmit.elements[document.getElementById(arguments[i]).name]) {
                // Look for elements by "id" within the entire document
                // IE6 does not allow you to search through the form in this way
                // Second: Make sure the found element is actually in the form
                //alert('Trying to use element found by "id"! ' + document.getElementById(arguments[i]).name); // Testing
                document.getElementById(arguments[i]).value = arguments[i+1];
            } else {
                //alert('Trying to create new element!'); // Testing
                var newInput = document.createElement("input");
                newInput.setAttribute("type", "hidden");
                newInput.setAttribute("id", arguments[i]);
                newInput.setAttribute("name", arguments[i]);
                newInput.setAttribute("value", arguments[i+1]);
                formToSubmit.appendChild(newInput);
            }
    }
    // Always create a fake submitvalue button
    var hiddenSubmit = document.createElement("input");
    hiddenSubmit.setAttribute("type", "hidden");
    hiddenSubmit.setAttribute("name", "submitvalue");
    hiddenSubmit.setAttribute("value", submitvalue);
    formToSubmit.appendChild(hiddenSubmit);
    // Actually submit the form
    formToSubmit.submit();
} // function submitform(form, submitvalue)
