﻿var loadSubcategories_sel;
function loadSubcategories(action, sel, targetId) {
    var value = sel.value;

    var form = sel.form;

    if (form) {
        var body = Sys.Mvc.MvcHelpers._serializeForm(form);

        var ajaxOptions = Sys.Mvc.$create_AjaxOptions();

        ajaxOptions.url = action;
        ajaxOptions.updateTargetId = targetId;
        ajaxOptions.onSuccess = loadSubcategories_ajaxsuccess;
        ajaxOptions.onFailure = loadSubcategories_ajaxerror;
        ajaxOptions.insertionMode = Sys.Mvc.InsertionMode.replace;

        sel.disabled = true;
        loadSubcategories_sel = sel;

        Sys.Mvc.MvcHelpers._asyncRequest(form.action, form.method || 'post', body, form, ajaxOptions);
    }
}
function loadSubcategories_ajaxsuccess(ajaxContext) {
    if (loadSubcategories_sel)
        loadSubcategories_sel.disabled = false;
    Sys.Mvc.MvcHelpers.updateDomElement(ajaxContext.get_updateTarget(), ajaxContext.get_insertionMode(), ajaxContext.get_data());
}
function loadSubcategories_ajaxerror(ajaxContext) {
    if (loadSubcategories_sel)
        loadSubcategories_sel.disabled = false;
    Sys.Mvc.MvcHelpers.updateDomElement(ajaxContext.get_updateTarget(), ajaxContext.get_insertionMode(), ajaxContext.get_data());
}

function zipcodeValid(a) {
    return (a.length == 5 && parseInt(a) >= 0);
}

function validateForm() {

    var cat = validateInput('searchCategory');

    var subcat = validateInput('searchSubcategory');

    var zipcode = validateInput('searchZipcode', zipcodeValid);

    var keywords = validateInput('searchKeywords');

    if (!zipcode && !cat && !keywords) {
        setValidationError(document.getElementById('searchCategory'));
        setValidationError(document.getElementById('searchZipcode'), zipcodeValid);
        setValidationError(document.getElementById('searchKeywords'));

        alert("One or more of keywords, category or zipcode are required.");
        
        return null;
    }

    return { cat: cat, subcat: subcat, zipcode: zipcode };
}

function doSearch(s) {
    var data = validateForm();
    if (!data)
        return;

    if (s.charAt(s.length - 1) == '/')
        s = s.substring(0, s.length - 2);

    var type = document.getElementById('searchType').value;
    if (type == '0')
        window.location.href = s + '/GigSearch/' + data.zipcode + '/' + data.cat + '/' + data.subcat;
    else
        window.location.href = s + '/GigsterSearch/' + data.zipcode + '/' + data.cat + '/' + data.subcat;
}

function validateInput(name, func) {
    var elm = document.getElementById(name);
    if (!elm)
        return null;
    return validateInputElm(elm, func);
}

function validateInputElm(elm, func) {
    var value = elm.value;
    if (value && func && !func(value)) {
        setValidationError(elm, func);
        return null;
    }
    else
        elm.className = null;
    return value;
}

function setValidationError(elm, func) {
    elm.className = 'input-validation-error';
    var old = elm.onchange;
    elm.onchange = function(e) { elm.onchange = old; validateForm(); if (old) old(e); };
}

function addCategory(catSelID, subcatSelID, listBoxID, categoryCount) {
    var catSel = document.getElementById(catSelID);
    if (catSel) {
        if (catSel.selectedIndex == 0 || catSel.selectedIndex == -1) {
            alert('Please select a category.');
            return;
        }

        var catOpt = catSel.options[catSel.selectedIndex];
        var sel = document.getElementById(subcatSelID);
        if (sel) {
            if (sel.selectedIndex == 0 || sel.selectedIndex == -1) {
                alert('Please select a subcategory.');
                return;
            }

            var opt = sel.options[sel.selectedIndex];

            var tbl = document.getElementById(listBoxID);
            if (tbl.rows.length >= categoryCount) {
                alert("Only " + categoryCount + " categories are permitted.");
                return;
            }
            
            var els = $("input", tbl);

            // Scan through the table to be sure one hasn't been added yet
            //var els = document.getElementsByName('mySubcategories');
            for (var i = 0; i < els.length; i++) {
                if (els[i].name != 'subcategoryIds')
                    continue;
                if (els[i].value == opt.value)
                    return;
            }

            // Add the new element

            var tr = tbl.insertRow(tbl.rows.length);
            var td = tr.insertCell(0);
            td.className = "category";
            td.appendChild(document.createTextNode(opt.text));
            td = tr.insertCell(1);
            td.setAttribute("align", "right");
            var el = document.createElement("input");
            el.setAttribute("type", "hidden");
            el.setAttribute("name", 'subcategoryIds');
            el.setAttribute("value", opt.value);
            td.appendChild(el);
            var el = document.createElement("a");

            el.appendChild(document.createTextNode("remove"));
            el.href = "#";
            el.onclick = function(e) { removeCategory(e, el); return false; }
            el.className = "text-action";
            td.appendChild(el);

            recomputeHeights();
        }
    }
}

function removeCategory(e) {
    if (!e) e = event;
    var el = (e.target) ? e.target : e.srcElement;
    if (el.nodeName.toLowerCase() == "img")
        el = el.parentNode;
    if (el) {
        var tr = el.parentNode.parentNode;
        var tbl = tr.parentNode;
        tbl.removeChild(tr);
    }

    recomputeHeights();
}

(function($) {
    $.fn.equalHeights = function(minheight) {
        var maxheight = minheight || 0;
        this.each(function() {
            var thisHeight = $(this).height();
            if (thisHeight > maxheight)
                maxheight = thisHeight;
        });
        this.height(maxheight);
        return this;
    }
})(jQuery);

function recomputeHeights() {
}

function waterMarkFocus(text) {
    text.style.backgroundImage = '';
}
function waterMarkBlur(text, bkg) {
    if (text.value == "")
        text.style.backgroundImage = bkg; 
}

