function pop(ShowPage, intwidth, intheight) {

	var leftpos = (screen.width - intwidth) / 2;
	var toppos = (screen.height - intheight) / 2;
	var winprops = 'toolbar=0, scrollbars=1, location=0, statusbars=0, menubar=0, width=' + intwidth + ', height=' + intheight + ', left=' + leftpos + ', top=' + toppos + ', resizable=1';
    var popupwindow = window.open(ShowPage, 'poppage', winprops);

}

function validateNumber(field, msg, min, max) {
	if (!min) { min = 0 }
	if (!max) { max = 255 }
	if ( (parseInt(field.value) != field.value) || field.value.length < min || field.value.length > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function validateString(field, msg, min, max) {
	if (!min) { min = 1 }
	if (!max) { max = 65535 }
	if (!field.value || field.value.length < min || field.value.max > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function validateEmail(email, msg, optional) {

	if (!email.value && optional) {
		return true;
	}

	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	
	if (!re_mail.test(email.value)) {
		alert(msg);
		email.focus();
		email.select();
		return false;
	}
	return true;
}

function validateList(listname, msg) {

	var foo = listname.selectedIndex;

	if (foo == 0 || foo == -1) {
		alert(msg);
		return false;
	}
	
	return true;
	
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function highlightRows(target) {
	
	// loops through all rows in table, highlighting rows where an checkbox or radio is checked
	
	// find the target
	//var target = findTarget(e);
	
	//alert(target.nodeName);
	// get a reference to the parent table
	while (target != document.body && target.nodeName.toLowerCase() != 'div') {
		target = target.parentNode;
	}
	// get all the TR elements in the table
	var rows = target.getElementsByTagName('TR');
	for (i = 0; i < rows.length; i++) {
		var row = rows[i];
		// get all the input elements in this row
		var inputs = row.getElementsByTagName('INPUT');
		for (x = 0; x < inputs.length; x++) {
			var input = inputs[x];
			if (input.getAttribute('type') == 'checkbox' || input.getAttribute('type') == 'radio') {
				// assign class if input is checked, remove class if not
				//alert(input.checked);
				if (input.checked) {
					//row.className += ' highlightrow';
					row.style.backgroundColor = '#ffc';
					row.style.color = '#000';
				} else {
					//row.className = row.className.replace(/\b ?highlightrow\b/,'');
					row.style.backgroundColor = '';
					row.style.color = '';
				}
			}
		}
	}
}

function showProductInfo(productNo) {
	
	document.getElementById('submit_product_enquiry').disabled = false;
		
	// set color of all rows in #product_info
	var productInfoTable = document.getElementById('product_info');
	var productInfoTableRows = productInfoTable.getElementsByTagName('TR');
	for (var i = 0; i < productInfoTableRows.length; i++) {
		thisRow = productInfoTableRows[i];
		thisRow.style.backgroundColor = '#fff';
		thisRow.style.color = '#666';
		
		// uncheck all checkboxes too
		var inputs = thisRow.getElementsByTagName('INPUT');
		for (var j = 0; j < inputs.length; j++) {
			var input = inputs[j];
			//alert(input.getAttribute('type'));
			if (input.getAttribute('type') == 'checkbox') {
				input.checked = false;
			}
		}
		
	}
	
	var rowClass = 'product_' + productNo;
	// highlight all rows in the product info table with this class
	var rows = getElementsByClass(rowClass, null, 'TR');
	for (var i = 0; i < rows.length; i++) {
		thisRow = rows[i];
		thisRow.style.backgroundColor = '#ffc';
		thisRow.style.color = '#000';
		// check all checkboxes too
		var inputs = thisRow.getElementsByTagName('INPUT');
		for (var j = 0; j < inputs.length; j++) {
			var input = inputs[j];
			//alert(input.getAttribute('type'));
			if (input.getAttribute('type') == 'checkbox') {
				input.checked = true;
			}
		}
		
	}
	
}

function toggleSubmit(target) {

	highlightRows(target);
	
	// loop through all checkboxes in #product_info
	// if any checked enable the submit button
	// if not, disable it
	var productInfoTable = document.getElementById('product_info');
	var inputs = productInfoTable.getElementsByTagName('INPUT');
	
	var submit = document.getElementById('submit_product_enquiry');
	// turn if off to begin
	submit.disabled = true;
		
	// loop through all inputs
	for (var j = 0; j < inputs.length; j++) {
		var input = inputs[j];
		//alert(input.getAttribute('type'));
		if (input.getAttribute('type') == 'checkbox') {
			if (input.checked) {
				// highlight the row
				// enable the submit button
				submit.disabled = false;
				// don't need to carry on looking
				break;
			} else {
				// unhighlight row
			}
		}
	}
}

function selectCounty() {
	var value = document.getElementById('county').value;
	if (value != '') {
		document.location.href = '?county=' + value;
	}
}