var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttp=false
  }
 }
@else
 xmlhttp=false
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false
 }
}

diggburylock = 0;

function myXMLHttpRequest() {
  var xmlhttplocal;
  try {
    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
    xmlhttplocal=false;
  }
 }
 if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
  try {
   var xmlhttplocal = new XMLHttpRequest();
  } catch (e) {
   var xmlhttplocal=false;
   alert('couldn\'t create xmlhttp object');
  }
 }
 return(xmlhttplocal);
}

var hex = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
var i, j, k;
k = 0;
for (i = 0; i < 16; i++) {
    for (j = 0; j < 16; j++) {
       rgb[k] = hex[i] + hex[j];
       k++;
   }
}

function rgb(r, g, b) {
   return('#'+rgb[r]+rgb[g]+rgb[b]);
}

var ColorIn= new Array();
for (i = 1; i <= 5; i++) {
  ColorIn[i] = new Array();
}

ColorIn[1][1] = rgb(248, 242, 198);
ColorIn[1][2] = rgb(234, 227, 181);
ColorIn[1][3] = rgb(220, 212, 164);
ColorIn[1][4] = rgb(206, 195, 147);
ColorIn[1][5] = rgb(192, 180, 130);
ColorIn[1][6] = rgb(178, 165, 113);
ColorIn[1][7] = "#A39A5D";

ColorIn[2][1] = rgb(247, 236, 172);
ColorIn[2][2] = rgb(231, 220, 158);
ColorIn[2][3] = rgb(215, 204, 140);
ColorIn[2][4] = rgb(199, 188, 122);
ColorIn[2][5] = rgb(183, 172, 104);
ColorIn[2][6] = rgb(167, 156, 86);
ColorIn[2][7] = "#998D43"; 

ColorIn[3][1] = rgb(247, 236, 158);
ColorIn[3][2] = rgb(229, 218, 140);
ColorIn[3][3] = rgb(211, 200, 122);
ColorIn[3][4] = rgb(193, 182, 104);
ColorIn[3][5] = rgb(175, 164, 86);
ColorIn[3][6] = rgb(157, 146, 68);
ColorIn[3][7] = "#897D2F";

ColorIn[4][1] = rgb(247, 232, 137);
ColorIn[4][2] = rgb(225, 210, 119);
ColorIn[4][3] = rgb(203, 188, 101);
ColorIn[4][4] = rgb(181, 166, 83);
ColorIn[4][5] = rgb(159, 144, 65);
ColorIn[4][6] = rgb(137, 122, 47);
ColorIn[4][7] = "#73681F"; 

ColorIn[5][1] = "#F6E26B";
ColorIn[5][2] = "#DAC85B";
ColorIn[5][3] = "#BEAE4B";
ColorIn[5][4] = "#A2943B";
ColorIn[5][5] = "#867A2B";
ColorIn[5][6] = "#6A6019";
ColorIn[5][7] = "#50470D";

ColorSpy = new Array();
ColorSpy[1] = "#FFFFFF";
ColorSpy[2] = "#F1F3F5";
ColorSpy[3] = "#EBEEF1";
ColorSpy[4] = "#DEE2E7";
ColorSpy[5] = "#CCD5DB";
ColorSpy[6] = "#BCC7CF";
ColorSpy[7] = "#ACB9C4";
var timeid = new Array();
var responsestring = new Array();
var myxmlhttp = new Array()
var responseString = new String;

function checkIE() {
	var useragent = navigator.userAgent;
	var pos = useragent.indexOf('MSIE');
	if (pos > -1) {
		return true;
	} else {
		return false;
	}
}

function flash(id) {
  timeid[id] = window.setTimeout("fadeInSpy(7, '"+id+"')",100);
}

function alreadydug() {
  alert("You have already dug this item");
}

function alreadydugi() {
  alert("You have already dug this story (or someone at the same internet address has)\nYou can try logging in and digging then.");
}

function myclearTimeout(id) {
  clearTimeout(timeid[id]);
} 

var digging = -1;
function wrapper_full (id, shade, itemd, digcheck) {
  $('diglink'+id).innerHTML = '<a href="javascript:void(0)">digg it</a>';
  if (digging == id) {
    alert('You have already dugg this story');
    return;
  } else {
    digging = id;
  }
  digjfull(id, itemd, digcheck);
}

function digjfull(row, id, digcheck) {
  url = "/diginfull";
  content="id="+id+"&row="+row+"&digcheck="+digcheck;
  myxmlhttp[row] = new myXMLHttpRequest();
  if (myxmlhttp[row]) {
	myxmlhttp[row].open("POST",url,true);
	myxmlhttp[row].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 	myxmlhttp[row].onreadystatechange=function() {
		if (myxmlhttp[row].readyState==4) {
   		 	responsestring[row] = myxmlhttp[row].responseText;
			if (responsestring[row].substr(0,6) == 'ERROR:') {
				alerttext = responsestring[row].replace(/ERROR:/, '');
				alert(alerttext);
			} else if (responsestring[row].substr(0,5) !== '3~--~') {
				// Do nothing, this is caused 99% of the time by a user abort.
			} else {
				Effect.FadeKeepSpace('diggs-strong-'+row,
									 { duration: 1, afterFinish: function() {up_diggs(row)}} );
			}
	  	}
	}
 	myxmlhttp[row].send(content);
  }

  return false;
}

function up_diggs(row) {
	split = new RegExp("~--~");
	var b = responsestring[row].split(split);
	var target1 = $('diggs-strong-'+row);
	// Update if bigger
	if (target1.innerHTML < b[1]) {
		target1.innerHTML = b[1];
	} else {
		target1.innerHTML = parseInt(target1.innerHTML) + 1;
	}
	Effect.AppearKeepSpace('diggs-strong-'+row, {duration: 1});
	var target2 = $('diglink'+row);
	target2.innerHTML = "<span>dugg!</span>";
	// make #1 link
	var t = $('enclosure'+row);
	var h3s = t.getElementsByTagName('h3');
	h3s[0].innerHTML += ' <a id="myn' + row +
'" class="news-label" href="javascript:addn1(' + b[3] + ',' + row + ",'" + b[4] + "')" + 
'"><img src="/img/myone-add.gif" alt="Add as My Number One" width="50" height="16" /></a>';
}

function fadeInSpy(where, id) {
  if (where >= 1) {
    $('main'+id).style.backgroundColor = ColorSpy[where];
    if (where > 1) {
      where -= 1;
      timeid[id] = window.setTimeout("fadeInSpy("+where+",'"+id+"')", 100);
    } else {
      where -= 1;
      $('main'+id).style.backgroundColor = "transparent";
      myclearTimeout(id);
    }
  }
}

function openSpellChecker() {
  // get the textarea we're going to check
  var text1 = $('title');
  var textarea1 = $('bodytext');
  var speller = new spellChecker( text1, textarea1 );
  speller.openChecker();
}

function openSpellCheckerComment() {
  // get the textarea we're going to check
  var textarea1 = $('comment');
  var speller = new spellChecker( textarea1 );
  speller.openChecker();
}

function confirmblock(username) {
  return(confirm('Are you sure you want to block user '+username+' ?'));
}

function rjp(id, code, row) {
  HideandUNhideObj(row);
  url="/reportj?id="+id+"&code="+code;
  xmlhttp.open("GET",url,true);
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
      $('enclosure'+row).className += ' news-buried';
	  // Need to ensure undiggs look right when buried:
      $('diglink'+row).className = 'digg-it';
      $('diglink'+row).innerHTML = '<span>buried</span>';
    }
  }
  xmlhttp.send(null);
}

function updaterating(frm,commentid,commentdiv,tolerance) {
  url="/setratingj/"+commentid+"/"+frm.ratings.value;
  xmlhttp.open("GET",url,true);
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
      score = xmlhttp.responseText;
      $('scorediv'+commentid).innerHTML = score;
      if (score < tolerance) {
	thediv = $(commentdiv); 
	thediv.innerHTML = '';
	frm.ratings.selectedIndex=xmlhttp.responseText;
      }
    }
  }
  //  xmlhttp.setRequestHeader('Accept','message/x-formresult');
  xmlhttp.send(null);
  return false;
}

function showCaptcha(name) {
  var target = $('commentcaptcha');
  target.onclick = null;
  target.style.display = 'block';
  if (!(target.innerHTML.length)) {	
    $('submitbutton').disabled = true;
    url = '/get_captcha?keyname='+name;
    xmlhttp.open("GET",url,true);
    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
	target.innerHTML = xmlhttp.responseText;
	$('submitbutton').disabled = false;
      }
    }
    xmlhttp.send(null);
  } 
}

function enablebutton(button, button2, target) {
  var string = target.value; 
  button2.disabled = false;
  if (string.length > 0) {
    button.disabled = false;
  } else {
    button.disabled = true;
  }
  var target2 = $('commentcaptcha');
}

function startlastdigs() {
  window.setInterval("lastdigs()",10000);
}

function startlastdigs_up() {
  window.setInterval("lastdigs_up()",10000);
}

function filldigs() {
  var s;
  var url2 = 'last10';
  s = new myXMLHttpRequest();
  s.open("GET",url2,true);
  s.onreadystatechange=function() {
    if (s.readyState == 4) {
      responsestring2 = s.responseText;	
      split = new RegExp("!#-!");
      b = responsestring2.split(split);
      split2 = new RegExp("~");
      c = b[1].split(split2);
      $('diggspy').innerHTML = b[0];
      do_anim();
    }
  }
  s.send(null);
  delete s;
  return;
}

function filldigs_up() {
  var s;
  var url2 = 'last10digg';
  s = new myXMLHttpRequest();
  s.open("GET",url2,true);
  s.onreadystatechange=function() {
    if (s.readyState == 4) {
      responsestring2 = s.responseText;	
      split = new RegExp("!#-!");
      b = responsestring2.split(split);
      split2 = new RegExp("~");
      c = b[1].split(split2);
      $('diggspy').innerHTML = b[0];
      do_anim();
    }
  }
  s.send(null);
  delete s;
  return;
}

function do_anim() {
    // find all items in c not in ids
    var notdone = 1;
    var i = 0;
    var j = 0;
    while (notdone) {   
       if (c[i] != ids[j]) {
            flash(c[i]); 
            i++;
            if (i > c.length) {
                notdone = 0;
            }
       } else {
            notdone = 0;
       }
    }
    for (i = 0; i < c.length; i++) {
       ids[i] = c[i];
    }
}

function lastdigs() {
  if (!spypause) {
    filldigs();
  }
}

function lastdigs_up() {
  if (!spypause_up) {
    filldigs_up();
  }
}

function checkit(form) {
  var name = escape(form.username.value);
  url = 'checkusername?username='+name;
  checkitxmlhttp = new myXMLHttpRequest();
  checkitxmlhttp.open("GET",url,true);
  checkitxmlhttp.onreadystatechange=function() {
    if (checkitxmlhttp.readyState==4) {
      responsestring = checkitxmlhttp.responseText;
      if (responsestring == 'taken'|| responsestring == 'not acceptable - user names must have 4-15 characters, and only alphanumeric characters.') {
	$('checkitvalue').innerHTML = '<strong style="background: url(/img/deny.png) 0 1px no-repeat; padding-left: 16px; font-size: 85%; color: #c00;">Sorry, the username "'+name+'" is '+responsestring+'.</strong>';
	form.submit.disabled = 'disabled';
      } else {
	$('checkitvalue').innerHTML = '<strong style="background: url(/img/confirm.png) 0 1px no-repeat; padding-left: 16px; font-size: 85%; color: #090;">Great, the username "'+name+'" is '+responsestring+'.</strong>';
	form.submit.disabled = '';
      }
    }
  }
  //  xmlhttp.setRequestHeader('Accept','message/x-formresult');
  checkitxmlhttp.send(null);
  return false;  
}

function blankcontent(id) {
  hovercontent(id);
  content = '<div id=\'popup\'></div>';
  return(content);
}

function hovercontent (id) {
  url = '/hovercontent?id='+id;
  hoverxmlhttp = new myXMLHttpRequest();
  hoverxmlhttp.open("GET",url,true);
  hoverxmlhttp.onreadystatechange=function() {
    if (hoverxmlhttp.readyState==4) {
      responsestring = hoverxmlhttp.responseText;
      $('popup').innerHTML = responsestring;
    }
  }
  //  xmlhttp.setRequestHeader('Accept','message/x-formresult');
  hoverxmlhttp.send(null);
  return('');
}

function fadeInIncrement(target, count) {
  $(target).innerHTML = count;
  Effect.AppearKeepSpace(target, { duration: 1} );
}

function getdpage(itemid, page, friends) {
  url="/newdiggers?id="+itemid+"&page="+page+"&friends="+friends;
  xmlhttp.open("GET",url,true);
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
	target = $('diggers');
	target.innerHTML = xmlhttp.responseText;	
    }
  }
  xmlhttp.send(null);
  return false;
}

// For dismissing messages
function dismiss(id, cook, what) {
    SetCookie('dismiss-' + cook, what, 365);
    $(id).style.display = 'none';
    return false;
}

// For setting default homepage
function sethome(what) {
	xh = new myXMLHttpRequest();
	xh.open("POST", "/setdefhome", true);
	xh.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xh.onreadystatechange = function() {
		if (xh.readyState == 4) {
			if (xh.responseText == 'OK') {
				var t = $('sethome');
				t.className = "tool set-off";
				t.innerHTML = 'Digg Home!';
			}
		}
	}
	xh.send('home='+what);
	return false;
}

// Make a link be different
function diffclick() {
	window.location.href = mydiffref;
	return false;
}

// My#1 add
function addn1(story, which, check) {
	addxh = new myXMLHttpRequest();
	addxh.open("POST", "/addn1", true);
	addxh.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	addxh.onreadystatechange = function() {
	if (addxh.readyState == 4) {
			if (addxh.responseText == 'OK') {
				// Clear them all:
				var i=0;
				var t='';
				while (t = $('myn'+(i++))) {
					t.innerHTML = '<img src="/img/myone-add.gif" alt="Add as My Number One" width="50" height="16" />';
					t.onclick = null;
				}
				t = $('myn'+which);
				t.innerHTML = '<img src="/img/myone.gif" alt="Saved as My #1" width="50" height="16" />';
				t.onclick = diffclick;
			}
		}
	}
	addxh.send('item='+story+'&check='+check);
}

// Undigging
allowun = true;
function undig(story, check) {
	if (allowun) {
		if (confirm('Are you sure you wish to undigg this story?\nYou will not be able to digg it again in the future.')) {
			allowun = false;
			var newform = '<form id="newundigg" action="/undig" method="post"><input type="hidden" name="item" value="' + story + '" /><input type="hidden" name="check" value="' + check + '" /></form>';
			var tag = $('container');
			tag.innerHTML += newform;
			$('newundigg').submit();
		}
	}
}

// Removing a My#1
function unmy1(story, check) {
	if (allowun) {
		allowun = false;
		var newform = '<form id="newunmy1" action="/unmy1" method="post"><input type="hidden" name="item" value="' + story + '" /><input type="hidden" name="check" value="' + check + '" /></form>';
		var tag = $('container');
		tag.innerHTML += newform;
		$('newunmy1').submit();
	}
}

// Needed for IE flash ads - Don't Remove
function phpads_deliverActiveX(content) {
	document.write(content);	
}