var req;
String.prototype.leftPad = function (l, c) { return new Array(l - this.length + 1).join(c || '0') + this; };
function AJAXreq(meth, url, async, sendString, func, loud) {
	this.xmlhttp = null;
	this.meth = meth;
	this.url = url;
	this.async = async;
	this.sendString = sendString;
	this.func = func;
	this.loud = loud;
	this.send =  function() {
		if (window.XMLHttpRequest) {// code for all new browsers
			this.xmlhttp = new XMLHttpRequest();
		} else if (window.ActiveXObject) {// code for IE5 and IE6
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (this.xmlhttp != null) {
			this.xmlhttp.onreadystatechange = this.handle;
			if (this.meth == "GET") {
				this.url = this.url + "?" + this.sendString;
				this.xmlhttp.open(this.meth, this.url, this.async);
				this.xmlhttp.send(null);
			}
			else if (this.meth == "POST") {
				this.xmlhttp.open(this.meth, this.url, this.async);
				this.xmlhttp.send(this.sendString);
			}
		} else {
			if (this.loud) {
				window.alert('Your browser does not support XMLHTTP.');
			}
			else {
				console.error('Your browser does not support XMLHTTP.');
			}
		}
	};
	this.handle = function() {
		if (this.readyState == 4) {// 4 = "loaded"
			if (this.status == 200) {// 200 = OK
				req.func(this);
			} else {
				if (req.loud) {
					window.alert('There was a problem communicating with the server.');
				}
				else {
					console.error('There was a problem communicating with the server.');
				}
			}
		}
	};
	this.addVar = function(name, value) {
		if(this.sendString != null && this.sendString.length >= 3) {
			this.sendString = this.sendString + "&" + name + "=" +  encodeURIComponent(value);
		}
		else {
			this.sendString = name + "=" + encodeURIComponent(value);
		}
	};
}
function loadImage(mainsrc, mainImg, num) {
	if (typeof mainImg == 'undefined') { // Default mainImg
		mainImg = document.getElementById('container1a');
		mainImg = mainImg.firstChild.firstChild.firstChild;
	}
	var imgSrc, aSrc;
	imgSrc = mainImg.attributes.getNamedItem('src');
	imgSrc.nodeValue = mainsrc;
	aSrc = mainImg.parentNode.attributes.getNamedItem('href');
	aSrc.nodeValue = "gallery.php?id=" + num;
}
function getDesc(imgNum, b, type, picNum) {
	if (typeof type == "undefined") {
		type = 0;
	}
	if (typeof getDesc.descArray == "undefined") {
		getDesc.descArray = new Array;
	}
	if (!(typeof getDesc.descArray[Number((!(typeof picNum == "undefined")) ? picNum : imgNum)] == "undefined")) {
		writeDesc(getDesc.descArray[Number((!(typeof picNum == "undefined")) ? picNum : imgNum)], b, (!(typeof picNum == "undefined")) ? picNum : imgNum);
	}
	else {
		req = new AJAXreq("GET", "index.php", true, null, null, false);
		req.addVar("t", type);
		req.addVar("id", imgNum);
		if (!(typeof picNum == "undefined")) {
			req.addVar("p", picNum);
		}
		req.func = function(xmlhttp) {writeDesc(xmlhttp.responseText, b, (!(typeof picNum == "undefined")) ? picNum : imgNum);};
		req.send();
	}
}
function writeDesc(x, b, imgNum) {
	if (typeof getDesc.descArray[imgNum] == "undefined") {
		getDesc.descArray[imgNum] = x;
	}
	b.innerHTML = x;
}
