function parseForDatabase(a)
{
	var b="";
	for (var i=0; i<a.length; i++)
	{
		l = a.charAt(i);
		c = a.charCodeAt(i);
		if (c==10)
			b = b + "<BR>";
		else if (l=="'")
			b = b + "\'";
		else if (c!=13)
			b = b + l;
	}
	return b;
}

function writeTo(id, text)
{
  	x = findElement(id);
  	if (x) {
		x.innerHTML = '';
		x.innerHTML = "<font class=text>" + text;
	}
}

function showContent(url)
{
	top.icontent.location = url;
	top.document.getElementById("closeDivContent").style.display="";
}

function showContentWithTitle(title,url) 
{
	setTitle(title);
	top.icontent.location = url;
	top.document.getElementById("closeDivContent").style.display="";
}

function setTitle(txt) {
  //x = findRootElement('title');
  //x.innerHTML = txt;
}

function setTitleWiki(txt) {
  key = txt;
  key = key.replace("'", "");
  txt = txt + " <a onclick='window.open(\"wiki.php?key=" + key + "\", \"_blank\", \"height=400,width=400,status=no,toolbar=no,resizable=yes,menubar=no,location=no,directories=no\");'><img src='images/wiki1.png' border=0></a>";
  setTitle(txt);
}

function wiki(key) {
  	key = key.replace("'", "");
  	return " <a onclick='window.open(\"wiki.php?key=" + key + "\", \"_blank\", \"height=400,width=400,status=no,toolbar=no,resizable=yes,menubar=no,location=no,directories=no\");'><img src='images/wiki1.png' border=0></a>";   
}

function wikiTiny(key) {
  	key = key.replace("'", "");
  	return " <a onclick='window.open(\"wiki.php?key=" + key + "\", \"_blank\", \"height=400,width=400,status=no,toolbar=no,resizable=yes,menubar=no,location=no,directories=no\");'><img src='images/wiki2.png' border=0></a>";   
}

function findElement(id) {
    x = false;
 	if (document.getElementById)
		x = document.getElementById(id);
	else if (document.all)
		x = document.all[id];
	return x;
}

function findRootElement(id) {
  	x = false;
	if (top.document.getElementById)
		x = top.document.getElementById(id);
	else if (top.document.all)
		x = top.document.all[id];
	return x;	  	
}

function findElementIn(container, id) {
    x = false;
 	if (container.document.getElementById)
		x = container.document.getElementById(id);
	else if (container.document.all)
		x = container.document.all[id];
	return x;
}

function findLastForm(fname) {
	for (var i=document.forms.length-1; i>=0; i--)
		if (document.forms[i].name == fname)
			return document.forms[i];
	return null;
}

function findElementInForm(f, e) {
	for (j=0; j<f.elements.length; j++) {
		if (f.elements[j].name == e)
			return f.elements[j];
	}
	return false;
}

function showGang(id,sect,extraParams) { 
   //top.icontent.location = "gang.php?id=" + id + "&s=" + sect; 
   var url = "gang.php?id=" + id + "&s=" + sect;
   if (arguments.length == 3)
		url += "&" + extraParams;
   showContent(url);
   //showDivContent("gang.php?id=" + id,"",true);
}

function showUser(id) { 
	//top.icontent.location = "user.php?id=" + id; 
	showContent("user.php?id=" + id);
	//showDivContent("user.php?id=" + id,"",true);
}

function showCamp(id) { 
	//top.icontent.location = "camp.php?id=" + id; 
	showContent("camp.php?id=" + id);
	//showDivContent("camp.php?id=" + id,"",true);
}

function parseRequestString(s) {
	// expand weird characters from cookies, etc.
	var t="";
	for (var i=0; i<s.length; i++) {
	  l = s.charAt(i);
	  done = 0;
	  if (l=="%") {
	  		a = s.charAt(i+1);
			b = s.charAt(i+2);
			if (a=="2" && b=="7") {
			  done = 1;
			  t = t + "'";
			  i += 2;
			}		    
	   }
	   if (l=="+")  {
	     	done = 1;
	     	t = t + " ";
		}
	   if (done==0)
	   		t = t + l;
	}
	return t;
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function showCharacter(pk,arg) {   
	var win = showWindowURL("Character","character.php?id=" + pk + "&arg=" + arg,350,400,0,0,false);
}

function showCampDonations(camp,gang,gangname) {
	var win = showWindowURL(gangname,"showCampDonations.php?camp=" + camp + "&gang=" + gang,300,300,0,0,false);
}

function showCharacterNoWindow(pk,arg) {
	top.icontent.icharacter.location = "character.php?id=" + pk + "&arg=" + arg; 
}

function showHeropointWindow(town,tname) {
	var win = showWindowURL("Hero Points - " + tname,"useHeroPoints.php?town=" + town,400,250,0,0,false);
}

function showMarketNotes(type,vname,cname,pk) {
	var extra = "";
	if (vname=="")
		extra = "&lookup=1";
	var win = showWindowURL("Seller Notes - " + cname + " '" + vname + "'","marketNotes.php?type="+type+"&pk="+pk+extra,400,450,0,0,false);
}

function showVehicle(pk,arg,extraParams) {  
	url = "vehicle.php?id=" + pk + "&arg=" + arg;
	if (arguments.length == 3) // extraParams is optional
		url += extraParams;
	var win = showWindowURL("Vehicle",url,600,500,0,0,false);
}

function showVehicleV(pk,arg) {
	showVehicle(pk,arg);	
}

function showVehicleNoWindow(pk,arg) {
	top.icontent.icharacter.location = "vehicle.php?id=" + pk + "&arg=" + arg;  
}

function showSquad(pk) {
	/*if (top.icontent.ivehicle==undefined)
		top.icontent.iundermap.ivehicle.location = "squad.php?id=" + pk; 
	else
		top.icontent.ivehicle.location = "squad.php?id=" + pk;    */
	//var win = showWindowURL("Squad","squad.php?id=" + pk,500,300,0,0,false);
	top.icontent.location = "squadFrames.php?id=" + pk;
}

function doSignup() {
	theForm = findLastForm('frmSignup');
	theForm.txtName.value = parseForDatabase(theForm.txtName.value);
	theForm.txtLogin.value = parseForDatabase(theForm.txtLogin.value);
	theForm.txtEmail.value = parseForDatabase(theForm.txtEmail.value);
	theForm.txtCEmail.value = parseForDatabase(theForm.txtCEmail.value);
	if (theForm.txtEmail.value != theForm.txtCEmail.value) {
	 	alert("Please re-check your email address, as your confirm e-mail does not match!");
		return; 
	}
	theForm.txtGangName.value = parseForDatabase(theForm.txtGangName.value);
	theForm.submit();
}

function hideIContent() {
	top.icontent.location = "blank.html";
	top.document.getElementById("closeDivContent").style.display="none";
}

/*function showContent(url,disallowFloat) {
	document.getElementById("divContent").style.display="";
	document.getElementById("closeDivContent").style.display="";
	//document.getElementById("prevDivContent").style.display="";
	img = document.getElementById("imgFloat");
	if (disallowFloat) {
		img.style.width="0px";
		img.style.height="0px";
	}
	else {
		img.style.width="30px";
		img.style.height="28px";	
	}
	document.getElementById("icontent").src=url;
}

*/

function prevContent() {
	frames["icontent"].history.back();
}

/*function floatContent() { // display in a window and close div itself. only certain pages allow this
	url = frames["icontent"].location.href;
	var title;
	var ok=false;
	if (url.indexOf("forums2")>0) {
		title = "Darkwind Forums";
		ok = true;
	}
	if (url.indexOf("instructions.php")>0) {
		title = "Darkwind Game Information";
		ok = true;
	}
	if (ok) {
		showWindowURL(title,url,800,550,0,0,false);	
		hideIContent();
	}
}*/

function setWindowTitle(id,title) {
	if (Windows != null)
		if  (Windows.getWindow(id) != null)
			Windows.getWindow(id).setTitle(title + "."); // extra fullstop is for a firefox bug
}

function autoSizeWindow(id) {
	// only works with ajax windows
	if (Windows != null) {
		var win = Windows.getWindow(id);
		if  (win != null) {
			win.updateHeight();
			win.updateWidth();
		}
	}
}

function showWindowURL(title,url,width,height,left,top,useAjax) { // centres on screen if nonezero left and top not supplied
	var win = new Window({className: "mac_os_x", top:55, left:170, showEffectOptions: {duration:0.5}, wiredDrag: true, maximizable: false});
	
	if (url.indexOf("?")>0)
		url += "&";
	else
		url += "?";
	url += "winId=" + win.getId();
	
	if (useAjax)
		win.setAjaxContent(url);
	else
		win.setURL(url); // for content that's not happy to be in a div
	win.setTitle(title + "."); // extra fullstop is for a firefox bug
	win.show(); 
	if (width>0 && height>0)
		win.setSize(width,height);
	if (left>0 && top>0)
		win.setLocation(top,left);
	else {
		win._center();
		var loc = win.getLocation(); 
		if (parseFloat(loc.top) < 65) {
			win.setLocation(65,parseFloat(loc.left));
		}
	}
	return win;
}

// recent/bookmarked items lists in menus
function addRecentChar(pk,name) {
	var l = recCharsName.length; 
	// check it's not already there
	for (i=0; i<l; i++) {
		if (recCharsPK[i]==pk)
			return;
	}
	if (l >= 10) { // make space in list
		menu.removeItem("menu","charsMenu","char"+recCharsPK[0]);
		for (i=0; i<l-1; i++) {
			recCharsName[i]=recCharsName[i+1];
			recCharsPK[i]=recCharsPK[i+1];
		}
		l--;
	}
	// add item
	recCharsName[l] = name;
	recCharsPK[l] = pk;
	menu.addItem("menu","charsMenu","<a href='javascript:showCharacter(" + pk + ",0);'>" + name + "</a>","char"+pk);
}

function removeVehFromMenu(n) {
	var l = recVehsName.length; 
	menu.removeItem("menu","vehiclesMenu","veh"+recVehsPK[n]);
	for (i=n; i<l-1; i++) {
		recVehsName[i]=recVehsName[i+1];
		recVehsPK[i]=recVehsPK[i+1];
	}
	recVehsName.length--;
	return l-1;
}

function addRecentVeh(pk,name) {
	var l = recVehsName.length; 
	// check it's not already there
	for (i=0; i<l; i++) {
		if (recVehsPK[i]==pk) {
			if (recVehsName[i]!=name) {
				l = removeVehFromMenu(i);
				break;
			}
			else
				return;
		}
	}
	if (l >= 10) { // make space in list
		l = removeVehFromMenu(0);
	}
	// add item
	recVehsName[l] = name;
	recVehsPK[l] = pk;
	menu.addItem("menu","vehiclesMenu","<a href='javascript:showVehicle(" + pk + ",0);'>" + name + "</a>","veh"+pk);
}

function addRecentSquad(pk,name) {
	var l = recSquadsName.length; 
	// check it's not already there
	for (i=0; i<l; i++) {
		if (recSquadsPK[i]==pk)
			return;
	}
	if (l >= 10) { // make space in list
		menu.removeItem("menu","squadsMenu","sq"+recSquadsPK[0]);
		for (i=0; i<l-1; i++) {
			recSquadsName[i]=recSquadsName[i+1];
			recSquadsPK[i]=recSquadsPK[i+1];
		}
		l--;
	}
	// add item
	recSquadsName[l] = name;
	recSquadsPK[l] = pk;
	menu.addItem("menu","squadsMenu","<a href='javascript:showSquad(" + pk + ");'>" + name + "</a>","sq"+pk);
}

function clearBookmarks() {
	// clears all bookmarks
	var l = bkGangsName.length; 
	for (i=0; i<l; i++) {
		menu.removeItem("menu","gangsMenu","gang"+bkGangsPK[i]);
	}	
	bkGangsName.length = 0;
	bkGangsPK.length = 0;
}

function bookmarkGang(pk,name) {
	var l = bkGangsName.length; 
	// check it's not already there
	for (i=0; i<l; i++) {
		if (bkGangsPK[i]==pk)
			return;
	}	
	// add item
	bkGangsName[l] = name;
	bkGangsPK[l] = pk;
	menu.addItem("menu","gangsMenu","<a href='javascript:showGang(" + pk + ",1);'>" + name + "</a>","gang"+pk);
	// save in database
	top.phptarget.location = "bookmarkGang.php?action=add&bkGang=" + pk;
}

function unBookmarkGang(pk) {
	var l = bkGangsName.length; 
	for (i=0; i<l; i++) {
		if (bkGangsPK[i]==pk) {
			menu.removeItem("menu","gangsMenu","gang"+pk);
			top.phptarget.location = "bookmarkGang.php?action=del&bkGang=" + pk;
			for (j=i; j<l-1; j++) {
				bkGangsName[j]=bkGangsName[j+1];
				bkGangsPK[j]=bkGangsPK[j+1];
			}
			bkGangsName.length = l-1;
			bkGangsPK.length = l-1;
			return;
		}
	}		
}

function isGangBookmarked(pk) {
	var l = bkGangsName.length; 
	for (i=0; i<l; i++) {
		if (bkGangsPK[i]==pk) {
			return true;
		}
	}
	return false;
}

function toggleGangBookmark(pk,name) {
	var msg;
	if (isGangBookmarked(pk)) {
		unBookmarkGang(pk);
	}
	else {
		bookmarkGang(pk,name);
	}
}
//

// window functions
function makeDialog() {
	var win = new Window({className: "dialog",  width:250, height:150, zIndex: 100, resizable: true, title: "Sample Window", showEffect:Effect.BlindDown, hideEffect: Effect.SwitchOff, draggable:true, wiredDrag: true, zIndex: -1})
	win.getContent().update(document.frmTest.txtHTML.value); 
	win.showCenter(); 
}

function makeWindow() {
	var win = new Window({className: "mac_os_x", title: "Sample", width:200, height:150, destroyOnClose: true, recenterAuto:false});
	win.getContent().update(document.frmTest.txtHTML.value); 
	win.showCenter(); 
}

function makeAlert() {
	var txt = document.frmTest.txtHTML.value;
	Dialog.alert(txt, {className: "alert", buttonClass: "myButtonClass", id: "myDialogId", title: "Alert!", width:300, okLabel: "OK", ok:function(win) {return true;}}); 
}

function okClicked() {
	alert("OK clicked");
}

function cancelClicked() {
	alert("Cancel clicked");
}

function makeConfirm() {
	var txt = document.frmTest.txtHTML.value;
	Dialog.confirm(txt, {width:300, okLabel: "OK", buttonClass: "myButtonClass", id: "myDialogId", cancel:function(win) {cancelClicked(); return false;}, ok:function(win) {okClicked(); return true;} });
}

function showTownCampPanel() {
	var w = 1024;
	if (window.innerWidth)
		w = window.innerWidth;
	else if (document.all)
		w = document.body.clientWidth;
		
	l = w - 200;
	t = 70;
	
	showWindowURL("","listTownsAndCamps.php",100,250,l,t,true);
}

function preloadImages(path, images) {
  $A(images).each(function(i) {
    var image = new Image(); 
    image.onload = function() {
    }
    image.src= path + "/" + i;
  })
}




	
	


