
$(document).bind('ready', function() {
  profile_func();
})
function profile_check_() {
  var found = false;
  var el = this;
  if (el && el.name) $(this).removeClass('error');
  $('#profile input[@type=text]').not('.nocheck').each(function() {
    if (el && el.name && $(this).attr('name') == el.name) found = true;
    if (!found) {
      if ((!$(this).attr('value') || ($(this).attr('name') == 'email' && !/^[^@]+@[^@]+$/.test($(this).attr('value'))) || ($(this).attr('name') == 'phone' && !/^[01-9 \+\-\)\(]+$/.test($(this).attr('value')))) && !$(this).attr('disabled')) $(this).addClass('error'); else $(this).removeClass('error');
    }
  })
}
function profile_check__() {
  var found = false;
  var el = this;
  $('#profile input[@type=text]').not('.nocheck').each(function() {
    if (!found) {
      if ((!$(this).attr('value') || ($(this).attr('name') == 'email' && !/^[^@]+@[^@]+$/.test($(this).attr('value'))) || ($(this).attr('name') == 'phone' && !/^[01-9 \+\-\)\(]+$/.test($(this).attr('value')))) && !$(this).attr('disabled')) $(this).addClass('error'); else $(this).removeClass('error');
    }
    if (el && el.name && $(this).attr('name') == el.name) found = true;
  })
}
function profile_check () {
  var st = true;
  $('#profile input[@type=text]').not('.nocheck').each(function() {
    if (!$(this).attr('value') && !$(this).attr('disabled')) st = false;
  })
  if (!$('#profile input[@name=email]').hasClass('nocheck') && !/^[^@]+@[^@]+$/.test($('#profile input[@name=email]').attr('value'))) st = false;
  if (!/^[01-9 \+\-\)\(]+$/.test($('#profile input[@name=phone]').attr('value'))) st = false;
  if (st) {
    $('#profile .buttons a.primary span').removeClass('disabled').parent().removeClass('disabled');
  } else {
    $('#profile .buttons a.primary span').addClass('disabled').parent().addClass('disabled');
  }
}
function profile_func()
{
  if ($('#profile').length == 0) return;
  profile_check();
  $('#profile a.cityname').bind('click', function() {
    if ($('#cities').hasClass('citpos2')) {
      $('#cities').hide().removeClass('citpos2').prev().hide();
    } else {
      $('#cities_wrap').show().next().show().removeClass('citpos3').removeClass('citpos1').addClass('citpos2').find('.flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
    }
    return false;
  });
  $('#profile #phone-data').bind('blur', function() {
    if (/^[01-9 \+\-\)\(]+$/.test($(this).attr('value')) && $(this).attr('value') != $(this).attr('rel') && !$('#signOut').length) {
      $(this).attr('rel', $(this).attr('value'));
      $.ajax({
          url: '/login.php?action=preload2&regid='+$(this).attr('value'),
          type: 'GET',
          dataType: 'json',
          timeout: 2000,
          error: function(){
          },
          success: function(obj){
            if (obj.id) {
              if (!$('#profile #fio-data').attr('value')) $('#profile #fio-data').attr('value', obj.fio);
              if (!$('#profile #email-data').attr('value')) $('#profile #email-data').attr('value', obj.email);
              /*if (!$('#profile #street').attr('value')) {
                $('#profile #street').attr('value', obj.street);
                $('#profile #house').attr('value', obj.house);
                $('#profile #flat').attr('value', obj.office);
              }*/
              $('#profile #addr_place2').html(obj.address);
              addr_place2();
              $('#cities a.q'+obj.id_city).not('.act').trigger('click');
              //clickcity(obj.id_city);
              profile_check();
            }
          }
      });
    }
  });
  $('#profile input[@type=text]').bind('keyup', profile_check).bind('keyup', profile_check_).bind('blur', profile_check__).bind('focus', function() {$(this).removeClass('error')});;
  $('a.profsave').bind('click', function() {
    if (!$(this).is('.disabled')) {
      if (1)  {
        if ($('div.container').length > 1) {
          $('#invis').load(modal_link('/profile/?modal=1&postmodal=1&' + fserialize(document.regform)), function() {
            set_win_title();
            $(this).html('');
            //$('#login a.address2').html($('#profile #phone-data').attr('value'));
            $('#dv_cart a.address').html($('#profile #phone-data').attr('value'));
            page_close();
          });    	
        } else {
          document.regform.submit();
        }
      }
    } else {
      profile_check_();
    }
    return false
  });
  $('#profile #address a.addr').bind('click', function() {
    $(this).parent().parent().addClass('hide').next().removeClass('hide');
    return false
  })
}
