
jQuery(function($) {
    
    if ( _formjson != null ) {
        for ( field in _formjson ) {
            if (field.substr(0,2) == 'a_') {
                if (field == 'a_artisttype') {
                    var f = $('#'+field).get(0);
                    for (i=0; i<f.options.length; i++) {
                        if (f.options[i].value == _formjson[field]) {
                            f.selectedIndex = i;
                            break;
                        }
                    }
                } else {
                    $('#'+field).get(0).value = _formjson[field];   
                }
            }
        }

        if ( _formjson['_ferror'] ) {
            var ferr = 'Photo Errors:<br/>';
            for ( field in _formjson['_ferror'] ) {
                ferr += " - " + field.substr(2).replace(/_/g,' ').replace('sm','Small').replace('lg','Large').replace('photo','Photo');
                ferr += ": " + _formjson['_ferror'][field].join(' ') + "<br/>";
            }
            $('#photoerrors').show().html(ferr);
        }
        
        //if ( _formjson['_files'] ) {
        //    for ( field in _formjson['_files'] ) {
        //        $('#'+field+'_curr').show().html('Current: <a class="lightbox" href="/media/temp/photos/' + _formjson['_files'][field] + '">' + _formjson['_files'][field] + '</a>');
        //    }
        //}
        
        $('a.lightbox').lightBox({txtOf: '', imageBtnClose: '/images/lightbox-btn-close.gif'});
    }

    var txtformat = "%1 characters or less";
    
    $('#a_mediadesc').charCounter(60, {
        container: "#a_mediadesc_count",
        delay: 100,
        format: txtformat
    });
    
    $("#a_statement").charCounter(250, {
        container: "#a_statement_count",
        delay: 100,
        format: txtformat
    });

    $("#a_bio").charCounter(500, {
        container: "#a_bio_count",
        delay: 100,
        format: txtformat
    });
    
    $.validator.addMethod("accept", function(value, element, param) {
        if ( value != '' ) {
            return value.match(new RegExp("." + param + "$", "i"));
        } 
        return true;
    }, "&nbsp;&nbsp;Incorrect file format"
    );
    $.validator.addMethod("phone", function(pnumber) {
        var stripped = pnumber.replace(/[\(\)\.\-\ ]/g,'');
        if (isNaN(parseInt(stripped)) || !(stripped.length == 10)) {
            return false;
        }else{
            return true;
        }
    }, "&nbsp;&nbsp;Invalid phone"
    );
    
    jQuery.validator.messages.required = "&nbsp;&nbsp;Required";
    jQuery.validator.messages.email = "&nbsp;&nbsp;Invalid email";
    
    $("#regform").validate({                                     
        errorElement: "span",
        errorPlacement: function(error, element) {
            error.appendTo( element.prev("label") );
        },
        
        rules: {
            a_artisttype: "required",
            a_name: "required",
            a_email: {
                required: true,
                email: true
            },
            a_phone: {
                required: true,
                phone: true
            },
            a_mediadesc: "required",
            a_statement: "required",
            a_bio: "required",
            
            a_photo_1_lg: { 
                accept: "(jpe?g|gif|png)" 
            },
            a_photo_1_sm: { 
                accept: "(jpe?g|gif|png)" 
            },            
            a_photo_2_lg: { 
                accept: "(jpe?g|gif|png)" 
            },
            a_photo_2_sm: { 
                accept: "(jpe?g|gif|png)" 
            },
            a_mmedia1: { 
                accept: "(avi|mp3|mpg|wav)" 
            },
            a_mmedia2: { 
                accept: "(avi|mp3|mpg|wav)" 
            }
        }
        
    });    
    
});

