var request = null;
var error_handler = '';
var ajax_core_defined = 1;

// A quick function for all those changes for innertext, firefox capable yay!!
var ie = document.all && navigator.userAgent.indexOf("Opera")==-1;

function aa(a,b)
{
	if(document.getElementById)
	{
		document.getElementById(a).innerHTML = b;
	}
	else if(document.all)
	{
		obj = document.all[a];
	}
	else if(document.layers)
	{
		document.all[a].document.write(b);
		document.all[a].document.close();
	}
	else
	{
		return 1;
	}
}

function ad(a)
{
	var obj = "";
	if(document.getElementById)
	{
		obj = document.getElementById(a);
	}
	else if(document.all)
	{
		obj = document.all[a];
	}
	else if(document.layers)
	{
		obj = document.layers[a];
	}
	else
	{
		return 1;
	}
	return obj;
}

function ih()
{
	aa('loading',' ');
}

function ii()
{
	if (!ie)
	{
		ad('loading').style.position = 'fixed';
	}
	else
	{
		ad('loading').style.position = 'absolute';
	}
	if (ie||document.getElementById)
	{
		aa('loading','&nbsp;Loading...&nbsp;');
	}
	else if (document.layers)
	{
		ad('loading').document.write('&nbsp;Loading...&nbsp;');
		ad('loading').document.close();
	}
}

//
// General function. This one is the mother of all AJAX functions ;)
//
function loadXMLDoc(content_url, changehandler)
{
	//Use the native object available in Mozilla, Safari, ...
	if (window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
		eval("request.onreadystatechange = "+changehandler);
		request.open("GET", content_url, true);
		request.send(null);
	}
	//Use the IE/Windows ActiveX version
	else if (window.ActiveXObject)
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (request)
		{
			eval("request.onreadystatechange = "+changehandler);
			request.open("GET", content_url, true);
			request.send();
		}
	}
}


//
// This function is used to parse any standard error file
function error_req_change()
{
	//Check if the request is completed, if not, just skip over
	if (request.readyState == 4)
	{
		result_code = 0;
		error_msg = '';
		//If the request wasn't successful, we just hide any information we have.
		if (request.status == 200)
		{
			response = request.responseXML.documentElement;
			//Don't react if no valid response was received
			if (response != null)
			{
				result_code_ar = response.getElementsByTagName('result');
				if (result_code_ar.length > 0)
				{
					result_code = result_code_ar[0].firstChild.data;
				}
				
				if (result_code != 0)
				{
					//No need to look for an error message, 0 means we don't have one ;)
					error_msg_ar = response.getElementsByTagName('error_msg');
					if (error_msg_ar.length > 0)
					{
						error_msg = error_msg_ar[0].firstChild.data;
					}
				}
			}
		}
		eval(error_handler+"(result_code, error_msg);");
		delete request;
	}
}

//
// Just like sprintf() in php
// replacements can be any type
//
function sprintf(text, replacements)
{
	var i = 0;
	//This prevents us from having to create an array for replacements with one value
	//checking for type 'object' may not be really smart, but who cares ;)
	if ((typeof replacements) != 'object')
	{
		repl = Array(1);
		repl[0] = replacements;
	}
	else
	{
		repl = replacements;
	}
	
	while (((charindex = text.indexOf('%s')) >= 0) && (i < repl.length))
	{
		temptext = text.substr(0, charindex);
		text = temptext + repl[i] + text.substr(charindex+2, text.length);
		i++;
	}
	
	return text;
}

function select_switch_groupcp_info_members()
{
	for (i = 0; i < document.getElementById('post_pending').length; i++)
	{
		if ( document.getElementById('post_pending').elements[i].checked == true )
		{
			document.getElementById('post_pending').elements[i].checked = false;
		}
		else
		{
			document.getElementById('post_pending').elements[i].checked = true;
		}
	}
}

function select_switch_groupcp_info_members()
{
	for (i = 0; i < document.getElementById('post_members').length; i++)
	{
		if ( document.getElementById('post_members').elements[i].checked == true )
		{
			document.getElementById('post_members').elements[i].checked = false;
		}
		else
		{
			document.getElementById('post_members').elements[i].checked = true;
		}
	}
}

function open_chat()
{
	window.open('http://chat.warez-bb.org/chat/chat.html','Warez-BB IRC','height=400,width=650,scrollbars=no,toolbar=no');

	if (window.focus) 
	{
		newwindow.focus()
	}

	return false;
}

function bookmarksite(title, url) 
{
	if (document.all) 
	{
		window.external.AddFavorite(url, title);
	}
	else if (window.sidebar) 
	{
		window.sidebar.addPanel(title, url, "")
	}
}

/* Current time on the users computer
*/
var Offset = 0;
var Offset_initial = new Date();
var Offset_initial = Date.parse(Offset_initial);

/* Main dating function
*/
function DateDiff() 
{ 
	/* Calculate the time it is now
	*/
	Offset_now = new Date();
	Offset_now = Date.parse(Offset_now);

	/* Compare initial time to the current time to find difference
	*/
	Offset = (Offset_now - Offset_initial) / 1000;

	/* Calculate days, hours, etc to count down to
	*/
	Days = Math.floor((WADIO_NEXT_NT - WADIO_TIME - Offset) / 86400);
	Hours = (Math.floor(((WADIO_NEXT_NT - WADIO_TIME - Offset) % 86400) / 3600)) + (Days * 24);
	Min = Math.floor((((WADIO_NEXT_NT - WADIO_TIME - Offset) % 86400) % 3600) / 60);
	Sec = Math.floor(((((WADIO_NEXT_NT - WADIO_TIME - Offset) % 86400) % 3600) % 60) / 1);
		
	/* If the time has already passed we'll have a new message instead of a negative count down
	*/
	if ((Days <= 0) && (Hours <= 0) && (Min <= 0)) {
		aa('wadio_now', '<a href="http://www.wadiomedia.org" alt="Wadio, Internet Broadcasting Media">Wadio (Official Radio Station)</a> now playing ' + WADIO_NEXT_NP + ' by ' + WADIO_NEXT_DJ);
		ad('wadio_next').display = 'none';
	} else {
		aa('wadio_next', "Next <b>live</b> show in " + Hours + " Hours " + Min + " Minutes  &  " + Sec + " Seconds. <a href='http://www.wadiomedia.org' alt='Wadio, the Official Radio Station'>Make sure you tune in!</a>"); 
	}

	/* Run the same function again to calculate the new time
	*/
	window.setTimeout("DateDiff();", 1000);
}

function safeLinkingOptions(mode)
{
	var slOptions = document.getElementById('sl-options');
	var changeCaptcha = document.getElementById('choose-captcha');
	var passwordPrefs = document.getElementById('set-password');
	var setPassword = document.getElementById('password-field');
	var safelinkingPrefs = document.getElementById('sl-options');
	var enableCaptchaCheckbox = document.getElementById('toggle-captcha');
	var captchaEnabled = document.getElementById('enable-captcha');
	var rsdf_container = document.getElementById('rsdf-container');
	var click_container = document.getElementById('click-container');
	var viewLiveLinks = document.getElementById('view-live-links');
	var dlc_container = document.getElementById('dlc-container');
	var clk2_container = document.getElementById('click2-container');
	var protect_individual_links = document.getElementById('protect-individual-links');
	var show_link_name = document.getElementById('show-link-name');
	
	if(mode == 'showprefs')
	{
		if(slOptions.style.display == 'none')
		{
			slOptions.style.display = 'block';
			passwordPrefs.style.display = 'block';
			rsdf_container.style.display = 'block';
			click_container.style.display = 'block';
			viewLiveLinks.style.display = 'block';
			dlc_container.style.display = 'block';
			clk2_container.style.display = 'block';
			protect_individual_links.style.display = 'block';
			show_link_name.style.display = 'block';		
			
			if(enableCaptchaCheckbox.checked == true)
			{
				if(changeCaptcha.style.display == 'none')
				{
					captchaEnabled.style.display = 'block';
				}
			}
		}
		else
		{
			slOptions.style.display = 'none';
			captchaEnabled.style.display = 'none';	
			changeCaptcha.style.display = 'none';
			passwordPrefs.style.display = 'none';
			setPassword.style.display = 'none';
			rsdf_container.style.display = 'none';
			click_container.style.display = 'none';
			viewLiveLinks.style.display = 'none';
			dlc_container.style.display = 'none';
			clk2_container.style.display = 'none';
			protect_individual_links.style.display = 'none';
			show_link_name.style.display = 'none';	
		}
	}
	else if(mode == 'togglecaptcha')
	{
		if(enableCaptchaCheckbox.checked == true)
		{
			captchaEnabled.style.display = 'block';
		}
		else
		{
			captchaEnabled.style.display = 'none';
			changeCaptcha.style.display = 'none';
		}
	}
	else if(mode == 'changecaptcha')
	{
		if(changeCaptcha.style.display == 'none')
		{
			changeCaptcha.style.display = 'block';
		}
		else
		{
			changeCaptcha.style.display = 'none';
		}
	}
	else if(mode == 'password')
	{
		if(setPassword.style.display == 'none')
		{
			setPassword.style.display = 'block';
		}
		else
		{
			setPassword.style.display = 'none';
		}
	}
}
