var fadeballot;
var fadetip;
var fadeouttip;
var pollid;
var fullboxID;
var onfeatureID = 1;
var featureslide_timer;

function htmlEntityDecode(str) 
{
	var ta = document.createElement("textarea");
	ta.innerHTML = str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	return ta.value;
}


function viewanyway(answerID) {
  
  answersummary = 'answersummary' + answerID
  votebutton = 'votebutton_spot' + answerID;
  answerimage = 'answerimage' + answerID;
  message_more = 'showmore' + answerID;
  message_less = 'showless' + answerID;
  
  toggleme(answersummary); 
  toggleme(votebutton,0); 
  toggleme(answerimage, 0);
  toggleme(message_more, 0);
  toggleme(message_less, 0);
  
}

function featureslide() {

  clearTimeout(featureslide_timer); //in case there is already a timer going, let's reset.
  featureslide_timer = setTimeout('featurenav("next", 4); featureslide()',5000);
  
}


function featurenav(featuretargetID, totalfeaturepages) 
{
  
  clearTimeout(featureslide_timer); //in case there is already a timer going, let's reset.
  
  //convert next/previous to target ID's
  if (featuretargetID == "next") { featuretargetID=onfeatureID+1;} 
  if (featuretargetID == "previous") { featuretargetID=onfeatureID-1; } 

  //make sure the target value is within acceptable range. If not, loop to the end or beginning value.
  if (featuretargetID < 1) { featuretargetID = totalfeaturepages}
  if (featuretargetID > totalfeaturepages) { featuretargetID = 1 }

  onfeaturelink = 'featurelink' + onfeatureID;
  featurelink = 'featurelink' + featuretargetID;
  onfeature = 'feature' + onfeatureID;
  targetfeature = 'feature' + featuretargetID;
  
  featureslide(); // Restart the timer. 

  
  	if (onfeature!=targetfeature) //if we're not already looking at the right page, then turn the page.
	{   
		if (document.getElementById(onfeature) != null)
		{
			toggleme(onfeature); //turns off current page
		}

		if (document.getElementById(targetfeature) != null)
		{
			showme(targetfeature); //turns on target page
		}

		if (document.getElementById(featurelink) != null)
		{
			document.getElementById(featurelink).className = 'on';
		}
		
		if (document.getElementById(onfeaturelink) != null)
		{
			document.getElementById(onfeaturelink).className = '';
		}

		onfeatureID = featuretargetID;
  }	   
}


function gotopage(pagenum)
{
	document.samesearch.page.value = pagenum;
	document.samesearch.submit();
}
	
function sortme(column)
{
	if (column == 'voters')
	{
		if (document.samesearch.sortby.value == 'num_voters')
		{ 
			// They clicked what it was already sorted by so we'll change direction
			if (document.samesearch.sortdir.value == 'DESC')
			{
				document.samesearch.sortdir.value = 'ASC';
			}
			else
			{
				document.samesearch.sortdir.value = 'DESC';
			}
		}
		else 
		{
			document.samesearch.sortby.value = 'num_voters';
			document.samesearch.sortdir.value = 'DESC';
		}
   	}
	else
	{
		if (document.samesearch.sortby.value == 'topic_date')
		{ 
			// they clicked the same sortby, lets change dir
			if (document.samesearch.sortdir.value == 'DESC')
			{
				document.samesearch.sortdir.value = 'ASC';
			}
			else
			{
				document.samesearch.sortdir.value = 'DESC';
			}
		}
		else
		{
			document.samesearch.sortby.value = 'topic_date';
			document.samesearch.sortdir.value = 'DESC';
		}
	}
	
	document.samesearch.submit();
}

function speak (id, what)
{
	var element = document.getElementById(id);
	element.innerHTML = 'Clicked ' + what;
}

function showBallotError(input, results, success)
{
    alert(results[0].getElementsByTagName('errortext')[0].firstChild.data);

	// If they also told us to reload the ballot, then we are going to
	if (results[0].getElementsByTagName('ballotreload')[0].firstChild.data == 'reload')
	{	
		var topicID = results[0].getElementsByTagName('topicid')[0].firstChild.data;
		var userConfirmReload = confirm('Would you like us to try and reload your ballot for you? Select "OK" to reload or "Cancel" to return to the page.');
		if (userConfirmReload)
		{
			reloadBallot(topicID);
		}
	}
}
	
function showcommentform(form_element, input_element)
{
  	toggleme(form_element); 
  
  	if(document.getElementById(form_element).style.display != 'none' && document.getElementById(form_element) != null)
	{ 
		// if comment form is visible, put focus on input field 
		document.getElementById(input_element).focus(); 
	}
}

function storeanswerdatainsession()
{
	var desc = escape(document.answers.description.value);
	var topicid = document.answers.topicid.value;
	var postString = 'desc=' + desc + '&topicid=' +topicid;
	var postURI = '/ajaxserver/ainfosession.php5';

	// now prevent IE caching
	var d = new Date();
	var t = d.getTime();
	postString += '&TIME=' + t;

	loadXMLDocPost(postURI, postString);
}

function storetopicdatainsession()
{
	var tags = escape(document.newquestion.tags.value);
	var megatagString = 'mega';
	var megatags = '';
	var topicForm = document.newquestion;
	var topicFormElements = topicForm.elements;
	for (ii=0; ii<=topicFormElements.length; ii++)
	{
		var topicElement = topicFormElements[ii];
		if (topicElement != null)
		{
			if (topicElement.type == 'checkbox')
			{
				if (topicElement.name.indexOf(megatagString) != -1)
				{
					if (topicElement.checked)
					{
						megatags += topicElement.value + ',';
					}
				}
			}
		}
	}

	var body = escape(document.newquestion.description.value);
	var postString = 'tags=' + tags + '&body=' + body + '&megatags=' + megatags; 
	var grupidElement = document.newquestion.grupid;
	if (grupidElement != null)
	{
		var grupid = grupidElement.value;
		postString += '&grupid=' + grupid;

		// We're also going to grab the grup permissions
		postString += '&gruppublic=';
		cbox = document.newquestion.gruppermview;
		if (cbox != null)
		{
			if (cbox.checked)
			{
				postString += '1';
			}
			else
			{
				postString += '0';
			}
		}

		postString += '&grupvote=';
		cbox = document.newquestion.gruppermvote;
		if (cbox != null)
		{
			if (cbox.checked)
			{
				postString += '1';
			}
			else
			{
				postString += '0';
			}
		}
		
		postString += '&grupanswer=';
		cbox = document.newquestion.gruppermanswers;
		if (cbox != null)
		{
			if (cbox.checked)
			{
				postString += '1';
			}
			else
			{
				postString += '0';
			}
		}
		
		postString += '&grupcomment=';
		cbox = document.newquestion.gruppermcomments;
		if (cbox != null)
		{
			if (cbox.checked)
			{
				postString += '1';
			}
			else
			{
				postString += '0';
			}
		}
		
		postString += '&gruppubliclist=';
		cbox = document.newquestion.publistperm;
		if (cbox != null)
		{
			if (cbox.checked)
			{
				postString += '1';
			}
			else
			{
				postString += '0';
			}
		}
	}
	var postURI = '/ajaxserver/qinfoinsession.php5';

	// now prevent IE caching
	var d = new Date();
	var t = d.getTime();
	postString += '&TIME=' + t;

	loadXMLDocPost(postURI, postString);
}

function redirectToAsk(input, results, success)
{
	grupid = results[0].getElementsByTagName('grupid')[0].firstChild.data;
	if (grupid != 0)
	{
		var uri = '/ask/?grupid=' + grupid;
	}
	else
	{
		var uri = '/ask/';
	}
	document.location = uri;
}

function redirectToAdd(input, results, success)
{
	topicid = results[0].getElementsByTagName('topicid')[0].firstChild.data;
	var uri = '/answer/add.php5?topicid=' + topicid;
	document.location = uri;
}

//The AJAX call and response functions for next/prev topic filter
function setTopicFilter(topicid, filtertype, override) 
{
	var postString = 'topicid='+topicid+'&filtertype='+filtertype;
	if (override == 'False')
	{
		postString += '&override=false';
	}
	// now prevent IE caching   
	var d = new Date();
	var t = d.getTime();
	postString += '&amp;TIME=' + t;
	var postURI = '/ajaxserver/settopicfilter.php5';
	loadXMLDocPost(postURI, postString);
}


function setTopicFilterAnswer(topicid, filtertype) 
{
	var postString = 'answerid='+topicid+'&filtertype='+filtertype;
	// now prevent IE caching   
	var d = new Date();
	var t = d.getTime();
	postString += '&amp;TIME=' + t;
	var postURI = '/ajaxserver/settopicfilter.php5';
	loadXMLDocPost(postURI, postString);
}

function viewTopicFiltered(input, results, success)
{
   newloc = "/topic/" + results[0].getElementsByTagName('topicid')[0].firstChild.data;
   document.location = newloc;
}

function viewTopicFilteredAnswer(input, results, success)
{
   newloc = "/answer/" + results[0].getElementsByTagName('answerid')[0].firstChild.data;
   document.location = newloc;
}



function stripHTML(text)
{
  var re= /<\S[^><]*>/g;
  return text.value.replace(re, "");
}

function showadvsearch()
{
  if(document.getElementById('advsearch').style.display =='none') 
  {
	document.getElementById('advsearch').style.display =''; 
	fademein('advsearch');
  }
}

// For limiting the length on text areas
function checkinputlength(field, maxlimit)
{
	if (document.getElementById(field) != null)
	{
		if (field.value.length > maxlimit)
		{
			field.value = field.value.substring(0, maxlimit);
			alert("Your input has been truncated. There is a " + maxlimit + " character limit on this field.");
		}
	}
}


// SHOW/HIDE Hint popup
function togglehintpop()  
{      
	//Functions that grab screen dimensions
   	var arrayPageSize = getPageSize(); 
	var arrayPageScroll = getPageScroll(); 
	
	//ID's for affected html elements	
	hintboxID = 'hintpop'; 
	
	hintbox = document.getElementById(hintboxID);
	if (hintbox != null)
	{
		hintbox.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 500) / 2) + 'px'); 
		hintbox.style.left = (((arrayPageSize[0] - 500) / 2) + 'px');
	}
	
	toggleme('opacitize', '0'); 
	
	//Wierd IE7 Bug: If height of opacitize div is over 4096, it won't accept an alpha transparency effect. Workaround:
	if (arrayPageSize[1] > 4096 && document.all) 
	{ 
		document.getElementById('opacitize').style.height = '4096px'; 
	}
	else //do the intended thing
	{
		document.getElementById('opacitize').style.height = (arrayPageSize[1] + 'px');     
	}
	
	//If IE, force alpha transparency using the "filter" property      
	if (document.all) { document.getElementById('opacitize').style.filter = "alpha(opacity=50)";}
	   
	toggleme(hintboxID);   
}

function closefull()
{
	toggleme('opacitize', '0');
	toggleme('fullimagebox');
	toggleme('fulllicensebox');
	// Bug in IE6 where dropdown boxes show through
	var dropDowns = document.getElementsByTagName('select');
	for (var ii = 0; ii < dropDowns.length; ii++)
	{
		if (dropDowns[ii].style.display == 'none')
		{
			dropDowns[ii].style.display = '';
		}
		else
		{
			dropDowns[ii].style.display = 'none';
		}
	}
}


// SHOW/HIDE Full-size image. Uses DOM to insert/replace the image in "fullimagebox" DIV
function togglefull(imgUrl)  
{      
	//Functions that grab screen dimensions
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	//ID's for affected html elements	
	fullboxID = 'fullimagebox';
	fulllnkID = 'fullimglnk';
	fullimgID = 'fullimg';

	fullbox = document.getElementById(fullboxID);
	fullbox.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 500) / 2) + 'px');
	fullbox.style.left = (((arrayPageSize[0] - 600) / 2) + 'px');

	toggleme('opacitize', '0');

	//Wierd IE7 Bug: If height of opacitize div is over 4096, it won't accept an alpha transparency effect. Workaround:
	if (arrayPageSize[1] > 4096 && document.all) 
	{ 
	document.getElementById('opacitize').style.height = '4096px'; 
	}
	else //do the intended thing
	{
	document.getElementById('opacitize').style.height = (arrayPageSize[1] + 'px');     
	}

	// Bug in IE6 where dropdown boxes show through
	var dropDowns = document.getElementsByTagName('select');
	for (var ii = 0; ii < dropDowns.length; ii++)
	{
		if (dropDowns[ii].style.display == 'none')
		{
			dropDowns[ii].style.display = '';
		}
		else
		{
			dropDowns[ii].style.display = 'none';
		}
	}

	//If IE, force alpha transparency using the "filter" property      
	if (document.all) { document.getElementById('opacitize').style.filter = 'alpha(opacity=50)';}

	toggleme(fullboxID);

	oldimgID = document.getElementById(fullimgID);
	var fullimage=document.createElement('img');	
	fullimage.onload = removebackground; //when image has loaded, change the class of div
	fullimage.src= imgUrl; 
	fullimage.id= fullimgID;
	document.getElementById(fulllnkID).replaceChild(fullimage,oldimgID);

	function removebackground() 
	{
	fullbox.className='imageloaded';
	}
    
}

function closevotingpopup(dontremind)
{
	if (document.getElementById('hintpop') != null)
	{
		var hintpop = document.getElementById('hintpop');
		if (dontremind == "never")
		{
			// Then we are going to start an ajax transaction
			var ajaxurl = '/ajaxserver/dontremindvotingpopup.php5';
			var postString = 'dontremind=1';
			
			// Prevent IE caching
			var d = new Date();
			var t = d.getTime();
			postString += '&TIME=' + t;
			loadXMLDocPost(ajaxurl, postString);
		}
		else
		{
			finishClosingVotingPopup();
		}
	}
}

function finishClosingVotingPopup()
{
	if (document.getElementById('hintpop') != null)
	{
		toggleme('hintpop');
		toggleme('opacitize', '0');
	}
}

// SHOW/HIDE ITEMS

function delay()
{
  return true;	
}

function toggleme(element,fade) //Set fade to 0 if you don't want the fade effect. Default is on.
{	
	// Make sure the element actually exists
	if (document.getElementById(element) != null)
	{
		if (document.getElementById(element).style.display == 'none') 
		{
			// Then we are going to display it
			document.getElementById(element).style.display = ''; 
			if (fade != 0)
			{
				// Then they asked for it to be faded in
				fademein(element);
			}
		}  
		else 
		{
			// We are going to hide it
			document.getElementById(element).style.display = 'none'; 	   
		}
	}
}


function toggleme2(element)
{
 
  if(document.getElementById(element).style.visibility =='hidden') 
  {
	showme(element);
  }  else {
	document.getElementById(element).style.visibility ='hidden'; 	   
  }
}

function showme(element)
{
	if (document.getElementById(element) != null)
	{
		if( document.getElementById(element).style.display == 'none') 
		{
			document.getElementById(element).style.display =''; 
			fademein(element);
		}
		
		if (document.getElementById(element).style.visibility == 'hidden') 
		{
			document.getElementById(element).style.visibility ='visible'; 
			fademein(element);
		}
	}
}

function hideme(elementID)
{
  
  element = document.getElementById(elementID);
  
  if(element.style.display =='') 
  {
	element.style.display ='none'; 
  }
}

// END SHOW/HIDE ITEMS

//GENERAL FADE EFFECTS FOR UNIQUE ELEMENTS
function fademein(element)
{
  var e = document.getElementById(element);
  fadesomething = new Fadomatic(e, 20, 1);
  fadesomething.fadeIn(); 	
}

function fademeout(element)
{
  var e = document.getElementById(element);
  fadesomething = new Fadomatic(e, 20, 100);
  fadesomething.fadeOut(); 	
}
//END GENERAL FADE EFFECTS FOR 

function removetip()
{
    var e = document.getElementById("tip");
	if(window.fadeouttip)  fadeouttip.haltFade();
 	fadeouttip = new Fadomatic(e, 25, 100); //global
	clearTimeout(tipTimer);
	fadeouttip.fadeOut(); 			
}

function getPosition(element)
{
	alert(document.getElementById(element).style.left);
}

//END TESTING
function el(name)
{
  return document.getElementById( name );
}


function changedisplay(element,display)
{
  if (display=='on') 
  {  document.getElementById(element).style.display =''; }
  else
  { document.getElementById(element).style.display ='none';}
}


function highlight(itemid)
{
  rowid = 'row' + itemid;
  row = document.getElementById(rowid);
  
  if(row.className != 'marked') 
  {
    row.className = 'highlighted';
  }

}


function dehighlight(itemid)  
{
  rowid = 'row' + itemid;

  if (document.getElementById(rowid).className !='marked')
  {
	row.className = '';
  }
}


// Change style class on any object
function changeclass(id, newClass) {
identity=document.getElementById(id);
identity.className=newClass;
}


/* Currently unused:  Left her for example */
function newitemfade(itemid)  
{
  var row = document.getElementById( 'row' + itemid );
    
	var fade = new Fade(row, '#c8e9f1', '#e4f4ba', 150, 15, 0); 
    fade.init();	
  
}




//------------OPACITY FADE EFFECT------------------//


// Fade interval in milliseconds
// Make this larger if you experience performance issues
Fadomatic.INTERVAL_MILLIS = 50;

// Creates a fader
// element - The element to fade
// speed - The speed to fade at, from 0.0 to 100.0
// initialOpacity (optional, default 100) - element's starting opacity, 0 to 100
// minOpacity (optional, default 0) - element's minimum opacity, 0 to 100
// maxOpacity (optional, default 0) - element's minimum opacity, 0 to 100
function Fadomatic (element, rate, initialOpacity, minOpacity, maxOpacity) {
  this._element = element;
  this._intervalId = null;
  this._rate = rate;
  this._isFadeOut = true;

  // Set initial opacity and bounds
  // NB use 99 instead of 100 to avoid flicker at start of fade
  
  // I chose to use 100 to avoid a jagged text bug in Google Chrome. I have not seen a flicker issue. -ss
  this._minOpacity = 0;
  this._maxOpacity = 100;
  this._opacity = 100;

  if (typeof minOpacity != 'undefined') {
    if (minOpacity < 0) {
      this._minOpacity = 0;
    } else if (minOpacity > 99) {
      this._minOpacity = 99;
    } else {
      this._minOpacity = minOpacity;
    }
  }

  if (typeof maxOpacity != 'undefined') {
    if (maxOpacity < 0) {
      this._maxOpacity = 0;
    } else if (maxOpacity > 99) {
      this._maxOpacity = 99;
    } else {
      this._maxOpacity = maxOpacity;
    }

    if (this._maxOpacity < this._minOpacity) {
      this._maxOpacity = this._minOpacity;
    }
  }
  
  if (typeof initialOpacity != 'undefined') {
    if (initialOpacity > this._maxOpacity) {
      this._opacity = this._maxOpacity;
    } else if (initialOpacity < this._minOpacity) {
      this._opacity = this._minOpacity;
    } else {
      this._opacity = initialOpacity;
    }
  }

  // See if we're using W3C opacity, MSIE filter, or just
  // toggling visiblity
  if(typeof element.style.opacity != 'undefined') {

    this._updateOpacity = this._updateOpacityW3c;

  } else if(typeof element.style.filter != 'undefined') {

    // If there's not an alpha filter on the element already,
    // add one
    if (element.style.filter.indexOf("alpha") == -1) {

      // Attempt to preserve existing filters
      var existingFilters="";
      if (element.style.filter) {
        existingFilters = element.style.filter+" ";
      }
      element.style.filter = existingFilters+"alpha(opacity="+this._opacity+")";
    }

    this._updateOpacity = this._updateOpacityMSIE;
    
  } else {

    this._updateOpacity = this._updateVisibility;
  }

  this._updateOpacity();
}

// Initiates a fade out
Fadomatic.prototype.fadeOut = function () {
  this._isFadeOut = true;
  this._beginFade();
}

// Initiates a fade in
Fadomatic.prototype.fadeIn = function () {
  this._isFadeOut = false;
  this._beginFade();
}

// Makes the element completely opaque, stops any fade in progress
Fadomatic.prototype.show = function () {
  this.haltFade();
  this._opacity = this._maxOpacity;
  this._updateOpacity();
}

// Makes the element completely transparent, stops any fade in progress
Fadomatic.prototype.hide = function () {
  this.haltFade();
  this._opacity = 0;
  this._updateOpacity();
}

// Halts any fade in progress
Fadomatic.prototype.haltFade = function () {

  clearInterval(this._intervalId);
}

// Resumes a fade where it was halted
Fadomatic.prototype.resumeFade = function () {

  this._beginFade();
}

// Pseudo-private members

Fadomatic.prototype._beginFade = function () {

  this.haltFade();
  var objref = this;
  this._intervalId = setInterval(function() { objref._tickFade(); },Fadomatic.INTERVAL_MILLIS);
}

Fadomatic.prototype._tickFade = function () {

  if (this._isFadeOut) {
    this._opacity -= this._rate;
    if (this._opacity < this._minOpacity) {
      this._opacity = this._minOpacity;
      this.haltFade();
    }
  } else {
    this._opacity += this._rate;
    if (this._opacity > this._maxOpacity ) {
      this._opacity = this._maxOpacity;
      this.haltFade();
    }
  }

  this._updateOpacity();
}

Fadomatic.prototype._updateVisibility = function () {
  
  if (this._opacity > 0) {
    this._element.style.visibility = 'visible';
  } else {
    this._element.style.visibility = 'hidden';
  }
}

Fadomatic.prototype._updateOpacityW3c = function () {
  
  this._element.style.opacity = this._opacity/100;
  this._updateVisibility();
}

Fadomatic.prototype._updateOpacityMSIE = function () {
  
  this._element.filters.alpha.opacity = this._opacity;
  this._updateVisibility();
}

Fadomatic.prototype._updateOpacity = null;

//------------ END FADE EFFECT ------------------//






//------------ COLOR FADE EFFECT (Currently Unused)------------------//
/* 

Script to gradually change background colour of an element

References: 
   http://www.37signals.com/svn/archives/000558.php
   http://www.meyerweb.com/eric/tools/color-blend/

Future work: 
   Could change foreground, border colours
   Transparency

*/

function Fade(id, startColour, endColour, count, speed, delay ) {

  /* Properties */

  this.id = id;                    /* id/object ref of element to fade */
  this.startColour = startColour;  /* initial colour (3 or 6 digit) hex */
  this.endColour = endColour;      /* final colour (3 or 6 digit) hex */
  this.count = count;              /* No of steps to take during fade */
  this.speed = speed;              /* Delay in ms between steps */
  this.delay = delay;              /* Initial delay before fade begins */

  if (typeof this.id == "string") {
    this.obj = document.getElementById(id);  
  } else {
    this.obj = this.id;
  }
  this.colour = new Array();
  this.steps = 0;

  /* Methods */

  this.init = init;
  this.fade = fade;
  this.parseColour = parseColour;

  function init() {

    first = this.parseColour(this.startColour, 'hex');
    last = this.parseColour(this.endColour, 'hex');

    this.colour = new Array();
    this.colour[this.count] = this.startColour;
    for (i=0; i<this.count; i++) {
      temp = "rgb(";
      temp += parseInt(first[0]+(last[0]-first[0])/this.count*i);
      temp += ",";
      temp += parseInt(first[1]+(last[1]-first[1])/this.count*i);
      temp += ",";
      temp += parseInt(first[2]+(last[2]-first[2])/this.count*i);
      temp += ")";
      this.colour[this.count-i] = temp;
    }
    this.colour[0] = this.endColour;

    var thisObj = this;
    setTimeout( function() { thisObj.fade() }, this.delay);

  }

  function fade() {
      
    if (this.count >= 0) {

      this.obj.style.backgroundColor = this.colour[this.count--];

      // I want to do this:
      // setTimeout("this.fade()", init.speed);
      // but setTimeout runs in a different thread so 'this' 
      // is out of context. 
      // See: http://www.faqts.com/knowledge_base/view.phtml/aid/2311

      var thisObj = this;
      setTimeout( function() { thisObj.fade() }, this.speed);

    }
  }

  function parseColour(colour, t) {
    /* From: http://www.meyerweb.com/eric/tools/color-blend/ */
    var m = 1;
    col = colour.replace(/[\#rgb\(]*/,'');
    if (t == 'hex') {
      if (col.length == 3) {
        a = col.substr(0,1);
        b = col.substr(1,1);
        c = col.substr(2,1);
        col = a + a + b + b + c + c;
      }
      var num = new Array(col.substr(0,2),col.substr(2,2),col.substr(4,2));
      var base = 16;
    } else {
      var num = col.split(',');
      var base = 10;
    }
    if (t == 'rgbp') {m = 2.55}
    var ret = new Array(parseInt(num[0],base)*m,parseInt(num[1],base)*m,parseInt(num[2],base)*m);
    return(ret);
  }
}

//------------ END COLOR FADE EFFECT ------------------//

//------------ ToolTip code ---------------------------//
var showTp = 0;

function showToolTip(id)
{
   showTp = 1;
   var d = new Date();
   var t = d.getTime();
   var ajaxurl = '/ajaxserver/tooltips.php5?tooltip_id=' + id + '&time=' + t; 
   loadXMLDoc(ajaxurl);
}

function setToolTip(input, results, success)
{
   if (success == 1)
   {
      var szText = results[0].getElementsByTagName('tooltip')[0].firstChild.data;
      if (showTp == 1)
         overlib(szText, LEFT);
   }
}

function getPolls( search, searchdisp, userid )
{
   document.mysearch.advsearchoptions.value = search;
   document.mysearch.searchtxt.value = searchdisp;
   if( document.mysearch.userid )
     document.mysearch.userid.value = userid;
   document.mysearch.submit();
}

function addToNotify(topicid)
{
	var topicid = topicid;
	var postURI = '/ajaxserver/addtonotify.php5';
	
	// now prevent IE caching
	var d = new Date();
	var t = d.getTime();
	postString = 'topicid=' + topicid + '&TIME=' + t;

	// Now send the request away
	loadXMLDocPost(postURI, postString);
}

function addNotifyDone( input, results, success )
{
	var modmsg = document.getElementById( 'commentremovenotifymsg' );
	if (modmsg != null)
	modmsg.style.display = 'none';
	
	var modmsg = document.getElementById( 'commentnotifymsg' );
	if (modmsg != null)
	modmsg.style.display = 'none';
	
	var modmsg = document.getElementById( 'answernotifymsg' );
	if (modmsg != null)
	modmsg.style.display = '';
	
	var modmsg = document.getElementById( 'answerremovenotifymsg' );
	if (modmsg != null)
	modmsg.style.display = 'none';
	
	var notify = document.getElementById( 'notifymeanswers' );
	if (modmsg != null)
	notify.style.display = 'none';
	
	var notifyoff = document.getElementById( 'notifymeanswersoff' );
	if (modmsg != null)
	notifyoff.style.display = '';
}

function removeFromNotify(topicid)
{
	var topicid = topicid;
	var postURI = '/ajaxserver/removefromnotify.php5';
	
	// now prevent IE caching
	var d = new Date();
	var t = d.getTime();
	postString = 'topicid=' + topicid + '&TIME=' + t;

	// Now send the request away
	loadXMLDocPost(postURI, postString);
}

function removeFromNotifyDone( input, results, success )
{
	var modmsg = document.getElementById( 'commentremovenotifymsg' );
	modmsg.style.display = 'none';
	
	var modmsg = document.getElementById( 'commentnotifymsg' );
	modmsg.style.display = 'none';
	
	var modmsg = document.getElementById( 'answerremovenotifymsg' );
	modmsg.style.display = '';
	
	var modmsg = document.getElementById( 'answernotifymsg' );
	modmsg.style.display = 'none';
	
	var notify = document.getElementById( 'notifymeanswers' );
	notify.style.display = '';
	
	var notifyoff = document.getElementById( 'notifymeanswersoff' );
	notifyoff.style.display = 'none';
}

function commentNotify(contentid, contenttype, remove)
{
	var contentid = contentid;
	var contenttype = contenttype;
	var remove = remove;
	var postURI = '/ajaxserver/commentnotify.php5';
	
	// now prevent IE caching
	var d = new Date();
	var t = d.getTime();
	postString = 'contentid=' + contentid + '&contenttype=' + contenttype + '&remove=' + remove + '&TIME=' + t;

	// Now send the request away
	loadXMLDocPost(postURI, postString);
}

function commentNotifyDone( input, results, success )
{
  var removeaction = results[0].getElementsByTagName('actionremove')[0].firstChild.data;
  
  var modmsg = document.getElementById( 'answerremovenotifymsg' );
  if (modmsg != undefined)
     modmsg.style.display = 'none';
  var modmsg = document.getElementById( 'answernotifymsg' );
  if (modmsg != undefined)
     modmsg.style.display = 'none';
  
  if (removeaction == "0") // we removed the notification
  {
     var modmsg = document.getElementById( 'commentremovenotifymsg' );
     modmsg.style.display = 'none';
     var modmsg = document.getElementById( 'commentnotifymsg' );
     modmsg.style.display = '';
     var notify = document.getElementById( 'notifymecomment' );
     notify.style.display = 'none';
     var notifyoff = document.getElementById( 'notifymecommentoff' );
     notifyoff.style.display = '';
  }
  else
  {
     var modmsg = document.getElementById( 'commentremovenotifymsg' );
     modmsg.style.display = '';
     var modmsg = document.getElementById( 'commentnotifymsg' );
     modmsg.style.display = 'none';
     var notify = document.getElementById( 'notifymecomment' );
     notify.style.display = '';
     var notifyoff = document.getElementById( 'notifymecommentoff' );
     notifyoff.style.display = 'none';
  }
}

function modComment( id, mod )
{
  var ajaxurl = '/ajaxserver/modcomment.php5?id=' + id + '&mod=' + mod;
  loadXMLDoc(ajaxurl);
}

function modCommentDone( input, results, success )
{
  var id = results[0].getElementsByTagName('commentid')[0].firstChild.data;
  id = parseInt( id, 10 );
  var mod = results[0].getElementsByTagName('moderation')[0].firstChild.data;

  // first, turn them all on
  document.getElementById( 'modhelpful' + id ).className = '';
  document.getElementById( 'modunhelpful' + id ).className = '';
  document.getElementById( 'modinappropriate' + id ).className = '';

  // then, turn the selected one off
  document.getElementById( 'mod' + mod + id ).className = 'disabled';

  var modmsg = document.getElementById( 'commentmodmsg' + id );
  modmsg.style.display = '';
}

function modAnswer( id, mod, deleteme )
{
  var ajaxurl = '/ajaxserver/modanswer.php5?id=' + id + '&mod=' + mod;
  if (deleteme)
     ajaxurl += '&delete=1';
  // now prevent IE caching
   var d = new Date();
   var t = d.getTime();
   ajaxurl += '&TIME=' + t;
  loadXMLDoc(ajaxurl);
}

function modAnswerDone( input, results, success )
{
  var id = results[0].getElementsByTagName('answerid')[0].firstChild.data;
  id = parseInt( id, 10 );
  var mod = results[0].getElementsByTagName('moderation')[0].firstChild.data;
  var on = results[0].getElementsByTagName('on')[0].firstChild.data;

  // first, turn them all on
  //document.getElementById( 'modinappropriate' + id ).className = '';
  //document.getElementById( 'modadult' + id ).className = '';

  // then, turn the selected one off
  // document.getElementById( 'mod' + mod + id ).className = 'disabled';
  
  if (mod == 'adult')
  {
     var modadult = document.getElementById( 'modadult' + id);
     if (on == "1")
     {
        modadult.firstChild.data = 'Not Adult';
        modadulton = true;
     }
     else
     {
        modadult.firstChild.data = 'Adult';
        modadulton = false;
     }
  }
  else if (mod == 'inappropriate')
  {
     var modinap = document.getElementById( 'modinappropriate' + id);
     if (on == "1")
     {
        modinap.firstChild.data = 'Relevant';
        modinapon = true;
     }
     else
     {
        modinap.firstChild.data = 'Not Relevant';
        modinapon = false;
     }
  }

  var modmsg = document.getElementById( 'answermodmsg' + id );
  modmsg.style.display = '';
}

function modPoll( id, mod, deleteme )
{
  var ajaxurl = '/ajaxserver/modpoll.php5?id=' + id + '&mod=' + mod;
  if (deleteme)
     ajaxurl += "&delete=1";
  // now prevent IE caching
   var d = new Date();
   var t = d.getTime();
   ajaxurl += '&TIME=' + t;
  loadXMLDoc(ajaxurl);
}

function modPollDone( input, results, success )
{
  var id = results[0].getElementsByTagName('pollid')[0].firstChild.data;
  id = parseInt( id, 10 );
  var mod = results[0].getElementsByTagName('moderation')[0].firstChild.data;
  var on = results[0].getElementsByTagName('on')[0].firstChild.data;

  var pollmod = document.getElementById( 'pollmodadult' + id );
  if (on == "1")
  {
     pollmod.firstChild.data = "Not Adult";
     modadulton = true;
  }
  else
  {
     pollmod.firstChild.data = "Adult";
     modadulton = false;
  }

  var modmsg = document.getElementById( 'pollmodmsg' + id );
  modmsg.style.display = '';
}


function addToSessionAynom(key, value)
{
  var ajaxurl = '/ajaxserver/addtosession.php5?key=' + key + '&value=' + value;
  // now prevent IE caching
   var d = new Date();
   var t = d.getTime();
   ajaxurl += '&TIME=' + t;
   loadAynomXMLDoc(ajaxurl);
}

function addToSession(key, value)
{
  var ajaxurl = '/ajaxserver/addtosession.php5?key=' + key + '&value=' + value;
  // now prevent IE caching
   var d = new Date();
   var t = d.getTime();
   ajaxurl += '&TIME=' + t;
   loadXMLDoc(ajaxurl);
}

function genericAjaxSuccess( input, results, success )
{
  // call the overide function if it is defined
  if (self.genericOveride) 
     genericOveride();
}

//-------------------Positioning -------------------------//

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//------------------End Positioning ----------------------//

		/*********************************************************************
		 * No onMouseOut event if the mouse pointer hovers a child element 
		 * *** Please do not remove this header. ***
		 * This code is working on my IE7, IE6, FireFox, Opera and Safari
		 * 
		 * Usage: 
		 * <div onMouseOut="fixOnMouseOut(this, event, 'JavaScript Code');"> 
		 *		So many childs 
		 *	</div>
		 *
		 * @Author Hamid Alipour Codehead @ webmaster-forums.code-head.com		
		**/
		function is_child_of(parent, child) {
			if( child != null ) {			
				while( child.parentNode ) {
					if( (child = child.parentNode) == parent ) {
						return true;
					}
				}
			}
			return false;
		}
		function fixOnMouseOut(element, event, JavaScript_code) {
			var current_mouse_target = null;
			if( event.toElement ) {				
				current_mouse_target 			 = event.toElement;
			} else if( event.relatedTarget ) {				
				current_mouse_target 			 = event.relatedTarget;
			}
			if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
				eval(JavaScript_code);
			}
		}
		/*********************************************************************/

