function getAjaxObject(){
	try{
		var xmlhttp = new XMLHttpRequest();		
	}catch(err1){
		var ieXmlHttpVersions = new Array();
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.7.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.6.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";

		var i;
		for (i=0; i < ieXmlHttpVersions.length; i++){
			try{
				var xmlhttp = new ActiveXObject(ieXmlHttpVersions[i]);
				break;
			}catch (err2){
				
			}
		}
	}
	return xmlhttp;
}

 
function AjaxLoad(link, elementid){
	var element = document.getElementById(elementid);
	if( !element )
		return;
	var AjaxObject = getAjaxObject();
	if(typeof AjaxObject == "undefined"){
		alert('In order to view this page your browser has to support AJAX')
		return;
	}
	AjaxObject.open("GET", link, true);
	AjaxObject.send(null);
	AjaxObject.onreadystatechange=function() {
		if (AjaxObject.readyState==4 && AjaxObject.status==200){
			element.innerHTML = AjaxObject.responseText;
		}
	}
	return;
}

function reloadCaptcha() {
	var img = document.getElementById('cappic');
	img.src = '/lib/frmcaptcha.php?rld=' + Math.random();
	return false;
}

function rldContent(t, txt) {
	if (t.value == txt) {
		t.value = '';
	}
}

function rldContent2(t, txt) {
	if (t.value == '') {
		t.value = txt;
	}
}

function CheckLoginForm(frm, uname, upass) {
	if (frm.uname.value == uname) {
		frm.uname.value = '';
	}
	
	if (frm.upass.value == upass) {
		frm.upass.value = '';
	}

	return true;

}

function pollsubmit(p,t,cid) {
	var http_request = getAjaxObject();
	if (!http_request) return true;
	
	disablepollbuttons(p);
	
	http_request.onreadystatechange = function() { poll_submit_callback(http_request,cid); };
	
	var qry = generatepollquery(p);
	
	var lmethod = 'GET';
	
	http_request.open(lmethod, '/lib/poll_submit.php?type=' + t + '&' + (lmethod == 'GET' ? qry : ''), true);
	if (lmethod == 'POST') http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(lmethod == 'GET' ? null : qry);
	
	return false;
}

function pollsubmitleft(p,t,cid) {
	var http_request = getAjaxObject();
	if (!http_request) return true;
	
	disablepollbuttons(p);
	
	http_request.onreadystatechange = function() { poll_submit_callback(http_request,cid); };
	
	var qry = generatepollquery(p);
	
	var lmethod = 'GET';
	
	http_request.open(lmethod, '/lib/poll_submit_left.php?type=' + t + '&' + (lmethod == 'GET' ? qry : ''), true);
	if (lmethod == 'POST') http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(lmethod == 'GET' ? null : qry);
	
	return false;
}

var lastsubmitbut = '';
var lastsubmitval = '';

function poll_btnclick(b) {
	lastsubmitbut = b.name;
	lastsubmitval = b.value;
	return true;
}

function generatepollquery(f) {
	var retstr = "";
	for (var i = 0; i < f.elements.length; i++) {
		if (f.elements[i].type.toLowerCase() == 'text' || f.elements[i].type.toLowerCase() == 'textarea' 
			|| f.elements[i].type.toLowerCase() == 'hidden') {
			retstr += f.elements[i].name + "=" + escape(f.elements[i].value) + "&";
		} else if (f.elements[i].type.toLowerCase() == 'submit') {
			if (f.elements[i].name == lastsubmitbut && f.elements[i].value == lastsubmitval) retstr += f.elements[i].name + "=" + escape(f.elements[i].value) + "&";
		} else if (f.elements[i].type.toLowerCase() == 'select') {
			retstr += f.elements[i].name + "=" + escape(f.elements[i].options[f.elements[i].selectedIndex]) + "&";
		} else if (f.elements[i].type.toLowerCase() == 'radio' || f.elements[i].type.toLowerCase() == 'checkbox') {
			if (f.elements[i].checked) retstr += f.elements[i].name + "=" + escape(f.elements[i].value) + "&";
		}
	}
	return retstr;
}

function poll_submit_callback(p,cid) {
	if (p.readyState == 4 && p.status == 200) {
		var canketa = document.getElementById(cid);
		canketa.innerHTML = p.responseText;
		return;
	}
}

function disablepollbuttons(p) {
	for(i=0; i < p.elements.length; i++) {
		if (p.elements[i].type.toLowerCase() == 'submit') {
			p.elements[i].disabled = true;
		}
	}
}

// correctly handle PNG transparency in Win IE 5.5 & 6.
function correctPNG() {
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	if ((version >= 5.5 && version < 7.0) && (document.body.filters)) {
		for(var i=0; i<document.images.length; i++) {
			var img = document.images[i];
			var imgName = img.src.toUpperCase();
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
				var imgID = (img.id) ? "id='" + img.id + "' " : "";
				var imgClass = (img.className) ? "class='" + img.className + "' " : "";
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
				var imgStyle = "display:inline-block;" + img.style.cssText;
				if (img.align == "left") imgStyle = "float:left;" + imgStyle;
				if (img.align == "right") imgStyle = "float:right;" + imgStyle;
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
					+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
					+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
					+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
				img.outerHTML = strNewHTML;
				i = i-1;
			}
		}
	}
}

function ChangeColorPic(pOper, pImgId, pId) {
	var menu = document.getElementById('rightmenulink_' + pId);
	if(pOper == 1) {
		menu.style.background = 'url(\'/showimg.php?filename=oo_' + pImgId + '.jpg\') no-repeat top left';
	} else {
		menu.style.background = 'url(/i/' + pImgId + '_inactive.jpg) no-repeat top left';
	}
}

function mPrevNext(pos, pHolder, pStoryId) {	
	var listImgs = pHolder.getElementsByTagName("img");
	var img = listImgs[0];
	for (var i = 0; i < listImgs.length; i ++) {
		if (listImgs[i] == currSel) {
			if ((i + pos) < 0 || (i + pos) >= listImgs.length) {
				img = listImgs[i];
			} else { 
				img = listImgs[i + pos];
			}
		}
	}
	mGetPhoto(img, pHolder, pStoryId);
}

function mPrevNext2(pos, pHolder, pStoryId) {
	var listImgs = pHolder.getElementsByTagName("img");
	var img = listImgs[0];
	for (var i = 0; i < listImgs.length; i ++) {
		if (listImgs[i] == currSel) {
			if ((i + pos) < 0 || (i + pos) >= listImgs.length) {
				img = listImgs[i];
			} else { 
				img = listImgs[i + pos];
			}
		}
	}
	
	mGetPhotoBig(img, pHolder, pStoryId);
}

var currSel;
function mGetPhoto(pImage, pHolder, pStoryId) {
	if (pImage.nodeName.toLowerCase() != "img") 
		pImage = pImage.getElementsByTagName('img')[0];
	
	if (typeof pHolder == 'undefined') {
		var pHolder = pImage.parentNode;
		while (!(pHolder.nodeName.toLowerCase() == 'div' && pHolder.id == 'gpics')) {
			pHolder = pHolder.parentNode;
		}
	}
	
	var listImgs = pHolder.getElementsByTagName("img");
	var prevlink = document.getElementById("prevlink");
	var nextlink = document.getElementById("nextlink");
	
	var mainPhoto = document.getElementById('bigphoto_' + pStoryId);
	
	currSel = pImage;
	
	mainPhoto.src = pImage.src.replace(/filename=[^_]*/, "filename=d285") + "&rld=" + Math.random();
}

function mGetPhotoBig(pImage, pHolder) {
	if (pImage.nodeName.toLowerCase() != "img") 
		pImage = pImage.getElementsByTagName('img')[0];
	
	if (typeof pHolder == 'undefined') {
		var pHolder = pImage.parentNode;
		while (!(pHolder.nodeName.toLowerCase() == 'div' && pHolder.id == 'gpics')) {
			pHolder = pHolder.parentNode;
		}
	}
	
	var listImgs = pHolder.getElementsByTagName("img");
	var prevlink = document.getElementById("prevlink");
	var nextlink = document.getElementById("nextlink");
	
	//~ if (pImage == listImgs[0]) {
		//~ prevlink.className = "pageingInactiveLink noLeftMargin";
	//~ } else {
		//~ prevlink.className = "noLeftMargin";
	//~ }
	
	//~ if (pImage == listImgs[listImgs.length - 1]) {
		//~ nextlink.className = "pageingInactiveLink";
	//~ } else {
		//~ nextlink.className = "";
	//~ }
	
	var mainPhoto = document.getElementById('bigphoto');
	var photoText = document.getElementById('phototext');
	
	currSel = pImage;
	 
	mainPhoto.src = pImage.src.replace(/filename=[^_]*/, "filename=d373x") + "&rld=" + Math.random();
	mainPhoto.alt = pImage.alt;
	photoText.innerHTML = pImage.alt;
}

//resize image
	var resizeDelay = 30;
	var resizeIncrement = 5;
var imgCache = new Object();

function getCacheTag (imgElement) {
	return imgElement.src + "~" + imgElement.offsetLeft + "~" + imgElement.offsetTop;
}

function cachedImg (imgElement, increment) {
	this.img = imgElement;
	this.cacheTag = getCacheTag(imgElement);
	this.originalSrc = imgElement.src;
	
	var h = imgElement.height;
	var w = imgElement.width;
	this.originalHeight = h;
	this.originalWidth = w;
	
	increment = (!increment) ? resizeIncrement : increment;
	this.heightIncrement = Math.ceil(Math.min(1, (h / w)) * increment);
	this.widthIncrement = Math.ceil(Math.min(1, (w / h)) * increment);
}

function resizeImg (imgElement, percentChange, ImageURL) {
	var pct = (percentChange) ? percentChange / 100 : 1;
	
	var cacheTag = imgElement.getAttribute("cacheTag");
	if (!cacheTag) {
		cacheTag = getCacheTag(imgElement);
		imgElement.setAttribute("cacheTag", cacheTag);
	}
	
	var cacheVal = imgCache[cacheTag];
	if (!cacheVal) {
		imgCache[cacheTag] = new Array(new cachedImg(imgElement), pct);
	} else {
		cacheVal[1] = pct;
	}
	
	if (ImageURL)
		imgElement.src = ImageURL;
	
	resizeImgLoop(cacheTag);
	return true;
}

function resizeImgLoop (cacheTag) {
	var cacheVal = imgCache[cacheTag];
	if (!cacheVal)
		return false;
	
	var cachedImageObj = cacheVal[0];
	var imgElement = cachedImageObj.img;
	var pct = cacheVal[1];
	var plusMinus = (pct > 1) ? 1 : -1;
	var hinc = plusMinus * cachedImageObj.heightIncrement;
	var vinc = plusMinus * cachedImageObj.widthIncrement;
	var startHeight = cachedImageObj.originalHeight;
	var startWidth = cachedImageObj.originalWidth;
	
	var currentHeight = imgElement.height;
	var currentWidth = imgElement.width;
	var endHeight = Math.round(startHeight * pct);
	var endWidth = Math.round(startWidth * pct);
	
	if ( (currentHeight == endHeight) || (currentWidth == endWidth) )
		return true;
	
	var newHeight = currentHeight + hinc;
	var newWidth = currentWidth + vinc;
	if (pct > 1) {
		if ((newHeight >= endHeight) || (newWidth >= endWidth)) {
			newHeight = endHeight;
			newWidth = endWidth;
		}
	} else {
		if ((newHeight <= endHeight) || (newWidth <= endWidth)) {
			newHeight = endHeight;
			newWidth = endWidth;
		}
	}
	
	imgElement.height = newHeight;
	imgElement.width = newWidth;
	
	if ((newHeight == cachedImageObj.originalHeight) || (newWidth == cachedImageObj.originalwidth)) {
		imgElement.src = cachedImageObj.originalSrc;
	}
	
	setTimeout("resizeImgLoop('" + cacheTag + "')", resizeDelay);
}


function fbs_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t='+encodeURIComponent(t), 'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function popUp(pImgUrl, pDescription, pWidth, pHeight) {
	pImgUrl += "&rld=" + Math.random();
	lBumperPixels = 60;
	pWidth = parseInt(pWidth) + parseInt(lBumperPixels);
	pHeight = parseInt(pHeight) + parseInt(lBumperPixels);
	
	var lOpened = false;
	var lWin;
	var lWinFeatures = "status=0," +
		"scrollbars=1," +
		"menubar=0," +
		"toolbar=0," +
		"location=0," +
		"resizeable=0," +
		"width=" + pWidth + "," +
		"height=" + pHeight + "," +
		"left=" + ((screen.width-pWidth)/2) + "," +
		"top=" + ((screen.height-pHeight)/2);
	
   	if (lOpened == false) {
		lWin = open('','',lWinFeatures);
		lWin.document.write('<html><head><title>' + pDescription + '</title></head><body>');
		lWin.document.write('<center><img src="' + pImgUrl + '" border="0" /></center>');
		lWin.document.write('</body></html>');
		lWin.document.close();
		lWin.focus();
		
	}  else if (lOpened == true) {
		if (lWin.closed == false)
			lWin.close();
		lWin = open('','',lWinFeatures);
		lWin.document.write('<html><head><title>' + pDescription + '</title></head><body>');
		lWin.document.write('<center><img src="' + pImgUrl + '" border="0" /></center>');
		lWin.document.write('</body></html>');
		lWin.document.close();
		lWin.focus();
		
	}
	
	lOpened = true;
}

