/* FORM validation functions */
/* Example use -

<form method="post" action="" onsubmit="return (validateString(this.name, 'Please provide your name.', 1, 200)  && validateEmail(this.email, 'Please supply a valid email address.', 1) && validateList(this.country, 'Please select a country.'));"> 

*/

function toggleProducts(foo, bar) {

	if (document.getElementById) {
	
		subList = document.getElementById(foo);
		listElement = document.getElementById(bar);
		
		if (subList.style.display == 'none') {
			subList.style.display = '';
			listElement.className = 'activeSection';
		} else {
			subList.style.display = 'none';
			listElement.className = 'foo';
		}
		
	}
}


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 validateCheckbox(check_box_name, msg) {

	var checkbox_array = document.getElementsByName(check_box_name);

	//You can then loop the array testing the "checked" property, something like:

	for (var i = 0; i< checkbox_array.length; i++) {
		if (checkbox_array[i].checked) {
			return true;
		}
	}
	
	alert(msg);
	return false; 
}

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;
	
}
