// JavaScript Document
var inputs = document.getElementsByTagName("input");
var textareas =  document.getElementsByTagName("textarea");

var valorinicial = new Array();

function formulario() {
  for (var i = 0; i < inputs.length; i++) {
    if((inputs[i].type == "text") || (inputs[i].type == "password")) {
      valorinicial[inputs[i].id]=inputs[i].value;
       inputs[i].onfocus = function() {vacia(this.id,this.value)}
      inputs[i].onblur = function() {restaura(this.id,this.value)}
    }
  }
  for (var i = 0; i < textareas.length; i++) {
    valorinicial[textareas[i].id]=textareas[i].value;
    textareas[i].onfocus = function() {vacia(this.id,this.value)}
    textareas[i].onblur = function() {restaura(this.id,this.value)}
  }
}

function vacia(id,valor){
  var targetElement = document.getElementById(id);
  var tipo = targetElement.type;
  switch ( tipo ) {
    case "text":
      if (valorinicial[id] == valor)
        targetElement.value = "";
      break
    case "password":
      if (valorinicial[id] == valor)
        targetElement.value = "";
      break
    case "textarea":
      if (valorinicial[id] == valor) {
        targetElement.innerHTML = "";
        targetElement.value = "";
      }
    break
  }
}

function restaura(id){
  var targetElement = document.getElementById(id);
  var tipo = targetElement.type;
  switch ( tipo ) {
    case "text":
      if (targetElement.value.replace(/ /g, '') == '')
        targetElement.value = valorinicial[id];
      break
    case "password":
      if (targetElement.value.replace(/ /g, '') == '')
        targetElement.value = valorinicial[id];
      break
    case "textarea":
      revisa = targetElement.value.replace(/ /g, '');
      // Eliminamos saltos de línea y tabulaciones
      revisa = revisa.replace(/n/g, '')
      revisa = revisa.replace(/t/g, '')
      revisa = revisa.replace(/r/g, '')
      if (revisa == '') {
        targetElement.innerHTML = valorinicial[id];
        targetElement.value = valorinicial[id];
      }
      break
  }
}

window.onload = formulario;