// main.js Functions for berrystainedglass.co.uk (c) K.J. Petrie 2005 version 11/10/05

//Controls
ElementBackgroundColour=[0,0,0];
ElementFadeColour=[185,185,185];
ElementFadeInterval=40;
ElementFadeTime=1200;
ElementWaitTime=1000;
ElementRotationTime=4000;

//Globals
CurrentColourFull=[];
CurrentlyDisplayedElementNo=0;
NextFade=0;
NextFadeValid=false;
NextRotation=0;

//Main

//Functions
function CheckContactForm(form){
	with(form){
		if((typeof(name.value)=="undefined")||(name.value=='')||(name.value=="void")||(name.value=="null")){
			alert('Please fill in your name');
			name.focus();
			return false;
		}
		if((typeof(email.value)=="undefined")||(email.value=='')||(email.value=="void")||(email.value=="null")){
			alert('Please fill in your E-mail address');
			email.focus();
			return false;
		}
		if(((typeof(telephoneO.value)=="undefined")||(telephoneO.value=='')||(telephoneO.value=="void")||(telephoneO.value=="null"))&&((typeof(messageO.value)=="undefined")||(messageO.value=='')||(messageO.value=="void")||(messageO.value=="null")||(messageO.value.match(/telephone me/i)))){
			alert('Please fill in your telephone number or ask a specific question which can be answered by E-mail');
			telephoneO.focus();
			return false;
		}
		if(typeof(validateO.value)!="undefined"){
			validateO.value='';
		}
	}
	return true;
}

function FadeElement(idname,idnum,startcolour,endcolour){
	while(typeof(startcolour[0])!='number'){// MSIE gets confused - needs arrays passed in brackets -sometimes!
		startcolour=startcolour[0];
	}
	while(typeof(endcolour[0])!='number'){
		endcolour=endcolour[0];
	}
	var numsteps=Math.round(ElementFadeTime/ElementFadeInterval);
	var redstep=(endcolour[0]-startcolour[0])/numsteps;
	var greenstep=(endcolour[1]-startcolour[1])/numsteps;
	var bluestep=(endcolour[2]-startcolour[2])/numsteps;
	CurrentColourFull=startcolour.slice(0);
	if(NextFadeValid){
		window.clearInterval(NextFade);
		NextFadeValid=false;
	}
	NextFade=window.setInterval("FadeStep('"+idname+"',"+idnum+","+redstep+","+greenstep+","+bluestep+",["+endcolour+"]);",ElementFadeInterval);//TestWin.document.writeln('<plaintext>NextFade='+NextFade+"\n"+idname+"',"+idnum+","+redstep+","+greenstep+","+bluestep+","+endcolour+","+startcolour+'typeof endcolour='+typeof endcolour);TestWin.document.close();
	NextFadeValid=true;
}

function FadeStep(idname,idnum,redstep,greenstep,bluestep,endcolour){
	var el=document.getElementById(idname+idnum);
	while(typeof(endcolour[0])!='number'){
		endcolour=endcolour[0];
	}
	CurrentColourFull[0]+=redstep;
	CurrentColourFull[1]+=greenstep;
	CurrentColourFull[2]+=bluestep;
	if(((redstep==0)||((redstep>0)&&(CurrentColourFull[0]>endcolour[0]))||((redstep<0)&&(CurrentColourFull[0]<endcolour[0])))&&((greenstep==0)||((greenstep>0)&&(CurrentColourFull[1]>endcolour[1]))||((greenstep<0)&&(CurrentColourFull[1]<endcolour[1])))&&((bluestep==0)||((bluestep>0)&&(CurrentColourFull[2]>endcolour[2]))||((bluestep<0)&&(CurrentColourFull[2]<endcolour[2])))){
		if(el.style){
			el.style.color="rgb("+endcolour[0]+","+endcolour[1]+","+endcolour[2]+")";
			if(NextFadeValid){
				window.clearInterval(NextFade);
				NextFadeValid=false;
			}
		}
	}
	else{
		if(el.style){
			el.style.color="rgb("+Math.round(CurrentColourFull[0])+","+Math.round(CurrentColourFull[1])+","+Math.round(CurrentColourFull[2])+")";
		}
	}
}

function GetElement(id){
	if(document.all && eval("document.all."+id)){
		return eval("document.all."+id);
	}
}


function instabook_newWindow(obj){
  return(!window.open(obj.href,'other'));
}

function RotateElement(idname,highnumber){
	if(!NextFadeValid){// Skip rotation if Windows has let fade timer slow down
		var el=document.getElementById(idname+CurrentlyDisplayedElementNo);
		if(typeof(el.style)!="undefined"){
			el.style.display="none";
			CurrentlyDisplayedElementNo=(CurrentlyDisplayedElementNo==highnumber)?0:CurrentlyDisplayedElementNo+1;
			el=document.getElementById(idname+CurrentlyDisplayedElementNo);
			el.style.color="rgb("+ElementBackgroundColour[0]+","+ElementBackgroundColour[1]+","+ElementBackgroundColour[2]+")";
			el.style.display="list-item";
			FadeElement(idname,CurrentlyDisplayedElementNo,ElementBackgroundColour.slice(0),ElementFadeColour.slice(0));
			window.setTimeout("FadeElement('"+idname+"',"+CurrentlyDisplayedElementNo+",["+ElementFadeColour.slice(0)+"],["+ElementBackgroundColour.slice(0)+"]);",(ElementFadeTime+ElementWaitTime));
		}
	}
}

function StartRotation(idname, highnumber){//TestWin=window.open('about:blank');TestWin.document.writeln('<html><body><plaintext>');
	if(document.getElementById){
		for(var i=1;i<highnumber+1;i++){
			var el=document.getElementById(idname+i);// alert (el);
			if(el.style){
				el.style.display="none";
			}
		}
		CurrentlyDisplayedElementNo=0;
		NextRotation=setInterval("RotateElement('"+idname+"',"+highnumber+");", ElementRotationTime);// TestWin.document.writeln('NextRotation='+NextRotation);
	}
}
