function toggleImage(id, image1, image2, image3, image4) {
	if($('image'+id).src == image1) {
		$('image'+id).src = image2;
		$('popup'+id).setStyle('background-image','url('+image4+')');
	} else {
		$('image'+id).src = image1;
		$('popup'+id).setStyle('background-image','url('+image3+')');
	}
}

function setSize() {

	var docSize = document.getSize();
	var docWidth = docSize.x;
	var multiplier = 0;
	var newWidth = 0;


	if(docWidth > smallestWidth) {
		multiplier = Math.floor((docWidth - smallestWidth) / step);
		newWidth = original + (step * multiplier);
		$('content').setStyle('width', newWidth);
	} else {
		newWidth = original;
		$('content').setStyle('width', original );
	}



	$('header').setStyle('width', newWidth);

	$('footer').setStyle('width',  newWidth );
	if($('col')) $('col').setStyle('width',  newWidth - ($('menu').offsetWidth ) );
	if($('bottomBox')) $('bottomBox').setStyle('width',  newWidth +2);

	if(multiplier > 0) {
		if($('btmPanels'))$('btmPanels').setStyle('width',  1190);
		if($('extraBtmPanel'))$('extraBtmPanel').style.display = 'block';
	} else {
		if($('btmPanels'))$('btmPanels').setStyle('width',  895);
		if($('extraBtmPanel'))$('extraBtmPanel').style.display = 'none';
	}
}




function addToBasket (id, noUpdate) {

	
	
	if ($('qty07' + id).disabled) {
		$('button' + id).innerHTML = 'ADD TO BASKET';
		$('button' + id).style.backgroundImage = 'url(images/btn-bg.gif)';
		$('qty07' + id).disabled = false;
		$('smallPanel' + id).tween('background-color','#fff');

		if (!noUpdate) {
			//ajax to remove from basket
			updateBasket(id, 'remove');
		}

	} else {
		
		if($('qty07' + id).value > 0  && $('qty07' + id).value <= 100) {
			$('button' + id).innerHTML = 'REMOVE';
			$('button' + id).style.backgroundImage = 'url(images/btn-bg-red.gif)';
			$('qty07' + id).disabled = true;
			$('smallPanel' + id).tween('background-color','#eee');
	
			if (!noUpdate) {
				//ajax to add to basket
				updateBasket(id, 'add');
			}
		} else {
			
			flashBg('smallPanel'+id,'#ebafaf');
		}
	}
}

function displayBasket() {
	var cart = new Request({
		method: 'get',
		url: 'cartajax.php',

		onSuccess: function(txt){
			$('cart').innerHTML = txt;
		},


		onFailure: function(){
			//alert('Sorry there was a communication problem and your basket could not be loaded.');
		}

	});

	cart.send();

}

function addStockToBasket (id, noUpdate) {

	$('addRem' + id).innerHTML = 'WORKING';
	
	var cart = new Request({
		method: 'get',
		url: 'cartajax.php',

		onSuccess: function(txt){
			$('cart').innerHTML = txt;
			toggleStockPanel(id);
		},


		onFailure: function(){
			alert('Sorry there was a communication problem and your selection was not addded to the basket. \n\nPlease try again.');
		}

	});



	if ($('addRem' + id).getStyle('background-image').match('btn-bg-red.gif')) {
		
		if (noUpdate) {
			toggleStockPanel(id);
		} else {
			updateBasket(id, 'remove');
		}

	} else {

		if (noUpdate) {
			toggleStockPanel(id);
		} else {
			updateBasket(id, 'add');
		}
	}
	
}

function updateBasket(id, action) {

	var cart = new Request({
		method: 'get',
		url: 'cartajax.php',

		onSuccess: function(txt){
			$('cart').innerHTML = txt;
			if(action != 'update') {
				toggleStockPanel(id);
			} else {
				flashBg('bigPanel'+id,'#92d050');
				
				
			}
		},


		onFailure: function(){
			alert('Sorry there was a communication problem and your selection was not addded to the basket. \n\nPlease try again.');
		}

	});

	var qtys = '';
	var i;
	var totalQty = 0;

	for(i=1;i < 25;i++) {
		if($('qty'+padDigits(i,2)+id)) {
			qtys += '&qty'+i+'='+$('qty'+padDigits(i,2)+id).value;
			totalQty += $('qty'+padDigits(i,2)+id).value;
		}
	}

	if (totalQty > 0) {
		cart.send('action='+action+'&product='+$('productCode'+id).value + qtys);
	} else {
		flashBg('bigPanel'+id,'#ebafaf');
		$('addRem' + id).innerHTML = 'ADD TO BASKET';
		
	}
}

function toggleStockPanel(id) {
	if ($('bigPanel' + id)){
	//alert($('addRem' + id).getStyle('background-image'));
		if ($('addRem' + id).getStyle('background-image').match('btn-bg-red.gif')) {
	
	
			$('addRem' + id).innerHTML = 'ADD TO BASKET';
			$('addRem' + id).style.backgroundImage = 'url(images/btn-bg.gif)';
			$('update' + id).fade('out');
			$('bigPanel' + id).tween('background-color','#fff');
			$('tip' + id).innerHTML = 'Select your quantity for each shoe in the WHITE box below and click "<strong>Add to basket</strong>"';
	
		} else {
	
	
	
			$('addRem' + id).innerHTML = 'REMOVE';
			$('addRem' + id).style.backgroundImage = 'url(images/btn-bg-red.gif)';
			$('update' + id).fade('in');
			$('bigPanel' + id).tween('background-color','#eee');
			$('tip' + id).innerHTML = 'This item is in your basket. If you change the quantities you must click "<strong>Update</strong>"';
	
		}
	}
}

function maxValue(id, maxValue) {

	if(maxValue > 12) maxValue = 12;

	if($(id).value > maxValue) $(id).value = maxValue;
	if($(id).value < 0) $(id).value = 0;
	if(!isNumeric($(id).value)) $(id).value = 0;

}

function padDigits(n, totalDigits) {
        n = n.toString();
        var pd = '';
        if (totalDigits > n.length)
        {
            for (i=0; i < (totalDigits-n.length); i++)
            {
                pd += '0';
            }
        }
        return pd + n.toString();
}


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;

}

function flashBg(id,colour) {
//	if ($(id)) {
		oldColour = $(id).getStyle('background-color');
		$(id).tween('background-color',colour,oldColour);
//	}
}


function imgPopup(id) {

	$('image'+id).addEvent('mousemove', function(event) {

		var x = event.page.x.toInt() - $('image' +id).getCoordinates().left;
		var y = event.page.y.toInt() - $('image' +id).getCoordinates().top;


		var bgX = (x / $('image' +id).getCoordinates().width) * 100;
		var bgY = (y / $('image' +id).getCoordinates().height) * 100;

		$('popup'+id).setStyle('top',event.page.y-150);
		$('popup'+id).setStyle('left',event.page.x+100);

		$('popup'+id).setStyle('background-position',bgX +'% '+ bgY+'%');




	});

	$('image'+id).addEvent('mouseenter', function(event) {
												  
		var x = event.page.x.toInt() - $('image' +id).getCoordinates().left;
		var y = event.page.y.toInt() - $('image' +id).getCoordinates().top;


		var bgX = (x / $('image' +id).getCoordinates().width) * 100;
		var bgY = (y / $('image' +id).getCoordinates().height) * 100;

		$('popup'+id).setStyle('top',event.page.y-150);
		$('popup'+id).setStyle('left',event.page.x+100);

		$('popup'+id).setStyle('background-position',bgX +'% '+ bgY+'%');


		$('popup'+id).fade('in');

	});

	$('image'+id).addEvent('mouseleave', function(event) {

		$('popup'+id).fade('out');

	});

	$('popup'+id).fade('hide');
	$('popup'+id).set('tween', {duration: 'short'});


}


function posApprove(order) {
	
	var pos = new Request({
		method: 'get',
		url: 'adminpos-ajax.php',

		onSuccess: function(txt){
			if (txt == 'OK') {
				$('order'+order).setStyle('display','none');
			}
		},


		onFailure: function(){
			alert('Sorry there was a communication problem and the order was not approved. \n\nPlease try again.');
		}

	});
	
	pos.send('action=approve&order='+order);
}

function placeOrder() {
	if (!$('agree').checked) { 
		alert('Please agree to our terms and conditions before placing your order.'); 
		return false;
	}else{
		document.specialinfo.submit();
		return true;
	}
}
