/*
 * Copyright (c) 2006-2007 Nokia Corporation.
 *
 * This material, including documentation and any related computer programs,
 * is protected by copyright controlled by Nokia. All rights are reserved.
 * Copying, including reproducing, storing, adapting or translating,
 * any or all of this material requires the prior written consent of Nokia.
 * This material also contains confidential information, which may not be
 * disclosed to others without the prior written consent of Nokia.
 *
 * DESCRIPTION
 * -------------------------------------------------------------------------
 * The is the main/global javascript used for the NGI project. 
 * -------------------------------------------------------------------------
 */


/*
 * STATIC VARIABLES
 */

// Content Column Resize
var CONTENT_COLUMNS = new Array("contentColumn01", "contentColumn02", 
							"contentColumn03", "contentColumn04");
var CONTENT_COLUMN_WIDTH = 184;  // Pixels
var CONTENT_COLUMN_SEPARATOR_WIDTH = 10; // Pixels


/*
 * Function: ngiStart
 * Description:
 *   Called by the body on load. All functions that needs to run on load should
 *   be called within this function.
 */
function ngiStart() {
}

/*
 * Function: ngiContentColumnResize
 * Description:
 *   This will add a maximum with to the content column elements based on which
 *   of the four columns exists.
 * Assumptions:
 *   This assumes that the following static variables are within this JS file:
 *     CONTENT_COLUMNS[]
 *     CONTENT_COLUMN_WIDTH
 *     CONTENT_COLUMN_SEPARATOR_WIDTH
 */
function ngiContentColumnResize() {
  // Loop through the CONTENT_COLUMNS ARRAY
  var maxWidth = CONTENT_COLUMN_WIDTH;
  var lastId = -1;
  for(var i=0; i<CONTENT_COLUMNS.length; i++) {
    if(document.getElementById(CONTENT_COLUMNS[i])) {
      if(lastId >= 0) {
        document.getElementById(CONTENT_COLUMNS[lastId]).style.width = (maxWidth + "px");
        maxWidth = CONTENT_COLUMN_WIDTH;
      }

     lastId = i;

    }
    else if(lastId >= 0) {
      maxWidth += CONTENT_COLUMN_SEPARATOR_WIDTH;
      maxWidth += CONTENT_COLUMN_WIDTH;
    }
    // Give the last column a width
    if(lastId >= 0) {
      document.getElementById(CONTENT_COLUMNS[lastId]).style.width = (maxWidth + "px");
    }

    //alert(document.getElementById(CONTENT_COLUMNS[lastId]).style.height);
  }
}

/*
 * Function: ngiClearTextBox
 * Description:
 *   This function removes the value of a given text box if the value is equal
 *   to a parameter value.
 * Parameters:
 *   valueCheck - The value to check. If this value equals the current value
 *                clear the text box.
 */

function ngiClearTextBox(valueCheck, elementId) {
  // Check if the calling element has a "value" property
  if(document.getElementById(elementId) && document.getElementById(elementId).value == valueCheck) {
    document.getElementById(elementId).value = '';
  }
}

/*
 * Function: ngiToggleElement
 * Description:
 *   This function toggles the display properties of an element from none to inline
 * Parameters:
 *   elementId - The ID of the element to toggle
 */

function ngiToggleElement(elementId) {
  if(document.getElementById(elementId).style.display) {
    if(document.getElementById(elementId).style.display == 'none')
      document.getElementById(elementId).style.display = 'inline';
    else
      document.getElementById(elementId).style.display = 'none';
  }
  else
    document.getElementById(elementId).style.display = 'inline';
}

/* Function: ngiAddLoadEvent
 * Description:
 *   This function is used to manage event functions
 * Parameters:
 *   fn - Function to be added
 */
function ngiAddLoadEvent(fn) {
  if(typeof(fn) != "function")return;
  var tempFunction = window.onload;
  window.onload=function() {
    if(typeof(tempFunction)=="function")tempFunction();
    fn();
  }
}

function ngiChangeInputType(
  oldElm, // a reference to the input element
  iType, // value of the type property: 'text' or 'password'
  iToggle, // true to toggle between text and password
  iValue, // the default value, set to 'password' in the demo
  blankValue, // true if the value should be empty, false otherwise
  noFocus, // set to true if the element should not be given focus
  currentStyle,
  styleStringFocus,
  styleStringBlur) { //string of style
  if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
    !document.getElementById || !document.createElement) return;
  var newElm = document.createElement('input');
  newElm.type = iType;
  if(oldElm.name) newElm.name = oldElm.name;
  if(oldElm.id) newElm.id = oldElm.id;
  if(oldElm.className) newElm.className = oldElm.className;

  //if(oldElm.value) newElm.value = oldElm.value;
  //if(oldElm.value) newElm.text = oldElm.text;

  if(oldElm.size) newElm.size = oldElm.size;
  if(oldElm.tabIndex) newElm.tabIndex = oldElm.tabIndex;
  if(oldElm.accessKey) newElm.accessKey = oldElm.accessKey;
  if(currentStyle == "focus") {
    if(styleStringFocus) {
      //newElm.style.border = styleStringFocus;
      newElm.className = styleStringFocus;
    }
  }
  else if(currentStyle == "blur") {
    if(styleStringBlur) {
      //newElm.style.border = styleStringBlur;
      newElm.className = styleStringBlur;
    }
  }
  newElm.onfocus = function(){return function(){
    if(this.hasFocus) return;
    var newElm = ngiChangeInputType(this,(iToggle?'password':iType),iToggle,iValue,
      (this.value.toLowerCase()==iValue.toLowerCase())?true:false,false,"focus",styleStringFocus,styleStringBlur);
    if(newElm) newElm.hasFocus=true;
  }}();
  newElm.onblur = function(){return function(){
    if(this.hasFocus)
    if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
      ngiChangeInputType(this,(iToggle?'text':iType),iToggle,iValue,false,true,"blur",styleStringFocus,styleStringBlur);
    }
  }}();
 // hasFocus is to prevent a loop where onfocus is triggered over and over again
  newElm.hasFocus=false;
  oldElm.parentNode.replaceChild(newElm,oldElm);
  if(!blankValue) newElm.value = iValue;
  if(!noFocus || typeof(noFocus)=='undefined') {
    window.tempElm = newElm;
    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
  }
  return newElm;
}

/* Keo
 * Function: fixPNG
 * Description:
 *   Handle individual transparency of an <img> tag 
 * Sample usage : <img onload="fixPNG()">
 * Caveats: This may cause JavaScript errors if you plan to use the img tag with JavaScript  
 */
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])
function fixPNG(myImage) 
{

    if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
    {
       var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
	   var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
	   var imgTitle = (myImage.title) ? 
		             "title='" + myImage.title  + "' " : "title='" + myImage.alt + "' "
	   var imgStyle = "display:inline-block;" + myImage.style.cssText
	   var strNewHTML = "<span " + imgID + imgClass + imgTitle
                  + " style=\"" + "width:" + myImage.width 
                  + "px; height:" + myImage.height 
                  + "px;" + imgStyle + ";"
                  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                  + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>"
	   myImage.outerHTML = strNewHTML	  
    }

}

/* JKerger
 * Function: getCookie(cookie_name)
 * Description:
 *   gets a cookie with the passed name
 */
function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1 
			c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end=document.cookie.length
				return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return "";
}

/* JKerger
 * Function: getSession
 * Description:
 *   gets the session ID of the of the user session in the cookie
 */
function getSession() {
	jsession = getCookie('JSESSIONID')
	if (jsession != null && jsession != "") {
		/*alert('Your jsession id is ' + jsession)*/
		return jsession
	}
}

/* JKerger
 * Function: isLoggedIn
 * Description:
 *   This tests the client-side cookie on the browser session
 */
function isLoggedIn(cookieName) {
	if (getCookie(cookieName) != "") {
/*		alert('found login cookie'); */
		return 'true';
	}
	return 'false';
}