﻿function addPrompt(textbox, prompt) {
    
    if (textbox.value.length == 0) {
        $(textbox).addClass("txtPrompt");
        textbox.value = prompt;
        textbox["hasPrompt"] = true;
    }
}

function removePrompt(textbox) {
    if (textbox["hasPrompt"] == true) {
        $(textbox).removeClass("txtPrompt");
        textbox.value = '';
        textbox["hasPrompt"] = false;
    }
}

function SetSelectedIndexByValue(dropdown, value) {
    var i = 0;
    for (i = 0; i < dropdown.options.length; i++) {
        if (dropdown.options[i].value == value) {
            //alert(i + ' - ' + value + ' - ' + dropdown.options[i].value);
            dropdown.selectedIndex = i;
        }
    }
}

function validateRequired() {
    //alert('validate required enter');
    var valid = true;
    for (var i = 0; i < arguments.length; i++) {

        if ($(arguments[i]).val() == null || $(arguments[i]).val().length == 0 || $(arguments[i]).hasClass('txtPrompt')) {
            valid = false;
            if ($(arguments[i]).hasClass('valError') == false) {
                $(arguments[i]).addClass('valError');


                var pos = $(arguments[i]).offset()

                $("#search_wrapper").append('<div id="' + arguments[i].id + '_errorDiv' + '">?</div>');
                var errdiv = document.getElementById(arguments[i].id + '_errorDiv');

                $(errdiv).css('top', (pos.top + 1) + 'px');
                $(errdiv).css('left', (pos.left + $(arguments[i]).width() - 16) + 'px');

                $(errdiv).addClass('errorDiv');

                $("#search_wrapper").append('<div id="' + arguments[i].id + '_errorMessageDiv' + '">Please fill in this field before searching</div>');
                var errmsgdiv = document.getElementById(arguments[i].id + '_errorMessageDiv');
                $(errmsgdiv).hide(0);

                $(errmsgdiv).css('top', (pos.top + 21) + 'px');
                $(errmsgdiv).css('left', pos.left + 'px');
                $(errmsgdiv).css('width', ($(arguments[i]).width() - 12) + 'px');

                $(errmsgdiv).addClass('errorMessageDiv');

                $(errdiv).mouseover(function() {
                    $(errmsgdiv).show(0);
                });

                $(errdiv).mouseout(function() {
                    $(errmsgdiv).hide(0);
                });
            }
        }
    }

    //alert('validate required exit');
    return valid;
}

function validateClearIfHasValue() {
    for (var i = 0; i < arguments.length; i++) {

        if ($(arguments[i]).val() != null && $(arguments[i]).val().length != 0 && $(arguments[i]).hasClass('txtPrompt') == false) {
            if ($(arguments[i]).hasClass('valError') == true) {
                $(arguments[i]).removeClass('valError');
                var errdiv = document.getElementById(arguments[i].id + '_errorDiv');
                $(errdiv).remove();
                var errmsgdiv = document.getElementById(arguments[i].id + '_errorMessageDiv');
                $(errmsgdiv).remove();
            }
        }
    }
}


function selectedMinMaxChanged(ddlMin, ddlMax, arrayOfValues, arrayOfValueText) {
    var minSelectedIndex = ddlMin.selectedIndex;
    var maxSelectedIndex = ddlMax.selectedIndex;

    var minSelectedValue = 0;
    if (minSelectedIndex != 0 && minSelectedIndex != -1 && ddlMin.options[minSelectedIndex].value != null) {
        minSelectedValue = ddlMin.options[minSelectedIndex].value;
    }

    var maxSelectedValue = 100000000;
    if (maxSelectedIndex != -1 && (maxSelectedIndex != (ddlMax.options.length - 1)) && ddlMax.options[maxSelectedIndex].value != null) {
        maxSelectedValue = ddlMax.options[maxSelectedIndex].value;
    }

    // UPDATE MIN MAX VALUES

    //alert('updating s');
    $(ddlMax).empty();
    $(ddlMin).empty();

    var oNull = null;

    oNull = document.createElement('option');
    oNull.text = 'Any';
    oNull.value = '';

    try {
        ddlMin.add(oNull, null); // standards compliant; doesn't work in IE
    }
    catch (ex) {
        ddlMin.add(oNull); // IE only
    }

    var i = 0;
    for (i = 0; i < arrayOfValues.length; i++) {
        var value = arrayOfValues[i];

        if (maxSelectedValue == null || maxSelectedValue == '' || maxSelectedValue == 'null' || value < maxSelectedValue) {
            var o = document.createElement('option');
            o.text = arrayOfValueText[i];
            o.value = arrayOfValues[i];

            try {
                ddlMin.add(o, null); // standards compliant; doesn't work in IE
            }
            catch (ex) {
                ddlMin.add(o); // IE only
            }
        }

        if (minSelectedValue == null || minSelectedValue == '' || minSelectedValue == 'null' || value > minSelectedValue) {

            var omax = document.createElement('option');
            omax.text = arrayOfValueText[i];
            omax.value = arrayOfValues[i];

            try {
                ddlMax.add(omax, null); // standards compliant; doesn't work in IE
            }
            catch (ex) {
                ddlMax.add(omax); // IE only
            }
        }
    }

    oNull = document.createElement('option');
    oNull.text = 'No Limit'; //Array[Array.length - 1] + ' +';
    oNull.value = '';
    try {
        ddlMax.add(oNull, null); // standards compliant; doesn't work in IE
    }
    catch (ex) {
        ddlMax.add(oNull); // IE only
    }

    if (maxSelectedValue == null || maxSelectedValue == '' || maxSelectedValue == 'null' || maxSelectedValue == NaN) {
        // if nothing was selected, select the highest number, i.e. no limit
        ddlMax.selectedIndex = ddlMax.options.length - 1;
    }
    
    
    // DONE UPDATING MIN MAX VALUES

    var i = 0;
    if (minSelectedIndex != 0/* && minSelectedIndex != ddlMin.options.length -1*/) {
        for (i = 0; i < ddlMin.options.length; i++) {
            if (minSelectedValue == ddlMin.options[i].value) {
                ddlMin.selectedIndex = i;
                break;
            }
        }
    }

    if (maxSelectedValue != 100000000) {
        for (i = 0; i < ddlMax.options.length; i++) {
            if (maxSelectedValue == ddlMax.options[i].value) {
                ddlMax.selectedIndex = i;
                break;
            }
        }
    }
    else {
        ddlMax.selectedIndex = ddlMax.options.length - 1;
    }
}
