var max_chars = 200;
var test_id_len = 10;

$(document).ready(function() {
						   
    $(".uploadFormSubmit").click(function() {
        document.pet_upload.submit();
    });

    $("input#reportCode").focus(function() {
        if( !$(this).hasClass('active') ) {
            $(this).addClass('active');
        }
    });

    $("input#reportCode").blur(function() {
        if( $(this).hasClass('active') ) {
            $(this).removeClass('active');
        }
    });

    $('input#reportCode').keypress(function(e){
        if($(this).attr("value").length < test_id_len || e.which == 8) {
            return ( e.which >= 48 && e.which <= 57 ) || e.which == 8;
        } else {
            return false;
        }
    });

    $('input#reportCode').keyup(function(e){
        if($(this).attr("value").length == test_id_len) {
            checkStatus($(this).attr("value"));
        } else {
            if( $('input#reportCode').hasClass('warning') ) {
                $('input#reportCode').removeClass('warning');
            }
            $('#messageInvalid').hide();
            $('#verifyStatusText').hide();
            $('input#codeStatus').attr("value","");
        }
    });

    $('textarea.characterCount').keyup(function(){
        if(this.value.length >= max_chars) {
            $('#charsLeft').addClass('overlimit');
            this.value = this.value.substring(0, max_chars);
        } else {
            $('#charsLeft').removeClass('overlimit');
        }

        $('#charsLeft').text(max_chars-this.value.length);
    });
});

function checkStatus(test_id)
{
    if( $('input#reportCode').hasClass('warning') ) {
        $('input#reportCode').removeClass('warning');
    }
    $('input#reportCode').blur().attr("disabled", "disabled").removeClass('active').addClass('loading');
    $('#verifyStatusText').show();

    url = $('input#pageURL').attr("value");
    $.getJSON( url + "/tid" + test_id,
    function(data){
        $('#additionalUploads').slideDown('fast');
        $('input#reportCodeValue').attr("value",$('input#reportCode').attr("value"));

        if(data.status == 'OK') {
            $('#verifyStatusText').text("Success!");
            $('input#reportCode').removeClass('loading').addClass('valid');
            $('#messageValid').html("<p>" + data.message + "</p>").slideDown('fast');
            $('input#codeStatus').attr("value","valid");
            $('input#reportCodeCity').attr("value",data.city);
            $('input#reportCodeState').attr("value",data.state);
            $('input#reportType').attr("value",data.reporttype);
            $('input#dateTested').attr("value",data.datetested);
            $('input#testType').attr("value",data.testtype);
            var x = data.breeds;
            for (var i=0;i<x.length;i++) {
                $('form#photoUpload').append('<input type="hidden" name="breed_names[]" value="' + x[i].name + '" />')
                    .append('<input type="hidden" name="breed_proportions[]" value="' + x[i].proportion + '" />');
            }
        } else {
            $('#verifyStatusText').text("Ooops!");
            $('input#reportCode').attr("disabled", "").removeClass('loading').addClass('warning');
            $('#messageInvalid').html("<p>" + data.message + "</p>").slideDown('fast');
            $('input#codeStatus').attr("value","invalid");
        }
    });

    return true;
}