
function newAjaxReq(){
	if(window.XMLHttpRequest && !(window.ActiveXObject)) { // branch for native XMLHttpRequest object;
		try {
			req = new XMLHttpRequest();
	        }catch(e){
			req = false;
	        }
	}else if(window.ActiveXObject){ // branch for IE/Windows ActiveX version
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
	      	}catch(e){
	       		try {
	          		req = new ActiveXObject("Microsoft.XMLHTTP");
	        	} catch(e) {
	          		req = false;
	        	}
		}
	}
	return req;
}

var commentsys_req=null
var commentsys_targetDiv=null;
var commentsys_topic=null;
var commentsys_tuid=null;
var commentsys_guid=null;
var commentsys_cookie_domain='';
var height=null;
var g_divHeightBefore=null;
function replies(parent,count){
	commentsys_count=count;
	commentsys_targetDiv="commentsys_replies"+parent;
	document.getElementById(commentsys_targetDiv).innerHTML='<img src="http://admin.dotnology.co.uk/comments/images/ajax-loader.gif" alt="Loading..." />';
	request(parent);
}
function request(parent){
	commentsys_req=newAjaxReq();
	commentsys_req.open("GET", "/comments/comments.ajax.php?guid="+encodeURIComponent(commentsys_guid)+"&tuid="+encodeURIComponent(commentsys_tuid)+"&parent="+parent+"&topic="+encodeURIComponent(commentsys_topic), true); 
	commentsys_req.onreadystatechange = gotReplies;
	commentsys_req.send(null);
}
function gotReplies(){
	if(commentsys_req.readyState == 4){
		//alert(req.responseText);
		
		var hide="";
		if(commentsys_targetDiv!="commentsys_comments")
			hide="<a href=\"javascript:hide('"+commentsys_targetDiv+"',"+commentsys_count+")\">Hide "+((commentsys_count==1)?"reply":"replies")+"</a>";
		document.getElementById(commentsys_targetDiv).innerHTML=hide+commentsys_req.responseText;

		getHeight();
		publishHeight();
		
		// TODO this works fine in IE 6 & 7, but not in Gecko or KHTML :/
		//document.getElementById(commentsys_targetDiv).innerHTML=commentsys_req.responseText;
		//publishHeight();
	}
}
function commentsys(guid,tuid,topic,new_comment_text,cookie_domain){
	if(!new_comment_text)
		new_comment_text="<strong>Leave a new comment &raquo;</strong>";
	document.getElementById("commentsys").innerHTML="<a href=\"javascript:replyForm('commentsys_post','0');\">"+new_comment_text+"</a><div id=\"commentsys_post\"></div><div id=\"commentsys_comments\"></div>";
	//replyForm("commentsys_post","0");
	commentsys_tuid=tuid;
	commentsys_guid=guid;
	commentsys_targetDiv="commentsys_comments";
	commentsys_topic=topic;
	commentsys_cookie_domain=cookie_domain;
	request(0);
}
function submit(target,parent){
	var comment=document.getElementById("commentsys_field"+parent).value;
	var name=document.getElementById("commentsys_name"+parent).value;
	var email=document.getElementById("commentsys_email"+parent).value;
	if(comment && name && email.match(new RegExp("@.*\\."))){
		setCookie('commentsys_name',name,3650,'/',commentsys_cookie_domain);
		setCookie('commentsys_email',email,3650,'/',commentsys_cookie_domain);
		var ele=document.getElementById(target);
		g_divHeightBefore=ele.clientHeight;

		document.getElementById("commentsys_submit"+parent).value="Please wait...";
		document.getElementById("commentsys_submit"+parent).disabled=true;
		commentsys_targetDiv=target;
		commentsys_req=newAjaxReq();
		commentsys_req.open("GET", "/comments/comments.ajax.php?url="+encodeURIComponent(window.location)+"&tuid="+encodeURIComponent(commentsys_tuid)+"&parent="+parent+"&comment="+encodeURIComponent(comment)+"&guid="+encodeURIComponent(commentsys_guid)+"&topic="+encodeURIComponent(commentsys_topic)+"&name="+encodeURIComponent(name)+"&email="+encodeURIComponent(email), true); 	
		commentsys_req.onreadystatechange = submitted;
		commentsys_req.send(null);
	}else{
		alert("All fields are rquired.");
	}
}
function submitted(){
	if(commentsys_req.readyState == 4){
		document.getElementById(commentsys_targetDiv).innerHTML=commentsys_req.responseText;
		var ele=document.getElementById(commentsys_targetDiv);
		var divHeightAfter=ele.clientHeight;
		var heightDiff=g_divHeightBefore-divHeightAfter;
		if(heightDiff==0)
			getHeight();
		else
			height=height-heightDiff;
		publishHeight();
	}
}
function replyForm(target,parent){
	document.getElementById(target).innerHTML="<input id=\"commentsys_name"+parent+"\" type=\"text\" value=\""+getCookie('commentsys_name')+"\" class=\"commentsys\"/> <span class=\"commentsys_label\">Display Name (required) (published)</span><br/><input id=\"commentsys_email"+parent+"\" type=\"text\" value=\""+getCookie('commentsys_email')+"\" class=\"commentsys\"/> <span class=\"commentsys_label\">E-Mail (required) (not published)</span><br/><textarea class=\"commentsys\" id=\"commentsys_field"+parent+"\" onfocus=\"fix_textarea_size(this);\"></textarea><br/><input type=\"button\" value=\"Submit Comment\" id=\"commentsys_submit"+parent+"\" onClick=\"javascript:submit('"+target+"','"+parent+"');\" />"+((parent!="0")?" <input type=\"button\" value=\"Discard\" id=\"commentsys_discard"+parent+"\" onClick=\"javascript:discardForm('"+target+"','"+parent+"');\" />":"");
	if(parent!="0") {
		document.getElementById("commentsys_replylink"+parent).style.display="none";
	}
	getHeight();
	publishHeight();
}
function report(target,parent){
	commentsys_targetDiv=target;
	commentsys_req=newAjaxReq();
	commentsys_req.open("GET", "/comments/comments.ajax.php?report="+parent, true); 	
	commentsys_req.onreadystatechange = submitted;
	commentsys_req.send(null);
	getHeight();
	publishHeight();
}

function discardForm(target,parent){
	var ele=document.getElementById(target);
	var divHeightBefore=ele.clientHeight;
	ele.innerHTML="";
	if(parent!="0") {
		document.getElementById("commentsys_replylink"+parent).style.display="";
	}
	var divHeightAfter=ele.clientHeight;
	var heightDiff=divHeightBefore-divHeightAfter;
	if(heightDiff==0)
		getHeight();
	else
		height=height-heightDiff;
	publishHeight();
}
function hide(target,count){
	id=target.replace("commentsys_replies","");
	var ele=document.getElementById(target);
	var divHeightBefore=ele.clientHeight;
	ele.innerHTML="<a href=\"javascript:replies('"+id+"',"+commentsys_count+")\">View "+commentsys_count+" "+((commentsys_count==1)?"reply":"replies")+" &raquo;</a>";
	var divHeightAfter=ele.clientHeight;
	
	var heightDiff=divHeightBefore-divHeightAfter;
	if(heightDiff==0)
		getHeight();
	else
		height=height-heightDiff;
	publishHeight();
}
function getHeight() {
	var actualHeight = getBodyHeight();
    var currentHeight = getViewPortHeight();
    
   // if(Math.abs(actualHeight - currentHeight) > 15) {
	height=actualHeight;
	//alert(height);
	//}
}
function publishHeight(){
	if( typeof(hosturl)!="undefined" ){
		window.parent.location = "http://"+hosturl+"#"+height.toString();
	}
}
function fix_textarea_size(obj) {
	obj.style.width=obj.clientWidth;
}

function getBodyHeight()
{
    var height;
    
    var scrollHeight;
    var offsetHeight;

    if (document.height) {
        height = document.height;
    } else if(document.body) {
        if(document.body.scrollHeight) {
            height = scrollHeight = document.body.scrollHeight;
        } else if (document.body.offsetHeight) {
            height = offsetHeight = document.body.offsetHeight;
        }
    }
    return height;
}

function getViewPortHeight()
{
    var height = 0;

    if (window.innerHeight)
    {
        height = window.innerHeight - 18;
    }
    else if ((document.documentElement) && (document.documentElement.clientHeight))
    {
        height = document.documentElement.clientHeight;
    }
    else if ((document.body) && (document.body.clientHeight))
    {
        height = document.body.clientHeight;
    }

    return height;
}

//cookie code//
function setCookie(name, value, days, path, domain, secure) {

	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = date.toGMTString();
	}

  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or ''
  if cookie does not exist
*/

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 '';
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
////////////////////

