function validate(frm){
	//all-purpose form validation function
	//give form elements class="validate" to perform validation
	//could be enhanced to display label text instead of form name, perhaps?
	//would need to get reference to label first, then form
	//for the moment, name form elements as you would like them to be displayed, but with underscore, e.g. full_name
	//does this element need validating?
	for(i=0;i<frm.elements.length;i++){
		el=frm.elements[i];
		str=el.value;
		strDisplayName=el.name.replace('_',' ')
		aryV=el.className.split('_');
		if(aryV[0]=='validate'){
			//first, check presence
			if(str.length<1){
				alert('Please provide ' + strDisplayName);
					  //name.replace('_',' '));
				return false;
			}else{
				switch(aryV[1]){
					case 'number':
						//spaces, brackets,+ and  - are allowed
						re=/[\s()+-]/g;
						str=el.value.replace(re,'');
						re=/^\d+$/;
					break;
					case 'email':
						//nothing fancy, just checks for presence of @ and . with at least one character between each
						//1@2.3 is valid, for example
						re=/.+@.+\..+/gi;
					break;
					default:
						re='';
					break;
				}
				if(re!=''){
					if(str.search(re)==-1){
						alert('Please provide a valid ' + strDisplayName);
						return false;
					}	
				}
			}
		}	
	}
	return true;
}

function addressFiller(chk){
	//allows visible pre-filling of delivery address if javascript supported
	var d=document.getElementById('dsame');
	fill=false;
	aryNames=new Array('name','address','postcode','tel');
	if(chk.checked){
		fill=true;
	}
		//fill boxes with values
		newValue=' ';
		for(i=0;i<aryNames.length;i++){
			if(fill){
				newValue=document.getElementById(aryNames[i]).value;
			}
			document.getElementById('d'+aryNames[i]).value=newValue;
		}
}

function calculatePrice(){
	var oQuantity=document.getElementById('quantity');	
	var iQuantityValue=oQuantity.options[oQuantity.options.selectedIndex].value;
	var oPrice=document.getElementById('price');
	var iUnitPrice=document.getElementById('unitprice').value;
	//alert(iUnitPrice);
	//alert(iQVal);
	//alert(iUnitPrice*iQuantityValue);
	if(!isNaN(iUnitPrice)){
		iUnitPrice=iUnitPrice*iQuantityValue;
		oPrice.value=iUnitPrice.toFixed(2);
	}	
}

function attachEvents(){
	var oQuantity=document.getElementById('quantity');
	if(oQuantity){
		oQuantity.onchange=function(){calculatePrice();};
	}	
	//allows visible pre-filling of delivery address if javascript supported
	var d=document.getElementById('dsame');
	if(d){
		d.onclick=function(){addressFiller(this);};
	}
}


//this is outside all functions
if(document.getElementById){
	window.onload=attachEvents;
}