// JavaScript Document

function showPic(img, desc, large_img){
	if(document.getElementById){
  		document.getElementById('product_img_lg').src = img
		document.getElementById('large_image_link').href = large_img;
		document.getElementById('img_desc').innerHTML = desc
 		return false;
 	}else
 		return true;
}


function search_fill()
	{
	if(document.forms['pro_search'].p_search.value == ""){document.forms['pro_search'].p_search.value = "SEARCH"}
	}
	
function search_empty()
	{
	if(document.forms['pro_search'].p_search.value == "SEARCH"){document.forms['pro_search'].p_search.value = ""}
	}
	
	
function showFlash(obj){
	document.write ('<object type="application/x-shockwave-flash" data="/flash/' + obj + '.swf" width="515" height="286">')
	document.write ('<param name="movie" value="/flash/' + obj + '.swf"/>')
	document.write ('<param name="wmode" value="transparent"/>')
	document.write ('<img src="/flash/images/1.jpg" alt="" />')
	document.write ('</object>')	
}

var mem = [];
var x = 0;
var list = '';

function addColour(name) {
	// Testing
	//alert('X: ' + x);
	// Try and store selection
	if(x < 5) {
		// Check if already in list
		var ok = true;
		for(var foo=0; foo<mem.length; foo++) {
			if(name == 	mem[foo]) {
				ok = false;	
			}
		}
		
		if(ok == true) {
			mem[x] = name;
			x++;
		}
		
		// Refresh list
		list = '<h3>Your Selection</h3><ul>';
		for(var f=0; f<x; f++) {
			list += '<li><a href="#" onclick="removeColour(\''+mem[f]+'\'); return false;">' + mem[f] + '</a></li>';
		}
		list += '</ul>';
		// Output List
		document.getElementById('requestBlock').innerHTML=list;
	} else {
		//alert('Memory is full... Sorry dude!');
		// Refresh list
		list = '<h3>Your Selection</h3><ul>';
		for(var f=0; f<x; f++) {
			list += '<li><a href="#" onclick="removeColour(\''+mem[f]+'\'); return false;">' + mem[f] + '</a></li>';
		}
		list += '</ul>';
		
		list += '<p class="red">You can only select five swatches. If you wish to add another colour please remove a colour from the list above first.</p>';
		// Output List
		document.getElementById('requestBlock').innerHTML=list;
	}
	
	// Update form fields
	var swatches = '';
	for(var foo=0; foo<mem.length; foo++) {
		if(foo != mem.length - 1) {
			swatches += mem[foo] + ', ';
		} else {
			swatches += mem[foo];
		}
	}
	
	document.getElementById('swatches').value=swatches;
}

function removeColour(name) {
	// Remove item
	for(var foo=0; foo<mem.length; foo++) {
		if(mem[foo] == name) {
			mem.splice(foo, 1);	
			//alert(name + ' removed!');
		}
	}
	
	// Resort list
	var newArray = [];
	var ind = 0;
	for(var foo=0; foo<mem.length; foo++) {
		if(mem[foo] != undefined) {
			ind = newArray.length;
			newArray[ind] = mem[foo];
		}
	}
	
	var cnt = newArray.length;
	// Clear old memory
	mem.splice(0, mem.length);
	for(var foo=0; foo<cnt; foo++) {
		mem[foo] = newArray[foo];	
		//alert('mem['+foo+'] added ('+newArray[foo]+')');
	}
	if(x != 0) { x = x - 1; }
	
	// Refresh list
	list = '<h3>Your Selection</h3><ul>';
	for(var f=0; f<mem.length; f++) {
		list += '<li><a href="#" onclick="removeColour(\''+mem[f]+'\'); return false;">' + mem[f] + '</a></li>';
	}
	list += '</ul>';
	
	// Update form fields
	var swatches = '';
	for(var foo=0; foo<mem.length; foo++) {
		if(foo != mem.length - 1) {
			swatches += mem[foo] + ', ';
		} else {
			swatches += mem[foo];
		}
	}
	
	document.getElementById('swatches').value=swatches;
	
	// Output List
	document.getElementById('requestBlock').innerHTML=list;
}

function validateBrochure() {
	var required = ['firstname', 'surname', 'email', 'telephone', 'housename', 'street', 'town', 'county', 'country', 'postcode'];
	var friendly = ['First Name', 'Surname', 'Email', 'Telephone / Mobile Number', 'House Name / Number', 'Street', 'Town / City', 'County / State', 'Country', 'Zip / Postcode'];
	var errors = [];
	var count = 0;
	var firstError = '';
	for(var i=0; i<required.length; i++) {
		if(document.getElementById(required[i]).value == '') {
			errors[count] = friendly[i];
			if(count == 0) {
				firstError = required[i];	
			}
			count++;
		}
	}
	
	if(errors.length > 0) {
		var str = 'Sorry, the following fields can not be left blank:\n\n';
		for(var x=0; x<errors.length; x++) {
			str += '     ' + errors[x] + '\n';	
		}
		str += '\nPlease complete the above fields and try again.';
		alert(str);
		document.getElementById(firstError).focus();
		return false;
	}
}