  // JavaScript Document


function init_validation(id_form){
  el_form = document.getElementById(id_form);
  el_form.onsubmit = validation;
  
}

function validation(){
  var msg_error = '';
  id_form = this.id;
  arr_input = document.getElementsByTagName('input');
  //el_style = document.getElementsByTagName('label')[0].firstChild.nodeValue;
  i = 0;
  while(arr_input[i]){
    if(arr_input[i].id){
      if(arr_input[i].type == 'text'){
        id_label = get_Id_Label(arr_input[i].id);
        document.getElementById(id_label).className = 'normal';       
        // if attribute is required
        if(class_is(arr_input[i].id, 'required')){
          if(arr_input[i].value == ''){
            msg_error += '<li>falta texto en '+ document.getElementById(id_label).firstChild.nodeValue+'</li>';
            document.getElementById(id_label).className = 'error_val';

          }
        }
        // if attribute is mail
        if(class_is(arr_input[i].id, 'mail')){
          if(arr_input[i].value.lastIndexOf('@') <=2 ){
            msg_error += '<li>El campo '+ document.getElementById(id_label).firstChild.nodeValue + ' no es valido </li>';
            document.getElementById(id_label).className = 'error_val';
          }
        }
      }
    }
    i++;
  }
  
  
  arr_textarea = document.getElementsByTagName('textarea');
  //el_style = document.getElementsByTagName('label')[0].firstChild.nodeValue;
  //alert(current_color);
  i = 0;
  while(arr_textarea[i]){

    if(arr_textarea[i].id){
        id_label = get_Id_Label(arr_textarea[i].id);
        document.getElementById(id_label).className = 'normal';       
        // if attribute is required
        if(class_is(arr_textarea[i].id, 'required')){
          if(arr_textarea[i].innerHTML == ''){
            msg_error += '<li>falta texto en '+ document.getElementById(id_label).firstChild.nodeValue+'</li>';
            document.getElementById(id_label).className = 'error_val';

            //alert(document.getElementById(id_label).style.color = 'red');
          }
        }
        // if attribute is mail
    }
    i++;
  }  
  
   
   if(msg_error != ''){
    msg_error = '<ul> '+ msg_error + ' <ul>';
    document.getElementById('errores').innerHTML = msg_error;
    return false;  
   }
 
}

function class_is(el_input, class_item){
    //alert('-->' + document.getElementById(el_input).className);
  if(document.getElementById(el_input).className){
    if(document.getElementById(el_input).className.lastIndexOf(class_item) >= 0){
      //alert('si es de la clase');
      return true;
    }else{
      //alert('nooooo es de la clase');
      return false;
    }  
  }else{
      //alert('otra opcion');
      return false;
  }
}

function get_Id_Label(id_input){
  id_label = 'lbl_' + id_input;
  return id_label;
}
