/*
YourShop
Version 0.3

YourShop 0.3 by Bradley Nelson
http://the-ccw.net or ccworld2235@gmail.com
Creative Commons Attribution-Noncommercial-Share Alike 3.0 License

Main Code
*/

var i; var j; var k; var l; var newChild; var cProLink; var vpath = "0.3"; var rts; var q;
var tds = document.getElementsByTagName("td"); var refApp; var newInTable; var newChild2;
var newInTable2; var inTabRows = 1; var newOffset; var tables = document.getElementsByTagName("table");

function addScript(url){
	newChild = document.createElement("script");
	newChild.type = "text/javascript"
	newChild.src = "http://pb.ccsr.us/"+url;
	newChild.defer = "true";
	document.getElementsByTagName("head").item(0).appendChild(newChild);
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

function setFromCookie(){
	if (!(location.href.match(/action=modifyprofile&user=/i)) && !(pb_username=="Guest")){
		return pb_username;
	}
	else if (location.href.match(/action=modifyprofile&user=([\w]+)/i)){
		return new String(RegExp.$1);
	}
}

function setAdmin(usn){
	for (i=0; i<shopAdmin.length; i++){
		if (shopAdmin[i] == usn){
			return true;
			break;
		}
	}
	return false;
}

function setEdit(usn){
	for (i=0; i<shopStaff.length; i++){
		if (shopStaff[i] == usn){
			return true;
			break;
		}
	}
	return false;
}

function makeMoney(posts, offset){
	return Math.floor((parseInt(posts) * moneyPP) + parseInt(offset));
}

function setItems(refUIClass){
	if (!(refUIClass.pt.match(/(\~|\:\:)/i))){
		refUIClass.money = makeMoney((refUIClass.posts), 0);
	}
	if (refUIClass.pt.match(/o(\~|\:\:)(((\-|\+)?)(\d)+);;/i)){
		refUIClass.money = makeMoney(refUIClass.posts, RegExp.$2);
	}
	if (refUIClass.pt.match(/b(\~|\:\:)(\d)+;;/i)){
		refUIClass.items.usernameBold.has = parseInt(RegExp.$2);
	}
	if (refUIClass.pt.match(/i(\~|\:\:)(\d)+;;/i)){
		refUIClass.items.usernameItalic.has = parseInt(RegExp.$2);
	}
	if (refUIClass.pt.match(/c(\~|\:\:)([a-fA-F0-9]{6});;/i)){
		refUIClass.items.usernameColor.has = 1;
		refUIClass.items.usernameColor.color = new String(RegExp.$2);
	}
	if (refUIClass.pt.match(/u(\~|\:\:)(\d)+;;/i)){
		refUIClass.items.usernameUline.has = parseInt(RegExp.$2);
	}
	if (refUIClass.pt.match(/t(\~|\:\:)((.)+?);;/i)){
		refUIClass.items.title.has = 1;
		refUIClass.items.title.text = new String(RegExp.$2);
	}
	if (refUIClass.pt.match(/s(\~|\:\:)(\d)+;;/i)){
		refUIClass.items.sig.has = parseInt(RegExp.$2);
	}
	if (refUIClass.pt.match(/a(\~|\:\:)(\d)+;;/i)){
		refUIClass.items.avatar.has = parseInt(RegExp.$2);
	}
	if (refUIClass.pt.match(/r(\~|\:\:)(\d+)%(\d+);;/i)){
		refUIClass.items.rank.has = 1;
		refUIClass.items.rank.amount = parseInt(RegExp.$2);
		refUIClass.items.rank.index = parseInt(RegExp.$3);
	}
	if (refUIClass.pt.match(/p(\~|\:\:)((\d|,|\s)+?);;/i)){
		refUIClass.items.custom.has = 1;
		refUIClass.items.custom.raw = new String(RegExp.$2).replace(" ", "");
		for (k=0; k<refUIClass.items.custom.raw.split(",").length; k++){
			refUIClass.items.custom.arr[k] = parseInt(refUIClass.items.custom.raw.split(",")[k]);
		}
	}
}

function runEverythingElse() {
	addScript("shop/"+vpath+"/common.js");
	addScript("shop/"+vpath+"/constants.js");
	addScript("js-security/sha1.js");
	if (location.href.match(/action=(display|search2|(user)?recent|viewprofile|pmview)/i)){
		addScript("shop/"+vpath+"/post.js");
	}
	if (location.href.match(/action=(view|modify)profile/i)){
		addScript("shop/"+vpath+"/pro.js");
	}
	if (location.href.match(/action=modifyprofile/i)){
		addScript("shop/"+vpath+"/promod.js");
	}
	if (document.title.match(/admin home/i) || location.href.match(/action=(headersfooters(2|3)|admin)/i)){
		addScript("shop/"+vpath+"/adminmain.js");
	}
	if (location.href.match(/action=pm(send|view)?/i)){
		addScript("js-security/BigInt.js");
		addScript("js-security/Barrett.js");
		addScript("js-security/RSA.js");
		addScript("shop/"+vpath+"/donation.js");
	}
	if (!(location.href.match(/suba=(go)?update/i))){
		if (getCookie("shp-act")){
			if (getCookie("shp-act") == "sendToModifyProfile"){
				setTimeout("sendToModPro()", 200);
			}
			if (getCookie("shp-act") == "sendToModifyProfile2"){
				setTimeout("sendToModPro2()", 200);
			}
		}
	}
}

function userInfoClass () {
	this.username = "NaN",
	this.canEdit = "NaN",
	this.canAdmin = "NaN",
	this.posts = 0,
	this.money = 0,
	this.pt = "NaN",
	this.items = {},
	this.items.title = {},
	this.items.title.has = 0,
	this.items.title.text = "",
	this.items.sig = {},
	this.items.sig.has = 0,
	this.items.avatar = {},
	this.items.avatar.has = 0,
	this.items.usernameBold = {},
	this.items.usernameBold.has = 0,
	this.items.usernameItalic = {},
	this.items.usernameItalic.has = 0,
	this.items.usernameColor = {},
	this.items.usernameColor.has = 0,
	this.items.usernameColor.color = "",
	this.items.usernameUline = {},
	this.items.usernameUline.has = 0,
	this.items.rank = {},
	this.items.rank.has = 0,
	this.items.rank.amount = 0,
	this.items.rank.index = 0,
	this.items.custom = {},
	this.items.custom.has = 0
	this.items.custom.raw = "NaN",
	this.items.custom.arr = []
}

var userInfo = new userInfoClass;
document.write("<div id=\"refApp\"></div>");
var refApp = document.getElementById("refApp");

userInfo.username = setFromCookie();
userInfo.canAdmin = setAdmin(userInfo.username);
userInfo.canEdit = (userInfo.canAdmin == true) ? true : setEdit(userInfo.username);

if (useButton == 1 && !(pb_username=="Guest")){
	k = document.createElement("img");
	k.src = buttonUrl;
	k.alt = "[Shop]"
	k.border = 0;
	newChild = document.createElement("a");
	newChild.href = "/index.cgi?action=viewprofile&suba=GOSHOP";
	newChild.appendChild(k);
	k = 0;
	tds.item(5).firstChild.replaceChild(newChild, tds.item(5).firstChild.lastChild);
	tds.item(5).firstChild.insertBefore(document.createTextNode(" "), newChild);
}

setTimeout("runEverythingElse()", 900);