// This javascript file contains functions used by EMM. It has functions for validating email, submitting optin form.

// This function returns true if passed parameter is alphabate otherwise returns false
function isLetter (c){
  return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

// This function returns true if passed parameter is digit otherwise returns false
function isDigit (c){
  return ((c >= "0") && (c <= "9"))
}

// This function returns true if passed parameter is digit or alphabate  otherwise returns false
function isLetterOrDigit (c){
  return (isLetter(c) || isDigit(c))
}

// This function validate the email address entered by user when he clicks on Go button in optin form. If email address is valid
// it submits the form to the Responsys. This function gets the email address by calling getElementById() function. Before 
// submitting the form it sets the optin to passwed optin name.

function submitSubscriber(optinName)
{	
	if( validateSubscribe() != false)
	{
		document.subscribeform.email.value = document.getElementById("emailAddId").value;
		document.getElementById("optinId").name = optinName;
		document.subscribeform.submit();
	}
}

// This function is called for validating the email address. If email address is valid it returns the trur otherwise it 
// returns false and set the focus to email address text field after alerting user to enter valid email address.	
function validateSubscribe()
{

	
	emailStr = document.getElementById("emailAddId").value; //get email address through getElementById() function

    indx = emailStr.indexOf("@")
	invalidChars = "!@#$%&=~_/?`|^*-+{},'" ; //String of invalid characters

	add = emailStr.substring(0,indx) ;		
	domain=emailStr.substring(indx+1,emailStr.length)
    firstChar = emailStr.charAt(0);			
	lastChar = emailStr.charAt(emailStr.length-1); 
	
	// To check that email address does not start or end with special character
	i=0;
	for(i=0;i<invalidChars.length;i++)
	{
		if( (invalidChars.charAt(i) == firstChar) || (invalidChars.charAt(i) == lastChar) )
		{
			alert("Invalid Email Address. Email address can not start or end with special characters");
			document.getElementById("emailAddId").focus();
			return false;
		}
	}

	// To check that domain name in email address does not contain special character
	i=0;
	for(i=0;i<domain.length;i++)
	{
		if(  (! isLetterOrDigit(domain.charAt(i))) && (domain.charAt(i) != '-') && (domain.charAt(i) != '.') )
			{
				alert("Invalid Email Address. Domain name can not have special characters");
				document.getElementById("emailAddId").focus();
				return false;
			}
	}


	// To check that sub domain name in email address is valid
	i=0;
	idx=0;
	subdomain = domain.substring(domain.indexOf(".")+1,domain.length);
	for(i=0;i<subdomain.length;i++)
	{
		idx=subdomain.indexOf(".");
		tempStr = subdomain.substring(0,idx);
		if( idx != -1)
			{
				if( tempStr.length < 2 || tempStr.length > 4)
					{
						alert("The email doesn't seem to be valid.");
						document.getElementById("emailAddId").focus();
						return false;
					}
			}
		domain = domain.substring(idx+1,domain.length);
	}

	
	// If email adress is an empty string
	if(emailStr=="")
	{
		alert("Invalid Email Address. Email address can not be empty.");
		document.getElementById("emailAddId").focus();
		return false;
	}
	
	// If first char is space in email address
	if(emailStr.charAt(0)==" ")
	{
		alert("Invalid Email Address. First character can not be a blank space.");
		document.getElementById("emailAddId").focus();
		return false;
	}
	
	//rest of validation
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>\\@%&,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null)
	{
		alert("Email address seems incorrect (check @ and .'s)");
		document.getElementById("emailAddId").focus();
		document.subscribeform.email.select();
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	if (user.match(userPat)==null)
	{
	    alert("The email doesn't seem to be valid.");
	    document.getElementById("emailAddId").focus();
	    return false
	}
	
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null)
	{
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        alert("Destination IP address in Email is invalid!")
		        document.getElementById("emailAddId").focus();
			return false
		    }
	    }
	    return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null)
	{
		alert("The domain name in Email Address doesn't seem to be valid.");
		document.getElementById("emailAddId").focus();
		document.subscribeform.email.select();
	    return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
	{
	   // the address must end in a two letter or three letter word.
	   alert("The email address must end in a three-letter domain, or two letter country.");
	   document.getElementById("emailAddId").focus();
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2)
	{
	   var errStr="The email address is missing a hostname!"
	   alert(errStr);
	   document.getElementById("emailAddId").focus();
	   return false
	}
}


// This function is called when user clicks on Change your Preference link.
// This function validates the email address after getting through getElementById(). It sets the optin to passed optinName.
// Then it opens a new window with Responsys URL.
function onClickLink(optinName)
{
	if( validateSubscribe() != false)
		{
			var emailid = document.getElementById("emailAddId").value;
			document.getElementById("optinId").name = optinName;
			var url = "http://motorolaiDEN.rsc01.net/servlet/campaignrespondent?_ID_=mot.177&email=" + emailid;
			window.open(url);
		}else
			{
				return false;
			}
}
