// Copyright © 2010 by Eclipse Web Media.
//
// 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 validate_form(thisform) {
    with (thisform) {
        valid = true;
        if (!validateEmail(em)) {
            em.focus();
            valid = false;
        } else {emailLabel(0);}
	if (!validatePhone(phone)) {
            phone.focus();
            valid = false;
        } else {phoneLabel(0);}
	if (!validateName(name)) {
            name.focus();
            valid = false;
        } else {nameLabel(0);}
        return valid;
    }
}

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

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

function validateName(field){
    with (field) {
        if (value==null||value==""){
	    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;
        }
    }
}



//-------------------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 fill out your website";
    }
}

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";
    }
}

