var validators={"dorequire":{regex:'^.+$',errmsg:'we do require something here'},
"email":{regex:/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,errmsg:'not a valid email'},
"password":{regex:/^[a-zA-Z0-9]{6,100}$/,errmsg:'at least 6 letters/numbers please'}};
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    if ( ver >= 8.0 ) 
      msg = "You're using a recent copy of Internet Explorer."
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  }
  return msg;
}


function fob(i) {return document.getElementById(i);}
function showOverlay(srcO,show) {
    //if (theSrcElement(e)==srcObj) {
   // if (srcO.id!==null) {
		if (srcO.id=="scrollerControl") {
			if (cobj=currentScrollElement(fob("scrollthrough"))) srcO=cobj;
		}
		if (srcO.id!==null) {
			var tar=null;
			tar=fob(srcO.id.replace("block","overlay"));
			if (show) {
				revealObject(tar);
			}else{
				hideObjectDelay(tar);
			}
		}
	//}
}
var revealObjects=new Array();
var revealTimer=null;
var hideTimer={};
function revealObject(tar) {
    var id=tar.id.replace("overlay","");
    if (tar.getAttribute("showing")!="1") {
		tar.setAttribute("showing","1");
		var dims=objFreeSize(tar,false,true);
		var st=maxExpandStep(0,0,0,dims[1],true);
		resizer.resize([tar],false,true,0,0,null,dims[1],null,st,20,false,null,true);

	   // resizer.resize(tar,false,true,0,0,null,null,null,10,4,true,null,true);
		if (hideTimer[id]) clearTimeout(hideTimer[id]);
	   // alert("Reveal");
   }
}
function hideObjectDelay(tar) {
	var id=tar.id.replace("overlay","");
	if (hideTimer[id]) clearTimeout(hideTimer[id]);
	hideTimer[id]=setTimeout("hideObject('"+tar.id+"')",500);
}
function hideObject(tar) {
	if (typeof(tar)=="string") tar=fob(tar);
	var dims=objFreeSize(tar);
	var st=maxExpandStep(0,0,0,dims[1],false);
		tar.setAttribute("showing","0");
	resizer.resize([tar],false,true,null,null,null,0,null,st,20,false,null,true);
}
function theSrcElement(e) {
    if (!e) var e = window.event;
    if (e.target) target = e.target
    else if (e.srcElement) target = e.srcElement
    return target;
}
function requireScrollers(container) {
	var theslide=container;
	if (!container.id.match(/slide$/)) {
		theslide=fob(container.id+"slide");
	}
    var nodes=theslide.childNodes;
    var kids=[];
    for(var n=0;n<nodes.length;n++) {
    	if (nodes[n].tagName=="DIV") kids.push(nodes[n]);
    }
    var kidwidth=kids[0].offsetWidth;
    var ds=container.offsetWidth;
    var shows=Math.round(ds/kidwidth);
    var isscroll=(shows<kids.length);
    fob(container.id+"left").style.display=isscroll?"block":"none";
    fob(container.id+"right").style.display=isscroll?"block":"none";
}
function currentScrollElement(container) {
    var l=container.scrollLeft;
    var theslide=fob(container.id+"slide");
    var nodes=theslide.childNodes;
    var kids=[];
    for(var n=0;n<nodes.length;n++) {
    	if (nodes[n].tagName=="DIV") kids.push(nodes[n]);
    }
    if(kids.length>0) {
		//var kidwidth=kids[0].offsetWidth;
		var ds=container.offsetWidth;
		var kidwidth=totalWidth(kids[0]);
		//kidwidth=kids[0].offsetWidth;
		var shows=Math.round(ds/kidwidth);
		var cindex=Math.round(l/kidwidth);
		//alert(l+"/"+kidwidth);
		return kids[cindex];
	}
}
function totalWidth(obj) {
//alert(obj.className);
	var ml=getStyle(obj,"margin-left").replace("px","");
	var mr=getStyle(obj,"margin-right").replace("px","");
	if(ml=="auto") ml=0;
	if(mr=="auto") mr=0;
	var margin=obj.offsetWidth+(1*ml)+(1*mr);
	//alert(obj.innerHTML);
	//alert(getStyle(obj,"margin-left"));
	return margin;
}
function scrollToElement(container,dindex,index) {
	//alert(container.id);
	if (container.id!="dvdslider") showOverlay(fob("scrollerControl"),false);
    var l=container.scrollLeft;
    var theslide=fob(container.id+"slide");
    var nodes=theslide.childNodes;
    var kids=[];
    for(var n=0;n<nodes.length;n++) {
    	if (nodes[n].tagName=="DIV") kids.push(nodes[n]);
    }
    var kidwidth=totalWidth(kids[0]);
    var ds=container.offsetWidth;
    var shows=Math.round(ds/kidwidth);
    if (index===null) {
	    var cindex=Math.round(l/kidwidth);
	    cindex+=dindex;
	    if (cindex>=kids.length-shows) cindex=kids.length-shows;
	    if (cindex<0) cindex=0;
    }else{
    	    cindex=index;
    }
    var newl=cindex*kidwidth;
    scrollObjectTo(container,newl);
    var dots=null;
    if (dots=fob(container.id+"dots")) {
		var dts=dots.getElementsByTagName("LI");
		for(var k=0;k<dts.length;k++) {
			dts[k].className=((dts.length-k-1)==cindex)?"":"selectable";
		}
    }
}
function gotoSlideLink() {
	var container=fob("scrollthrough");
    var l=container.scrollLeft;
    var theslide=fob(container.id+"slide");
    var nodes=theslide.childNodes;
    var kids=[];
    for(var n=0;n<nodes.length;n++) {
    	if (nodes[n].tagName=="DIV") kids.push(nodes[n]);
    }
    if(kids.length>0) {
		var ds=container.offsetWidth;
		var kidwidth=totalWidth(kids[0]);
		var shows=Math.round(ds/kidwidth);
		var cindex=Math.round(l/kidwidth);
		window.location=sliderLinks[cindex];
	}

}
var scrollingObjects=[]; // obj, current scrollleft, desired scrollleft
var scrollingTimer=null;
var scrollingTimerOn=false;
function scrollObjectTo(obj,scrleft) {
    var idx=-1;
    for(var i=scrollingObjects.length-1;i>=0;i--) {
        if (scrollingObjects[i]["obj"]==obj) idx=i;
    }
    if (idx<0) {
        scrollingObjects.push({"obj":obj,"current":obj.scrollLeft,"desired":scrleft});
        if (!scrollingTimerOn) {
        	scrollingTimerOn=true;
        	scrollingTimer=setTimeout("scrollObjects()",2);
        }
    }
}
function scrollObjects() {
    for(var i=scrollingObjects.length-1;i>=0;i--) {
        var ns=(scrollingObjects[i]["desired"]-scrollingObjects[i]["current"])/4;
        if (ns>10) ns=10;
        if (ns<-10) ns=-10;
        if (ns>-1 && ns<1) {
            ns=scrollingObjects[i]["desired"];
            scrollingObjects[i]["obj"].scrollLeft=ns;
            scrollingObjects.splice(i,1);
        }else{
            scrollingObjects[i]["current"]+=ns;
            scrollingObjects[i]["obj"].scrollLeft=Math.round(scrollingObjects[i]["current"]);
        }
        
    }
    if (scrollingObjects.length>0) {
        scrollingTimer=setTimeout("scrollObjects()",2);
    }else{
        clearTimeout(scrollingTimer);
        scrollingTimer=null;
        scrollingTimerOn=false;
    }
}
function buttondown(srcObj) {
	if (iev>0) {
		
		var h=srcObj.clientHeight;
		var pos='';
		if (pos=getStyle(srcObj,"background-position-x")) {
			pos+=" "+(-(h*2))+"px";
			srcObj.style.backgroundPosition=pos;
		}
	}else{
		var h=srcObj.clientHeight;
		var pos='';
		if (pos=getStyle(srcObj,"background-position")) {
			var pos=pos.split(" ");
			pos[1]=(-(h*2))+"px";
			srcObj.style.backgroundPosition=pos.join(" ");
		}
	}
}
function buttonup(srcObj) {
	srcObj.style.backgroundPosition="";
}
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	if (strValue=="") strValue=oElm.style[strCssRule];
	return strValue;
}
function fadeBackground(alpha) {
	if (backdrop=fob("backgroundfade")) {
	}else{
		ins="<div id='backgroundfade'></div>";
		if (!(dest=fob("overlays"))) dest=document.body;
		pRH(ins,dest);
		backdrop=fob("backgroundfade");
	}
	fadeObject.fade(backdrop,0,alpha,5,null);
}
function loseBackground(alpha) {
	if (backdrop=fob("backgroundfade")) fadeObject.fade(backdrop,alpha,0,10,'lostBackground()');
}
function lostBackground() {
	removeTheNode("backgroundfade");
}
function pRH(theHTML,destinationObj) {
    var hiddenArea=null;
    if (fob("ParseArea")==null) {
        hiddenArea=document.createElement("div");
        hiddenArea.style.display="none";
        hiddenArea.ID="ParseArea";
    } else {
        hiddenArea = fob("ParseArea");
    }
    hiddenArea.innerHTML=theHTML;
    for (c=0;c<hiddenArea.childNodes.length;c++) destinationObj.appendChild(hiddenArea.childNodes[c]);
    hiddenArea.innerHTML="";
}
function cO(obj) {
    scr=getScrollXY();
	if (getStyle(obj,'position')=="fixed") scr=[0,0];
    wh = getWindowHeight();
    t=(obj.clientHeight>wh)?scr[1]:((wh-obj.clientHeight)/2)+scr[1];
    if (t<10) t=10;
    l=((document.body.offsetWidth-obj.clientWidth)/2);
    if (l<0) l=0;
	l+=scr[0];
    obj.style.left=l+"px";
    obj.style.top=t+"px";
}
function getWindowHeight() {
    dims=windowDims();
	return dims[1];
} 
function getWindowWidth() {
    dims=windowDims();
	return dims[0];
} 
function windowDims() {
    var myWidth=myHeight=0;
    if(typeof(window.innerWidth)=='number'){myWidth=window.innerWidth;myHeight=window.innerHeight;}else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){myWidth=document.documentElement.clientWidth;myHeight=document.documentElement.clientHeight;}else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){myWidth=document.body.clientWidth;myHeight=document.body.clientHeight;}
    return [myWidth,myHeight];
}
function removeTheNode(idname) {
	if (typeof(idname)=="string") {
		if (fob(idname)!=null) {
			thisNode=fob(idname);
			thisNode.parentNode.removeChild(thisNode);
		}
	}else{
		idname.parentNode.removeChild(idname);
	}
}

function validateInput(srcObj,type) {
	op=true;
	if (regex=validators[type]["regex"]) {
		rg=new RegExp(regex);
		if(rg.test(srcObj.value)) {
			srcObj.className="ok";
		}else{
			srcObj.className="fail";
			op=false;
		}
	}else{
		srcObj.className="ok";
	}
	var errmsg='';
	if (!op) {
		errmsg=validators[type]["errmsg"];
	}
	if (err=fob(srcObj.id+"bubble")) {
		err.style.display=(errmsg=="")?"none":"block";
	}
	if (err=fob(srcObj.id+"err")) {
		err.innerHTML=errmsg;
	}
	return op;
}
function confirmInput(srcObj,ip) {
	var op=validateInput(fob(ip),"password");
	if (op && srcObj.value!=fob(ip).value) {
		srcObj.className="fail";
		op=false;
	}else{
		srcObj.className='';
	}
	var errmsg='';
	if (!op) errmsg="Does not match";
	if (err=fob(srcObj.id+"alert")) {
		if (errmsg=="") {
			err.style.display="none";
		}else{
			err.style.display="block";
			err.innerHTML=errmsg;
		}
	}
	return op;
}
function pressingEnterOrEscape(srcObj,e,enterhandler,escapehandler) {
    if (window.event) {
        e=window.event;
        keycode=window.event.keyCode;
    }else{
        keycode=e.which;
    }
	reply=true;
	if (keycode==13 && enterhandler) {
		eval(enterhandler+"(srcObj)");
		reply=false;
	}
	if (keycode==27 && escapehandler) eval(escapehandler+"()");
	return reply;
}
function gotoStrippedURL() {
	var url=window.location.href.replace(/\?.*$/,'');
	window.location=url;
}
function requestNewsLetter() {
	if (validateInput(fob("Newsletter_name"),"dorequire") && validateInput(fob("Newsletter_email"),"email")) {
		var vars="cmd=newsletter&email="+fob("Newsletter_email").value+"&name="+fob("Newsletter_name").value;
		sR.post("/",vars,requestedNewsLetter);
	}
}
function requestedNewsLetter(reply) {
	var com='';
	switch(reply) {
		case "exists":
			com=fob("Newsletter_email").value+" is already registered";
			break;
		case "signedup":
			com=fob("Newsletter_email").value+" is now registered for our mailing list";
			fob("Newsletter_name").value='Name';
			fob("Newsletter_email").value='Email address';
			break;
		case "error":
			com="There has been an error on the system";
			break;
		default:
			com="There has been an error on the system";
			break;
	}
	fob("newslettercomment").innerHTML=com;
	if (fob('newsletterreply').style.display=="none") {
			fadeObject.fade(fob('newsletterreply'),0,70,5,null);
	}
}
function hideNLComment() {
	fob('newsletterreply').style.display="none";
}
function submitContact() {
	if (fob("contactsubmitbutton").className=="bigbuynow") {
		if (validateInput(fob("contactfirstname"),"dorequire") && validateInput(fob("contactlastname"),"dorequire") && validateInput(fob("contactemail"),"email")) {
			var vars="cmd=contactus&email="+encodeUpload(fob("contactemail").value)+"&name="+encodeUpload(fob("contactfirstname").value)+" "+encodeUpload(fob("contactlastname").value)+"&salutation="+fob("contactsalutation").value+"&address="+encodeUpload(fob("contactaddress").value)+"&telephone="+encodeUpload(fob("contacttelephone").value)+"&school="+encodeUpload(fob("contactorganisation").value)+"&enquiry="+encodeUpload(fob("contactenquiry").value)+"&where="+encodeUpload(fob("contactwhere").value);
			fob("contactsubmitbutton").innerHTML="sending...";
			fob("contactsubmitbutton").className="bigwait";
			//contactedus("");
			sR.post("/",vars,contactedus);
		}
	}
}
function contactedus(reply) {
	dropThisMessage("Your message has been sent to Stage on Screen");
	fob("contactsubmitbutton").innerHTML="Submit";
	fob("contactsubmitbutton").className="bigbuynow";
	fob("contactemail").value='';
	fob("contactfirstname").value='';
	fob("contactlastname").value='';
	fob("contactaddress").value='';
	fob("contacttelephone").value='';
	fob("contactorganisation").value='';
	fob("contactenquiry").value='';
}
function encodeUpload(ip) {
	return ip.replace(/&/g,"(amp)").replace(/=/g,"(eq)").replace(/%/g,"(per)").replace(/\+/g,"(plus)").replace(/ /g,"+");
}
function displayMovie(srcObj,container) {
	var obj=images[srcObj.id];
	if(flashobj=swfobject.getObjectById('video_player')) {
		playMovie(obj["source"]);
	}else{
		lightbox.show(srcObj,container);
	}
}
function playMovie(src) {
	var flashobj=swfobject.getObjectById('video_player');
	//if (flashobj===null) alert("No movie player");
	if (flashobj===null) flashobj=swfobject.getObjectById('lb_player');
	flashobj.playMovie(src);
}

function showdialog(title,ins,cmd,width) {
	fadeBackground(50);
	var con="";
	if (title=="") title="Dialog box";
	if (ins===null) ins="<div class='loading' style='width:"+width+"px;height:"+width+"px'></div>";
	con="<div id='dialog' style='width:"+width+"px'><div><div class='content'><div class='dialogtitle'><div class='close' onclick='closeDialog()'></div><span onmousedown='dragObject(event,fob(\"dialog\"),null);return false;' id='dialogtitle'>"+title+"</span></div><div id='dialogContent'>"+ins+"</div></div></div></div>";
	if (!(dest=fob("overlays"))) dest=document.body;
	pRH(con,dest);
	cO(fob("dialog"));
	if (cmd!==null) sR.post('/index.php',cmd,rSC);
}
function closeDialog() {
	fadeObject.fade(fob("dialog"),100,0,5,lostDialog);
	loseBackground(50);
}
function lostDialog() {
	removeTheNode("dialog");
}
function selectedValue(selector) {
	return selector.options[selector.selectedIndex].text;
}
function isIE() {
      return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}
function autovalidate(obj) {
	var ok=true;
	var validator='';
	if (/email/.test(obj.id)) {
		ok=validateInput(obj,"email");
	}else{
		ok=validateInput(obj,"dorequire");
	}
	return ok;
}
function validateForm(id) {
	var ok=true;
	var form=fob(id);
	var ips=form.getElementsByTagName("INPUT");
	for(var i=0;i<ips.length;i++) {
		if (/validateForm/.test(ips[i].getAttribute("onkeyup"))) {
			if (!autovalidate(ips[i])) ok=false;
		}
	}
	var ips=form.getElementsByTagName("TEXTAREA");
	for(var i=0;i<ips.length;i++) {
		if (/validateForm/.test(ips[i].getAttribute("onkeyup"))) {
			if (!autovalidate(ips[i])) ok=false;
		}
	}
	var buts=form.getElementsByTagName("BUTTON");
	//buts[0].disabled=ok?"":"disabled";
	buts[0].disabled=!ok;
	buts[0].className=ok?"available":"disabled";
	//buts[0].style.display=ok?"block":"none";
	//alert(ok);
	return ok;
}
function checkInput(src,e,type,len) {
    if (window.event) {
        e=window.event;
        keycode=window.event.keyCode;
    }else{
        keycode=e.which;
    }
    var ok=false;
    if (type=="INT") {
    	if (keycode>47 && keycode<58) ok=true;
    	if (keycode>95 && keycode<106) ok=true;
    }else if(type=="VARCHAR") {
    	ok=true;
    }
    if (src.value.length+1>len) ok=false;
    if (keycode==16) ok=true; //shift
    if (keycode==8) ok=true; //backspace
    if (keycode==37) ok=true; //left
    if (keycode==39) ok=true; //right
    if (keycode==13) ok=true; //enter
    if (keycode==46) ok=true; //del
    if (keycode==20) ok=true; //caps
    if (keycode==9) ok=true; //tab
	return ok;
}
function checkBox(nom,select) {
	var ins="<div class='checkbox"+((select=="1")?"on":"")+"' onclick='clickClickbox(\""+nom+"\")' id='"+nom+"_checker'><input type='hidden' name='"+nom+"' id='"+nom+"' value='"+select+"' /></div>";
	return ins;
}
function clickClickbox(nom) {
	var ip=fob(nom);
	ip.value=1-ip.value;
	fob(nom+"_checker").className='checkbox'+((ip.value=="1")?"on":"");
}
var dropdowns=[];
function dropdown(nom,select,options) {
	dropdowns.push(new dropdown_object(nom,select,options,dropdowns.length));
	return dropdowns[dropdowns.length-1].html();
}

function dropdown_object(nom,selection,options,index) {
	this.object=null;
	this.index=index;
	this.html=html;
	this.open=open;
	this.close=close;
	this.select=select;
	this.options=options;
	this.nom=nom;
	this.initial=selection;
	this.ref='dropdowns['+index+'].';
	function html() {
		var ins="<div class='dropdownContainer'><div class='dropdown' id='"+this.nom+"_selected' onclick='"+this.ref+"open()'>";
		ins+="<div id='"+this.nom+"_value'>"+this.initial+"</div>";
		ins+="<input type='hidden' id='"+this.nom+"' name='"+this.nom+"' value='"+this.initial+"' />";
		ins+="</div>";
		ins+="<div class='rel'>";
		ins+="<div class='blockout' onclick='"+this.ref+"close()' id='"+this.nom+"_blockout'></div>";
		ins+="<div class='drop' id='"+this.nom+"_drop'>";
		for(var i=0;i<this.options.length;i++) {
			ins+="<div class='option' onclick='"+this.ref+"select("+i+")'>"+this.options[i]+"</div>";
		}
		ins+="</div>";
		ins+="</div>";
		ins+="</div>";
		return ins;
	}
	function open() {
		fob(this.nom+"_blockout").style.display="block";
		fob(this.nom+"_drop").style.display="block";
	}
	function close() {
		fob(this.nom+"_blockout").style.display="none";
		fob(this.nom+"_drop").style.display="none";
	}
	function select(i) {
		fob(this.nom+"_value").innerHTML=this.options[i];
		fob(this.nom).value=this.options[i];
		this.close();
	}
}

