var form_colors_ar = new Array;
form_colors_ar['ok'] = '#ddffdd';
form_colors_ar['empty'] = '#ffffdd';
form_colors_ar['warning'] = '#ffdddd';
form_colors_ar['error'] = '#ffdddd';
form_colors_ar['bg_ok'] = '#ffffff';
form_colors_ar['bg_error'] = '#ffdddd';

function diForm(instance_name, form_name)
{
  this.colors = form_colors_ar;
  this.titles = new Array;
  this.use_titles = false;
  this.inputs = new Array;
  this.hints = new Array;
  this.containers = new Array;
  this.holds = new Array;
  this.error_flags = new Array;
  this.handlers = new Array;
  this.properties = new Array;
  this.submit_obj = {id: false, error_msg: ''};
  this.hold_hints = false;
  this.instance_name = instance_name;
  this.form_name = form_name;

  // ['none','display','visibility']
  // if none - only message is put inside error container
  // if display - the style.display is set to 'block'/'none'
  // if visibility - the style.visibility is set to 'visible'/'hidden'
  this.error_display_mode = 'none';

  if (typeof this.form_name == 'string')
  {
    var form = _ge(this.form_name);

    if (!form)
      form = eval('document.forms["'+this.form_name+'"]');

    this.form = form;
  }
  else if (typeof this.form_name == 'object')
  {
    this.form = this.form_name;
    this.form_name = this.form.name;
  }
  else if (typeof this.form_name == 'undefined')
    this.form = null;

  eval('_add_event(this.form, "submit", function(){return '+this.instance_name+'.onSubmit();});');
}

diForm.prototype.colors;
diForm.prototype.titles;
diForm.prototype.inputs;
diForm.prototype.hints;
diForm.prototype.containers;
diForm.prototype.holds;
diForm.prototype.error_flags;
diForm.prototype.handlers;
diForm.prototype.properties;
diForm.prototype.submit_element;
diForm.prototype.hold_hints;
diForm.prototype.instance_name;
diForm.prototype.form_name;
diForm.prototype.form;
diForm.prototype.error_display_mode;

diForm.prototype.set_use_titles = function(status)
{
  this.use_titles = status;
}

diForm.prototype.set_error_display_mode = function(mode)
{
  this.error_display_mode = mode;
}

// set color
diForm.prototype.setColor = function(status, color)
{
  this.colors[status] = color;
}

// set input
diForm.prototype.setInput = function(id, handler, hold)
{
  this.inputs[id] = _ge(id);
  this.hints[id] = _ge(id+'_hint');
  this.handlers[id] = handler;
  this.holds[id] = hold;
  this.error_flags[id] = 0;
  this.properties[id] = {min_len: 0, max_len: 0, check_on_submit: true, necessary: false};
}

diForm.prototype.setInputsAr = function(ar)
{
  var i, r, id;

  for (var i = 0; i < ar.length; i++)
  {
    r = ar[i];
    id = typeof r.id != 'undefined' ? r.id : r.name;

    this.inputs[id] = this.form ? eval('this.form.'+r.name) : _ge(id);

    this.hints[id] = _ge(id+'_hint');
    this.handlers[id] = r.handler;
    this.holds[id] = typeof r.hold != 'undefined' ? r.hold : false;
    this.error_flags[id] = 0;
    this.properties[id] = {min_len: 0, max_len: 0, check_on_submit: true, necessary: false};
    this.titles[id] = typeof r.title != 'undefined' ? r.title : false;

    if (typeof r.properties != 'undefined')
    {
      for (var j in r.properties)
      {
        eval('this.properties[id].'+j+'=r.properties[j];')
      }
    }

    if (this.inputs[id])
    {
      var blur_f = 'function() {'+
      this.instance_name+'.check("'+id+'");'+
      (this.use_titles && r.title!==false?'if (!'+this.instance_name+'.inputs["'+id+'"].value)'+this.instance_name+'.inputs["'+id+'"].value = "'+r.title+'";':'')+
      '}';

      var focus_f = 'function() {'+
      (this.use_titles && r.title!==false?'if ('+this.instance_name+'.inputs["'+id+'"].value == "'+r.title+'") '+this.instance_name+'.inputs["'+id+'"].value = "";':'')+
      '}';

      var main_f = 'function() {'+
      this.instance_name+'.check("'+id+'");'+
      '}';

      eval('_add_event('+this.instance_name+'.inputs[id], "blur", '+blur_f+');');
      eval('_add_event('+this.instance_name+'.inputs[id], "focus", '+focus_f+');');
      eval('_add_event('+this.instance_name+'.inputs[id], "click", '+main_f+');');
      eval('_add_event('+this.instance_name+'.inputs[id], "keyup", '+main_f+');');

      if (this.use_titles && this.titles[id] !== false && !this.inputs[id].value)
        this.inputs[id].value = this.titles[id];
    }
  }
}

diForm.prototype.setInputProperty = function(id, key, value)
{
  eval('this.properties[id].'+key+' = value;');
}

diForm.prototype.setSubmit = function(id, error_msg)
{
  this.submit_obj.id = id;
  this.submit_obj.error_msg = error_msg;
  this.inputs[id] = _ge(id+'_btn');
  this.hints[id] = _ge(id+'_hint');
  this.containers[id] = _ge(id+'_container');
}

diForm.prototype.holdHints = function(to_hold)
{
  this.hold_hints = to_hold;
}

diForm.prototype.set_hint_display = function(id, status)
{
  if (this.hints[id])
  {
    switch (this.error_display_mode)
    {
      case 'display':
        this.hints[id].style.display = status ? 'block' : 'none';
        break;

      case 'visibility':
        this.hints[id].style.visibility = status ? 'visible' : 'hidden';
        break;

      case 'none':
      default:
        break;
    }
  }
}

// raise error
diForm.prototype.showError = function(id, hint, color_idx)
{
  if (this.inputs[id]) this.inputs[id].style.backgroundColor = this.colors[color_idx];
  if (this.hints[id]) this.hints[id].innerHTML = hint;
  else if (id.substr(0,4) == 'dob_') this.hints['dob'].innerHTML = hint;

  this.set_hint_display(id, true);

  this.error_flags[id] = 1;
}

// hide error
diForm.prototype.hideError = function(id)
{
  var hint = diForm.prototype.hideError.arguments.length >= 2 ? diForm.prototype.hideError.arguments[1] : '';
  var color_idx = diForm.prototype.hideError.arguments.length >= 3 ? diForm.prototype.hideError.arguments[2] : 'ok';

  if (this.inputs[id]) this.inputs[id].style.backgroundColor = this.colors[color_idx];
  if (this.hints[id]) this.hints[id].innerHTML = hint;
  else if (id.substr(0,4) == 'dob_') this.hints['dob'].innerHTML = hint;

  this.set_hint_display(id, false);

  this.error_flags[id] = 0;
}

// run checker by handler
diForm.prototype.check = function(id)
{
  if (this.hold_hints && this.holds[id]) return false;

  if (typeof this.handlers[id] != 'undefined')
    this.handlers[id](this, id);

  if (this.submit_obj.id && this.hints[this.submit_obj.id] && id != 'tags')
  {
    this.hints[this.submit_obj.id].innerHTML = '';
    //!!!//
    if (this.containers[this.submit_obj.id]) this.containers[this.submit_obj.id].style.backgroundColor = this.colors['bg_ok'];
  }
}

diForm.prototype.clear = function()
{
  for (var i in this.inputs)
  {
    if (i == this.submit_obj.id) continue;

    this.hideError(i, '', 'empty');
  }

  if (this.submit_obj.id && this.hints[this.submit_obj.id])
  {
    this.hints[this.submit_obj.id].innerHTML = '';
    //!!!//
    if (this.containers[this.submit_obj.id]) this.containers[this.submit_obj.id].style.backgroundColor = this.colors['bg_ok'];
  }
}

// did any errors happen
diForm.prototype.errorsHappened = function()
{
  var r = false;

  for (var i in this.error_flags)
  {
    eval('var t = typeof Array.'+i);
    if (t == 'object' || t == 'function') continue;

    if (this.error_flags[i])
    {
      r = true;
      break;
    }
  }

  return r;
}

// on submit
diForm.prototype.onSubmit = function()
{
  var r = true;

  for (var i in this.error_flags)
  {
    eval('var t = typeof Array.'+i);
    if (t == 'object' || t == 'function') continue;

    if (this.properties[i] && this.properties[i].check_on_submit)
      this.check(i);

    if (this.error_flags[i])
    {
      //alert(t+' '+i+' '+this.properties[i].necessary);
      r = false;
    }
  }

  this.toggleSubmitContainer(r);

  return r;
}

diForm.prototype.toggleSubmitContainer = function(r)
{
  if (this.submit_obj.id && this.hints[this.submit_obj.id])
  {
    if (r)
    {
      this.hints[this.submit_obj.id].innerHTML = '';
      //!!!//
      if (this.containers[this.submit_obj.id])
        this.containers[this.submit_obj.id].style.backgroundColor = this.colors['bg_ok'];
    }
    else
    {
      this.hints[this.submit_obj.id].innerHTML = this.submit_obj.error_msg;
      //!!!//
      if (this.containers[this.submit_obj.id])
        this.containers[this.submit_obj.id].style.backgroundColor = this.colors['bg_error'];
    }
  }

  /*
  if (r && false)
  {
    this.inputs[this.submit_obj.id].disabled = true;
  }
  */
}

diForm.prototype.lockSubmit = function(flag)
{
  if (typeof flag == 'undefined') flag = true;

  this.inputs[this.submit_obj.id].disabled = flag ? true : false;
}

// --[ standard handle functions ]---------------------------------------------------------------

// form - diForm object
// id - id of the input to get checked
function __check_login(form, id)
{
  var e = form.inputs[id];
  var p = form.properties[id];

  if (e.value.length && !check_correct_latin_symbols(e.value))
    form.showError(id, 'You can use only latin letters, digits and symbols: &quot;_&quot;, &quot;-&quot;, &quot;.&quot;', 'error');
  else if (e.value.length < 3)
    form.showError(id, 'Min length - 3 symbols', 'warning');
  else if (e.value.length > 15)
    form.showError(id, 'Max length - 15 symbols', 'warning');
  else
    form.hideError(id);
}

function __check_password(form, id)
{
  if (id.substr(id.length - 1) == '2')
  {
    var id2 = id;
    id = id.substr(0, id.length - 1);
  }
  else
  {
    var id2 = id+'2';
  }

  var e = form.inputs[id];
  var e2 = form.inputs[id2];

  if (e.value.length < 6)
    form.showError(id, 'Min length - 6 symbols', 'warning');
  else
  {
    form.hideError(id, '', 'empty');

    if (e.value != e2.value && e.value.substr(0, e2.value.length) == e2.value)
      form.hideError(id2, '', 'warning');
    else if (!e2.value.length)
      form.hideError(id2, '', 'empty');
    else if (e.value.length && e2.value.length && e.value == e2.value)
    {
      form.hideError(id, '', 'ok');
      form.hideError(id2, '', 'ok');
    }
    else //if (e.value.length && e2.value.length && e.value != e2.value)
      form.showError(id2, 'Entered passwords not match', 'error');
  }
}

function __check_string(form, id)
{
  var e = form.inputs[id];
  var p = form.properties[id];

  if (p.min_len && e.value.length < p.min_len)
    form.showError(id, 'Min length - '+p.min_len+' symbol(s)', 'warning');
  else if (p.necessary && !p.min_len && (e.value.length == 0 || (form.use_titles && e.value == form.titles[id])))
    form.showError(id, 'Please enter your '+form.titles[id], 'warning');
  else if (p.max_len && e.value.length > p.max_len && (!form.use_titles || e.value != form.titles[id]))
    form.showError(id, 'Max length - '+p.max_len+' symbol(s)', 'error');
  else
    form.hideError(id);
}

function __check_number(form, id)
{
  var e = form.inputs[id];
  var p = form.properties[id];

  if (e.value.length && !check_correct_digits(e.value))
    form.showError(id, 'Please enter digit value', 'error');
  else if (p.necessary && e.value.length == 0)
    form.showError(id, 'Please enter your ', 'warning');
  else if (p.max_len && e.value.length > p.max_len)
    form.showError(id, 'Max length - '+p.max_len+' symbols', 'error');
  else
    form.hideError(id);
}

function __check_email(form, id)
{
  var id2 = id.substr(id.length - 1) == '2' ? id.substr(0, id.length - 1) : id+'2';
  var e = form.inputs[id];
  var e2 = form.inputs[id2];
  var p = form.properties[id];

  if (p.necessary && e.value.length == 0)
    form.showError(id, 'Please fill in', 'warning');
  else if (e.value.length > 30)
    form.showError(id, 'Max length - 30 symbols', 'error');
  else if (!check_correct_email(e.value))
    form.showError(id, 'Please enter correct E-mail', 'warning');
  else
  {
    form.hideError(id);

    if (e2)
    {
      if (e.value != e2.value && e.value.substr(0, e2.value.length) == e2.value)
        form.hideError(id2, '', 'warning');
      else if (!e2.value.length)
        form.hideError(id2, '', 'empty');
      else if (e.value.length && e2.value.length && e.value == e2.value)
      {
        form.hideError(id, '', 'ok');
        form.hideError(id2, '', 'ok');
      }
      else //if (e.value.length && e2.value.length && e.value != e2.value)
        form.showError(id2, 'Entered E-mails not match', 'error');
    }
  }
}

function __check_customers_email(form, id)
{
  var e = form.inputs[id];
  var p = form.properties[id];

  if (p.necessary && e.value.length == 0)
    form.showError(id, 'Please enter your ', 'warning');
  else if (e.value.length > 30)
    form.showError(id, 'Max length - 30 symbols', 'error');
  else if (!check_correct_email(e.value))
    form.showError(id, 'Please enter correct E-mail', 'warning');
  else
  {
    form.hideError(id);

    form.customer.email = e.value;
    customer_lookup(form);
  }
}

function customer_lookup(form)
{
  var xml = 'xml/customer_lookup.php';
  var query = 'email='+escape(form.customer.email)+'&password='+escape(form.customer.password)+
    '&instance_name='+escape(form.instance_name);

  diXMLRequest.post(xml, query, customer_lookup_response);

  return false;
}

function customer_lookup_response(xmlDoc)
{
  show_loader(false);

  try { var rs = xmlDoc.documentElement.getElementsByTagName('result'); } catch(e) {}
  if (!rs) return false;

  var r = rs[0];
  var email_ok = r.getAttribute('email_ok')*1;
  var password_ok = r.getAttribute('password_ok')*1;
  var instance_name = r.getAttribute('instance_name');
  var form = false;

  if (instance_name && __correct_latin_symbols_regexp.test(instance_name))
  {
    eval('form = '+instance_name+';');
  }

  var tr = _ge('password_tr');
  var tr_visibility = 'hidden';

  var password_enter = _ge('password_enter');
  var password_matched = _ge('password_matched');

  if (form)
  {
    if (email_ok)
    {
      tr_visibility = 'visible';
    }

    if (password_ok)
    {
      password_enter.style.display = 'none';
      password_matched.style.display = 'block';
    }
    else
    {
      password_enter.style.display = 'block';
      password_matched.style.display = 'none';
    }
  }

  tr.style.visibility = tr_visibility;

  form.properties['password'].necessary = tr_visibility == 'visible' ? true : false;
  form.check('password');

  // customer's data
  try { var c_rs = xmlDoc.documentElement.getElementsByTagName('customer'); } catch(e) {}
  if (c_rs)
  {
    var c_r = c_rs[0];

    if (c_r)
    {
      for (var _id in form.inputs)
      {
        var _value = c_r.getAttribute(_id);

        if (
            (!form.inputs[_id].value || form.inputs[_id].value == form.titles[_id]) &&
            _value &&
            _value != form.titles[_id]
           )
          form.inputs[_id].value = _value;
      }
    }
  }
  //

  return false;
}

function booking_form_try_to_login()
{
  show_loader(true);

  customer_lookup(b_form);
}

function show_loader(status)
{
  var e = _ge('booking-loader');

  if (e)
    e.style.display = status ? 'block' : 'none';
}

function __check_customers_password(form, id)
{
  var e = form.inputs[id];
  var p = form.properties[id];

  if (p.necessary && e.value.length == 0)
    form.showError(id, 'Please fill in Password', 'warning');
  else if (p.necessary && e.value.length < 6)
    form.showError(id, 'Min length - 6 symbols', 'warning');
  else
  {
    form.hideError(id);

    form.customer.password = hex_md5(e.value);
    //customer_lookup(form);
  }
}

function __check_url(form, id)
{
  var e = form.inputs[id];
  var p = form.properties[id];

  if (p.necessary && e.value.length == 0)
    form.showError(id, 'Please fill in', 'warning');
  else if (p.max_len && e.value.length > p.max_len)
    form.showError(id, 'Max length - '+p.max_len+' symbols', 'error');
  else if (e.value != "http://" && e.value != "" && !/^(http[s]?:\/\/)?(www\.)?[0-9a-zA-Z]([-._]?[0-9a-zA-Z])*[.]{1}[a-zA-Z]{2,4}(\/.*)?$/.test(e.value))
    form.showError(id, 'Please enter correct URL', 'warning');
  else
    form.hideError(id);
}

function __check_random_code(form, id)
{
  var e = form.inputs[id];
  var hash = _ge(id+'_hash');
  var cur_value = hex_md5(e.value);

  if (e.value.length != 4)
    form.showError(id, 'Code length is 4 symbols', 'warning');
  else if (hash && hash.value != cur_value)
    form.showError(id, 'Enter correct code', 'error');
  else
    form.hideError(id);
}

var hobby_err_s = 'Choose at least one checkbox';

function __check_tags(form, id)
{
  var f = _ge('registration_form');
  var at_least_1 = false;

  for (var i = 0; i < f.elements.length; i++)
  {
    var e = f.elements[i];

    if (e.name && e.name.substr(0,6) == 'tags[]')
    {
      if (e.checked)
      {
        at_least_1 = true;
        break;
      }
    }
  }

  if (!at_least_1)
  {
    form.showError(id, hobby_err_s, 'error');

    //_ge('tags_table_td').style.backgroundColor = '#f66';
    //_ge('tags_table__').style.backgroundColor = '#f66';

    form.hints[form.submit_obj.id].innerHTML = form.submit_obj.error_msg;
    if (form.containers[form.submit_obj.id]) form.containers[form.submit_obj.id].style.backgroundColor = form.colors['bg_error'];
    form.inputs[form.submit_obj.id].disabled = true;
  }
  else
  {
    form.hideError(id, '', 'empty');

    //_ge('tags_table_td').style.backgroundColor = '#FAD78A';
    //_ge('tags_table__').style.backgroundColor = '#FAD78A';

    form.hints[form.submit_obj.id].innerHTML = '';
    if (form.containers[form.submit_obj.id]) form.containers[form.submit_obj.id].style.backgroundColor = form.colors['bg_ok'];
    form.inputs[form.submit_obj.id].disabled = false;
  }
}

function __check_tags2(form, id)
{
  var f = _ge('setup_tags_form');
  var at_least_1 = false;

  for (var i = 0; i < f.elements.length; i++)
  {
    var e = f.elements[i];

    if (e.name.substr(0,6) == 'tags[]')
    {
      if (e.checked)
      {
        at_least_1 = true;
        break;
      }
    }
  }

  if (!at_least_1)
  {
    form.showError(id, hobby_err_s, 'error');

    form.hints[form.submit_obj.id].innerHTML = form.submit_obj.error_msg;
    if (form.containers[form.submit_obj.id]) form.containers[form.submit_obj.id].style.backgroundColor = form.colors['bg_error'];
    form.inputs[form.submit_obj.id].disabled = true;
  }
  else
  {
    form.hideError(id, '', 'empty');

    form.hints[form.submit_obj.id].innerHTML = '';
    if (form.containers[form.submit_obj.id]) form.containers[form.submit_obj.id].style.backgroundColor = form.colors['bg_ok'];
    form.inputs[form.submit_obj.id].disabled = false;
  }
}

function __check_select(form, id)
{
  var e = form.inputs[id];
  var p = form.properties[id];

  var msg = id == 'sex' ? 'Выберите пол' : 'Please fill in';

  if (p.necessary && !e.value)
    form.showError(id, msg, 'warning');
  else
    form.hideError(id);
}

function __check_dob_select(form, id)
{
  __check_dob_selects(form, 'dob');
  return;

  //var id2 = (id && id.substr(0,4) == 'dob_') ? 'dob' : id;
}

function __check_dob_select2(form, id)
{
  var e = form.inputs[id];
  var p = form.properties[id];

  //alert(p.necessary+' '+e.value);

  if (p.necessary && !e.value)
  {
    form.showError(id, 'Please enter correct date of birth', 'warning');

    return false;
  }
  else
  {
    form.hideError(id);

    return true;
  }
}

function __check_dob_selects(form, id)
{
  if (id == 'dob')
  {
    var r1 = __check_dob_select2(form, 'dob_d');
    var r2 = __check_dob_select2(form, 'dob_m');
    var r3 = __check_dob_select2(form, 'dob_y');

    if (!r1 || !r2 || !r3)
      form.showError(id, 'Please enter date of birth', 'warning');
    else
      form.hideError(id);
  }
}

function __check_city(form, id)
{
  var id2 = id+'2';

  var e = form.inputs[id];
  var e2 = form.inputs[id2];
  var p = form.properties[id];

  var msg = 'Please fill in';

  if (!e || !e2) return;

  if (e.value == -1)
  {
    e2.style.visibility = 'visible';

    if (!e2.value)
    {
      form.showError(id, 'Please enter city', 'warning');
      form.showError(id2, 'Please enter city', 'warning');
    }
    else
    {
      form.hideError(id);
      form.hideError(id2);
    }
  }
  else
  {
    e2.style.visibility = 'hidden';

    if (p.necessary && !e.value)
      form.showError(id, msg, 'warning');
    else
      form.hideError(id);
  }
}

function __check_empty(form, id)
{
}

function __check_email4invite(form, id)
{
  var id2 = id+'2';
  var e = form.inputs[id];
  var e2 = form.inputs[id2];
  var p = form.properties[id];

  if (p.necessary && e.value.length == 0)
    form.showError(id, 'Please fill in', 'warning');
  else if (e.value.length > 60)
    form.showError(id, 'Max length - 60 symbols', 'error');
  else if (e.value.length && !check_correct_email(e.value))
    form.showError(id, 'Please enter correct E-mail', 'warning');
  else
  {
    form.hideError(id);

    if (e2)
    {
      if (e.value != e2.value && e.value.substr(0, e2.value.length) == e2.value)
        form.hideError(id2, '', 'warning');
      else if (!e2.value.length)
        form.hideError(id2, '', 'empty');
      else if (e.value.length && e2.value.length && e.value == e2.value)
      {
        form.hideError(id, '', 'ok');
        form.hideError(id2, '', 'ok');
      }
      else //if (e.value.length && e2.value.length && e.value != e2.value)
        form.showError(id2, 'Entered E-mails not match', 'error');
    }
  }
}



function remind_password(email)
{
  if (!email || !check_correct_email(email))
  {
    alert('Please enter correct E-mail for password restoring');

    return false;
  }

  if (!confirm('Are you sure you want us to send your password to '+email+'?'))
    return false;

  var xml = '/xml/remind_password.php';
  var query = 'email='+escape(email);

  diXMLRequest.post(xml, query, remind_password_response);

  return false;
}

function remind_password_response(xmlDoc)
{
  try { var rs = xmlDoc.documentElement.getElementsByTagName('result'); } catch(e) {}
  if (!rs) return false;

  var r = rs[0];
  var ok = r.getAttribute('ok')*1;

  if (ok)
    alert('Your password has been sent to your email');
  else
    alert('An error occured');
}

