var arrCart = [];
var maxSwatchCount = 6;
/* if this option changes, make sure to change the text in the CMS as well as the process page (for non javascript clients) AND SAMPLES_FORM.CFM*/

	function clearChecks() {	
	/*This function will remove all checks from check boxes and samples in the samples Palette when page is refreshed. This may need to be removed if selected sample information is to be passed from the Buy Curtains / Blinds page???*/
		var checkAll = document.getElementById('freeSamplesForm').getElementsByTagName('input');
		var sampleCart = document.getElementById('swatchHolder');
		var count = arrCart.length;
		
		if (count == '0') {
			//sampleCart.innerHTML = '';
			sampleCart.innerHTML = '<p>No samples selected yet,<br />click on a thumbnail on the left to start!</p>';
		}
		
		for (i=0; i<checkAll.length; i++) {
			checkAll.checked = false;
		}
		
	}
	
	function removeItems(array, item) {
	var i = 0;
	while (i < array.length) {
	if (array[i] == item) {
	array.splice(i, 1);
	} else {
	i++;
	}
	}
	return array;
	}
		
	function onlyNineSwatch()
	{	
		var samplesForm = document.getElementById('freeSamplesForm');
		var checkboxes = samplesForm.getElementsByTagName('input');
		
		var selectedForm = document.getElementById('swatchHolder');
		var selectedSamples = selectedForm.getElementsByTagName('input');

		for (i=0; i<checkboxes.length; i++)
		{
			var cb = checkboxes[i];

			if (cb.getAttribute('type') == 'checkbox')
			{
			
				cb.onclick = function () 
				{
					return countChecks(this);
				};

				if(cb.getAttribute('checked')=="true"||cb.getAttribute('checked')==true){
					countChecks(cb);
				}
	
			}
		}

		var arrLinks = samplesForm.getElementsByTagName('a');
		
		for (i=0; i<arrLinks.length; i++)
		{
			var aLink = arrLinks[i];

			if (aLink.className == 'swatch_link')
			{		
				aLink.onclick = function () 
				{	
					refItem = 'sample' + this.getAttribute('rel');
					
					if(document.getElementById(refItem).checked==true){
						document.getElementById(refItem).checked = false;	
					} else {
						document.getElementById(refItem).checked = true;
					}
					
					
					
					countChecks(document.getElementById('sample' + this.getAttribute('rel')));
					return false;
				};
				
			}
		}
	}
	
	function countChecks(input)	{	

		var checkAll = document.getElementsByTagName('input');
		var sampleCart = document.getElementById('swatchHolder');
		
		
		var count = arrCart.length;


		if (count >= maxSwatchCount && input.checked == true)
		{	
			alert('You are allowed up to a maximum of ' + maxSwatchCount + ' free swatches.\nPlease click on the ticked swatch image to deselect it or call us if you\'d like more than '+ maxSwatchCount);
			
			input.checked = false;
			return false;
		} else if (input.checked == false) {
			/* Remove from cart */
					
			inputNode = input.getAttribute('id') + 'Div';
						
			//clear all samples from samplePalette
			document.getElementById('swatchHolder').removeChild(document.getElementById(inputNode));
			//if the array is empty make sure any html is removed
			if (count == '0') {
				//sampleCart.innerHTML = '<p>No samples selected yet,<br />click on a thumbnail on the left to start!</p>';
				input.checked = false;
			}
			
			//remove the element from the array
			for (j=0; j<arrCart.length; j++) {

				if (arrCart[j] == input.id) {
					
					arrCart.splice(j, 1);
					//removeItems(arrCart, j) 
				}

			}	
			
			intId = input.getAttribute('id').replace(/sample/,"");		
			document.getElementById("swatch"+intId).className = "";	
			
		} else {//i.e.the checkbox isn't checked
			
			//remove text from samples palette
			if (count == '0') {
				sampleCart.innerHTML = '';
			}
			
			/* Add to cart */
			if (count == '1') {
				
			}
			
			//get swatchUrl value from hidden field
			var swatchUrl = input.previousSibling.previousSibling.getAttribute('value'); 
			//alert(varietyTypeIdVal);
			
			//var swatchUrl = input.getAttribute('rel');
			var div = document.createElement('div');
			div.className = "sampleSwatchSmall";
			div.setAttribute('id', input.getAttribute('id') + 'Div');

			//hook to allow user to click sample to remove it
			var linkSample;
			
			var img = document.createElement('img');
			img.setAttribute('src', '/productimages/fabrics/sml/' + swatchUrl + ".jpg"); /*	This will ne to be made dynamic	*/
			/**/

			div.appendChild(img);

			//Add new swatch in front of swatches
			sampleCart.appendChild(div);			
			
			arrCart.splice(arrCart.length + 1, 0, input.id);
			arrPos = arrCart.length + 1;				
			
			intId = input.getAttribute('id').replace(/sample/,"");
			
			document.getElementById("swatch"+intId).className = "selected_swatch";	

		}
	
	}
	
	function updateSelected() {
		//function to allow user to remove samples by clicking them
	}

	addLoadEvent(onlyNineSwatch);
	addLoadEvent(clearChecks);
	

