String.prototype.startsWith = function(str) {
  return (this.match("^"+str)==str);
}
String.prototype.endsWith = function(str) {
  return (this.match(str+"$")==str);
}
alert_numbered= 0;
var regexp_email  = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/;
var regexp_date  = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
var regexp_float  = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
function sux_fail()  {
    return {
      /*onFailure: '<img class="ko" src="/imgs/default/ko.gif"/>',*/
      /*onSuccess: '<img class="ok" src="/imgs/default/ok.gif"/>'*/
      onFailure: '<span style="color:red;font-size:17px;"><b>*</b></span>',
      onSuccess: '<span style="color:green;font-size:17px;"><b>*</b></span>'
      }
  }

function n_alert()
{
  alert(alert_numbered);
  alert_numbered+= 1;
}

function init_vform(where, sfx)
{
  sfx= typeof(sfx) != 'undefined' ? sfx : '_errors';
  res = new Form.Validator(where);
  res.reporter('InnerHTML');    // show a pop-up with all errors
  var report = res.reporter('InnerHTML');
  report.suffix = sfx;
  return res;
};


function present_field (where, field)
{
  where.set(field, 'notBlank', sux_fail());
}

function match_field (where, field, rex)
{
where.set(field, where.makeMatchRegex(rex), sux_fail());
}

function opt_match_field (where, field, rex)
{
where.set(field, where.makeOptional(where.makeMatchRegex(rex)), sux_fail());
}

function same_field (where, field, field1)
{
where.set(field, where.compareWith(field1), sux_fail());
}

function mail_field (where, field)
{
  where.set(field, where.makeMatchRegex(regexp_email), sux_fail());
}

function _date_field(nofield)
{
  return function(where, field) {
    var d= where.form[field].value;
    if (typeof(nofield) != 'undefined' && d == '') return true;
    return isDate(d, 'd/M/yyyy');
  }
}

function date_field(where, field)
{
  where.set(field, _date_field() , sux_fail());
}

function opt_date_field (where, field)
{
  where.set(field, _date_field(true), sux_fail());
}

function _min_integer(n, opt) {
  return function(where, field) {
    var form= where.form;
    var i= form[field].value;

    if (typeof(opt) != 'undefined' && i == '') return true;
    if (parseInt(i)!= i - 0) return false;
    if (i < n) return false;
    return true;
  }
}

function min_integer_field (where, field, lim)
{
  where.set(field, _min_integer(lim), sux_fail());
}

function opt_min_integer_field (where, field, lim)
{
  where.set(field, _min_integer(lim, true), sux_fail());
}

function _short_name(n) {
  return function(where, field) {
    var f= where.form[field].value;
    if (f.length < 1) return false;
    if (f.length > n) return false;
    return true;
  }
}

function short_name(where, field, n)
{
  where.set(field, _short_name(n), sux_fail());
}

function _float_check(f, opt, negative) {
    if (opt && f.length == 0) return true;
    if (negative && f.charAt(0) == '-') f= f.substring(1);
    if (regexp_float.test(f)) return true;
    return false;
}

function _float_field(opt, negative)
{
  return function(where, field) {
    var f= where.form[field].value;
    return _float_check(f, opt, negative);
    }
}


function float_field(where, field, opt)
{
  where.set(field, _float_field(false, false), sux_fail());
}

function opt_float_field(where, field)
{
  where.set(field, _float_field(true, false), sux_fail());
}

function ufloat_field(where, field)
{
  where.set(field, _float_field(false, true), sux_fail());
}

function _discount_value(tfield, policy) {
  return function (where, field) {

    dtype= where.form[tfield].value;
    if (dtype == '4') return true;
    if (dtype.startsWith('plan_')) return true;
    f= where.form[field].value;

    if (typeof(policy) != 'undefined') {
      if (dtype == '5' || dtype == '1') return true;
      if (dtype == '2' || dtype == '3') {
        if (!regexp_float.test(f)) return false;
        ff= parseFloat(f);
        if  ( ff < 0 || ff > 100) return false;
        return true;
        }
      }

    return _float_check(f, false, false);
  }
}

function discount_field(where, field, tfield, policy)
{
  where.set(field, _discount_value(tfield, policy), sux_fail());
}

function _check_box() {
  return function (where, field) {
    return bvalue= where.form[field].checked;
    }
}

function check_box(where, field) {
  where.set(field, _check_box(), sux_fail());
}

function _cc_dateexp_field(nofield)
{
  return function(where, field) {
    var d= where.form[field].value;
    if (isDate(d, 'M/yy') || isDate(d, 'M/yyyy')) return true;
    return false;
  }
}

function cc_date_field(where, field)
{
  where.set(field, _cc_dateexp_field() , sux_fail());
}

function _text_len(min, max) {

  return function(where, field) {

    txt= where.form[field].value;
    var slen= txt.length;
      
    if (typeof(min) != 'undefined') {
      if (slen < min) return false;
    }
    if (typeof(max) != 'undefined') {
      if (slen > max) return false;
    }
    return true;
  }
}

function text_len(where, field, min, max) {
  where.set(field, _text_len(min, max), sux_fail());
}

function _at_least_one_prefixed_boxes(boxes) {
  return function(where, field) {
    els= where.form.elements;
    for (i=0; i< els.length; i++) {
      fname= els[i].name;
      for (j=0; j< boxes.length; j++) {

        if (fname.indexOf(boxes[j]) == 0 && where.form[fname].checked) {
            return true;
        }
      }
    }
    return false;
  }
}

function at_least_one_prefixed_boxes(where, field, boxes) {
  where.set(field, _at_least_one_prefixed_boxes(boxes), sux_fail());
}

function _check_radio() {
  return function(where, field) {
    f= where.form[field];
    flen= f.length;
    for (i=0; i < flen; i++) {
      if (f[i].checked) return true;
      }
    return false;
    }
}

function check_radio(where, field) {
  where.set(field, _check_radio(), sux_fail());
}
