// Define function to display random images for frontpage (ledt hand side)
function fpimagelh(){
	var random = Math.random() ;
	var fpimagenumlh = Math.round(random*4)+1;
	document.write('<img src="http://www.porscheshop.co.uk/images/frontpage/'+fpimagenumlh+'lh.jpg" border="0" width="198" height="110" alt="Porsche parts, gifts and accessories">')
}
// Define function to display random images for frontpage (centre)
function fpimagec(){
	var random = Math.random() ;
	var fpimagenumc = Math.round(random*4)+1;
	document.write('<img src="http://www.porscheshop.co.uk/images/frontpage/'+fpimagenumc+'c.jpg" border="0" width="199" height="110" alt="Porsche parts, gifts and accessories">')
}
// Define function to display random images for frontpage (right hand side)
function fpimagerh(){
	var random = Math.random() ;
	var fpimagenumrh = Math.round(random*4)+1;
	document.write('<img src="http://www.porscheshop.co.uk/images/frontpage/'+fpimagenumrh+'rh.jpg" border="0" width="198" height="110" alt="Porsche parts, gifts and accessories">')
}
// Define function to display this year
function getThisYear(){
	var dNow = new Date()
	var year = dNow.getFullYear();
	document.write(year)
}
// Add additional table cell to table if the is no "Add to Basket line"
// This maintains the table structure
function check_for_product_quantity(product_quantity) {
	if (product_quantity == "") {
	document.write('<td> </td>');
	}
}
// Check to see if item is POA within the model section
function check_poa(poa) {
	if (poa == "yes") {
	document.write('<span class="bodybold">£POA</span>');
	}
}
// define Harlequin currency convertor
function CurrencyPopup(QueryString)
{
CurrencyWindow = window.open ('', 'CurrencyWindow', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,height=170,width=600')
CurrencyWindow.focus()
CurrencyWindow.location.href = 'http://www.xe.net/ecc/input.cgi?Template=sw&'+QueryString
}	
//Get search by model parameters
function get_model() {
		if (get_cookie("cookiemodel")!=""){
		document.modelsearch.S_model1_0.value = get_cookie("cookiemodel");
		}
}
//write search by brand parameters
function write_brand() {
var wcookiemodel=document.modelsearch.S_model1_0.value;
set_cookie("cookiemodel", wcookiemodel, null, "/");
}
// store info on cookie. 
function set_cookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
//Get cookie routine by Shelley Powers 
function get_cookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}
// delete info on cookie. 
function DeleteCookie(name) {
var expire = new Date ();
expire.setTime (expire.getTime() - (24 * 60 * 60 * 1000)); 
document.cookie = name + "=; expires=" + expire.toGMTString(); 
}
// validate Car for Sale Application form routine
	function valcarforsale() {
		if (document.forms) {
			if (document.forms["frmcarforsale"].name.value.length == 0) {
				alert("You must enter your name in the field provided.");
				document.forms["frmcarforsale"].name.focus();
				return false;
			}
			if (document.forms["frmcarforsale"].address1.value.length == 0) {
				alert("You must enter your address in the field provided.");
				document.forms["frmcarforsale"].address1.focus();
				return false;
			}
			if (document.forms["frmcarforsale"].postcode.value.length == 0) {
				alert("You must enter your postcode in the field provided.");
				document.forms["frmcarforsale"].postcode.focus();
				return false;
			}
			if (!doValidateMail(document.forms["frmcarforsale"].email)) {
				document.forms["frmcarforsale"].email.select();
				document.forms["frmcarforsale"].email.focus();
				return false;			
			}
			if (document.forms["frmcarforsale"].daytimephone.value.length == 0) {
				alert("You must enter your daytime telephone no. in the field provided.");
				document.forms["frmcarforsale"].daytimephone.focus();
				return false;
			}
			if (document.forms["frmcarforsale"].eveningphone.value.length == 0) {
				alert("You must enter your evening telephone no. in the field provided.");
				document.forms["frmcarforsale"].eveningphone.focus();
				return false;
			}
			if (document.forms["frmcarforsale"].model.value.length == 0) {
				alert("You must enter your model in the field provided.");
				document.forms["frmcarforsale"].model.focus();
				return false;
			}
			if (document.forms["frmcarforsale"].year.value.length == 0) {
				alert("You must choose the year your car was built from the list provided.");
				document.forms["frmcarforsale"].year.focus();
				return false;
			}
			if (document.forms["frmcarforsale"].gearbox.value.length == 0) {
				alert("You must choose the gearbox type from the list provided.");
				document.forms["frmcarforsale"].gearbox.focus();
				return false;
			}
			if (document.forms["frmcarforsale"].colour.value.length == 0) {
				alert("You must enter the colour of your car in the field provided.");
				document.forms["frmcarforsale"].colour.focus();
				return false;
			}
			if (document.forms["frmcarforsale"].mileage.value.length == 0) {
				alert("You must enter the mileage of your car in the field provided.");
				document.forms["frmcarforsale"].mileage.focus();
				return false;
			}
			if (document.forms["frmcarforsale"].enginecc.value.length == 0) {
				alert("You must enter the engine capacity of your car in the field provided.");
				document.forms["frmcarforsale"].enginecc.focus();
				return false;
			}
			if (document.forms["frmcarforsale"].askingprice.value.length == 0) {
				alert("You must enter the asking price for your car in the field provided.");
				document.forms["frmcarforsale"].askingprice.focus();
				return false;
			}
			if (document.forms["frmcarforsale"].description.value.length == 0) {
				alert("You must enter the description of your car in the field provided.");
				document.forms["frmcarforsale"].description.focus();
				return false;
			}
			return true;
		} 
	}	
// validate quote form routine
	function valquote() {
		if (document.forms) {
			if (document.forms["frmquote"].name.value.length == 0) {
				alert("You must enter your name in the field provided.");
				document.forms["frmquote"].name.focus();
				return false;
			}
			if (!doValidateMail(document.forms["frmquote"].email)) {
				document.forms["frmquote"].email.select();
				document.forms["frmquote"].email.focus();
				return false;			
			}
			if (document.forms["frmquote"].country.value.length == "") {
				alert("You must choose a delivery country from the list provided.");
				document.forms["frmquote"].country.focus();
				return false;
			}
			if (document.forms["frmquote"].model.value.length == "") {
				alert("You must choose a model of car from the list provided.");
				document.forms["frmquote"].model.focus();
				return false;
			}
			if (document.forms["frmquote"].year.value.length == 0) {
				alert("You must choose the year your car was built from the list provided.");
				document.forms["frmquote"].year.focus();
				return false;
			}
			if (document.forms["frmquote"].itemdescription.value.length == 0) {
				alert("You must enter the item description in the field provided.");
				document.forms["frmquote"].itemdescription.focus();
				return false;
			}
			return true;
		} 
	}	
 //Define the email address validation function
	function doValidateMail(field) {
	var email = field.value;
	var invalid_chars = " /:,;";
	if (email == "") {
		alert("You must enter an email address in the field provided.");			
		return false;
	}
	for (var i = 0;i < invalid_chars.length;i++) {
		bad_char = invalid_chars.charAt(i);
		if (email.indexOf(bad_char,0) > -1) {
			alert("You cannot enter blank space, slash, colon, comma or semicolon characters in an email address.");
			field.focus();
			field.select();
			return false;
		}
	}
	at_pos = email.indexOf("@",1);
	if (at_pos == -1) {
		alert("You must include the @ character in an email address.");
		field.focus();
		field.select();
		return false;
	}
	if (email.indexOf("@",at_pos + 1) > -1) {
		alert("You must have only one @ character in an email address.");
		field.focus();
		field.select();
		return false;
	}
	period_pos = email.indexOf(".",at_pos);
	if (period_pos == -1) {
		alert("You must include the domain extension (ie .com, .co.uk) in an email address.");
		field.focus();
		field.select();
		return false;
	}
	if (period_pos + 3 > email.length) {
		alert("You have entered an invalid domain extension in the email address.");
		field.focus();
		field.select();
		return false;
	}
	return true;
}	
 //Define the textarea function to limit number of charaters
<!-- Original:  Ronnie T. Moore -->
<!-- Dynamic 'fix' by: Nannette Thacker -->
<!-- Web Site: http://www.shiningstar.net -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}
// define MM functions
function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function swapImage() { //v3.0
  var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function swapImgRestore() { //v3.0
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.p) d.p=new Array();
   var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}
function displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.returnValue = true;
}
function ReloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.pgW=innerWidth; document.pgH=innerHeight; onresize=ReloadPage; }}
  else if (innerWidth!=document.pgW || innerHeight!=document.pgH) location.reload();
}