//cross-browser dhtml vars
//netscape 4.x has passed its use-by date
var ie4=document.all && !document.getElementById;
var pagewidth = 0;
var divwidth=804; //for positioning photo divs & zoom
var currentphoto=0;
var bigpvis=0;
var showbigpics = true;

var loaded=false;
if(ie4) {
    document.getElementById = new Function('var expr = /^\\w[\\w\\d]*$/, elname=arguments[0]; if(!expr.test(elname)) { return null; } else if(eval("document.all."+elname)) { return eval("document.all."+elname); } else return null;')
}

function init() {
	pagewidth=getPagewidth();
	//disable bigpics
	showbigpics = (page=='furniture' || page=='lighting') ? false : true;
}

//------------------------------------
//GENERAL DHTML ETC.

function movediv(divid, l, t) {
	document.getElementById(divid).style.left=l+'px';
	document.getElementById(divid).style.top=t+'px';
}
function showdiv(divid, v) {
	document.getElementById(divid).style.display = v ? 'block' : 'none' ;
}
function updatecontent(divid, content) {
	if(!ie4 && !document.getElementById) return;
	document.getElementById(divid).innerHTML = '';
	document.getElementById(divid).innerHTML = content;
}
function getPagewidth() {
	if(self.innerwidth) {
		//alert('self.innerwidth');
		return self.innerwidth;
	}
	if(document.documentElement.clientWidth) {
				//alert('document.documentElement.clientWidth='+document.documentElement.clientWidth);
		return document.documentElement.clientWidth;
	}
	if(document.body.clientWidth) {
				//alert('document.body.clientWidth');
		return document.body.clientWidth;
	}
	return false;
}

//------------------------------------
//NAVIGATION & ROLLOVERS

var a = new Array();
function buttonover(el,over) {
	document.getElementById(el).style['backgroundColor'] = (over) ? '#f2f1f1' : '#e3e2e2';
	a=document.getElementById(el).getElementsByTagName('a');
	a[0].style['color'] = (over) ? '#bc4138' : '#585857';
 	document.getElementById(el).style.cursor = 'pointer';
}

function navigate(url) {
	window.location=url;
	return false;
}

//quick rollover - no preload / imgstate replaces last char before ext
function imgover(imgid, imgstate) {
	if(!document.images) return;
	var imgsrc = document.images[imgid].src;
	//don't allow rollovers if last char == 2
	var lastchar = imgsrc.substring((imgsrc.length-5),(imgsrc.length-4));
	if(lastchar=='2') return;
	document.images[imgid].src = imgsrc.substring(0,(imgsrc.length-5))+imgstate+imgsrc.substring((imgsrc.length-4),imgsrc.length);
}

//------------------------------------
//PHOTO GALLERIES

var timerID=null;
var photodivs = new Array('bigpbg','bigphoto');
var zoom='in';
var zoomvis=0;

function initphotos() {
	pagewidth=getPagewidth();

	//bic pics disabled for furniture page
	if(!showbigpics) return;

	showzoom(zoom,zoomvis);
	for (var i=0; i<photodivs.length; i++) {
		positionphotodiv(photodivs[i]);
	}
}

//photos array & p0 set by page
function scrollphotos(d) {
	if(!document.images) {
		alert("Your browser doesn't support the document.images property required for this photo gallery.");
		return;
	}
	if((d*4+p0)>=photos.length) {
		scrollblur();
		return;
	}
	p0 = d*4+p0;
	p0 = (p0<0) ? 0 : p0;
	for(var i=0; i<4; i++) {
		document.images[('thumb'+i)].src = (p0+i>=photos.length) ? 'images/red-lion-marquees/spacer.gif' : 'images/photos/small/'+photos[(p0+i)]+'.jpg';
	}
	//update scroll arrows
	//pscroll_left" src="images/photos/scrollbar/left_2.gif
	// (1-d)/2 etc... convert d=1 or -1 to src last char 0 or 1
	var l= (p0==0) ? '2' : (1-d)/2;
	var r= ((p0+4)>=photos.length) ? '2' : (1+d)/2;
	var last = ((p0+4)>=photos.length) ? photos.length : p0+4;
	
	document.images['pscroll_left'].src = 'images/photos/scrollbar/left_'+l+'.gif';
	document.images['pscroll_right'].src = 'images/photos/scrollbar/right_'+r+'.gif';
	scrollblur();
	var photocount = (p0==photos.length-1) ? photos.length+' of '+photos.length : 'photos: '+(p0+1)+' - '+last+' of '+photos.length;
	updatecontent('pscrollmid', photocount);
}

function scrollblur() {
	document.images['pscroll_left'].blur();
	document.images['pscroll_right'].blur();
}

function thumbover(p) {
	if(!document.getElementById) return;
	//show default cursor if no photo
	if(p0+p>=photos.length) {
		document.getElementById('thumb'+p).style.cursor = 'default';
	} else {
		document.getElementById('thumb'+p).style.cursor = 'pointer';
		document.getElementById('thumb'+p).style.cursor = 'hand';
	}
	//blur photo
	if(document.images) document.images['thumb'+p].blur();
}

function showphoto(p) {
//show medium photo based on current thumbnails
	if(!document.images) {
		alert("Your browser doesn't support the document.images property required for this photo gallery.");
		return;
	}
	document.images['thumb'+p].blur();
	p = parseInt(p) + parseInt(p0);
	if(p>=photos.length) return;
	
	currentphoto=p;	
	document.images['photo'].src='images/photos/medium/'+photos[p]+'.jpg';
	//showzoom('in',1);
	//update text
	updatecontent('photoblurb', '<h3>'+pheadings[p]+'</h3><p>'+ptexts[p]+'</p>');
}

function bigphoto() {
	//bic pics disabled for furniture page
	if(!showbigpics) {
		timerID=setTimeout("clearTimeout(timerID)",100);
		return;
	}
	
	if(!ie4 && !document.getElementById) {
		alert("Sorry, your browser doesn't support the script used to show large photos. Please use a modern web browser and enable JavaScript. (Compatible browsers include Firefox / Internet Explorer / Safari / Opera)");
		return;
	}
	if(bigpvis) {
		hidebigphoto();
		return;
	}
	document.images['photo'].blur();
	//updatecontent('bigp3', '<img src="images/photos/large/'+photos[currentphoto]+'.jpg" class="greyborder" alt="click to close" /><div class="photoblurb"><h3>'+pheadings[currentphoto]+'</h3><p>'+ptexts[currentphoto]+'</p></div>');
	updatecontent('bigp3', '<img src="images/photos/large/'+photos[currentphoto]+'.jpg" border="0" class="greyborder" alt="large photo" />');
	showphotodivs();
	showzoom('out',1);
	timerID=setTimeout("clearTimeout(timerID)",100);
	bigpvis=1;
}

function showphotodivs() {
	for (var i=0; i<photodivs.length; i++) {
		positionphotodiv(photodivs[i]);
		showdiv(photodivs[i],1);
	}
	showzoom('out',1);
}
function showzoom(z,v) {
	//bic pics disabled for furniture page
	if(!showbigpics) {
		document.getElementById('photo').style.cursor = 'default';
		return;
	}

	clearTimeout(timerID);	
	if(pagewidth==0) return;
	movediv('zoomouter',Math.floor((pagewidth-156)/2-divwidth/2)+151+divwidth-31,255);
	if(z.length>0 && document.images) document.images['zoom'].src='images/red-lion-marquees/zoom-'+z+'.gif';
	showdiv('zoomouter',v);
	zoom=z;
	zoomvis=v;
}
function hidezoom() {
	timerID=setTimeout("showzoom('',0)",300);
}
function positionphotodiv(pd) {
	movediv(pd,Math.floor((pagewidth-156)/2-divwidth/2)+156,250);
}
function hidebigphoto() {
	for (var i=0; i<photodivs.length; i++) {
		showdiv(photodivs[i],0);
	}
	showzoom('in',1);
	bigpvis=0;
}

//------------------------------------
//form checking

var required=new Array('_title','name','address','telephone','hire_date','event_details','no_of_guests','seating','marquee','flooring','music','str');
//0=txt / 1=select
var fieldtypes=new Array(1,0,0,0,0,1,0,1,1,1,1,0);
var displaynames=new Array('Title','Name','Address','Telephone number','Hire date','Function','Number of guests','Seating','Marquee','Flooring','Music','3 character verification code');
var missing=new Array();
var fieldstr='';

function verify() {
	var f=document.forms[0];

	//reset in case page was not refreshed
	fieldstr='';
	missing.length=0;
	
	for(var i=0; i<required.length; i++) {
		var val = (fieldtypes[i]==0) ? f.elements[required[i]].value : f.elements[required[i]][f.elements[required[i]].selectedIndex].value;
		if(val.length<1) {
			missing[missing.length]=i;
		}
	}
		
	if (missing.length>0) {
		for(var i=0; i<missing.length;i++) {
			fieldstr+=unescape("%0A%0D")+displaynames[missing[i]];
		}
		alert('Sorry, the form could not be submitted because required information was not supplied. Please complete the following field(s) and submit the form again:'+unescape("%0A")+fieldstr);
		return false;	
	}

	if (!validEmail(f.elements['e-mail'].value)) {
		alert("Please check the email address you entered - it doesn't appear to be valid.");
	    return false;
	}
	
	if (!validCode(f.elements['str'].value)) {
		alert("The 3 digit verification code was not recognised. Please enter it again.");
	    return false;
	}

	return true;
}

function validEmail(str) {
//allow no email address
  if(str.length<1) {
    return true;
  }
  if (window.RegExp) {
    var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
    var reg1 = new RegExp(reg1str);
    var reg2 = new RegExp(reg2str);
    if (!reg1.test(str) && reg2.test(str)) return true;
    return false;
  } else {
    if(str.indexOf("@") >= 0) return true;
    return false;
  }
}

//Okay so it's not exactly uncrackable...
var stringything="ab6r4btzc378";
var codes=4;
function validCode(str) {
	if(str.length!=3) return false;
	for(var i=0; i<3; i++) {
		var c=str.charAt(i).toLowerCase();
		var d=stringything.charAt(codes*i+code);
		if(c!=d) return false;
	}
	return true;
}

var numbers=new Array('zero','one','two','three','four','five','six','seven','eight','nine');
function getcode() {
	var c='';
	var str='';
	for(var i=0; i<3; i++) {
		c=stringything.charAt(codes*i+code);
		str += (isDigit(c)) ? numbers[c] : c.toUpperCase();
		str += (i<2) ? ' ' : '';
	}
	alert('Please enter the following 3 character code:'+unescape("%0A%0D")+str);
}
function isDigit (c) {
	return ((c >= "0") && (c <= "9"));
}
var popupvis=0;
function showpopup() {
	popupvis = !popupvis;
	movediv('popup', parseInt(pagewidth/2), 325);
	showdiv('popup', popupvis);
	if(popupvis) setTimeout("document.onclick = hidepopup",100);
}
function hidepopup() {
	popupvis=1;
	showpopup();
	document.onclick = null;
}
