﻿
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 <= 8) {
        if (letters >= 3) {
            text = textBox.value;
            for (start = 3; start <= letters; start = start + 1) {
                if (textBox.value.charAt(start - 2) != '-') {
                    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) {
                var img = $('#' + image);
                if (img != null && !noPostback) {
                    img.css('display', 'block');
                }

                $('#imgUwAuto').css("display", "block");

                if (!noPostback) {
                    
                     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');
                }

                if (Cars.UwAuto == "true") {
                    $.getJSON(_siteRoot + 'Ajax/Car/GetCarDetails.aspx?licence=' + textBox.value, function (data) {
                        if (data != null) {
                            if (data.error == "False") {
                                //$('#tdCarPerformace').html(data.carImages);
                                $('#tdCarDetails').html(data.carDetails + data.carPerformace);
                            }
                            else {
                                $('#tdCarDetails').html("");
                            }
                        }
                    });
                }

                $.getJSON(_siteRoot + 'Ajax/Car/GetCarImage.aspx?licence=' + textBox.value, function (data) {
                    if (data != null) {
                        if (data.error == "False") {
                            $('#tdCarName').html(data.carName);
                            $('#tdCarPerformace').html(data.carImages);
                            $('#tdTyres').html("Uw bandenmaat: " + data.carTyre);
                        }
                        else {
                            $('#tdCarName').html("U heeft geen geldig kenteken ingevoerd");
                            $('#tdTyres').html("");
                            $('#tdCarPerformace').html("");
                        }
                    }
                });

                $('#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/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) {
                    }

                });
            }

            if (Cars.UwAuto == "true") {
                $.getJSON(_siteRoot + 'Ajax/Car/GetCarDetails.aspx?licence=' + textBox.value, function (data) {
                    if (data != null) {
                        //$('#tdCarPerformace').html(data.carImages);
                        $('#tdCarDetails').html(data.carDetails + data.carPerformace);
                    }
                });

                $.getJSON(_siteRoot + 'Ajax/Car/GetCarImage.aspx?licence=' + textBox.value, function (data) {
                    if (data != null) {
                        $('#tdCarName').html(data.carName);
                        $('#tdCarPerformace').html(data.carImages);
                        $('#tdTyres').html("Uw bandenmaat: " + data.carTyre);
                    }
                });
            }

            $('#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;
    }
}
