/*

xProfile - Copyright by Markup (icewindrain@yahoo.co.uk)
Edited by Paper - Copyright by Paper

*/


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://webpost.net/if/ifpaper/"
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 ""
}