// Note: I recommend you use a more comprehensive event management script for
// production (aka "live") pages.
// Dean Edwards' event manipulation functions is a good example. You can find
// them here: http://dean.edwards.name/weblog/2005/10/add-event2/

var LoadHandler = {
  handlers:[],
  add:function(fn){
	if ($!=undefined) {
		$(document).ready(fn);
	} else {
	    if(window.onload!=LoadHandler.theHandler) LoadHandler._push(window.onload);
	    LoadHandler._push(fn);
	    window.onload=LoadHandler.theHandler;
	}
  },
  _push:function(fn){
    if(typeof(fn)!='function') return;
    LoadHandler.handlers[LoadHandler.handlers.length]=fn;
  },
  theHandler:function(){
    var handlers=LoadHandler.handlers,i=-1,fn;
    while(fn=handlers[++i]) fn();
  }
}

// this is to prevent the form from submitting in this demo
LoadHandler.add(function(){
//  document.forms[0].onsubmit=function(){return false;}
});

// Example 1
LoadHandler.add(function(){
  var allowInputTypeChange = true;
  var fld1 = document.forms[0].field1;
  if(document.getElementById) {
    try { // to keep IE from showing errors.
      fld1.type = 'text';
      if(fld1.type == 'text') fld1.value = 'password';
      else allowInputTypeChange = false;
    } catch(e) { allowInputTypeChange = false; }
  }
  if(allowInputTypeChange) {
    fld1.onfocus = function() {
      var tempVal = this.value; // NS6 fix.
      var iType = this.type; // Opera 7 fix.
      if(this.type != 'password') this.type = 'password';
      this.value = tempVal; // NS6 fix.
      if(this.value.toLowerCase()=='password')
        this.value = '';
      if(window.opera && iType != 'password') this.focus();
    }
    fld1.onblur = function() {
      if(this.type == 'password')
      if(this.value=='' || this.value.toLowerCase()=='password') {
        this.type = 'text';
        this.value = 'password';
      }
    }
  }
});

// Example 2 (JS part 1)
function changeInputType(
  oldElm, // a reference to the input element
  iType, // value of the type property: 'text' or '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
  sValue,
  ifunc) {
	var sValue2 = (sValue == null) ? "password" : sValue;
	var func = ifunc;
	
	if (iType=="textarea") var returning = "textarea";
	else var returning = "text";
	
  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){
	  if (iType=="textarea") {
		  var newElm=document.createElement('textarea');
	  } else {
		  var newElm=document.createElement('input');
		  newElm.type=iType;
	  }
  } else {
	  if (iType=="textarea") {
		var newElm=document.createElement('span');
	    newElm.innerHTML='<textarea name="'+oldElm.name+'"></textarea>';
	    newElm=newElm.firstChild;
	  } 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=function(){return function(){
    if(this.hasFocus) return;
    var newElm=changeInputType(this,sValue2,iValue,
      (this.value.toLowerCase()==iValue.toLowerCase())?true:false,false,sValue2,func);
    if(newElm) newElm.hasFocus=true;
  }}();
  newElm.onblur=function(){return function(){
	if(this.hasFocus)
    if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
      changeInputType(this,returning,iValue,false,true,sValue2,func);
    }
  }}();
 // 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;
	  if (func!=null) {
		  func(newElm);
	  }
  }
  
  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;
}


//$(document).ready(function() {
LoadHandler.add(function(){
	  var ua=navigator.userAgent.toLowerCase();
	  if(!((ua.indexOf('konqueror')!=-1) && /khtml\/3\.[0-4]/.test(ua)) && 
	    !(((ua.indexOf('safari')!=-1) && !window.print))) {
		  func = function(obj) {
//			  obj.style.fontSize = "8pt";
//			  obj.style.fontWeight = "bold";
//			  obj.style.color = "#557";
			  obj.style.backgroundColor = "#fff";
			  obj.className  += " field";
			  
		  }
		  
		  field = document.forms[0].firstname;
		  label = field.previousSibling;
//		  field.parentNode.style.display="inline";
		  if (label.tagName == "LABEL") {
			  text = label.innerHTML.replace(/(^\s*|\s*$)/g,"");
			  label.innerHTML = "";
		  } else if ((label = label.previousSibling).tagName == "LABEL") {
			  text = label.innerHTML.replace(/(^\s*|\s*$)/g,"");
			  label.innerHTML = "";
		  }
	      // Set the third value to the text you want to appear in the field.
	      changeInputType(field,'text',text,false,true,"text",func);
	      
	      field = document.forms[0].lastname;
		  label = field.previousSibling;
//		  field.parentNode.style.display="inline";
		  if (label.tagName == "LABEL") {
			  text = label.innerHTML.replace(/(^\s*|\s*$)/g,"");
			  label.innerHTML = "";
		  } else if ((label = label.previousSibling).tagName == "LABEL") {
			  text = label.innerHTML.replace(/(^\s*|\s*$)/g,"");
			  label.innerHTML = "";
		  }
	      // Set the third value to the text you want to appear in the field.
	      changeInputType(field,'text',text,false,true,"text",func);
	      
	      field = document.forms[0].phone;
		  label = field.previousSibling;
//		  field.parentNode.style.display="inline";
		  if (label.tagName == "LABEL") {
			  text = label.innerHTML.replace(/(^\s*|\s*$)/g,"");
			  label.innerHTML = "";
		  } else if ((label = label.previousSibling).tagName == "LABEL") {
			  text = label.innerHTML.replace(/(^\s*|\s*$)/g,"");
			  label.innerHTML = "";
		  }
	      // Set the third value to the text you want to appear in the field.
	      changeInputType(field,'text',text,false,true,"text",func);
	      
	      field = document.forms[0].email;
		  label = field.previousSibling;
//		  field.parentNode.style.display="inline";
		  if (label.tagName == "LABEL") {
			  text = label.innerHTML.replace(/(^\s*|\s*$)/g,"");
			  label.innerHTML = "";
		  } else if ((label = label.previousSibling).tagName == "LABEL") {
			  text = label.innerHTML.replace(/(^\s*|\s*$)/g,"");
			  label.innerHTML = "";
		  }
	      // Set the third value to the text you want to appear in the field.
	      changeInputType(field,'text',text,false,true,"text",func);
	      
	      field = document.forms[0].details;
		  label = field.previousSibling;
//		  field.parentNode.style.display="inline";
		  if (label.tagName == "LABEL") {
			  text = label.innerHTML.replace(/(^\s*|\s*$)/g,"");
			  label.innerHTML = "";
		  } else if ((label = label.previousSibling).tagName == "LABEL") {
			  text = label.innerHTML.replace(/(^\s*|\s*$)/g,"");
			  label.innerHTML = "";
		  }
	      // Set the third value to the text you want to appear in the field.
	      changeInputType(field,'textarea',text,false,true,"textarea",func);
	  }
	  
});
/*
LoadHandler.add(function(){
	  var vars = document.getElementById("vars");
	  vars.innerHTML = document.images[0].width*.50;
});*/