//-------------------------Drag Div-------------------------

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}


//--------------------------------------------------

function showLogin(linkname)
{
	var obj				= document.getElementById("loginfrm");

	var cord			= getAnchorPosition(linkname);
	//alert("X :"+cord.x +" Y : "+cord.y); return false;
	//cord.y				= cord.y - 122;
	//cord.x				= cord.x + 24;
	obj.style.top		= eval("'"+cord.y + "px'"); 
	obj.style.left		= eval("'"+cord.x + "px'"); 

	obj.style.width		= "220px";
	obj.style.height	= "120px";
	obj.style.visibility= "visible";
	obj.style.display	= "block";
	obj.style.zIndex	= "100000";
	document.frmlogin.email.focus();
}

//--------------------------------------------------
function showCmtfrm(linkname)
{

	var obj				= document.getElementById("commentfrm");

	var cord			= eval("getAnchorPosition('"+linkname+"')");
	
	obj.style.top		= eval("'"+cord.y+ "px'"); 
	obj.style.left		= eval("'"+cord.x+ "px'"); 

	obj.style.width		= "260px";
	obj.style.height	= "350px";
	obj.style.visibility= "visible";
	obj.style.display	= "block";
	obj.style.zIndex	= "100000";
	document.cmtfrm.name.focus();
	var temp_pos = cord.y-170;
	document.cmtfrm.topval.value=eval("'"+temp_pos+ "px'");
}
//--------------------------------------------------
function hidelogin()
{
	var obj				= document.getElementById("loginfrm");
	obj.style.visibility= "hidden";
}

//--------------------------------------------------

function hidecmt()
{
	var obj				= document.getElementById("commentfrm");
	obj.style.visibility= "hidden";
}

//--------------------------------------------------

function noCache(url)
{
	var sep = (-1 < url.indexOf("?")) ? "&" : "?"	
	url = url + sep + "__=" + encodeURIComponent((new Date()).getTime());	
	return url;
}

//--------------------------------------------------

function commentLogin()
{
	toURL = document.frmlogin.action;

		
	email	=	encodeURI(document.frmlogin.email.value);
	passwd	=	encodeURI(document.frmlogin.passwd.value);
	
	sendArray	= new Array();

	sendArray.push("email="+email);
	sendArray.push("passwd="+passwd);	
	sendArray	=	sendArray.join('&');

	getLoginResponse(toURL, sendArray);
}

//--------------------------------------------------
function saveComment()
{

	if(trim(document.cmtfrm.name.value) == "")
	{
		alert("Enter your name");
		document.cmtfrm.name.focus();
		return false;
	}
	
	if(trim(document.cmtfrm.email.value) == "")
	{
		alert("Enter your email");
		document.cmtfrm.email.focus();
		return false;
	}
	else if(Validate_Email(document.cmtfrm.email.value)== false)
	{
		return false;	
	}
	
	if(trim(document.cmtfrm.commenttxt.value) == "")
	{
		alert("Enter your comment");
		document.cmtfrm.commenttxt.focus();
		return false;
	}
	toURL = document.cmtfrm.action;

	name		=	encodeURI(document.cmtfrm.name.value);	
	email		=	encodeURI(document.cmtfrm.email.value);
	commenttxt	=	encodeURI(document.cmtfrm.commenttxt.value);
	savearticle	=	encodeURI(document.cmtfrm.savearticle.value);
	topval		=	encodeURI(document.cmtfrm.topval.value);
	
	sendArray	= new Array();

	sendArray.push("name="+name);
	sendArray.push("email="+email);
	sendArray.push("commenttxt="+commenttxt);
	sendArray.push("savearticle="+savearticle);
	sendArray.push("topval="+topval);

	sendArray	=	sendArray.join('&');

	if(trim(document.cmtfrm.savearticle.value)!="")
		getCmtResponse(toURL, sendArray);
}

//--------------------------------------------------
function trim(m)
{
	while(m.charAt(m.length-1)==' ')
	m=m.substring(0,m.length-1);
	return m;
}
//--------------------------------------------------
function getLoginResponse(fromURL, Linkpostdata)
{	
	if (window.ActiveXObject) 
	{
		HttpGetLoginResponse = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else 
	{
        HttpGetLoginResponse = new XMLHttpRequest;
    }    
	//alert("res:"+fromURL);
	HttpGetLoginResponse.open("POST", noCache(fromURL), true);
    HttpGetLoginResponse.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    HttpGetLoginResponse.onreadystatechange = HandleHttpGetResponseLogin;
    HttpGetLoginResponse.send(Linkpostdata);
}

//------------------------------------------------------------------------------

function HandleHttpGetResponseLogin() 
{

	if (HttpGetLoginResponse.readyState == 4) 
	{
		var show = HttpGetLoginResponse.responseText;

		if(show=="success")	
		{
			var obj				= document.getElementById("loginfrm");
			obj.style.visibility= "hidden";

				var obj				= document.getElementById("commentfrm");
				var cord			= getAnchorPosition("cmtlink");
				//alert("X :"+cord.x +" Y : "+cord.y); return false;
				//cord.y				= cord.y - 122;
				//cord.x				= cord.x + 24;
				obj.style.top		= eval("'"+cord.y + "px'"); 
				obj.style.left		= eval("'"+cord.x + "px'"); 

				obj.style.width		= "220px";
				obj.style.height	= "350px";
				obj.style.visibility= "visible";
				obj.style.display	= "block";
				obj.style.zIndex	= "100000";
				document.cmtfrm.name.focus();
		}
		else
			alert("Invalid login");

	}
		
}   

//--------------------------------------------------


function getCmtResponse(fromURL, Linkpostdata)
{	
	if (window.ActiveXObject) 
	{
		HttpGetCmtResponse = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else 
	{
        HttpGetCmtResponse = new XMLHttpRequest;
    }    
	//alert("res:"+fromURL);
	HttpGetCmtResponse.open("POST", noCache(fromURL), true);
    HttpGetCmtResponse.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    HttpGetCmtResponse.onreadystatechange = HandleHttpGetResponseCmt;
    HttpGetCmtResponse.send(Linkpostdata);
}

//------------------------------------------------------------------------------

function HandleHttpGetResponseCmt() 
{

	if (HttpGetCmtResponse.readyState == 4) 
	{
		var show = HttpGetCmtResponse.responseText;
			
		var cmtarr   = new Array();
		cmtarr		 = show.split('||');
		document.getElementById('cmttable').innerHTML = "<center><br><br>Your comments has been posted successfully<br><br><div style='text-align:center;width:220px;'><a href='javascript:void(0);' onclick='hidecmt()' class='a5'>Close</div></center>";
		//alert("top1:"+cmtarr[0] + "top2:"+cmtarr[1]+ "top2:"+cmtarr[2]);
		if(cmtarr[1] != undefined && cmtarr[1] != null )
		{
			document.getElementById('rewardsdiv').style.display = "block";
			document.getElementById('rewardsdiv').style.top = cmtarr[2];
			document.getElementById('rewardsdiv').style.left = "141px";
			document.getElementById('rewardsdiv').style.visibility = "visible";
			document.getElementById('rewardsdiv').style.zIndex = "1000000";
			document.getElementById('rewardsdiv').innerHTML = cmtarr[1];
		}


		//var obj			= document.getElementById("cmttable");
		
		//obj.innerHTML	= "<center><br><br>Your comments has been posted successfully<br><br><div style='text-align:center;width:220px;'><a href='javascript:void(0);' onclick='hidecmt()' class='a5'>Close</div></center>";
		
	}
		
}   

//--------------------------------------------------

function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
	if (use_gebi && document.all) {
			x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
			y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
			}
	else if (use_gebi) {
			var o=document.getElementById(anchorname);
			x=AnchorPosition_getPageOffsetLeft(o);
			y=AnchorPosition_getPageOffsetTop(o);
			}
	else if (use_css) {
			x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
			y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
			}
	else if (use_layers) {
			var found=0;
			for (var i=0; i<document.anchors.length; i++) {
					if (document.anchors[i].name==anchorname) { found=1; break; }
					}
			if (found==0) {
					coordinates.x=0; coordinates.y=0; return coordinates;
					}
			x=document.anchors[i].x;
			y=document.anchors[i].y;
			}
	else {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

function AnchorPosition_getPageOffsetLeft (el) {
		var ol=el.offsetLeft;
		while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
		return ol;
		}

function AnchorPosition_getWindowOffsetLeft (el) {
		return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
		}       

function AnchorPosition_getPageOffsetTop (el) {
		var ot=el.offsetTop;
		while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
		}

function AnchorPosition_getWindowOffsetTop (el) {
		return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
		}

//--------------------------------------------------