// JavaScript Document

function validate(f) {
	var reg, fname, lname, email, areacode, prefix, suffix, comments;
	var errors = [];
	var i = 0;
	var j
	var errorlist = "";
	
	reg = f.regarding.options[f.regarding.selectedIndex].value;
	fname = f.firstname.value;
	lname = f.lastname.value;
	email = f.email.value;
	areacode = f.areacode.value;
	prefix = f.prefix.value;
	suffix = f.suffix.value;
	comments = f.comments.value;
	
	// Check regarding
	if (f.regarding.selectedIndex == 0) {
		errors[i++] = "Select how your <strong>inquiry</strong> is related to your questions/comments.";
	}
	
	// Check fname
	if (fname == "") {
		errors[i++] = "Your <strong>first name</strong> is required.";
	}
	
	// Check lname
	if (lname == "") {
		errors[i++] = "Your <strong>last name</strong> is required.";
	}
	
	// Check email address
	if (!checkMail(email)) {
		errors[i++] = "A valid return <strong>email address</strong> is required for email responses.";
	}
	
	// Check phone ** only if something is in one of the fields
	if (areacode != "" || prefix != "" || suffix != "") {
		if (areacode.length != 3 || prefix.length != 3 || suffix.length != 4) {
			errors[i++] = "A valid <strong>phone number</strong> is needed if you would like to be called back.";
		}
	}
	
	// Check comments
	if (comments == "") {
		errors[i++] = "<strong>Questions/Comments</strong> are needed so we can assist you better.";
	}
	
	// Show errors
	if (errors.length > 0) {
		for (j = 0; j <= errors.length-1; j++) {
			errorlist += errors[j] + "<br />";
		}
		document.getElementById("errors").display = "visible";
		document.getElementById("errors").innerHTML = errorlist;
		
		return false;
	} else {
		return true;
	}
}

function checkMail(e) {
	var x = e;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) {
		return true;
	} else {
		return false;
	}
}

function formInit() {
	document.getElementById("errors").display = "hidden";
}

function numbersonly(myfield, e, dec) {
	var key;
	var keychar;
	
	if (window.event)
	key = window.event.keyCode;
	else if (e)
	key = e.which;
	else
	return true;
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
	(key==9) || (key==13) || (key==27) )
	return true;
	
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	return true;
	
	// decimal point jump
	else if (dec && (keychar == "."))
	{
		myfield.form.elements[dec].focus();
		return false;
	}
	else
	return false;
}

function autoTab(e) {
    if(this.value.length == this.getAttribute("maxlength") && 
        e.KeyCode != 8 && e.keyCode != 16 && e.keyCode != 9) {
            new Field.activate(findNextElement(this.getAttribute("tabindex")));
        }
}

function findNextElement(index) {
    elements = new Form.getElements('shippingInfo');
    for(i = 0; i < elements.length; i++) {
        element = elements[i];
        if(parseInt(element.getAttribute("tabindex")) == (parseInt(index) + 1)) {
            return element;
        }
    }
    return elements[0];
}
