﻿/*  GET XMLHttpObject */

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

/* ---------------------- */

/* Tagging - Add tag weight */

function AddTagWeight(tag,news_id,id)
{

	xmlHttp = GetXmlHttpObject();
	if(xmlHttp==null)
	{
		return;
	}

	var url = "s_scripts/tagweight.php";
	url = url + "?id=" + id;
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.getElementById("tagreturn_" + news_id).innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

/* ---------------------- */

/* Submit a shout AJAX style */
function SubmitShout(shoutbox_id,message_id, name_id, captcha_id, form_id)
{
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp==null)
	{
		return;
	}
		
	message = document.getElementById(message_id).value;
	name = document.getElementById(name_id).value;
	captcha = document.getElementById(captcha_id).value;
	
	document.getElementById(form_id).innerHTML="Sending data...";

//	var url = "s_content/newsShoutSidebar.php?action=shouted&shout=" + message + "&name=" + name + "&security_code=" + captcha;
	var url = "s_content/newsShoutSidebar.php"
	var params = "action=shouted&shout=" + message + "&name=" + name + "&security_code=" + captcha;

	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.getElementById(shoutbox_id).innerHTML=xmlHttp.responseText;
		}
	}
	//xmlHttp.open("GET", url, true);
	//xmlHttp.send(null);
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);

}
/* ---------------------- */


/* Toggles shoutbox visibility (yeah, not really ajax related) */ 

function ToggleShoutbox(div_id, shouts_id)
{

	if (document.getElementById(div_id).innerHTML == "")
	{
		rand = Math.round(Math.random()*1000);

		shoutboxContent = "<form method=\"post\" action=\"javascript:SubmitShout('" + shouts_id + "','shout_mess', 'shout_name', 'security_code', '" + div_id + "')\">";
		shoutboxContent+= "<input class=\"textForm\" type=\"text\" name=\"name\" value=\"Name\" maxlength=\"15\" id=\"shout_name\"  onFocus=\"clearDefault('shout_name', 'Name')\" onBlur=\"setDefault('shout_name', 'Name')\">";
		shoutboxContent+= "<div class=\"spacer2\"></div>";
		shoutboxContent+= "<textarea rows=\"3\" name=\"shout\" class=\"textArea\" cols=\"5\" id=\"shout_mess\" onFocus=\"clearDefault('shout_mess', 'Message')\" onBlur=\"setDefault('shout_mess', 'Message')\">Message</textarea>";
		shoutboxContent+= "<div class=\"spacer2\"></div>";
		shoutboxContent+= "<img src=\"/s_scripts/skull_captcha.php?characters=5&p=" + rand + " alt=\"spam protection, sorry, need images enabled to see it\">";
//		shoutbox+='<img src="skull_captcha.php?characters=5" alt="spam protection, sorry, need images enabled to see it">';

		shoutboxContent+= "	Security Code: ";
		shoutboxContent+= "<input id=\"security_code\" name=\"security_code\" type=\"text\" class=\"textForm\">";
		shoutboxContent+= "<div class=\"spacer2\"></div>";
		shoutboxContent+= "<input class=\"button\" type=\"submit\" name=\"submit\" value=\"- shout -\"></form>";
		document.getElementById(div_id).innerHTML=shoutboxContent;
	}
	else
		document.getElementById(div_id).innerHTML = "";
} 

/* ---------------------- */

/* Set default text if focus is lost (and empty) */ 

function setDefault(form_id, defaultText)
{
	if (document.getElementById(form_id).value == "")
		document.getElementById(form_id).value = defaultText;

} 

/* ---------------------- */

/* Remove default text if focus is aquired */ 

function clearDefault(form_id, defaultText)
{
	if (document.getElementById(form_id).value == defaultText)
		document.getElementById(form_id).value = "";

} 

/* ---------------------- */



/* Changes current style, dynamically as well as setting it in the database */ 

function switchStyleSheet(styleName, id)
{
	curStyle = document.getElementById("currentStyleSheet");
	if (curStyle.href.match("styles_xmas.css"))
	{
		alert("Oh noes, its december, you can't change the theme then!");
		return;
	}

	curStyle.href = '/styles' + styleName + '.css';
	document.getElementById("theme").innerHTML = getThemeName(id);

	worker = GetXmlHttpObject();
	if(worker==null)
	{
		return;
	}

	var url = "http://www.skallen.net/s_scripts/switchStyle.php?style=" + id;
	
	worker.onreadystatechange=function()
	{
		if(worker.readyState==4)
		{
			if ( worker.responseText != "OK!" && worker.responseText != "")
			{
				//alert("error");
				alert(worker.responseText);
			}
		}
	}
	
	worker.open("GET", url, true);
	worker.send(null);
}

/* style sheet names */
function getThemeName(theme)
{	
	theme_month = new Date().getMonth();
	if( theme_month == 11)
		return("'Christmas Theme!'");

	switch (theme)
	{
		case 0: return("'Blue Sky'"); break;
		case 1: return("'Grassy Green'"); break;
		case 2: return("'Orange Sun'"); break;
		case 3: return("'Red Carpet'"); break;
		case 4: return("'<3 Skellefte&aring; AIK'"); break;
		case 5: return("Halloween, booo!"); break;
		case 6: return("'Grey Garden'"); break;
		case 7: return("'Purple Galaxy'"); break;
		default: return("'Some kind of haxx!!'"); break;
	}
}

/* ---------------------- */