var myZipLookup_Instances = new Array();
// public functions ************************************************

function ZipLookup_New(zipid, cityid, countyid, stateid, path2pg, timerimg) {
    var next = ZipLookup_FindInstance(zipid)    
    myZipLookup_Instances[next] = new ZipLookup(zipid, cityid, countyid, stateid, timerimg);
    ZipLookup_Path2Pg = path2pg;
    if (!document.getElementById(ZipLookup_idIframe)) {
        //ZipLookup_CreateIframe(path2pg);
    }
}
 
function ZipLookup_Go(zipid, zipValue) {//interface to class
    var found = ZipLookup_FindInstance(zipid)
    if (found < myZipLookup_Instances.length) myZipLookup_Instances[found].Go(zipValue)
} 

function ZipLookup_BuildCounty(zipid, city) {//interface to class
    var found = ZipLookup_FindInstance(zipid)
    if (found < myZipLookup_Instances.length) myZipLookup_Instances[found].BuildCountySelect(city)
} 

function ZipLookup_BuildState(zipid, state, ignoreblank) {//interface to class
    var found = ZipLookup_FindInstance(zipid)
    if (found < myZipLookup_Instances.length) myZipLookup_Instances[found].BuildStateSelect(state, ignoreblank)
} 

function ZipLookup_UpdateInfo(zipid, list, state, city, county, ok, ignoreblank) {
//called by the zip look page running in the hidden iframe

    var found = ZipLookup_FindInstance(zipid)
    if (found < myZipLookup_Instances.length) {
        myZipLookup_Instances[found].UpdateStateCityCounty(list, state, city, county);
        
        myZipLookup_Instances[found].SetSelect('State', state, ok);
        myZipLookup_Instances[found].SetSelect('City', city, ok);
        myZipLookup_Instances[found].SetSelect('County', county, ok);
    }
}

function ZipLookup_UpdateInfo2(zipid, list, state, city, county, stateok, cityok, countyok, ignoreblank) {
//called by the zip look page running in the hidden iframe
    var found = ZipLookup_FindInstance(zipid)
    if (found < myZipLookup_Instances.length) {
        myZipLookup_Instances[found].UpdateStateCityCounty(list, state, city, county, ignoreblank);
        
        myZipLookup_Instances[found].SetSelect('State', state, stateok);
        myZipLookup_Instances[found].SetSelect('City', city, cityok);
        myZipLookup_Instances[found].SetSelect('County', county, countyok);
    }
}

// class static members ************************************************
var ZipLookup_idIframe = 'ziplookupiframe0' 
var ZipLookup_Path2Pg = '';

function ZipLookup_FindInstance(zipID) {
// returns the index for the instance matching the specified zipID, or else the next empty instance
    var found = myZipLookup_Instances.length;
    if (myZipLookup_Instances && myZipLookup_Instances[0]) {
        var i = 0;
        for (; (i < myZipLookup_Instances.length) && (myZipLookup_Instances[i].idZip != zipID); i++){}
        found = i;
    }
    return found;
}
 
// class definition ************************************************

function ZipLookup(zip, city, county, state, timerimg) {
    // *** public attributes
    this.idZip = zip;
    this.idCity = city;
    this.idCounty = county;
    this.idState = state;
    this.imgTimercircle = timerimg;

    // *** private attributes
    this.state_city_county = null;
    this.stateI = 0;
    this.cityI = 1;
    this.countyI = 2;
    this.option1msg = '-- # --'
    this.timerImgPath = 'NetLibrary/Images/AjaxIcons/'    
    this.timerImg = (timerimg)?  timerimg : 'circles.gif'    
    this.NotOKBgColor = '#FFF520'; //'#CC0000';
    this.NotOKColor = 'black'; //'#FFFFFF';
    this.OKBgColor = '#FFFFFF'; //'#CC0000';
    this.OKColor = 'black'; 
        
    // *** Methods
    this.Go = ZipLookup_Fire;
    this.UpdateStateCityCounty = ZipLookup_UpdateStateCityCounty;
    this.StateCityCounty_Reset = ZipLookup_StateCityCounty_Reset;
    this.StateCityCounty_Add = ZipLookup_StateCityCounty_Add;
    this.BuildSelect = ZipLookup_BuildSelect;
    this.ResetSelect = ZipLookup_ResetSelect;    
    this.SingleCounty = ZipLookup_SingleCounty;
    this.GetFirstOption = ZipLookup_GetFirstOption;
    this.DisplayWaitImg = ZipLookup_DisplayWaitImg;
    this.DdlClear = ZipLookup_DdlClear;
    this.BlankValue = ZipLookup_BlankValue;
    this.FormatOptionMsg = ZipLookup_FormatOptionMsg;
    this.BuildCitySelect = ZipLookup_BuildCitySelect;
    this.BuildCountySelect = ZipLookup_BuildCountySelect;
    this.SetState = ZipLookup_SetState;   
    this.SetSelect = ZipLookup_SetSelect;
    this.BuildStateSelect = ZipLookup_BuildStateSelect;
    this.MarkOption = ZipLookup_MarkOption;
}

function ZipLookup_UpdateStateCityCounty(list, state, city, county, ignoreblank) {
    this.StateCityCounty_Reset(); //needs to reset every new search
    if (list) {
        var temp = list.split(',');
        for (i=0; i< temp.length; i+=3) {// expects triplets
            this.StateCityCounty_Add(this.BlankValue(temp[i]), this.BlankValue(temp[i+1]), this.BlankValue(temp[i+2]));
        }
        
        this.BuildCitySelect((state)? state: temp[this.stateI], ignoreblank);
        this.BuildCountySelect((city)? city :  (this.SingleCounty())? temp[this.cityI] : this.GetFirstOption(this.idCity), ignoreblank)
        this.BuildStateSelect((state)? state: temp[this.stateI], ignoreblank);
    }
    else
    {
        this.ResetSelect(this.idCity, 'Not found');
        this.ResetSelect(this.idCounty, 'Not found');
        this.SetState('??');
    }
    
    this.DisplayWaitImg(false);
}

function ZipLookup_StateCityCounty_Reset() {
    this.state_city_county = null;
    this.state_city_county = new Array();
}

function ZipLookup_StateCityCounty_Add(state, city, county) {
    if (this.state_city_county) {
        var i = this.state_city_county.length;
        if (this.state_city_county[i]) i++; //add to next position if already used.
        this.state_city_county[i] = new Array(3);
        this.state_city_county[i][this.stateI] = state;
        this.state_city_county[i][this.cityI] = city;
        this.state_city_county[i][this.countyI] = county;
    }
}

function ZipLookup_Fire(zip) {
// this is initiates the zip lookup
    if (zip && zip.length > 0 ) {
        this.ResetSelect(this.idCity, '');
        this.ResetSelect(this.idCounty, '');    
        
        this.DisplayWaitImg(true);
        
        document.getElementById(ZipLookup_idIframe).src = ZipLookup_Path2Pg+ '/Library/ziplookup.asp?id='+ this.idZip +'&zip='+zip
    }
}

function ZipLookup_BuildCitySelect(state) {    
    if (state && state.length > 0 ) 
        this.BuildSelect(this.idCity, 'this.cityI', 'this.stateI', state, false);
    else
        this.ResetSelect(this.idCity, 'Enter valid postal code');

    var ddl = document.getElementById(this.idCity);
    if (ddl.options.length == 1 && ddl.options[0].value == '') this.MarkOption (ddl.options[0], false);   
    
}

function ZipLookup_BuildCountySelect(city) {
    if (city && city.length > 0 ) 
        this.BuildSelect(this.idCounty, 'this.countyI', 'this.cityI', city, true);
    else
        this.ResetSelect(this.idCounty, 'Select city');        

    var ddl = document.getElementById(this.idCounty);
    if (ddl.options.length == 1 && ddl.options[0].value == '') this.MarkOption (ddl.options[0], false);   
}

function ZipLookup_BuildStateSelect(state, ignoreblank) {
    if (state && state.length > 0 ) 
        this.BuildSelect(this.idState, 'this.stateI', '', '', false, ignoreblank);
    else
        this.ResetSelect(this.idState, 'Enter valid postal code');        

    var ddl = document.getElementById(this.idState);
    if (ddl.options.length == 1 && ddl.options[0].value == '') this.MarkOption (ddl.options[0], false);   
}

function ZipLookup_SetState(value) {
    obj = document.getElementById(this.idState);
    switch (obj.type) {
        case 'select-one': //ddl select
            this.DdlClear(obj);
            obj.options[0] = new Option(value, value); //single option
            break;
            
        default: //text input
            obj.value = value;    
    }
    
}

// general build select
function ZipLookup_BuildSelect(ddlId, indexName, filterName, filterValue, repeat, ignoreblanks) {
    try {
        var ddlGen = document.getElementById(ddlId);
        var indexValue = eval(indexName);
        var indexFilter = (filterName)? eval(filterName) : null;
        var previous = '';
        this.DdlClear(ddlGen); //reset select
        if (this.state_city_county && this.state_city_county.length > 0) {
            var options = new Array();
            for (var i=0, j=0; i < this.state_city_county.length; i++) {
                if (indexFilter != null) {
                    if (this.state_city_county[i][indexFilter].toUpperCase() == filterValue.toUpperCase() && (repeat || previous.toUpperCase() != this.state_city_county[i][indexValue].toUpperCase())) {
                        if (!ignoreblanks || this.state_city_county[i][indexValue].length>0) options[j++] = new Option(this.state_city_county[i][indexValue], this.state_city_county[i][indexValue]);
                     }
                 }
                 else
                    if ( repeat || previous.toUpperCase() != this.state_city_county[i][indexValue].toUpperCase() ) {
                        if (!ignoreblanks || this.state_city_county[i][indexValue].length>0) options[j++] = new Option(this.state_city_county[i][indexValue], this.state_city_county[i][indexValue]);
                     }
                    
                 previous = this.state_city_county[i][indexValue];
            }
            
            this.ResetSelect( ddlId, (options.length > 1)? 'Please Choose One' : '' )
            var j = ddlGen.options.length;
            for (var i=0; i < options.length ; i++) {
                ddlGen.options[i+j] = options[i];
            }
        }
        else
            ddlGen.options[0] = new Option('Not found', '');
        
        return  ddlGen.options.length;       
   } catch (ex) {}
}

function ZipLookup_GetFirstOption(id) {
    return document.getElementById(id).options[0].value;
}

function ZipLookup_DdlClear(ddl) {ddl.options.length = 0}

function ZipLookup_ResetSelect(id, firstoption) {
    var ddl = document.getElementById(id);
    ddl.options.length = 0;
    if (firstoption && firstoption.length>0) {
        ddl.options[0] = new Option(this.FormatOptionMsg(firstoption), '');        
    }
}

function ZipLookup_BlankValue(val) {
    if (!val) return '';
    return val;
}

function ZipLookup_FormatOptionMsg(msg) {
    return this.option1msg.replace('#', msg)
}

function ZipLookup_DisplayWaitImg(on) {
    try {
        var box = document.getElementById(this.idCity)
        box.style.backgroundImage = "none";        
        if (on) {
            box.style.backgroundImage = "url('"+ZipLookup_Path2Pg + this.timerImgPath + this.timerImg+"')";
            box.style.backgroundRepeat = "no-repeat";        
            box.style.backgroundPosition = "left center";        
        }
    } catch (ex) {}
}

function ZipLookup_SingleCounty() {
    var previous = this.state_city_county[0][this.countyI]; //get the first
    for (i=0; i < this.state_city_county.length && previous.toUpperCase() == this.state_city_county[i][this.countyI].toUpperCase();i++){}
    return (i == this.state_city_county.length);
}


function ZipLookup_CreateIframe(path2pg) {
   ifrm = document.createElement('iframe'); 
   ifrm.setAttribute('id', ZipLookup_idIframe);
   ifrm.setAttribute('width', '0');
   ifrm.setAttribute('height', '0');
   ifrm.setAttribute('src', path2pg+'blank.html'); //this is require to prevent an error with iframe not initializing with a src file out of the ssl realm
   document.body.appendChild(ifrm); 
}

function ZipLookup_SetSelect(idtype, value, ok) {
    var ddl = document.getElementById(eval('this.id' +idtype))  
    for(i=0; i< ddl.options.length; i++) {
        myvalue = (value)? value.toUpperCase() : '';
        if (ddl.options[i].value.toUpperCase() == myvalue) {
            ddl.options[i].selected = true;
            if (ok == false) this.MarkOption(ddl.options[i], false);            
        }
    } 
}

function ZipLookup_ActivateWarningClue(id, on) {
    try {
        document.getElementById(id).style.display = (on == false)? 'none': '';
    } catch (ex) {}
}

function ZipLookup_MarkOption(opt, ok) {
    if (ok == true) {
        opt.style.backgroundColor = this.OKBgColor;            
        opt.style.color = this.OKColor;            
    }
    if (ok == false) {
        opt.style.backgroundColor = this.NotOKBgColor;            
        opt.style.color = this.NotOKColor;            
    }
}

function ZipLookup_DupSelect(sel1, sel2) { // sel2 = sel1
    if (sel1 && sel2) {
        sel1 = document.getElementById(sel1);
        sel2 = document.getElementById(sel2);
        sel2.options.length = 0; //reset
        for (i=0; i<sel1.options.length; i++){
            sel2.options[i] = new Option(sel1.options[i].text, sel1.options[i].value);
            sel2.options[i].selected = sel1.options[i].selected;
        }
        sel2.options.style.backgroundColor = sel1.options.style.backgroundColor;
        sel2.options.style.color = sel1.options.style.color;
    }
}

function ZipLookup_BlankFields(fieldlist) {
    if (fieldlist) {
        var alist = fieldlist.split(',')
        for (i=0; i< alist.length; i++) {
            if (document.getElementById(alist[i])) document.getElementById(alist[i]).value = '';
        }
    }
}
