﻿function setMinutes(min) {
    document.getElementById('minutespan').innerHTML = min;
}

function handleBuddy(e, action, usrid2, nick) {
    var hw = getMouseXY(e);
    var myAjax = new Ajax.Request(
		buddiesUtilitiesLink + action + '&usrid1=' + userID + '&usrid2=' + usrid2 + '&gbr=' + Math.random().toString().substr(3),
		{
		    method: 'get',
		    onSuccess: function(oHttp) {
		        var obj = $('ttMsg');
		        if ("accept" == action)
		            obj.innerHTML = nick + " is now your Buddy on FixYa.com!<br />If you need to contact " + nick + " simply click the \"Ask Buddy\" on your Tech Buddy's Page.";
		        else
		            obj.innerHTML = "You have Ignored " + nick + " Tech Buddy Request.";
		        doModal();
		        ShowToolTipDIVWithPos('divBudAct', hw.y - 100, hw.x - 10);
		    }
		}
	);
}

function refreshMe() {
    document.location.href = unansweredURLPrefix;
}

function deleteRequest(thid) {
    var myAjax = new Ajax.Request(
		buddiesUtilitiesLink2 + thid + '&gbr=' + Math.random().toString().substr(3),
		{
		    method: 'get',
		    onSuccess: function(oHttp) {
		        document.location.href = unansweredURLPrefix + '?gbr=' + Math.random().toString().substr(3) + '#divBuddProbHdr';
		    }
		}
	);
}
var IsIE = Prototype.Browser.IE;
function showChildContent(sender, container, data) {
    sender.style.display = 'none';
    container.innerHTML = unescape(data).replace(/\+/g, ' ');
    container.style.width = '390px';
}
var currentActiveOpenedWriter = null,
	temprte_2Obj,
	divSolTypeObj,
	css_blured = [],
	css_focused = [],
	searchDivs = null,
	actionURL = '/HTTPHandlers/Experts/PostAnswer.ashx?msgid={0}&thid={1}&prdid={2}&ctgid={3}&fmlid={4}&frmid={5}&brdid={6}',
	changeProductURL = '/HTTPHandlers/Experts/ChangeProduct.ashx';
function onSelectButtonClicked(threadID) {
    var productID;
    var fldName;

    if (arguments.length > 1) {
        fldName = "hdnProdId_" + arguments[1];
        if (!($(fldName)))
            fldName = "hdnProdId";
    }
    else
        fldName = "hdnProdId";
    
        
    productID = $F(fldName);
    if (productID == "0")
        return;
    var onSuccess = function(href, result) {
        if ('1' == result.responseText) {
            alert('Product was successfully updated');
            window.location.href = href.replace(/#.*/g, '');
        } else {
            alert('Product was not updated');
        }
    }

    var options = { method: 'post', onSuccess: onSuccess.bind(this, window.location.href), postBody: 'productID=' + productID + '&threadID=' + threadID }
    new Ajax.Request(changeProductURL, options);
}
var onSelectButtonClickedAjaxSuccess = function(href, thid, divContainerObj, itemCounter, oldThreadID, showMessage, result) {
    eval("retObj = " + result.responseText + ";");
    if ('1' == retObj.result) {
        if (showMessage)
            alert('Product was successfully updated');
        eval('var handler = selDelegate_' + itemCounter + ';');
        if ($('btnSelect_' + oldThreadID) !== null) {
            $('btnSelect_' + oldThreadID).stopObserving('click', handler);
        }
        divContainerObj.innerHTML = retObj.html;
        for (var i = 0; i < genericThreads.length; i++) {
            if (genericThreads[i].thid == thid)
                genericThreads[i].thid = retObj.thid;
        }
        eval("var handler = selDelegate_" + itemCounter + ";");
        var autoCompleter = new AutoComplete($('inputsearch_' + itemCounter), $('acresultsdiv_' + itemCounter), 0);
        autoCompleter.ProductIdInput = 'hdnProdId_' + itemCounter;
        handler = onSelectButtonClickedAjax.bind(this, retObj.thid, itemCounter);
        if ($('btnSelect_' + itemCounter) !== null) {
            $('btnSelect_' + itemCounter).observe('click', handler);
        }

    } else {
        alert('Product was not updated');
    }
}

function onSelectButtonClickedAjax(threadID, itemCounter) {
    var productID;
    var fldName;

    if (arguments.length > 1) {
        fldName = "hdnProdId_" + arguments[1];
        if (!($(fldName)))
            fldName = "hdnProdId";
    }
    else
        fldName = "hdnProdId";


    productID = $F(fldName);
    if (productID == "0")
        return;
    var thids = '', divContainer = '';
    for (var i = 0; i < genericThreads.length; i++)
    {
        thids = thids + genericThreads[i].thid + ',';
        if (genericThreads[i].thid == threadID)
            divContainer = genericThreads[i].clientID;
    }
    var postBody = Object.toQueryString({productID:productID, threadID: threadID, page: pageGen, top:topGen, ctgid: m_categoryID, thids:thids, retmsg:1, itemCounter: itemCounter});
    var options = { method: 'post', onSuccess: onSelectButtonClickedAjaxSuccess.bind(this, window.location.href, threadID, $(divContainer), itemCounter, threadID,true), postBody: postBody }
    new Ajax.Request(changeProductURL, options);
}
function onSolveButtonClicked(sender, thid, ctgid, prdid, brdid, fmlid, itemCounter, showSolTypes, arrIndex, frmid, showComplete, messageid) {
    var divCloseObj = $('divClose_' + itemCounter),
        divSeeCommentsObj = $('divSeeComments_' + itemCounter);
    var objToDisplay = [divCloseObj, null];
    if (divSeeCommentsObj !== null)
        objToDisplay[1] = divSeeCommentsObj;
    var container = document.getElementById('divSolutionWriter_' + itemCounter);
    var selDelegate = onSelectButtonClicked.bind(this, thid);

    if ($F("c_solution_window_type") == 0) {
        logEvent(5922);
        logEventPtype(1, 5922);
    }
    if (!checkOpenQuestion())
        return;

    divSolTypeObj = document.getElementById('divSolutionType');
    if (divSolTypeObj !== null)
        divSolTypeObj.style.visibility = (showSolTypes) ? 'visible' : 'hidden';
    document.getElementById('frmAnswer').action = actionURL.replace(/\{0\}/g, messageid).replace(/\{1\}/g, thid).replace(/\{3\}/g, ctgid).replace(/\{2\}/g, prdid).replace(/\{4\}/g, fmlid).replace(/\{5\}/g, frmid).replace(/\{6\}/, brdid);
    if (currentActiveOpenedWriter !== null) {
        if ($('btnSelect') !== null)
            $('btnSelect').stopObserving('click', currentActiveOpenedWriter.selectDelegate);
        setAllDisplay(currentActiveOpenedWriter.objToDisplay, 'none');
        toggleMore(false, currentActiveOpenedWriter.itemCounter);
        container.appendChild(currentActiveOpenedWriter.container.ownerDocument.getElementById('temprte_2'));
        reopen_rte_2();
        setVisible(true, container);
        currentActiveOpenedWriter.sender.className = css_blured[currentActiveOpenedWriter.arrIndex];
    }
    else {
        document.getElementById('hdnURL').value = window.location.href;
        css_blured = bluredCssClass;
        css_focused = focusedCssClass;
        temprte_2Obj = document.getElementById('temprte_2');
        container.appendChild(temprte_2Obj);
        reopen_rte_2();
        temprte_2Obj.style.position = '';
        setVisible(true, temprte_2Obj);
        $('hdnProdId').observe('prd:productChange', checkSelectEnabled.bind(this));
    }
    if ($('btnSelect') !== null) {
        $('btnSelect').observe('click', selDelegate);
        disableButton($('btnSelect'));
    }

    toggleMore(true, itemCounter);
    currentActiveOpenedWriter = { container: container, objToDisplay: objToDisplay, sender: sender, arrIndex: arrIndex, itemCounter: itemCounter, selectDelegate: selDelegate };
    sender.className = css_focused[arrIndex];
    $('hdnProdId').value = '0';
    if ($('inputsearch') !== null)
        $('inputsearch').value = 'Type in the product\'s Brand and Model name';
    if (typeof MyCompleter != 'undefined') {
        MyCompleter.searchInput.style.color = 'gray';
        MyCompleter.firstClick = true;
    }
    setAllDisplay([$('divProduct'), $('btnSelect')], (showComplete) ? 'block' : 'none');
    setAllDisplay(objToDisplay, 'block');
	 
	 if (divSolTypeObj != null) {
        if (frmid == 10)
            divSolTypeObj.style.visibility = 'hidden';
        else
            divSolTypeObj.style.visivility = 'visible';
    }
}
function onCategorizeButtonClicked(sender, thid, ctgid, prdid, brdid, fmlid, itemCounter, messageid, selDelegate) {
    var objToDisplay = [document.getElementById('divClose_' + itemCounter), document.getElementById('divSeeComments_' + itemCounter)];
    var container = document.getElementById('divSolutionWriter_' + itemCounter);
    logEvent(5101);

    divSolTypeObj = document.getElementById('divSolutionType');
    if (divSolTypeObj !== null)
        divSolTypeObj.style.visibility = 'visible';
    if (currentActiveOpenedWriter !== null) {
        if ($('btnSelect_' + itemCounter) !== null)
            $('btnSelect_' + itemCounter).stopObserving('click', currentActiveOpenedWriter.selectDelegate);
        setAllDisplay(currentActiveOpenedWriter.objToDisplay, 'none');
        toggleMoreCat(false, currentActiveOpenedWriter.itemCounter);
        setVisible(true, container);
    }
    else {
        setVisible(true, container);
        $('hdnProdId_' + itemCounter).observe('prd:productChange', checkSelectEnabled.bind(this));
    }
    
    toggleMoreCat(true, itemCounter);
    currentActiveOpenedWriter = { container: container, objToDisplay: objToDisplay, sender: sender, itemCounter: itemCounter, selectDelegate: selDelegate };
    $('hdnProdId_' + itemCounter).value = '0';
    if ($('inputsearch_' + itemCounter) !== null)
        $('inputsearch_' + itemCounter).value = 'Type in the product\'s Brand and Model name';
    if (typeof MyCompleter != 'undefined') {
        MyCompleter.searchInput.style.color = 'gray';
        MyCompleter.firstClick = true;
    }
    setAllDisplay(objToDisplay, 'block');
}

function checkSelectEnabled(id) {
    var hdn = $('hdnProdId');
    var btn = $('btnSelect');
    if (hdn !== null && btn !== null)
        if (hdn.value == '0')
        disableButton(btn);
    else
        enableButton(btn);
}
function reopen_rte_2() {

    var frameHtml = "<html id=\"" + rte_2 + "\">\n";
    frameHtml += "<head>\n";
    frameHtml += '<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />' + "\n";
    frameHtml += "<style type='text/css' media='all'>\n";
    frameHtml += "body {\n";
    frameHtml += "	background: #FFFFFF;\n";
    frameHtml += "	margin: 0px;\n";
    frameHtml += "	padding: 4px;\n";
    frameHtml += "	font-family: arial, verdana, sans-serif;\n";
    frameHtml += "	font-size: 10pt;\n";
    frameHtml += "}\n";
    frameHtml += "p {\n";
    frameHtml += "	margin:0px;\n";
    frameHtml += "	padding:0px;\n";
    frameHtml += "}\n";
    frameHtml += '.spwrap\n';
    frameHtml += '{\n';
    frameHtml += 'background: transparent url(../wysiwyg_rte_2/misspell.gif) repeat-x scroll center bottom;\n';
    frameHtml += '}\n';
    frameHtml += "</style>\n";
    frameHtml += "</head>\n";
    frameHtml += "<body>\n";
    frameHtml += "</body>\n";
    frameHtml += "</html>";
    try {
        rte_2window = document.getElementById(rte_2).contentWindow;
        rte_2document = document.getElementById(rte_2).contentDocument;



        try {
            rte_2window.document.open("text/html", "replace");
            rte_2window.document.write(frameHtml);
            rte_2window.document.close();
            rte_2window.document.body.style.fontSize = '10pt';

            rte_2window.document.addEventListener("mouseup", rte_2_button_update, false);
            rte_2window.document.addEventListener("keyup", rte_2_button_update, false);
            if (isGecko) {
                //attach a keyboard handler for gecko browsers to make keyboard shortcuts work
                rte_2window.document.addEventListener("keypress", kb_handler, false);
            }


        }
        catch (e) {
            //alert(jsfile_errorc_lang + e);
        }


        rte_2window.document.designMode = "on";

    }
    catch (e) {

    }

    castspell2();
}
function closeOpenedItems(arrIndex) {
    var container = currentActiveOpenedWriter.container;
    if (currentActiveOpenedWriter === null)
        return;
    if (!checkOpenQuestion())
        return;
    setVisible(false, container);
    currentActiveOpenedWriter.sender.className = css_blured[currentActiveOpenedWriter.arrIndex];
    setAllDisplay(currentActiveOpenedWriter.objToDisplay, 'none');
    if (arguments.length > 1)
        toggleMore(false, arguments[1]);
}
function closeOpenedCategorizeItems() {
    var container = currentActiveOpenedWriter.container;
    if (currentActiveOpenedWriter === null)
        return;
    setAllDisplay(currentActiveOpenedWriter.objToDisplay, 'none');
    if (arguments.length > 1)
        toggleMoreCat(false, arguments[1]);
}
function setProp(varName, name) {
    if (typeof eval(varName + '.' + name) == 'undeinfed')
        eval(varName + '.' + name) = null;
    return eval(varName + '.' + name);
}
function setAllDisplay(arr, value) {
    for (var i = 0; i < arr.length; i++)
        if (arr[i] !== null)
        arr[i].style.display = value;
    // Constant object that need to be set
    var divSolObj = document.getElementById('divSolutionType');
    if (divSolObj !== null)
        divSolObj.style.display = value;
}
function setVisible(show, element) {
    if (IsIE) {
        element.style.display = (show) ? 'block' : 'none';
    } else {
        if (typeof temprte_2Obj != 'undefined') {
            if (temprte_2Obj !== null) {
                temprte_2Obj.style.visibility = (show) ? 'visible' : 'hidden';
                temprte_2Obj.style.height = (show) ? 'auto' : '1px';
                temprte_2Obj.style.display = (show) ? 'block' : 'none';
            }
        }
        element.style.visibility = (show) ? 'visible' : 'hidden';
        element.style.height = (show) ? 'auto' : '1px';
        element.style.display = (show) ? 'block' : 'none';
    }
}
function toggleDiv(index) {
    if (searchDivs === null)
        setSearchDivs();
    clearDisplay(index);
    searchDivs[index].style.display = (searchDivs[index].style.display != 'block') ? 'block' : 'none';
}
function toggleMore(isExpand, itemIndex) {
    var spnA = document.getElementById('spanContainer_' + itemIndex + 'a'),
		spnB = document.getElementById('spanContainer_' + itemIndex + 'b');
    if (spnB !== null) {
        spnA.style.display = (isExpand) ? 'none' : 'block';
        spnB.style.display = (isExpand) ? 'block' : 'none';
    }
}
function toggleMoreCat(isExpand, itemIndex) {
    var spnA = $('spanContainer_' + itemIndex + 'a'),
		spnB = $('spanContainer_' + itemIndex + 'b'),
		divCommentsObj = $('divSeeComments_' + itemIndex),
		spnMoreObj = $('spnMore_'+itemIndex);
    if (spnB !== null) {
        spnA.style.display = (isExpand) ? 'none' : 'block';
        spnB.style.display = (isExpand) ? 'block' : 'none';
    }
//    if (divCommentsObj !== null) {
//        divCommentsObj.style.display = (isExpand) ? 'none' : 'block';
//    }
    if (spnMoreObj !== null)
        spnMoreObj.style.display = (isExpand) ? 'none' : '';
}
function toggleMoreSol(isFirstShow, obj1, obj2) {
    for (var i = 0; i < obj1.length; i++) {
        obj1[i].style.display = (isFirstShow) ? '' : 'none';
    }
    for (var i = 0; i < obj2.length; i++) {
        obj2[i].style.display = (isFirstShow) ? 'none' : '';
    }
}

function AddGreenBorder(obj) {
    $(obj).style.border = '1px solid #99BE64';
    $(obj).style.zIndex = '65';
}
function RemoveGreenBorder(obj) {
    $(obj).style.border = 'none';
    $(obj).style.zIndex = '0';
}

function toggleElement(obj) {
    var isDisplayed = false;
    if (obj.style.display == 'none')
        isDisplayed = true;
    else
        isDisplayed = false;
    obj.style.display = (isDisplayed) ? 'block' : 'none';
}
function setSearchDivs() {
    searchDivs = [document.getElementById('divUnsolvedForm')];
}
function clearDisplay(index) {
    for (var i = 0; i < searchDivs.length; i++) {
        if (i != index)
            searchDivs[i].style.display = 'none';
    }
}
function checkOpenQuestion() {
    if (typeof rte_2window == 'undefined') return false;

    if (rte_2window.document === null)
        rte_2window = document.getElementById(rte_2).contentWindow;
    update_rte_2();
    var content = document.getElementById(g_rte_2_hcontent);
    if (content == null)
        return true;
    if (typeof content.value == 'undefined')
        return true;
	if (content.value.indexOf('from fixya rte') == 13)
        return true;
    if (content.value.length > 0) {
        if (confirm("Navigating away from this page will cause you to lose your changes.\n\nDiscard your message?")) {
            reopen_rte_2();
            return true;
        }
        else
            return false;
    }
    return true;
}
function resetInput() {
    if (typeof this.isDirty == 'undefined') {
        this.style.color = '#000000';
        this.value = '';
        this.isDirty = true;
    }
}
function isDirty(obj) {
    if (typeof obj.isDirty == 'undefined')
        return false;
    else
        return obj.isDirty;
}
function srchSubmit(obj, formObj) {
    if (!isDirty(obj))
        return false;
    else {
        formObj.submit();
    }
}
var actionDelegate = null;
function setAction(actionID, msgid) {
    var flagText = '';
    var userName = '';
    switch (actionID) {
        case ModerateAction.Spam: flagText = 'Spam'; break;
        case ModerateAction.Revert: flagText = 'Revert'; break;
        case ModerateAction.Question: flagText = 'Question'; break;
        case ModerateAction.ClarificationRequest: flagText = 'Clarification Request'; break;
        case ModerateAction.Approve: flagText = 'Approved'; break;
        case ModerateAction.SpammerAccount:
            flagText = 'Spammer Account';
            userName = arguments[2];
            break;
    }
    if (actionID == ModerateAction.Revert) {
        $('div_FlagMeAs').innerHTML = "You're about to <span id=\"spnFlagMeAs\"></span> this Tip.<br />Are you sure?";
    }
    else {
        $('div_FlagMeAs').innerHTML = "You're about to flag this as <span id=\"spnFlagMeAs\"></span>.<br />Are you sure?";    
    }    
    
    $('spnFlagMeAs').innerHTML = flagText;
    doModal();
    ShowCenteredAlertDIV("divFlagAsMessage");
    var btnYes = $('btnFlagYes'),
        btnNo = $('btnFlagNo');
    actionDelegate = moderate.bind(this, btnYes,btnNo, actionID, msgid, userName);
    btnYes.observe('click', actionDelegate);
    btnNo.observe('click', clearObservers.bind(this, [btnYes, btnNo]));
}
function moderate(btnYes, btnNo, actionID, msgid,userName) {
    //btnYes.stopObserving('click', actionDelegate);
    clearObservers([btnYes, btnNo]);
    callModerate(actionID, msgid,userName);
}
function callModerate(actionID, msgid,userName) {
    callModerateHandler(actionID, msgid, userName, onActionSuccess.bind(this, actionID));
}
function callModerateHandler(actionID, msgid, userName, handler) {
    var msgids = '';
    
    if (isTips == 'true') {
        for (var i = 0; i < moderateIndexTips.length; i++) {
            msgids = msgids + moderateIndexTips[i].msgid + ',';
        }
    }
    else {
        for (var i = 0; i < moderateIndex.length; i++) {
            msgids = msgids + moderateIndex[i].msgid + ',';
        }
    }
    var postObject = { actiontype: actionID, msgid: msgid, page: page, top: top, ctgid: m_categoryID, msgids: msgids };
    if (typeof userName != undefined)
        postObject.posterName = userName;
    new Ajax.Request('/HttpHandlers/Experts/moderate.ashx', { method: 'post', postBody: Object.toQueryString(postObject), onSuccess: handler });
}
function clearObservers(btnArray) {
    if (actionDelegate === null)
        return;
    for (var i=0;i<btnArray.length;i++)
    {
        btnArray[i].stopObserving('click', actionDelegate);
    }
}
function onActionSuccess(result, action) {
    eval('var resultValue = ' + action.responseText);
    HideToolTipDIV('divFlagAsMessage');
    clearObservers([$('btnFlagYes'), $('btnFlagNo')]);
    if (resultValue.result == ModerateResult.AlreadyModerated) {
        doModal();
        ShowCenteredAlertDIV("divModWarn");
    }
    else {
        //moderateIndex OR moderateIndexTips
        var modItem,resItem;
        for (var i = 0; i < resultValue.htmlArray.length; i++) {
            resItem = resultValue.htmlArray[i];
            if (resItem != false) {
                if (isTips == 'true') {
                    modItem = moderateIndexTips[resItem.index];
                }
                else {
                    modItem = moderateIndex[resItem.index];
                }
                modItem.msgid = resItem.msgid;
                $(modItem.containerID).innerHTML = resItem.html;
            }
        }
    }
    //window.location.reload(true);
}
function onGenericSpecified(threadID, itemCounter) {
    var thids = '', divContainer = '';
    for (var i = 0; i < genericThreads.length; i++) {
        thids = thids + genericThreads[i].thid + ',';
        if (genericThreads[i].thid == threadID)
            divContainer = genericThreads[i].clientID;
    }
    var postBody = Object.toQueryString({ skipCategorize : 1, page: pageGen, top: topGen, ctgid: m_categoryID, thids: thids, retmsg: 1, itemCounter: itemCounter });
    var options = { method: 'post', onSuccess: onSelectButtonClickedAjaxSuccess.bind(this, window.location.href, threadID, $(divContainer), itemCounter, threadID, false), postBody: postBody }
    new Ajax.Request(changeProductURL, options);
}
function ctrl_solution_editor_doPost(me) {
    update_rte_2();
    var isOK = ctrl_solution_editor_checkf();
    if (typeof ctrl_solution_editor_checkRnr == 'function')
        isOK = isOK && ctrl_solution_editor_checkRnr()
    if ($F("c_solution_window_type") == 0) {
        logEventPtype(1, 5923);
    } else {
        logEvent(5946);
    }

    return isOK;
}
function ctrl_solution_editor_checkf(z) {
    var ttl = $i("rte_text2");
    var sttl = ttl.value;
    if ($i("rte_html2").value.length > 10000) {
        alert('The post body is too long, \n\rPlease remove some text.');
        return false;
    }
    sttl = sttl.replace(/^\s+|\s+$/g, "");
    ttl.value = sttl;
    if (sttl.length < 3) {
        alert("Please fill in your response.");
        return false;
    }
    return true;
}

/*ctrl_CategoryExpert_Status*/
function openInfo(obj) {
    $('categoryExpertInfoClosed' + obj).style.display = 'none';
    $('categoryExpertInfoOpened' + obj).style.display = 'block';
    return false;
}
function closeInfo(obj) {
    $('categoryExpertInfoOpened' + obj).style.display = 'none';
    $('categoryExpertInfoClosed' + obj).style.display = 'block';
    return false;
}