// JavaScript Document
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
		return c.substring(nameEQ.length,c.length);
	}
	return 0;
}

function friend(x)
{
var passwd = readCookie('pass');
var username = readCookie('username');
var user_id = readCookie('userid');

if(!passwd == 0 && !username == 0)
{
document.getElementById('login').innerHTML = 'Welcome back <b>'+ username +'</b>,&nbsp;<a href="/myprofile.php?id='+user_id+'">My profile</a>,&nbsp;<a href="/login/logout.php">Logout</a>';
}
else
{
document.getElementById('login').innerHTML = '<a href="/login/login.php?page='+x+'">Login</a>&nbsp;|&nbsp;<a href="/login/register_form.php">Register</a>';
}
}


//Declaration of global variables

var _ad;          //Scrolling banner element
var _adTop;       //Original vertical position of div#ad-right
var _maxScroll;   //Space between the top of the document and the place where the ad should stop scrolling (the top of the div#footer)
var _lastPos = 0; //Last value returned by function pos()


function init() {

 if(document.getElementById('adv_right')) 
 {
  //Define the variables
  _ad = document.getElementById('adv_right');
  _adTop = _ad.offsetTop;
  _maxScroll = document.getElementById('main').offsetHeight + _adTop - _ad.offsetHeight;

  //Checks if the page is long enough for a scrolling ad
  if(_maxScroll > _adTop)
   adScroll();

 }
}


function pos() { //Returns current vertical position on page (for scrolling ad)

 if(window.innerHeight)
  return window.pageYOffset;

 if(document.documentElement && document.documentElement.scrollTop)
  return document.documentElement.scrollTop;

 if(document.body)
  return document.body.scrollTop;

 return 0;

}


function adScroll(){ //Recursive function, changes the position of the scrolling ad

 if(_lastPos != pos()) {

  _lastPos = pos();

  if(pos() < _maxScroll && pos() > _adTop)
   _ad.style.top = pos() + 'px';

  else if(pos() < _maxScroll)
   _ad.style.top = _adTop + 'px';

  else _ad.style.top = _maxScroll + 'px';

 }

 setTimeout('adScroll()', 10);

}


window.onresize = function() { //Relocate the ad when the browser window resizes

 if(_ad) {

  _adTop = document.getElementById('main').offsetTop;
  _maxScroll = document.getElementById('main').offsetHeight + _adTop - _ad.offsetHeight;

 }

}



		  
function Ajaxify(target, method, url, param, slideIt, loadImage, shoWait) {

if(shoWait != 0)
{
document.getElementById(target).innerHTML = shoWait;	
}


if(loadImage != 0)
{
document.getElementById(loadImage).innerHTML = '<img src="/images/load.gif"/>';
}

/*
	Find Correct XMLHTTP Connection	*/ 
if(!window.XMLHttpRequest) 
{
 window.XMLHttpRequest = function() 
 {
  var types = [
   'Microsoft.XMLHTTP',
   'MSXML2.XMLHTTP.5.0',
   'MSXML2.XMLHTTP.4.0',
   'MSXML2.XMLHTTP.3.0',
   'MSXML2.XMLHTTP'
  ];
  for(var i = 0; i < types.length; i++) {
   try {
    return new ActiveXObject(types[i]);
   } catch(e) {}
  }
  return false;
 };
}
  
/*
	Create Socket	*/
NewRequest = new XMLHttpRequest();
NewRequest.open(method, url, true);
NewRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
NewRequest.send(param); 


NewRequest.onreadystatechange = function() 
{

if (NewRequest.readyState == 4) 
{
if (NewRequest.status == 200) 
{

if(loadImage != 0)
{
document.getElementById(loadImage).innerHTML = '';
}

if(slideIt != 0)
{
var comment = new Fx.Slide(target, {duration: slideIt}).hide();
}
document.getElementById(target).innerHTML = unescape(NewRequest.responseText);
if(slideIt != 0)
{
comment.toggle();
}
}
	
else
{
document.getElementById(target).innerHTML = 'Ajax Error';
}
}
   
};
}


//SEARCH FORM VALIDATION--------------------------------------------------------------------------------------------------------------------------------------------------------------------

function checkSearchForm()
{
 if(document.gosearch.term.value == "")
 {
  alert("Fill out the entire form. Please try again");
  document.gosearch.term.focus();
 } 
 else
 { 
  document.gosearch.submit();
 }
 
 //alert("Under construction!");
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// spinners -------------------------------------------------------------------------------------------------------------------------------------------------------------

function spinners(z)
{
  imgObj = document.getElementById('img_'+z);
  divObj = document.getElementById(z);
  //alert(escape(imgObj.innerHTML));
  if(escape(imgObj.innerHTML) == '%u2192')
  {
	imgObj.innerHTML = "&darr; &nbsp;";
	divObj.style.display = "block";
  }
  else
  {
	imgObj.innerHTML = "&rarr;";
	divObj.style.display = "none";
  }
}