﻿function hasPageValidators()
{
    var hasValidators = false;
  
    try
    {
        if (Page_Validators.length > 0)
        {
            hasValidators = true;
        }
    }
    catch (error)
    {
    }
  
    return hasValidators;
}

function validationGroupEnable(validationGroupName, isEnable)
{
    if (hasPageValidators())
    {
        for(var i = 0; i < Page_Validators.length; i++)
        {
            if (Page_Validators[i].validationGroup == validationGroupName)
            {
                ValidatorEnable(Page_Validators[i], isEnable);
            }
        }
    }
}

function validateCheckBox(source, args) {
    args.IsValid = $get(source.controltovalidate).checked;
}

function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    }
    else if (elm.attachEvent) {
        var r = elm.attachEvent('on' + evType, fn);
        return r;
    }
    else {
        elm['on' + evType] = fn;
    }
}

function getWindowWidth() {
    var windowWidth;
    if (window.innerWidth) {
        windowWidth = window.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth) {
        windowWidth = document.documentElement.clientWidth;
    }
    else if (document.body) {
        windowWidth = document.body.clientWidth;
    }
    
    return windowWidth;
}

function getWindowHeight() {
    var windowHeight;
    if (window.innerHeight) {
        windowHeight = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body) {
        windowHeight = document.body.clientHeight;
    }

    return windowHeight;
}

function NavigatorToggle(header, content, expandedByDefault, getNavValue) {
    this.header = header;
    this.content = content;
    this.originalHeaderClassName = header.className;
    this.originalContentClassName = content.className;
    this.getValue = getNavValue;

    this.collapse = function() {
        this.header.className = this.originalHeaderClassName + ' Collapsed';
        this.content.className = this.originalContentClassName + ' Collapsed';

        var descriptionDiv = document.createElement('div');
        descriptionDiv.innerHTML = this.getValue(this.content);
        descriptionDiv.className = 'NavDescription';
        this.header.appendChild(descriptionDiv);
    }

    this.expand = function() {
        this.header.className = this.originalHeaderClassName;
        this.content.className = this.originalContentClassName;

        var descriptionNavFind = $('.NavDescription', this.header);
        if (descriptionNavFind.length > 0) {
            this.header.removeChild(descriptionNavFind[0]);
        }
    }

    if (!expandedByDefault) {
        this.collapse();
    }

    this.onClickWrapper = function() { //Maybe we pass GetValueFunction
        if (this.content.className == this.originalContentClassName) {
            this.collapse();
        }
        else {
            this.expand();
        }
    }
}

nav_getCheckBoxListValue = function(content) {
    var inputs = $('input', content);
    var labels = $('label', content);
    var result = '';

    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].checked == true) {
            if (result != '') {
                result += ', ';
            }
            result += labels[i].innerHTML;
        }
    }
    
    return result;
}
nav_getAutoCompleteValue = function(content) {
    var inputs = $('input', content);
    var result = '';

    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == 'hidden') {
            //Trim '|' chars
            var cleanHiddenValue = inputs[i].value.replace(/^\|*/, "").replace(/\|*$/, "");
            //Replace remaining '|' with ', '
            cleanHiddenValue = cleanHiddenValue.replace(/\|/g, ", ");

            if (cleanHiddenValue != '') {
                if (result != '') {
                    result = result + ', ';
                }
                result = result + cleanHiddenValue;
            }
        }
        else if (inputs[i].type == 'text') {
            if (inputs[i].value != '') {
                if (result != '') {
                    result = result + ', ';
                }
                result = result + inputs[i].value;
            }
        }
    }

    return result;
}
nav_getTextBoxValue = function(content) {
    var textBoxes = $('input', content);
    var result = '';
    for (var i = 0; i < textBoxes.length; i++) {
        if (textBoxes[i].value != '') {
            if (result != '') {
                result = result + ', ';
            }
            result = result + textBoxes[i].value;
        }
    }
    return result;
}
nav_getCheckBoxFieldValue = function(content) {
    var checkBoxes = $('.CheckBoxField', content);
    var result = '';
    for (var i = 0; i < checkBoxes.length; i++) {
        var findCheckbox = $('input', checkBoxes[i]);
        var findLabel = $('label', checkBoxes[i]);
        if (findCheckbox.length > 0 && findLabel.length > 0) {
            if (findCheckbox[0].checked) {
                if (result != '') {
                    result = result + ', ';
                }
                result = result + findLabel[0].innerHTML;
            }
        }
    }

    return result;
}
nav_getRangeBoxValue = function(content) {
    var textBoxes = $('input', content);
    var labels = $('.PostFieldText', content);
    var result = '';
    var delimiter = ' to ';
    var suffix = '';

    if (labels.length > 0) {
        delimiter = labels[0].innerHTML;
    }

    if (labels.length > 1) {
        suffix = labels[1].innerHTML;
    }

    if (textBoxes.length == 2) {
        if (textBoxes[0].value != '' && textBoxes[1].value != '') {
            result = textBoxes[0].value + ' ' + delimiter + ' ' + textBoxes[1].value + ' ' + suffix;
        }
        else if (textBoxes[0].value != '') {
            result = textBoxes[0].value + ' ' + suffix + ' and above';
        }
        else if (textBoxes[1].value != '') {
            result = textBoxes[1].value + ' ' + suffix + ' and under';
        }
    }
    return result;
}
nav_getDropDownValue = function(content) {
    var dropDowns = $('select', content);
    var result = '';

    for (var i = 0; i < dropDowns.length; i++) {
        if (result != '') {
            result += ', ';
        }
        result = result + dropDowns[i].options[dropDowns[i].selectedIndex].text.replace(/&nbsp;/g, ' ').replace(/\s+/g, ' ');
    }

    return result;
}
nav_getMusLookingForValue = function(content) {
    var dropDowns = $('select', content);
    var result = '';
    var checkBoxResult = nav_getCheckBoxFieldValue(content);

    if (dropDowns.length > 1) {
        var genderOption = dropDowns[1].options[dropDowns[1].selectedIndex];
        if (genderOption.value != '') {
            result = genderOption.text + ' ';
        }
    }

    if (dropDowns.length > 0) {
        result = result + dropDowns[0].options[dropDowns[0].selectedIndex].text.replace(/&nbsp;/g, ' ').replace(/\s+/g, ' ');
    }

    result = result + ' ' + checkBoxResult;

    return result;
}

function showElement(element) {
    if (element.className.indexOf('Hidden') > -1) {
        element.className = element.className.replace(/\s*Hidden/, '');
    }
}

function hideElement(element) {
    
    if (element.className.indexOf('Hidden') == -1) {
        element.className = element.className + ' Hidden';
    }
}
