function validate(theForm, type) {
	if(theForm.name.value=='')
	{
		theForm.name.focus();
		alert('Please enter your name.');
		return false;
	}
	if(theForm.address.value=='')
	{
		theForm.address.focus();
		alert('Please enter your address.');
		return false;
	}
	if(theForm.tel.value=='')
	{
		theForm.tel.focus();
		alert('Please enter your telephone number.');
		return false;
	}
	if(theForm.email.value=='')
	{
		theForm.email.focus();
		alert('Please enter your email address.');
		return false;
	}
	return true;
}

function validate(frm,type){
	//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 attachEvents(){
	//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;
}

function maskKeyPress(objEvent) 
{
    var iKeyCode;   
	if (objEvent.keyCode) { 
	iKeyCode = objEvent.keyCode; 
	} 
	else { 
	iKeyCode = objEvent.which; 
	}; 
	alert(iKeyCode);
    if((iKeyCode>=48 && iKeyCode<=57)||(iKeyCode==8)||(iKeyCode==46)) return true;
    return false;
}
