/*******************************************************************************
* forms.js -
*
* $Source: H:\\CVS\\cvsrepo/Moonedge\040Javascripts/forms.js,v $
* $Revision: 1.1 $
* $Date: 2006/03/17 12:42:06 $
* $Author: Markus $
*
* Copyright (C) 2005 Moonedge E-Business GmbH, http://www.moonedge.ch
*******************************************************************************/

/*------------------------------------------------------------------------------


------------------------------------------------------------------------------*/

var fldList = new Array;
var annList = new Array;
var FormChecker = {

  msg : function(fld,nr) {
    var cap,mi,ma;
    cap = fld.__Caption; mi = fld.__Minimum; ma = fld.__Maximum;
    switch(nr) {
      case 0: return('Geben Sie einen Wert in das Feld "'+cap+'" ein.');
      case 1: return('Geben Sie einen Wert der länger oder gleich '+mi+' Zeichen lang ist in das Feld "'+cap+'" ein.');
      case 2: return('Geben Sie einen Wert der kürzer oder gleich '+ma+' Zeichen lang ist in das Feld "'+cap+'" ein.');
      case 3: return('Geben Sie einen Wert größer oder gleich '+mi+' in das Feld "'+cap+'" ein.');
      case 4: return('Geben Sie einen Wert kleiner oder gleich '+ma+' in das Feld "'+cap+'" ein.');
      case 5: return('Geben Sie nur ganze Zahlen in das Feld "'+cap+'" ein.');
      case 6: return('Geben Sie nur Zahlen in das Feld "'+cap+'" ein.');
      case 7: return('Geben Sie ein gültiges Datum in das Feld "'+cap+'" ein (Format dd mm yyyy [hh:mm:ss]).');
      case 8: return('Geben Sie eine gültige Zeit in das Feld "'+cap+'" ein (Format hh:mm:ss).');
      case 9: return('Geben Sie eine gültige E-Mail Adresse "'+cap+'" ein.');
      case 10: return('Geben Sie eine gültige URL in das Feld "'+cap+'" ein.');
    }
  },

  setAutocomplete : function(id1,id2) {
  	var fld = document.getElementById(id1);
  	var sel = document.getElementById(id2);
  	fld.__sel = id2; fld.onkeyup = FormChecker.autocomplete;
  	sel.__fld = id1; sel.onchange = FormChecker.selectitem;
	},

	selectitem : function() {
  	var fld = document.getElementById(this.__fld);
  	fld.value = this.value;
	},

	autocomplete : function(evt) {
  	var found = false;
  	var sel = document.getElementById(this.__sel)
  	for (var i = 0; i < sel.options.length; i++) {
  	  if (sel.options[i].value.toUpperCase().indexOf(this.value.toUpperCase()) == 0) {
  		  found = true; break;
  		}
  	}
  	if (found) { sel.selectedIndex = i; } else { sel.selectedIndex = -1; }
    if (!evt) { evt = event; }
    var key = evt.keyCode;
    if (key < 32 || (key >= 33 && key <= 46) || (key >= 112 && key <= 123)) {
      //ignore
    } else {
      var oldVal = this.value;
   		var newVal = found ? sel.options[i].value : oldVal;
    	if (this.createTextRange) {
  			if (newVal != this.value) {
  			  this.value = newVal;
  				var rNew = this.createTextRange();
  				rNew.moveStart('character',oldVal.length) ;
  				rNew.select();
  		  }
  	  } else if (this.setSelectionRange) {
  			if (newVal != this.value) {
  			  this.value = newVal;
          this.setSelectionRange(oldVal.length,newVal.length);
        }
      }
    }
  },

  add : function(id,typ,cap,mnd,mi,ma) {
    var o = document.getElementById(id)
    if (o) {
      o.__Type = typeof typ != 'undefined' ? typ.toString() : 'str';
      o.__Caption = typeof cap != 'undefined' ? cap.toString() : '';
      o.__Mandatory = typeof mnd != 'undefined' ? mnd.toString() != '' : false;
      o.__Minimum = typeof mi != 'undefined' ? parseFloat(mi) : '';
      o.__Maximum = typeof ma != 'undefined' ? parseFloat(ma) : '';
      fldList[fldList.length] = o;
    }
  },

  addfunction : function(frm,fnc) {
    if (!frm.__fncList) frm.__fncList = new Array;
    frm.__fncList[frm.__fncList.length] = fnc;
  },

  setannotation : function(id,def) {
    var o = document.getElementById(id)
    if (o) {
      o.__Annotation = typeof def != 'undefined' ? def.toString() : '';
      o.__OriginalColor = o.style.color;
      o.style.color = '#CCCCCC';
      if (o.value == '') o.value = o.__Annotation;
      o.onfocus = this.onfocus;
      o.onblur = this.onblur;
      annList[annList.length] = o;
    }
  },
  
  onfocus : function(e) {
    o = (document.all) ? event.srcElement : e.target;
    if (o.value == o.__Annotation) o.value = '';
    o.style.color = o.__OriginalColor;
  },

  onblur : function(e) {
    o = (document.all) ? event.srcElement : e.target;
    if (o.value == '') {
      o.style.color = '#CCCCCC';
      o.value = o.__Annotation;
    }
  },
  
  check : function(frm) {
    var fld,val,len;
    if (frm) {
    	for (var i=0;i<fldList.length;i++) {
        fld = fldList[i]; 
        if (fld.form == frm) {
          val = fld.value; len = val.length
          if (val == fld.__Annotation) { val = ''; len = 0 };
          if (fld.__Mandatory && (len == 0)) {
            alert(this.msg(fld,0)); fld.focus(); fld.select(); return (false);
          }
          if (len > 0) {
            switch(fld.__Type) {
              case 'str':
              case 'pwd':
                if ((fld.__Minimum != '') && (len < fld.__Minimum)) { alert(this.msg(fld,1)); fld.focus(); fld.select(); return (false); }
                if (fld.__Maximum != '' && (len > fld.__Maximum)) { alert(this.msg(fld,2)); fld.focus(); fld.select(); return (false); }
                break;
              case 'eml':
                var re = new RegExp("^([a-z0-9_\-]+)(\.[a-z0-9_\-]+)*@([a-z0-9_\-]+\.)+([a-z]{2,7})\s*$","i");
                if (!re.test(val)) { alert(this.msg(fld,9)); fld.focus(); fld.select(); return (false); }
                break;
              case 'hyp':
                var re = new RegExp("^(((http|ftp|https):\/\/)|(.\/)|(..\/)|(\/))?[a-z0-9_\-]+(\.[a-z0-9_\-]+)+([a-z0-9\-.,@?^=%&:/~\+#]*[a-z0-9\-\@?^=%&/~\+#])?\s*$","i");
                if (!re.test(val)) { alert(this.msg(fld,10)); fld.focus(); fld.select(); return (false); }
                break;
              case 'int':
                var re = new RegExp("^\s*[+-]?[0-9]*\s*$");
                if (re.test(val)) {
                  val = parseFloat(val)
                  if ((fld.__Minimum != '') && (val < fld.__Minimum)) { alert(this.msg(fld,3)); fld.focus(); fld.select(); return (false); }
                  if (fld.__Maximum != '' && (val > fld.__Maximum)) { alert(this.msg(fld,4)); fld.focus(); fld.select(); return (false); }
                } else {
                  alert(this.msg(fld,5)); fld.focus(); fld.select(); return (false);
                }
                break;
              case 'dbl':
                var re = new RegExp("^\s*[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?\s*$");
                if (re.test(val)) {
                  val = parseFloat(val)
                  if ((fld.__Minimum != '') && (val < fld.__Minimum)) { alert(this.msg(fld,3)); fld.focus(); fld.select(); return (false); }
                  if (fld.__Maximum != '' && (val > fld.__Maximum)) { alert(this.msg(fld,4)); fld.focus(); fld.select(); return (false); }
                } else {
                  alert(this.msg(fld,6)); fld.focus(); fld.select(); return (false);
                }
                break;
              case 'dat':
                var re = new RegExp("^\s*([0]?[1-9]|[12][0-9]|3[01])[ .]([0]?[1-9]|1[012])[ .](([12][0-9])?[0-9][0-9])([ ]*([01][0-9]|2[0-3])(:[0-5][0-9]){0,2})?\s*$");
                if (!re.test(val)) { alert(this.msg(fld,7)); fld.focus(); fld.select(); return (false); }
                break;
              case 'tim':
                var re = new RegExp("^\s*([01][0-9]|2[0-3])(:[0-5][0-9]){0,2}\s*$");
                if (!re.test(val)) { alert(this.msg(fld,8)); fld.focus(); fld.select(); return (false); }
                break;
            }
          }
        }  
      }
      
    	if (frm.__fncList) {
      	for (var i=0;i<frm.__fncList.length;i++) {
          if (!eval(frm.__fncList[i]+'(frm)')) return (false);
        }
      }  

      // remove all annotations
    	for (var i=0;i<annList.length;i++) {
    	  if (annList[i].value == annList[i].__Annotation) annList[i].value = '';
      }

      return (true);
    }
  }

};


