// JavaScript Document
//var MAX_COUNT = 160;

function addtolist() {
		
	var receiptname = document.send_friend.fname.value;
	var receiptemail = document.send_friend.femail.value;
	
//	var listitem start = receiptemail.substring(0,2);
	/*var len			= receiptemail.length;*/
	var maxLength =5;
//if(validateForm()) {
	
	if( checkemail(receiptemail) == false )
	{
		alert('Please input a valid email address!');
		document.send_friend.femail.focus();
		return false;
	}	
	else if (document.send_friend.fname.value == "") {
			alert("Name field cannot be left empty!");
			document.send_friend.fname.focus();
			return false;
	}
	else {
			var list = document.send_friend.emailadd; //sendSMS.reclist
			var listLength = list.length;
			if ( (listLength+1) > maxLength)
			{
				alert("Cannot send message to more than " +maxLength+ " recipients");
				return false;
			}
			var listitem  = receiptname+" => " +receiptemail;
		//	var listitem = receiptname+"-"+receiptemail;
		//	var countryCode	= document.send_friend.countrycode.value;
			//listitem 	=	countryCode+listitem ;
			
			
			var targetIndex = listLength;
			for( var i = listLength - 1; i >= 0; i-- ) {
				if( list.options[i].text == (receiptname+ " => " +receiptemail) ) {
					alert( "Email ("+list.options[i].text+") already exists!" );
					return;
				}
				if( list.options[i].text > listitem  ) {
					targetIndex = i;
				}
			} //end of for
			for(var i = listLength -1; i >= targetIndex; i--) {
				list.options[i+1] = new Option( list.options[i].text );
			}
			list.options[targetIndex] = new Option(listitem );
			list.options[targetIndex].value = listitem;
//			list.options[targetIndex].selected = true;
			list.options[targetIndex].selected = 'selected';
//			list.options[targetIndex].text = document.send_friend.receiptname.value + " - "+document.send_friend.femail.value ;
			list.options[targetIndex].text = receiptname + " => "+receiptemail;
//			targetIndex++;
			
			document.send_friend.fname.value = '';	
			document.send_friend.femail.value = '';
			
		}

//	document.send_friend.femail.focus();
	document.send_friend.fname.focus();
return true;
//}
}
/*
			if ( (listLength+1) > maxLength)
			{
				alert("Cannot send message to more than " +maxLength+ " recipients");
				return false;
			}
	*/
	/*		
			if(receiptno.length < 10)
			{

				alert("Phone listitem  should be atleast 7 digits long ");
				document.send_friend.femail.focus();
				return false;
			}
		*/	
			// for sorting, targetIndex is where should we insert the listitem  at
			

function validateForm() { 

	var name 	= document.send_friend.name.value;
	var email	= document.send_friend.email.value;
	var fname	= document.send_friend.fname.value;
	var femail	= document.send_friend.femail.value;
	var message	= document.send_friend.message.value;
	var flag = true;
	
	if (name == '') {
		alert('Name is required');
		document.send_friend.name.focus();
		flag = false;
		//return false;
	}
	else if(checkemail(email) == false )
	{
		alert('Please input a valid email address!');
		document.send_friend.email.focus();
//		return false;
		flag = false;
	}	
	else if(document.send_friend.emailadd.length == 0 ) {
			alert("Add email!");
			document.send_friend.femail.focus();
		//	return false;
			flag = false;
	} 
	else if (document.send_friend.emailadd.length > 0 ) 
	{
	//		alert("Add email!");
		//	document.send_friend.femail.focus();
			flag = true;
				if (name == '') 
				{
						alert('Name is required');
						document.send_friend.name.focus();
						flag = false;
				}
				else if(checkemail(email) == false )
				{
						alert('Please input a valid email address!');
						document.send_friend.email.focus();
						flag = false;
				}
				else if(message == "") 
				 {
						alert("Please enter the message to be sent");
						document.send_friend.message.focus();
						//return false;
						flag = false;
				 }
				
			return flag;
	}
return flag;
}
/*
	else if(message == "") {
		alert("Please enter the message to be sent");
		document.send_friend.message.focus();
		//return false;
		flag = false;
	}
	*/
/*	
	else if (fname == '') {
		alert("Friend's name is required");
		document.send_friend.fname.focus();
		//return false;	
		flag = false;
	}/*if(document.send_friend.femail.value.length == "") {
		if(document.send_friend.emailadd.length == "") {
			alert("Please enter email of the recipient");
			document.send_friend.femail.focus();
			return false;
		}
	}*/
	/*
	else if( checkemail(femail) == false )
	{
		alert('Please input a valid email address!');
		document.send_friend.femail.focus();
		flag = false;
//return false;
	}	
	
	
	
	/*else {
	//	var listLength = document.send_friend.emailadd.length;
	//	for(i=0;i<listLength;i++) {
	//	document.send_friend.emailadd.options[i].selected = true;
		return true;	
		}
*/
	
	
/*	else if( email == '')
	{
		alert('Email is required');
		document.send_friend.email.focus();
		return false;	
	}
	*/
	

/*	
	if (document.send_friend.message.value.length > MAX_COUNT ) {
		alert("Too much data in the text area ("+document.send_friend.value+" characters).\nYour message should not exceed "+MAX_COUNT+" characters.");
		return false;
	}
	*/
//	return true;
	


	
	
function checkemail(email){
//var str=document.validation.emailcheck.value
var str=email
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
testresults=true
else{
//alert("Please input a valid email address!")
testresults=false
}
return (testresults)
}
/*
function isValidEmail(email)
{
	
    var RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/  
	
	
	var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/ 
	
//var RegExp = ^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$ 
//	if(RegExp.test(email) != true)
	if(emailRegxp.test(email) != true)
	{
        return false;
    }else
	{
        return true;
    }
} 
*/
function removeselected() {
	if(document.send_friend.emailadd.selectedIndex < 0) {
		alert( "Select an email to Remove!" );
	} else {
		var options = document.send_friend.emailadd.options;
		for( var i = options.length - 1; i >= 0; i-- ) {
			if( options[ i ].selected ) {
				options[ i ] = null;
			}
		}
		return false;
	}
}
/*
function countCharacters() {
	setTimeout("countCharactersCall()", 10);
}
var msgSize=0;
function countCharactersCall() {

	//msgSize = MAX_COUNT - document.send_friend.message.value.length ;
	document.send_friend.value=MAX_COUNT - document.send_friend.message.value.length ;
	if (document.send_friend.value < 0) {
		document.send_friend.message.value = document.send_friend.message.value.substring(0, MAX_COUNT);
		alert("Your message cannot exceed more than 160 characters");
		
		document.send_friend.value = 0;
	} 
}

function addPhonelistitem s(string)
{
	document.send_friend.mobilelist.value=string;
	var separator = ',';
	var stringArray = string.split(separator);
	var length=5;
	var existinglistitem s=string.split(document.send_friend.emailadd);
	list = document.send_friend.emailadd;
	
	if((list.length+stringArray.length)>6)
	   {
	   alert("Cannot send SMS message to more than 5 receipients.");
	   length=5-list.length;
	   }
	  else{length=stringArray.length-1;
	  }
	for (var i=0; i < length; i++)
	
	{		   
		    	addFromAddressBook(stringArray[i]);	    			    	
			
	}
}
*/
