var SITE_URL = "http://www.monkeydevil.co.uk/clients/tmfl/new/";
var DEBUG = true;

$(document).ready(function(){

	topMenuHover();
	sideMenuHover();
	faqreveal();
	setInterval("goslideshow()",6000);
});


function topMenuHover()
{
	$(".menuitem").hover(function(){
		$(this).css({"backgroundPosition": "0px -24px"});
	},
	function(){
		$(this).css({"backgroundPosition": "0px 0px"});
	}
	)
}

function sideMenuHover()
{
	$(".sidemenu a").hover(function(){
		$(this).css({"backgroundPosition": "0px -32px"});
	},
	function(){
		$(this).css({"backgroundPosition": "0px 0px"});
	}
	)
}

function faqreveal()
{
	$('div.answer').hide();
	$('.question').hover(function() {
		$(this).css({"cursor":"pointer"})
	}
	);
//	$('h2.question').click(function() {

		
		/*if($(this).next('div.answer').css('display') == 'block')
		{
			$(this).next('div.answer').slideUp();
		}
		else
		{
			$(this).next('div.answer').slideDown();
		}*/
		//$(this).css({"margin-top":"10px"});
	//}

	$(".question").click(function(){
		//alert("clicked");
		$(this).find(".answer").slideToggle("fast");
	});
}

function goslideshow()
{
	if($('.slideshow img').length > 1)
	{
		currentslide = $('.slideshow IMG.currimage');

		if(currentslide.next().length == 0)
		{
			newslide = $('.slideshow img:first');
		}
		else
		{
			newslide = currentslide.next();
		}

		newslide.css({opacity:1});
		newslide.addClass('nextimage');
		
		currentslide.animate({opacity:0},2000, function() {

			newslide.addClass('currimage');
			currentslide.removeClass('currimage');
		});
	}
}

function checkBillingAddressForm()
{
	err=0;
	errStr = "Please correct the following error(s) and try again:\n";
	if(document.getElementById("fullname").value=="")
	{
		err++;
		errStr += "- Please enter your name\n";
	}
	
	if(document.getElementById("address1").value=="")
	{
		err++;
		errStr += "- Please provide us with your address\n";
	}
	
	if(document.getElementById("town").value=="")
	{
		err++;
		errStr += "- Please enter the name of your town\n";
	}
	
	if(document.getElementById("county").value=="")
	{
		err++;
		errStr += "- Please enter the name of your county\n";
	}
	
	if(document.getElementById("postcode").value=="")
	{
		err++;
		errStr += "- Please enter your postcode\n";
	}
	
	/*if(document.getElementById("country").value=="")
	{
		err++;
		errStr += "-  Please enter your country\n";
	}*/
	
	if(document.getElementById("telephone").value=="")
	{
		err++;
		errStr += "- Please provide a telephone number\n";
	}
	
	if((document.getElementById("email").value=="")||(!validEmail(document.getElementById("email").value)))
	{
		err++;
		errStr += "- Please provide a valid email address\n";
	}
	
	if(document.getElementById("agreeterms").checked==false)
	{
		err++;
		errStr += "- You must agree to our terms and conditions prior to making a purchase\n";
	}

	if(document.getElementById("delivertobill").checked)
	{
		if(err==0)
		{
			document.getElementById("deliveryfullname").value = document.getElementById("fullname").value;
			document.getElementById("deliveryaddress1").value = document.getElementById("address1").value;
			document.getElementById("deliveryaddress2").value = document.getElementById("address2").value;
			document.getElementById("deliverytown").value = document.getElementById("town").value;
			document.getElementById("deliverycounty").value = document.getElementById("county").value;
			document.getElementById("deliverypostcode").value = document.getElementById("postcode").value;
			document.getElementById("ordersummary").value = 1;
		}
	}
	

	if(err>0)
	{
		alert(errStr);
		return false;
	}
	else
	{
		return true;
	}
}

function checkDeliveryAddressForm()
{
	err=0;
	errStr = "Please correct the following error(s) and try again:\n";
	if(document.getElementById("deliveryfullname").value=="")
	{
		err++;
		errStr += "- Please enter your name\n";
	}

	if(document.getElementById("deliveryaddress1").value=="")
	{
		err++;
		errStr += "- Please provide us with your address\n";
	}

	if(document.getElementById("deliverytown").value=="")
	{
		err++;
		errStr += "- Please enter the name of your town\n";
	}

	if(document.getElementById("deliverycounty").value=="")
	{
		err++;
		errStr += "- Please enter the name of your county\n";
	}

	if(document.getElementById("deliverypostcode").value=="")
	{
		err++;
		errStr += "- Please enter your postcode\n";
	}

	if(err>0)
	{
		alert(errStr);
		return false;
	}
	else
	{
		document.getElementById("ordersummary").value = 1;
		return true;
	}
}

function scalePage()
{
	var arrSizes = getPageSize();

	if(document.getElementById("sitebounds"))
	{
		document.getElementById("sitebounds").style.height = "";
		if (document.getElementById("sitebounds").offsetHeight<arrSizes[3])
		{
			document.getElementById("sitebounds").style.height = ""+arrSizes[3]+"px";
			newHeight = arrSizes[3];
			if (self.innerHeight)
			{
				newHeight -= 163;
			}
			else
			{
				newHeight -= 160;
			}
			document.getElementById("pagecont").style.height = "" + newHeight + "px";
		}
		else
		{
			//alert("page is higher");
			document.getElementById("sitebounds").style.height = document.getElementById("sitebounds").offsetHeight + "px";

			newHeight =  document.getElementById("sitebounds").offsetHeight;
			if (self.innerHeight)
			{
				newHeight -= 163;
			}
			else
			{
				newHeight -= 160;
			}
			
			document.getElementById("pagecont").style.height = "" + newHeight + "px";
			
		}
	}
	scaleMenu();
}	

function scaleMenu()
{
	var arrSizes = getPageSize();


	//alert("called");
       //Does the container Exists?!!1
	if(document.getElementById("sitemenu"))
	{
			//Doe the height of the container equal less than the screen height.!!
			//document.title = "Screen "+arrSizes[3]+" Div Height "+document.getElementById("container").offsetHeight+"";
			//alert("Screen "+arrSizes[3]+" Div Height "+document.getElementById("sitebounds").offsetHeight);

			//Clear height!

		//alert(document.getElementById("sitebounds").style.height);
		//alert(document.getElementById("sitemenu").style.height)
		document.getElementById("sitemenu").style.height ="";
		newHeight = document.getElementById("sitebounds").style.height;
		newHeight = parseInt(newHeight.replace("px",""));
		//alert(newHeight);
		if (self.innerHeight)
		{
			newHeight -= 163;
		}
		else
		{
			newHeight -= 160;
		}		

		//alert(newHeight);
		
		document.getElementById("sitemenu").style.height = ""+newHeight+"px";
		if(document.getElementById("sidebar"))
		{
			if (self.innerHeight)
			{
				newHeight -= 40;
			}
			
			document.getElementById("sidebar").style.height = ""+newHeight+"px";
		}


/*		var newHeight = arrSizes[1];

		if (self.innerHeight)
		{
			newHeight -= 163;
		}
		else
		{
			newHeight -= 143;
		}
		
		if (document.getElementById("sitemenu").offsetHeight<newHeight)
		{
			
			//alert("Expand");
			document.getElementById("sitemenu").style.height = ""+newHeight+"px";
			if(document.getElementById("sidebar"))
			{
				if (self.innerHeight)
				{
					newHeight -= 40;
				}
				
				document.getElementById("sidebar").style.height = ""+newHeight+"px";
			}
		}*/
	}

}


function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;

	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		//xScroll = document.body.scrollWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.scrollWidth;
		//windowHeight = document.body.offsetHeight;
		windowHeight = document.documentElement.scrollHeight;
		//windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	if (navigator.appName == "Microsoft Internet Explorer")
		{yposition = document.body.scrollTop;}
	else {yposition = window.pageYOffset;}

	if (navigator.appName == "Microsoft Internet Explorer")
		{xposition = document.body.scrollLeft;}
	else {xposition = window.pageXOffset;}	

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight, xposition, yposition);
	return arrayPageSize;
}
function init() 
{
	if (arguments.callee.done) return;
	arguments.callee.done = true;

	      //DOM has now loaded!
	      //alert("hi");
		FullScreenContainer();

};

	/* for Mozilla */
if (document.addEventListener) 
{
	document.addEventListener("DOMContentLoaded", init, false);
}

	/* for Internet Explorer */
	/*@cc_on @*/
	/*@if (@_win32)
                document.write("<script defer src="+SITE_URL+"/ie_onload.js><"+"/script>");
        /*@end @*/

	/* for other browsers */
	window.onload = init;
	window.onresize = function()
	{
		FullScreenContainer();
	}

	function FullScreenContainer()
	{
		var arrSizes = getPageSize();

		
		//alert("called");
	       //Does the container Exists?!!1
		if(document.getElementById("contentarea"))
		{
			//Doe the height of the container equal less than the screen height.!!
//			document.title = "Screen "+arrSizes[3]+" Div Height "+document.getElementById("container").offsetHeight+"";
			//alert("Screen "+arrSizes[3]+" Div Height "+document.getElementById("pagecontainer").offsetHeight);

			//Clear height!
			document.getElementById("contentarea").style.height = "";

			if (document.getElementById("contentarea").offsetHeight<arrSizes[3])
			{
				//alert("Expand");
				document.getElementById("contentarea").style.height = ""+arrSizes[3]+"px";
			}
		}
	}


//CONTACT PAGE RELATED JAVASCRIPT
/////////////////////////////////////

function validate_contact_form()
{
	errstr = "";
	errcount = 0;

	//Check name
	if(document.getElementById("contact_name").value.length < 1)
	{
		errstr += "You must enter a name before submitting the contact form\n";
		errcount++;
	}

	//Check email
	email = document.getElementById("contact_email").value;
	
	if(email.length < 1)
	{
		errstr += "You must enter a contact email address before submitting the contact form\n";
		errcount++;
	}
	else
	{
		if(!validEmail(email))
		{
			errstr += "You must enter a valid email address before submitting the contact form\n";
			errcount++;
		}
	}

	//Check query text
	if(document.getElementById("contact_query").value.length < 1)
	{
		errstr += "You must enter some text in the query area before submitting the contact form\n";
		errcount++;
	}

	if(errcount == 0)
	{
		document.getElementById("contact_form").submit();
	}
	else
	{
		alert(errstr);
	}
}

function validEmail(email)
{
	var rPattern = "^[^0-9][A-z0-9-_]+([.][A-z0-9-_]+)*[@][A-z0-9_-]+([.][A-z0-9_-]+)*[.][A-z]{2,4}$";
	var re = new RegExp(rPattern);

	if (email.match(re)) 
	{
		return true;
	} 
	else 
	{
		return false;
	}
}


var out_fade =
{
fadeout : function (id,curropacity,opacityToFade,bHidden)
	  {
		  var opacity = curropacity;
		  if(bHidden)
		  {
			  document.getElementById(id).style.display='';
		  }
		  this.fadeLoop(id, opacity,opacityToFade); // remember its not  "->" function name.. its "."
	  },
fadeLoop : function (id, opacity,opacityToFade)
	  {
		  var object = document.getElementById(id);
		  if (opacity <= opacityToFade)
		  { // just encase i forget 99% due to a firefox bug..
			  out_fade.setOpacity(object, opacity);
			  opacity += 15;
			  window.setTimeout("out_fade.fadeLoop('" + id + "', " + opacity + "," + opacityToFade + ")", 30);
		  }
		  else
		  {
			  ajaxrunning=false;
		  }
	  },
setOpacity : function (object, opacity)
	  {
		  object.style.filter = "alpha(style=0,opacity:" + opacity + ")";	// IE
		  object.style.KHTMLOpacity = opacity / 100;				// Konqueror
		  object.style.MozOpacity = opacity / 100;					// Mozilla (old)
		  object.style.opacity = opacity / 100;					// Mozilla (new)
	  }
}


var in_fade =
{
fadeout : function (id,curropacity,opacityToFade,bHide)
	  {
		  var opacity = curropacity; 
		  this.fadeLoop(id, opacity,opacityToFade,bHide); // remember its not  "->" function name.. its "."
	  },

fadeLoop : function (id, opacity,opacityToFade,bHide)
	  {
		  var object = document.getElementById(id);
		  if (opacity >= opacityToFade)
		  {
			  in_fade.setOpacity(object, opacity);
			  opacity -= 15;
			  window.setTimeout("in_fade.fadeLoop('" + id + "', " + opacity + "," + opacityToFade + ","+ bHide +")", 30);
		  }
		  else
		  {
			  if(bHide)
			  {
				  object.style.display='none';
			  }
		  }
	  },
setOpacity : function (object, opacity)
	  {
		  object.style.filter = "alpha(style=0,opacity:" + opacity + ")";	// IE
		  object.style.KHTMLOpacity = opacity / 100;				// Konqueror
		  object.style.MozOpacity = opacity / 100;					// Mozilla (old)
		  object.style.opacity = opacity / 100;					// Mozilla (new)
	  }
}

function zoomImage(imgsrc,caption,path,w,h)
{

  //document.getElementById("page").style.display = 'none';
//  document.getElementById("msh-contentarea").style.width = '100%';

	imgsrc++;
	if(imgsrc<10)
	{
		imgsrc = "0" + imgsrc;
	}

	var imgurl = SITE_URL + path + imgsrc + ".jpg";

	//alert(imgurl);

	document.getElementById("zoombox").style.left = 50 + "%";
	document.getElementById("zoombox").style.top = 50 + "%";
	document.getElementById("zoombox").style.display = "block";

	//document.getElementById("zoomcaption").innerHTML = caption;
	//w = document.getElementById("zoomimg").width;
	w = w + 10;
	document.getElementById("zoombox").style.width = w + "px"//w  + 10 + "px";

	//h = document.getElementById("zoomimg").height;
	h = h + 30; 
	document.getElementById("zoombox").style.height = h + "px";
	document.getElementById("zoomimg").style.marginLeft = "5px";
	document.getElementById("zoomimg").style.marginTop = "5px";


	//setOpacity(document.getElementById("fadebox"),30);
	document.getElementById("fadebox").style.width = "100%";

	var arrPageData = getPageSize();
	h2 = arrPageData[1];
	w2 = arrPageData[0];
	xpos = arrPageData[4];
	ypos = arrPageData[5];


//	document.getElementById("zoombox").style.marginTop = ypos + (-1 * (h/2)) + "px";
//	document.getElementById("zoombox").style.marginLeft = xpos + (-1 * (w/2)) + "px";
	document.getElementById("zoombox").style.marginTop = ypos -200 + "px";
	document.getElementById("zoombox").style.marginLeft = xpos -160 + "px";

	document.getElementById("fadebox").style.height = h2 + "px";
	document.getElementById("fadebox").style.width = w2 + "px";
	document.getElementById("fadebox").style.display = "";

	//document.getElementById("zoombox").style.marginLeft =  + "px";
	//document.getElementById("zoombox").style.marginTop = (-200) + "px";		

	out_fade.fadeout("fadebox",0,20,true);
	out_fade.fadeout("zoombox",0,200,true);

        //Load Image
        var loader = new ImageLoader(imgurl);
        loader.loadEvent = function(url, image)
        {
                //document.getElementById("zoomimg").style.display='block';
                //document.getElementById("fadebox").style.display='block';
                document.getElementById("zoomimg").src = loader.url; 
        }
        loader.load();
}

function hideImage()
{
	document.getElementById("zoomimg").src = SITE_URL + "/images/blank.jpg";
	in_fade.fadeout("fadebox",20,0,true);
	in_fade.fadeout("zoombox",100,0,true);
	
	//document.getElementById("fadebox").style.display='none';
	//document.getElementById("zoombox").style.display='none';
        
}

//Event Listener!!!
function addListener(element, type, expression, bubbling)
{
          bubbling = bubbling || false;
          if(window.addEventListener)	
          { // Standard
                    element.addEventListener(type, expression, bubbling);
                    return true;
          } 
          else if(window.attachEvent) 
          { // IE
                    element.attachEvent('on' + type, expression);
                    return true;
          } 
          else 
          {
                return false;
          }
}

//Image Object
var ImageLoader = function(url)
{
          this.url = url;
          this.image = null;
          this.loadEvent = null;
};

ImageLoader.prototype = 
{
          load:function()
          {
          
            this.image = document.createElement('img');
            var url = this.url;
            var image = this.image;
            var loadEvent = this.loadEvent;
            
            addListener(this.image, 'load', function(e)
            {
                      if(loadEvent != null)
                      {
                                loadEvent(url, image);
                      }
            }, false);
            this.image.src = this.url;
            
          },
          getImage:function()
          {
                return this.image;
          }
};

function showHideExtraOption(sourceID, sourceValue, targetID)
{
	if(document.getElementById(sourceID).value==sourceValue)
	{
		showItem(targetID);
	}
	else
	{
		hideItem(targetID);
	}
}

//simply sets an item visible by ID
function showItem(objID)
{
	if(objID)
	{
		document.getElementById(objID).style.display='';
	}
	else
	{
		alert("Error - " + objID + " doesn't exist.");
	}
}

function hideItem(objID)
{
	if(objID)
	{
		document.getElementById(objID).style.display='none';
	}
	else
	{
		alert("Error - " + objID + " doesn't exist.");
	}
}

function book_stage2(iCurrentStage)
{
	var bOK = true;;
	if(iCurrentStage==1)
	{
		bOK = validateBookStage1();
		if(bOK)
		{
			hideItem("book-stage1");
		}
	}

	if(bOK)
	{
		document.getElementById("book-stage"+iCurrentStage).style.display='none';
		document.getElementById("book-stage2").style.display='';
	}
}

function book_stage1(iCurrentStage)
{
	//doesn't matter where we are - we have to go through each step
	//anyway, so no validation required to do this...
	document.getElementById("book-stage"+iCurrentStage).style.display='none';
	document.getElementById("book-stage1").style.display='';
}

function book_stage3(iCurrentStage)
{
	var bOK = true;;
	if(iCurrentStage==2)
	{
		bOK = validateBookStage2();
		if(bOK)
		{
			hideItem("book-stage2");
		}
	}

	if(bOK)
	{
		document.getElementById("book-stage"+iCurrentStage).style.display='none';
		document.getElementById("book-stage3").style.display='';
	}	
}

function requestInspection()
{
	//simply call the book_stage3 function to
	//validate the stage 2 form
	if(book_stage3(2))
	{
		alert("OK");
	}
	return false;
}

function validateBookStage2()
{
	err = 0;
	errStr = "Please correct the following errors and try again:\n";

	if(!document.getElementById("sameasperson").checked)
	{
		if(document.getElementById("manmodel").value=="")
		{
			err++;
			errStr += "- Please enter the manufaturer/model of the vehicle to be inspected\n";
		}

		if(document.getElementById("enginesize").value=="")
		{
			err++;
			errStr += "- Please enter the engine size of the vehicle\n";
		}

		if(document.getElementById("registration").value=="")
		{
			err++;
			errStr += "- Please enter the vehicle registration\n";
		}

		if(document.getElementById("contactname").value=="")
		{
			err++;
			errStr += "- Please enter the name of the contact\n";
		}

		if(document.getElementById("contactaddress_1").value=="")
		{
			err++;
			errStr += "- Please enter the address for the inspection\n";
		}

		if(document.getElementById("contacttown").value=="")
		{
			err++;
			errStr += "- Please enter the town where the inspection will take place\n";
		}

		if(document.getElementById("contactcounty").value=="")
		{
			err++;
			errStr += "- Please enter the county where the inspection will take place\n";
		}

		if(document.getElementById("contactpostcode").value=="")
		{
			err++;
			errStr += "- Please enter the postcode where the inspection will take place\n";
		}

		if(document.getElementById("contactphone").value=="")
		{
			err++;
			errStr += "- Please enter the a telephone number for the inspection contact\n";
		}
	}

	if(err>0)
	{
		alert(errStr);
		return false;
	}
	else
	{
		return true;
	}
}

function validateBookStage1()
{
	err = 0;
	errStr = "Please correct the following errors and try again:\n";

	if(document.getElementById("inspectiontype").value==-1)
	{
		errStr+= "- Please select an inspection type\n";
		err++;
	}

	if((document.getElementById("inspectiontype").value=="Other")&&(document.getElementById("otherinspectiontype").value==""))
	{
		errStr+= "- Please enter the type of inspection\n";
		err++;
	}

	if(document.getElementById("person_name").value=="")
	{
		errStr+= "- Please enter your full name\n";
		err++;
	}


	if(document.getElementById("address_1").value=="")
	{
		errStr += "- Please enter at least the first line of your address\n";
		err++;
	}

	if(document.getElementById("town").value=="")
	{
		errStr += "- Please enter your town\n";
		err++;
	}

	if(document.getElementById("county").value=="")
	{
		errStr += "- Please enter your county\n";
		err++;
	}

	if(document.getElementById("postcode").value=="")
	{
		errStr += "- Please enter your postcode\n";
		err++;
	}

	
	if(document.getElementById("emailaddress").value=="")
	{
		errStr+= "- Please enter your email address\n";
		err++;
	}
	else
	{
		if(!validEmail(document.getElementById("emailaddress").value))
		{
			errStr += "- Please enter a valid email address\n";
			err++;
		}
	}

	if(document.getElementById("daytimephone").value=="")
	{
		errStr += "- Please enter a daytime contact number\n";
		err++;
	}

	if(!document.getElementById("termsagreed").checked)
	{
		errStr += "- Please agree to the DEKRA Expert terms and conditions\n";
		err++;
	}

	if(err>0)
	{
		alert(errStr);
		return false;
	}
	else
	{
		return true;
	}
}

function checkContactForm()
{

	err = 0;
	errStr = "Please correct the following errors and try again:\n";


	if(document.getElementById("personname").value=="")
	{
		errStr+= "- Please enter your full name\n";
		err++;
	}


	if(document.getElementById("email").value=="")
	{
		errStr+= "- Please enter your email address\n";
		err++;
	}
	else
	{
		if(!validEmail(document.getElementById("email").value))
		{
			errStr += "- Please enter a valid email address\n";
			err++;
		}
	}

	if(document.getElementById("telephone").value=="")
	{	
		errStr += "- Please enter a daytime contact number\n";
		err++;
	}	


	if(document.getElementById("businessarea").value=="-1")
	{	
		errStr += "- Please select a business area\n";
		err++;
	}


	if(document.getElementById("enquiry").value=="")
	{	
		errStr += "- Please enter a message\n";
		err++;
	}		
	
	if(!document.getElementById("termsagreed").checked)
	{
		errStr += "- Please agree to The Canvas Conspiracy terms and conditions\n";
		err++;
	}

	
	if(err>0)
	{
		alert(errStr);
		return false;
	}
	else
	{
		return true;
	}
	
}

/*function checkExpress()
{
	if(!document.getElementById("expressagreeterms").checked)
	{
		alert("Please agree to our terms and conditions before placing an order with us.");
		return false;
	}

	document.getElementById("paypal").value="express";
	document.getElementById("checkoutform").submit();

	return true;
}*/

function checkExpress()
{
	if(!document.getElementById("expressagreeterms").checked)
	{
		alert("Please agree to our terms nad conditions before placing an order with us.");
		return false;
	}
	else
	{
		document.getElementById("express").value = "express";
		document.getElementById("expresscheckout").submit();
		//return true;
	}
}


function changeCardType()
{
	if((document.getElementById("cardtype").value=="VISA")||(document.getElementById("cardtype").value=="MASTERCARD"))
	{
		document.getElementById("div-issueno").style.display="none";
		document.getElementById("div-startdate").style.display="none";		
	}

	if(document.getElementById("cardtype").value=="MAESTRO")
	{
		document.getElementById("div-issueno").style.display="";
		document.getElementById("div-startdate").style.display="none";
	}

	if((document.getElementById("cardtype").value=="SOLO")||(document.getElementById("cardtype").value=="OTHER"))
	{
		document.getElementById("div-issueno").style.display="none";
		document.getElementById("div-startdate").style.display="";		
	}
}

function checkCardDetailsForm()
{
	err = 0;
	errStr = "Please correct the following error(s) and try again:\n";
	if(document.getElementById("cardholderf").value=="")
	{
		err++;
		errStr += "- Please enter the cardholder forename\n";
	}

	if(document.getElementById("cardholders").value=="")
	{
		err++;
		errStr += "- Please enter the cardholder surname\n";
	}

	if(document.getElementById("cardno").value=="")
	{
		err++;
		errStr += "- Please enter a card number\n";
	}

	if(document.getElementById("endm").value=="")
	{
		err++;
		errStr += "- Please enter a card end month\n";
	}

	if(document.getElementById("endy").value=="")
	{
		err++;
		errStr += "- Please enter a card end year\n";
	}

	if(document.getElementById("secno").value=="")
	{
		err++;
		errStr += "- Please enter the card security number\n";
	}

	if(document.getElementById("cardtype").value=="MAESTRO")
	{
		if(document.getElementById("issno").value=="")
		{
			err++;
			errStr += "- Please enter the issue number of the card\n";
		}
	}

	if((document.getElementById("cardtype").value=="SOLO")||(document.getElementById("cardtype").value=="OTHER"))
	{
		if(document.getElementById("startm").value=="")
		{
			err++;
			errStr += "- Please enter a card start month\n";
		}

		if(document.getElementById("starty").value=="")
		{
			err++;
			errStr += "- Please enter a card start year\n";
		}		
	}

	if(err>0)
	{
		alert(errStr);
		return false;
	}
	else
	{
		return true;
//		return false;
	}




}

function verifyNumeric(obj)
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	val = obj;
	if (val.length == 0) return false;
	if((val.length==1)&&(val.charAt(0)=="0"))
	{
		return false;
	}

   //  test obj consists of valid characters listed above
	for (i = 0; i < val.length && blnResult == true; i++)
	{
		strChar = val.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;	   
}


//updates the price of a product, given the options
function updatePrice()
{
	var x;

	var unitprice = parseFloat(document.getElementById("p_unitprice").value);
	var totalprice = parseFloat(unitprice);

	for(x in arrP)
	{
		var y;
		var t = arrP[x];
		for(y in t)
		{
			if($("#optionselection_"+x+"").val()>"")
			{
				actualid=-1;
				
                                actualidpos = $("#optionselection_"+x+"").val().lastIndexOf("_");
				if(actualidpos>-1)
				{
					actualid = $("#optionselection_"+x+"").val().substr(actualidpos+1);
				}
				if(y==actualid)
				{
					if(t[y][1]>0)
					{
						totalprice += parseFloat(t[y][1]);
						//alert("addprice");
					}
					else
					{
						//totalprice += parseFloat(unitprice);
					}
				
				}
			}
		}
	}
	
	$("#quotedprice").html("" + toCurrency(totalprice) + "");
	
	
}

function addToBasket()
{
	err=0;

	//check that the quantity value submitted is numeric
	//if(!IsNumeric(document.productform.pquantity.value))
	//{
	//err++;
	//}
	
	//check that all options available have been selected
	var x;
	for(x in arrP)
	{
		var y;
		var t = arrP[x];
		for(y in t)
		{
			//alert($("#optionselection_"+x+"").val());
			//alert("#optionselection_"+x+"");
			if($("#optionselection_"+x+"").val()>"")
			{
				//alert("we are here?//");
				actualid=-1;
				actualidpos = $("#optionselection_"+x+"").val().lastIndexOf("_");
                                
                                //alert(actualidpos);
				if(actualidpos>-1)
				{
					actualid = $("#optionselection_"+x+"").val().substr(actualidpos+1);
				}
				
				if (actualid==-1)
				{
					err++;
				}
				
			}
		}
	}

	if(err>0)
	{
		alert("There was an error creating your order.\nPlease ensure that you have selected all options for this product.");
		return false;
	}
	else
	{
		return true;
	}
	
	
}


//shows/hides the object (pass in the id)
function toCurrency(val)
{
	return numberformat(val,2);
}

function numberformat(num,dec)
{

	mul=Math.pow(10,dec);
	num=num*mul;
	num=Math.round(num);
	num = num/mul;
	var numstr=String(num);
	if(numstr.indexOf(".") == -1)
	{
		numstr = numstr + ".";
		for(nfi=0;nfi<dec;nfi++) numstr = numstr + "0";
	}
	decpl = numstr.length - numstr.indexOf(".");
	decpl = decpl - 1;
	if (decpl < dec)
	{
		for(nfi=decpl;nfi<dec;nfi++) numstr = numstr + "0";
	}
	return (numstr);

}


function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

