var W3CDOM = (document.getElementsByTagName && document.createElement);

function validateForm() {
	validForm = true;
	firstError = null;
	errorstring = '';
	nonEmptyInputs = new Array("firstname", "lname", "company", "address2", "city", "state", "zip", "email", "phone", "fax", "piqaddress", "piqcity");
	/* Make sure that all required fields are non-empty. */
	for (i=0; i < nonEmptyInputs.length; i++) {
		if (!document.getElementById(nonEmptyInputs[i]).value) {
			writeError(nonEmptyInputs[i],'This field is required');
			if (!firstError)
				firstError = document.getElementById(nonEmptyInputs[i]);
		}
	}
	/* Make sure that all email fields are valid. */
	if (document.getElementById('email').value.indexOf('@') == -1) {
		writeError('email','This is not a valid email address');
		if (!firstError)
			firstError = document.getElementById('email');
	}
    /* Make sure that at least one delivery options was selected. */
	numSelected = 0;
	if (document.getElementById('del_fax').checked)
		numSelected++;
	if (document.getElementById('del_email').checked)
		numSelected++;
	if (document.getElementById('del_del').checked)
		numSelected++
	if (document.getElementById('del_pickup').checked)
		numSelected++
	if (numSelected == 0) {
		writeError('del_pickup','Please select at least one option');
	}
	/*return the curson to the first error found on the page */
	if (firstError)
		firstError.focus();
    /*if there is a problem, do not submit the form */
    
    
	if (!validForm)
		return false;
}
function writeError(obj,message) {
	validForm = false;
	obj= document.getElementById(obj);
	if (obj.hasError) return;
	if (W3CDOM) {
		obj.className += ' error';
		obj.onchange = removeError;
		var sp = document.createElement('span');
		sp.className = 'error';
		sp.appendChild(document.createTextNode(message));
		obj.parentNode.appendChild(sp);
		obj.hasError = sp;
	}
	else {
		errorstring += obj.name + ': ' + message + '\n';
		obj.hasError = true;
	}
}

function removeError()
{
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.parentNode.removeChild(this.hasError);
	this.hasError = null;
	this.onchange = null;
}