﻿var isMainSiteLink;
function GetMainSiteLinks() {
    isMainSiteLink = true;
}

var ProductListArr = []
function usr_details(img, nick, link, level, rank_icon) {
    this.img = img;
    this.nick = nick;
    this.link = link;
    this.level = level;
    this.rank_icon = rank_icon;
}
function lhStartClock() {
    g_lhClockID = setTimeout("lhUpdateClock()", g_lhClockTick);
}
function validateEmail(str) {
    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9_]{2,4})+$/;
    if (str.match(emailRegEx)) {
        return true;
    }
    else {
        alert('Please enter a valid email address');
        return false;
    }
}
function lhUpdateClock() {
    if (g_lhClockID) {
        clearTimeout(g_lhClockID);
        g_lhClockID = 0;
    }

    var prevOwnerDiv = $i('lh_div' + g_CurrentExpert);

    g_CurrentExpert++;

    g_CurrentExpert = g_CurrentExpert % g_NumExperts;
    var currOwnerDiv = $i('lh_div' + g_CurrentExpert);

    var AnimFadePrev = new Animation(prevOwnerDiv.id, 50, 2, 90);

    AnimFadePrev.FadeOut(600);
    prevOwnerDiv.style.display = 'none';

    currOwnerDiv.style.display = 'block';
    currOwnerDiv.style.zIndex = '13';
    currOwnerDiv.style.opacity = '1';

    prevOwnerDiv.style.zIndex = '12';
    //prevOwnerDiv.style.display = 'none';

    currOwnerDiv.style.filter = 'alpha(opacity=100)';
    g_lhClockID = setTimeout("lhUpdateClock()", g_lhClockTick);
}
function openDrawer() {
    var AnimDrawerMov = new Animation('lh_drawer', 50, 4, 84);
    var AnimDrawerStretch = new Animation('lhStretch', 50, 4, 84);

    AnimDrawerMov.MoveDown();
    AnimDrawerStretch.ExpandHeight();
}

function ClearSelected(listBox) {
    for (var i = 0; i < listBox.options.length; i++) {
        listBox.options[i].selected = false;
    }
}

function SelectedIndex(indx, lbl) {
    stopTabs();
    for (var i = 0; i < 4; i++) {
        var list = $('rtr_list' + i);

        var listBox = $('TopRecommendLst');
        //ClearSelected(listBox);
        listSelect(list, i == indx);
    }
}

function tabClick(indx) {
    for (var i = 0; i < g_num_tabs; i++) {
        // var tab = $('rtr_tab' + i);
        var list = $('rtr_list' + i);

        if (null != list) {
            var listBox = $('TopRecommendLst');
            //tabSelect(tab, i == indx);
            //ClearSelected(listBox);
            listBox.options[indx].selected = true;

            listSelect(list, i == indx);
        }
    }
    //    var rtr_name_span = $('rtr_name_span');
    //    var rtr_name_span_tab = $('rtr_name_span' + indx);
    //    if (rtr_name_span != null && rtr_name_span_tab != null) {
    //        rtr_name_span.innerHTML = rtr_name_span_tab.innerHTML;
    //    }
}
function tabSelect(tab, isselect) {
    tab.className = isselect ? 'tab selected' : 'tab';
}
function listSelect(list, isselect) {
    list.className = isselect ? 'list_box' : 'list_box hide';
}

function rotateTabs() {
    g_tabs_clock = setInterval(nextTab, g_tabs_clock_tick);
    var elm = $('TopRecommendLst');
    var textLbl = elm.options[elm.selectedIndex].text
    $('lblTopCategory').innerHTML = textLbl;

}
function stopTabs() {
    clearInterval(g_tabs_clock);
}
function nextTab() {
    g_selected_tab++;
    g_selected_tab = g_selected_tab % (g_num_tabs);
    tabClick(g_selected_tab);

    var elm = $('TopRecommendLst');
    var textLbl = elm.options[elm.selectedIndex].text
    //alert(textLbl);
    $('lblTopCategory').innerHTML = textLbl;
}
function showComplete(sender) {
    $('divProductsRec').style.display = '';
    sender.style.display = 'none';
}
function getProductList() {
    var val = $F('hdnProdId');
    for (var i = 0; i < ProductListArr.length; i++)
        if (ProductListArr[i] == val)
        return;
    isAllZero('');
    ProductListArr.push(val);
    new Ajax.Request('/httphandlers/prd/get_prd_info.ashx?prdid=' + val, { method: 'get', onSuccess: updateList.bind(this, val) });
}
function setButtonTitle(val, isLongValue) {
    var btnSelectObj = $('btnSelect');
    if (btnSelectObj === null)
        return;
    btnSelectObj.innerHTML = val;
    if (isLongValue) {
        btnSelectObj.style.fontSize = '14px';
        btnSelectObj.style.paddingTop = '2px';
        btnSelectObj.style.height = '20px';
    }
    else {
        btnSelectObj.style.fontSize = '16px';
        btnSelectObj.style.paddingTop = '0px';
        btnSelectObj.style.height = '22px';
    }
}
function checkProducts() {
    var hdnProductListObj = $('hdnProductList');
    if (hdnProductListObj === null)
        return true;
    if (hdnProductListObj.value.indexOf(',') > -1) {
        return true;
    }
    else {
        alert('Please enter product from the list');
        return false;
    }
}

var imageSrcURL = "";
function updateList(prdid, returnedValue) {
    if (prdid != 0)
        setButtonTitle('Select another Product', true);
    else {
        alert('Please enter product from the list');
    }
    eval('var retObj = ' + returnedValue.responseText);
    var oLI = document.createElement('DIV'),
			oAnc1 = document.createElement('A'),
			oAnc2 = document.createElement('span');
    oAnc1.href = retObj.ProductUrl;
    oAnc1.target = '_blank';
    oAnc1.innerHTML = retObj.ProductName;

    if (isMainSiteLink) {
        oAnc1.href = retObj.ProductUrl.replace("recommendations", "support");
    }

    //oAnc2.href = 'javascript:void(0);';
    oAnc2.innerHTML = 'Change';
    oAnc2.style.cursor = 'hand';
    oAnc2.style.cursor = 'pointer';
    oAnc2.style.textDecoration = 'underline';
    oAnc2.style.color = '#000000';
    oAnc2.setAttribute("font-size", "13px !important");
    
    var url = "/rnr/AjaxGetImageURL.aspx?prdid=" + prdid;
    new Ajax.Request(url, { method: 'get', onComplete: showImageURL });
    
    oLI.appendChild(oAnc1);
    oLI.appendChild(document.createTextNode(' | '));
    oLI.appendChild(oAnc2);

    $('ulProductsRec').appendChild(oLI);
    
    
    $(oAnc2).observe('click', removeProduct.bind(oLI, prdid));
    
    
    setHiddenProducts();

    $('SelectAndProductLine').style.display = 'none';
    $('divProductsRecommended').style.display = 'block';
}

function showImageURL(response) {
    $('img_id_product').src = response.responseText.toString();
}

function ShowSelectAndProductLine() {
    document.getElementById('SelectAndProductLine').style.display = 'block';
    document.getElementById('divProductsRecommended').style.display = 'none';

    return false;
}

function setHiddenProducts() {
    var _tempString = '';
    for (var i = 0; i < ProductListArr.length; i++)
        if (ProductListArr[i] != '0')
        _tempString = _tempString + ProductListArr[i] + ',';
    $('hdnProductList').setValue(_tempString);
}

function removeProduct(prdid, sender) {
    sender.element().stopObserving('click');
    $(sender.element().parentNode).remove();
    for (var i = 0; i < ProductListArr.length; i++)
        if (ProductListArr[i] == prdid)
        ProductListArr[i] = '0';
    setHiddenProducts();
    isAllZero('none');


    // remove product
    return ShowSelectAndProductLine();
}
function isAllZero(display) {
    var _isAllZero = true;
    for (var i = 0; i < ProductListArr.length; i++)
        if (ProductListArr[i] > 0)
        _isAllZero = false;
    if (_isAllZero) {
        setButtonTitle('Select', false);
    }
    //    if (_isAllZero)
    //        $('divProductsRecommended').style.display = display;
    return _isAllZero;
}
function checkEmptyText() {
    update_rte();
    if (isError) {
        return false;
    }
    var _errMsg = 'Please enter recommendation text';
    if (g_rte_hcontent === null) return alertError(_errMsg);
    if ($F(g_rte_hcontent) == null) return alertError(_errMsg);
    if (/(^$|^[ ]{1,}$)/.test($F(g_rte_hcontent)))  // check blank or empty
        return alertError(_errMsg);
    return true;
}
function alertError(errMsg) {
    alert(errMsg);
    return false;
}
function checkRec() {
    setGS(1);
    if ($('ctgid') === null || $('sel_cat2') === null) return true;
    if ($F('sel_cat2') == '-1') return alertError('Please select a category');
    else if ($F('sel_cat2') == '-2') {
        if ($('ctgid').selectedIndex == -1)
            return alertError('Please select a sub category');
    }
    return true;
}
function rvw_listprod_sort(slct) {
    if (slct != null) {
        var opt = slct[slct.selectedIndex].value;
        var currentURL = location.href;
        currentURL = currentURL.replace(/#.*$/g, '');
        currentURL = currentURL + '#' + opt;
        location.href = currentURL;
        location.reload();
    }
}
function rvw_listprod_submitsort(slct) {
    if (slct != null) {
        var fremSort = $('fremSort');
        var hdnSort = $('hdnSort');

        if (fremSort != null && hdnSort != null) {
            hdnSort.value = slct[slct.selectedIndex].value;
            fremSort.submit();
        }
    }
}
function checkSelectors() {
    if ($F('sel_cat2') == '-2')
        $('categoy_selection').style.display = 'block';
    else
        $('categoy_selection').style.display = 'none';
}
RnrHeighter = function(prd_info_height, top3Obj, divContainers) {
    this.top3Obj = top3Obj;
    this.prdInfoHeight = prd_info_height;
    this.divContainers = divContainers;
    if (this.top3Obj !== null) {
        this.prdInfoHeight = this.prdInfoHeight + this.top3Obj.offsetHeight;
        if (!Prototype.Browser.IE)
            this.prdInfoHeight += 5;
    }
    else
        this.prdInfoHeight += 5;
    for (var i = 0; i < divContainers.length; i++) {
        divContainers[i].style.display = divContainers[i].childElements()[0].style.display;
        divContainers[i].childElements()[1].style.height = this.prdInfoHeight - 5 + 'px';
    }
}
function showSelectCategories() {
    doModal();
    ShowCenteredAlertDIV("popup_otherCategories");
}

function closeSelectOtherCategories() {
    HideToolTipDIV("popup_otherCategories");
}

function validateAskOwner() {
    //update_rte();
    if ($F('repair_text').length < 10)
        alert('Please enter text');
    //    if (document.getElementById('hdnctgid').value == 0)
    //        showSelectCategories();
    else
        $('felm').submit();

}

function validateAskOwnerGeneric(obj) {
    if ($F(obj) < 10)
        alert('Please enter text');
    else
        $('felm').submit();
    //showSelectCategories();
}
function submitCategory() {
    if (($F('sel_cat') == 231) || ($F('sel_cat') == 391)) { //cars=231 motorcycles=391 
        $('felm').submit();
        closeSelectOtherCategories();
        return;
    }
    if ($F('sel_cat') <= 0) {
        alert('Please select a category');
    }
    else if ($F('ctgid') <= 0)
        alert('Please select a sub category');
    else {
        $('felm').submit();
        closeSelectOtherCategories();
    }
}
function ValidateTextArea(obj) {
    if ($F(obj) < 10) {
        alert('Please enter text');
    }
    else {
        $('form5').submit();
    }
} 
function ShowLessMessageM(uniqueID) {
    $('recLongdesc' + uniqueID).style.display = 'none';
    $('recShortdesc' + uniqueID).style.display = 'block';
}
function ShowMoreMessageM(uniqueID) {
    $('recLongdesc' + uniqueID).style.display = 'block';
    $('recShortdesc' + uniqueID).style.display = 'none';
}
function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\#&]" + name + "=([^&]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) return "";
    else return results[1];
}