﻿var emailPattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

function validateEmail(source, arguments) {
    $("#" + source.getAttribute('Host') + " > ." + source.getAttribute('EmptyFieldToolTip')).addClass("invisible");
    $("#" + source.getAttribute('Host') + " > ." + source.getAttribute('InvalidFieldToolTip')).addClass("invisible");
    $("#" + source.getAttribute('Host') + " > ." + source.getAttribute('ValidFieldToolTip')).addClass("invisible");

    if (arguments.Value != '') {
        if (!emailPattern.test(arguments.Value)) {
            arguments.IsValid = false;
            $("#" + source.getAttribute('Host') + " > ." + source.getAttribute('InvalidFieldToolTip')).removeClass("invisible").text(source.getAttribute('InvalidFieldMessage'));
        }
        else {
            arguments.IsValid = true;
            $("#" + source.getAttribute('Host') + " > ." + source.getAttribute('ValidFieldToolTip')).removeClass("invisible");
        }
    } else {
        arguments.IsValid = false;
        $("#" + source.getAttribute('Host') + " > ." + source.getAttribute('InvalidFieldToolTip')).removeClass("invisible").text(source.getAttribute('EmptyFieldMessage'));
    }
}

function validateRequiredField(source, arguments) {
    $("#" + source.getAttribute('Host') + " > ." + source.getAttribute('EmptyFieldToolTip')).addClass("invisible");
    $("#" + source.getAttribute('Host') + " > ." + source.getAttribute('InvalidFieldToolTip')).addClass("invisible");
    $("#" + source.getAttribute('Host') + " > ." + source.getAttribute('ValidFieldToolTip')).addClass("invisible");

    if (arguments.Value != '') {
        arguments.IsValid = true;
        $("#" + source.getAttribute('Host') + " > ." + source.getAttribute('ValidFieldToolTip')).removeClass("invisible");
    } else {
        arguments.IsValid = false;
        $("#" + source.getAttribute('Host') + " > ." + source.getAttribute('InvalidFieldToolTip')).removeClass("invisible").text(source.getAttribute('EmptyFieldMessage'));
    }
}


function initInputField(id, inactive, invalid, valid) {
    $("#" + id + " input").focus(function() {
        if ($("#" + id + " ." + invalid).hasClass("invisible") && $("#" + id + " ." + valid).hasClass("invisible"))
            $("#" + id + " ." + inactive).removeClass("invisible");
    });

   $("#" + id + " input").blur(function() {
        $("#" + id + " ." + inactive).addClass("invisible");
    });
}

function initTextAreaField(id, inactive, invalid, valid) {
    $("#" + id + " textarea").focus(function() {
        if ($("#" + id + " ." + invalid).hasClass("invisible") && $("#" + id + " ." + valid).hasClass("invisible"))
            $("#" + id + " ." + inactive).removeClass("invisible");
    });

    $("#" + id + " textarea").blur(function() {
        $("#" + id + " ." + inactive).addClass("invisible");
    });
}

function calcPrice(objLength, valCnt, objResult, unitPrice, tmpl) {
    
    var length = 3; //default
    var element = document.getElementById(objLength);
    length = Number(element[element.selectedIndex].value);
    var sum = Math.round(length * unitPrice * 1000)/1000;
    $get(objResult).innerHTML = sum.toFixed(2) + tmpl;

}

