$(document).ready(function(){
	updateForm($(":input[name='newcountry']").val(),'newcountry');
	updateForm($(":input[name='oldcountry']").val(),'oldcountry');
});

function updateForm( selectedCountry, selInputname ){
	var id = 'old';	
	if (selInputname.indexOf('new') > -1)
		id = 'new';
	if ( selectedCountry == "US" ){
		$("#"+ id +"nonUS").hide();
		$("#" + id + "US").show();
	}
	else{
		$("#"+ id +"nonUS").show();
		$("#" + id + "US").hide();
	}
}

function validateCOAForm(){
	var errors = '';
	var msg = '';
	errors+= required($(":input[name='oldemail']"));
	errors+= required($(":input[name='oldemail2']"));
	errors+= required($(":input[name='oldcompany']"));
	errors+= required($(":input[name='oldaddress']"));
	errors+= required($(":input[name='oldcity']"));
	errors+= required($(":input[name='oldcountry']"));
	errors+= required($(":input[name='oldzip']"));
	errors+= required($(":input[name='prefix']"));
	errors+= required($(":input[name='fname']"));
	errors+= required($(":input[name='lname']"));
	errors+= required($(":input[name='newemail']"));
	errors+= required($(":input[name='newemail2']"));
	errors+= required($(":input[name='newcompany']"));
	errors+= required($(":input[name='newaddress']"));
	errors+= required($(":input[name='newcity']"));
	errors+= required($(":input[name='newcountry']"));
	errors+= required($(":input[name='newzip']"));
	
	if ($(":input[name='oldcountry']").val() == 'US')
		errors+= required($(":input[name='oldstate']"));
	else
	  errors+= required($(":input[name='oldprovince']"));
	
	if ($(":input[name='newcountry']").val() == 'US')
		errors+= required($(":input[name='newstate']"));
	else
	  errors+= required($(":input[name='newprovince']"));
	  
	if($(":input[name='oldemail']").val() != $(":input[name='oldemail2']").val())
	{
		$(":input[name='oldemail2']").val('');
		msg += 'Old E-mail and confirm E-mail do not match, please re-enter.\n';
		errors+= required($(":input[name='oldemail2']"));
	}
	
	if($(":input[name='newemail']").val() != $(":input[name='newemail2']").val())
	{
		$(":input[name='newemail2']").val('');
		msg += 'New E-mail and confirm E-mail do not match, please re-enter.\n';
		errors+= required($(":input[name='newemail2']"));
	}
	
	
	if ( errors ){

		alert(msg + "Please fill out all the required fields. \n\n The required fields will be highlighted for you.");

		window.location='#top';

		return false;

	}

	else{
		return true;
	}

}

