/*

xProfile - Copyright by Markup (icewindrain@yahoo.co.uk)
Edited by Paper - Copyright by Paper

*/


if(document.getElementById('userlinks').innerHTML.match("Admin CP") != null)
{

document.getElementById('userlinks').rows[0].cells[0].innerHTML += "<span id='domain'>&nbsp;·&nbsp;(&nbsp;<a href='"+myurl+"index.php?xprofile'><b>XProfile CP</b></A>&nbsp;)</span>"


  if (document.location.href==myurl+"index.php?xprofile") {
    function loaded() {
      document.getElementById("content").innerHTML='<p>Please make alterations to this table and then save<table border="1" cellpadding="5"><tr><td><b>Features</b></td><td><b>Label prefix</b></td><td><b>Label suffix</b></td><td><b>Value prefix</b></td><td><b>Value suffix</b></td><td><b>Where shown</b></td></tr><tr><td><input id="age1" type="checkbox" checked="checked" />Age</td><td><input id="age2" type="text" value="<B>" /></td><td><input id="age3" type="text" value="</B>" /></td><td><input id="age4" type="text" value="<I>" /></td><td><input id="age5" type="text" value="</I>" /></td><td><select id="age6"><option VALUE="Both">Both</option><option VALUE="Profile">Profile</option><option VALUE="Posts">Posts</option></select></td></tr><tr><td><input id="location1" type="checkbox" checked="checked" />Location</td><td><input id="location2" type="text" value="" /></td><td><input id="location3" type="text" value="" /></td><td><input id="location4" type="text" value="<FONT color=red>" /></td><td><input id="location5" type="text" value="</FONT>" /></td><td>&nbsp;</td></tr><tr><td><input id="flags1" type="checkbox" checked="checked" />Flags</td><td><input id="flags2" type="text" value="<SPAN style=display:none>" /></td><td><input id="flags3" type="text" value="</SPAN>" /></td><td><input id="flags4" type="text" value="" /></td><td><input id="flags5" type="text" value="" /></td><td><select id="flags6"><option VALUE="Both">Both</option><option VALUE="Profile">Profile</option><option VALUE="Posts">Posts</option></select></td></tr><tr><td><input id="sound1" type="checkbox" checked="checked" />Sound in the profiles</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table><br><p><b>i = 0<br>newField = []</b><br>You can enter more options here, based on the guide from <a href="http://s7.invisionfree.com/markup/index.php?showtopic=215" target="_blank">here</a><br>Please look at the text in the box below to get the idea of the format. You must include ; at the end of each custom xprofile entry</p>Custom: <input id="custom1" type="text" value="" /><br><br><input id="submit" type="button" value="Save" OnClick="sxprofile()" />';

    document.getElementById('age1').checked=xage;
    document.getElementById('age2').value=AlabelPrefix;
    document.getElementById('age3').value=AlabelSuffix;
    document.getElementById('age4').value=AvaluePrefix;
    document.getElementById('age5').value=AvalueSuffix;
    document.getElementById('age6').value=AwhereShown;

    document.getElementById('location1').checked=xlocation;
    document.getElementById('location2').value=LlabelPrefix;
    document.getElementById('location3').value=LlabelSuffix;
    document.getElementById('location4').value=LvaluePrefix;
    document.getElementById('location5').value=LvalueSuffix;

    document.getElementById('flags1').checked=xflags;
    document.getElementById('flags2').value=FlabelPrefix;
    document.getElementById('flags3').value=FlabelSuffix;
    document.getElementById('flags4').value=FvaluePrefix;
    document.getElementById('flags5').value=FvalueSuffix;
    document.getElementById('flags6').value=FwhereShown;

    document.getElementById('sound1').checked=xsound;

    document.getElementById('custom1').value=xcustom;
    }

  function saved() {
    document.getElementById("content").innerHTML="<P>The wrappers were successfuly saved, and the code has been updated</p>";
  }
    load();
  }

}


function sxprofile() {
var quotechar='"';

var savexprofile='var beginxprofile; ';
savexprofile+= 'var xage=' + document.getElementById("age1").checked + '; ';
savexprofile+= 'var AlabelPrefix = ' + quotechar + document.getElementById("age2").value + quotechar + '; ';
savexprofile+= 'var AlabelSuffix = ' + quotechar + document.getElementById("age3").value + quotechar + '; ';
savexprofile+= 'var AvaluePrefix = ' + quotechar + document.getElementById("age4").value + quotechar + '; ';
savexprofile+= 'var AvalueSuffix = ' + quotechar + document.getElementById("age5").value + quotechar + '; ';
savexprofile+= 'var AwhereShown = ' + quotechar + document.getElementById("age6").value + quotechar + '; ';
savexprofile+= 'var xlocation='+ document.getElementById("location1").checked + '; ';
savexprofile+= 'var LlabelPrefix =' + quotechar + document.getElementById("location2").value + quotechar + '; ';
savexprofile+= 'var LlabelSuffix =' + quotechar + document.getElementById("location3").value + quotechar + '; ';
savexprofile+= 'var LvaluePrefix =' + quotechar + document.getElementById("location4").value + quotechar + '; ';
savexprofile+= 'var LvalueSuffix =' + quotechar + document.getElementById("location5").value + quotechar + '; ';
savexprofile+= 'var xflags='+ document.getElementById("flags1").checked + '; ';
savexprofile+= 'var FlabelPrefix = ' + quotechar + document.getElementById("flags2").value + quotechar + '; ';
savexprofile+= 'var FlabelSuffix = ' + quotechar + document.getElementById("flags3").value + quotechar + '; ';
savexprofile+= 'var FvaluePrefix = ' + quotechar + document.getElementById("flags4").value + quotechar + '; ';
savexprofile+= 'var FvalueSuffix = ' + quotechar + document.getElementById("flags5").value + quotechar + '; ';
savexprofile+= 'var FwhereShown = ' + quotechar + document.getElementById("flags6").value + quotechar + '; ';
savexprofile+= 'var xsound='+ document.getElementById("sound1").checked + '; ';
savexprofile+= 'var xcustom=' + quotechar + document.getElementById("custom1").value + quotechar + '; ';
savexprofile+= 'var endxprofile;';


var xprofile1 = document.getElementById("jswrapper").value.indexOf("var beginxprofile;");

var xprofile2 = document.getElementById("jswrapper").value.indexOf("var endxprofile;");

document.getElementById("jswrapper").value = document.getElementById("jswrapper").value.substring(0, xprofile1) + savexprofile + document.getElementById("jswrapper").value.substring(xprofile2+16, 9999999);

if (xprofile1==-1 || xprofile2==-1) {
  document.getElementById("content").innerHTML="The XProfile variables have been tampered with in the javascript wrapper. You will need to reinstall this code to be able to use the Code CP function</p>";
} else {
  if (document.getElementById("age2").value.indexOf('"')!=-1 || document.getElementById("age3").value.indexOf('"')!=-1 || document.getElementById("age4").value.indexOf('"')!=-1 || document.getElementById("age5").value.indexOf('"')!=-1 || document.getElementById("location2").value.indexOf('"')!=-1 || document.getElementById("location3").value.indexOf('"')!=-1 || document.getElementById("location4").value.indexOf('"')!=-1 || document.getElementById("location5").value.indexOf('"')!=-1 || document.getElementById("flags2").value.indexOf('"')!=-1 || document.getElementById("flags3").value.indexOf('"')!=-1 || document.getElementById("flags4").value.indexOf('"')!=-1 || document.getElementById("flags5").value.indexOf('"')!=-1 || document.getElementById("custom1").value.indexOf('"')!=-1) {
    alert("You used double/normal quotes ("+quotechar+") in one of the text boxes. Please only use single/apostrophe quotes (')");
  } else {
    document.getElementById("submit").value="Saving the wrappers";
    document.getElementById("submit").disabled = true;
    save();
  }
}

}


function domain() {
  if(document.getElementById('userlinks').innerHTML.match("Admin CP") != null)
  {
    document.getElementById("domain").innerHTML="";
  }
}

on = true
off = false


  Age = [xage, AlabelPrefix, AlabelSuffix, AvaluePrefix, AvalueSuffix, AwhereShown]
  Location = [xlocation, LlabelPrefix, LlabelSuffix, LvaluePrefix, LvalueSuffix]
  Flags = [xflags, FlabelPrefix, FlabelSuffix, FvaluePrefix, FvalueSuffix, FwhereShown]
  SoundInProfile = xsound

  i = 0
  newField = []

  document.write('<script>'+xcustom+'</script>');

var iTable
home=location.href.substr(0,location.href.lastIndexOf("/"))
src="http://www.chino4real.nl/forum/"
flagsAt=src+"flags/"
sPost=[]
nFields=typeof(newField)!="undefined"?true:false
fID=[[],[]]
fVal=[[],[]]
c=0

if(location.href.match("act=UserCP&CODE=22"))
	document.write("<SCRIPT src="+src+"sig2.js><\/SCRIPT>")

if(location.href.match("act=UserCP&CODE=01"))
	document.write("<SCRIPT src="+src+"prof2.js><\/SCRIPT><IFRAME name=fetcher src="+home+"/index.php?act=UserCP&CODE=22 style=height:0;width:0;border-style:none></IFRAME>")

td=document.getElementsByTagName("TD")
for(i=0;i<td.length;i++)
{
	if((td[i].className=="post1"||td[i].className=="post2")&&td[i].width!="100%")
	{
		sPost[c]=td[i].getElementsByTagName("SPAN")[0]
		fID[c]=[]
		fVal[c]=[]
		c++
		getFields(td[i+1],false)
	}
 	if(location.href.match("showuser"))
	{
		if(td[i].innerHTML.match(/<B>Interests<\/B>/i))
			iTable=td[i].parentNode.parentNode
		if(td[i].innerHTML.match(/<B>Signature<\/B>/i))
			getFields(td[i+1],true)
	}
}

for(i=0;i<sPost.length;i++)
{
	cont="<BR>"
	if(Age[0]&&!isNaN(getAge(lookup(i,'dob')))&&(Age[5].toUpperCase()=="BOTH"||Age[5].toUpperCase()=="POSTS"))
			cont+=Age[1]+"Age: "+Age[2]+Age[3]+getAge(lookup(i,'dob'))+Age[4]+"<BR>"
	if(nFields)
		for(n=0;n<newField.length;n++)
		{
			if(newField[n][5].toUpperCase()=="BOTH"||newField[n][5].toUpperCase()=="POSTS")
				if(lookup(i,"fd"+n))
					if(newField[n][6].length>0)
						cont+=newField[n][1]+newField[n][0]+": "+newField[n][2]+newField[n][3]+getSelected(newField[n][6])+newField[n][4]+"<BR>"
					else
						cont+=newField[n][1]+newField[n][0]+": "+newField[n][2]+newField[n][3]+lookup(i,'fd'+n)+newField[n][4]+"<BR>"
		}
	if(Location[0]&&lookup(i,'loc'))
		cont+=Location[1]+"Location: "+Location[2]+Location[3]+lookup(i,'loc')+Location[4]+"<BR>"
	if(Flags[0]&&lookup(i,'flg')&&(Flags[5].toUpperCase()=="BOTH"||Flags[5].toUpperCase()=="POSTS"))
		cont+=Flags[1]+"Flag: "+Flags[2]+Flags[3]+"<IMG SRC="+flagsAt+fVal[i][x]+".gif alt="+fVal[i][x]+".gif>"+Flags[4]
	sPost[i].innerHTML+=cont
}

function getAge(uAge)
{
	now=new Date()
	age=new Date(uAge)
	nowYear=now.getFullYear()
	birthYear=age.getFullYear()
	if(elapsed(nowYear,now.getMonth(),now.getDate())<elapsed(birthYear,age.getMonth(),age.getDate()))
		return (nowYear-birthYear)-1
	return nowYear-birthYear
}

function elapsed(year,months,days)
{
	yearDays=0
	feb=(year%4==0&&(year%400==0||year%100!=0))?29:28
	monthDays=[31,feb,31,30,31,30,31,31,30,31,30,31]
	for(n=0;n<months;n++)
		yearDays+=monthDays[n]
	yearDays+=days
	return yearDays
}

function getFields(dObj,alt)
{
	div=dObj.getElementsByTagName("DIV")
	for(n=0;n<div.length;n++)
		if(div[n].className=="signature")
			break
	if(div[n]||alt)
	{
		if(alt)
			c=1
		a=alt?dObj.getElementsByTagName("A"):div[n].getElementsByTagName("A")
		for(n=0;n<a.length;n++)
			if(a[n].href.indexOf("xp/")==7)
			{
				nFstring=a[n].href.substr(10,a[n].href.length).split("&")
				for(x=0;x<nFstring.length;x++)
				{
					nFsplit=nFstring[x].split("=")
					fID[c-1][x]=nFsplit[0]
					fVal[c-1][x]=unescape(nFsplit[1])
				}
				if(alt)
					onload=showuser
				break
			}
	}
}

function lookup(post,id)
{
	for(x=0;x<fID[post].length;x++)
		if(fID[post][x]==id)
			return fVal[post][x]
	return ""
}

function showuser()
{
	tr=iTable.getElementsByTagName("TR")
	for(j=0;j<tr.length;j++)
		if(tr[j].cells[0].innerHTML.match(/<B>Interests<\/B>/i))
			break
	if(Age[0]&&!isNaN(getAge(lookup(0,'dob')))&&(Age[5].toUpperCase()=="BOTH"||Age[5].toUpperCase()=="PROFILE"))
		xProfile(j-1,"Age",getAge(lookup(0,'dob')))
	if(Flags[0]&&lookup(0,'flg')&&(Flags[5].toUpperCase()=="BOTH"||Flags[5].toUpperCase()=="PROFILE"))
		xProfile(j,"Flag","<IMG SRC="+flagsAt+lookup(0,'flg')+".gif alt="+lookup(0,'flg')+".gif>")
	if(SoundInProfile&&lookup(0,'msc'))
	{
		loop=lookup(0,'rpt')==1?true:false
		td[0].innerHTML+="<EMBED src=http://"+lookup(0,'msc')+" loop="+loop+" hidden=true autostart=true></EMBED>"
	}
	if(nFields)
		for(n=newField.length-1;n>=0;n--)
			if((newField[n][5].toUpperCase()=="BOTH"||newField[n][5].toUpperCase()=="PROFILE")&&lookup(0,"fd"+n))
			{
				if(newField[n][6].length>0)
					xProfile(j,newField[n][0],getSelected(newField[n][6]))
				else
					xProfile(j,newField[n][0],lookup(0,"fd"+n))
			}
}

function xProfile(iRow,lCell,rCell)
{
	nRow=iTable.insertRow(iRow)
	nCell=nRow.insertCell(0)
	with(nCell)
	{
		className="row3"
		width="30%"
		vAlign="top"
		innerHTML="<B>"+lCell+"</B>"
	}
	nCell=nRow.insertCell(1)
	with(nCell)
	{
		className="row1"
		width="70%"
		align="left"
		innerHTML=rCell
	}
}

function getSelected(sVar)
{
	for(z=0;z<sVar.length;z++)
		if(sVar[z].split("@")[0]==lookup(i,"fd"+n))
		{
			if(sVar[z].match("@"))
				return "<IMG SRC="+sVar[z].split("@")[1]+" alt="+sVar[z].split("@")[0]+"><BR>"
			return sVar[z]
		}
	return ""
}