﻿function validate() {
    var summeryHtml = '';

    summeryHtml += valTestRequiredField('txtCompany');
    summeryHtml += valTestRequiredField('txtContact');
    summeryHtml += valTestRequiredField('txtAddress');
    summeryHtml += valTestRequiredField('txtZip');
    summeryHtml += valTestRequiredField('txtCity');
    summeryHtml += valTestRequiredField('txtCountry');
    summeryHtml += valTestRequiredField('txtPhone');
    summeryHtml += valTestRegexField('txtEmail', /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/);
    summeryHtml += valTestChkboxChecked('chkAcceptRepairFee');

    var divProducts = $('#divProducts').children();
    var index = 0;
    $.each(divProducts, function () {
        summeryHtml += valTestRequiredField('txtProduct', index);
        summeryHtml += valTestRegexField('txtProductSerial', /^[0-9]{6,6}$/, index);
        summeryHtml += valTestRequiredField('txtProductDescription', index);
        index += 1;
    }
            );


    var divValidationSummary = $('#divValidationSummary');
    if (summeryHtml.length > 0) {
        divValidationSummary.html(summeryHtml);
        divValidationSummary.show()
        return false;
    }
    else {
        divValidationSummary.hide()
        return true;
    }
}

function valTestRequiredField(fieldName, index) {

    fieldNameIndexed = fieldName;
    if (!(index === undefined)) {
        fieldNameIndexed += index;
    }

    var field = $('[id$="' + fieldNameIndexed + '"]');
    if (field.val().length < 1) {
        field.parent().prev().find('.valStar').show();
        return $('[id$="lblVal' + fieldName + '"]').html() + '<br/>';
    }
    else {
        field.parent().prev().find('.valStar').hide();
    }

    return '';
}

function valTestRegexField(fieldName, regexString, index) {

    fieldNameIndexed = fieldName;
    if (!(index === undefined)) {
        fieldNameIndexed += index;
    }

    var field = $('[id$="' + fieldNameIndexed + '"]');
    var regex = regexString;
    if (field.val().length < 1 || !regex.test(field.val())) {
        field.parent().prev().find('.valStar').show();
        return $('[id$="lblVal' + fieldName + '"]').html() + '<br/>';
    }
    else {
        field.parent().prev().find('.valStar').hide();
    }

    return '';
}

function valTestChkboxChecked(fieldName) {
    var field = $('[id$="' + fieldName + '"]');
    if (!field.is(':checked')) {
        field.parent().prev().find('.valStar').show();
        return $('[id$="lblVal' + fieldName + '"]').html() + '<br/>';
    }
    else {
        field.parent().prev().find('.valStar').hide();
    }

    return '';
}



function addProduct() {
    var divProducts = $('#divProducts');
    var productsCount = divProducts.children('#divProduct').length;

    var divProductTemplateHtml = $('#divProductTemplate').html();
    divProductTemplateHtml = divProductTemplateHtml.replace(/ProductIndex/g, productsCount);
    divProducts.append(divProductTemplateHtml);

}
