$(document).ready(function()
{
	var $size = $('#frm_size').val();
	var $length = $('#frm_length').val();

	if ($size != 'undefined' && $size != '') {
		$('select[name = "id[2]"]').each(function() {
			$('option', $(this)).each(function() {
				if (strpos($(this).text(), $size) !== false) {
					$(this).attr('selected', 'selected');
				}
			});
		});
	}

	if ($length != 'undefined' && $length != '') {
		$('select[name = "id[3]"]').each(function() {
			$('option', $(this)).each(function() {
				var result = false;
				if (strpos($length, '.') === false) {
					if (strpos($(this).text(), $length) === 0) {
						result = true;
					}
				} else if (strpos($(this).text(), $length) !== false) {
					result = true;
				}

				if (result) {
					$(this).attr('selected', 'selected');
					var the_price = $(this).text();
					
					// if no pound sign, have to get price from hidden field
					if (strpos(the_price, "£") === false)
					{
						// get id from select list to find the id of product
						select_id = $(this).parents("select").attr("id");
						// now replace to get name of originalprice field
						original_price_id = select_id.replace("option", "originalPrice");
						the_price = $("input#"+original_price_id).val();
					}
					else
					{
						the_price = the_price.split('(£');
						the_price = str_replace(')', '', the_price[1]);
					}
					
					var price_id = str_replace('option', 'price', $(this).parent().attr('id'));
					document.getElementById(price_id).innerHTML = the_price;
				}
			});
		});
	}
});

function strpos (haystack, needle, offset)
{
	// http://kevin.vanzonneveld.net
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Onno Marsman
	// *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
	// *     returns 1: 14
	var i = (haystack+'').indexOf(needle, offset);
	return i===-1 ? false : i;
}

function str_replace (search, replace, subject)
{
	// http://kevin.vanzonneveld.net
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Gabriel Paderni
	// +   improved by: Philip Peterson
	// +   improved by: Simon Willison (http://simonwillison.net)
	// +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	// +   bugfixed by: Anton Ongson
	// +      input by: Onno Marsman
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +    tweaked by: Onno Marsman
	// *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
	// *     returns 1: 'Kevin.van.Zonneveld'
	// *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
	// *     returns 2: 'hemmo, mars'
	var f = search, r = replace, s = subject;
	var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
	while (j = 0, i--) { if (s[i]) { while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){} } }
	return sa ? s : s[0];
}

