/*<![CDATA[*/
/******************
THIS CODE WAS WRITTEN BY NIALL KADER ON 11/1/2007
*******************/

var posX = 0;
var posY = 0;
var IE = document.all?true:false;

if (!IE){
	//window.captureEvents(Event.CLICK);
	window.onmousedown=getXY;
}
else
{
	document.onmousedown=getXY;
}

function getXY(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        cursor.x = e.clientX + 
            (document.documentElement.scrollLeft || 
            document.body.scrollLeft) - 
            document.documentElement.clientLeft;
        cursor.y = e.clientY + 
            (document.documentElement.scrollTop || 
            document.body.scrollTop) - 
            document.documentElement.clientTop;
    }
    posX = cursor.x;
    posY = cursor.y;
}

function createXMLObj()//pass in varName for the variable reference you want to use for this instance of the object
{
	varName = false;
	try
	{
		varName = new ActiveXObject("MSXML2.XMLHTTP");
	}
	catch(exception1)
	{
		try
		{
			varName = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(exception2)
		{
			varName = false
		}
	}

	if(!varName && window.XMLHttpRequest)
	{
		varName = new XMLHttpRequest();
	}
	return varName;
}

function getCommentForm(article_id,form_desc,show_close)
{
	var xmlObj = createXMLObj();
	var dataSource = app_root + "/comment_form.php?article_id=" + article_id;
	
	if(form_desc)//form_desc will show up as the description of the form ex)'How would you rate this article?'
	{
		dataSource += "&form_desc="+encodeURI(form_desc);
	}
	if(show_close)//if this is set to 'yes' then the 'close' link will show up - it fires the hide(function)
	{
		dataSource += "&show_close="+encodeURI(show_close);	
	}
	var divID = "comment_container";
	
	if(xmlObj)//this checks to make sure that the xmlhttp object has been instanciated properly before carrying out the rest of the function
	{
		var obj = document.getElementById(divID);
		dataSource +="&t=" + new Date().getTime();

		//alert(dataSource);
		xmlObj.open("GET", dataSource, true);
		xmlObj.onreadystatechange = function()
		{
			if(xmlObj.readyState == 4 && xmlObj.status == 200)
			{
				//alert("readystatereached");
				var txt = xmlObj.responseText;
				obj.innerHTML = txt;
				//alert(txt);
				//this line works on the first page load, but not when I try to refresh the calendar
			}
			else
			{
				//alert(xmlObj.readyState + "  " + xmlObj.status);
			}
		}
		xmlObj.send(null);
	}
	else
	{
		alert("xmlObj is false!");
	}
	showBox(divID);
}


function showBox(divID,offsetX,offsetY)
{
	var box = document.getElementById(divID);
	//var e = new mouseEvent(e);
	//alert("showInfoBox");
	var xOffset = (offsetX ? offsetX : 40);
	var yOffset = (offsetY ? offsetY : 40);
	
	box.style.left = (posX-xOffset) +"px";
	box.style.top = (posY-yOffset) + "px";;
	box.style.visibility="visible";
	box.style.display="block";
	//box.innerHTML = "<div style='text-align:right'><a href='javascript:hideInfoBox()' style='color:white;text-decoration:none;background:#C85E35;border:1px solid white;'>&nbsp;&nbsp;&nbsp;&nbsp;close&nbsp;&nbsp;&nbsp;&nbsp;</a>&nbsp;&nbsp;</div><b>"+dateParam + "</b><br /><br />The information here could be retrieved from a database.<br /><br /> Or you could allow the user to upload information to the server.  <br /><br />Note that in either case, there is no page refresh.<br />";
}

function hideBox(divID){
	var box = document.getElementById(divID);
	box.style.display="none";
	box.style.visibility="hidden";
}

function validateCommentPost()
{
	if((document.form1.upfile.value == "") && (document.form1.comment_desc.value == ""))
	{
		alert("You must either upload an image or enter a comment.");
		document.form1.upfile.focus();
		return false;
	}
	return true;
}
function linkImgToSponsor(img_id,img_link){
	//alert(img_id + "\n" + img_link);
	var xmlObj = createXMLObj();
	var dataSource = app_root + "/ajax_tracker.php?img_id=" + img_id + "&img_link=" + img_link;
	
	//alert(dataSource);
	//return;
				
	if(xmlObj)//this checks to make sure that the xmlhttp object has been instanciated properly before carrying out the rest of the function
	{
		dataSource +="&t=" + new Date().getTime();
		//alert(dataSource);
		xmlObj.open("GET", dataSource, true);
		xmlObj.onreadystatechange = function()
		{
			if(xmlObj.readyState == 4 && xmlObj.status == 200)
			{
				var txt = xmlObj.responseText;
				if(txt == "success"){
					alert("success log of this click to: " + img_link);	
				}else{
					alert("log ad click failed");	
				}
			}
			else
			{
				//alert(xmlObj.readyState + "  " + xmlObj.status);
			}
		}
		xmlObj.send(null);
	}
	else
	{
		//alert("Your browser does not support AJAX.");
	}
	//for testing purposes, when live remember to uncomment the line below and remove the alerts above...
	//location.href = img_link;

}
/*]]>*/
