﻿
/*helper method*/
function confirmAndGo(msg, to) {
    jConfirm(msg, 'Attention!', function(result) { if (result) window.location = to; });
}


/*
Methods supporting roundInput control
*/
function showInput(input) {
    $(input).prev().css('display', 'none');
}
function inputLoseFocus(input) {
    if (!input.value) {
        $(input).prev().css('display', '');
    }
    else {
        $(input).prev().css('display', 'none');
    }
}
function placeHolderClick(span) {
    $(span).css('display', 'none');
    $(span).next().focus();
}

/*
Methods supporting validation
*/
function validate(group) {
    var result = true;
    //required field validation - req
    $("." + group + " .req").each(function() {
        if (!this.value) {
            if (!$(this).hasClass('invalid')) {
                $(this).addClass('invalid').parent().parent().after('<span style="font-size:10px;color:#D50A37;float:left;clear:left;" class="valMsg">Required!</span>');
            }
        }
        else if ($(this).hasClass('invalid')) {
            $(this).removeClass('invalid');
            $(this).parent().parent().next().remove();
        }
    });
    $("." + group + " .reqsel").each(function() {
        if (this.options[0].selected) {
            if (!$(this).hasClass('invalid')) {
                $(this).addClass('invalid').parent().parent().after('<span style="font-size:10px;color:#D50A37;float:left;clear:left;" class="valMsg">Required!</span>');
            }
        }
        else if ($(this).hasClass('invalid')) {
            $(this).removeClass('invalid');
            $(this).parent().parent().next().remove();
        }
    });
    
    //mail validation - mail
    $("." + group + " .mail").each(function() {
        var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
        if (!this.value || !pattern.test(this.value)) {
            if (!$(this).hasClass('invalid')) {
                $(this).addClass('invalid').parent().parent().after('<span style="width:100px;font-size:10px;color:#D50A37;float:left;clear:left;" class="valMsg">Required Mail!</span>');
            }
        }
        else if ($(this).hasClass('invalid')) {
            $(this).removeClass('invalid');
            $(this).parent().parent().next().remove();
        }
    });
    //phone validation not required- phone (add req class to make it required)
    $("." + group + " .phone").each(function() {
        var pattern = /^([0-9]{7})/;
        if (this.value && (!pattern.test(this.value) || this.value.length != 7)) {
            if (!$(this).hasClass('invalid')) {
                $(this).addClass('invalid').parent().parent().after('<span style="width:120px;font-size:10px;color:#D50A37;float:left;clear:left;" class="valMsg">Number in wrong format!</span>');
            }
        }
        else if ($(this).hasClass('invalid') && pattern.test(this.value) || (!this.value && !$(this).hasClass("req"))) {
            $(this).removeClass('invalid');
            $(this).parent().parent().next().remove();
        }
    });
    
    //Card validation not required- phone (add req class to make it required)
    $("." + group + " .ccard").each(function() {
        var pattern = /^([0-9]{8,17})$/;
        this.value = this.value.trim();
        if (!this.value || !pattern.test(this.value) || this.value.length < 8 || this.value.length > 17) {
            if (!$(this).hasClass('invalid')) {
                $(this).addClass('invalid').parent().parent().after('<span style="width:120px;font-size:10px;color:#D50A37;float:left;clear:left;" class="valMsg">Card number not valid!</span>');
            }
        }
        else if ($(this).hasClass('invalid') && pattern.test(this.value) || (!this.value && !$(this).hasClass("req"))) {
            $(this).removeClass('invalid');
            $(this).parent().parent().next().remove();
        }
    });
    
    
    result = $("." + group + " .invalid").size() == 0;
    if (!result) {
        if ($("a." + group).hasClass('valid')) {
            $("a." + group).removeClass('valid');
        }
    }
    else {
        if (!$("a." + group).hasClass('valid')) {
            $("a." + group).addClass('valid');
        }
    }
    return result;
}
