/****************************************************************************   
Java Scripts used for Email Colleague functionality
***************************************************************************/


function imposeMaxLength(Object, MaxLen){
	return (Object.value.length < MaxLen);
}

function validateEmailForm(){

	var emailto = document.emailForm.key_recipientEmailAddress.value;
	var hasErrors = false;
	hideAllErrors();
	if(emailto==""){
		document.getElementById("recipientEmailError").style.display="inline";
		document.getElementById("recipientEmail").style.display="none";
		hasErrors = true;
	}else if(!validateEmailRecipients(emailto)){
		document.getElementById("validRecipientError").style.display="inline";
		document.getElementById("recipientEmail").style.display="none";
		document.getElementById("recipientEmailError").style.display="inline";
		hasErrors = true;
	}
	var firstname = document.emailForm.key_firstName.value;
	if(firstname==""){
		document.getElementById("firstNameError").style.display="inline";
		document.getElementById("firstName").style.display="none";
		hasErrors = true;
		
	}else if(!validateRecipientName(firstname)){
		document.getElementById("firstNameError").style.display="inline";
		document.getElementById("firstName").style.display="none";
		document.getElementById("validFirstNameError").style.display="inline";
		hasErrors = true;
		
	}	
	var lastname = document.emailForm.key_lastName.value;
	if(lastname==""){
		document.getElementById("lastNameError").style.display="inline";
		document.getElementById("lastName").style.display="none";
		hasErrors = true;

	}else if(!validateRecipientName(lastname)){
		document.getElementById("validLastNameError").style.display="inline";
		document.getElementById("lastNameError").style.display="inline";
		document.getElementById("lastName").style.display="none";
		hasErrors = true;

	}
	var email = document.emailForm.key_emailAddress.value;
	if(email==""){
		document.getElementById("yourEmailError").style.display="inline";
		document.getElementById("yourEmail").style.display="none";
		hasErrors = true;

	}else if(!validateEmail(email)){
		document.getElementById("yourEmailError").style.display="inline";
		document.getElementById("yourEmail").style.display="none";
		document.getElementById("validYourEmailError").style.display="inline";
		hasErrors = true;
	}
	
	var comments = document.emailForm.key_message.value;
	if(comments.length >2000){
		document.getElementById("messageLengthError").style.display="inline";
		hasErrors = true;

	}   
   if(!hasErrors){
	   	document.emailForm.submit();
   }else{
   		document.getElementById("requiredFieldsMsgError").style.display = "inline";
		document.getElementById("requiredFieldsMsg").style.display = "none";
   		return false;	
   }		
}
	
function validateRecipientName(str){
	var strRE = "[0-9\@\*\&\^\(\)\%\\$\#\!\\~\`\|\\[\\]\;\\:\"]";
	if(str.match(strRE)) {
		return false;
	} else{
		return true;
	}
}
	
function validateEmail(str) {

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var lastldot=str.lastIndexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	   return false;
	}

	if (str.indexOf(at,(lat+1))!=-1){
	   return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false;
	}

	if (str.indexOf(dot,(lat+2))==-1){
	    return false;
	}
	
	if (str.indexOf(" ")!=-1){
	    return false;
	}
	
	if (lastldot==(lstr-1)){
		return false
	}

	return true;				
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function validateEmailRecipients(emailTo){
	var comma = ",";
	emailTo = emailTo + comma;
	var lcomma = emailTo.indexOf(comma);
	var inValidEmailfound = false;
	var previousLcomma = 0;
	while(lcomma!=-1){
		var emailAdd = emailTo.substring(previousLcomma, lcomma);
		var inValidEmailfound = validateEmail(trim(emailAdd));
		if(inValidEmailfound){
			previousLcomma = lcomma + 1;
			lcomma = emailTo.indexOf(comma, lcomma+1);
		}else{
			return false;
		}
	}
	return true;
	
}
function hideAllErrors() {
	document.getElementById("requiredFieldsMsgError").style.display = "none";
	document.getElementById("requiredFieldsMsg").style.display = "inline";
	document.getElementById("recipientEmailError").style.display="none";
	document.getElementById("recipientEmail").style.display="inline";
	document.getElementById("validRecipientError").style.display="none";
	document.getElementById("firstNameError").style.display="none";
	document.getElementById("firstName").style.display="inline";
	document.getElementById("validFirstNameError").style.display="none";
	document.getElementById("lastNameError").style.display="none";
	document.getElementById("lastName").style.display="inline";
	document.getElementById("validLastNameError").style.display="none";
	document.getElementById("yourEmailError").style.display="none";
	document.getElementById("yourEmail").style.display="inline";
	document.getElementById("validYourEmailError").style.display="none";
}
