//  JavaScript Document

//  clearFormDefaultValues (2010-03-17)

//  typical using of this component
//  var clearFormDefaultValues = new clearFormDefaultValues();
//  clearFormDefaultValues.init('top-login');

function clearFormDefaultValues() {
  var self = this;
  //
  this.init = function(formID,imageSubmitID) {
    var formElem = document.getElementById(formID);
    if (!formElem) return false;
    dojo.query('input',formElem).forEach(self.applyParams);
    formElem.elImageSubmit = dojo.byId(imageSubmitID);
    if (formElem.elImageSubmit) { // if submit is IMAGE type, disable it to prevent sending coordinates
        dojo.connect(formElem,'onsubmit',function () {
            formElem.elImageSubmit.disabled = 'disabled';
        })
    }
    return true;
  };
  //
  this.applyParams = function(thisElem,index) {
    if (thisElem.type=='text') {
      thisElem.cfTooltip = true; // prevent CF tooltip on this element
      thisElem.def = thisElem.title;
      thisElem.title = '';
      if (thisElem.value=='') {
        thisElem.value = thisElem.def;
      }
      dojo.connect(thisElem,'onfocus',self.textFocus);
      dojo.connect(thisElem,'onblur',self.textBlur);
    }
    if (thisElem.type=='password') {
      thisElem.cfTooltip = true; // prevent CF tooltip on this element
      thisElem.def = thisElem.title;
      thisElem.title = '';
      var newInput = document.createElement('input');
      var attributesArr = [['type','text'],['class',thisElem.className],['className',thisElem.className],['name',thisElem.name+'_simul'],['value',thisElem.def],['size',thisElem.size]];
      dojo.forEach(attributesArr,function(arrItem){
        newInput.setAttribute(arrItem[0],arrItem[1]);
      });
      dojo.connect(newInput,'onfocus',self.passwordFocus);
      dojo.connect(thisElem,'onblur',self.passwordBlur);
      thisElem.parentNode.insertBefore(newInput,thisElem);
      thisElem.style.display = 'none';
    }
  };
  //
  this.textFocus = function(e,thisElem) {
    if (!thisElem) thisElem = this;
    if (thisElem.value==thisElem.def) {
      thisElem.value = '';
    }
  };
  //
  this.textBlur = function(e,thisElem) {
    if (!thisElem) thisElem = this;
    if (thisElem.value=='') {
      thisElem.value = thisElem.def;
    }
  };
  //
  this.passwordFocus = function(e,thisElem) {
    if (!thisElem) thisElem = this;
    if (true) {
      thisElem.style.display = 'none';
      thisElem.blur();
      thisElem.nextSibling.style.display = 'inline';
      thisElem.nextSibling.focus();
    }
  };
  //
  this.passwordBlur = function(e,thisElem) {
    if (!thisElem) thisElem = this;
    if (thisElem.value=='') {
      thisElem.style.display = 'none';
      thisElem.blur();
      thisElem.previousSibling.style.display = '';
      thisElem.previousSibling.blur();
    }
  };
  //
  //return self.init();
}

//  end of document