function performRegistration() {
	var form = document.register;
 	var why = "";

	why += checkFirstName(form.first_name.value); 
	why += checkLastName(form.last_name.value); 
	why += checkCompanyName(form.company_name.value); 
	why += checkEmailAddress(form.email_address.value); 
	why += checkPhone(form.phone.value); 

	if (why != "") {
		alert(why);
		return false;
	}
	else {
		return true;
	}	

}


function checkFirstName(strng) {
   var error = "";
   if (isWhitespace(strng) == true) {
       error += "First Name is a required field\n";
   }
   else {
      var illegalChars = /\W/;
      // allow only letters, numbers, and underscores
      if (illegalChars.test(strng)) {
        error += "First Name contains illegal characters.\n";
      }

     // first name can't be smaller than 3 chars
     if (strng.length < 2) {
          error = "First Name is the wrong length.\n";
      }
   }

   return error;
}

function checkLastName(strng) {
   var error = "";
   if (isWhitespace(strng) == true) {
       error += "Last Name is a required field\n";
   }
   else {
      var illegalChars = /\W/;
      // allow only letters, numbers, and underscores
      if (illegalChars.test(strng)) {
        error += "Last Name contains illegal characters.\n";
      } 
     
     // last name can't be smaller than 3 chars
     if (strng.length < 2) {
          error = "Last Name is the wrong length.\n";
      }

   }

   return error;
}

function checkCompanyName(strng) {
   var error = "";
   if (isWhitespace(strng) == true) {
       error += "Company Name is a required field\n";
   }
    
     // company name can't be smaller than 3 chars
    else if (strng.length < 2) {
          error = "Company Name is the wrong length.\n";
      }

   

   return error;
}

function checkEmailAddress(strng) {
   var error = "";
   if (isWhitespace(strng) == true) {
       error += "Email Address is a required field\n";
   }
   else {
       // For email addresses, we’re forbidding the following: ( ) < > [ ] , ; : \ / "
       var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/
       if (strng.match(illegalChars)) {
          error += "The email address contains illegal characters.\n";
       }

       if(isValidEmail(strng) == false) {
		error += "Invalid Email Address entered.\n";
	}
   }
   return error;

}

function checkPhone(strng) {
   var error = "";
   if (isWhitespace(strng) == false) {
       // However, if value entered, check to see if it's valid
       var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
       //strip out acceptable non-numeric characters
       if (isNaN(parseInt(stripped))) {
          error += "The phone number contains illegal characters.\n";
       }

       // phone number must be atleast 10 digits
       if (!(stripped.length >= 10)) {
	error += "The phone number is the wrong length. Make sure you included an area code.\n";
       }
   }

   return error;
}


function isWhitespace (s) {   
	var i;
	
	// whitespace characters
	var whitespace = " \t\n\r";
	
	// Is s empty?
	if (isEmpty(s)) return true;

	// Search through string's characters one by one
	// until we find a non-whitespace character.
	// When we do, return false; if we don't, return true.

	for (i = 0; i < s.length; i++) {   
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}

	// All characters are whitespace.
	return true;
}

function isValidEmail(email)
    {
        var re;
        // Rules for the email regular expression:
        // The start of the email must have at least one character 
        // before the @ sign
        // There may be either a . or a -, but not together before the @ sign
        // There must be an @ sign
        // At least once character must follow the @ sign
        // There may be either a . or a -, but not together in the address
        // The address must end with a . followed by at least 2 characters
        re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
        if (re.test(email) == true) {
		return true;
	}
        else {
		return false;
        }
 }

function isEmpty(s) {   
	return ((s == null) || (s.length == 0))
}

