var id = 0;
var foto = 0;
var status = 0;

function checkFormReg()
{
  val_name = trim(byID('id-name').value);
  if ((val_name == '') || val_name == def_name_val) { alert('Вы не указали ваше имя!'); return false; }

  val_email = byID('id-email').value;
  var tmp = val_email.match(/^[0-9a-z._-]+@[0-9a-z._-]+[.][0-9a-z]{2,7}$/i);
  if (tmp == null) { alert('Укажите реальный и действующий e-mail адрес!'); return false; }

  val_passwd1 = byID('id-passwd').value;
  if (val_passwd1 == def_pass_val) { alert('Вы не указали ваш пароль.'); return false; }
  if (val_passwd1.length < 6) { alert('Пароль должен состоять минимум из 6 символов.'); return false; }

  var regCountry = byID('id-country-2').selectedIndex;
  if (regCountry == 0) { alert('Вы не указали страну.'); return false; }

  var regRegion = byID('id-region-2').selectedIndex;
  if (regRegion == 0) { alert('Вы не указали регион.'); return false; }

  var regCity = byID('id-city-2').selectedIndex;
  if (regCity == 0) { alert('Вы не указали город.'); return false; }

  if (gender == 'woman')
  {
    //val_name = trim(byID('id-phone').value);
    //if (val_name.length == 0) { alert('Вы не указали ваш телефон!'); return false; }

    if (trim(byID('aboutme_id').value) == '') { alert('Нужно написать что нибудь о себе.'); return false; }
    if (trim(byID('abouthe_id').value) == '') { alert('Нужно написать что о том кого хотите найти.'); return false; }

    addtoBase();
  }
  else {  regForMan();  }

}

function addtoBase()
{
  u_rid = parseInt(byID('urid_id').value);
  u_sub = parseInt(byID('usub_id').value);
  name = encodeURIComponent(byID('id-name').value);
  mail = encodeURIComponent(byID('id-email').value);
  pass = encodeURIComponent(byID('id-passwd').value);
  phone = encodeURIComponent(byID('id-phone').value);
  aboutme = encodeURIComponent(byID('aboutme_id').value);
  abouthe = encodeURIComponent(byID('abouthe_id').value);

  i = byID('id-country-2').selectedIndex;
  country_id = parseInt(byID('id-country-2').options[i].value);

  i = byID('id-region-2').selectedIndex;
  reg_id = parseInt(byID('id-region-2').options[i].value);

  i = byID('id-city-2').selectedIndex;
  city_id = parseInt(byID('id-city-2').options[i].value);

  i = byID('sizegrud_id').selectedIndex;
  sizegrud = parseInt(byID('sizegrud_id').options[i].value);

  i = byID('eyecolor_id').selectedIndex;
  eyecolor = parseInt(byID('eyecolor_id').options[i].value);

  i = byID('hearcolor_id').selectedIndex;
  hearcolor = parseInt(byID('hearcolor_id').options[i].value);

  i = byID('alkoholstatus_id').selectedIndex;
  alkoholstatus = parseInt(byID('alkoholstatus_id').options[i].value);

  i = byID('smokstatus_id').selectedIndex;
  smokstatus = parseInt(byID('smokstatus_id').options[i].value);
  
  i = byID('reg_birdthyear').selectedIndex;
  yearWomen = byID('reg_birdthyear').options[i].text;
  var d = new Date();
  year_cur = d.getFullYear();
  age = year_cur - yearWomen;

  var stringPost = 'u_rid=' + u_rid + '&u_sub=' + u_sub + '&name='+name+'&mail='+mail+'&pass='+pass+'&phone='+phone+'&country_id='+country_id+'&reg_id='+reg_id+'&city_id='+city_id+'&sizegrud='+sizegrud+'&eyecolor='+eyecolor+'&hearcolor='+hearcolor+'&age='+age+'&aboutme='+aboutme+'&abouthe='+abouthe+'&alkoholstatus='+alkoholstatus+'&smokstatus='+smokstatus;
  url = HTTP_FULL_PATH + '/callback/cb_soderganka_addrecordinbase.php';
  //alert(url);
  ajaxChainLoaderPost(url, stringPost, getResult_callback);
}

function getResult_callback()
{
  if (this.req.responseXML) { nl = this.req.responseXML.getElementsByTagName('retotvet'); }
  if (nl)
  {
    var nli = nl.item(0);
    var res = nli.getAttribute('result');
    id = nli.getAttribute('id');
    //alert(res);
    if (res=='true') { uploadImg(); } else { alert('Такой E-mail уже есть.') }
  }
}


function uploadImg()
{
  hide('registr_photo_block');
  hide('step1');
  show('step2');

  if (getCookie('step') == false) setCookie('step', id, 1);
  byID('user_id').value = id;
  parent.listphoto();
}

function doSubmitForm()
{
  byID('div_stat').innerHTML = 'Загрузка...';
}


function setstaus(id,status)
{
  byID(id).innerHTML = status;
}


function listphoto()
{
  if (byID('div_list'))
  {
    url = HTTP_FULL_PATH + '/callback/cb_soderganka_upload.php?getlistphoto';
    ajaxChainLoaderGet(url, listphoto_callback);
  }
}

function listphoto_callback()
{
  //alert('gdfgdfgdftg');
  var nl = null;
  if (this.req.responseXML) { nl = this.req.responseXML.getElementsByTagName('listphoto'); }
  if (nl)
  {
    var nli = nl.item(0);
    var htmltext = nli.getAttribute('htmltext');
    foto = nli.getAttribute('kolvo_img');
    byID('div_list').innerHTML = htmltext;
    if (byID('div_stat').innerHTML == 'Фото загружено')
    {
      byID('div_stat').innerHTML += '. Вы можете загрузить ещё';
      byID('upfile').value = '';
    }
    kolFoto();
  }
}

function delPhoto(photoID)
{
  byID('div_stat').innerHTML = '';
  url = HTTP_FULL_PATH + '/callback/cb_soderganka_upload.php?delphoto&ph_id='+parseInt(photoID);
  ajaxChainLoaderGet(url, listphoto);
}

function kolFoto()
{
  if (foto > 0)  { show('reg_button');  }
  if (foto == 0) { hide('reg_button');  }
  //if (foto >= 2) { hide('load_button'); byID('div_stat').innerHTML = 'Фото загружено';}
  //if (foto > 0 && foto < 2) { show('load_button'); }
}


function regForMan()
{
  setCookie('gender', 0, 1);
  setCookie('man', 1, 1);
  sessStatus(1);
}

function sessStatus(s)
{

  url = HTTP_FULL_PATH + '/callback/cb_soderganka_upload.php?status='+s;
  ajaxChainLoaderGet(url, sessStatus_callback);
}

function sessStatus_callback()
{
  if (this.req.responseXML) { nl = this.req.responseXML.getElementsByTagName('sess_fake'); }
  if (nl)
  {
    var nli = nl.item(0);
    status = nli.getAttribute('status'); //alert(status);
    /*if(status == '1')*/
    window.location.href = HTTP_FULL_PATH + '/index.php?content=register';
  }
}