function getElement(sID) {
	return document.getElementById(sID);
}
function show(sID) {
	var o=getElement(sID);
	if (o) o.style.visibility="visible";
}
function hide(sID) {
	var o=getElement(sID);
	if (o) o.style.visibility="hidden";
}
function openEle(sID) {
	var o=getElement(sID);
	if (o) o.style.display="block";
}
function closeEle(sID) {
	var o=getElement(sID);
	if (o) o.style.display="none";
}
function openclose(sID) {
	var o=getElement(sID);
	if (o) {
		if (o.style.display=='none')
			o.style.display="block";
		else
			o.style.display="none";
	}
}
function focus(sID) {
	var o=getElement(sID);
	if (o) o.focus();
}
String.prototype.ltrim=new Function("return this.replace(/^\\s+/,'')");
String.prototype.rtrim=new Function("return this.replace(/\\s+$/,'')");
String.prototype.trim=new Function("return this.replace(/^\\s+|\\s+$/g,'')");
String.prototype.nocr=new Function("return this.replace(/(\\r\\n|[\\r\\n])/g,'')");
String.prototype.makeplain=new Function("return this.toLowerCase().replace(/\\s/g,'_')");
String.prototype.endsWith=function(s) {	return(new RegExp(s+"$")).test(this); }

function PicSwitch(imgID,picPosID) {
	this.imgID=imgID;
	this.picPosID=picPosID;
	this.current=0;
	this.aPics=null;
	this.sURL=null;	
	
	this.loadPics=function(oid,otype,pids,alts,aid,size) {
		var asPIDs=pids.split('\|');
		var asAlts=alts.split('\|');
		
		aid=(aid==undefined ? -1 : aid);		
		size=(size==undefined ? -1 : size);
		
		this.aPics=new Array();
		
		for (var iPos=0 ; iPos<asPIDs.length ; iPos++) {
			this.aPics[iPos]=new Image();

			if (size!=-1 && aid!=-1)
				this.aPics[iPos].src='jsp/feed/getimage.jsp?size='+size+'&oid='+oid+'&otype='+otype+'&aid='+aid+'&pid='+asPIDs[iPos];
			else
				this.aPics[iPos].src='jsp/feed/getimage.jsp?oid='+oid+'&otype='+otype+'&mpid='+asPIDs[iPos];

			this.aPics[iPos].alt=asAlts[iPos];
		}
	}
	this.nextPic=function() {
		this.current++;
		if (this.current>=this.aPics.length) this.current=0;
		this.switchPic(this.current);
	}
	this.prevPic=function() {
		this.current--;
		if (this.current<0) this.current=this.aPics.length-1;
		this.switchPic(this.current);
	}
	this.switchPic=function(idx) {
		if (document.images) {
			var img=getElement(this.imgID);
			if (img) {
				img.src=this.aPics[idx].src;
				img.alt=this.aPics[idx].alt;
			}
			
			var txt=getElement(this.picPosID);
			if (txt) txt.innerHTML=""+(this.current+1)+" of "+this.aPics.length;
		}
	}
}

function Fetcher() {
	var me=this;
	this.fnCB=null;
	this.xmlHttp=null;
	try { this.xmlHttp=new XMLHttpRequest(); }
	catch (trymicrosoft) {
		try { this.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (othermicrosoft) {
			try { this.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (failed) {
				this.xmlHttp=null;
			}  
		}
	}
	this.now=function(sURL) {
		var sRet="";
		if (this.xmlHttp) {
			this.xmlHttp.open("GET",cacheBust(sURL),false);
			this.xmlHttp.send(null);
  			if (this.xmlHttp.readyState==4)
				sRet=this.xmlHttp.responseText.nocr().trim();
		}
		return sRet;
	}	
	this.whenever=function(sURL,fnCB) {
		if (this.xmlHttp) {
			this.fnCB=fnCB;
			this.xmlHttp.open("GET",cacheBust(sURL),true);
			this.xmlHttp.onreadystatechange=this.loader;
			this.xmlHttp.send(null);
		}
	}
	this.loader=function() {
		if (me.xmlHttp) {
			if (me.xmlHttp.readyState==4 && me.xmlHttp.status==200) {
				me.fnCB(me.xmlHttp.responseText.nocr().trim());
			}
		}
	}
}
function PG(pp,pg,max) {
	this.iPP=(1*pp);
	this.iPg=(1*pg);
	this.iMax=(1*max);

	this.max=function() { return this.iMax; }
	this.perpage=function() { return this.iPP; }
	this.current=function(pg) {
		if (pg!=undefined) this.iPg=(1*pg);
		return this.iPg;
	}

	this.from=function() { return (this.iPg*this.iPP)+1; }
	this.to=function() { return Math.min((1+this.iPg)*this.iPP,this.iMax); }

	this.move=function(iDelta) {
		this.iPg+=iDelta;
		
		if (this.iPg<0) this.iPg=Math.ceil(this.iMax/this.iPP)-1;
		else if (this.from()>this.iMax) this.iPg=0;
	}
}
function extractXML(sXML,sTag) {
	var iFrom=sXML.indexOf("<"+sTag+">");
	var iTo=sXML.indexOf("</"+sTag+">",iFrom);
	return (iFrom!=-1 && iTo!=-1 ? sXML.substring(iFrom+(sTag.length+2),iTo) : "");
}
function getThumbPicPath(sID) {
	return cacheBust("upload/pics/"+sID.substring(sID.length-1)+"/"+sID+"/thumb.jpg");
}
function areYouSure(sURL,sMsg) {
	if (confirm(sMsg)) goURL(sURL);
}
function goURL(sURL) {
	location.replace(fixURL(sURL));
}
function fixURL(sURL) {
	var b=document.getElementsByTagName('base');
	if (b && b[0] && b[0].href) {
		if (b[0].href.substr(b[0].href.length-1)=='/' && sURL.charAt(0)=='/') sURL=sURL.substr(1);
		sURL=b[0].href+sURL;
	}
	return sURL;
}
function clean(sIn) {
	var sTemp=sIn.trim();

	sTemp=sTemp.replace(/“/g,"\"")
				.replace(/”/g,"\"")
				.replace(/…/g,"...")
				.replace(/’/g,"'")
				.replace(/‘/g,"'")
				.replace(/–/g,"-")
				.replace(/—/g,"-")
				.replace(/	/g," ")
				.replace(/•/g,"-");
	return sTemp;
}
function initHelp(frmid) {
	var frm=getElement(frmid);
	var help=null;
	for (var i=0 ; i<frm.length ; i++) {
		help=getElement('h_'+frm.elements[i].id);
		if (help!=null) {
			frm.elements[i].onfocus=new Function('openEle(\'h_'+frm.elements[i].id+'\');');
			frm.elements[i].onblur=new Function('closeEle(\'h_'+frm.elements[i].id+'\');');
		}
	}
}
function mapLineBreaks(txt) {
	return txt.replace(/\r\n/g,"<br />")
				.replace(/\n/g,"<br />")
				.replace(/\r/g,"<br />");
}
function decodeMultiLine(txt) {
	return txt.replace(/<br>/gi,'\n');
}
function wordCount(txt) {
	var arr = txt.trim().split(/\s+/g);
	return arr.length;
}

function validUsername(un) {
	var reg=/^[0-9a-z\-]+$/i; 
	return reg.test(un);
}
function validPostcode(pc) {
	var reg=/^[a-z]{1,2}[\da-z]{1,2} \d[a-z][a-z]$/i; 
	return reg.test(pc);
}
function validEmail(email) {
	var reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/i;
	return reg.test(email);
}
function validInt(sVal) {
	var re=/(^\d+$)/;
	return (re.test(sVal));
}
function validNumeric(sVal) {
	var re=/(^\d+$)|(^\d+\.\d+$)/;
	return (re.test(sVal));
}
function isInteger(sName) {
	return validInt(getElement(sName).value);
}
function isNumeric(sName) {
	return validNumeric(getElement(sName).value);
}
function isDate(sNameDD,sNameMM,sNameYYYY,bRequired) {
	var bValid=false;
	var DD=getElement(sNameDD).value;
	var MM=getElement(sNameMM).value;
	var YYYY=getElement(sNameYYYY).value;

	if (!bRequired && DD.length==0 && MM.length==0 && YYYY.length==0)
		bValid=true;
	else {
		if (DD.length<=2 && MM.length<=2 && YYYY.length<=4) {
			if (calcAge(DD,MM,YYYY)<120) {
				MM=(1*MM)-1;
				var dateTest=new Date(YYYY,MM,DD);
				if (dateTest.getDate()==DD && dateTest.getMonth()==MM && dateTest.getFullYear()==YYYY)
					bValid=true;
			}
		}
	}
	return bValid;
}
function daysInMonth(year,month) {
     return 32-new Date(year,month,32).getDate();
}
function dateAddDays(date,ndays) {
	var millidays = ndays * (24 * 60 * 60 * 1000);
	var tz1=date.getTimezoneOffset();
	date=new Date(date.getTime() + millidays);
	var tz2=date.getTimezoneOffset();
	if (tz1!=tz2) {
		if (tz1!=0) date=new Date(date.getTime() - (tz1 * 60 * 1000));
		else if (tz2!=0) date=new Date(date.getTime() - (tz2 * 60 * 1000));
	}
	 return date;
}
function calcAge(dd,mm,yyyy) {
	var bdd=parseInt(dd);
	var bmm=parseInt(mm);
	var byyyy=parseInt(yyyy);
	var now = new Date();
	var ndd=now.getDate();
	var nmm=now.getMonth()+1;
	var nyyyy=now.getFullYear();

	if (nmm>bmm || (nmm==bmm && ndd>=bdd))
		return nyyyy-byyyy;
	else
		return nyyyy-byyyy-1;
}
function doLoad() {
	if (typeof(GLoad)!='undefined') GLoad();
}
function doUnload() {
	if (typeof(GUnload)!='undefined') GUnload();
}
function makeCookie(sName,sValue,Expiry,Path,Domain,Secure) {
	if (Expiry!=null && !isNaN(Expiry)) {
		var datenow = new Date();
		datenow.setTime(datenow.getTime() + Math.round(86400000*Expiry));
		Expiry = datenow.toGMTString();
	}
	Expiry=(Expiry) ? '; expires='+Expiry : '';
	Path=(Path) ? '; path='+Path : '';
	Domain=(Domain) ? '; domain='+Domain : '';
	Secure=(Secure) ? '; secure' : '';

	document.cookie=sName+'='+escape(sValue)+Expiry+Path+Domain+Secure;
}
function updateCookie(sName,sKey,sValue) {
	removeFromCookie(sName,sKey);
	var sTemp=readCookie(sName);
	if (sTemp==null) sTemp="";
	sTemp=sTemp+"~"+sKey+"|"+sValue;
	makeCookie(sName,sTemp,365,'/','',false);
}
function removeFromCookie(sName,sKey) {
	var sFind="~"+sKey+"|";
	var sTemp=readCookie(sName)+"";
	var start=sTemp.indexOf(sFind);
	var finish = sTemp.substring(start+1,sTemp.length);

	if (finish.indexOf('~')==-1) {
		if (start!=-1) sTemp=sTemp.substring(0,start);
	}
	else {
		if (start!=-1) sTemp=sTemp.substring(0,start)+sTemp.substring(start + finish.indexOf('~')+1,sTemp.length);
	}
	makeCookie(sName,sTemp,365,'/','',false);
}
function readCookie(sName) {
	var cookies=' '+document.cookie;
	
	if (cookies.indexOf(' '+sName+'=')==-1) return null;

	var start=cookies.indexOf(' '+sName+'=')+(sName.length + 2);
	var finish=cookies.substring(start,cookies.length);
	finish=(finish.indexOf(';') == -1) ? cookies.length : start + finish.indexOf(';');

	var ret=unescape(cookies.substring(start,finish).trim());
	return (ret=='null' ? null : ret);
}
function goPop(sURL,sName,iW,iH,sExtra) {
	if (sExtra==undefined) sExtra="";
    var win=window.open(fixURL(sURL),sName,"width="+iW+",height="+iH+sExtra);
	if (win!=null) win.focus();
}
function cacheBust(sURL) {
	return sURL+(sURL.indexOf('?')==-1 ? '?' : '&')+'cbzz='+(new Date()).getTime();
}