/**
 * trim
 * removes spaces at the begin and end of a string
 * @param value - the input string 
 * @return value without spaces at the begin and at the end
 **/ 
function trim(value) {
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}

function MM_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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/**
 * changeInputType
 * changes the type of an input field
 * @param oldElm - a reference to the input element
 * @param iType - value of the type property: 'text' or 'password'
 * @param iValue - the default value, set to 'password' in the demo
 * @param blankValue - true if the value should be empty, false otherwise
 * @param noFocus - set to true if the element should not be given focus 
 * @return a reference to the new object
 **/ 
function changeInputType( oldElm, iType, iValue, blankValue, noFocus) 
{  
  if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
    !document.getElementById || !document.createElement) return;
  var isMSIE=/*@cc_on!@*/false; //http://dean.edwards.name/weblog/2007/03/sniff/
  if(!isMSIE){
    var newElm=document.createElement('input');
    newElm.type=iType;
  } else {
    var newElm=document.createElement('span');
    newElm.innerHTML='<input type="'+iType+'" name="'+oldElm.name+'">';
    newElm=newElm.firstChild;
  }
  var props=['name','id','className','size','tabIndex','accessKey'];
  for(var i=0,l=props.length;i<l;i++){
    if(oldElm[props[i]]) newElm[props[i]]=oldElm[props[i]];
  }
  newElm.onfocus = oldElm.onfocus;
  newElm.onblur = oldElm.onblur;
 // hasFocus is to prevent a loop where onfocus is triggered over and over again
  newElm.hasFocus=false;
  // some browsers need the value set before the element is added to the page
  // while others need it set after
  if(!blankValue) newElm.value=iValue;
  oldElm.parentNode.replaceChild(newElm,oldElm);
  if(!isMSIE && !blankValue) newElm.value=iValue;
  if(!noFocus || typeof(noFocus)=='undefined') {
    window.tempElm=newElm;
    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
  }
  return newElm;
}

/**
 * clearInputField
 * clears the temporary value of an input field
 * @param elm - a reference to the input element
 * @param cap - the default caption
 * @param classname - the normal classname
 **/ 
function clearInputField( elm, cap, changetype )
{
  /* decide if we need to clear the value */
  if ( trim(elm.value) == cap )
  {
    elm.value = "";
    
    /* determine if we need to change the type to password */
    if ( changetype )
      changeInputType( elm, 'password', elm.value, elm.value == "", false);
  }
}

/**
 * restoreInputField
 * restores the value of an input field
 * @param elm - a reference to the input element
 * @param cap - the default caption
 * @param classname - the normal classname
 **/ 
function restoreInputField( elm, cap, changetype )
{
  /* decide if we need to change the value */
  if ( trim(elm.value) == "" )
  {
    elm.value = cap;
    
    /* determine if we need to change the type to text */
    if ( changetype )
      changeInputType( elm, 'text', elm.value, elm.value == "", true);    
  }
}

/**
 * changeCheckbox
 * changes the state of a checkbox
 * @param id - the id of the checkbox element
 * @param base_src - the path to the base image
 * @param checked_src - the path to the checked image
 **/
function changeCheckbox( id, base_src, checked_src ) {
	elm = MM_findObj(id);
	if ( elm.src == base_src )
		elm.src = checked_src;
	else
		elm.src = base_src;
}  

/**
 * openWindow
 * creates a new window with the given url
 * @param url - the webpage to show
 * @param width - the width of the window
 * @param height - the height of the window
 **/
function openWindow( url, name, width, height, scroll ) {
	/* create the window */
	newWindow = window.open( url, name,"toolbar=0, status=no, menubar=0, width=" + width + ", height=" + height + ", resizable=0, scrollbars=" + scroll);
	newWindow.focus();
} 

/**
 * getXmlHttpObject
 * this function returns a XmlHttp object
 * @return a XmlHttp object or null
 **/ 
function getXmlHttpObject()
{
  var xmlHttp=null;
  try {
    xmlHttp=new XMLHttpRequest();
  } catch (e) {
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}