// JavaScript Document

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

  reason += validateCompany(theForm.company);
  reason += validateCity(theForm.city);
  reason += validateProvince(theForm.province);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
  reason += validateCategory(theForm.category1, theForm.newcategories);
  reason += validateAbout(theForm.about);


  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

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

function validateCompany(fld) {
    var error = "";
    var illegalChars = /[^a-zA-Z0-9 \'\-\(\)]/;
 
    if (fld.value == "" || fld.value == "Company Name") {
		//document.getElementById('contactheading').style.background = '#CCFF77';
        fld.style.background = '#CCFF77'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 30)) {
        fld.style.background = '#CCFF77'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#CCFF77'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateCity(fld) {
    var error = "";
    var illegalChars = /[^a-zA-Z0-9 \'\-\(\)]/;
 
    if (fld.value == "" || fld.value == "City") {
        fld.style.background = '#CCFF77'; 
        error = "You didn't enter a city.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 30)) {
        fld.style.background = '#CCFF77'; 
        error = "The city is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#CCFF77'; 
        error = "The city contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateProvince(fld) {
    var error = "";
    var illegalChars = /[^a-zA-Z0-9 \'\-\(\)]/;
 
    if (fld.value == "" || fld.value == "Province") {
        fld.style.background = '#CCFF77'; 
        error = "You didn't enter a province.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 30)) {
        fld.style.background = '#CCFF77'; 
        error = "The province is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#CCFF77'; 
        error = "The province contains illegal characters.\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 == "Email") {
        fld.style.background = '#CCFF77';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#CCFF77';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#CCFF77';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "" || fld.value == "Phone Number") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = '#CCFF77';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#CCFF77';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#CCFF77';
    } else {
		fld.style.background = 'White';
	}
    return error;
}

function validateCategory(fld, fld2) {
	var error = "";
    var illegalChars = /[^a-zA-Z0-9 \'\-\(\)]/;
	
	if ((fld.value == "" || fld.value == "Select Category") && (fld2.value == "" || fld2.value == "Suggest New Categories")) {
		error = "You didn't enter a category.\n";
		fld.style.background = '#CCFF77';
		fld2.style.background = '#CCFF77';
	} else if (fld2.value.length > 100) {
        fld2.style.background = '#CCFF77'; 
        error = "The suggested category is the wrong length.\n";
    } else if (illegalChars.test(fld2.value)) {
        fld2.style.background = '#CCFF77'; 
        error = "The suggested category contains illegal characters.\n";
    } else {
		fld.style.background = 'White';
		fld2.style.background = 'White';
	}
	return error;
}

function validateAbout(fld) {
    var error = "";
    var illegalChars = /[^a-zA-Z0-9 \'\-\(\)\s\.\,\;\:\"]/;
	var tfld = trim(fld.value);
	
    if (tfld == "" || tfld == "A little bit about the company") {
        fld.style.background = '#CCFF77'; 
        error = "You didn't enter an about section.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 1000)) {
        fld.style.background = '#CCFF77'; 
        error = "The about section is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#CCFF77'; 
        error = "The about section contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}


