﻿///
///cfg = { baseName:'',pdiv:<containerDiv>, fields:{addr:<obj>, addr1:<obj>,addr2:<obj>,addr3:<obj>,city:<obj>,state:<obj>,county:<obj>,state:<obj>,country:<obj>,zip:<obj>,zipp:<obj>},
///                     errflds:{addr1:<obj>,addr2:<obj>,city:<obj>,state:<obj>,zip:<obj>},
///        validateNotify: handler }

var AddrCfg = function(){ return {'baseName':'', 'pdiv':null,
"fields":{ "addr1":null, "addr2":null, "addr3":null,
"city":null,"state":null,"county":null,"state":null,
"country":null, "zip":null, "zipp":null},
"errflds":{"addr1":null, "addr2":null, "addr3":null,
"city":null,"state":null,"county":null,"state":null,
"country":null, "zip":null, "zipp":null},
validateNotify:null};
}

var Addr = function(cfg){
    this.cfg = cfg;
    this.ele = cfg.fields;
    this.Init(cfg);
    this.errs = {"MAG201":"Address line 1 is required.",
                 "MAG202":"City is required.",
                 "MAG203":"State is required.",
                 "MAG204":"Zip is required.",
                 "MAG205":"Address could not be validated.",
                 "MAG206":"Zip is invalid."
                 }
}
Addr.prototype = new BaseClass();
Addr.prototype.Validate = function(){
    var errs = [], flds = [], te=this.ele, tf=this.errflds;
    var ret = { err: errs, flds: flds };
    this.clearErrs();
    //verify lengths
    if(this.getVal(te["addr1"]).length == 0){ errs.push(this.getErr("MAG201")); flds.push(tf.addr1);}
    if(this.getVal(te["city"]).length == 0){ errs.push(this.getErr("MAG202"));flds.push(tf.city);}
    if(te["state"].selectedIndex == 0){ errs.push(this.getErr("MAG203"));flds.push(tf.state);}
    var len=this.getVal(te["zip"]).length;
    if(len == 0) {errs.push(this.getErr("MAG204"));flds.push(tf.zip);}
    if(len > 0 && len != 5) {errs.push(this.getErr("MAG206"));flds.push(tf.zip);}
    if( this.cfg.validateNotify && typeof(this.cfg.validateNotify)=="function" ) this.cfg.validateNotify(ret);
    return ret;
}
Addr.prototype.Serialize = function(){
   var el = this.ele,c='';
    c = "addr1:"+el["addr1"].value+"|"+"addr2:"+el["addr2"].value+"|"+
        "state:"+el.state.options[el.state.selectedIndex].text+"|"+"city:"+el["city"].value+"|"+"zip:"+el["zip"].value+"|";
    if( this.check ) c += "chk:false"+"|";
    if( el["addr"] ) el.addr.innerHTML = el.addr1.value+" "+el.addr2.value+" <br /> "+
                        el.state.options[el.state.selectedIndex].text+" "+el.city.value+" "+el.zip.value;
    return c;
}
Addr.prototype.IsEmpty = function(){
    var ele = this.cfg.fields;
    return ( ele.addr1.value.trim().length == 0 && ele.addr2.value.trim().length == 0 &&
        ele.city.value.trim().length == 0 && ele.zip.value.trim().length == 0 ) ? true: false;
}
Addr.prototype.CheckSuccess = function(o){
    alert('success');
    if(o.responseText != undefined ){
    }
}
Addr.prototype.CheckFailure = function(o){
}
Addr.prototype.CheckAddress = function(){
    var ele=this.ele;
    var postData =  "addr1="+this.getVal(ele.addr1)+"&addr2="+this.getVal(ele.addr2)+"&addr3="+
                    "&city="+this.getVal(ele.city)+"&state="+ele.state.options[ele.state.selectedIndex].text+
                    "&zip="+this.getVal(ele.zip)+"&zipp="+"&frmt=json";
    var callback =
    {
      success:delegate(this,this.CheckSuccess),
      failure: delegate(this,this.CheckFailure)
    };
    
    var sUrl = "/webservice/addressvalidate.asmx/ValidateAddress?"+Math.floor(Math.random() * 1000000);    
    var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}
