﻿// var nameRegEx = /^[A-Za-z]+[A-Za-z]+[a-zA-Z]*$/;
var nameRegEx = /.*/;
var emailRegEx = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
var phoneRegEx = /^(\([2-9]|[2-9])(\d{2}|\d{2}\))(-|.|\s)?\d{3}(-|.|\s)?\d{4}$/;
// var zipCodeRegEx = /(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)/;
var zipCodeRegEx = /^\d{5}$/;

String.prototype.Trim = function ()
{
    return this.replace(/^\s+|\s+$/g, "");
}

function IsDropDownItemSelected(ddlId)
{
    return ($("#" + ddlId).attr("selectedIndex") != 0);
}

function IsValidZipCode(elementId)
{
    return $("#" + elementId).val().Trim().match(zipCodeRegEx);
}

function ShowErrorMsg(msg, marginTop, marginLeft, labelId, controlId)
{
    //Change the background of the control to light red

    if ((controlId != null) && (document.getElementById(controlId) != null))
    {
        // The timeout is a workaround for IE only
        setTimeout(function () { $("#" + controlId).focus(); }, 100);

        if (document.getElementById(controlId).type == "text")
        {
            document.getElementById(controlId).style.backgroundColor = "#FFB6C1";
        }
    }

    if ((labelId != null) && (document.getElementById(labelId) != null))
    {
        document.getElementById(labelId).style.color = "#f7341a";
    }
    $("#error_moving").css("display", "block");
    $("#error_moving").css("marginTop", marginTop + "px");
    if (marginLeft != null)
    {
        $("#error_moving").css("marginLeft", marginLeft + "px");
    }
    $("#error_moving").css("visibility", "visible");
    SetErrorMsg(msg);
}

function SetErrorMsg(msg)
{
    $("#errMsg").html(msg);
}

function GetRadioListSelectedValue(radioListName)
{
    var radioListItems = document.getElementsByName(radioListName);

    for (var j = 0; j < radioListItems.length; j++)
    {
        if (radioListItems[j].checked)
            return radioListItems[j].value;
    }

    return null;
}

function IsValidPhone(phoneNumber)
{
    phoneNumber = phoneNumber.replace(/[^\d]/g, ""); //Clear all non digit characters
    if (phoneNumber.toString().substr(0, 1) == "1") //The is a leading 1 - remove it
    {
        phoneNumber = phoneNumber.substr(1);
    }

    return ((phoneNumber.match(phoneRegEx)) && (phoneNumber.length == 10));
}

function IsRadioItemSelected(radioListName)
{
    var radioListItems = document.getElementsByName(radioListName);

    for (var j = 0; j < radioListItems.length; j++)
    {
        if (radioListItems[j].checked)
            return true;
    }

    return false;
}

function CapitalizeFirstChar(e, id)
{
    var keyID = (window.event) ? event.keyCode : e.keyCode;
    if (keyID >= 65 && keyID <= 90)
    { //Lower case char
        var firstChar = $("#" + id).val().substr(0, 1);
        if (firstChar >= 'a' && firstChar <= 'z')
        {
            $("#" + id).val(firstChar.toUpperCase() + $("#" + id).val().substr(1))
        }
    }
}

function IsNumeric(sText)
{
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++)
    {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1)
        {
            IsNumber = false;
        }
    }

    return IsNumber;
}

function CancelEvent(evt)
{
    /// <summary>
    /// Cancels the event thrown (also it's drilldown and bubble).
    /// </summary>

    e = (window.event) ? window.event : evt;

    if (!e)
        return false;

    if (BrowserDetectName == FIREROX_BROWSER || BrowserDetectName == CHROME_BROWSER)
    {
        e.stopPropagation();
        e.preventDefault();
    }
    else
    {
        e.cancelBubble = true;
        e.returnValue = false;
    }
    e.cancel = true;
}

var lastErrorLabelId = null;

function MarkInputError(errorMessage, labelId, inputId, ignoreMessageHeight, widthOffset)
{
    var label = $("#" + labelId);
    var input = $("#" + inputId);
    $("#errMsg").text(errorMessage);
    $("#new-error-box").show();
    messageHeight = $("#new-error-box").height();

    if (ignoreMessageHeight != undefined && ignoreMessageHeight == true)
    {
        $("#new-error-box").css("top", input.position().top - 10);
    }
    else
    {
        $("#new-error-box").css("top", input.position().top - messageHeight + 10);
    }

    if (widthOffset != undefined)
    {
        $("#new-error-box").css("left", input.position().left + input.width() + widthOffset);
    }
    else
    {
        $("#new-error-box").css("left", input.position().left + input.width() + 20);
    }

    if (input.is("input")) // Only for html input we display red background color
    {
        input.addClass("input-error");
    }
    input.focus();

    input.one("blur", RemoveErrorFromField);
    input.one("click", RemoveErrorFromField);
    input.one("keydown", RemoveErrorFromField);

    if (lastErrorLabelId != null)
    {
        $("#" + lastErrorLabelId).removeClass("label-error");
    }

    lastErrorLabelId = labelId;
    label.addClass("label-error");
}

function RemoveErrorFromField()
{
    $(this).removeClass("input-error");
    $("#" + lastErrorLabelId).removeClass("label-error");
    $("#new-error-box").hide();
}

function SetDefaultValueIfUndefined(fieldValue, defaultValue)
{
    if (fieldValue == undefined)
    {
        if (defaultValue == undefined)
        {
            return "";
        }
        else
        {
            return defaultValue;
        }
    }
    else
    {
        return fieldValue;
    }
}
