if (window.location.search.indexOf("err=1")!=-1) alert ('You must fill in your name and email!');
if (window.location.search.indexOf("err=2")!=-1) alert ('You must fill in both name and email!');
if (window.location.search.indexOf("err=3")!=-1) alert ('Some of the emails match each other!');
function emailCheck(field){
	var errr = false;
	var arrEmail = field.value.split('@');
	if(arrEmail.length != 2 || arrEmail[0].length < 1)
		errr = true;
	if(!errr) {
		if(arrEmail[0].length > 2)
			res = arrEmail[0].search(/^[-\w][-\.\w]+[-\w]$/gi);
		else
			res = arrEmail[0].search(/^[-\w]+$/gi);
		if(res == -1)
			errr = true;
	}
	if(!errr) {
		var arr2Email = arrEmail[1].split('.');
		if(arr2Email.length < 2)
			errr = true;
	}
	if(!errr) {
		var domenTail = arr2Email[arr2Email.length-1];
		var _arr2Email = new Array();
		for(x = 0; x < arr2Email.length-1; x++)
			_arr2Email[x] = arr2Email[x];
		arr2Email = _arr2Email;
		var domen = arr2Email.join('.');
		res = domen.search(/^[-!#\$%&*+\\\/=?\.\w^`{|}~]+$/gi);
		if(res == -1)
			errr = true;
		res = domenTail.search(/^[a-zA-Z]+$/gi);
		if(res == -1 || domenTail.length < 2 || domenTail.length > 6)
			errr = true;
	}
	return !errr;
}
function validate(){
	j=0;
	err = "";
	var fields = 5;
	var min_fields = 5;
	var repp="";
	for (i=1;i<=fields;i++){
		if (document.getElementById("name"+i).value != '' && document.getElementById("email"+i).value != '' && document.getElementById("email"+i).value != repp && emailCheck(document.getElementById("email"+i))){
			j++;
			repp = document.getElementById("email"+i).value;
		}
	}
	if (j < min_fields){
		err += "You are required to send at least "+min_fields+" emails.\n";
	}

	if (document.getElementById("member_name").value == '' || document.getElementById("member_email").value == '' || !emailCheck(document.getElementById("member_email"))){
		err += "You need to fill your name and email.\n";
	}

	if (err != ""){
		alert(err);
		return false;
	}else return true;
}
