document.observe("dom:loaded", function() {
	updateBasketStatus();	
});

Event.observe(window, 'load', applyCorners);

function updateBasketStatus() {
	// runs ajax to update 'basket status'.  Not on all pages, hence the $ check.
	if ($('basketStatus')) {
		new Ajax.Updater('basketStatus', '/index.php', {
		  parameters: {pageType:'ajax', mode: 'basketStatus' },
		  method: 'get'
		});		
	}
}

function applyCorners() {
	var ie6=!(window.XMLHttpRequest);
	function applyPNG(ele,strGraphic) {
		// ie6 - applies the png alpha channel to the corners
		ele.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/img/corners/"+strGraphic+".png',sizingMethod='scale')";
		ele.src="/img/blank.gif";			
	}						  
	// assign rounded corners - top left and top right to H2s
	$$('h2').each(function(s, index) {
		var elem=new Element('img', {src:"/img/corners/tl.png", width:"7", height:"7"});
		elem.setStyle({
			position:"absolute",
			top:"-1px",
			left:"-1px",
			zIndex:"1"
		});	
		s.appendChild(elem);		
		if (ie6) { applyPNG(elem,"tl"); }	
		var elem=new Element('img', {src:"/img/corners/tr.png", width:"7", height:"7"})
		elem.setStyle({
			position:"absolute",
			top:"-1px",
			left:s.offsetWidth-6+"px",
			zIndex:"2"			
		})	 
		s.appendChild(elem);
		if (ie6) { applyPNG(elem,"tr"); }
		
	});
	


	// Assign rounded corners to rest - usually on white bg.
	$$('.roundtl').each(function(s, index) {
		var elem=new Element('img', {src:"/img/corners/tl.png", width:"7", height:"7"});
		elem.setStyle({
			position:"absolute",
			top:"-1px",
			left:"-1px",
			zIndex:"1"
		});	
		if (ie6) { applyPNG(elem,"tl"); }
		s.appendChild(elem);	

	});
	$$('.roundtr').each(function(s, index) {
		var elem=new Element('img', {src:"/img/corners/tr.png", width:"7", height:"7"})
		elem.setStyle({
			position:"absolute",
			top:"-1px",
			left:s.offsetWidth-6+"px",
			zIndex:"2"			
		})	  
		s.appendChild(elem);
		if (ie6) { applyPNG(elem,"tr"); }
	});
	$$('.roundbl').each(function(s, index) {
		var elem=new Element('img', {src:"/img/corners/bl.png", width:"7", height:"7"})
		elem.setStyle({
			position:"absolute",
			left:"-1px",
			bottom:"-1px",
			zIndex:"3"			
		})
		s.appendChild(elem);		
		if (ie6) { applyPNG(elem,"bl"); }		
	});	
		
	$$('.roundbr').each(function(s, index) {
		var elem=new Element('img', {src:"/img/corners/br.png", width:"7", height:"7"})
		s.appendChild(elem);
		elem.setStyle({
			position:"absolute",
			left:s.offsetWidth-6+"px",
			bottom:"-1px",
			zIndex:"4"				
		})
		if (ie6) { applyPNG(elem,"br"); }		
	});		
}

function initProductAdd() {
	// check page for basket additions
	$$('img.productAdd').each(function(s, index) {
		s.observe('click',function(event){
			// we've clicked on an 'add to basket' button
			
			// go up the dom to the previous <input> to find out amount and what ID we are talking about.
			var ele=s.previous();
			var iQuantity=ele.value;
			var iProductId=ele.id.substr(2);
			ele.value="0";
			
			new Ajax.Request('/index.php', {
			  parameters: {pageType:'ajax', mode: 'addToBasket', product_id: iProductId, quantity: iQuantity},
			  method: 'get',
			  onComplete: function(transport) {		
				new Effect.Highlight('basketStatus', {startcolor:'#ffff99', endcolor:'#ffffff'})
				// update the basket
				updateBasketStatus();	  
			  }
			});	
		
		});
	});	
}
function initBasket() {
	// inits /shop/basket/

	new Ajax.Updater('basketContent', '/index.php', {
	  parameters: {pageType:'ajax', mode: 'basketContent' },
	  method: 'get',
	  onComplete: initBasketEvents,
	  evalScripts: true
	});		
}

function initBasketEvents() {

	// so, content has come back to basketContent.  Now we can assign events to the buttons on it.	

	// 1) Sort out remove muttons
	$$('table img').each(function(s) {
		s.observe('click',function(e) {
			// The rows of the order table are id's as prod_prodid, eg prod_3333, so
			var iProductId=this.up().up().id.substr(5);	
			// send removal request....
			new Ajax.Request('/index.php', {
			  parameters: {pageType:'ajax', mode: 'removeFromBasket', product_id: iProductId},
			  method: 'get',
			  onComplete: initBasket
			});
		});
	});
	

	// 2) Sort out keypresses on values

	$$('table input').each(function(s) {								
		s.observe('focus',function(e) {
			this.value="";		   
		});
		
		s.observe('keyup',function(e) {
			// The rows of the order table are id's as prod_prodid, eg prod_3333, so
			var iProductId=this.up().up().id.substr(5);	
			var iValue=Number(this.value);
			// send removal request....
			new Ajax.Request('/index.php', {
			  parameters: {pageType:'ajax', mode: 'updateBasket', product_id: iProductId, quantity: iValue},
			  method: 'get',
			  onComplete: initBasket
			  
			});
		});
	});	
	
	new Effect.Highlight('grand_total', {startcolor:'#ffff99', endcolor:'#ffffff'});
}


function initAddress() {

	// if you change location, update
	$('location').observe('change', function(e) {
		address_update_delivery_options();
	});
	
	// update it straight away.
	address_update_delivery_options();											

	// Validate the form
	$('bsk').observe('submit', function(e) {
		var strError="";
		if ($F('firstname').length==0) strError+=" - Firstname<br />";
		if ($F('surname').length==0) strError+=" - Surname<br />";
		if ($F('address1').length==0) strError+=" - Address 1<br />";
		if ($F('address2').length==0) strError+=" - Address 2<br />";
		if ($F('town').length==0) strError+=" - Town 2<br />";		
		if ($F('postcode').length==0) strError+=" - Postcode <br />";
		if ($F('telephone').length==0) strError+=" - Telephone <br />";
		if ($F('email').length==0) strError+=" - Email <br />";
		if (strError.length>0) {
			myAlert("Please provide the following:<br /><br /><strong>" + strError+"</strong>");	
			e.stop();			
		}
	});

	
}

function address_update_delivery_options() {
		var iLocationid=$F('location');
		new Ajax.Updater('deliveryOptions','/index.php', {
		 parameters: {pageType:'ajax', mode: 'delivery_location',  location_id: iLocationid},
		 method: 'get',
		 evalScripts: true
		});			
}

function initContact() {
	$('contactform').observe('submit', function(e) {
		var strError="";
		if ($F('firstname').length==0) strError+=" - Firstname<br />";
		if ($F('surname').length==0) strError+=" - Surname<br />";
		if ($F('address1').length==0) strError+=" - Address 1<br />";
		if ($F('address2').length==0) strError+=" - Address 2<br />";
		if ($F('town').length==0) strError+=" - Town <br />";
		if ($F('postcode').length==0) strError+=" - Postcode <br />";
		if ($F('telephone').length==0) strError+=" - Telephone <br />";
		if ($F('email').length==0) strError+=" - Email <br />";		
		if ($F('enquiry').length==0) strError+=" - Enquiry <br />";		
		if (strError.length>0) {
			myAlert("Please provide the following:<br /><br /><strong>" + strError+"</strong>");		
			e.stop();			
		}
	});
}

function initRegistration() {
	$('registrationForm').observe('submit', function(e) {
		var strError="";
	
		if ($F('firstname').length==0) strError+=" - Firstname<br />";
		if ($F('surname').length==0) strError+=" - Surname<br />";
		if ($F('address1').length==0) strError+=" - Address 1<br />";
		if ($F('address2').length==0) strError+=" - Address 2<br />";
		if ($F('postcode').length==0) strError+=" - Postcode <br />";
		if ($F('telephone').length==0) strError+=" - Telephone <br />";
		if ($F('email').length==0) strError+=" - Email <br />";		
		
		if ($F('carregno').length==0) strError+=" - Car Registration Number <br />";
		if ($F('history').length==0) strError+=" - Brief history of car <br />";
		if ($F('img').length==0) strError+=" - Car Image <br />";		
		
		var strFilename=$F('img').toLowerCase();
		if (strFilename.indexOf('jpg')==-1 && strFilename.indexOf('jpeg')==-1) strError+=" - A JPEG Image <br />";

		if (strError.length>0) {
			myAlert("Please provide the following:<br /><br /><strong>" + strError+"</strong>");		
			e.stop();			
		}
	});
}

function initCatalog() {
	$('catForm').observe('submit', function(e) {
		var strError="";
		if ($F('firstname').length==0) strError+=" - Firstname<br />";
		if ($F('surname').length==0) strError+=" - Surname<br />";
		if ($F('address1').length==0) strError+=" - Address 1<br />";
		if ($F('address2').length==0) strError+=" - Address 2<br />";
		if ($F('postcode').length==0) strError+=" - Postcode <br />";
		if ($F('telephone').length==0) strError+=" - Telephone <br />";
		if ($F('email').length==0) strError+=" - Email <br />";		
		if (strError.length>0) {
			myAlert("Please provide the following:<br /><br /><strong>" + strError+"</strong>");	
			e.stop();			
		}
	});
}



function myAlert(strWhat) {
	Modalbox.show('<div class=\'warning\'><p>' + strWhat + '</p><input type=\'button\' value=\'OK\' onclick=\'Modalbox.hide()\' />', {title: 'Wood and Pickett', width: 300});	
}