var MAX_channel = '';
var MAX_p=location.protocol=='https:'?'https:':'http:';
var MAX_r=Math.floor(Math.random()*99999999);
if (!document.MAX_used) document.MAX_used = ',';
function MAX_adjs(z,n)
{
  if (z>-1) {
    var az="<"+"script language='JavaScript' type='text/javascript' ";
    az+="src='"+MAX_p+"//dynamic1.dailytech.com/www/delivery/ajs.php?n="+n+"&zoneid="+z;
    az+="&source="+MAX_channel+"&exclude="+document.MAX_used+"&r="+MAX_r;
    az+="&mmm_fo="+(document.mmm_fo)?'1':'0';
    if (document.context) az+= "&context=" + escape(document.context);
    if (window.location) az+="&loc="+escape(window.location);
    if (document.referrer) az+="&referer="+escape(document.referrer);
    az+="'><"+"/script>";
    document.write(az);
  }
}
function MAX_adpop(z,n)
{
  if (z>-1) {
    var az="<"+"script language='JavaScript' type='text/javascript' ";
    az+="src='"+MAX_p+"//dynamic1.dailytech.com/www/delivery/apu.php?n="+n+"&zoneid="+z;
    az+="&source="+MAX_channel+"&exclude="+document.MAX_used+"&r="+MAX_r;
    if (window.location) az+="&loc="+escape(window.location);
    if (document.referrer) az+="&referer="+escape(document.referrer);
    az+="'><"+"/script>";
    document.write(az);
  }
}

var searchText = '';

function SearchOnFocus(o) {

	if (searchText == '') {
		
		searchText = o.value;
	}
	
	o.value = '';
}

function SearchOnBlur(o) {

	if (o.value == '') {
	
		o.value = searchText;
	}
}

function ShowObject(e){
	var elem = document.getElementById(e);
	if(elem.style.display == 'block'){
		elem.style.display = 'none';
	}else{
		elem.style.display = 'block';
	}
}

		
	function ExpandOrCollapseComment(o) {
	
        var p = o.parentNode;
		p = p.parentNode;
		p = p.parentNode;
		var children = p.getElementsByTagName("div");	
		var im = o.getElementsByTagName("img")[0];
			
		var doneFirst = false;
		var doneSecond = false;
		
		if (im.src.indexOf("collapse") != -1) {
		
			im.src = '/i/expand.gif';
			for (var i = 0; i < children.length; ++i) {
			    if (!doneFirst && children[i].id.indexOf("commentContent") != -1) {
				    children[i].style.display = 'none';
				    doneFirst = true;
				}
			    if (!doneSecond && children[i].id.indexOf("CommentDetails") != -1) {
				    children[i].style.display = 'inline';
				    doneSecond = true;
				}
			}
			
			
			
		} else {
		
			for (var i = 0; i < children.length; ++i) {
			    im.src = '/i/collapse.gif';
			    if (!doneFirst && children[i].id.indexOf("commentContent") != -1) {
				    children[i].style.display = 'block';				 
				    doneFirst = true;
				}
		        if (!doneSecond && children[i].id.indexOf("CommentDetails") != -1) {
				    children[i].style.display = 'none';
				    doneSecond = true;
				}
			}
			
			
		}
		return false;
	}

	function ExpandOrCollapseThread(o) {
	
		var p = o.parentNode;
		p = p.parentNode;
		p = p.parentNode;
		var children = p.getElementsByTagName("div");
		var doneOnce = false;
		var doneBackground = false;
		
		var im = o.getElementsByTagName("img")[0];
		
		if (im.src.indexOf("collapse") != -1) {
		
			im.src = '/i/expand.jpg';
			for (var i = 0; i < children.length; ++i) {
			    if (!doneOnce && children[i].id.indexOf("ItemChildren") != -1) {
				    children[i].style.display = 'none';
				    doneOnce = true;
				}
			    if (!doneBackground && children[i].id.indexOf("ControlBar") != -1) {
				    children[i].style.backgroundImage = 'url(/i/controlbarbgend.gif)';
				    doneBackground = true;
				}
			}
			p = o.parentNode.parentNode;
			children = p.getElementsByTagName("div");
			for (var i = 0; i < children.length; ++i) {
			    if (children[i].id.indexOf("ctLabel") != -1) {
				    children[i].style.display = 'inline';
				    break;
				}
			}			
			
		} else {
		
			for (var i = 0; i < children.length; ++i) {
			    im.src = '/i/collapse.jpg';
			    if (!doneOnce && children[i].id.indexOf("ItemChildren") != -1) {
				    children[i].style.display = 'block';
				    doneOnce = true;
				}
			    if (!doneBackground && children[i].id.indexOf("ControlBar") != -1) {
				    children[i].style.backgroundImage = 'url(/i/controlbarbg.gif)';
				    doneBackground = true;
				}
			}
			p = o.parentNode.parentNode;
			children = p.getElementsByTagName("div");
			for (var i = 0; i < children.length; ++i) {
			    if (children[i].id.indexOf("ctLabel") != -1) {
				    children[i].style.display = 'none';
				    break;
				}
			}			
			
			
		}
		return false;
	}
		
	function setCookie(name, value, expires, secure)
    {
	    var domain = '';
	    var path = '/';
        document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    }
    
    function getCookie(name)
    {
        var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1)
        {
            begin = dc.indexOf(prefix);
            if (begin != 0) return null;
        }
        else
        {
            begin += 2;
        }
        var end = document.cookie.indexOf(";", begin);
        if (end == -1)
        {
            end = dc.length;
        }
        return unescape(dc.substring(begin + prefix.length, end));
    }
    
   function insertText(element,opentag,closetag){
	var useragent_ver   = parseInt(navigator.appVersion);
	var useragent    = navigator.userAgent.toLowerCase();
	var is_ie     = ((useragent.indexOf("msie") != -1) && (useragent.indexOf("opera") == -1));
	var is_win   = ( (useragent.indexOf("win")!=-1) || (useragent.indexOf("16bit")!=-1) );
	
	var elem = document.getElementById(element);	
	if(useragent_ver >= 4 && is_ie && is_win){
		//IE
		elem.focus();
		var select = document.selection;
		var textrange = select.createRange();
		var htmltag = '';
		textrange.colapse;
		if((select.type == "Text" || select.type == "None") && textrange != null)
		{
			htmltag += opentag + textrange.text + closetag;
			textrange.text = htmltag;
		}
	}else if(elem.selectionEnd){
		// MOZILLA
		var selLength = elem.textLength;
		var selStart = elem.selectionStart;
		var selEnd = elem.selectionEnd;
		var scrollTop = elem.scrollTop;
		
		if (selEnd <= 2)
		{
			selEnd = elem.textLength;
		}
		
		var start  = (elem.value).substring(0, selStart);
		var middle = (elem.value).substring(selStart, selEnd);
		var end    = (elem.value).substring(selEnd, elem.textLength);
		
		if (elem.selectionEnd - elem.selectionStart > 0){
			middle = opentag + middle + closetag;
		}else{
			middle = opentag + middle + closetag;
		}				
		elem.value = start + middle + end;
		
		var cposition = selStart + (middle.length);
		
		elem.selectionStart = cposition;
		elem.selectionEnd   = cposition;
		elem.scrollTop      = scrollTop;
		
	}else{
		// EVERYTHING ELSE
		elem.value += opentag + closetag;
	}
	elem.focus();
}

function quote(field) 
{
var quotet=prompt('Quote Text','Text');
if (quotet)
	{
		insertText(field,'<quote>'+quotet+'</quote>','');
	}
}

function clickButton(e, buttonid){ 
      var bt = document.getElementById(buttonid); 
      if (typeof bt == 'object'){ 
            if(navigator.appName.indexOf("Opera")>(-1)){ 
                  if (e.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } else if(navigator.appName.indexOf("Netscape")>(-1)){ 
                  if (e.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } else if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){ 
                  if (event.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } 
      } 
} 


//AJAX Methods
    function RenderComments(commentid,threshhold,resourceid,expand,save){
	    if(threshhold == 'select'){
            var elem = document.getElementById('ctl00_MainContent_selThreshold');
            threshhold = elem.options[elem.options.selectedIndex].value;
            
            if(save){
                var thisDate = new Date();
	            thisDate.setYear(2040);
	            thisDate.setMonth(0);
	            thisDate.setDate(1);
    	        setCookie("DailyTechCommentThresh",threshhold,thisDate,false);
                var elem2 = document.getElementById('ctl00_MainContent_hdThreshhold');
                elem2.value = threshhold;

    	    }
        }
        var elem2 = document.getElementById('ctl00_MainContent_hdRootComment');
        elem2.value = commentid;
        
        
	    Article.RenderCommentTreeHTML(commentid,threshhold,resourceid,expand,RenderComments_callback);
	    return false;
	}
	
	function RenderComments_callback(res)
    {
       var elem = document.getElementById('CommentTreeDiv');
       elem.innerHTML = res.value;
    }
	
	var id1;
	var link;
	var direction;

    function RateCommentUp(commentid,resourceid,controlId,linkNode) {
        id1 = controlId + '_';    
        link = linkNode;
        Article.RateCommentUp(commentid,resourceid,Rating_callback);
        return false;
    }

    function RateCommentDown(commentid,resourceid,controlId,linkNode) {
        id1 = controlId + '_';
        link = linkNode;
        Article.RateCommentDown(commentid,resourceid,Rating_callback);
        return false;
    }

    function ModerateComment(commentid,resourceid,controlId,linkNode) {
        if (confirm('Are you sure you want to moderate this comment?')) {
            id1 = controlId + '_';
            link = linkNode;
            Article.ModerateComment(commentid,resourceid,Rating_callback);
        }
        return false;
    }

    function SilentModComment(commentid,resourceid,controlId,linkNode) {
        if (confirm('Are you sure you want to silent mod this comment?')) {
            id1 = controlId + '_';
            link = linkNode;
            Article.SilentModComment(commentid,resourceid,Rating_callback);
        }
        return false;
    }

    function RateCommentSix(commentid,resourceid,controlId,linkNode) {
        if (confirm('Are you sure you want to rate this comment as six?')) {
            id1 = controlId + '_';
            link = linkNode;
            Article.RateCommentSix(commentid,resourceid,Rating_callback);
        }
        return false;
    }
    
   	function Rating_callback(res)
    {
        var elem;
    
       if (res.value != -99) {
           elem = document.getElementById(id1 + 'Rating');
           elem.innerHTML = res.value;
           elem = document.getElementById(id1 + 'Rating2');
           elem.innerHTML = res.value;
           elem = document.getElementById(id1 + 'RateResult');
           elem.innerHTML = "Your rating was recorded.";           
       } else {
           elem = document.getElementById(id1 + 'RateResult');
           elem.innerHTML = "You have no votes left for this article.";           
       }
       
       elem = document.getElementById(id1 + 'RateUpLink');
       if (elem != null) {
         elem.style.display='none';
       }

       elem = document.getElementById(id1 + 'RateUpSep');
       if (elem != null) {
         elem.style.display='none';
       }
       
       elem = document.getElementById(id1 + 'RateDownLink');
       if (elem != null) {
         elem.style.display='none';
       }

       elem = document.getElementById(id1 + 'RateDownSep');
       if (elem != null) {
         elem.style.display='none';
       }
       
       elem = document.getElementById(id1 + 'cheader');
       if (elem != null) {
           if (res.value <= -1) {            
                elem.style.backgroundImage = 'url(/i/lowcheaderbg.gif)';
           } else if (res.value == 6) {            
                elem.style.backgroundImage = 'url(/i/sixcheaderbg.gif)';
           } else {
                elem.style.backgroundImage = 'url(/i/cheaderbg.gif)';
           }
       }
       
       /*var p = link.parentNode;
       
   	   var spans = p.getElementsByTagName("span");	
	   var links = p.getElementsByTagName("a");	
	   
			for (var i = 0; i < spans.length; ++i) {
			    if (spans[i].id.indexOf("RateUp") != -1) {
				    spans[i].style.display = 'none';				    
				}
			    if (spans[i].id.indexOf("RateDown") != -1) {
				    spans[i].style.display = 'none';				    
				}
				if (spans[i].id.indexOf("RateResult") != -1) {
				    if (res.value != -99)
				        spans[i].innerHTML = "Your rating was recorded.";
				    else
				        spans[i].innerHTML = "You have no votes left for this article.";
				}
			}
			
			for (var i = 0; i < links.length; ++i) {
			    if (links[i].id.indexOf("RateUp") != -1) {
				    links[i].style.display = 'none';				    
				}
			    if (links[i].id.indexOf("RateDown") != -1) {
				    links[i].style.display = 'none';				    
				}
			}
			*/
			var votes = document.getElementById('VotesRemain');
			if (votes != null) {
			    var voteCount = votes.innerHTML;
			    if (voteCount==null) {
			    voteCount = 0;
			    }
			    voteCount = voteCount - 1;
			    if (voteCount < 0) voteCount = 0;
			    votes.innerHTML = voteCount;
			}

    }
/* OpenSearch Provider */
function judgePopUp(id,type)
{
    var leftval=(screen.width)?(screen.width-650)/2:100;
    var topval=(screen.height)?(screen.height-350)/2:100;
    var popwin=window.open("http://mycroft.mozdev.org/judge.php?id=" + id + "&table=" + type, "JudgePopup", "width=650,height=300,top=" + topval + ",left=" + leftval + ",toolbar=0,scrollbars=no,directories=no,location=0,statusbar=0,menubar=0,resizable=0");
    popwin.focus();
}

function addEngine(name,ext,cat,pid)
{
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function")) {
    window.sidebar.addSearchEngine(
      "http://mycroft.mozdev.org/install.php/" + pid + "/" + name + ".src",
      "http://mycroft.mozdev.org/install.php/" + pid + "/" + name + "."+ ext, name, cat );
  } else {
    alert("You will need a browser which supports Sherlock to install this plugin.");
  }
}

function addOpenSearch(name,ext,cat,pid,meth)
{
  if ((typeof window.external == "object") && ((typeof window.external.AddSearchProvider == "unknown") || (typeof window.external.AddSearchProvider == "function"))) {
    if ((typeof window.external.AddSearchProvider == "unknown") && meth == "p") {
      alert("This plugin uses POST which is not currently supported by Internet Explorer's implementation of OpenSearch.");
    } else {
      window.external.AddSearchProvider(
        "http://mycroft.mozdev.org/installos.php/" + pid + "/" + name + ".xml");
    }
  } else {
    alert("You will need a browser which supports OpenSearch to install this plugin.");
  }
} 