/*
*
* str_replace
*
* This function returns a string or an array with all occurrences of 
* [search] in [subject] replaced with the given [replace] value.
* If you don't need fancy replacing rules (like regular expressions), you should always use this function. 
*
* last modified: 04-04-2007
* 
* @param	string	search
* @param	string	replace
* @param	string	string
*/
function str_replace (search, replace, subject)
{
  var result = "";
  var  oldi = 0;
  for (i = subject.indexOf (search)
     ; i > -1
     ; i = subject.indexOf (search, i))
  {
    result += subject.substring (oldi, i);
    result += replace;
    i += search.length;
    oldi = i;
  }
  return result + subject.substring (oldi, subject.length);
}

/*
* Dynamically resize a text field
*/
function autoSizeField(fieldId,minrows)
{
	
	var lines = document.getElementById(fieldId).value.split(/\n/).length;
	document.getElementById(fieldId).rows=lines+1;
	if(document.getElementById(fieldId).rows < minrows) document.getElementById(fieldId).rows=minrows;
	
}

/*
* Prepare our datepicker
* See http://marcgrabanski.com/code/jquery-calendar/api/ for more information
*/
popUpCal.firstDay 		= 1;
popUpCal.dateFormat 	= 'YMD-';
popUpCal.clearText 		= "Clear";
popUpCal.closeText 		= "Close";
popUpCal.prevText 		= "&lt;Prev";
popUpCal.nextText 		= "Next&gt;";
popUpCal.currentText 	= "Today";
popUpCal.dayNames 		= new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
popUpCal.monthNames 	= new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
popUpCal.speed 				= 'fast';
popUpCal.yearRange 		= '-80:+10';


/*
* Apply fancy button styles
*/
function switchButton(where,state)
{
	
	if(state == 'off')
	{
		where.className='button';
	}
	else
	{
		where.className='buttonO';
	}
	
}

function applyButtonStyles()
{
	
	var buttons = document.getElementsByTagName('input');
	for(var i=0;i<buttons.length;i++)
	{
		
		if(buttons[i].type=='submit' || buttons[i].type=='button' || buttons[i].type=='reset')
		{
			switchButton(buttons[i],'off');
			buttons[i].onmouseover=function(){switchButton(this,'on');};
			buttons[i].onmouseout=function(){switchButton(this,'off');};
			
		}
		
	}

}

/*
* Set avatar
*/
function setAvatar(id,sorc)
{
	
	try
	{
		document.getElementById('newAvatar').style.display='';
		document.getElementById('newAvatarImg').src=sorc;
		document.getElementById('pickedavatar').value=id;	
		
		tb_remove();
		
	}
	catch(ror)
	{
		alert("An error occured, please try again later\r\n" + ror);
	}
	
	
	
}


function rel(where,n) {
	if(n == 0) {window.location = where;}
	else {open(where);}
}
function addSmiley(code) {
	document.form.message.value += " "+code+" ";
}
function BBsize(where,size) {
	text = prompt("Enter your text that should be formatted with size "+size, "");
	if (!text) {return false}
	else {where.value += " [size="+size+"]" + text + "[/size] ";}
}
function BBcolor(where,color) {
	text = prompt("Enter your text that should be formatted with the color "+color, "");
	if (!text) {return false}
	else {where.value += " [COLOR="+color+"]" + text + "[/COLOR] ";}
}
function BBcustomColor(where) {
	color = prompt("Enter your color, this maybe in hex code or the color name", "");
	if (!color) {return false}
	else {
	text = prompt("Enter your text that should be formatted with the color "+color, "");
	if (!text) {return false}
	else {where.value += " [COLOR="+color+"]" + text + "[/COLOR] ";}
	}
}
function BBcustomFont(where) {
	font = prompt("Enter your font name", "");
	if (!font) {return false}
	else {
	text = prompt("Enter your text that should be formatted with the font "+font, "");
	if (!text) {return false}
	else {where.value += " [FONT="+font+"]" + text + "[/FONT] ";}
	}
}
function BBfont(where,font) {
	text = prompt("Enter your text that should be formatted with the font "+font, "");
	if (!text) {return false}
	else {where.value += " [FONT="+font+"]" + text + "[/FONT] ";}
}
function BBbold(where) {
	text = prompt("Enter your text that should be formatted bold", "");
	if (!text) {return false}
	else {where.value += " [B]" + text + "[/B] ";}
}
function BBitalic(where) {
	text = prompt("Enter your text that should be formatted italic", "");
	if (!text) {return false}
	else {where.value += " [I]" + text + "[/I] ";}
}
function BBunderline(where) {
	text = prompt("Enter your text that should be formatted underline", "");
	if (!text) {return false}
	else {where.value += " [U]" + text + "[/U] ";}
}
function BBurl(where) {
	text = prompt("Enter your text that should be displayed (optional)", "");
	if (!text) {
		url = prompt("Enter the website url", "http://");
		if (!url) {return false}
		else {where.value += " [URL]" + url + "[/URL] ";}
	}
	else {
		url = prompt("Enter the website url", "http://");
		if (!url) {return false}
		else {where.value += " [URL="+url+"]" + text + "[/URL] ";}
	}
}
function BBemail(where) {
	text = prompt("Enter your text that should be displayed (optional)", "");
	if (!text) {
		email = prompt("nter the e-mail adress", "");
		if (!email) {return false}
		else {where.value += " [EMAIL]" + email + "[/EMAIL] ";}
	}
	else {
		email = prompt("Enter the e-mail adress", "");
		if (!email) {return false}
		else {where.value += " [EMAIL="+email+"]" + text + "[/EMAIL] ";}
	}
}
function BBgoogle(where) {
	text = prompt("Enter your text that should be displayed (optional)", "");
	if (!text) {
		keywords = prompt("Enter the keyword(s)", "");
		where.value += " [GOOGLE]" + keywords + "[/GOOGLE] ";
	}
	else {
		keywords = prompt("Enter the keyword(s)", "");
		where.value += " [GOOGLE="+keywords+"]" + text + "[/GOOGLE] ";
	}
}
function BBquote(where) {
	text = prompt("Enter your the persons name you will be quoting", "");
	if (!text) {
		return false
	}
	else {
		keywords = prompt("Enter your text", "");
		where.value += " [QUOTE="+keywords+"]" + text + "[/QUOTE] ";
	}
}
function BBcode(where) {
	text = prompt("Enter your text that should be formatted as code", "");
	if (!text) {return false}
	else {where.value += " [CODE]" + text + "[/CODE] ";}
}
function BBphp(where) {
	text = prompt("Enter your text that should be formatted as PHP code", "");
	if (!text) {return false}
	else {where.value += " [PHP]" + text + "[/PHP] ";}
}
function BBimage(where) {
	text = prompt("Enter the location of your image", "http://");
	if (!text) {return false}
	else {where.value += " [IMG]" + text + "[/IMG] ";}
}
	  
function showSquadPhotos(squadid,totalsquads)
{
	
	var i = 0;
	
	while(i < totalsquads)
	{
		i++;
		document.getElementById('squadphotos'+i).style.display = 'none';
	}
	
	document.getElementById('squadphotos'+squadid).style.display = 'block';

}



function showMemberName(name)
{
	document.getElementById('squadphotosname').innerHTML = name;
}	  



function checkAll() {
	if(document.form.elements.length > 0) {
		for (i=0; i<document.form.elements.length; i++) {
			e = document.form.elements[i];
			if ( ( e.name != 'checkall' ) && ( e.type == 'checkbox' )) {
				e.checked = document.form.checkall.checked;
			}
		}
	} else {
		 document.form.box.checked = document.form.checkall.checked;
	}
}
function moveChecked() {
	var ToSend = '';
	if(document.form.elements.length > 0) {
		for(i=0; i<document.form.elements.length; i++) {
			e = document.form.elements[i];
			if(( e.name != 'checkall' ) && ( e.type == 'checkbox' ) && (document.form.elements[i].checked == true) ) {
				ToSend += document.form.elements[i].value +',';
			}
		}
	}
	else {
		ToSend = document.form.box.value
	}
	document.form.selectedlist.value = ToSend;
}

var wnr = 0;

function popup(url,width,height,scroll,left,top) 
{       
	wnr++;
	popupWin = window.open(url,'extern'+wnr,'width='+width+',height='+height+',left='+left+',top='+top+',scrollbars='+scroll);
	popupWin.focus();
}

function profile(program,username) 
{
	popup('index.php?module=profiles&action=messengerdetails&type=' + program + '&id=' + username + '', 400, 150, 'auto', 40, 40);
}		 


// ajax functions
// #############################
var temp_value = new Array();

function changeTopicStatus(id)
{
	temp_value[id] = document.getElementById(id).innerHTML;
	document.getElementById(id).innerHTML = '<img src="templates/Default/images/progress.gif" alt="Loading" />';

	makePOSTRequest('?module=forum&action=changetopicstatus','&tid='+id);
	awaitAjaxResult('changeTopicStatusConfirmed("'+id+'")',id);
	
}

function quickedit(id)
{
	document.getElementById(id).innerHTML = '<img src="templates/Default/images/progress.gif" alt="" /> Fetching topic title...';
	makePOSTRequest('?module=forum&action=gettopictitle','tid='+id);
	temp_value[id] = document.getElementById(id).innerHTML;
	awaitAjaxResult('getTopicTitleConfirmed("'+id+'")',id);
}

function getTopicTitleConfirmed(id)
{
	var title = returnresult;
	returnresult = '';
	temp_value[id] = document.getElementById(id).innerHTML;
	document.getElementById(id).innerHTML = '<input maxlength="100" id="ecn' + id + '" type="text" value="' + title + '" class="inlineditfield"/> <img src="templates/Default/images/save.gif" alt="" class="vm" style="cursor: pointer" onclick="process_quickedit(\'' + id + '\')"/>';

}

function process_quickedit(id)
{
	var text = document.getElementById('ecn'+id).value;
	document.getElementById(id).innerHTML = '<img src="templates/Default/images/progress.gif" alt="Saving" /> Saving...';
	makePOSTRequest('?module=forum&action=changetopictitle','tid='+id+'&title=' + text);
	awaitAjaxResult('quickeditConfirmed("'+id+'")',id);
}

function quickeditConfirmed(id,newtitle)
{
	if(returnresult == 'failure')
	{
		alert("You are not allowed to perform this action");
		alert(temp_value[id]);
		document.getElementById(id).innerHTML = temp_value[id];
	}
	else
	{
		document.getElementById(id).innerHTML = returnresult;
	}
	returnresult = '';
	
}




// this is what does .... the... ..uh ...  
// waiting and performs confirmation
function awaitAjaxResult(callSucces,id)
{
	if(returnresult == "")
	{
		setTimeout("awaitAjaxResult(''+callSucces+'',''+id+'')",500); // try again in 0.5 second
	}
	else
	{
	
		if(returnresult == "false" ) // oeeeeeeeeeeeeh, access DENIED!
		{
			alert("You are not authorized to perform this command");
			document.getElementById(id).innerHTML = temp_value[id];
			temp_value[id] = false;
		}
		else
		{
			setTimeout(callSucces,500);	
		}
		
		
	}
	
}


// and up next is the handling of data sent to us!
function changeTopicStatusConfirmed(id)
{

	if(returnresult == "closed")
	{
		icon = '<img src="templates//images/topicnonewlocked.gif" alt="Closed"/>';
		txt = "This topic is closed, click here to re-open.";
	}
	else if(returnresult == "open")
	{
		icon = '<img src="templates//images/topicnonew.gif" alt="Open"/>';
		txt = "This topic is open, click here to close.";	
	}
	else
	{
		alert("You are not allowed to perform this action");
		icon = '<img src="templates//images/topicnonew.gif" alt="Open"/>';
		txt = "This topic is closed, click here to re-open.";	
	}
		
	document.getElementById(id).innerHTML = icon + "<span>"+txt+"</span>";
	returnresult = "";
		
}

/* GENERAL FORM FUNCTIONS */
/* ############################## */
var oldValues = Array();
function clearValue(where)
{
	if(!oldValues[where]) 
	{
		oldValues[where] = where.value;
		where.value = "";
	}
	else
	{
		if(where.value == oldValues[where])
		{
			where.value = "";
		}
	}
	
}

/* ############################## */


/* SHOUTBOX FUNCTIONS */
/* ############################## */
function sbSmilie(code)
{
	document.getElementById("s_message").value += code;
}

function sbControl(where)
{
	var Errors = "";
	if(document.getElementById(where + "_name").value.length < 2)
	{
		Errors += "- Please enter a name that is at least 2 characters in length\r\n";
	}
	if(document.getElementById(where + "_email").value.length < 5)
	{
		Errors += "- Your e-mail address should be at least 5 characters in length\r\n";
	}

	if(document.getElementById(where + "_message").value.length < 5)
	{
		Errors += "- Your message should be at least 5 characters in length\r\n";
	}
	if(Errors.length > 0)
	{
		alert("While trying to add your shout, I encountered the following errors:\r\n" + Errors);
		return false;
	}
	
	return true;
	
}
/* ############################## */

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
function Smaller() 
{
	textarea = document.getElementById("message");
	if(textarea.rows > 10) {
		textarea.rows = textarea.rows-5;
	}
}
function Bigger() 
{
	textarea = document.getElementById("message");
	if(textarea.rows < 30) {
		textarea.rows = textarea.rows+5;
	}
}

function CheckPostFields() 
{

	var al = "";
	var newline = "\r\n";
	var errors = 0;

	if(parseInt(document.getElementById("message").value.length) > 50000)
	{
		al += "- Your message is too short! please use a maximum of 50 000 charachters."+newline;
		errors = errors+1;
	}
	if(parseInt(document.getElementById("message").value.length) < 6)
	{
		al += "- Your message is too short! please use at least 6 charachters.";
		errors = errors+1;
	}
	
	if(errors > 0) 
	{
		alert("The following errors were encountered."+newline+al);
		return false;
	}
	else
	{
		return true;
	}
}
function addSmiley(code) 
{
	document.getElementById("message").value += " " + code + " ";
}