﻿
function moveFocus(text1, callBackFunction, callBackFunctionParams, image, noPostback) {
    var textBox = text1;
    textBox.value = $.trim(textBox.value);
    var letters = textBox.value.length + 1;
    var text;
    var start;

    if (letters <= 10) {
        if (letters >= 3) {
            text = textBox.value;
            for (start = 3; start <= letters; start = start + 1) {
                if (textBox.value.charAt(start - 3) != '-') {
                    if (((isNaN((textBox.value.charAt(start - 2)))) && (!isNaN(textBox.value.charAt(start - 3))) && (textBox.value.charAt(start - 3) != '-') && (textBox.value.charAt(start - 2) != '-'))
                    || ((!isNaN((textBox.value.charAt(start - 2)))) && (isNaN(textBox.value.charAt(start - 3))) && (textBox.value.charAt(start - 3) != '-') && (textBox.value.charAt(start - 2) != '-'))) {
                        text = textBox.value;
                        textBox.value = text.substring(0, start - 2) + "-" + text.substring(start - 2);
                        text = textBox.value;
                        textBox.focus();
                    }
                    if (((isNaN(textBox.value.charAt(start - 2))) && (isNaN(textBox.value.charAt(start - 3))) && (isNaN(textBox.value.charAt(start - 4))) && (isNaN(textBox.value.charAt(start - 5))) && (textBox.value.charAt(start - 5) != '-') && (textBox.value.charAt(start - 4) != '-') && (textBox.value.charAt(start - 3) != '-') && (textBox.value.charAt(start - 2) != '-'))
                        || ((!isNaN(textBox.value.charAt(start - 2))) && (!isNaN(textBox.value.charAt(start - 3))) && (!isNaN(textBox.value.charAt(start - 4))) && (!isNaN(textBox.value.charAt(start - 5))) && (textBox.value.charAt(start - 5) != '-') && (textBox.value.charAt(start - 4) != '-') && (textBox.value.charAt(start - 3) != '-') && (textBox.value.charAt(start - 2) != '-') && (textBox.value.charAt(start - 4) != '') && (textBox.value.charAt(start - 5) != ''))) {
                        textBox.value = text.substring(0, start - 3) + "-" + text.substring(start - 3);
                        textBox.focus();
                    }

                }
            }
            //alert('mumu1');
            if ((letters == 8 && textBox.value.charAt(3) == '-') || (letters == 10 && !isNaN(textBox.value.charAt(0)) && (textBox.value.charAt(1) == '-'))) {
                var img = $('#' + image);
                if (img != null) {
                    img.css('display', 'block');
                }


                $('#imgUwAuto').css("display", "block");
                path = _siteRoot + 'Ajax/Utils.aspx?method=GetCarDetails&licenceplate=' + textBox.value;
                $.get(path, function (result) {
                    try {
                        var q = "(" + result + ")";
                        var json = eval(q);
                        eval(callBackFunction + "(json)");
                        if (img != null) {
                            img.css('display', 'none');
                        }

                    } catch (e) {
                    }

                });
                img.css('display', 'none');

                $('#imgUwAuto').css("display", "none");

                textBox.blur();
            }
        }
        else {
            textBox.focus();
        }
    }
    else {
        if (IsLicenceValid(text1)) {

            $('#imgUwAuto').css("display", "block");

            textBox.value.length = 8;
            if (!noPostback) {
                path = _siteRoot + 'Ajax/Ajax_LicencePlate.aspx?method=GetCarDetails&licenceplate=' + textBox.value;
                $.get(path, function(result) {
                    try {
                        var q = "(" + result + ")";
                        var json = eval(q);
                        eval(callBackFunction + "(json)");

                    } catch (e) {
                    }

                });
            }

            $('#imgUwAuto').css("display", "none");

            textBox.blur();
        }
    }
}

function EmptyBox(tb) {
    tb.value = "";
}

function IsLicenceValid(text1) {

    var licence1 = text1.value;

    if (licence1 == null) {
        return false;
    }

    if (licence1.length == 8) {
        return true;
    }
    else {
        return false;
    }
}

function GetLeasingCompany(text1) {
    var textBox = text1;
    textBox.value = $.trim(textBox.value);
    var letters = textBox.value.length + 1;
    var text;
    var start;

    if (letters <= 10) {
        if (letters >= 3) {
            text = textBox.value;
            for (start = 3; start <= letters; start = start + 1) {
                if (textBox.value.charAt(start - 3) != '-') {
                    if (((isNaN((textBox.value.charAt(start - 2)))) && (!isNaN(textBox.value.charAt(start - 3))) && (textBox.value.charAt(start - 3) != '-') && (textBox.value.charAt(start - 2) != '-'))
                    || ((!isNaN((textBox.value.charAt(start - 2)))) && (isNaN(textBox.value.charAt(start - 3))) && (textBox.value.charAt(start - 3) != '-') && (textBox.value.charAt(start - 2) != '-'))) {
                        text = textBox.value;
                        textBox.value = text.substring(0, start - 2) + "-" + text.substring(start - 2);
                        text = textBox.value;
                        textBox.focus();
                    }
                    if (((isNaN(textBox.value.charAt(start - 2))) && (isNaN(textBox.value.charAt(start - 3))) && (isNaN(textBox.value.charAt(start - 4))) && (isNaN(textBox.value.charAt(start - 5))) && (textBox.value.charAt(start - 5) != '-') && (textBox.value.charAt(start - 4) != '-') && (textBox.value.charAt(start - 3) != '-') && (textBox.value.charAt(start - 2) != '-'))
                        || ((!isNaN(textBox.value.charAt(start - 2))) && (!isNaN(textBox.value.charAt(start - 3))) && (!isNaN(textBox.value.charAt(start - 4))) && (!isNaN(textBox.value.charAt(start - 5))) && (textBox.value.charAt(start - 5) != '-') && (textBox.value.charAt(start - 4) != '-') && (textBox.value.charAt(start - 3) != '-') && (textBox.value.charAt(start - 2) != '-') && (textBox.value.charAt(start - 4) != '') && (textBox.value.charAt(start - 5) != ''))) {
                        textBox.value = text.substring(0, start - 3) + "-" + text.substring(start - 3);
                        textBox.focus();
                    }
                }
            }

            if ((letters == 8 && textBox.value.charAt(3) == '-') || (letters == 10 && !isNaN(textBox.value.charAt(0)) && (textBox.value.charAt(1) == '-'))) {
                var img = $("[id$='_imgLicence']");
                if (img != null) {
                    img.css('display', 'block');
                }

                path = _siteRoot + 'Ajax/Utils.aspx?method=GetCarDetails&licenceplate=' + textBox.value;
                $.getJSON(path, function (result) {
                    try {
                        if (img != null) {
                            img.css('display', 'none');
                        }

                        if (result.LeasingCompanyId != "") {
                            $('#imgLoader').css('display', 'block');
                            $('#imgleaseCompany').css('display', 'none');
                            $.getJSON(_siteRoot + 'Ajax/Lease/Lease.aspx?leaseID=' + result.LeasingCompanyId, function (data) {
                                if (data.Response != null) {
                                    $('#imgleaseCompany').attr("src", data.Response);
                                    $('#imgleaseCompany').css('display', 'block');
                                    $('#imgLoader').css('display', 'none');
                                }
                                else {
                                    $('#imgLoader').css('display', 'none');
                                }
                            });
                            $('#imgLoader').css('display', 'none');
                        }

                    } catch (e) {
                    }

                });
                img.css('display', 'none');

            }
            else {
                //alert('mumu2')
            }
        }
        else {
            textBox.focus();
            //alert('mumu3');
        }
    }
    else {
        //alert('mumu4');
    }
}
