// JavaScript Document
$(function(){

	//*****************************************************************************
	$('#countryId').load('form/nudist_form.php?req_type=country');	
	$('#countryId').change( function() {
		$('#regionId, #cityId').empty();
		$('dl.horizontal.city').hide();
		if ($(this).val()!=0){
			$('#regionId').load("form/nudist_form.php?req_type=region&req_val=" + $(this).val(), function() {});
		};
	});
	
	$('#regionId').change( function() {
		$('#cityId').empty();
		if ($(this).val()!=0){
			$('#cityId').load("form/nudist_form.php?req_type=city&req_val=" + $(this).val(), function() {
				if( $('#cityId').val() != null ) {$('dl.horizontal.city').show();};
			});
		};
	});
	
	$('#sexId').load('form/nudist_form.php?req_type=sex');
	$('#sexId').change( function() {
		$('#orientationId').empty();
		if ($(this).val()!=0){
			$('#orientationId').load("form/nudist_form.php?req_type=orientation&req_val=" + $(this).val(), function() {});
		};
	});
	
	$('#seekingSexId').load('form/nudist_form.php?req_type=sex');
	$('#seekingSexId').change( function() {
		$('#seekingOrientationId').empty();
		if ($(this).val()!=0){
			$('#seekingOrientationId').load("form/nudist_form.php?req_type=orientation&req_val=" + $(this).val(), function() {});
		};
	});
	
	$('#criteria').load('form/nudist_form.php?req_type=criteria');
	$('#birthYearPerson1').load('form/nudist_form.php?req_type=year');
	
	$('.Registration form').submit( function(){
		var submit_error=0;
		var alert_msg='';
		
		if ($('#sexId').val()==0){ alert_msg += "Please select 'Gender'\n";
		} else {
			if ($('#orientationId').val()==0){ alert_msg += "Please select 'Orienation'\n";}
		};
				
		if($('#birthDayPerson1').val()==0 || $('#birthMonthPerson1').val()==0 || $('#birthYearPerson1').val()==0 ) { alert_msg += "Please select correct 'Date of Birth'\n"; };
		
		if ($('#seekingSexId').val()==0){ alert_msg += "Please select 'Looking Gender'\n";
		} else {
			if ($('#seekingOrientationId').val()==0){ alert_msg += "Please select 'Looking Orienation'\n";}
		};
		
		$('#email').val().replace(/^\s+|\s+$/g, '');
		if(!$('#email').val().match(/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i)){
			alert_msg += "Incorrect e-mail\n";
		}
		
		if ($('#screenname').val()==''){ alert_msg += "Please fill 'Username' field\n";	};
		
		if ($('#criteria').val()=='0'){ alert_msg += "Please select 'Criteria'\n";	};


		if ($('#countryId').val()=='0'){ alert_msg += "Please select 'Country'\n";
		} else if($('#countryId').val()=='37' || $('#countryId').val()=='203' || $('#countryId').val()=='204')  {
			 if($('#regionId').val()=='0' && $('#postalCode').val()=='') {alert_msg += "Please select 'Region' or fill 'Post Code'\n";}
			 if($('#regionId').val()!='0' && $('#cityId').val()=='0' && $('#postalCode').val()=='') {alert_msg += "Please select 'City' or fill 'Post Code'\n";}
		} else {
			if ($('#regionId').val()=='0'){ alert_msg += "Please select 'Region'\n";
			} else {
				if ($('#cityId').val()=='0'){ alert_msg += "Please select 'City'\n";}
			};
		};

/*
		if ($('#countryId').val()=='0'){ alert_msg += "Please select 'Country'\n";
		} else {
			if($('#countryId').val()!='37' && $('#countryId').val()!='203' && $('#countryId').val()!='204')  {
				if ($('#regionId').val()=='0'){ alert_msg += "Please select 'Region'\n";
				} else {
					if ($('#cityId').val()=='0'){ alert_msg += "Please select 'City'\n";}
				};
			};
		};
*/
		//if (($(this).val()!='37' && $(this).val()!='203' && $(this).val()!='204') || ($('#postalCode').val()=='')&&($(this).val()=='37' && $(this).val()=='203' && $(this).val()=='204')){
		
		
		//if ($('#postalCode').val()==''){ alert_msg += "Please fill 'Zipcode' field\n";};
		
		if (alert_msg==''){
			$.post("form/nudist_form.php", $(this).serializeArray(), function(data){
				var ans=data.split('|');
				if(ans[0]=='SUCCESS'){
					parent.window.location.href = ans[1];
				} else {
					alert(ans[0]+" : "+ans[1]);
				};
			},'text');
		} else {
			alert(alert_msg);
		};
		

		return false;
	});
});
	//*********************************************************************************
