// --- Page Load Event Handlers & Cookie / Cursor Sniff ---
function event_handler()
	{	
	//Buy Button Mouseover
	var a = document.getElementsByTagName("INPUT")
	for (var b=0;b<a.length;b++)
		{
		if (a[b].className=="btn" ||  a[b].className=="opt btn" || a[b].className=="e btn")
			{
			a[b].onmouseover = function(){cursor_sniff(this.style)}
			a[b].onmouseout  = function(){}
			if (a[b].className!="opt btn" && a[b].className!="e btn")
				a[b].onclick	 = function(){if(this.id){num=this.id.replace(/b/,'');if(parseInt(num))
					{
					if (this.value=='Enquire')		
						location.href='/poa.asp?stock_id='+num
					else
						location.href='/shopcart.asp?mode=add&stock_id='+num
					}
				}}
			}
		}
	//Multi Option Buy
	var a = document.getElementsByTagName("SELECT")
	for (var b=0;b<a.length;b++)
		{
		if (a[b].className=="add")
			{
			a[b].onchange = function(){if(this.id){num=this.id.replace(/b/,'');if(parseInt(num)){location.href='/shopcart.asp?mode=add&stock_id='+num+'&opt='+this.value}}}
			}
		}
	//Menu 
	var a = document.getElementsByTagName("UL")
	for (var b=0;b<a.length;b++)
		{
		if (a[b].className=="menu")
			{
			for (i=0;i<a[b].childNodes.length;i++)
				{
				li=a[b].childNodes[i]
				if(li.nodeName=="LI")
					{
					li.onmouseover 	= function(){cursor_sniff(this.style)}
					li.onclick 		= function(){reset_submenus();this.style.background="url(/images/li2.gif) no-repeat 10px 9px";sub_menu = this.innerHTML.split("\n",1).toString();set_cookie('sub',sub_menu);for (j=0;j<this.childNodes.length;j++){if(this.childNodes[j].nodeName=="UL")this.childNodes[j].style.display="block"}}
					}
				}	
			}
		}		
	//# Focus
	if (document.getElementById("focus"))document.getElementById("focus").focus()
	//# Drop Downs
	if(document.getElementById("catbox"))
		{
		populate_sub(document.getElementById("catbox").value,"ddown5","ddown2")
		populate_sub(document.getElementById("catbox").value,"ddown6","ddown3")
		l=0;h=0;w=0
		drop_down("ddown1","selbtn","catbox")
		drop_down("ddown2","selbtn2","catbox2")
		drop_down("ddown3","selbtn3","manbox")
		drop_down("ddown4","selbtn4","countrybox")
		}
	}
	
function flash_movie()
	{
	document.write ('<p id=flash><object type="application/x-shockwave-flash" data="/includes/flash.swf" width="756" height="140">')
	document.write ('<param name="movie" value="/includes/flash.swf"/>')
	document.write ('<param name="wmode" value="transparent"/>')
	document.write ('</object></p>') 
	}
	
function show_basket(url)
	{location.href=url+'shopcart.asp'}

		
function drop_down(drop,btn,box)
	{
	var id=document.getElementById(drop);var s=document.getElementById(btn); var c=document.getElementById(box);
	if (id)
		{
		if (document.all){w=32;h=111}
		id.onfocus=function(){this.style.display="none"}
		c.onclick=function(){id.style.display="none"}
		s.onmouseover=function(){s.src="/images/select-over.gif"}
		s.onmousedown=function(){l=0;id.style.left=c.offsetLeft+w+"px";id.style.top=(c.offsetTop+22)+h+"px";close_select();id.style.display="block"}
		s.onmouseout=function(){l=1;s.src="/images/select.gif"}
		document.body.onclick=function(){if(l==1){l=0;close_select()}}
		var a=id.getElementsByTagName("LI");
		for(var b=0;b<a.length;b++)
			{
			a[b].onmouseover=function(){this.style.background="#316AC5";this.style.color="#fff"}
			a[b].onmouseout=function(){this.style.background="#fff";this.style.color="#000"}
			a[b].onclick=function(){if(document.all)c.value=this.innerText;else c.value=this.textContent;if(drop=='ddown1')populate_sub(c.value,"ddown5","ddown2");populate_sub(document.getElementById("catbox").value,"ddown6","ddown3");drop_down("ddown2","selbtn2","catbox2");drop_down("ddown3","selbtn3","manbox");close_select()}
			}	
		}	
	}
	
function populate_sub(cat,s,d)
	{
	source=document.getElementById(s);dest=document.getElementById(d);dest.innerHTML=''
	for (i=0;i<source.childNodes.length;i++){if(source.childNodes[i].className.replace("_"," ")==cat){if(document.all)dest.innerHTML+='<li>'+source.childNodes[i].innerText+'</li>';else dest.innerHTML+='<li>'+source.childNodes[i].textContent+'</li>'}}
	}
	
function close_select()
	{for(i=1;i<=4;i++){var d=eval('document.getElementById("ddown'+i+'")');d.style.display="none"}}


function cursor_sniff(obj)
	{if (document.all)obj.cursor="hand";else obj.cursor="pointer"}

function reset_submenus()
	{
	var a = document.getElementsByTagName("UL")
	for (var b=0;b<a.length;b++){if (a[b].className=="menu"){for (i=0;i<a[b].childNodes.length;i++){li=a[b].childNodes[i];if(li.nodeName=="LI"){li.childNodes[1].style.display="none";li.style.background="url(/images/li.gif) no-repeat 10px 9px"}}}}
 	}
	
function set_cookie(c,v)
	{document.cookie=c+ "=" +escape(v)+";path=/;1"}

function get_cookie(c_name)
	{
	c_start=document.cookie.indexOf(c_name + "=")
	if (c_start!=-1)
		{ 
		c_start=c_start + c_name.length+1;c_end=document.cookie.indexOf(";",c_start)
		if (c_end==-1)c_end=document.cookie.length
		return unescape(document.cookie.substring(c_start,c_end))
		} 
	}

function image_reload(c)
	{cc=c.options[c.selectedIndex].text;document.getElementById("ccard").src='/images/' + cc + '.gif'}
	
// --- Form Validation ---
function checker(obj,msg)
	{if(obj.value=="" && mt==true)message(obj,msg)}
	
function message(obj,msg)
	{alert(msg);obj.focus();mt=false}
	
function validate_search(fm)
	{
	mt=true;
	checker(fm.searchfor,'You must enter some text')
	return mt
	}

function contact(fm)
	{
	mt=true;
	checker(fm.sender_name,'You must enter your name')
	email(fm.sender_email)
	checker(fm.enquiry,'You must enter your enquiry')
	checker(fm.captcha,'Please verify the letters displayed on the screen')
	return mt
	}
	
function checkout(fm)
	{
	mt=true;vmr(fm);	
	checker(fm.fname,'You must enter your first name')
	checker(fm.surname,'You must enter your Surname')
	checker(fm.billingaddress1,'You must enter the first line of your address')
	checker(fm.billingaddress2,'You must enter the second line of your address')
	checker(fm.billpcode,'You must enter your Post Code')
	email(fm.email)
	checker(fm.tel,'You must enter a telephone number')
	card_checker(fm)	
	return mt;
	}
	
function poa(fm)
	{
	mt=true;vmr(fm);	
	checker(fm.fname,'You must enter your first name')
	checker(fm.surname,'You must enter your Surname')
	checker(fm.billingaddress1,'You must enter the first line of your address')
	checker(fm.billingaddress2,'You must enter the second line of your address')
	checker(fm.billpcode,'You must enter your Post Code')
	email(fm.email)
	checker(fm.tel,'You must enter a telephone number')
	return mt;
	}
	
function email(obj)
	{
	var emailStr=obj.value;var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)!£$%^&*()+='#~?<>@,;:\\\\\\\"\\.\\[\\]";var validChars="\[^\\s" + specialChars + "\]";var quotedUser="(\"[^\"]*\")";var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+';var word="(" + atom + "|" + quotedUser + ")";var userPat=new RegExp("^" + word + "(\\." + word + ")*$");var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");var matchArray=emailStr.match(emailPat)
	if ((matchArray==null) && mt==true){message(obj,"Email address seems incorrect (check @ and .'s)")}
	if (mt==true){var user=matchArray [1];var domain=matchArray [2];if (user.match(userPat)==null)message(obj,"The username doesn't seem to be valid.")}
	if (mt==true){var IPArray=domain.match(ipDomainPat);if (IPArray!=null){for (var j=1;j<=4;j++){if (IPArray[j]>255)message(obj,"Destination IP address is invalid!")}}}
	if (mt==true){var domainArray=domain.match(domainPat);if (domainArray==null)message(obj,"The domain name doesn't seem to be valid.")}
	if (mt==true){var atomPat=new RegExp(atom,"g");var domArr=domain.match(atomPat);var len=domArr.length;if(domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4)message(obj,"The address must end in a two,three or four letter domain")}
	if ((len<2) && mt==true)message(obj,"This address is missing a hostname!");
	}
	
function vmr(fm)
	{
	if (document.getElementById("vmr"))
		{
		checker(fm.horses,'Please give details of any horses or enter 0')
		checker(fm.dogs,'Please give details of any dogs or cats or enter 0')
		checker(fm.livestock,'Please give details of any livestock or poultryor enter 0')
		checker(fm.other,'Please give details of any other animals or enter 0')	
		checker(fm.pregnant,'Please give details of any pregnant, lactating or undergoing longterm treatment animals or enter 0')	
		}
	}
	
//Card Checker
function card_checker(fm)
	{
	ccard=fm.creditcard;Str=ccard.value;newStr='';card=fm.card.options[fm.card.selectedIndex].text;
	exp_month=fm.expirymonth.options[fm.expirymonth.selectedIndex].text;exp_year=fm.expiryyear.options[fm.expiryyear.selectedIndex].text;
	val_month=fm.startmonth.options[fm.startmonth.selectedIndex].text;val_year=fm.startyear.options[fm.startyear.selectedIndex].text;
	d=new Date();cur_month=d.getMonth()+1;cur_year=d.getFullYear()
	if (card=='Select' && mt==true)message(ccard,"You must select a Card Type")
	//Remove Dashes and Spaces & Numeric Check
	for (var k=0;k<parseInt(Str.length);k++){var a=Str.charAt(k);if((a!=" ")&&(a!="-")){newStr += Str.charAt(k);}};for(var k=0;k<parseInt(newStr.length);k++){var a=newStr.charAt(k);if(((a<"0")||(a>"9"))&&mt==true)message(ccard,"Numbers only Please")}
	if((Str==""||newStr.length<"10")&&mt==true)message(ccard,"Incorrect Number of Digits")
	//Modulus 10 Checker
	tot=0;z=parseInt(newStr.charAt(0));if(newStr.length%2=="0" && mt==true)bigger_than_5();else tot=tot+z;x=1;y=parseInt(newStr.length);while(x<y){z=parseInt(newStr.charAt(x));if(newStr.length%2=="0" && mt==true){if(x%2!= 0)tot=tot+z;else bigger_than_5()}else{if(x%2!=0)bigger_than_5();else tot=tot+z};x++};if(tot%10!=0 && mt==true)message(ccard,"Invalid Card Number")
	//Check Start Date & Expiry
	if (((val_year==cur_year && val_month>cur_month)||(val_year>cur_year)) && mt==true)message(fm.startmonth,"Your start date is incorrect")
	if ((exp_year<cur_year)||(exp_year==cur_year && exp_month<cur_month) && mt==true)message(fm.expirymonth,"Your Card has expired")
	checker(fm.cv2,'You must enter your card signature code on the back of your card')
	}

function bigger_than_5()
	{if(z*2>=10){num=z*2;n=num.toString();n1=parseInt(n.charAt(0));n2=parseInt(n.charAt(1));tot=tot + n1 + n2;}else tot=tot+(z*2)}	

window.onload=event_handler