// Global used to indicate count of media items satisfying current filters, used in entry detail page
var totalCount = 0;

var provinces = new Array (
    'AB', 'ON', 'MB', 'QC', 'NS', 'NB', 'BC', 'PE', 'SK', 'NL', 'NT', 'YT', 'NU', 'AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE',
    'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH',
    'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'
);

var categories = $A( $R( 482, 503 ) );

function validateRequired ( field ) {
    if ( String ( $( field ).value ).strip ().length == 0 )
        return field.id;
    else
        return false;
}

function validateProvince ( field ) {
    if ( provinces.indexOf ( String ( $( field ).value ).strip () ) == -1 )
        return field.id;
    else
        return false;
}

function validateCategory ( field ) {
    if ( categories.indexOf ( parseInt ( $( field ).value ) ) == -1 )
        return field.id;
    else
        return false;
}

function validatePostalCode ( field ) {
    var stripped = $( field ).value.replace ( /[^a-zA-Z0-9]/g, '' ).toUpperCase ();

    if ( !/^[A-Z]\d[A-Z]\d[A-Z]\d$/.test ( stripped ) && !/^([\d]{5})|([\d]{9})$/.test ( stripped ) )
        return field.id;
    else
        return false;
}

function validatePhoneNumber ( field ) {
    if ( !/^[2-9]\d{2}[2-9]\d{6}$/.test ( $( field ).value.replace ( /[^0-9]/g, '' ) ) )
        return field.id;
    else
        return false;
}

function redirectToPreviousDetail () {
    var params = document.location.toString ().toQueryParams ();

    if ( params.offset == null )
        return false;

    params.offset = parseInt ( params.offset );

    if ( params.offset != 0 )
        document.location = document.location.toString ().replace ( /offset=[0-9]+/, 'offset=' + ( params.offset - 1 ) ).replace ( /#comments/, '' );
    else
        return false;
}

function redirectToNextDetail () {
    var params = document.location.toString ().toQueryParams ();

    if ( params.offset == null )
        return false;

    params.offset = parseInt ( params.offset );

    if ( params.offset < ( parseInt ( totalCount ) - 1 ) )
        document.location = document.location.toString ().replace ( /offset=[0-9]+/, 'offset=' + ( params.offset + 1 ) ).replace ( /#comments/, '' );
    else
        return false;
}

function highlightCurrentPage () {
    var pages = $H ( {
        '/home': 'navHome',
        '/rules': 'navRules',
        '/prizes': 'navPrizes',
        '/enter': 'navEnter'
    } );

    var location = document.location.toString ();

    pages.each ( function ( pair ) {
        if ( location.include ( pair.key ) ) {
            $( pair.value ).addClassName ( 'active' );

            return false;
        }
    } );

    var category = /category=([0-9]+)/.exec ( location );

    if ( category ) {
        var navCategory = $( 'navCat' + category [ 1 ].toString () );

        if ( navCategory ) {
            navCategory.addClassName ( 'active' );

            return false;
        }
    }

    if ( location.include ( '/gallery' ) )
        $( 'navCatAll' ).addClassName ( 'active' );
}

function voteSubmit () {
    var source = $( 'afterMediaID' );
    var destination = $( 'voteMediaID' );

    if ( source && destination )
        destination.value = source.value;
}

function voteAJAX ( mediaID ) {
    jsonRequest (
        'media.rateFile',
        {
            'id': mediaID,
            'rating': 10
        },
        function ( result ) { $( 'voteCount_' + mediaID ).innerHTML = result.toString (); },
        function ( exception ) { /* no action */ }
    );
}

AppInit.addEvent ( highlightCurrentPage );

function showShareEmail () {
    if ( $( 'entryShare' ) ) $( 'entryShare' ).setStyle ( { 'display': 'block' } );
}

function hideShareEmail () {
    if ( $( 'entryShare' ) ) $( 'entryShare' ).setStyle ( { 'display': 'none' } );
    if ( $( 'entryUserData' ) ) $( 'entryUserData' ).setStyle ( { 'display': 'block' } );
}
