// JavaScript Document
returnVal=false;

function checkEmailOccupy(){
	$("#emailChecker").show();

	new Ajax.Request('/index.php?c=json&m=check_EmailOccupy&getMail='+encodeURIComponent($("#email").val()), {
		method:'get',
		onSuccess: function(transport){
			if(parseInt(transport.responseText) == 1){
				$("#emailError").show();
				returnVal = true;
				return returnVal;
			}else{
				$("#emailError").hide();
				returnVal = false;
				return returnVal;
			}
		},
		onComplete: function(){
			$("#emailChecker").hide();
		}
	});
}



function country_onSelect(elm,backfunction){
	if(elm.options[elm.selectedIndex].value==0) {$('select#region option').remove();$('tr#regiontr').hide();$('tr#citytr').hide();$('select#city option').remove();return;}
	$.ajax({
		url: '/json/regions/'+elm.options[elm.selectedIndex].value,
		type: 'GET',
		dataType: 'json',
		timeout: 1000,
		error: function(){
			alert('Error loading XML document');
		},
		success: function(json){
				$('select#region option').remove();
				$('select#city option').remove();
				$('tr#regiontr').show();
				$('tr#citytr').hide();

					$('select#region').append('<option value="0">Выберете регион</option>');
					for(itm=0; itm < json.length;itm++){
						$('select#region').append('<option value="'+json[itm].region_id+'">'+json[itm].region_name+'</option>');
					}

			if(typeof(backfunction)=="function"){
				backfunction();
			}
		}
	});
}



function region_onSelect(elm,backfunction){
	if(elm.options[elm.selectedIndex].value==0) {$('tr#citytr').hide();$('select#city option').remove();return;}

	$.ajax({
		url: '/json/cities/'+elm.options[elm.selectedIndex].value,
		type: 'GET',
		dataType: 'json',
		timeout: 1000,
		error: function(){
			alert('Error loading XML document');
		},
		success: function(json){
				$('select#city option').remove();
				$('tr#citytr').show();

					$('select#city').append('<option value="0">Выберете город</option>');
					for(itm=0; itm < json.length;itm++){
						$('select#city').append('<option value="'+json[itm].city_id+'">'+json[itm].city_name+'</option>');
					}
			if(typeof(backfunction)=="function"){
				backfunction();
			}
		}
	});

}


function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}


function checktoNextstep(){
	if($('#country').val()==0){ alert('Выберете страну!'); return;}
	if($('#region').val()==0){ alert('Выберете регион, затем город!'); return;}
	if($('#city').val()==0){ alert('Выберете город!'); return;}

		$checkedBoxes = 0;
		for (i=0; i<document.registrationForm.elements['target[]'].length; i++){
			if (document.registrationForm.elements['target[]'][i].checked==true) $checkedBoxes++;
		}
		if($checkedBoxes==0){alert('Выберете цели которые вы здесь приследуете!');return;}
		
	$('#reg_step1').slideUp(1000);$('#reg_step2').slideDown(1000);
}


function testingAndSubmit_m(){
		if(!isValidEmailAddress($('#email').val())){ alert('Неверный e-mail!'); return false;}
		if($('#password').val()!=$('#password_retype').val()){ alert('Пароли не совпадают!'); return false;}
		
		return true;
}

function testingAndSubmit(){

	if($('#country').val()==0){ alert('Выберете страну!'); return false;}
	if($('#region').val()==0){ alert('Выберете регион, затем город!'); return false;}
	if($('#city').val()==0){ alert('Выберете город!'); return false;}

		$checkedBoxes = 0;
		for (i=0; i<document.registrationForm.elements['target[]'].length; i++){
			if (document.registrationForm.elements['target[]'][i].checked==true) $checkedBoxes++;
		}
		if($checkedBoxes==0){alert('Выберете цели которые вы здесь приследуете!');return false;}

		if(!isValidEmailAddress($('#email').val())){ alert('Неверный e-mail!'); return false;}
		
		return true;
}
