var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
var NSX = (navigator.appName == "Netscape");
var IE4 = (document.all) ? true : false;

function addOption(theForm,name,email)
{
	if (NSX)
	{
		addOptionNS(theForm,name,email);
	}
	else if (IE4)
	{
		addOptionIE(theForm,name,email);
	}
}

// (1) addOptionNS(theForm)
function addOptionNS(theForm,name,email)
{
	if(name!=""&&email!="")
	{
	if(!checkemail(email))
	{
		alert("Invalid friend email address!");
	}
	else
	{
	if(theForm.friendlist.length<5)
	{
	var newOpt  = new Option(name+"<"+email+">",name+"<"+email+">");
	var selLength = theForm.friendlist.length;
	theForm.friendlist.options[selLength] = newOpt;
	theForm.friend_email.value="";
	theForm.friend_name.value="";
	theForm.friend_name.focus();
	if (NS4) history.go(0);
	}
	}
	}
	else
	{
		alert("Please insert your friend's name and email address");
	}
}

// (2) addOptionIE(theForm)
function addOptionIE(theForm,name,email)
{
	if(name!=""&&email!="")
	{
	if(!checkemail(email))
	{
		alert("Invalid friend email address!");
	}
	else
	{
	if(theForm.friendlist.length<5)
	{
	var newOpt = document.createElement("OPTION");
	newOpt.text=name+"<"+email+">";
	newOpt.value=name+"<"+email+">";
	theForm.friendlist.add(newOpt);
	theForm.friend_email.value="";
	theForm.friend_name.value="";
	theForm.friend_name.focus();
	}
	}
	}
	else
	{
		alert("Please insert your friend's name and email address");
	}
}

function deleteOption(theForm)
{	
	if (NSX)
	{
		deleteOptionNS(theForm);
	}
	else if (IE4)
	{
		deleteOptionIE(theForm);
	}	
}

// (3) deleteOptionNS(theForm)
function deleteOptionNS(theForm)
{
	for (var i=theForm.friendlist.options.length-1;i>=0;i--)
   	{
		if (theForm.friendlist.options[i].selected==true)	theForm.friendlist.options[i]=null;
	}
	if (NS4) history.go(0);
}

// (4) deleteOptionIE(theForm)
function deleteOptionIE(theForm)
{
	for (var i=theForm.friendlist.options.length-1;i>=0;i--)
   	{
		if (theForm.friendlist.options[i].selected==true) theForm.friendlist.remove(i);
	}
}
function SelectAll(theForm)
 {
   //theForm.friendlist.type="select-multiple";
   for (var i=0;i<theForm.friendlist.options.length;i++)
   {
     theForm.friendlist.options[i].selected=true;
   }
 }

function VerifyForm(thisForm)
{
	var error = 0;
	var errorMsg = "";
	
	if(trim(thisForm.sender_name.value)=="") 
	{
		error+=1;
		errorMsg+="Please insert your name\n";
	}
	
	if(trim(thisForm.sender_email.value)!="") 
	{
		if(!checkemail(thisForm.sender_email.value))
		{
			error+=1;
			errorMsg+="Invalid email address\n";
		}
	}
	
	if(trim(thisForm.sender_email.value)=="") 
	{
		error+=1;
		errorMsg+="Please insert your email address\n";
	}
	
	if(thisForm.friendlist.options.length==0)
	{
		error+=1;
		errorMsg+="Please insert at least one of your friend's email address and name\n";
	}
	
	if (error==0)
	{
		thisForm.friendlist.name='friendlist[]';
		SelectAll(thisForm);
		return true;
	
	}else
		{
		alert(errorMsg);
		return false;
		}
	
}

function checkemail(email){
	
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(email))	return true;
	else return false;
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

