// add attributes on click
//document.side_bar_demo.name.onclick = function(){
//	document.side_bar_demo.action="side_bar_free_trial.php";
//	document.side_bar_demo.livecontrol.value="confirm";
//}
document.side_bar_demo.phone.onclick = function(){
	document.side_bar_demo.action="side_bar_free_trial.php";
	document.side_bar_demo.livecontrol.value="confirm";
}
document.side_bar_demo.email.onclick = function(){
	document.side_bar_demo.action="side_bar_free_trial.php";
	document.side_bar_demo.livecontrol.value="confirm";
}
document.side_bar_demo.login_name.onclick = function(){
	document.side_bar_demo.action="side_bar_free_trial.php";
	document.side_bar_demo.livecontrol.value="confirm";
}
document.side_bar_demo.login_password.onclick = function(){
	document.side_bar_demo.action="side_bar_free_trial.php";
	document.side_bar_demo.livecontrol.value="confirm";
}
// console.log(document.side_bar_demo.comments.value);

sidebardemo_submitform = function() {
    if (document.side_bar_demo.comments.value == "") {
        validateFormOnSubmit(document.side_bar_demo);
    } else {document.side_bar_demo.livecontrol.value="select";}
	// if
} // function

function validateFormOnSubmit(theForm) {

// for google analytics tracking
pageTracker._trackPageview('/submitted_sidebar_freetrial_form');

// for form validation
    var reason = "";

    reason += validateEmpty(theForm.name);
    reason += validateEmail(theForm.email);
    reason += validatePhone(theForm.phone);
    reason += validateLoginName(theForm.login_name);
	reason += validateLoginPassword(theForm.login_password);

    if (reason != "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    document.side_bar_demo.submit();
    // return true;
}
function validateEmpty(fld) {
    var error = "";

    if (fld.value.length == 0 || fld.value == "Your Name") {
        fld.style.background = '#ff5f02';
        error = "Name is a required field.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateLoginName(fld) {
    var error = "";

    if (fld.value.length == 0 || fld.value == "Choose Login Name") {
        fld.style.background = '#ff5f02';
        error = "Login Name is a required field.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateLoginPassword(fld) {
    var error = "";

    if (fld.value.length == 0 || fld.value == "Choose Login Password") {
        fld.style.background = '#ff5f02';
        error = "Login Password is a required field.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function trim(s) {
    return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
    var error = "";
    var tfld = trim(fld.value); // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;

    if (fld.value == "" || fld.value == "Your Email Address") {
        fld.style.background = '#ff5f02';
        error = "Email is a required field.\n";
    } else if (!emailFilter.test(tfld)) { //test email for illegal characters
        fld.style.background = '#ff5f02';
        error = "Enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#ff5f02';
        error = "Enter a valid email address.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

    if (fld.value == "" || fld.value == "Your Phone Number") {
        error = "Phone is a required field.\n";
        fld.style.background = '#ff5f02';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#ff5f02';
    } else if (stripped.length < 7) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#ff5f02';
	} else {
        fld.style.background = 'White';
    }
    return error;
}
function validateURL(fld) {
    var error = "";
    var theurl = fld.value;

    var tomatch = /^([a-z0-9_-]+\.)*[a-z0-9_-]+(\.[a-z]{2,6}){1,2}$/;

    if (tomatch.test(theurl) == false) {
        error = "Enter your website url.\n";
        fld.style.background = '#ff5f02';
    }
    return error;
} // function
