// From main.js
function nodeOf() {
    var a = arguments;
    var r = new Array();
    for (var i=0; i<a.length; i++) {
	var o = a[i];
	if (typeof(o) == 'string') r.push(document.getElementById(o));
	else r.push(o);
    }
    if (r.length == 1) return r[0];
    return r;
}

function createClient() {
    var client;
    try {
        client = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        alert("Sorry, your browser is not AJAX-enabled!");
    }
    return client;
}

// 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', nodeOf('contact_form').action, 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);
    }
}

function validateForm2() {
    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 region = nodeOf('visitor-region').value;
	var reps = nodeOf('visitor-reps').value;
	var line = nodeOf('visitor-line').value;
	var products = nodeOf('visitor-products').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) +
	'&region=' + encodeURIComponent(region) +
	'&reps=' + encodeURIComponent(reps) +
	'&line=' + encodeURIComponent(line) +
	'&products=' + encodeURIComponent(products) +
	'&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-region').value = '';
			nodeOf('visitor-reps').value = '';
			nodeOf('visitor-line').value = '';
			nodeOf('visitor-products').value = '';
		    nodeOf('visitor-notes').value = '';
		    alert('Thanks, email sent.  We\'ll be in touch!');
		} else {
		    alert(client.responseText);
		}
            }
        };
        client.open('POST', nodeOf('contact_form').action, 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);
    }
}
