/* S E A R C H */
function EventSearchForm_onLoad() {
    // If a country is selected, populate the state list.
    var f = document.getElementById( "mainForm" );
    var c = f.country_id;
    if ( c != null ) {
        var countryID = core_getValue( c );
        if( ! core_isStringBlank( countryID ) ) {
            EventEdit_chgCountry( countryID, "state_region_id" );
        }
    }
}

function EventSearch_checkEnableProxUI( prox, enabled ) {

    var textUI = document.getElementById( "EventSearch_zipUI" )
    if ( enabled ) {
        prox.style.display = "";
        textUI.style.display = "none";
    } else {
        prox.style.display = "none";
        textUI.style.display = "";
    }
}

function isProxEnabled() {
    var result = false;
    var prox = document.getElementById( 'EventSearch_proxUI' );
    if ( prox != null ) {
        return ( prox.style.display != "none" );
    }
    return result;
}

function EventSearch_submit() {

    if ( isProxEnabled() ) {
        var nonProxZip = document.getElementById( 'zip_postal_code' );
        if ( nonProxZip != null ) {
            nonProxZip.value = "";
        }
    } else {
        var proxZip = document.getElementById( 'zip_origin' );
        if ( proxZip != null ) {
            proxZip.value = "";
        }
    }

    var zipElement = document.getElementById( 'zip_origin' );
    if ( zipElement != null &&
         zipElement.style.display != "none" &&
         !core_isStringBlank( zipElement.value ) &&
         !EventSearch_checkProximiyZip( zipElement ) ) {
            alert( 'Zip Code must be 5 digits.' );
            zipElement.focus();
            return false;
    }

    return true;
}

function EventSearch_chooseDatePeriod( obj ) {

    var val = obj.options[obj.options.selectedIndex].value;
    var f = obj.form;
    var sDate = f.start_date;
    var eDate = f.end_date;

    var newDate = new Date();

    sDate.value = newDate.getFullYear() + "-" + (newDate.getMonth()+1) + "-" + newDate.getDate();

    // end date = today + 1 month
    if ( val == 1 ) {
        newDate.setDate(newDate.getDate()+30);
    }

    // end date = today + 2 months
    if ( val == 2 ) {
        newDate.setDate(newDate.getDate()+60);
    }

    // end date = today + 3 months
    if ( val == 3 ) {
        newDate.setDate(newDate.getDate()+90);
    }

    eDate.value = newDate.getFullYear() + "-" + (newDate.getMonth()+1) + "-" + newDate.getDate();

}


function EventSearch_checkProximiyZip( zip ) {
    var patternUS = /^[0-9]{5} */;
    if ( zip == null ) return true;
    return zip.value.match(patternUS);
}


// common
function EventEdit_chgCountry( countryID, stateField, selectedState ) {
    var sel = document.getElementById( stateField );
    if( sel != null && sel.type != "hidden" ) {
        sel.options.length = 1;
        var hasStates = false;
        for ( var i = 0 ; i < countryStateMap.length ; i++ ) {
            var map = countryStateMap[i];
            if ( map[0] == countryID ) {
                hasStates = true;
                sel.options[sel.options.length] = new Option( map[2], map[1] );
                if ( map[1] == selectedState ) {
                    sel.selectedIndex = sel.options.length;
                }
            }
        }
        if ( selectedState != null ) {
            var s = document.getElementById( "state_region_id" );
            for (var i = 0 ; i < s.options.length ; i++ ) {
                if ( s.options[i].value == selectedState ) {
                    s.options[i].selected = true;
                }
            }
        }
        if ( hasStates ) {
            sel.disabled = false;
            sel.required = true;
        } else {
            sel.disabled = true;
            sel.required = false;
        }
    }
    var prox = document.getElementById( 'EventSearch_proxUI' );
    if ( prox != null ) {
        EventSearch_checkEnableProxUI( prox, (countryID == '10277') );
    }
}

