YAHOO.namespace("tmm");

YAHOO.tmm.FormVerifier = function(originalLookupHandle){
     
    this.originalLookupHandle = originalLookupHandle;
    this.payFormat = YAHOO.util.Dom.get("PAY_FORMAT");
    this.state = YAHOO.util.Dom.get("STATE");
    this.email = YAHOO.util.Dom.get("EMAIL");
    this.militaryStatus = YAHOO.util.Dom.get("MILITARY_RELATED");
    this.monthlyIncome = YAHOO.util.Dom.get("NET_MONTHLY_INCOME");
    
    this.tmmpo = YAHOO.util.Dom.get("_TMMPO");
    this.lookupHandle = YAHOO.util.Dom.get("_fflh");
    
    YAHOO.util.Event.addListener([this.payFormat, this.state, this.monthlyIncome], "change", this.handleFieldChange, this, true);
};

YAHOO.tmm.FormVerifier.prototype = {

    handleFieldChange: function(ev){
        YAHOO.util.Event.stopEvent(ev);
        this.togglePing();
    },
    
    togglePing: function(){
        
        var stateCode = this.state != null ? this.state.value : 'CA';
        var inMilitary = this.militaryStatus != null ? this.militaryStatus : 'NO';
        var income = this.monthlyIncome.value != '' ? parseInt(this.monthlyIncome.value) : 1000;
        var regexp = /\.(gov|mil)$/;
        
        if (stateCode == 'GA' || stateCode == 'NY') { 
            this.tmmpo.value = 'true';
        } else {
            this.tmmpo.value = 'false';
        }

        if (this.payFormat.value == 'CASH') {
            this.lookupHandle.value = this.originalLookupHandle + '-payformat';
        } else if (this.payFormat.value == 'PAPER_CHECK') {
            this.lookupHandle.value = this.originalLookupHandle + '-papercheck';
        } else if (inMilitary == 'YES' || this.email.value.search(regexp) > 0) {
            this.lookupHandle.value = this.originalLookupHandle + '-military';
        } else if (income < 1000) { 
            this.lookupHandle.value = this.originalLookupHandle + '-lowincome';
        } else {
            this.lookupHandle.value = this.originalLookupHandle;
        }            
    }
};