function validateSubscribeForm()
{
	clearErrorMsgs();
	trimAttributes();
	if(validateAttributes())
	{
		document.CRNewsSub.submit();
	}
}

function trimAttributes()
{
    document.CRNewsSub.email.value =trimAll(document.CRNewsSub.email.value);
}

function trimAll(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function clearErrorMsgs()
{
	document.getElementById('emailError').innerHTML="";
}
function validateAttributes()
{
	var email = document.CRNewsSub.email.value;
	var isAttrValid = true;
    if(isEmptyString(email))
	{
		isAttrValid = false;
		document.getElementById('emailError').innerHTML="Please enter your Email";
	 }
	 else if(validateEMailFormat(email) == false )
	 {
		isAttrValid = false;
		document.getElementById('emailError').innerHTML="Please enter a valid Email"; 
	 }
	
	 return isAttrValid;
}
   
function isEmptyString(text)
{

	if( text == null || text == '')
		return true;
		
	var empty = true;
	
	for(var k=0; k< text.length; k++)
	{
		if( text.charAt(k) != ' ' )
			return false;
	}
	
	return empty;
}

function validateEMailFormat(email)
{
	var atIndex = email.lastIndexOf('@');
	var dotIndex = email.lastIndexOf('.');	
	var stringBeforeAt =  email.substring(0, atIndex);     
	var stringafterAt =  email.substring(atIndex+1 , email.length);   
    if(email.indexOf('.')==0)
	{
	   return false;
	}
    
	if((stringafterAt == null  || stringafterAt =='') || (stringBeforeAt == null || stringBeforeAt =='') )
	{
	   return false;
	}
	
	if(!/^[\,\.0-9A-Za-z-_-]+.$/.exec( stringafterAt) || !/^[`~\"'"+?^~!#$%&*=\/|®\.0-9A-Za-z-_-]+$/.exec( stringBeforeAt))
	{
	   return false;
	}
	
	if (email.indexOf('@') != email.lastIndexOf('@') || atIndex == -1 || atIndex == 0 ||
		dotIndex == -1 || dotIndex == 0 || atIndex == dotIndex +1 || atIndex +1 == dotIndex || 
		stringBeforeAt.lastIndexOf(".") + 1 == atIndex  || email.length == email.lastIndexOf(".") + 1 ||stringafterAt.indexOf(".") == 0)
	{
		return false;
	}
	else
	{
	
		dotIndex = stringafterAt.lastIndexOf('.');	
		stringAfterDot = stringafterAt.substring(dotIndex, stringafterAt.length); 
		if (stringAfterDot.indexOf('.') == -1 )
		{
			return false;
		}  
		while (dotIndex !=-1 )
		{
			email = email.replace('.', '');
			if (dotIndex == email.indexOf('.'))
			{
				return false;
			}
			else
			{
				dotIndex = email.indexOf('.');
			}
		} 
	}
	return true;
}
function switchCRSubDivs(subAction)
{
	if(subAction=='Subscribe')
	{
		document.getElementById('subscribe').style.display ='none';
		document.getElementById('unsubscribeConfirm').style.display ='none';
		document.getElementById('subscribeConfirm').style.display ='';
	}
	else
	{
		document.getElementById('subscribe').style.display ='none';
		document.getElementById('subscribeConfirm').style.display ='none';
		document.getElementById('unsubscribeConfirm').style.display ='';
		
	}
	
}
function switchDivstoUnsub(emailValue)
{
    document.CRNewsSub.email.value=emailValue;
	document.CRNewsSub.subscription[0].checked='';
	document.CRNewsSub.subscription[1].checked='checked';
	document.CRNewsSub.subButton.value='unsubscribe';
	document.getElementById('subscribe').style.display ='';
    document.getElementById('subscribeConfirm').style.display ='none';
	document.getElementById('unsubscribeConfirm').style.display ='none';
}