country_has_zips = []; // new Array(-1,616,5681);
var months = {
    1 : 'января',
    2 : 'февраля',
    3 : 'марта',
    4 : 'апреля',
    5 : 'мая',
    6 : 'июня',
    7 : 'июля',
    8 : 'августа',
    9 : 'сентября',
    10: 'октября',
    11: 'ноября',
    12: 'декабря'
}

function wopen(url,name,w,h,r,s,st)
{
    var w=window.open(url,name,"width="+w+",height="+h+",resizable="+r+",toolbar=0,location=0,status="+st+",menubar=0,directories=0,scrollbars="+s);
}

function changeCity() 
{
    sindex=document.getElementById('location').selectedIndex;
    if( document.getElementById('location')[sindex].value=="1_1_1" )
    {
        //wopen('edit_city_reg.phtml?country_id=&region_id=&city_id=&city_element=location','EditCity',850,600,'yes','yes',0);
        document.getElementById('location')[0].selected=true;
    }
    
    var country_id = document.getElementById('location')[sindex].value.split('_')[0];
    var has_zips = false;
    for(i=0;i<country_has_zips.length;i++)
    {
        if(country_id == country_has_zips[i])
        {
            has_zips = true; break;
        }
    }
}

function addSearchCity(velement,vvalue,vtext)
{
    var fcity=document.getElementById('location');
    var flength=fcity.length;
    var fcurrent=flength-1;
    fcity[fcurrent].selected=true;
    fcity.options[fcurrent].text=vtext;
    fcity.options[fcurrent].value=vvalue;
    fcity.options[flength]=new Option("Выбрать другой город...","1_1_1");
}

function ShowError(type, is_show)
{
    switch(type)
    {
        case 'name':
            if(is_show) {
            	$('#name').parent('td').addClass('email_error');
            } else {
            	$('#name').parent('td').removeClass('email_error');
            }
            break;
        
        case 'gender':
            if(is_show) $('#gender_msg').show();
             else $('#gender_msg').hide();
            break;
        
        case 'birthdate':
            if(is_show)
            {
                $('#birth_day'  ).addClass('error');
                $('#birth_month').addClass('error');
                $('#birth_year' ).addClass('error');
            }
            else
            {
                $('#birth_day'  ).removeClass('error');
                $('#birth_month').removeClass('error');
                $('#birth_year' ).removeClass('error');
            }
        break;
        
        case 'location':
            if(is_show)  $('#location').addClass('error');
             else  $('#location').removeClass('error');
            break;
        
        case 'email':
        case 'email_exists':
            $('#email_exists').hide();
            $('#email_incorrect').hide();
            if(is_show)
            {
                $('#email_msg').hide();
                $('#email').parent('td').addClass('email_error');
                
                if( type == 'email' ) $('#email_incorrect').show();
                 else $('#email_exists').show();
            }
            else
            {
                $('#email').parent('td').removeClass('email_error');
                $('#email_msg').show();
            }
        break;
        
        case 'password':
            if(is_show) $('#password').parent('td').addClass('email_error');
             else $('#password').parent('td').removeClass('email_error');
            break;
    }
}

function CheckUser()
{
    var is_ok = true;
    // name
    if( $('#name').val().length < 2 )
    {
        is_ok = false;
        ShowError('name', true);
    }
    else ShowError('name', false);
    
    // gender
    if( $('[name=gender]:checked').length == 0 )
    {
        is_ok = false;
        ShowError('gender', true);
    }
    else ShowError('gender', false);
    
    // birthday
    var day	  = $('#birth_day').val();
    var month = parseInt($('#birth_month').val()) - 1;
    var year  = $('#birth_year').val();
                   
    var cDate = new Date(year, month, day);
    
    if(
        isNaN(month) || month < 0   || 
        cDate.getDate() != day      || 
        cDate.getMonth() != month   || 
        cDate.getFullYear() != year
    )
   {
        is_ok = false;
        ShowError('birthdate', true);
    }
    else
    {
        ShowError('birthdate', false);
    }
    
    // city
    if( $('#location').val() == '0_0_0')
    {
        is_ok = false;
        ShowError('location', true);
    }
    else ShowError('location', false);
    
    var re_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var email_str = jQuery.trim( $('#email').val() );
    
    if( !re_email.test(email_str) )
    {
        is_ok = false;
        ShowError('email', true);
    }
    else
    {
        ShowError('email', false);
    }

    // password
    var password_str = jQuery.trim( $('#password').val() );
    if (password_str.length < 6)
    {
        is_ok = false;
        ShowError('password', true);
    }
    else ShowError('password', false);
    
    
    if( $('#disclaimer:checked').length == 0 )
    {
        is_ok = false;
        alert(LANG['LICENSE_ACCEPT']);
    }
    
    if(is_ok)
    {
    
        $('#check_name').val( $('#name').val() );
        $('#check_birth_day').val( $('#birth_day').val() );
        $('#check_birth_month').val( $('#birth_month').val() );
        $('#check_birth_year').val( $('#birth_year').val() );
        
        GoToStage(2);
    }
}

function AddUser()
{
    AmourAjax(
        'ajax/register.phtml',
        {
            'reg_arr[name]'        : $('#check_name').val(),
            'reg_arr[gender]'      : $('[name=gender]:checked').val(),
            'reg_arr[birth_day]'   : $('#check_birth_day').val(),
            'reg_arr[birth_month]' : $('#check_birth_month').val(),
            'reg_arr[birth_year]'  : $('#check_birth_year').val(),
            'reg_arr[location]'    : $('#location').val(),
            'reg_arr[email]'       : $('#email').val(),
            'reg_arr[password]'    : $('#password').val(),
            'reg_arr[promocode]'   : $('#promocode').val(),
            'reg_arr[orientation]' : $('#orientation').val()
        },
        function (type, msg)
        {
            //alert(type+'!'+msg);
            if (type == 'ER')
            {
                GoToStage(1);
                var errors = msg.split(',');
                for(var i in errors)
                {
                    ShowError(errors[i], true);
                }
            }
        }
    );
}

function GoToStage(s/*Int*/)/*Void*/
{
    $('#step1').css('display', s == 1 ? '' : 'none');
    $('#step2').css('display', s == 1 ? 'none' : '');
}

var locationSelectIsReady = false;

$(document).ready(function(){

    var lbOffset = 57;
    var lbBorders = 120;
    var viewMax = 470;
    var viewMin = 210;

    function lbResize(height) {
        $('.LS-Dialog').css('margin-top', -(height/2 + lbOffset) );
        $('.LS-Dialog .slide, .LS-Dialog .crop, .LS-Dialog .slide div, .LS-Dialog .slide select').height(height);
    }

    if (Window.getHeight() < viewMax) {
        lbResize(Math.max(Window.getHeight(), viewMin) - lbBorders);
    }

    $('#step1').show();
    $('#name').showlength({
    	maxLength	: 50,
    	element		: $('#name').next(),
    	filter		: /^[a-zA-Zа-яА-Яё ]*$/i
    });
    $('#check_name').showlength({
    	maxLength	: 50,
    	element		: $('#check_name').next(),
    	filter		: /^[a-zA-Zа-яА-Яё ]*$/i
    });    
    $('[name=gender]').click(function() {
        var gender = $(this).val() == 'female' ? 'female' : 'male';
        $('.m_find_w').hide();
        $(this).parents('label').find('.m_find_w').show();
    });
    
    $('#name, #email, #password').keyup(function () {
    	ShowError(this.id, false);
    });
    $('[name=gender]').click(function () {
    	ShowError('gender', false);
    });
	
	
    $('#location').change(function () {		
    	if (locationSelectIsReady) {
			ShowError('location' , $(this).val() == '0_0_0' );
		}
		locationSelectIsReady = true;
    });
  
    $('select[id^=birth]').change(function () {    	
    	var day	  = $('#birth_day').val();
    	var month = parseInt($('#birth_month').val()) - 1;
    	var year  = $('#birth_year').val();
    	
    	if (
    		!$(this).hasClass('error') &&
    		!( day && month >= 0 && year )
    	)
    	{
    		return;
    	}

    	
    	var cDate = new Date(year, month, day);
    	if(
    		isNaN(month) || month < 0   || 
    		cDate.getDate() != day      || 
    		cDate.getMonth() != month   || 
    		cDate.getFullYear() != year
    	) {
    		var valid = false;
    	}
    	else
    	{
    		var valid = true;
    	}
    	ShowError('birthdate', !valid);
    });
    
    var valid_second_step = function () 
    {
    	var day	  = $('#check_birth_day').val();
    	var month = parseInt($('#check_birth_month').val()) - 1;
    	var year  = $('#check_birth_year').val();
    	var cDate = new Date(year, month, day);
    	
    	return (
    		( $('#check_name').val().length > 2 ) &&
    		( !isNaN(month) && month >= 0 	&&
    		  cDate.getDate() == day  		&& 
    		  cDate.getMonth() == month		&&
    		  cDate.getFullYear() == year
    		  )
    	);    	
    }
    
    $('#check_name').keyup(function () {
    	this.value = jQuery.trim(this.value);
    	$('#confirm-user').get(0).disabled = !valid_second_step();
    });
    $('select[id^=check_birth]').change(function () {
    	$('#confirm-user').get(0).disabled = !valid_second_step();
    });    
});
