// Copyright © 2010 by Eclipse Web Media for North Atlanta Chiropractic Center.
//
// You may use this code for your own website
// without restriction. You may also redistribute
// and edit this code without restriction.



//----------Validate form on Submit----------

var valid;

function validateForm(thisform) {
    with (thisform) {
        valid = true;
		if (!validateDropdown(dropdown)) {
            dropdown.focus();
            valid = false;
        } 
		if (!validateEmail(em)) {
            em.focus();
            valid = false;
        } 
		if (!validatePhone(phone)) {
            phone.focus();
            valid = false;
        } 
		if (!validateName(name)) {
            name.focus();
            valid = false;
        } 
        return valid;
    }
}

function validateFAQ(thisform) {
	with (thisform) {
        valid = true;
		if (!validateFAQDropdown(practicearea)) {
            practicearea.focus();
            valid = false;
        } 
		if (!validateFAQEmail(em)) {
            em.focus();
            valid = false;
        } 
		if (!validateFAQTextarea(yourquestion)) {
            yourquestion.focus();
            valid = false;
        } 
        return valid;
    }
}

//--------------Validate Fields--------------


function validateName(field){
    with (field) {
		if (value==null||value==""){
	        nameLabel(1);
            return false;
        }
        if (value==null||value=="Name"){
	        nameLabel(1);
            return false;
        } else {
	        nameLabel(0);
            return true;
        }
    }

}

function validatePhone(field){
    with (field) {
        var stripped = value.replace(/[\(\)\.\-\ ]/g, '');
        if (value==null||value==""){
	    phoneLabel(1);
            return false;
        }
	if (isNaN(parseInt(stripped))) {
	    phoneLabel(2);
	    return false;
	} 
	if (!(stripped.length == 10)){
	    phoneLabel(3);
	    return false;
	} else {
            phoneLabel(0);
            return true;
        }
    }
}
  

function validateEmail(field) {
    var emailFilter=/^.+@.+\..{2,3}$/;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
    with (field) {
    if (value==null||value==""){
        emailLabel(1)
        return false;
    }
    if (!(emailFilter.test(value))) { 
	    emailLabel(2)
	    return false;
    } 
	if (value.match(illegalChars)) {
	    emailLabel(3);
	    return false;
	} else {
            emailLabel(0);             
            return true;
        }
    }
}

function validateDropdown(field) {
	with (field) {
        if (value == 0) {
            dropdownLabel(1);
			return false;
        } else {
			dropdownLabel(0);
    		return true;
		}
	}
}    



function validateFAQEmail(field) {
    var emailFilter=/^.+@.+\..{2,3}$/;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
    with (field) {
	if (value==null||value=="Email"){
        emailLabel(0);
        return true;
    }
    if (!(emailFilter.test(value))) { 
	    emailLabel(2)
	    return false;
    } 
	if (value.match(illegalChars)) {
	    emailLabel(3);
	    return false;
	} else {
            emailLabel(0);             
            return true;
        }
    }
}

function validateFAQDropdown(field) {
	with (field) {
        if (value == 0) {
            dropdownFAQLabel(1);
			return false;
        } else {
			dropdownFAQLabel(0);
    		return true;
		}
	}
}  

function validateFAQTextarea(field){
    with (field) {
		if (value==null||value==""){
	        textareaFAQLabel(1);
            return false;
        }
        if (value==null||value=="Your Question"){
	        textareaFAQLabel(1);
            return false;
        } else {
	        textareaFAQLabel(0);
            return true;
        }
    }

}
//-------------------Labels------------------

function nameLabel(num){
    var p = document.getElementById('name_label');
    if (num=="0") {
        p.childNodes[0].nodeValue = "";
    }
    if (num=="1") {
        p.childNodes[0].nodeValue = "Please tell us your name";
    }
}

function phoneLabel(num){
    var p = document.getElementById('phone_label');
    if (num=="0") {
        p.childNodes[0].nodeValue = "";
    }
    if (num=="1") {
        p.childNodes[0].nodeValue = "Please tell us your phone number";
    }
    if (num=="2") {
        p.childNodes[0].nodeValue = "Number contains illegal characters";
    }
    if (num=="3") {
        p.childNodes[0].nodeValue = "Please enter a 10 digit phone number";
    }
}


function emailLabel(num){
    var e = document.getElementById('email_label');
    if (num=="0") {
        e.childNodes[0].nodeValue = " ";
    }
    if (num=="1") {
        e.childNodes[0].nodeValue = "Please tell us your email";
    }
    if (num=="2") {
        e.childNodes[0].nodeValue = "Please enter a valid email address";
    }
    if (num=="3") {
        e.childNodes[0].nodeValue = "Email contains illegal characters";
    }
}

function dropdownLabel(num){
    var p = document.getElementById('dropdown_label');
    if (num=="0") {
        p.childNodes[0].nodeValue = "";
    }
    if (num=="1") {
        p.childNodes[0].nodeValue = "Please tell us your contact option";
    }
}

function dropdownFAQLabel(num){
    var p = document.getElementById('dropdown_label');
    if (num=="0") {
        p.childNodes[0].nodeValue = "";
    }
    if (num=="1") {
        p.childNodes[0].nodeValue = "What is your question's category?";
    }
}

function textareaFAQLabel(num){
    var p = document.getElementById('textarea_label');
    if (num=="0") {
        p.childNodes[0].nodeValue = "";
    }
    if (num=="1") {
        p.childNodes[0].nodeValue = "What is your question?";
    }
}

