/***
 Javascript functions for BadFellas
    www.badfellas.org  |  www.badfellas.nl
 All functions (C) 2004-2010 Zander Zoftware. All rights reserved
***/


/* GENERAL FUNCTIONS */
function checkAll(form,what)
{
	for (i=0; i<form.elements.length;i++) {
		if (form.elements[i].type == "checkbox") {
			form.elements[i].checked = what;
		}
	}
}

function setfocus(id)
{
	var x = document.getElementById(id);
	x.focus();
}

function disable(id)
{
	var x = document.getElementById(id);
	x.disabled = true;
}

function addperson (name, field)
{
	var ta = document.getElementById(field);
	ta.value = name;
}

function addpersons (name, field)
{
	var ta = document.getElementById(field);
	ta.value = ta.value + ', ' + name;
}

function copyid (cid)
{
	var cpal = document.getElementById("sendid");
	cpal.value = cid;
}

function replaceText(text, textarea)
{
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) {
		// Attempt to create a text range (IE).
		var caretPos = textarea.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		caretPos.select();
	}
	else if (typeof(textarea.selectionStart) != "undefined") {
		// Mozilla text range replace.
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text + end;

		if (textarea.setSelectionRange)
		{
			textarea.focus();
			textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
		}
		textarea.scrollTop = scrollPos;
	} else {
		// Just put it on the end.
		textarea.value += text;
		textarea.focus(textarea.value.length - 1);
	}
}


/* CARJACKING */
function ship (cid)
{
	var cpal = document.getElementById("shipid");
	cpal.value = cid;
}

function repair (money,carid) {
	if (confirm(repwarn+" € "+money) == true) {
		parent.frames['main'].location = 'cars.php?rid='+carid;
	}
}

function sellcar (carid) {
	if (confirm(selwarn) == true) {
		parent.frames['main'].location = 'cars.php?sid='+carid;
	}
}

function selship ()
{
	for (key in shipped) {
		var x = document.getElementById ('c'+shipped[key]);
		x.checked = true;
	}
}
function selunship ()
{
	for (key in unshipped) {
		var x = document.getElementById ('c'+unshipped[key]);
		x.checked = true;
	}
}


/* SMUGGLING */
function maxit (id, amount)
{
	var x = document.getElementById(id);
	x.value = amount;
}

function minit (id)
{
	var x = document.getElementById(id);
	x.value = '';
}


/* JAIL */
function putreason (reason)
{
	var jr = document.getElementById("jreason");
	jr.value = reason;
}


/* OC */
function addpersonoc (from, to)
{
	var cf = document.getElementById(from);
	var ct = document.getElementById(to);
	if (cf.selectedIndex > 0) {
		ct.value = cf.value;
		cf.selectedIndex = 0;
	}
}

function cancelwarn(ocid) {
	if (confirm(cancwarning) == true) {
		location.href='ocrime.php?cancel='+ocid;
	}
}

function kickwarn(userid) {
	if (confirm(kickwarning) == true) {
		location.href='ocrime.php?kick='+userid;
	}
}

function leavewarn(userid) {
	if (confirm(leavwarning) == true) {
		location.href='ocrime.php?kick='+userid;
	}
}


/* DRIVE-BY */
function cancdbwarn(ocid) {
	if (confirm(cancdbwarning) == true) {
		location.href='driveby.php?cancel='+ocid;
	}
}

function kickdbwarn(userid) {
	if (confirm(kickdbwarning) == true) {
		location.href='driveby.php?kick='+userid;
	}
}

function leavedbwarn(userid) {
	if (confirm(leavedbwarning) == true) {
		location.href='driveby.php?kick='+userid;
	}
}


/* INBOX */
function reportmsg (msgid) {
	if (confirm (reportwarn) == true) {
		parent.frames['main'].location = 'reportmsg.php?msg='+msgid;
	}
}


/* MENU */
function switchMenu(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}	else {
		el.style.display = '';
	}
}

function logout(logoutcode) {
	if (confirm(logoutwarn) == true) {
		parent.location='logout.php?code='+logoutcode;
	}
}


/* ONLINE PLAYERS LIST */
function hlayhide ()
{
	var hlay = document.getElementById("hlay");
	hlay.style.visibility = 'hidden';
}

function hlaytext (e, rank,clan,female,ocstat)
{
	var hlay = document.getElementById("hlay");

	if (female == 1)
		hlay.innerHTML = ranksf[rank]+'<br>'+clan;
	else
		hlay.innerHTML = ranks[rank]+'<br>'+clan;

	if (ocstat == 0)
		hlay.innerHTML += '<div id="hlayoc"><img src="img/ocred.png" alt=""></div>';
	else if (ocstat == 1)
		hlay.innerHTML += '<div id="hlayoc"><img src="img/ocgreen.png" alt=""></div>';
	else if (ocstat == 2)
		hlay.innerHTML += '<div id="hlayoc"><img src="img/ocorange.png" alt=""></div>';


	hlay.style.visibility = 'visible';
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX;
		posy = e.clientY;
	}
	if (posy < (window.innerHeight/2))
		hlay.style.top = posy-50;
	else
		hlay.style.top = posy-150;
	if (posx < (window.innerWidth/2))
		hlay.style.left = posx+10;
	else
		hlay.style.left = posx-75;
}


/* ASSETS */
function gamewarn (fid, type, code)
{
	if (confirm(droptext) == true) {
		location.href='game_drop.php?c='+fid+'&g='+type+'&code='+code;
	}
}

function dropwarn(fid, code)
{
	if (confirm(bfdroptext) == true) {
		location.href='game_drop.php?c='+fid+'&g=bf&code='+code;
	}
}

function update_total ()
{
	var bullets = document.getElementById('bullets').value;
	var each = document.getElementById('peach').value;
	document.getElementById('ptotal').value = bullets * each;
}

function update_price ()
{
	var bullets = document.getElementById('bullets').value;
	var total = document.getElementById('ptotal').value;
	document.getElementById('peach').value = Math.round (total / bullets);
}


/* POKER */
function highlight (pos)
{
	var td = document.getElementById(pos);
	td.style.backgroundColor = '#004000';
}


/* CLOCK */
function showtime()
{
	var texttoshow = "";
	if (hr < 10)
		texttoshow = texttoshow + "0";
	texttoshow = texttoshow + hr + ":";
	if (min < 10)
		texttoshow = texttoshow + "0";
	texttoshow = texttoshow + min + ":";
	if (sec < 10)
		texttoshow = texttoshow + "0";
	texttoshow = texttoshow + sec;
	document.getElementById("jst").innerHTML = texttoshow;
	sec = sec + 1;
	
	if (sec >= 60) {
		sec = sec - 60;
		min++;
	}
	if (min >= 60) {
		min = min - 60;
		hr++;
	}
	if (hr >= 24) {
		hr = hr - 24;
	}
	setTimeout("showtime()", 1000);
}


/* FORUM */
function quotepost (postdiv)
{
	var texttoquote = document.getElementById(postdiv).innerHTML;
	alert (texttoquote);
	/*texttoquote = getElementById(postdiv).innerHTML;
	alert (texttoquote);*/
}


/* REGISTRATION */
function re_username ()
{
	var myregex1 = /^[A-Z ]+$/;

	var uname = document.getElementById ('username');
	if (uname.value.toUpperCase() == uname.value) {
	//if (myregex1.test(document.regform.username.value)) {
		alert (warncapit);
	}
	if (uname.value.length < 4) {
		alert (un2short);
		document.regform.submitbtn.disabled = true;
	} else {
		document.regform.submitbtn.disabled = false;
	}
}

function re_email ()
{
	var myregex = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})$/;
	if (!myregex.test(document.regform.email.value)) {
		alert (warnmailvalid);
	}
}

function issameemail ()
{
	var e1 = document.getElementById('email1');
	var e2 = document.getElementById('email2');
	if (e1.value.toLowerCase() != e2.value.toLowerCase()) {
		alert (warnmailsame);
	}
}


/* OTHER */
/*
function scaleImage(im)
{
	var maxWidth = 300;
  if (typeof im.naturalWidth == 'undefined')
		im.naturalWidth = im.width;

  if (im.naturalWidth > maxWidth) {
    im.width = maxWidth;
    im.style.maxWidth = im.naturalWidth + 'px';
  }
}*/