﻿
// cmajorossy 12.01.2007 :: function ToolTip for error handeling
//MooTools, My Object Oriented Javascript Tools. Copyright (c) 2006-2007 Valerio Proietti, <http://mad4milk.net>, MIT Style License.
// cmajorossy 11.20.2007 :: function swapBackround for imageGallery created
// cmajorossy 11.14.2007 :: add ajax control for htnl requestrs

if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 var ieversion=new Number(RegExp.$1); // capture x.x portion and store as a number

}

function searchreviews(qstring) {
    window.location='/review/advsearch.aspx?t=' + encodeURIComponent(qstring);
}

function searchgoogle(elem) {
    var tag = document.getElementById(elem),
        val = tag !== null ? tag.value : elem;
    // window.location='http://www.laptopmag.com/search-results.aspx?q=' + encodeURIComponent(qstring) + '&cx=011239505048760151329%3axtwjxuvb8mo&cof=FORID%3a11';
    window.location = 'http://www.laptopmag.com/search.aspx?q=' + encodeURIComponent(val);
} 


function clearcell(cleartext,textfield) {
	if (textfield.value == cleartext) {
	textfield.value = '';
	}
}

function checkEnter(e,fn,passedvar){ 
    var characterCode;
    if (e && e.which){ 
    e = e
    characterCode = e.which //character code is contained in NN4's which property
}
else {
    e = event
    characterCode = e.keyCode //character code is contained in IE's keyCode property
}

if (characterCode == 13) { 
fn(passedvar);
return false;
}
else {
return true;
}

}

function cbusterlink(url) {
    var rnd = (Math.round((9999999999-1000000000) * Math.random() + 1000000000));
    var code = '<A HREF="' + url.replace('[CACHEBUSTER]',rnd) + '" target="_blank">';
    document.write(code);
}
function cbusterimg(url) {
    var rnd = (Math.round((9999999999-1000000000) * Math.random() + 1000000000));
    var code = '<IMG SRC="' + url.replace('[CACHEBUSTER]',rnd) + '" border="0">';
    document.write(code);
}

function hideAdvSearch() {
    if (ieversion != null) {
        if (ieversion < 7) {
            document.getElementById("advsearchd").style.display = 'none';
            /*
            document.getElementById("prodcat").style.display = 'none';
            document.getElementById("prodsubcat").style.display = 'none';
            */
        }
    }
}
function showAdvSearch() {
    if (ieversion != null) {
        if (ieversion < 7) {
            document.getElementById("advsearchd").style.display = 'block';
        }
    }
}

function clearCtrl(obj) {
    var temp = document.getElementById(obj);

    if (temp !== null) {
        temp.value = '';
    }
}

function showCtrl(obj) {
    var temp = document.getElementById(obj);

    if (temp !== null) {
        temp.style.display = 'block';
    }
}

function hideCtrl(obj) {
    var temp = document.getElementById(obj);

    if (temp !== null) {
        temp.style.display = 'none';
    }
}

//Main Feature functions
function setSelectedMFTab(obj)
{
    document.getElementById('ctl00_ContentPlaceHolder1_MainFeature1_hlTab1').className = "";
    document.getElementById('ctl00_ContentPlaceHolder1_MainFeature1_hlTab2').className = "";
    document.getElementById('ctl00_ContentPlaceHolder1_MainFeature1_hlTab3').className = "";
    document.getElementById('ctl00_ContentPlaceHolder1_MainFeature1_hlTab4').className = "";
    document.getElementById(obj).className = "selected";
}

function hideAllMFContent()
{
    hideCtrl('mfContent1');
    hideCtrl('mfContent2');
    hideCtrl('mfContent3');
    hideCtrl('mfContent4');
}

function showMFTab(tab)
{
   var divContent = 'mfContent' + tab;
   var tabObj = 'ctl00_ContentPlaceHolder1_MainFeature1_hlTab' + tab; 
   //alert(divContent);alert(tabObj);
   hideAllMFContent();
   showCtrl(divContent);
   setSelectedMFTab(tabObj);
}

var tab = 1;
var t;

function rotateMF()
{
    if(tab==5){tab=1;}
    //alert(tab);
    showMFTab(tab);
    t=setTimeout("rotateMF()",6000);
    tab++;
}

function stopRotate()
{
    clearTimeout(t);
}

// cmajorossy 11.14.2007 :: add ajax control for htnl requestrs
function ajaxFunction(requestPage, divElement) {
	var ajaxRequest;  
	// Browser Support Code
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	ajaxRequest.onreadystatechange = function() {
		if(ajaxRequest.readyState == 4)
		{
			document.getElementById(divElement).innerHTML = ajaxRequest.responseText;
		}						
	}
	ajaxRequest.open("GET", requestPage, true);
	ajaxRequest.send(null); 
}


function ajaxFunction_JSON(requestPage, divElement){
	var ajaxRequest;  
	// Browser Support Code
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("UNable to display in your browser.");
				return false;
			}
		}
	}
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4)
		{
			var JSON = eval('(' + ajaxRequest.responseText + ')'); 
			document.getElementById(divElement).innerHTML = JSON.HTML;
			eval(JSON.JS);	
		}						
	}
	ajaxRequest.open("GET", requestPage, true);
	ajaxRequest.send(null); 
}

function trackclick(d, r, n) {
	if (n === undefined) {
	n = '';
	}
    if(document.images){
        (new Image()).src="http://www.laptopmag.com/feeds/ct.php?r="+r+
        "&d="+d+"&n="+n;
    }

    _gaq.push(["_trackEvent", n, "click", ""]);
    
    return true;
}

function trackrevshop(d, r) {
    var n = 'pg_revpull';
    if (d.substr(0,17) == '/review/shop.aspx') {
	    if(document.images){
            (new Image()).src="http://www.laptopmag.com/feeds/ct.php?r="+r+
            "&d="+d+"&n="+n;
		}
    }

    window.location=d;
}


// cmajorossy 11.20.2007 :: function swapBackround for imageGallery created
function swapBackround(elementFill, imageSource)
{
    document.getElementById(elementFill).style.backgroundImage = "url('"+imageSource+"')";
}

function openWin(URL) {
    trackclick(URL, window.location, 'print');
    location.href = URL;
}


var table_tabs = new Array();
table_tabs[0] = "posts";
table_tabs[1] = "topics";

function HideContent(d) {
    document.getElementById("pagenav_" + d).style.display = "none";
}
function ShowContent(d) {
    document.getElementById("pagenav_" + d).style.display = "block";
    document.getElementById("td_" + d + "_hidden").style.display = "none";
    document.getElementById("td_" + d + "_active").style.display = "block";

    for (var i = 0, len = table_tabs.length; i < len; ++i) {
        if (table_tabs[i] != d) {
            document.getElementById("pagenav_" + table_tabs[i]).style.display = "none";
            document.getElementById("td_" + table_tabs[i] + "_active").style.display = "none";
            document.getElementById("td_" + table_tabs[i] + "_hidden").style.display = "block";
        }
    }

    if ((d != "products") && ((document.getElementById("td_products_active").style.display == "block") || (document.getElementById("td_products_hidden").style.display == "block"))) {
        document.getElementById("pagenav_products").style.display = "none";
        document.getElementById("td_products_hidden").style.display = "block";
        document.getElementById("td_products_active").style.display = "none";

    }
}

function hovertd(id) {
    var obj = document.getElementById(id);
    obj.className = "active_tab";
}

function unhovertd(id) {
    var obj = document.getElementById(id);
    obj.className = "inactive_tab";
}

function changeclass(elm, newclass) {
    var currTabElem = document.getElementById(elm);
    currTabElem.setAttribute("class", newclass);
    currTabElem.setAttribute("className", newclass);
    return;
}

/*************************** Nav Functions *********************************/
function navSetup() {
    var path = (document.location.pathname).toLowerCase(),
        parts = path.split("/");

    if (parts.length > 1) {
        var loc = parts[1] == "l" ? parts[2] : parts[1],
            a, b;

            if ( loc.substring(0, 3) === "ggg") {
                loc = "ggg";
            }
            
            loc = loc.replace(".aspx", "");
            a = $("#" + loc + "_a"),
            b = $("#" + loc + "_b");

        a.addClass("active");

        if (b.length > 0) {
            b.addClass("clearfix");
            a.parent().css("border-bottom", "none");
        }
    }

    setWidth();
}

function setWidth() {
    var max = $('#nav ul').width() - 1,
        wid = 0,
        diff = 0,
        ct = 0,
        delta = 0;

    $('#nav ul li').each(function () {
        wid += $(this).outerWidth(true);
    });

    diff = Math.round((max - (wid)) / 4);
    delta = ((wid + (diff * 4)) - max);

    $('#nav ul li a').each(function () {
        if (ct > 6 && ct < 9) {
            var me = $(this),
                padLeft = parseInt(me.css('padding-left')),
                padRight = parseInt(me.css('padding-right'));

            me.css('padding-left', (padLeft + (diff - ((ct == 7 && delta > 0) ? delta : 0))) + "px");
            me.css('padding-right', (padRight + diff) + "px");
        }

        ct++;
    });
}

function getProducts() {
    $.getJSON('http://www.laptopmag.com/scripts/service.ashx?callback=?&op=get_products', function (json) {
        var options = [];
        searchJson = json;

        options.push("<option value=''> -- All Products -- </option>");
        $('#ddlProducts').html(genSelect(searchJson.Products, options));
        getBrands();
    });
}

function getBrands() {
    var id = getSelectedValue("ddlProducts") || "-1",
        options = new Array();

    options.push("<option value=''> -- All -- </option>");
    $('#ddlBrands').html(genSelect(searchJson.Sub["sub_" + id], options));
}

function genSelect(data, options) {
    if (typeof data !== 'undefined') {
        for (var ct = 0, len = data.length; ct < len; ct += 1) {
            options.push("<option value='" + data[ct].value + "'>" + data[ct].name + "</option>")
        }
    }

    return options.join('');
}

function getSelectedValue(elem) {
    try {
        var e = document.getElementById(elem);
        return e.options[e.selectedIndex].value
    } catch (err) {
        return "";
    }
}

function advSearch() {
    var price = document.getElementById('pricehigh').value,
        arr = [
            { "name": "pcid", "value": getSelectedValue("ddlProducts") },
            { "name": "spcid", "value": getSelectedValue("ddlBrands") },
            { "name": "pricehigh", "value": (isNaN(parseInt(price)) ? "" : price) }
        ],
        qstr = "",
        ct = 0;

    for (x in arr) {
        var val = arr[x].value;
        if (val !== "" && typeof val !== 'undefined') {
            qstr += (ct === 0 ? "?" : "&") + arr[x].name + "=" + arr[x].value;
            ct += 1;
        }
    }

    document.location = "http://www.laptopmag.com/review/advsearch.aspx" + qstr;
}

function r_focus(me, txt) {
    if (me.value == txt) {
        me.value = "";
    }
}
function r_blur(me, txt) {
    if (me.value == "") {
        me.value = txt;
    }
} 

