function countEmails(sText)
{
   var separator = ";";
   var Char;
   var amount = 1;
 	
   for (i = 0; i < sText.length; i++) 
      { 
      Char = sText.charAt(i); 
      if (Char == separator) 
         {
         amount++;
         }
      }
	  
	  if (amount > 30){
	  	alert ('Too many recipients! (Max. 30 allowed)\n You have now entered '+amount);
		return false;
	  }
	  else return true;
   }	
	

function isValidEmails(eAddr) {
	countEmails(eAddr);
	var email_array = eAddr.split(/\s*;\s*/);
	while( email_array.length > 0 ) {
		if( ! echeck(email_array.pop()) ) {
			return false;
		}
	}

	return true;
}

/*function isValidEmail(str) {
	alert(str);
	ndxDot = str.indexOf(".");
	ndxAt  = str.indexOf("@");
	return (ndxDot > 2 && ndxAt > 0);
}*/

function leftTrim(str) 
{
   while ( str.substring(0,1) == ' ' )
      str = str.substring(1, str.length);

   return str;
}


function isEmptyString(str)
{
   if ( str == null )
      return true;

   if ( leftTrim(str).length == 0 )
      return true;

   return false;
}

function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		  // alert("Invalid E-mail Address")
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		  // alert("Invalid E-mail Address")
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   // alert("Invalid E-mail Address")
		    return false
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		   // alert("Invalid E-mail Address")
		    return false
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   // alert("Invalid E-mail Address")
		    return false
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		  //  alert("Invalid E-mail Address")
		    return false
		 }
		 if (str.indexOf(" ")!=-1){
		   // alert("Invalid E-mail Address")
		    return false
		 }
 		 return true					
	}



function checkform()
{
	var error=0;
	var nameError=0;
	var emailError=0;
	var recipientsError=0;
	var subjectError=0;
	var greetingError=0;
	
		// Set variables to normal if runned again 
		document.getElementById('nameLabel').className='';
		document.getElementById('recipientsLabel').className='';
		document.getElementById('subjectLabel').className='';
		document.getElementById('greetingLabel').className='';
		document.getElementById('emailLabel').className='';
	
	if ( isEmptyString(document.getElementById('name').value) )
	{
		nameError=1;
		error=1;
	}

	if ( isEmptyString(document.getElementById('email').value) )
        {
		emailError=1;
		error=1;
	} //else if( ! isValidEmail(document.getElementById('email').value) ) {
	else if( ! echeck(document.getElementById('email').value) )
        {
		emailError=1;
		error=1;
	}

	if ( isEmptyString(document.getElementById('recipients').value) )
        {
		recipientsError=1;
		error=1;
	}
        else if( ! isValidEmails(document.getElementById('recipients').value) )
        {
		recipientsError=1;
		error=1;
	}

	if ( isEmptyString(document.getElementById('greeting').value) )
	{
		greetingError=1;
		error=1;
	}

	if ( isEmptyString(document.getElementById('subject').value) )
	{
		subjectError=1;
		error=1;
	}

	if (error==1){
		document.getElementById('ingress').style.display='none';
		document.getElementById('sorryText').style.display='block';
		
		if (nameError==1){
		document.getElementById('nameLabel').className='error';
		}
		if (emailError==1){
		document.getElementById('emailLabel').className='error';
		}
		if (recipientsError==1){
		document.getElementById('recipientsLabel').className='error';
		}
		if (greetingError==1){
		document.getElementById('greetingLabel').className='error';
		}
		if (subjectError==1){
		document.getElementById('subjectLabel').className='error';
		}
		return false;
	}
	
	// If no errors return true 
	return true;
}

function infoboxEvents() {
	if (document.getElementById("ibutton") && document.getElementById("infobox")) {
		var box = document.getElementById("infobox");
		document.getElementById("ibutton").onmouseover = function() { box.style.display = "block"; };
		document.getElementById("ibutton").onmouseout = function() { box.style.display = "none"; };
	};
};