var currentpic = 0;

function setLightboxURL(theURL){
	var mainpiclink = document.getElementById('main_img_link');
	if(theURL == "")
	{
		mainpiclink.href = "#";
		mainpiclink.onclick = function () {return false;}	
	}
	else
	{
		mainpiclink = document.getElementById('main_img_link');
		mainpiclink.href = theURL;
		mainpiclink.onclick = function () {showLightbox(this); return false;}	
	}
}

function dobigpic(veh_id) {
	var pic_id = eval(currentpic)+1;
	var gotoURL = "/picpopup.php?veh_id=" + veh_id + "&pic_id=" + pic_id;
	popWin=window.open(gotoURL,"", "left=200,top=200,width=10,height=10,resizable=yes,scrollbars=no,status=yes");
	popWin.focus();
}

function swap(imgIndex) {
    document.main_img.src = document.arrayImages[imgIndex];
	currentpic = imgIndex;
}


function APHmailer() {
	var user = "enquiries";
	var site = "bikehutbristol.co.uk";
	document.write('\x3Ca href="mailto:' + user + '@' + site + '" /\x3E');
	document.write('\x3Cimg src="/images/spacer.gif" alt="Email: ' + user + '@' + site + '" width="250" height="20" border="0" /\x3E\x3C/a\x3E');
}

function APHinfopop(veh_id) {
	var gotoURL = "/infopopup.php?veh_id=" + veh_id;
	popWin=window.open(gotoURL,"", "left=200,top=200,width=10,height=10,resizable=yes,scrollbars=no,status=yes");
	popWin.focus();
}

function checkphone(input){
	var teststring = ("1234567890 ");
	var pass = true;
	for (var i = 0; i < input.length; i++)	{
		var found = false;
		for (var j = 0; j < teststring.length; j++)		{
			if (input.charAt(i) == teststring.charAt(j)) found = true;	}
			if (!found) pass = false;	}
	if(input.length < 8) pass = false;
	return (pass);
 }

function checkenquiry(myForm) 
{
	var myalertstring = "";

	if (myForm.firstname.value=="")
	{
		myalertstring = myalertstring + "Please enter your first name or an initial\n" ;
	}
	if (myForm.lastname.value=="")
	{
		myalertstring = myalertstring +  "Please enter your last name\n" ;
	}
	if ((myForm.contmeth[0].selected)*(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(myForm.email.value))))
	{
		myalertstring = myalertstring + "Please check your e-mail address is correct\n";
	}
	if ((myForm.contmeth[1].selected)*(!(checkphone(myForm.tel_day.value))))
	{
		myalertstring = myalertstring +  "Please check daytime telephone number (only digits 0-9 or spaces)\n" ;
	}
	if ((myForm.contmeth[2].selected)*(!(checkphone(myForm.tel_eve.value))))
	{
		myalertstring = myalertstring +  "Please check evening telephone number (only digits 0-9 or spaces)\n" ;
	}
	if (myalertstring != "")
		{
		alert(myalertstring + "Please correct your entries and try again.");
		return (false);
		}
	return (true);
}

function checkcontact(myForm)
{
	
	var myalertstring = "";
	
	if (myForm.cname.value=="")
		{myalertstring = myalertstring + "Please enter your name\n" ;}
	if (!(checkphone(myForm.ctel_day.value)))
		{myalertstring = myalertstring +  "Daytime telephone number must contain only digits 0-9 or spaces\n" ;}
	if (!(checkphone(myForm.ctel_eve.value)))
		{myalertstring = myalertstring +  "Evening telephone number must contain only digits 0-9 or spaces\n" ;}
	if (!(checkemail(myForm.cemail.value)))
		{myalertstring = myalertstring +  "Please check your email address is correct\n" ;}
	if ((!myForm.ctel_day.value)&&(!myForm.ctel_eve.value)&&(!myForm.cemail.value))
		{myalertstring = myalertstring +  "Please include an email address or telephone number\n" ;}
	if (!myForm.cmessage.value)
		{myalertstring = myalertstring +  "Please include your message\n" ;}
	if (myalertstring != "")
	{
		alert(myalertstring + "Please correct your entries and try again.");
		return (false);
	}
	return (true);
}

function checkemail(checkThisEmail)
{
	var myEMailIsValid = true;
	var myAtSymbolAt = checkThisEmail.indexOf('@');
	var myLastDotAt = checkThisEmail.lastIndexOf('.');
	var mySpaceAt = checkThisEmail.indexOf(' ');
	var myLength = checkThisEmail.length;
	
	// at least one @ must be present and not before position 2
	// @yellow.com : NOT valid
	// x@yellow.com : VALID
	
	if (myAtSymbolAt < 1 ) 
	 {myEMailIsValid = false}
	
	// at least one . (dot) afer the @ is required
	// x@yellow : NOT valid
	// x.y@yellow : NOT valid
	// x@yellow.org : VALID
	
	if (myLastDotAt < myAtSymbolAt) 
	 {myEMailIsValid = false}
	
	// at least two characters [com, uk, fr, ...] must occur after the last . (dot)
	// x.y@yellow. : NOT valid
	// x.y@yellow.a : NOT valid
	// x.y@yellow.ca : VALID
	
	if (myLength - myLastDotAt <= 2) 
	 {myEMailIsValid = false}
	
	// no empty space " " is permitted (one may trim the email)
	// x.y@yell ow.com : NOT valid
	
	if (mySpaceAt != -1) 
	 {myEMailIsValid = false}
	
	return myEMailIsValid;
}

function picpopup(type,picurl)
{
	switch(type)
	{
		case 'itm':
			picurl = picurl.replace(/m.jpg/, "l.jpg");
			var gotoURL = "/picview.htm?url=" + picurl;
			popWin=window.open(gotoURL,"", "left=200,top=200,width=10,height=10,resizable=yes,scrollbars=yes");
			popWin.focus();
		break;
	}
}

function tog(me)
{
	for (var i = 0; i < menuelements.length; i++){
		if (menuparents[i] == me.id){
			target = document.getElementById(menuelements[i]);
			if (target.style.display == ""){
				target.style.display == "none";
				dohide(menuelements[i]);}
			else if (target.style.display == "none"){
				target.style.display = "";}	} }
}

function dohide(elementid)
{
	document.getElementById(elementid).style.display = "none";
	for (var i = 0; i < menuelements.length; i++){
		if (menuparents[i] == elementid){
			document.getElementById(menuelements[i]).style.display = "none";
			dohide(menuelements[i]); } }
}

function preloadImages()
{
	var numpics = arguments.length;
	document.arrayImages = new Array(numpics);
	for (var i=0; i < numpics; i++)
	{
    	var preload = new Image();
		document.arrayImages[i] = arguments[i];
    	preload.src = document.arrayImages[i];
	}
}

function dolink(part1,part2,ext,myclass) {
	document.write('\x3Cspan class="' + myclass + '"\x3E\x3Ca href=\"mailto:' + part1 + '@' + part2 + '.' + ext + '\" \x3E');
	document.write('' + part1 + '@' + part2 + '.' + ext + '\x3C/' + 'a>\x3C/span\x3E');
}
