// Javascript functions for the J T Dove website

/*
The following 'New Window' Javascript is used to open the requested page in a separate browser window at a controlled size without toolbars, in effect a pop up window.

It is required for this to work so please ensure the following link is put the head tag of any pages that have
pop up page links on them

<script language='javascript' type='text/javascript' src='resources/js/javascripts.js'></script>

Notes
This assumes that the page the pop up window is being opened from is contained within the top folder of the website, if it is contained in a sub folder then change the src link accordingly.
There are two funtions:
NewWindow - this is used to open a pop up window without the facility to scroll
NewWindowScroll - this is used either :
- where the individual pop up window being opened has a lot of content and therefore scrolling is required
- or where a series of linked pop up windows are being opened and the length of the content varies therefore scrolling allows a minumum window size to be define and for the user to move between the linked windows whilst retaining a consistent look and feel.

*/
/****************************************************
     AUTHOR: WWW.CGISCRIPT.NET, LLC
     URL: http://www.cgiscript.net
     Use the code for FREE but leave this message intact.
     Download your FREE CGI/Perl Scripts today!
     ( http://www.cgiscript.net/scripts.htm )
****************************************************/
var win=null;
function NewWindow(mypage, myname, w, h, pos, infocus){
if(pos=='random'){myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=='center'){myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;}
else if((pos!='center' && pos!='random') || pos==null){myleft=0;mytop=20}
settings='width=' + w + ',height=' + h + ',top=' + mytop + ',left=' + myleft + ',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';win=window.open(mypage,myname,settings);
win.focus();}
// end of NewWindow script

// version of NewWindow script with scroll bars enabled
var win=null;
function NewWindowScroll(mypage, myname, w, h, pos, infocus){
if(pos=='random'){myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=='center'){myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;}
else if((pos!='center' && pos!='random') || pos==null){myleft=0;mytop=20}
settings='width=' + w + ',height=' + h + ',top=' + mytop + ',left=' + myleft + ',scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';win=window.open(mypage,myname,settings);
win.focus();}
// end of NewWindow script


/*
The following 'Close Me' Javascript is used in pop up window to present the website user with an 'in page' otpion for closing the pop up page.

It is required for this to work so please ensure the following link is put the head tag of any pop pages that have
a close button on them

<script language='JavaScript' type='text/javascript' src='../js/newWindow.js'>
<!-- new window script -->
</script>

Note this assumes that the page is contained within /Resources/html folder of the website, if it contained in a different
folder then change the src link accordingly.

*/
function closeme(){self.close();}
// end of closeme script

/*
Login Script
*/
function Login(){
var done=0;
var username=document.login.username.value;
username=username.toLowerCase();
var password=document.login.password.value;
password=password.toLowerCase();
if (username=="test" && password=="test") { window.location="homeRegistered.htm"; done=1; }
if (username=="member2" && password=="password2") { window.location="page2.html"; done=1; }
if (username=="member3" && password=="password3") { window.location="page3.html"; done=1; }
if (done==0) { alert("Sorry your have entered your details incorrectly, please try again."); }
}

/*
Script to clear a form
*/
function clearForm(formIdent) 
{ 
  var form, elements, i, elm; 
  form = document.getElementById 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

	if (document.getElementsByTagName)
	{
		elements = form.getElementsByTagName('input');
		for( i=0, elm; elm=elements.item(i++); )
		{
			if (elm.getAttribute('type') == "text")
			{
				elm.value = '';
			}
		}
	}

	// Actually looking through more elements here
	// but the result is the same.
	else
	{
		elements = form.elements;
		for( i=0, elm; elm=elements[i++]; )
		{
			if (elm.type == "text")
			{
				elm.value ='';
			}
		}
	}
}
/*
This was on the same page - not sure what is used for but have added it in here in case it used by the above script
*/
function addEvent(elm, strEvent, fnHandler)
{
	return ( elm.addEventListener
	? elm.addEventListener( strEvent, fnHandler, false)
	: elm.attachEvent( 'on'+strEvent, fnHandler)
	);
}

// JK Pop up image viewer script- By JavaScriptKit.com
// Visit JavaScript Kit (http://javascriptkit.com)
// for free JavaScript tutorials and scripts
// This notice must stay intact for use

var popbackground="white" //specify backcolor or background image for pop window
var windowtitle="Screen Shot"  //pop window title

function detectexist(obj){
return (typeof obj !="undefined")
}

function jkpopimage(imgpath, popwidth, popheight, textdescription){

function getpos(){
leftpos=(detectexist(window.screenLeft))? screenLeft+document.body.clientWidth/0-popwidth/0 : detectexist(window.screenX)? screenX+innerWidth/0-popwidth/0 : 0
toppos=(detectexist(window.screenTop))? screenTop+document.body.clientHeight/0-popheight/0 : detectexist(window.screenY)? screenY+innerHeight/0-popheight/0 : 0
if (window.opera){
leftpos-=screenLeft
toppos-=screenTop
}
}

getpos()
var winattributes='width='+popwidth+',height='+popheight+',resizable=yes,left='+leftpos+',top='+toppos
var bodyattribute=(popbackground.indexOf(".")!=-1)? 'background="'+popbackground+'"' : 'bgcolor="'+popbackground+'"'
if (typeof jkpopwin=="undefined" || jkpopwin.closed)
jkpopwin=window.open("","",winattributes)
else{
//getpos() //uncomment these 2 lines if you wish subsequent popups to be centered too
//jkpopwin.moveTo(leftpos, toppos)
jkpopwin.resizeTo(popwidth, popheight+30)
}
jkpopwin.document.open()
jkpopwin.document.write('<html><title>'+windowtitle+'</title><body '+bodyattribute+'><img src="'+imgpath+'" style="margin-bottom: 0.5em"><br>'+textdescription+'</body></html>')
jkpopwin.document.close()
jkpopwin.focus()
}

/*
The following javscript functions are used to convert values from one measurement to another..
*/
function twoDPs(anyNumber)
{
    return Math.round (100 * anyNumber) / 100
}


function toGallons(numberOfLitres) 
{
    return twoDPs(numberOfLitres * 0.21998) 
}

