var popUpWin = 0;

function popUpWindow(URLStr)
{
    if (popUpWin)
    {
        if (!popUpWin.closed) popUpWin.close();
    }
    popUpWin = open(URLStr, "newwin", 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=600');
}

function stripHtmlAndCountWordsLeft(htmlinput, maxwords) {
    var currentCount = stripHtmlAndCountWords(htmlinput);
    return maxwords - currentCount;
}

function stripHtmlAndCountWords(htmlinput) {
    var a = stripHtml(htmlinput);
    a = a.split(' ');
    var r = 0;
    for (z = 0; z < a.length; z++) {
        if (a[z].length > 0) r++;
    }
    return r;
}


function stripHtmlAndCountCharsLeft(htmlinput, maxchars) {
    var currentCount = stripHtmlAndCountChars(htmlinput);
    return maxchars - currentCount;
}

function stripHtmlAndCountChars(htmlinput) {
    return stripHtml(htmlinput).length;
}

function stripHtml(htmlinput) {
    var y = htmlinput.replace(/<\S[^><]*>/g, " ").replace(/\s+/g, " ");
    return y;
}


function validateWordLength(dijitField, wordCount) {
    var wordsLeft = stripHtmlAndCountWordsLeft($("#" + dijitField).val(), wordCount);
    var wordsLeftId = dijitField + "WordsLeftId";
    $("#" + wordsLeftId).text("Words Left: " + wordsLeft);
    //    if (wordsLeft < 0) {
    //        dojo.byId(wordsLeftId).className = "limitreached";
    //    } else {
    //        dojo.byId(wordsLeftId).className = "";
    //    }
}

function validateWordLengthYUI(yui, wordCount) {
    var wordsLeft = stripHtmlAndCountWordsLeft(yui.get('element').value, wordCount);
    var wordsLeftStr = "Words left: " + wordsLeft;

    if (wordsLeft < 0) {
        yui.dompath.innerHTML = "<span class='limitreached'>" + wordsLeftStr + "</span>";
    } else {
        yui.dompath.innerHTML = "<span class='wordcount'>" + wordsLeftStr + "</span>";
        ;
    }
}
function registerOnLoadWordLength(dijitField, wordCount) {
    validateWordLength(dijitField, wordCount);

    $("#" + dijitField).bind("keyup", function(e) {
        validateWordLength(dijitField, wordCount);
    });
}
function validateCharLength(dijitField, charCount) {
    var charsLeft = stripHtmlAndCountCharsLeft(dijit.byId(dijitField).getValue(), charCount);
    var charsLeftId = dijitField + "CharsLeftId";
    dojo.byId(charsLeftId).innerHTML = "Characters Left: " + charsLeft;
    if (charsLeft < 0) {
        dojo.byId(charsLeftId).className = "limitreached";
    } else {
        dojo.byId(charsLeftId).className = "";
    }
}
function registerOnLoadCharLength(dijitField, charCount) {
    dojo.addOnLoad(function() {
        validateCharLength(dijitField, charCount);
        dojo.connect(dijit.byId(dijitField), "onChange", function() {
            validateCharLength(dijitField, charCount);
        });
        dojo.connect(dijit.byId(dijitField), "onKeyUp", function() {
            validateCharLength(dijitField, charCount);
        });
    })
}
// ToolTip - call the below to activate it on any tooltip class
// <a href="#" class="tooltip" title="Hello">
//$(document).ready(function() {
//    tooltip();
//});
tooltip = function() {
    /* CONFIG */
    xOffset = 10;
    yOffset = 20;
    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result
    /* END CONFIG */
    $(".tooltip").hover(function(e) {
        this.t = this.title;
        this.title = "";
        $("body").append("<p id='tooltip'>" + this.t + "</p>");
        $("#tooltip")
                .css("top", (e.pageY - xOffset) + "px")
                .css("left", (e.pageX + yOffset) + "px")
                .fadeIn("fast");
    },
            function() {
                this.title = this.t;
                $("#tooltip").remove();
            });
    $(".tooltip").mousemove(function(e) {
        $("#tooltip")
                .css("top", (e.pageY - xOffset) + "px")
                .css("left", (e.pageX + yOffset) + "px");
    });
};

// From fatslice..For menus
function getIndex() {
    //var x = document.getElementById("divCatCmb").style.index;
    var x = document.getElementById("divList").style.display;
    alert(x);
}
function fakeCombo(x, e)
{
    var S = document.getElementById("selCombo");
    var L = S.options.length;
    var found = false;
    var myIndex = 0;

    var keycode;
    if (navigator.appName == "Microsoft Internet Explorer") {
        keycode = e.keyCode;
    }
    else {
        keycode = e.which;
    }

    if (keycode == 13) {
        for (var i = 0; i <= L - 1; i++) {
            if (x.value == S.options[i].value) {
                found = true;
                myIndex = i
            }
        }
        if (found) {
            S.options.selectedIndex = myIndex;
        } else {
            S.options[S.options.length] = new Option(x.value, x.value);
            S.options.selectedIndex = (S.options.length - 1);
        }
        return false;
    }
    return true;
}

function mySelect(x, mySelection) {

    if (mySelection == 1) {
        document.getElementById("txtCombo").value = x.options[x.selectedIndex].text;
        document.getElementById("hidTxtCategory").value = x.options[x.selectedIndex].value;
        document.getElementById("divList").style.display = "none";
    }
    if (mySelection == 2) {
        document.getElementById("txtLocCmb").value = x.options[x.selectedIndex].text;
        document.getElementById("hidTxtLocation").value = x.options[x.selectedIndex].value;
        document.getElementById("divLocList").style.display = "none";
    }
}

function showSelection(mySelection) {
    if (mySelection == 1) {
        if (document.getElementById("divList").style.display == "block") {
            document.getElementById("divList").style.display = "none";
        } else {
            document.getElementById("divList").style.display = "block";
        }
    }

    if (mySelection == 2) {
        if (document.getElementById("divLocList").style.display == "block") {
            document.getElementById("divLocList").style.display = "none";
        } else {
            document.getElementById("divLocList").style.display = "block";
        }
    }
}

function bookmarkThis(url) {
    jQuery.get(url, {},
            function(data) {
                alert("Saved to My [HQ]");
            });
}





