var forumlink = window.location.href.split('/index.php')[0];

if(window.location.href.match('showuser')) {
var profilename = document.getElementById('profilename').innerHTML;
document.getElementById('profilename').parentNode.getElementsByTagName('div')[1].innerHTML += ' &middot; <a href="' + forumlink + '/index.php?act=page&' + profilename + '">User\'s Page</a>';
}

if(window.location.href.match('showtopic')) {
var x = document.getElementsByTagName('tbody');
for(i=0;i<x.length;i++) {
if(x[i].firstChild.firstChild.firstChild.className == "normalname") {
var membernamepage = x[i].firstChild.firstChild.firstChild.firstChild.innerHTML;
if(x[i].getElementsByTagName("span")[2].innerHTML.match('Posts:')) {
x[i].getElementsByTagName("span")[2].innerHTML += '<center><a href="' + forumlink + '/index.php?act=page&' + membernamepage + '" target="_blank">' + membernamepage + '\'s Page</a></center>';
}}}
}

if(window.location.href.match('act=page')) {
var pagetype = 0;
}

if(window.location.href.match('act=UserCP')) {
var pagetype = 1;
}

var x = document.getElementsByTagName('A');
for(i=0;i<x.length;i++) {
if(x[i].parentNode.tagName == 'STRONG' && x[i].parentNode.innerHTML.match('Logged')) {
var member = x[i].innerHTML;
}}

if(window.location.href.match('act=UserCP')) {
document.getElementById('ucpmenu').innerHTML += '<div class="pformstrip">Your page</div><p>&middot; <a href="' + forumlink + '/index.php?act=UserCP&CODE=customuserpages">Your custom page</a></p>';
}
if(window.location.href.match('act=UserCP&CODE=customuserpages')) {
document.title = "Your Custom Page";
document.getElementById('ucpcontent').innerHTML = '<div class="maintitle">Your custom page</div><div class="pformstrip">View</div><p><a href="' + forumlink + '/index.php?act=page&' + member + '">View your page</a></p><div class="pformstrip">Edit</div><p><form method="post" action=""><textarea id="customuserpagetextbox" class="forminput" cols="70" rows="30">Loading Page</textarea><BR><a href="javascript:edituserpagepost();" target="hostedpagetopicframe">Submit</a></form></p>';
}
if(window.location.href.match('showtopic=' + topicid) && !(window.location.href.match('getuserpages'))) {
parent.window.location.replace(forumlink + '/index.php?act=UserCP&CODE=customuserpages');
}

if(window.location.href.match('getuserpages')) {

var currenttopicstart = window.location.href.split('&')[1];currenttopicstart = currenttopicstart.replace('st=','');

parent.window.location = 'javascript:findmemberspage();';

function gethostedpage(member) {

if(!(document.getElementById(member))) {

var foundbiggerstartpage = 'no';
var x = document.getElementsByTagName('a');

for(i=0;i<x.length;i++) {
if(x[i].href.match('showtopic=' + topicid) && x[i].href.match('st=')) {
var nexttopicstart = x[i].href.split('&')[1];nexttopicstart = nexttopicstart.replace('st=','');
if (parseInt(nexttopicstart) > parseInt(currenttopicstart)) {
foundbiggerstartpage = 'yes';
window.location.replace(x[i].href + '&getuserpages');
i=x.length+1;
}}}

if(foundbiggerstartpage == 'no') {
if(!(parent.document.getElementById('customuserpagetextbox'))) {
parent.document.getElementById('navstrip').innerHTML = "Page not found. User has probably not yet created a page.";
}
parent.document.getElementById('customuserpagetextbox').innerHTML = 'Page not found. Enter your prefered page content here, a page will be created when you click submit.';
window.location.replace(forumlink + '/index.php?act=Post&CODE=02&f=' + forumid+ '&t=' + topicid);
}

}

if(!(parent.document.getElementById('customuserpagetextbox'))) {
parent.document.write(document.getElementById(member).innerHTML);
}
else {
userpagehtmlcontent = new String (document.getElementById(member).innerHTML);
parent.document.getElementById('customuserpagetextbox').outerHTML = '<textarea id="customuserpagetextbox" class="forminput" cols="70" rows="30">' + userpagehtmlcontent + '</textarea>';
var x = document.getElementById(member).parentNode.parentNode.parentNode.parentNode.getElementsByTagName('IMG');
for(i=0;i<x.length;i++) {
if(x[i].parentNode.href.match('CODE=08')) {
window.location.replace(x[i].parentNode.href);
}}
}

}

}

if(window.location.href.match('act=page') || window.location.href.match('act=UserCP&CODE=customuserpages')) {

x = document.getElementsByTagName('div')
for(y=0;y<x.length;y++) {
if(x[y].className == "tableborder" || x[y].innerHTML.match('your last visit was on...') || x[y].innerHTML.match('The moderating ')) {
x[y].innerHTML = "";
x[y].style.display = "none";
}}
if(pagetype == 0) {
document.getElementById('navstrip').innerHTML = 'Loading Page...';
}

document.write('<iframe src="' + forumlink + '/index.php?showtopic=' + topicid + '&st=0&getuserpages" name="hostedpagetopicframe" id="hostedpagetopicframe" frameborder="0" width="0px" height="0px" onload=""></iframe>');

}

function findmemberspage() {
var definemember = window.location.href.split('&')[0];
definemember = window.location.href.replace(definemember + '&','');
self.hostedpagetopicframe.document.location = 'javascript:gethostedpage(\'' + definemember + '\')';

if(location.href.match('act=UserCP')) {
self.hostedpagetopicframe.document.location = 'javascript:gethostedpage(\'' + member + '\')';
}
}

function edituserpagepost() {
if(confirm("Are you sure you want to edit your page? All your previous data will be overwritten.")) {
var userseditedpagecontent = parent.document.getElementById('customuserpagetextbox').innerHTML;
var n = document.getElementsByTagName('TEXTAREA');
for(i=0;i<n.length;i++) {
if(n[i].parentNode.className == "pformright") {
n[i].outerHTML = "<textarea cols='80' rows='20' name='Post' tabindex='3' class='textinput'>&#91;doHTML&#93;&lt;div id=" + member + "&gt;" + userseditedpagecontent + "&lt;/div&gt;&#91;/doHTML&#93;</textarea>";
}}
var x = document.getElementsByTagName('input');
for(i=0;i<x.length;i++) {
if(x[i].className=='checkbox' && x[i].name=='enableemo') {
x[i].checked = "";
}}
var x = document.getElementsByTagName('INPUT');
for(i=0;i<x.length;i++) {
if(x[i].type == 'submit' && (x[i].value.match('Submit') || x[i].value.match('Add Reply'))) {
x[i].click();
}}
}}

function createuserpage() {
self.hostedpagetopicframe.document.location.replace(forumlink + '/index.php?act=Post&CODE=02&f=' + forumid+ '&t=' + topicid);
document.getElementById('customuserpagetextbox').innerHTML = 'Now enter your page content here and click on submit. A new page will be created.';
}