function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateEmpty1(fld) {
    var error = "";
 
    if (fld.value.length == 0 || fld.value == "Enter your name") {
        error = "field has not been filled in.\n"
    }
    return error;  
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateCheckbox(fld) {
	var error = "";
	if(fld.checked == 0) {
		fld.style.background = 'Yellow';
		error = "checbox has not been checked.\n"
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function validateRadio(fld) {
	var error = "";
	var myOption = -1;
	for(i=0; i < fld.length; i++) {
		if(fld[i].checked) {
			myOption = 1;
			break;
		}
	}
	if (myOption == -1) {
		error = "option has not been selected.\n";
	}
	return error;
}

function radioValue(fld) {
	var optionValue = "";
	var myOption = -1;
	for(i=0; i < fld.length; i++) {
		if(fld[i].checked) {
			optionValue = fld[i].value;
			break;
		}
	}
	return optionValue;
}

function validateSelect(fld) {
    var error = "";
 
    if (fld.value == "0") {
        error = "field has not been selected any option.\n"
    }
    return error;  
}

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.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateEmail1(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        error = "The email address contains illegal characters.\n";
    }
    return error;
}

function validateComments(theForm) {
var reason = "";

	if(validateEmpty(theForm.name)){  
  		reason += "Name " + validateEmpty(theForm.name);
  	}
  	if(validateEmail(theForm.email)) {
  		reason += validateEmail(theForm.email);
  	}
  	if(validateSelect(theForm.topic)) {
  		reason += "Topic " + validateSelect(theForm.topic);
  	}
  	if(validateSelect(theForm.affiliation)) {
  		reason += "Affiliation " + validateSelect(theForm.affiliation);
  	}
  	if(validateEmpty(theForm.subject)) {
  		reason += "Subject " + validateEmpty(theForm.subject);
  	}
  	if(validateEmpty(theForm.message)) {
  		reason += "Message " + validateEmpty(theForm.message);
  	}

	
     
  if (reason != "") {
    alert(reason);
    return false;
  }

  return true;
}

function validateForward(theForm) {
var reason = "";

  	if(validateEmail(theForm.femail)) {
  		reason += "Friend's Email: " + validateEmail(theForm.femail);
  	}
  	if(validateEmail(theForm.yemail)) {
  		reason += "Your Email: " + validateEmail(theForm.yemail);
  	}
  	if(validateEmpty(theForm.message)) {
  		reason += "Message " + validateEmpty(theForm.message);
  	}

	
     
  if (reason != "") {
    alert(reason);
    return false;
  }

  return true;
}


function validateSignup(theForm) {
var reason = "";
  	if(validateEmpty1(theForm.your_name)) {
  		reason += "Your Name " + validateEmpty1(theForm.your_name);
  	}
  	if(validateEmail1(theForm.email)) {
  		reason += validateEmail1(theForm.email);
  	}
     
  if (reason != "") {
    alert(reason);
    return false;
  }

  return true;
}
