function check_and_submit(theForm)
{
	var why = "";
	why += checkEmail(theForm.email.value);
	why += checkPhone(theForm.phone.value,"phone number");
	why += checkFax(theForm.fax.value, "fax number");	
	why += checkString(theForm.name.value,"name");
	why += checkString(theForm.surname.value,"surname");
	why += checkString(theForm.region.value,"region");
	why += checkString(theForm.country.value,"country");
	why += checkString(theForm.org_name.value,"organization name");
	why += checkString(theForm.profession.value,"profession");
	why += checkString(theForm.position.value,"position");
	why += checkDropdown(theForm.org_type.selectedIndex, "organization type");
	why += checkDropdown(theForm.interest.selectedIndex, "research interest");
	if (why != "") {
		alert(why);
		return false;
	}
	theForm.submit();
}

function checkString (strng, field) 
{
	 var error = "";
	 if (strng == "") 
	 {
	    error = "The field '"+field+"' is required.\n";
	 }
	/*var illegalChars = /\W/;
	  // allow only letters, numbers, and underscores
	    if (illegalChars.test(strng)) 
	    {
	         error = "The "+field+" contains illegal characters.\n";
	    a}*/ 
	    return error
}

function checkEmail(strng)
{
	var error = "";
	var emailFilter=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if (!(emailFilter.test(strng))) 
	{ 
	       error = "Please enter a valid email address.\n";
	}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars)) 
	{
	   error = "The email address contains illegal characters.\n";
	}
        if (strng == "")
        {
           error = "The email is required.\n";
        }
	return error
}

function checkPhone(strng,field)
{
	var error = "";
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) 
	{
	   error = "The "+field+" contains illegal characters.\n";
	}
        if (stripped.length<10)
        {
           error = "The phone needs to be at least 10 digits long.\n";
        }
	return error
}


function checkFax(strng,field)
{
        var error = "";
        var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
        //strip out acceptable non-numeric characters
        if (isNaN(parseInt(stripped)) && stripped.length>0)
        {
           error = "The "+field+" contains illegal characters.\n";
        }
        return error
}


function checkDropdown(choice,field) {
    var error = "";
    if ((choice == 0 && field == "organization type")  || (choice == -1 && field == "research interest")) {
       error = "You didn't choose an option from the " + field + " list.\n";
    }    
return error;
}

function check_position()
{
     s = document.getElementById('member_position_id')
     if (s.options[s.options.length-1].selected)
        document.getElementById('member_position_other').disabled = false
    else
        document.getElementById('member_position_other').disabled = true

}

function check_country()
{
     s = document.getElementById('member_country_id')
     if (s.options[s.options.length-1].selected)
        document.getElementById('member_country_other').disabled = false
    else
        document.getElementById('member_country_other').disabled = true

}

function check_domain()
{
     s = document.getElementById('member_domain_id')
     if (s.options[s.options.length-1].selected)
        document.getElementById('member_domain_other').disabled = false
    else
        document.getElementById('member_domain_other').disabled = true

}


