﻿
function activeOptionList(obj, options) {
    var fadeTime = 150;

    if (obj.title == "Expand") {
        options.fadeIn(fadeTime);
        obj.title = 'Compress';
        obj.innerHTML = "Close";
    } else {
        options.fadeOut(fadeTime);
        obj.title = 'Expand';
        obj.innerHTML = "Add";
    }
}

function showOptionList(obj) {

    activeOptionList(obj, $('#option_list2'));
}

function showOptionList1(obj) {
    activeOptionList(obj, $('#option_list1'));
}

// for firefox using.
function getTextFieldSelection(e) {
    if (e.selectionStart != undefined && e.selectionEnd != undefined) {
        var start = e.selectionStart;
        var end = e.selectionEnd;
        return e.value.substring(start, end);
    } else return "";
}

function getSelectionText(textBox) {
    if (window.getSelection) {
        return getTextFieldSelection(textBox);
    }
    else if (document.selection && document.selection.createRange) {
        return document.selection.createRange().text;
    }
    return '';
}

function valNum(original, evt) {
    var originalValue = original.value;
    evt = (evt) ? evt : ((window.event) ? window.event : "");
    var key = evt.keyCode ? evt.keyCode : evt.which;
    if ((evt.ctrlKey && key == 86) || key == 17) {
        var validClip = valClip();
        if (!validClip) {
            original.value = '';
        }
        return validClip;
    }
    var charCode = (evt.which) ? evt.which : event.keyCode;
    var charText=String.fromCharCode(charCode);
    var inputVal = parseInt(charText, 10);
    if (inputVal != NaN && inputVal >= 0 && inputVal <= 9) {
        if (getSelectionText(original) == originalValue) {
            $(original).val('');
        }
        return true;
    }
    else {
        if (document.all) {
            event.keyCode = 0;
        }
        else {
            if (key != 8 && key != 37 && key != 39) {
                evt.preventDefault();
            }
        }
        $(original).val(originalValue);
        return false;
    }
}

function valClip() {
    var content = window.clipboardData.getData("Text");
    if (content != null) {
        try {
            var test = parseInt(content);
            var str = "" + test;
            if (isNaN(test) == true) {
                return false;
            }
            else {
                if (str != content)
                    return false;
            }
            return true;
        }
        catch (e) {
            alert("Paste exception!");
        }
    }
    return false;
}

function autotab(original, destination, evt) {
    if (updateMobile(original, evt)) {
        if ($(original).val().length == $(original).attr("maxlength")) {
            $("#" + destination).focus().val('');
        }
    }
}

function updateMobile(original, evt) {
    if (!valNum(original, evt)) {
        return false;
    }
    else {
        $("#Mobile").val($("#cell1").val() + $("#cell2").val() + $("#cell3").val());
        return true;
    }
}
function KeyDown() {
    $(".field-validation-error").remove();
    $("#Mobile").val($("#cell1").val() + $("#cell2").val() + $("#cell3").val());
}

$().ready(function() {
    $("#Birthday").bindBirthday();

    $("#infoimg").hover(function(e) {
        $("#tooltip").css({ "left": e.pageX, "top": e.pageY + 18, "display": "block" });
    }, function() {
        $("#tooltip").css("display", "none");
    });

    $("#TimeZone").val(5);

    document.onkeydown = KeyDown;
    $("#submitBtn").click(KeyDown);

    $('#registrationform').validate({
        errorPlacement: function(error, element) {
            error.appendTo(element.parent().next());
        }
    });

    $("#cell3").val('');
})
