// Copyright © 2010 by Eclipse Web Media for Hollberg & Weaver, LLP.
//
// 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;
var name = document.getElementById('name');
var phone = document.getElementById('phone');
var email = document.getElementById('email');
var legal = document.getElementById('comments');
var why = "";
var nameError = "";
var phoneError = "";
var emailError = "";
var legalError = "";

function validateForm(thisform) {
    with (thisform) {
        valid = true;
		if (!validateComments(legal)) {
            legal.focus();
            valid = false;
        } 
        if (!validateEmail(email)) {
            email.focus();
            valid = false;
        } 
	    if (!validatePhone(phone)) {
            phone.focus();
            valid = false;
        } 
	    if (!validateName(name)) {
            name.focus();
            valid = false;
        } 
		return valid;
    }
}

function validateHomeForm(thisform) {
	why = "";
    with (thisform) {
        valid = true;
		if (!validateHomeComments(legal)) {
            legal.focus();
			why += legalError;
            valid = false;
        } 
        if (!validateHomeEmail(email)) {
            email.focus();
			why += emailError;
            valid = false;
        } 
	    if (!validateHomePhone(phone)) {
            phone.focus();
			why += phoneError;
            valid = false;
        } 
	    if (!validateHomeName(name)) {
            name.focus();
			why += nameError;
            valid = false;
        } if (!valid) {alert(why);}
		return valid;
    }
}


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

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

function validateHomeComments(field){
    with (field) {
        if (value==null||value==""){
	    document.getElementById('legal').style.borderColor = "red";
		document.getElementById('legal').style.backgroundColor = "pink";
		legalError = "Please tell us your legal issue.\n";
            return false;
        } else {
	    document.getElementById('legal').style.borderColor = "#CCCCCC";
		document.getElementById('legal').style.backgroundColor = "#ffffff";
		legalError = "";
            return true;
        }
    }
}

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

}

function validateHomeName(field){
    with (field) {
        if (value==null||value==""){
	    document.getElementById('name').style.borderColor = "red";
		document.getElementById('name').style.backgroundColor = "pink";
		nameError = "Please tell us your name.\n";
            return false;
        } else {
	    document.getElementById('name').style.borderColor = "#CCCCCC";
		document.getElementById('name').style.backgroundColor = "#ffffff";
		nameError = "";
            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 validateHomePhone(field){
    with (field) {
        var stripped = value.replace(/[\(\)\.\-\ ]/g, '');
        if (value==null||value==""){
	    document.getElementById('phone').style.borderColor = "red";
		document.getElementById('phone').style.backgroundColor = "pink";
		phoneError = "Please tell us your phone number.\n";
            return false;
        }
	if (isNaN(parseInt(stripped))) {
	    document.getElementById('phone').style.borderColor = "red";
		document.getElementById('phone').style.backgroundColor = "pink";
		phoneError = "Phone number contains invalid characters.\n";
	    return false;
	} 
	if (!(stripped.length == 10)){
	    document.getElementById('phone').style.borderColor = "red";
		document.getElementById('phone').style.backgroundColor = "pink";
		phoneError = "Phone number must be 10 digits.\n";
	    return false;
	}
	else {
        document.getElementById('phone').style.borderColor = "#CCCCCC";
		document.getElementById('phone').style.backgroundColor = "#ffffff";
		phoneError = "";
        return true;
        }
    }
}

function validateHomeEmail(field) {
    var emailFilter=/^.+@.+\..{2,3}$/;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
    with (field) {
    if (value==null||value==""){
        document.getElementById('email').style.borderColor = "red";
		document.getElementById('email').style.backgroundColor = "pink";
		emailError = "Please tell us your email address.\n";
        return false;
    }
    if (!(emailFilter.test(value))) { 
	    document.getElementById('email').style.borderColor = "red";
		document.getElementById('email').style.backgroundColor = "pink";
		emailError = "Email address is not valid.\n";
	    return false;
    } 
	if (value.match(illegalChars)) {
	    document.getElementById('email').style.borderColor = "red";
		document.getElementById('email').style.backgroundColor = "pink";
		emailError = "Email contains invalid characters.\n";
	    return false;
	}
        else {
            document.getElementById('email').style.borderColor = "#CCCCCC";
			document.getElementById('email').style.backgroundColor = "#ffffff";
			emailError = "";
            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;
        }
    }
}

//-------------------Labels------------------

function commentsLabel(num){
    var p = document.getElementById('comments_label');
    if (num=="0") {
        p.childNodes[0].nodeValue = "";
    }
    if (num=="1") {
        p.childNodes[0].nodeValue = "Please tell us your legal issue";
    }
}

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 = "The number entered 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 = "The email entered contains illegal characters";
    }
}



//----------valid selector from dropdown list-----------
//---note: This doesn't do anything, I just kept it for future reference if we wanted to check a dropdown menu
function checkDropdown(choice) {
    var error = "";
        if (choice == 0) {
            error = "You didn't choose an option from the drop-down list.\n";
        }    
    return error;
}    


