﻿var err_nobarcode      = ['Please, enter identifier of the posting.',
                          'Введите, пожалуйста, номер почтового отправления.'];
var err_invalidbarcode = ['You have entered incorrect posting identifier!',
                          'Введен неправильный идентификатор почтового отправления!'];
var err_nocaptcha      = ['Please, enter captcha.',
                          'Введите, пожалуйста 2 слова капчи.'];

function checkBarcode ( barcode )
{
    re = /^([A-Z]{2}\d{8}(\d|X)[A-Z]{2})|(\d{13})$/;
    return re.test ( barcode );
}

// search for barcode in PS database
function doTrack ()
{
    var query = document.getElementById('barcode').value.toUpperCase();
    if ( query == '' ) {
        alert ( err_nobarcode[1] );
        document.getElementById('barcode').focus ();
        return false;
    }
    if ( !checkBarcode ( query ) ) {
        alert ( err_invalidbarcode[1] );
        return false;
    }
//    if ( document.getElementById('recaptcha_response_field').value == '' ) {
//        alert ( err_nocaptcha[1] );
//        document.getElementById('recaptcha_response_field').focus ();
//        return false;
//    }
}

// ajax form posting
$(document).ready(function() {
    var options = { 
        target:        '#tracklog',
        beforeSubmit:  showRequest,  
        success:       showResponse
    }; 
    $('#theForm').ajaxForm(options); 
});

// pre-submit callback 
function showRequest ( formData, jqForm, options ) {
    // formData is an array; here we use $.param to convert it to a string 
    // to display it but the form plugin does this for you automatically 
    // when it submits the data 
    // var queryString = $.param ( formData ); 
 
    // jqForm is a jQuery object encapsulating the form element.  
    // To access the DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    // alert ( 'About to submit: \n\n' + queryString );
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to 
    // continue 
    // return true; 

    $('#tracklog').html('');
    $("<div id='loader'><img align='absmiddle' src='template/slimbox/css/loading.gif'> Поиск почтового отправления...</div>").appendTo("#tracklog").hide();
    $('#tracklog').css('display','block');
    $('#loader').fadeIn(300);

    return doTrack ();
} 
 
// post-submit callback 
function showResponse ( responseText, statusText, xhr, $form )  {
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    // alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    // '\n\nThe output div should have already been updated with the responseText.'); 
    $('#loader').remove ();
} 
