var commentIds = new Array();
var commListed = false;
var cid = 0;
var foldCont = 1;
var openCont = 2;
var checked = 3;



function sajax_init_object() { 			
  var A;
  try {
    A=new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      A=new ActiveXObject("Microsoft.XMLHTTP");
    } catch (oc) {
      A=null;
    }
  }
  if(!A && typeof XMLHttpRequest != "undefined")
    A = new XMLHttpRequest();
  if (!A)
    sajax_debug("Could not create connection object.");
  return A;
}


function fetchComment(uriPrefix, cid) {
	var i, x, n;
	var uri = uriPrefix + '/' + cid + '#' +Math.random();
	var post_data;
	var sajax_request_type = "GET";
	x = sajax_init_object();
	x.open(sajax_request_type, uri, true);
	x.onreadystatechange = function() {
		if (x.readyState != 4) 
			return;
	
		var status;
		var data;
		status = x.responseText.charAt(0);
		data = x.responseText;
		if (status == "-") 
			alert("Error: " + data);
		else  
			writeComment(cid, data);
			
	}
	x.send(post_data);
	delete x;
}

function writeComment ( cid, data )
{
var ai;
var divId = 'comment_' + cid;
	commDiv = document.getElementById (divId);
	commDiv.innerHTML = data;
//	comments["cid"]["open"] = data;
}

function openComment(uriPrefix, cid) {
if ( !commListed )
{
listComments(0);
}
	var ai;
	//ai = cid + ",open"
	ai = cid;
		tt_Hide();	
	if ( !comments[ai] )
	{
		var comm = new Array();
		comm[0] = cid;
		var divId = 'comment_' + cid;
		commDiv = document.getElementById (divId);


//		ai = cid + ",fold";comments[ai] = commDiv.innerHTML;
		comm[1] = commDiv.innerHTML;
//		ai = cid + ",open";comments[ai] = "";
		comm[2] = "";

		fetchComment(uriPrefix, cid);
	}
	else if ( comments[cid][2] == "" )
	{
		var divId = 'comment_' + cid;
		commDiv = document.getElementById (divId);
		comments[cid][1] = commDiv.innerHTML;
	fetchComment(uriPrefix, cid);
	}
	else
	{
//		ai = cid + ",open";
		ai = cid;
		//writeComment(cid, comments[ai]);
		writeComment(cid, comments[cid][2]);
	}
}

function foldComment(cid)
{
var ai;
var divId = 'comment_' + cid;
	commDiv = document.getElementById (divId);
	
//ai = cid + ",open";	
ai = cid;
comments[ai][2] = commDiv.innerHTML;

//ai = cid + ",fold";
ai = cid;
commDiv.innerHTML = comments[ai][1];
}

function ajaxLoaded ()
{
alert ( 'ajax loaded');
}
function listComments(choice)
{
  var inpArr = document.getElementsByTagName('input');
  var inpArrLn = inpArr.length;
  for ( i=0; i< inpArrLn; i++ )
  {
    var cb = inpArr[i];
    if (cb.id.indexOf('cb_') != 0 )
    {
      continue;
    }
	var checked = false;
    if ( choice == 0 || cb.checked )
    {
	checked = true;
    }
    var tcid = cb.id.substring( 3);
commentIds[commentIds.length]=tcid;
	comments[tcid] = new Array ( tcid, "", "", checked );
  }
	commListed = true;
}

function openCommentAll ( uriPrefix, choice )
{
//if ( !commListed )
{
listComments(choice);
}
/*
  var inpArr = document.getElementsByTagName('input');
  var inpArrLn = inpArr.length;
  for ( i=0; i< inpArrLn; i++ )
  {
    var cb = inpArr[i];
    alert ( cb.outerHTML );
    if (cb.id.indexOf('cb_') != 0 )
    {
      continue;
    }
    var tcid = cb.id.substring( 3);
    alert ( tcid );
    if ( choice == 0 || cb.checked )
    {
      openComment ( uriPrefix, tcid);
    }
  }
*/
  var commArrLn = commentIds.length;
  for ( i=0; i< commArrLn; i++ )
  {
	var cid = commentIds[i];
	if ( (comments[cid])[3] )
	{
		var tcid = (comments[cid])[0];
      		openComment ( uriPrefix, tcid);
	}
  }
}

function foldCommentAll ()
{
var inpArr = document.getElementsByTagName('div');
var inpArrLn = inpArr.length;
for ( i=0; i< inpArrLn; i++ )
{
	var cb = inpArr[i];
	if (!cb.id || cb.id.indexOf('comment_') != 0 )
	{
		continue;
	}
	var tcid = cb.id.substring( 8);
	foldComment ( tcid);
}
}

function commObj ( id, foldCont, openCont )
{
alert ( 'in commObj');
this.id = id;
this.foldCont = foldCont;
this.openCont = openCont;
}

function tt_Init_links()
{
  if (!(tt_op || tt_n4 || tt_n6 || tt_ie || tt_w3c)) return;

  var htm = tt_n4? '<div style="position:absolute;"><\/div>' : '',
      tags,
      t_tj,
      over,
      esc = 'return escape(';
	  var i = tt_tags.length; while (i--)
	  {
	  tags = tt_ie? (document.all.tags(tt_tags[i]) || 1)
	  : document.getElementsByTagName? (document.getElementsByTagName(tt_tags[i]) || 1)
	  : (!tt_n4 && tt_tags[i]=='a')? document.links
	  : 1;
	  if (tt_n4 && (tt_tags[i] == 'a' || tt_tags[i] == 'layer')) tags = tt_N4Tags(tt_tags[i]);
	  var j = tags.length; while (j--)
	  {
	  if (typeof (t_tj = tags[j]).onmouseover == 'function' && t_tj.onmouseover.toString().indexOf(esc) != -1 && !tt_n6 || tt_n6 && (over = t_tj.getAttribute('onmouseover')) && over.indexOf(esc) != -1)
	  {
	  if ( t_tj.onmouseover.toString().indexOf ('tt_Show') == -1 )
	  {
	  if (over) t_tj.onmouseover = new Function(over);
	  var txt = unescape(t_tj.onmouseover());
	  htm += tt_Htm(
	    t_tj,
	    'tOoLtIp'+i+''+j,
	    txt.wzReplace('& ','&')
	    );

	  t_tj.onmouseover = new Function('e',
	      'tt_Show(e,'+
		'"tOoLtIp' +i+''+j+ '",'+
		(typeof t_tj.T_ABOVE != tt_u) + ','+
		((typeof t_tj.T_DELAY != tt_u)? t_tj.T_DELAY : ttDelay) + ','+
		((typeof t_tj.T_FIX != tt_u)? '"'+t_tj.T_FIX+'"' : '""') + ','+
		(typeof t_tj.T_LEFT != tt_u) + ','+
		((typeof t_tj.T_OFFSETX != tt_u)? t_tj.T_OFFSETX : ttOffsetX) + ','+
		((typeof t_tj.T_OFFSETY != tt_u)? t_tj.T_OFFSETY : ttOffsetY) + ','+
		(typeof t_tj.T_STATIC != tt_u) + ','+
		(typeof t_tj.T_STICKY != tt_u) +
		');'
	      );
	  t_tj.onmouseout = tt_Hide;
	  if (t_tj.alt) t_tj.alt = "";
	  if (t_tj.title) t_tj.title = "";
	  }
	  }
	  }
	  }
  document.write(htm);
}

//alert ( comments.length);
