//
//for forum index page, hide and show category
//
var Browser = GetBrowser();
var catAllOn = true;

//******************************
//********** Tab Container change *************
//******************************
function ActiveTabChanged(sender, e) {}

//
//Get browser type
//
function GetBrowser()
{
    var b = { 
                'isIE6' : (navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0) && (navigator.userAgent.indexOf('6.0') >= 0), 
                'isIE7' : (navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0) && (navigator.userAgent.indexOf('7.0') >= 0),
                'isFirefox' : navigator.userAgent.indexOf('Firefox') >= 0,
                'isSafari' : navigator.userAgent.indexOf('Safari') >= 0
            };
    return b;
}
                    
function OpenCloseCat(oid)
{
    SetCat(oid,IsCatOff(oid));
    RememberCat("SSICat" + oid);
}

function SetCat(oid, open)
{
    var catImg = document.getElementById("imgCat_" + oid);
    if(catImg != null)
    {
        if(open != 0)  catImg.src='/forums/images/triangle-d.gif';
        else catImg.src='/forums/images/triangle-l.gif';
    }

    for(var i=0;i<100;i++)
    {
        var tr = document.getElementById("tr_" + oid + "_" + i);
        if(tr != null)
        {
            if(open != 0)
            {
                if(Browser.isIE6 == true || Browser.isIE7 == true) tr.style.display="block";
                else tr.style.display="table-row";
            }
            else tr.style.display="none";
        }
        else if(i != 0) break;
    }
}

function InitForumCatOnOff(oidList)
{
    var oList = oidList.split(";");
    for(var i=0;i<oList.length;i++)
    {
        SetCat(oList[i],!IsCatOffCookie("SSICat" + oList[i]));
    }
}


function OnOffAllForumCat()
{
    catAllOn = !catAllOn;

    if(catOidList != null)
    {
        var oList = catOidList.split(";");
        for(var i=0;i<oList.length;i++)
        {
            SetCat(oList[i],catAllOn);
            if(catAllOn && IsCatOffCookie("SSICat" + oList[i])) RememberCat("SSICat" + oList[i]);
            else if(!catAllOn && !IsCatOffCookie("SSICat" + oList[i])) RememberCat("SSICat" + oList[i]);
        }
    }
    
    if(catAllOn)
    {
        document.getElementById('imgOnOff').src='/forums/images/ElementMinus.jpg';
        document.getElementById('spanOnOffText').innerHTML = "Click To Collapse All Categories";
        document.getElementById('spanOnOffText').style.fontWeight = "bold";
    }
    else
    {
        document.getElementById('imgOnOff').src='/forums/images/ElementPlus.jpg';
        document.getElementById('spanOnOffText').innerHTML = "Click To Expand All Categories";
        document.getElementById('spanOnOffText').style.fontWeight = "bold";
    }
}

function IsCatOff(oid)
{
    var tr = document.getElementById("tr_" + oid + "_" + 0);
    if(tr == null) tr = document.getElementById("tr_" + oid + "_" + 1);
    
    if(tr != null)
    {
        if(tr.style.display=="none") return true;
        else return false;
    }
}
function IsCatOffCookie(n){if(Get_Cookie(n) == null)return 0;else return Get_Cookie(n);}//0 means this cat is open 
function RememberCat(n){if(Get_Cookie(n) != null)Delete_Cookie(n);else Set_Cookie(n,'1', 365);}

function ShowMenu(divName, hidetime)
{
    this.objDiv = document.getElementById(divName);
    this.hideTime = hidetime;
}

ShowMenu.prototype.st = null;

ShowMenu.prototype.ShowMessage = function(obj, queryString)
{
    var htmlText = this.GetHtmlText(queryString);
    clearTimeout(this.st);
    this.showDiv(htmlText, obj, queryString);
}

ShowMenu.prototype.GetHtmlText = function(type)
{
    switch(type)
    {
        case "showhot":
            return HotContent;
            break;
        case "showunanswerd":
            return UnanswerdContent;
            break;
    }
}

ShowMenu.prototype.HideMessage = function(objName)
{
    if(this.hideTime != 0)
        this.st = setTimeout(objName + ".HideDiv()", this.hideTime);
    else
        this.HideDiv();
}

ShowMenu.prototype.showDiv = function(htmlText, obj, type)
{
    this.objDiv.innerHTML = htmlText;
    this.objDiv.style.display = "block";
    var Browser = GetBrowser();
    if(Browser!= null && Browser.isIE6 == true)
        this.objDiv.style.whiteSpace = "nowrap";
    this.SetDivPosition(obj, type);
}

ShowMenu.prototype.HideDiv = function()
{
    this.objDiv.innerHTML = "";
    this.objDiv.style.display="none";
    this.objDiv.style.top = "0px";
    this.objDiv.style.left = "0px";
}

ShowMenu.prototype.SetDivPosition = function(obj, type)
{
    var intPositionY  = getTopCoordinate(obj);
    if(type == "referfriend") intPositionY += obj.offsetHeight;
    if(type == "showDemographics" || type == "showDetails") intPositionY -= 100;
    var intPositionX = getLeftCoordinate(obj);
    if(type != "referfriend") intPositionX += parseInt(obj.offsetWidth)
    this.objDiv.style.top = intPositionY + "px";
    this.objDiv.style.left = intPositionX + "px";
}

function getLength(){
  var objMessage = document.getElementById(Forum_Post_ClientID + "_post");
  if(objMessage != null)
  {
      var length = objMessage.value.length;
      alert("Your post is "+length+" characters.");
  }

  return false;
}

function insertSmiley(smiley) {
  insertCode(getText() + ' ' + smiley);
  return false;
}

function insertCode(code) {
    var objMessage = document.getElementById(Forum_Post_ClientID + "_post");
    if(objMessage == null)
        return;
    else
    {
        if (objMessage.createTextRange && objMessage.caretPos) 
        {
            objMessage.caretPos.text = code;
        } 
        else {
            objMessage.value += code;
        }

        objMessage.focus();
    } 
}

function storeCaret(el) {
    if (el.createTextRange) 
    {
        el.caretPos = document.selection.createRange().duplicate();
    }
}

function getText() {
    if (document.all) 
    {
        var objMessage = document.getElementById(Forum_Post_ClientID + "_post");
        if(objMessage == null)
            return '';
        
        if (objMessage.createTextRange && objMessage.caretPos) {
            return objMessage.caretPos.text;
        } 
        else 
        {
            return '';
        }
    }
    else 
    {
        return '';
    }
}

function mbCode(e) {
    var tag = e.name;
    var text = getText();

    if (text) 
    {
        var code = "[" + tag + "]" + text + "[/" + tag + "]";
    } 
    else 
    {
        if (e.value.indexOf("*") != -1) 
        {
            var code = "[/" + tag + "]";
            e.value = e.value.substring(0,(e.value.length-1));
        } 
        else 
        {
            var code = "[" + tag + "]";
            e.value += "*";
        }
    }

    insertCode(code);
}

function mbcURL(e) {
    var type = e.name;
    var text = getText();
    var isURL = (text.substring(0,7) == "http://");

    if (type == 'img') 
    {
        if (isURL) 
        {
            var code = "[img]" + text + "[/img]";
        } 
        else 
        {
            var code = text + "[img]" + prompt("URL:","") + "[/img]";
        }
    } 
    else 
    {
        var code = "[" + type + "=" + (isURL ? text : prompt("Enter an address:","")) + "]" + ((text && !isURL) ? text : prompt("Enter a description:","")) + "[/" + type + "]";
    }
    
    insertCode(code);
}

function mbcFont(list) {

    var attrib = list.id.substring(1,list.id.length);
    var value = list.options[list.selectedIndex].value;
    
    if (value && attrib) 
    {
        insertCode("[" + attrib + "=" + value + "]" + getText() + "[/" + attrib + "]");
    }
    
    setTimeout(  "mbtTimer('"+list.id+"')",10);
}

function mbtTimer(e)
{
    var obj = document.getElementById(e);
    if(obj == null)
        return;
    else
    {
        obj.options[0].selected = true;
    }
}

var cookieName_Category = "__galleryShowCategories";
function ShowHideSubCategories()
{
    var objContiner = null, imgSender = null, imgFolder = "", imgMinus = "", imgPlus = "", showCategories="", optionCookie=false;
    
    if(arguments.length > 0){
        objContiner = document.getElementById(arguments[0]);
    }
    if(arguments.length > 1){
        imgSender = arguments[1];
    }
    if(arguments.length > 2){
        imgFolder = arguments[2];
    }
    if(arguments.length > 3){
        imgMinus = arguments[3];
    }
    if(arguments.length > 4){
        imgPlus = arguments[4];
    }
    if(arguments.length > 5){
        if(arguments[5] == "true") {optionCookie=true; showCategories = getCookie(cookieName_Category);}
    }

    if(objContiner)
    {
        if(objContiner.style.display == "" || objContiner.style.display == "block")
        {
            objContiner.style.display = "none";
            if(optionCookie) showCategories = UpdateCookieValue(showCategories, arguments[0], "delete");
            if(imgSender && imgFolder && imgMinus && imgPlus)
            {
                imgSender.src = imgFolder + imgPlus;
            }
        }
        else
        {
            objContiner.style.display = "";
            if(optionCookie) showCategories = UpdateCookieValue(showCategories, arguments[0], "add");
            if(imgSender && imgFolder && imgMinus && imgPlus)
            {
                imgSender.src = imgFolder + imgMinus;
            }
        }
        if(optionCookie) setCookie(cookieName_Category, showCategories);
    }
}

function UpdateCookieValue(oldValue, optionValue, optionType)
{
    if(optionType == "delete")
    {
        if(oldValue.indexOf(optionValue) >= 0)
        {
            oldValue = oldValue.replace("|" + optionValue, "");
        }
    }
    else if(optionType == "add")
    {
        if(oldValue.indexOf(optionValue) < 0)
        {
            oldValue += "|" + optionValue;
        }
    }
    return oldValue;
}

function SetSelectCategories(sender, hideId)
{
    var catId = sender.id.split('_')[1];
    var objHide = document.getElementById(hideId);
    
    if(sender.checked)
    {
        if(objHide.value == "")
        {
            objHide.value = "|" + catId;
        }
        else
        {
            if(objHide.value.indexOf(catId) <0)
                objHide.value = objHide.value+ "|" + catId;
        }
    }
    else
    {
        objHide.value = objHide.value.replace("|" + catId, "");
    }
}

