﻿//The following functions are to get States for US
function GetStatesForUS(countryId, controlId) {
    Accordent.Amms.Web.Service.PortalService.GetStates(countryId, onSuccessGetStates, onFailedGetStates, controlId);
}

function onSuccessGetStates(result, userContext) {
    var dropDownId = userContext;
    $('#' + dropDownId).html(result.Results);
    $('#' + dropDownId).removeAttr("disabled");
}

function onFailedGetStates(error) {
    $find('<%=uxDivError.ClientID%>').html(error.get_message());
    return false; //TODO to be verified
}


function SetFocus() {
    //$('input:visible', this).get(0).focus();
    $("input[type='text']:first").focus();
}
function SubmitFormOnEnter(submitSelector) {
    $(document).bind("keypress", function(event) {
        if (event.keyCode == 13) {
            $(submitSelector).click();
        }
    });
}




//Common Validation functions

function ValidateValueWithRegEx(controlId, filter) {
    var isValid = true;
    var strValue = $('#' + controlId).val();

    //do not validate empty string.
    if (strValue.length == 0)
        return isValid;

    isValid = filter.test(strValue);
    return isValid;
}

function ValidateEmail(controlId) {
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    return ValidateValueWithRegEx(controlId, filter)
}

function ValidateEmptyField(controlId) {
    var isValid = !($.trim($('#' + controlId).val()).length == 0);
    return isValid;
}


function ValidateFieldForSpaces(controlId) {
    var strValue = $('#' + controlId).val();

    var regEx = new RegExp(/\s{1,}/);
    var result = regEx.test(strValue);

    //we do not want to check for empty string.
    var isValid = (strValue.length == 0) || (result == false);
    return isValid;
}


function ValidateWholeNumber(controlId) {
    var filter = /^[+]?\d*$/;
    return ValidateValueWithRegEx(controlId, filter);
}

function ValidateDecimalNumber(controlId) {
    var filter = /^[-+]?\d*\.?\d*$/;
    return ValidateValueWithRegEx(controlId, filter);
}

function ValidateTime(controlId) {
    var isValid = true;
    var sourceTime = $('#' + controlId).val();
    var formattedTime = "";

    //do not validate empty string. 
    if (sourceTime.length == 0)
        return isValid;

    var timeRegExp = /^\d{1,2}:{0,1}\d{2}\s*((AM|PM)|(A|P)){0,1}$/i;
    var hours;
    var minutes;
    var amPm;

    if (timeRegExp.test(sourceTime)) {
        try {
            // If a ":" isn't in the time string, insert one at the appropriate spot
            if (sourceTime.indexOf(":") < 0) {
                var ampmIndex;
                var time = sourceTime;
                if (sourceTime.indexOf("a") > -1)
                    ampmIndex = sourceTime.indexOf("a");
                if (sourceTime.indexOf("p") > -1)
                    ampmIndex = sourceTime.indexOf("p");
                if (ampmIndex > 0) {
                    time = $.trim(sourceTime.substr(0, ampmIndex));
                }
                var colonIndex = time.length - 2;
                sourceTime = sourceTime.substr(0, colonIndex) + ":" + sourceTime.substr(colonIndex);
            }
            var index = sourceTime.indexOf(":");
            if (index > 0) {
                hours = sourceTime.substr(0, index);
                minutes = sourceTime.substr(index + 1, 2);
                amPm = $.trim(sourceTime.substr(index + 3));

                // Change a to am and p to pm
                if (amPm == "a")
                    amPm = "AM";

                if (amPm == "p")
                    amPm = "PM";

                // Make sure the hours and minutes are within a valid range
                if (hours > 23 || minutes > 59)
                    return false;

                if (hours > 12) {
                    // If it's a 24 hour time, reduce it to a 12 hour format
                    hours = hours - 12;
                    amPm = "PM";
                }
                else if (hours == 12) {
                    // If the hour is 12 and neither am or pm is specified, assume it's pm
                    if (amPm == "")
                        amPm = "PM";
                }
                else {
                    // If the hour is 11 or less and neither am or pm is specified, assume it's am
                    if (amPm == "")
                        amPm = "AM";
                }

                // If hours happens to wind up being 0, then change it to 12
                if (hours == 0)
                    hours = 12;
            }
            else {
                isValid = false;
            }
        }
        catch (err) {
            return false;
        }
    }
    else
        isValid = false;

    if (isValid == true) {
        // Everything checks out so format the time, remove any leading zeros and return it
        var result = hours + ":" + minutes + " " + amPm.toUpperCase();
        formattedTime = result.replace(/^0/, "");
        $('#' + controlId).attr("value", formattedTime);
    }

    return isValid;
}


//Note: jquery show/hide not working for divPasswordMatchFailure and 
//divPasswordMatchSuccess elements so using plain javascript to change the visibility.
function ValidateMatch(controlToValidate, controlToComapre, divSuccess, divFailure) {
    //do not validate when controlToValidate is empty
    if ($('#' + controlToValidate).val().length == 0 && $('#' + controlToComapre).val().length == 0) {

        $('#' + divFailure).attr("style", "display:none");
        $('#' + divSuccess).attr("style", "display:none");
        return true;
    }
    var isValid = $('#' + controlToValidate).val() == $('#' + controlToComapre).val();
    if (isValid == false) {
        $('#' + divFailure).attr("style", "display:block");
        $('#' + divSuccess).attr("style", "display:none");
    }
    else {
        $('#' + divFailure).attr("style", "display:none");
        $('#' + divSuccess).attr("style", "display:block");
    }
    return isValid;
}  


