//script.js

var errorShowing;

function fieldFocus(field)	{
field.style.backgroundColor = "#ffffff";
}

function fieldBlur(field)	{
field.style.backgroundColor = "#f7f7f7";

}

function addClass(element, value) {

	if(!element.className) {
	element.className = value;
	}	else {
		newClassName = element.className;
		newClassName+= " ";
		newClassName+= value;
		element.className = newClassName;
		}

}

function RemoveClassName(objElement, strClass)	{

   // if there is a class
   if ( objElement.className ) {

      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');

      // get uppercase class for comparison purposes
      var strClassUpper = strClass.toUpperCase();

      // find all instances and remove them
      for ( var i = 0; i < arrList.length; i++ )	{
		// if class found
         if ( arrList[i].toUpperCase() == strClassUpper )	{
            // remove array item
            arrList.splice(i, 1);
            // decrement loop counter as we have adjusted the array's contents
            i--;
            }
         }
   }
}
		 
	  
		 

function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

var errorMsg = '<strong>Oops! You forgot some things:</strong><br>';

function validate(form)	{

		form = document.getElementById('commentForm');
		
		var is_valid = true;
		
		if (!author_check(form))	{
			is_valid = false;
		}
		
		if (!email_check(form))	{
			is_valid = false;
		}
		
		if (!comment_check(form))	{
			is_valid = false;
		}
		
		if (!is_valid)	{
			document.getElementById('error-msg').innerHTML = errorMsg;
			errorMsg = '<strong>Oops! You forgot some things:</strong><br>';
			errorShowing = 1;
		}
		return is_valid;
				
}

function author_check(form)	{
		if(!form.author) return true;
		if(form.author.value == '')	{
			errorMsg += 'Please enter a name (It doesn&rsquo;t have to be real). <br>';
			form.author.style.backgroundColor = '#FFE0E0;';
			return false;
		}	else	{
				return true;
			}
}

function email_check(form)	{
		if(!form.email) return true;
		var emailReg = new RegExp("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$");
		var email = form.email.value;
		if(emailReg.test(email) !=true)	{
			errorMsg += 'Please enter a valid email address. <br>';
			form.email.style.backgroundColor = '#FFE0E0;';
			return false;
		}	else	{
				return true;
			}
}

function comment_check(form)	{
		if(!form.comment) return true;
		if(form.comment.value == '')	{
			errorMsg += 'Please enter a comment. <br>';
			form.comment.style.backgroundColor = '#FFE0E0;';
			return false;
		}	else	{
				return true;
			}
}
