/**
 *
 * CoMask
 */

/*
if(typeof(CO) == "undefined")
  var CO = Class.create();
  */


CO.Validator = Class.create({

  initialize: function()
  {
    this.oneOfGroupValidators   = new Array(); //Merkt sich die Gruppen für newOneOfGroup
  },

  newOneOfGroup: function(pGroup, pId)
  {
    if(!this.oneOfGroupValidators[pGroup])
      this.oneOfGroupValidators[pGroup] =  new Object();

     this.oneOfGroupValidators[pGroup][pId] = 'x';
  },

  validateOneOfGroup: function(pGroup)
  {
    if(this.oneOfGroupValidators[pGroup])
    {
      $A(this.oneOfGroupValidators[pGroup]).each(function(id)
      {
        alert(id);
      }
      )
    }
    else
      alert("newOnOfGroup: Gruppe nicht gefunden: " + pGroup);
  }

});

CO.Validator.validateRequired = function(pNode)
{

  pNode = $(pNode);

  vText = new CO.Text("Das Feld %NAME% ist ein Pflichtfeld")

  if(pNode.value == "")
    pNode.addError(vText.replace("NAME", pNode.getName()).get());

}

function fixxxmeIsNumber(str) {
  for(var position=0; position<str.length; position++){
	var chr = str.charAt(position)
        if  ( (chr < "0") || (chr > "9") )
              return false;
  };
  return true;
};

CO.Validator.validateNumber = function(pNode)
{
  var vValue  = pNode.value.replace(/,/,".");
  pNode.value = vValue.replace(/\./,",");

  var vFormat = {mMin: null, mMax: null, mMant: null};

  vFormat = Object.extend(vFormat, pNode.co.numFormat);

  if(vValue == "")
    return(true);

  if(isNaN(vValue))
  {
    pNode.addError("Feld ist keine Nummer");
    return(false);
  }

  if((vFormat.mMin != null) && (vFormat.mMin > parseFloat(vValue)))
  {
    pNode.addError("Zahl zu klein");
    return(false);
  }

  if((vFormat.mMax != null) && (vFormat.mMax < parseFloat(vValue)))
  {
    pNode.addError("Zahl zu groß");
    return(false);
  }

  var vMantLen = vValue.substring(vValue.indexOf(".")+1,vValue.length).length;

  if((vFormat.mMant != null) && (vFormat.mMant != 0) && (vMantLen > vFormat.mMant) && vValue.match(/\./))
  {
    pNode.addError("Zahl hat zuviele Kommastellen");
    return(false);
  }

  return(true);
}


var coMask =  function()
{
/*************** private ******************/
  var allowSubmit = true;

  function enableApplyButton()
  {
	  $('MaskAction_apply').disabled  = false;
	}

  function disapleApplyButton()
  {
	  $('MaskAction_apply').disabled  = true;
	}


/******************************************/
return {

/*************** public *******************/

  allowApply : function()
  {
    allowSubmit = true;
    enableApplyButton();
  },


  checkSubmit : function()
  {
    if(allowSubmit)
    {
      return true;
    }
    else
    {
      return false;
    }
  },


  cancel : function()
  {
    /* FIXXME: bessere Lösung */
   allowSubmit = confirm('Wirklich abbrechen?');

  }


/******************************************/
}
}();



if(typeof(CO) == "undefined")
  var CO = Class.create();

CO.Validator = Class.create();

CO.Validator.validateRequired = function(pNode)
{

  pNode = $(pNode);

  vText = new CO.Text("Das Feld %NAME% ist ein Pflichtfeld")

  if(pNode.value == "")
    pNode.addError(vText.replace("NAME", pNode.getName()).get());

}

function fixxxmeIsNumber(str) {
  for(var position=0; position<str.length; position++){
	var chr = str.charAt(position)
        if  ( (chr < "0") || (chr > "9") )
              return false;
  };
  return true;
};

CO.Validator.validateNumber = function(pNode)
{
  var vValue  = pNode.value.replace(/,/,".");
  pNode.value = vValue.replace(/\./,",");

  var vFormat = {mMin: null, mMax: null, mMant: null};

  vFormat = Object.extend(vFormat, pNode.co.numFormat);

  if(vValue == "")
    return(true);

  if(isNaN(vValue))
  {
    pNode.addError("Feld ist keine Nummer");
    return(false);
  }

  if((vFormat.mMin != null) && (vFormat.mMin > parseFloat(vValue)))
  {
    pNode.addError("Zahl zu klein");
    return(false);
  }

  if((vFormat.mMax != null) && (vFormat.mMax < parseFloat(vValue)))
  {
    pNode.addError("Zahl zu groß");
    return(false);
  }

  var vMantLen = vValue.substring(vValue.indexOf(".")+1,vValue.length).length;

  if((vFormat.mMant != null) && (vFormat.mMant != 0) && (vMantLen > vFormat.mMant) && vValue.match(/\./))
  {
    pNode.addError("Zahl hat zuviele Kommastellen");
    return(false);
  }

  return(true);
}


var coMask =  function()
{
/*************** private ******************/
  var allowSubmit = true;

  function enableApplyButton()
  {
	  $('MaskAction_apply').disabled  = false;
	}

  function disapleApplyButton()
  {
	  $('MaskAction_apply').disabled  = true;
	}


/******************************************/
return {

/*************** public *******************/

  allowApply : function()
  {
    allowSubmit = true;
    enableApplyButton();
  },


  checkSubmit : function()
  {
    if(allowSubmit)
    {
      return true;
    }
    else
    {
      return false;
    }
  },



  cancel : function()
  {
    /* FIXXME: bessere Lösung */
   allowSubmit = confirm('Wirklich abbrechen?');

  }


/******************************************/
}
}();
