function subscribe() {

	var chk = true;
	if(document.forms[0].elements[0].value == ""){chk = false;}
	if(document.forms[0].elements[1].value == ""){chk = false;}
	if(document.forms[0].elements[2].value == ""){chk = false;}
	if(!isValidEmail(document.forms[0].elements[3].value)){chk = false;}
	if(chk){
		document.forms[0].elements[10].value = "subscribe";
		document.forms[0].elements[11].value = document.forms[0].elements[3].value;
		var txt = "Dear " + document.forms[0].elements[0].value + "&nbsp;" + document.forms[0].elements[1].value + ".<br />";
		txt = txt + "This message is to confirm your subscription to NineMillion.org Newsletter; ";
		txt = txt + "your details have been added to our mailing list:<br />";
		txt = txt + "Name: " + document.forms[0].elements[0].value + "&nbsp;" + document.forms[0].elements[1].value + "<br />";
		txt = txt + "Country: " + document.forms[0].elements[2].value + "<br />";
		txt = txt + "Email: " + document.forms[0].elements[3].value + "<br /><br />";
		txt = txt + "To unsubscribe, please send an email to: <a href=mailto:ninemillion@unhcr.org target=_blank>ninemillion@unhcr.org</a>";
		txt = txt + "<br /><br />Best regards.<br />The NineMillion.org Team.";
		document.forms[0].elements[12].value = txt;
		document.forms[0].elements[13].value = "NineMillion-Newsletters.org: Subscription confirmed";
		document.forms[0].submit();
	}
	else{
		alert("Please complete all fields and enter a valid email address.");
	}


}

function send() {

	//alert("send");
	var chk = true;
	if(document.forms[0].elements[5].value == ""){chk = false;}
	if(document.forms[0].elements[6].value == ""){chk = false;}
	if(document.forms[0].elements[7].value == ""){chk = false;}
	if(!isValidEmail(document.forms[0].elements[8].value)){chk = false;}
	if(chk){
		document.forms[0].elements[10].value = "send";
		document.forms[0].elements[11].value = document.forms[0].elements[8].value;
		var txt = "Dear " + document.forms[0].elements[7].value + ".<br />";
		txt = txt + "This message was sent to you because " + document.forms[0].elements[5].value;
		txt = txt + " thought you might find the page <i>www.ninemillion-newsletters.org</i>";
		txt = txt + " <http://www.ninemillion-newsletters.org> interesting. <br /> This is the link to the page:";
		txt = txt + " <a href=http://www.ninemillion-newsletters.org target=_blank>www.ninemillion-newsletters.org</a>.<br />";
		
		txt = txt + "Thank you for supporting the NineMillion.org UNHCR campaign!<br /><br />";
		txt = txt + "Best regards.<br />The NineMillion.org Team.";
		document.forms[0].elements[12].value = txt;
		document.forms[0].elements[13].value = "NineMillion-Newsletters.org";
		document.forms[0].submit();
	}
	else{
		alert("Please complete all fields and enter a valid email address.");
	}	

}



function isValidEmail(str){
	var at;
	var i = 0;
	var j = str.length - 1;
	var count = 0;
	at = str.indexOf("@", 0);
	if (at <= 0 || at == j) return false;
	while (i < at){
		if ((str.charAt(i) >= '0' && str.charAt(i) <= '9') ||
			(str.charAt(i) >= 'a' && str.charAt(i) <= 'z') ||
			(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') ||
			str.charAt(i) == '.' || str.charAt(i) == '&' || 
			str.charAt(i) == '?' || str.charAt(i) == '#' || 
			str.charAt(i) == '$' || str.charAt(i) == '*' || 
			str.charAt(i) == '+' || str.charAt(i) == '!' || 
			str.charAt(i) == '%' || str.charAt(i) == '\'' || 
			str.charAt(i) == '^' || str.charAt(i) == '/' || 
			str.charAt(i) == '_' || str.charAt(i) == '-')
				i++;
		else return false;
	}
	i = at + 1;
	if(str.charAt(i) == '.' || str.charAt(j) == '.') return false;
	while (i <= j){
		if(str.charAt(i) == '.'){
			count++;
			if (str.charAt(i + 1) == '.') return false;
			else i++;
		}
		if((str.charAt(i) >= '0' && str.charAt(i) <= '9') || (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') ||
			(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') || str.charAt(i) == '_' || str.charAt(i) == '-')
			i++;
		else return false;
	}
	return (count == 0) ? false : true;
}

