// Function checks for email validity
function checkEmail(node) {
    var re = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
    if (!node.value.match(re)) {
		return false;
    } else {
		return node.value;
    }
}

// Function checks field for a name
function checkName(node) {
    node.value = node.value.replace(/\d/g, '');
    if (!node.value) {
		return false;
    }
    node.value = node.value.replace(/\b([a-z])(\w*)\b/g, function(s,t,u) { 
		return (t.toUpperCase() + u);
    });
    return node.value;
}

// Function checks field for a certain number of digits
function checkNumber(node, l) {
    node.value = node.value.replace(/\D/g, '');
    var digNum = l ? l : node.maxLength;
    var len = node.value.length;
    if (len < digNum) {
		return false;
    } else {
		return node.value;
    }
}

function validateForm() {
    var btn = nodeOf('visitor-submit');
    btn.value = 'Sending...';
    btn.disabled = true;
    var name = nodeOf('visitor-name').value;
    var email = nodeOf('visitor-email').value;
    var phone_area = nodeOf('visitor-phone-area').value;
    var phone_first3 = nodeOf('visitor-phone-first3').value;
    var phone_last4 = nodeOf('visitor-phone-last4').value;
    var notes = nodeOf('visitor-notes').value;
    if (!name || !email || !notes) {
	nodeOf('visitor-errors').innerHTML = 'You must enter all required fields';
	btn.value = 'Submit';
	btn.disabled = false;					
	return;
    }
    name = checkName(nodeOf('visitor-name'));
    if (!name) {
	nodeOf('visitor-errors').innerHTML = 'Please enter a valid name';
	btn.value = 'Submit';
	btn.disabled = false;		
	return;
    }
    if (!checkEmail(nodeOf('visitor-email'))) {
	nodeOf('visitor-errors').innerHTML = 'Please enter a valid email address';
	btn.value = 'Submit';
	btn.disabled = false;
	return;
    }
    if (phone_area || phone_first3 || phone_last4) {
	phone_area = checkNumber(nodeOf('visitor-phone-area'));
	phone_first3 = checkNumber(nodeOf('visitor-phone-first3'));
	phone_last4 = checkNumber(nodeOf('visitor-phone-last4'));
	if (! (phone_area && phone_first3 && phone_last4)) {
	    nodeOf('visitor-errors').innerHTML = 'Please enter a valid phone number including the area code';
	    btn.value = 'Submit';
	    btn.disabled = false;
	    return;
	}
    }
    nodeOf('visitor-errors').innerHTML = '';
    // Send emails and Display Thank You page
    var the_call = 
	'name=' + encodeURIComponent(name) +
	'&email=' + encodeURIComponent(email) +
	'&phone=' + encodeURIComponent(phone_area + '-' + phone_first3 + '-' + phone_last4) +
	'&notes=' + encodeURIComponent(notes) +
	'&bsi-code=hello';
    var client = createClient();
    if (client) {
        client.onreadystatechange = function() {
            if (client.readyState == 4 && client.status == 200) {
		if (client.responseText == 'Ok') {
		    btn.value = 'Submit';
		    btn.disabled = false;
		    nodeOf('visitor-name').value = '';
		    nodeOf('visitor-email').value = '';
		    nodeOf('visitor-phone-area').value = '';
		    nodeOf('visitor-phone-first3').value = '';
		    nodeOf('visitor-phone-last4').value = '';
		    nodeOf('visitor-notes').value = '';
		    alert('Thanks, email sent.  We\'ll be in touch!');
		} else {
		    alert(client.responseText);
		}
            }
        };
        client.open('POST', 'bsi_scripts/contact_bsi.php', true);
        client.setRequestHeader("Content-type",
                                "application/x-www-form-urlencoded");
        client.setRequestHeader("Content-length", the_call.length);
        client.setRequestHeader("Connection", "close");
        client.send(the_call);
    }
}
