﻿/// JScript File
////*
///cfg = { baseName:'', fields:{ac:<obj>,exc:<obj>,ext:<obj>,chk:<obj>},
///                     errflds:{phone:<obj>}
///
///*/

var PhoneCfg = function(){
  return {baseName:'',
    "fields":{"ac":null,"exc":null,"line":null,"ext":null},
    "errflds":{"phone":null},
    "validateNotify":null };
}

var PhoneVal = function(cfg){
    this.cfg = cfg;
    this.ele = cfg.fields;
    this.Init(cfg);
    this.errs = {"MAG250":"Phone number is required.",
                 "MAG251":"Phone could not be validated."
                 }
}
PhoneVal.prototype = new BaseClass();
PhoneVal.prototype.Validate = function(){
    this.clearErrs();
    var errors = [], ele=[],te=this.ele;
    var ret = {err: errors, flds:ele};
    if( this.getVal(te["ac"]).length != 3 || this.getVal(te["exc"]).length != 3  || this.getVal(te["ext"]).length != 4 )
    { errors.push(this.getErr("MAG250"));ele.push(this.errflds.phone); }
    if( errors.length == 0 ){
            var reg = new RegExp("[^0-9]");
            if( 
            this.getVal(te["ac"]).match(reg) || 
            this.getVal(te["exc"]).match(reg) || 
            this.getVal(te["ext"]).match(reg) 
            ){
                //we have invalid characters so we return false
                errors.push(this.getErr("MAG251"));ele.push(this.errflds.phone);
            }            
    }
    return ret;
}
PhoneVal.prototype.Serialize = function(){
    var el=this.ele;
    return "||ac:"+el["ac"].value+"|exc:"+el["exc"].value+"|ext:"+el["ext"].value;
}
