// Affiliate JavaScript

function sendQuery(aff_id)
{
	term = document.getElementById("query").value.toUpperCase();
	term = term.replace(' ', '_');
	term = term.replace('+', '_');
	loc = 'http://www.carreg.co.uk/number_plates/search/' + term + '?r=' + aff_id;
	window.open(loc);
}

function updateNumber(number)
{
	i = 0;
	while(i < 8)
	{
		if (isAlphaNumeric(number.charAt(i)) == true)
		{
			newChar = number.charAt(i);
		}
		else if(number.charAt(i) == ' ')
		{
			newChar = '_';
		}
		else
		{
			newChar = 'spacer';
		}
		if(newChar != 'spacer')
		{
			newChar = newChar.toUpperCase();
		}
		if(newChar == 'I' || newChar == '1' || newChar == '_')
		{
			newWidth = 11;
		}
		else if(newChar == '' || newChar == 'spacer')
		{
			newWidth = 1;
		}
		else
		{
			newWidth = 27;
		}
		switch (i)
		{
		case 0:
			document.getElementById("char_1").src = 'http://www.carreg.co.uk/images/plates/' + newChar + '.gif';
			document.getElementById("char_1").width = newWidth;
			break;
		case 1:
			document.getElementById("char_2").src = 'http://www.carreg.co.uk/images/plates/' + newChar + '.gif';
			document.getElementById("char_2").width = newWidth;
			break;
		case 2:
			document.getElementById("char_3").src = 'http://www.carreg.co.uk/images/plates/' + newChar + '.gif';
			document.getElementById("char_3").width = newWidth;
			break;
		case 3:
			document.getElementById("char_4").src = 'http://www.carreg.co.uk/images/plates/' + newChar + '.gif';
			document.getElementById("char_4").width = newWidth;
			break;
		case 4:
			document.getElementById("char_5").src = 'http://www.carreg.co.uk/images/plates/' + newChar + '.gif';
			document.getElementById("char_5").width = newWidth;
			break;
		case 5:
			document.getElementById("char_6").src = 'http://www.carreg.co.uk/images/plates/' + newChar + '.gif';
			document.getElementById("char_6").width = newWidth;
			break;
		case 6:
			document.getElementById("char_7").src = 'http://www.carreg.co.uk/images/plates/' + newChar + '.gif';
			document.getElementById("char_7").width = newWidth;
			break;
		case 7:
			document.getElementById("char_8").src = 'http://www.carreg.co.uk/images/plates/' + newChar + '.gif';
			document.getElementById("char_8").width = newWidth;
			break;
		}
		i++;
	}
}

function isAlphaNumeric(char)
{
	char = char.toUpperCase();
	var strValidChars = "0123456789ABCDEFGHIJKLMNOPRSTUVWXYZ";
	if(char.length == 0)
	{
		return false;
	}
	if(strValidChars.indexOf(char) == -1)
	{
		return false;
	}
	else
	{
		return true;
	}
}