//Sidebar Functions

var sideBarBoxes = [];

function saveBoxOrder(userID)
{
	var theTable = document.getElementById('sideBarBoxTable');
	var trs = theTable.rows;
	var tempString = "";
	for(var i = 0; i < trs.length; i++)
	{
		if(tempString == "")
		{
			tempString = theTable.rows[i].id;
		} else {
			tempString = tempString + "," + theTable.rows[i].id;
		}
	}
	if(userID != "")
	{
		saveBoxData('-1', userID, 'displayorder', tempString, "");
	}
}
function saveBoxData(boxID, userID, dataName, data, statusDiv)
{
	var ajaxObj = getXhttp();
	var params = "box_id=" + boxID + "&user_id=" + userID + "&field_name=" + dataName + "&box_data=" + data;
	if(statusDiv != "")
	{
		document.getElementById(statusDiv).innerHTML = '<img src=\"images/loading.gif\" border=\"0\" />';
	}
	ajaxObj.open('POST', 'functions.php?f=saveBoxData', false);
	ajaxObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxObj.setRequestHeader("Content-length", params.length);
	ajaxObj.send(params);
	if(statusDiv != "")
	{
		document.getElementById(statusDiv).innerHTML = '';
		document.getElementById(statusDiv).innerHTML = ajaxObj.responseText;
	}
}
function toggleSideBarBox(theRowToHide, boxID, userID)
{
	currentRow = document.getElementById(theRowToHide);
	if (currentRow.style.display == "none")
	{
		currentRow.style.display = "table-row";
		saveBoxData(boxID, userID, "visible", "1", "");
	} else {
		currentRow.style.display = "none";
		saveBoxData(boxID, userID, "visible", "0", "");
	}
}
function moveBoxUp(theT, theR, userID)
{
	var theRow = document.getElementById(theR);
	var theTable = document.getElementById(theT);
	var oldPos = theRow.rowIndex;
	var newPos = oldPos - 1;
	//var trs = theTable.tBodies[0].getElementsByTagName("tr");
	var trs = theTable.rows;
	if(newPos > 0)
	{
		theTable.tBodies[0].insertBefore(trs[oldPos], trs[newPos]);
	}
	saveBoxOrder(userID);
}
function moveBoxDown(theT, theR, userID)
{
	var theRow = document.getElementById(theR);
	var theTable = document.getElementById(theT);
	//var theTable = theRow.parentNode.parentNode;
	var oldPos = theRow.rowIndex;
	var newPos = oldPos + 1;
	//var trs = theTable.tBodies[0].getElementsByTagName("tr");
	var trs = theTable.rows;
	if(newPos <= theTable.rows.length)
	{
		theTable.tBodies[0].insertBefore(trs[newPos], trs[oldPos]);
	}
	saveBoxOrder(userID);
}
function goLite(BTN)
{
	document.getElementById(BTN).style.backgroundColor = "#888888";
}
function goDim(BTN)
{
	document.getElementById(BTN).style.backgroundColor = "#444444";
}
function saveCookie(cookieName, cookieValue)
{
	document.cookie=cookieName+"="+cookieValue;
}
function loadCookie(cookieName)
{
	return get_cookie(cookieName);
}
function get_cookie(Name)
{
	var search = Name + "=";
	var returnvalue = "";
	if (document.cookie.length > 0)
	{
		offset = document.cookie.indexOf(search);
		if (offset != -1)
		{
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1)
				end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end));
		}
	}
	return returnvalue;
 }
function toggleRow(theRowToHide)
{
	currentRow = document.getElementById(theRowToHide);
	if (currentRow.style.display == "none")
	{
		currentRow.style.display = "table-row";
	} else {
		currentRow.style.display = "none";
	}
}

//Main Page Functions
function newCaptcha(theSpan)
{
	captchaSpan = document.getElementById(theSpan);
	captchaSpan.innerHTML = '';
	captchaSpan.innerHTML = '<img src="captcha.php?id=' + Math.floor(100*Math.random()).toString() + '" width="125" height="40" />';
}
function checkUsername()
{
	theUsername = document.getElementById('user_name');
	statusSpan = document.getElementById('statusSpan');
	checkButton = document.getElementById('checkUsernameB');
	checkButton.disabled = true;
	var ajaxObj = getXhttp();
	var params = "user_name=" + theUsername.value;
	ajaxObj.open('POST', 'functions.php?f=isUsernameAvailable', false);
	ajaxObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxObj.setRequestHeader("Content-length", params.length);
	ajaxObj.send(params);
	statusSpan.innerHTML = ajaxObj.responseText;
	checkButton.disabled = false;
}

//Other Functions

function stripslashes(str)
{
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\0/g,'\0');
	str=str.replace(/\\\\/g,'\\');
	return str;
}
function getXhttp ( )
{
	var ajax_request;

	if (  window.ActiveXObject  )  {
		var mSoftVersions = [
		'MSXML2.DOMDocument.5.0',
		'MSXML2.DOMDocument.4.0',
		'MSXML2.DOMDocument.3.0',
		'MSXML2.DOMDocument.2.0',
		'MSXML2.DOMDocument',
		'Microsoft.XmlDom',
		'Microsoft.XMLHTTP'
		];

		for (  i=0; i<mSoftVersions.length; i++  )  {
			try {
				ajax_request = new ActiveXObject (  mSoftVersions[i]  );
			}  catch (  e  )  {    }
		}
	}  else if (  !ajax_request && typeof XMLHttpRequest != 'undefined'  )  {
		try {
			ajax_request = new XMLHttpRequest;
		}  catch (  e  )  {    }
	}  else if (  !ajax_request && window.createRequest  )  {
		try {
			ajax_request = window.createRequest;
		}  catch (  e  )  {    }
	}  else  {
		ajax_request = false;
	}
	
	return ajax_request;
}