// vim:set ts=2 sts=2 sw=2 bs=2 ai si et nu ft=javascript fdm=marker commentstring=\ //%s:
// $Id$
// Last Change: 2006/11/25. 

function vCheckInit(){ //{{{
  if(document.getElementById("errors")){
    var obj = document.getElementById("errors");
    obj.innerHTML = "";
    obj.style.display = 'none';
  }
}
 //}}}
function changeBgColor(el,color){ //{{{
  if(BGCOLOR){
    if(!(el[0])){
      el.style.backgroundColor = color;
    }else{
      for(var i=0;i<el.length;i++){
        el[i].style.backgroundColor = color;
      }
    }
  }
}
 //}}}
function errorCheck(el,key,val){ //{{{

  if(el.length == 0){
    errors.push(val["empty_error"]);
    indexs.push(key);
    return false;
  }

  if(val["maxlength"] && el.length > val["maxlength"]){
    errors.push(val["maxlength_error"]);
    indexs.push(key);
    return false;
  }

  if(val["regexp"] && !(el.match(val["regexp"]))){
    errors.push(val["regexp_error"]);
    indexs.push(key);
    return false;
  }

  return true;

}
 //}}}
function makeError(errors){ //{{{
  
  var res = "";

  if(errors.length > 0){

    res += ERROR_TITLE;

    if(ERROR_MODE==1){
      res += ERROR_LIST_S + "<li>";
      res += errors.join("</li><li>");
      res += "</li>" + ERROR_LIST_E;
    }else if(ERROR_MODE==2){
      for(var i=0;i<names.length;i++){
        var obj = document.getElementById(ERROR_ID_PREFIX+names[i]);
        if(obj) obj.style.display = "none";
      }
      for(var i=0;i<indexs.length;i++){
        var obj = document.getElementById(ERROR_ID_PREFIX+String(indexs[i]));
        if(obj){
          obj.innerHTML = ERROR_PREFIX + errors[i];
          obj.style.display = "block";
        }
      }
    }

  }

  return res;

}
 //}}}
function vCheck(form){ //{{{
  if(DEBUG){
    var debug_values = new Array();
  }

  var elsize = names.length;
  indexs = new Array();
  errors = new Array();


  for(var i=0;i<elsize;i++){

    var key=names[i];
    var val=values[key];
    var el=form.elements[key];
    var elvalue=new Array();
    var errflag=true;

    if(val["plural"]){

      if(!(val["empty"]) && el.length == 0){
        changeBgColor(el, FALSE_BGCOLOR);
        errors.push(val["empty_error"]);
        indexs.push(key);
        continue;
      }

      if(el.type != "select-one"){
        for(var j=0;j<el.length;j++){
          if(el[j].type == "text" || el[j].type == "textarea"){
            elvalue.push(el[j].value);
          }else if(el[j].type == "checkbox" || el[j].type == "radio"){
            if(el[j].checked){
              elvalue.push(el[j].value);
            }
          }
        }
      }else{
        for(var k=0;k<el.options.length;k++){
          if(el.options[k].selected){
            elvalue.push(el.options[k].text);
          }
        }
      }

      if(DEBUG){
        debug_values.push(elvalue.toSource());
      }

      if(val["empty"] && elvalue.length == 0){
        continue;
      }else{

        if(!(val["empty"]) && elvalue.length == 0){
          changeBgColor(el, FALSE_BGCOLOR);
          errors.push(val["empty_error"]);
          indexs.push(key);
          continue;
        }

        if(val["plural_min"] && (elvalue.length < Number(val["plural_min"]))){
          changeBgColor(el, FALSE_BGCOLOR);
          errors.push(val["plural_min_error"]);
          indexs.push(key);
          continue;
        }

        if(val["plural_max"] && (elvalue.length > Number(val["plural_max"]))){
          changeBgColor(el, FALSE_BGCOLOR);
          errors.push(val["plural_max_error"]);
          indexs.push(key);
          continue;
        }

        for(var k=0;k<elvalue.length;k++){

          if(val["empty"] && elvalue[k].length == 0){
            continue;
          }

          errflag = errorCheck(elvalue[k],key,val);
        }
      }
        
    }else{

      if(el.type == "text" || el.type == "textarea"){
        elvalue.push(el.value);
      }else if(el.type == "checkbox" || el.type == "radio"){
        if(el.checked){
          elvalue.push(el.value);
        }
      }else if(el.type == "select-one"){
        elvalue.push(el.options[el.selectedIndex].value);
      }

      if(DEBUG){
        debug_values.push(elvalue.toSource());
      }

      if(!(val["empty"]) && elvalue.length == 0){
        changeBgColor(el, FALSE_BGCOLOR);
        errors.push(val["empty_error"]);
        indexs.push(key);
        continue;
      }else if(val["empty"] && elvalue[0].length == 0){
        continue;
      }else if(elvalue[0].length == 0){
        changeBgColor(el, FALSE_BGCOLOR);
        errors.push(val["empty_error"]);
        indexs.push(key);
        continue;
      }else{

        errflag = errorCheck(elvalue[0],key,val);
        if(errflag){
          if(val["confirm"]){
            if(elvalue[0] != form.elements[val["confirm"]].value){
              errors.push(val["confirm_error"]);
              indexs.push(key);
              errflag=false;
            }
          }
        }
      }

    }

    if(!errflag){
      changeBgColor(el, FALSE_BGCOLOR);
      errflag=true;
    }else{
      changeBgColor(el, TRUE_BGCOLOR);
    }

  }

  var obj = document.getElementById("errors");
  obj.style.display = 'block';
  location.hash = '#';

  if(errors.length > 0){
    if(DEBUG){
      obj.innerHTML = makeError(errors) + "<pre>" + debug_values.toSource() + "</pre>";
    }else{
      obj.innerHTML = makeError(errors);
    }
    return false;
//  }else{
//    obj.innerHTML = SUCCESS_TITLE;
//    return confirm(CONFIRM_COMMENT);
  }

}
 //}}}

