window.profile =
  {
    //ERRORS: BEGIN
    errors : [],
    //ERRORS: END

    flag : function(item, flag, title)
      {
        if (title.length > 0)
          {
            document.getElementById(item).style.cursor = 'help';
            document.getElementById(item).title = title;
          }
        else
          {
            document.getElementById(item).style.cursor = '';
            document.getElementById(item).title = '';
          }

             if (flag == 'ok')            document.getElementById(item).src = '/templates/images/pages/profile/ok.gif';
        else if (flag == 'error')         document.getElementById(item).src = '/templates/images/pages/profile/error.gif';
        else if (flag == 'not_available') document.getElementById(item).src = '/templates/images/pages/profile/not_available.gif';
        else if (flag == 'unknown')       document.getElementById(item).src = '/templates/images/pages/profile/unknown.gif';
        else                              document.getElementById(item).src = '/templates/images/pages/profile/null.gif';
      },

    all : function()
      {
        this.errors = [];
        
        //this.nick();
        
        /* this.email(); */
        
        this.subscribed();
        
        //this.name_first();
        //this.name_last();
        
        //this.birthdate();
        
        //this.gender();
        
        
        this.country();
        
        this.city();
        
        this.address1();
        
        this.address2();
        
        this.postcode();
        
        
        this.phone_cell();
        

        
        this.phone_home();
        this.phone_work();
        
        //this.im();

        if (this.errors.length > 0)
          {
            var message = ((this.errors.length == 1)?'Error:\r\n':'Errors:\r\n');
            for (var i = 0; i < this.errors.length; ++i)
              {
                message += '\r\n * ' + this.errors[i];
              }
            alert(message);

            return false;
          }
        else
          {
            return true;
          }
      },
    email_edit : function () {
      jQuery('tr.email_input_row').toggle();  
    },
    messenger_remove : function(obj) {
        jQuery(obj).parent().parent().parent().remove();
        jQuery('#messenger_add').show();
    },
    messenger_add : function() {
        var i = 0;
        var max_messengers_limit = 1;
        var types_select = '';
        var output = '<tr class="row contact_row"><th>';
        

        if (jQuery('tr.contact_row th select').length > 0) {
            i = jQuery('tr.contact_row th select').length + 1;
        }       

        jQuery.get(
                    '/en/profile/messenger_types/', { }, 
                    onSuccess = function(data) {
                        types_select = data;
                        output += '<select style="width:70px" name="profile_im[{messenger_row.num}][type]">';
                        output += types_select;
                        output += '</select>';
                        output += '</th>';
                        output += '<td>';
                        output += '<input name="profile_im[' + i + '][contact]" type="text" value="" class="text phone_cell_local" style="width:185px" maxlength="32" /><img src="/templates/images/pages/profile/delete.gif" border="0" title="Remove contact" alt="Remove contact" style="cursor:pointer; padding-top:2px; padding-left:3px;" onclick="profile.messenger_remove(this)" title="Remove contact" /></td>';
                        output += '<td align="left"></td>';
                        output += '</tr>';
                        if (jQuery('tr.contact_row:last')) {
                            if (i < max_messengers_limit) {
                                jQuery(output).insertAfter('tr.contact_row:last');      
                                jQuery('tr.contact_row:last th select option:selected').removeAttr('selected');                        
                                jQuery('#messenger_add').hide();
                            } else {
                                alert('You can add only one contact row');
                            }
                        }
                    }
                );
    },
    
    phone_toggle : function(obj) {
        jQuery('tr.additional_phone select.select_phones').each(function(index, el) {
            if (el.name != obj.name) {
                jQuery("option[value='" + (obj.value == 'home' ? 'work' : 'home') + "']", el).attr('selected', true);
            }
        });        
    },
    phone_add : function() {
        var max_phones = 2;
        var i = 0;
        var selected;
        var output = '';
        var types = {'work' : 'Work', 'home' : 'Home'};
        var cur_type = 'work';
        
        if (jQuery('tr.additional_phone')) {
            i = jQuery('tr.additional_phone').length;
        }
        
        var select = '<select style="width:65px;" name="phones[' + i + '][type]" class="select_phones" onChange="profile.phone_toggle(this)">';
        
        for ( key in types ) {
            selected = '';
            if (jQuery("tr.additional_phone select.select_phones option:selected").val() != key) {
                selected = 'selected';
                cur_type = key;
            }
            select += '<option ' + selected + ' value="' + key + '">' + types[key] + '</option>';    
        }
        

        var onchange = 'onchange="profile.phone_' + cur_type + '();" onkeyup="if ((event.keyCode != 9) && (event.keyCode != 16)) profile.phone_' + cur_type + '();"';


        var input = '<td class="phone_cell" valign="middle"><div style="float:left; margin-right: 5px;">';
            input += '+&nbsp;<input id="profile_phone_' + cur_type + '_country" ' + onchange + ' type="text" name="phones[' + i + '][country]" class="phone_cell_country" title="country code" maxlength="3" />';
            input += '<input id="profile_phone_' + cur_type + '_area" ' + onchange + ' type="text" class="phone_cell_area" name="phones[' + i + '][code]" title="area code" maxlength="5">';
            input += '<input id="profile_phone_' + cur_type + '_local"  ' + onchange + ' type="text" class="phone_cell_local" name="phones[' + i + '][number]" maxlength="24" title="subscriber\'s number"  />';
            input += '</div><div style="padding-top:1px; float:right"><img src="/templates/images/pages/profile/delete.gif" border="0" title="Remove contact" alt="Remove contact" onclick="profile.messenger_remove(this)" style="cursor:pointer;" /></div>';
            input += '</td><td><img id="profile_phone_' + cur_type + '_flag" src="/templates/images/pages/password/null.gif" width="22" height="11" border="0" /></td>';
            if (i <= max_phones -1) {
                output = '<tr class="additional_phone" valign="middle"><th>' + select + '</th>' + input +'</tr>';
                if (jQuery('tr.additional_phone').length >0) {
                    
                    jQuery(output).insertAfter('tr.additional_phone');        
                } else {
                    jQuery(output).insertAfter('tr#primary_cell');        
                }
            } else {
                alert('Max phones limit reached');
            }
        
    },
    nick : function(exists)
      {
        var regexp_nick1 = /^[A-Za-z0-9_-]+$/;
        var regexp_nick2 = /^[A-Za-z]/;

        if (document.getElementById('profile_nick').value.length == 0)
          {
            this.nick_exists_timing_stop();
            this.flag('profile_nick_flag', 'null', '');
            return true;
          }
        else if (!regexp_nick1.test(document.getElementById('profile_nick').value))
          {
            this.nick_exists_timing_stop();
            this.errors.push('Only Latin letters, numbers, hyphens and underscores can be used for your screen name');
            this.flag('profile_nick_flag', 'error', 'Only Latin letters, numbers, hyphens and underscores can be used for your screen name');
            return false;
          }
        else if (!regexp_nick2.test(document.getElementById('profile_nick').value))
          {
            this.nick_exists_timing_stop();
            this.errors.push('Screen name should begin with a letter');
            this.flag('profile_nick_flag', 'error', 'Screen name should begin with a letter');
            return false;
          }
        else if (document.getElementById('profile_nick').value.length < 3)
          {
            this.nick_exists_timing_stop();
            this.errors.push('Your screen name must be at least 3 characters');
            this.flag('profile_nick_flag', 'error', 'Your screen name must be at least 3 characters');
            return false;
          }
        else if (document.getElementById('profile_nick').value.length > 20)
          {
            this.nick_exists_timing_stop();
            this.errors.push('Your screen name is too long; 20 characters max');
            this.flag('profile_nick_flag', 'error', 'Your screen name is too long; 20 characters max');
            return false;
          }
        else if (document.getElementById('profile_nick').value == document.getElementById('profile_nick_current').value)
          {
            this.nick_exists_timing_stop();
            this.flag('profile_nick_flag', 'null', '');
            return true;
          }
        else if ((exists) && (document.getElementById('profile_nick').value.toLowerCase() != document.getElementById('profile_login').value.toLowerCase()))
          {
            document.getElementById('profile_nick_exists').value = 0;
            profile.flag('profile_nick_flag', 'unknown', 'Please, wait...');
            this.nick_exists(document.getElementById('profile_nick').value, 'profile.flag(\'profile_nick_flag\', \'not_available\', \'Sorry, but that screen name already exists\'); document.getElementById(\'profile_nick_exists\').value = 1;', 'profile.flag(\'profile_nick_flag\', \'ok\', \'OK\'); document.getElementById(\'profile_nick_exists\').value = 0;', 'profile.flag(\'profile_nick_flag\', \'unknown\', \'Screen name availability is unknown\'); document.getElementById(\'profile_nick_exists\').value = 0;');
            return true;
          }
        else if (document.getElementById('profile_nick_exists').value == 1)
          {
            this.errors.push('Your screen name is not available');
            return false;
          }
        else
          {
            this.flag('profile_nick_flag', 'ok', 'OK');
            return true;
          }
      },

    email : function()
      {
        var regexp_email = /^[a-zA-Z0-9\._-]+@(.{1,}\.)*[a-zA-Z0-9]+[a-zA-Z0-9-]*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/;

        if (document.getElementById('profile_email').value.length == 0)
          {
            this.errors.push('E-mail is required');
            this.flag('profile_email_flag', 'error', 'E-mail is required');
            return false;
          }
        else if (!regexp_email.test(document.getElementById('profile_email').value))
          {
            this.errors.push('There is a mistake in your e-mail');
            this.flag('profile_email_flag', 'error', 'There is a mistake in your e-mail');
            return false;
          }
        else if (document.getElementById('profile_email').value.length > 129)
          {
            this.errors.push('Your e-mail is too long; 129 characters max');
            this.flag('profile_email_flag', 'error', 'Your e-mail is too long; 129 characters max');
            return false;
          }
        else
          {
            this.flag('profile_email_flag', 'ok', 'OK');
            return true;
          }
      },

    subscribed : function()
      {
        if (document.getElementById('profile_subscribed').checked)
          {
            this.flag('profile_subscribed_flag', 'ok', 'OK');
            return true;
          }
        else
          {
            this.flag('profile_subscribed_flag', 'null', '');
            return true;
          }
      },

    name_first : function()
      {
        var regexp_name_first = /^[A-Za-z]+$/;

        if (document.getElementById('profile_name_first').value.length == 0)
          {
            this.errors.push('First name is required');
            this.flag('profile_name_first_flag', 'error', 'First name is required');
            return false;
          }
        else if (!regexp_name_first.test(document.getElementById('profile_name_first').value))
          {
            this.errors.push('Only Latin letters are allowed in your first name');
            this.flag('profile_name_first_flag', 'error', 'Only Latin letters are allowed in your first name');
            return false;
          }
        else if (document.getElementById('profile_name_first').value.length > 20)
          {
            this.errors.push('Your first name is too long; 20 characters max');
            this.flag('profile_name_first_flag', 'error', 'Your first name is too long; 20 characters max');
            return false;
          }
        else
          {
            this.flag('profile_name_first_flag', 'ok', 'OK');
            return true;
          }
      },

    name_last : function()
      {
        var regexp_name_last = /^[A-Za-z]+$/;

        if (document.getElementById('profile_name_last').value.length == 0)
          {
            this.errors.push('Last name is required');
            this.flag('profile_name_last_flag', 'error', 'Last name is required');
            return false;
          }
        else if (!regexp_name_last.test(document.getElementById('profile_name_last').value))
          {
            this.errors.push('Only Latin letters are allowed in your last name');
            this.flag('profile_name_last_flag', 'error', 'Only Latin letters are allowed in your last name');
            return false;
          }
        else if (document.getElementById('profile_name_last').value.length > 20)
          {
            this.errors.push('Your last name is too long; 20 characters max');
            this.flag('profile_name_last_flag', 'error', 'Your last name is too long; 20 characters max');
            return false;
          }
        else
          {
            this.flag('profile_name_last_flag', 'ok', 'OK');
            return true;
          }
      },

    birthdate : function()
      {
        if ((this.birthdate_day()) && (this.birthdate_month()) && (this.birthdate_year()))
          {
            if ((document.getElementById('profile_birthdate_day').value == 0) && (document.getElementById('profile_birthdate_month').value == 0) && (document.getElementById('profile_birthdate_year').value == 0))
              {
                this.errors.push('Date of birth is required');
                this.flag('profile_birthdate_flag', 'error', 'Date of Birth is required');
                return false;
              }
            else
              {
                this.flag('profile_birthdate_flag', 'ok', 'OK');
                return true;
              }
          }
        else
          {
            return false;
          }
      },

    birthdate_day : function()
      {
        if (((document.getElementById('profile_birthdate_day').value == 0) && ((document.getElementById('profile_birthdate_month').value != 0) || (document.getElementById('profile_birthdate_year').value != 0))) || ((document.getElementById('profile_birthdate_day').value != 0) && ((document.getElementById('profile_birthdate_month').value == 0) && (document.getElementById('profile_birthdate_year').value == 0))))
          {
            this.errors.push('Date of birth should be entered in full');
            this.flag('profile_birthdate_flag', 'error', 'Date of birth should be entered in full');
            return false;
          }
        else if (((document.getElementById('profile_birthdate_day').value == 29) && (document.getElementById('profile_birthdate_month').value == 2) && (document.getElementById('profile_birthdate_year').value % 4 != 0)) || ((document.getElementById('profile_birthdate_month').value == 2) && (document.getElementById('profile_birthdate_day').value == 30)) || (((document.getElementById('profile_birthdate_month').value == 2) || (document.getElementById('profile_birthdate_month').value == 4) || (document.getElementById('profile_birthdate_month').value == 6) || (document.getElementById('profile_birthdate_month').value == 9) || (document.getElementById('profile_birthdate_month').value == 11)) && (document.getElementById('profile_birthdate_day').value == 31)))
          {
            this.errors.push('Date of birth doesn\'t exist');
            this.flag('profile_birthdate_flag', 'error', 'Date of birth doesn\'t exist');
            return false;
          }
        else if (!((document.getElementById('profile_birthdate_day').value >= 0) && (document.getElementById('profile_birthdate_day').value <= 31)))
          {
            this.errors.push('Date of birth is invalid');
            this.flag('profile_birthdate_flag', 'error', 'Date of birth is invalid');
            return false;
          }
        else return true;
      },

    birthdate_month : function()
      {
        if (((document.getElementById('profile_birthdate_month').value == 0) && ((document.getElementById('profile_birthdate_day').value != 0) || (document.getElementById('profile_birthdate_year').value != 0))) || ((document.getElementById('profile_birthdate_month').value != 0) && ((document.getElementById('profile_birthdate_day').value == 0) && (document.getElementById('profile_birthdate_year').value == 0))))
          {
            this.errors.push('Date of birth should be entered in full');
            this.flag('profile_birthdate_flag', 'error', 'Date of birth should be entered in full');
            return false;
          }
        else if (!((document.getElementById('profile_birthdate_month').value >= 0) && (document.getElementById('profile_birthdate_month').value <= 12)))
          {
            this.errors.push('Date of birth is invalid');
            this.flag('profile_birthdate_flag', 'error', 'Date of birth is invalid');
            return false;
          }
        else return true;
      },

    birthdate_year : function()
      {
        if (((document.getElementById('profile_birthdate_year').value == 0) && ((document.getElementById('profile_birthdate_day').value != 0) || (document.getElementById('profile_birthdate_month').value != 0))) || ((document.getElementById('profile_birthdate_year').value != 0) && ((document.getElementById('profile_birthdate_day').value == 0) && (document.getElementById('profile_birthdate_month').value == 0))))
          {
            this.errors.push('Date of birth should be entered in full');
            this.flag('profile_birthdate_flag', 'error', 'Date of birth should be entered in full');
            return false;
          }
        else if (!(document.getElementById('profile_birthdate_year').value >= 0))
          {
            this.errors.push('Date of birth is invalid');
            this.flag('profile_birthdate_flag', 'error', 'Date of birth is invalid');
            return false;
          }
        else return true;
      },

    gender : function()
      {
        if ((document.getElementById('profile_gender_m').checked) || (document.getElementById('profile_gender_f').checked))
          {
            this.flag('profile_gender_flag', 'ok', 'OK');
            return true;
          }
        else
          {
            this.errors.push('Gender is required');
            this.flag('profile_gender_flag', 'error', 'Gender is required');
            return false;
          }
      },

    country : function(exists)
      {
          if (jQuery('#profile_country').length <= 0) {
              return true;
          }
          
        if (document.getElementById('profile_country').value.length == 0)
          {
            this.country_exists_timing_stop();
            this.flag('profile_country_flag', 'null', '');
            return true;
          }
        else if (document.getElementById('profile_country').value.length < 3)
          {
            this.country_exists_timing_stop();
            this.errors.push('Your country must be at least 3 characters');
            this.flag('profile_country_flag', 'error', 'Your country must be at least 3 characters');
            return false;
          }
        else if (document.getElementById('profile_country').value.length > 38)
          {
            this.country_exists_timing_stop();
            this.errors.push('Your country is too long; 38 characters max');
            this.flag('profile_country_flag', 'error', 'Your country is too long; 38 characters max');
            return false;
          }
        else if ((document.getElementById('profile_country').value.toLowerCase() == 'france') || (document.getElementById('profile_country').value.toLowerCase() == 'russia')  || (document.getElementById('profile_country').value.toLowerCase() == 'usa'))
          {
            this.country_exists_timing_stop();
            this.errors.push('Sorry, but your country is not available');
            this.flag('profile_country_flag', 'error', 'Sorry, but your country is not available');
            return false;
          }
        else if (exists)
          {
            document.getElementById('profile_country_exists').value = 1;
            profile.flag('profile_country_flag', 'unknown', 'Please, wait...');
            this.country_exists(document.getElementById('profile_country').value, 'profile.flag(\'profile_country_flag\', \'ok\', \'OK\'); document.getElementById(\'profile_country_exists\').value = 1;', 'profile.flag(\'profile_country_flag\', \'null\', \'\'); document.getElementById(\'profile_country_exists\').value = 1;', 'profile.flag(\'profile_country_flag\', \'not_available\', \'Sorry, but that country does not exist\'); document.getElementById(\'profile_country_exists\').value = 0;', 'profile.flag(\'profile_country_flag\', \'unknown\', \'Country availability is unknown\'); document.getElementById(\'profile_country_exists\').value = 1;');
            return true;
          }
        else if (document.getElementById('profile_country_exists').value == 0)
          {
            this.errors.push('Sorry, but your country is not available');
            return false;
          }
        else
          {
            profile.flag('profile_country_flag', 'null', '');
            return true;
          }
      },

    city : function()
      {
        var regexp_city = /^[A-Za-z-\s]+$/;
        if (jQuery('#profile_city').length <= 0) {
              return true;
         }


        if (document.getElementById('profile_city').value.length == 0)
          {
            this.flag('profile_city_flag', 'null', '');
            return true;
          }
        else if (!regexp_city.test(document.getElementById('profile_city').value))
          {
            this.errors.push('Only Latin letters, hyphens and spaces are allowed in your city');
            this.flag('profile_city_flag', 'error', 'Only Latin letters, hyphens and spaces are allowed in your city');
            return false;
          }
        else if (document.getElementById('profile_city').value.length > 32)
          {
            this.errors.push('Your city is too long; 32 characters max');
            this.flag('profile_city_flag', 'error', 'Your city is too long; 32 characters max');
            return false;
          }
        else
          {
            this.flag('profile_city_flag', 'ok', 'OK');
            return true;
          }
      },

    postcode : function()
      {
        var regexp_postcode = /^[A-Za-z0-9\s]+$/;
        if (jQuery('#profile_postcode').length <= 0) {
              return true;
        }

        if (document.getElementById('profile_postcode').value.length == 0)
          {
            this.flag('profile_city_flag', 'null', '');
            return true;
          }
        else if (!regexp_postcode.test(document.getElementById('profile_postcode').value))
          {
            this.errors.push('Only Latin letters, numbers and spaces are allowed in your postal code');
            this.flag('profile_city_flag', 'error', 'Only Latin letters, numbers and spaces are allowed in your postal code');
            return false;
          }
        else if (document.getElementById('profile_postcode').value.length > 16)
          {
            this.errors.push('Your postal code is too long; 16 characters max');
            this.flag('profile_city_flag', 'error', 'Your postal code is too long; 16 characters max');
            return false;
          }
        else
          {
            this.flag('profile_city_flag', 'ok', 'OK');
            return true;
          }
      },

    address1 : function()
      {
        if (jQuery('#profile_address1').length <= 0) {
              return true;
        }          
        var regexp_address1 = /^[A-Za-z0-9-_,\.\'\s\/\[\]\:\;\`\#\*\(\)]+$/;

        if (document.getElementById('profile_address1').value.length == 0)
          {
            this.flag('profile_address1_flag', 'null', '');
            return true;
          }
        else if (!regexp_address1.test(document.getElementById('profile_address1').value))
          {
            this.errors.push('Inadmissible character in your address line one');
            this.flag('profile_address1_flag', 'error', 'Inadmissible character in your address line one');
            return false;
          }
        else if (document.getElementById('profile_address1').value.length > 100)
          {
            this.errors.push('Your address line one is too long; 100 characters max');
            this.flag('profile_address1_flag', 'error', 'Your address line one is too long; 100 characters max');
            return false;
          }
        else
          {
            this.flag('profile_address1_flag', 'ok', 'OK');
            return true;
          }
      },

    address2 : function()
      {
        if (jQuery('#profile_address2').length <= 0) {
              return true;
        }            
        var regexp_address2 = /^[A-Za-z0-9-_,\.\'\s\/\[\]\:\;\`\#\*\(\)]+$/;

        if (document.getElementById('profile_address2').value.length == 0)
          {
            this.flag('profile_address2_flag', 'null', '');
            return true;
          }
        else if (!regexp_address2.test(document.getElementById('profile_address2').value))
          {
            this.errors.push('Inadmissible character in your address line two');
            this.flag('profile_address2_flag', 'error', 'Inadmissible character in your address line two');
            return false;
          }
        else if (document.getElementById('profile_address2').value.length > 100)
          {
            this.errors.push('Your address line two is too long; 100 characters max');
            this.flag('profile_address2_flag', 'error', 'Your address line two is too long; 100 characters max');
            return false;
          }
        else
          {
            this.flag('profile_address2_flag', 'ok', 'OK');
            return true;
          }
      },

    phone_cell_verification : function()
      {
        if ((document.getElementById('profile_phone_cell_country').value == document.getElementById('profile_phone_cell_country_current').value) && (document.getElementById('profile_phone_cell_area').value == document.getElementById('profile_phone_cell_area_current').value) && (document.getElementById('profile_phone_cell_local').value == document.getElementById('profile_phone_cell_local_current').value))
          {
            document.getElementById('profile_phone_cell_verification_block').style.display = '';
          }
        else
          {
            document.getElementById('profile_phone_cell_verification_block').style.display = 'none';
          }

        return this.phone_cell();
      },

    phone_cell : function()
      {
        if ((this.phone_cell_country()) && (this.phone_cell_area()) && (this.phone_cell_local()))
          {
            if (document.getElementById('profile_phone_cell_local').value.length == 0)
              {
                this.flag('profile_phone_cell_flag', 'null', '');
              }
            else
              {
                this.flag('profile_phone_cell_flag', 'ok', 'OK');
              }

            return true;
          }
        else
          {
            return false;
          }
      },

    phone_cell_country : function()
      {

        var regexp_phone_cell_country = /^[0-9]+$/;

        if ((document.getElementById('profile_phone_cell_country').value.length == 0) && (document.getElementById('profile_phone_cell_local').value.length != 0))
          {
            this.errors.push('Cell phone should be entered in full');
            this.flag('profile_phone_cell_flag', 'error', 'Cell phone should be entered in full');
            return false;
          }
        else if (document.getElementById('profile_phone_cell_country').value.length == 0)
          {
            return true;
          }
        else if (!regexp_phone_cell_country.test(document.getElementById('profile_phone_cell_country').value))
          {
            this.errors.push('Only numbers allowed in cell phone country code');
            this.flag('profile_phone_cell_flag', 'error', 'Only numbers allowed in cell phone country code');
            return false;
          }
        else if (document.getElementById('profile_phone_cell_country').value.length > 3)
          {
            this.errors.push('Cell phone country code is too long; 3 characters max');
            this.flag('profile_phone_cell_flag', 'error', 'Cell phone country code is too long; 3 characters max');
            return false;
          }
        else return true;
      },

    phone_cell_area : function()
      {
        var regexp_phone_cell_area = /^[0-9]+$/;

        if ((document.getElementById('profile_phone_cell_area').value.length == 0) && (document.getElementById('profile_phone_cell_local').value.length != 0))
          {
            this.errors.push('Cell phone should be entered in full');
            this.flag('profile_phone_cell_flag', 'error', 'Cell phone should be entered in full');
            return false;
          }
        else if (document.getElementById('profile_phone_cell_area').value.length == 0)
          {
            return true;
          }
        else if (!regexp_phone_cell_area.test(document.getElementById('profile_phone_cell_area').value))
          {
            this.errors.push('Only numbers allowed in cell phone area code');
            this.flag('profile_phone_cell_flag', 'error', 'Only numbers allowed in cell phone area code');
            return false;
          }
        else if (document.getElementById('profile_phone_cell_area').value.length > 5)
          {
            this.errors.push('Cell phone area code is too long; 5 characters max');
            this.flag('profile_phone_cell_flag', 'error', 'Cell phone area code is too long; 5 characters max');
            return false;
          }
        else return true;
      },

    phone_cell_local : function()
      {
        var regexp_phone_cell_local = /^[0-9]+$/;

        if (document.getElementById('profile_phone_cell_local').value.length == 0)
          {
            return true;
          }
        else if (!regexp_phone_cell_local.test(document.getElementById('profile_phone_cell_local').value))
          {
            this.errors.push('Only numbers allowed in cell phone subscriber\'s number');
            this.flag('profile_phone_cell_flag', 'error', 'Only numbers allowed in cell phone subscriber\'s number');
            return false;
          }
        else if (document.getElementById('profile_phone_cell_local').value.length > 24)
          {
            this.errors.push('Cell phone subscriber\'s number is too long; 24 characters max');
            this.flag('profile_phone_cell_flag', 'error', 'Cell phone subscriber\'s number is too long; 24 characters max');
            return false;
          }
        else return true;
      },

    phone_cell_verification_send : function()
      {
        this.phone_verification_send(document.getElementById('profile_login').value, document.getElementById('profile_phone_cell_country_current').value, document.getElementById('profile_phone_cell_area_current').value, document.getElementById('profile_phone_cell_local_current').value, 'document.getElementById(\'profile_phone_cell_verification_block_send\').style.display = \'none\'; document.getElementById(\'profile_phone_cell_verification_block_code\').style.display = \'\';', 'alert(\'This cell phone already exists.\');', 'alert(\'Sms sending failed.\');');
        return true;
      },

    phone_cell_verification_check : function()
      {
        this.phone_verification_check(document.getElementById('profile_login').value, document.getElementById('profile_phone_cell_country_current').value, document.getElementById('profile_phone_cell_area_current').value, document.getElementById('profile_phone_cell_local_current').value, document.getElementById('profile_phone_cell_verification_code').value, 'document.getElementById(\'profile_phone_cell_verification_block_code\').style.display = \'none\'; document.getElementById(\'profile_phone_cell_verification_block_done\').style.display = \'\';', 'alert(\'Code is invalid.\')', 'alert(\'Checking code failed.\');');
        return true;
      },

    phone_home : function()
      {
        if (jQuery('#profile_phone_home_local').length <= 0) {
            return true;
        }
        if ((this.phone_home_country()) && (this.phone_home_area()) && (this.phone_home_local()))
          {
            if (document.getElementById('profile_phone_home_local').value.length == 0)
              {
                this.flag('profile_phone_home_flag', 'null', '');
              }
            else
              {
                this.flag('profile_phone_home_flag', 'ok', 'OK');
              }

            return true;
          }
        else
          {
            return false;
          }
      },

    phone_home_country : function()
      {
          
        var regexp_phone_home_country = /^[0-9]+$/;
        if ((document.getElementById('profile_phone_home_country').value.length == 0) && (document.getElementById('profile_phone_home_local').value.length != 0))
          {
            this.errors.push('Home phone should be entered in full');
            this.flag('profile_phone_home_flag', 'error', 'Home phone should be entered in full');
            return false;
          }
        else if (document.getElementById('profile_phone_home_country').value.length == 0)
          {
            return true;
          }
        else if (!regexp_phone_home_country.test(document.getElementById('profile_phone_home_country').value))
          {
            this.errors.push('Only numbers allowed in home phone country code');
            this.flag('profile_phone_home_flag', 'error', 'Only numbers allowed in home phone country code');
            return false;
          }
        else if (document.getElementById('profile_phone_home_country').value.length > 3)
          {
            this.errors.push('Home phone country code is too long; 3 characters max');
            this.flag('profile_phone_home_flag', 'error', 'Home phone country code is too long; 3 characters max');
            return false;
          }
        else {
            return true
        };
      },

    phone_home_area : function()
      {
        var regexp_phone_home_area = /^[0-9]+$/;


        if ((document.getElementById('profile_phone_home_area').value.length == 0) && (document.getElementById('profile_phone_home_local').value.length != 0))
          {

            this.errors.push('Home phone should be entered in full');
            this.flag('profile_phone_home_flag', 'error', 'Home phone should be entered in full');
            return false;
          }
        else if (document.getElementById('profile_phone_home_area').value.length == 0)
          {
            return true;
          }
        else if (!regexp_phone_home_area.test(document.getElementById('profile_phone_home_area').value))
          {
            this.errors.push('Only numbers allowed in home phone area code');
            this.flag('profile_phone_home_flag', 'error', 'Only numbers allowed in home phone area code');
            return false;
          }
        else if (document.getElementById('profile_phone_home_area').value.length > 5)
          {
            this.errors.push('Home phone area code is too long; 5 characters max');
            this.flag('profile_phone_home_flag', 'error', 'Home phone area code is too long; 5 characters max');
            return false;
          }
        else {
            return true;
        }
      },

    phone_home_local : function()
      {
        var regexp_phone_home_local = /^[0-9]+$/;

        if (document.getElementById('profile_phone_home_local').value.length == 0)
          {
            return true;
          }
        else if (!regexp_phone_home_local.test(document.getElementById('profile_phone_home_local').value))
          {
            this.errors.push('Only numbers allowed in home phone subscriber\'s number');
            this.flag('profile_phone_home_flag', 'error', 'Only numbers allowed in home phone subscriber\'s number');
            return false;
          }
        else if (document.getElementById('profile_phone_home_local').value.length > 24)
          {
            this.errors.push('Home phone subscriber\'s number is too long; 24 characters max');
            this.flag('profile_phone_home_flag', 'error', 'Home phone subscriber\'s number is too long; 24 characters max');
            return false;
          }
        else return true;
      },

    phone_work : function()
      {
        if (jQuery('#profile_phone_work_local').length <= 0) {
            return true;
        }
        if ((this.phone_work_country()) && (this.phone_work_area()) && (this.phone_work_local()))
          {
            if (document.getElementById('profile_phone_work_local').value.length == 0)
              {
                this.flag('profile_phone_work_flag', 'null', '');
              }
            else
              {
                this.flag('profile_phone_work_flag', 'ok', 'OK');
              }

            return true;
          }
        else
          {
            return false;
          }
      },

    phone_work_country : function()
      {
        var regexp_phone_work_country = /^[0-9]+$/;

        if ((document.getElementById('profile_phone_work_country').value.length == 0) && (document.getElementById('profile_phone_work_local').value.length != 0))
          {
            this.errors.push('Work phone should be entered in full');
            this.flag('profile_phone_work_flag', 'error', 'Work phone should be entered in full');
            return false;
          }
        else if (document.getElementById('profile_phone_work_country').value.length == 0)
          {
            return true;
          }
        else if (!regexp_phone_work_country.test(document.getElementById('profile_phone_work_country').value))
          {
            this.errors.push('Only numbers allowed in work phone country code');
            this.flag('profile_phone_work_flag', 'error', 'Only numbers allowed in work phone country code');
            return false;
          }
        else if (document.getElementById('profile_phone_work_country').value.length > 3)
          {
            this.errors.push('Work phone country code is too long; 3 characters max');
            this.flag('profile_phone_work_flag', 'error', 'Work phone country code is too long; 3 characters max');
            return false;
          }
        else return true;
      },

    phone_work_area : function()
      {
        var regexp_phone_work_area = /^[0-9]+$/;

        if ((document.getElementById('profile_phone_work_area').value.length == 0) && (document.getElementById('profile_phone_work_local').value.length != 0))
          {
            this.errors.push('Work phone should be entered in full');
            this.flag('profile_phone_work_flag', 'error', 'Work phone should be entered in full');
            return false;
          }
        else if (document.getElementById('profile_phone_work_area').value.length == 0)
          {
            return true;
          }
        else if (!regexp_phone_work_area.test(document.getElementById('profile_phone_work_area').value))
          {
            this.errors.push('Only numbers allowed in work phone area code');
            this.flag('profile_phone_work_flag', 'error', 'Only numbers allowed in work phone area code');
            return false;
          }
        else if (document.getElementById('profile_phone_work_area').value.length > 5)
          {
            this.errors.push('Work phone area code is too long; 5 characters max');
            this.flag('profile_phone_work_flag', 'error', 'Work phone area code is too long; 5 characters max');
            return false;
          }
        else return true;
      },

    phone_work_local : function()
      {
        var regexp_phone_work_local = /^[0-9]+$/;

        if (document.getElementById('profile_phone_work_local').value.length == 0)
          {
            return true;
          }
        else if (!regexp_phone_work_local.test(document.getElementById('profile_phone_work_local').value))
          {
            this.errors.push('Only numbers allowed in work phone subscriber\'s number');
            this.flag('profile_phone_work_flag', 'error', 'Only numbers allowed in work phone subscriber\'s number');
            return false;
          }
        else if (document.getElementById('profile_phone_work_local').value.length > 24)
          {
            this.errors.push('Work phone subscriber\'s number is too long; 24 characters max');
            this.flag('profile_phone_work_flag', 'error', 'Work phone subscriber\'s number is too long; 24 characters max');
            return false;
          }
        else return true;
      },

    im : function()
      {
        var regexp_im = /^[-_A-Za-z0-9\@\.\s]+$/;

        if (document.getElementById('profile_im').value.length == 0)
          {
            this.flag('profile_im_flag', 'null', '');
            return true;
          }
        else if (!regexp_im.test(document.getElementById('profile_im').value))
          {
            this.errors.push('Only Latin letters, numbers, hyphens, underscores, the \'@\' symbol, dots and spaces are allowed in your instant messenger ID');
            this.flag('profile_im_flag', 'error', 'Only Latin letters, numbers, hyphens, underscores, the \'@\' symbol, dots and spaces are allowed in your instant messenger ID');
            return false;
          }
        else if (document.getElementById('profile_im').value.length > 32)
          {
            this.errors.push('Your instant messenger ID is too long; 32 character max');
            this.flag('profile_im_flag', 'error', 'Your instant messenger ID is too long; 32 character max');
            return false;
          }
        else
          {
            this.flag('profile_im_flag', 'ok', 'OK');
            return true;
          }
      },

    //LOGIN EXISTS: BEGIN
    login_exists : function(login, action_true, action_false, action_error)
      {
        time = new Date();
        this.login_exists_timestamp = time.getTime();
        setTimeout('profile.login_exists_timing(\''+login.replace(/\'/g, '\\\'')+'\', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 1000);
      },

    login_exists_timing : function(login, action_true, action_false, action_error)
      {
        time = new Date();
        if (time.getTime() - this.login_exists_timestamp >= 950)
          {
            if (id = this.login_exists_request(login)) {setTimeout('profile.login_exists_response('+id+', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 100);}
          }
      },

    login_exists_timing_stop : function()
      {
        time = new Date();
        this.login_exists_timestamp = time.getTime();
      },

    login_exists_request : function(login)
      {
        vars = [];
        vars['name_exists'] = login;
        return ajax.request('/scripts/api/api.php', 'get', vars);
      },

    login_exists_response : function(id, action_true, action_false, action_error)
      {
        if (answer = ajax.response(id))
          {
                 if (answer['error'])            eval(action_error);
            else if (answer['response'] == true) eval(action_true);
            else                                 eval(action_false);
          }
        else
          {
            setTimeout('profile.login_exists_response('+id+', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 100);
          }
      },
    //LOGIN EXISTS: END

    //NICK EXISTS: BEGIN
    nick_exists : function(nick, action_true, action_false, action_error)
      {
        time = new Date();
        this.nick_exists_timestamp = time.getTime();
        setTimeout('profile.nick_exists_timing(\''+nick.replace(/\'/g, '\\\'')+'\', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 1000);
      },

    nick_exists_timing : function(nick, action_true, action_false, action_error)
      {
        time = new Date();
        if (time.getTime() - this.nick_exists_timestamp >= 950)
          {
            if (id = this.nick_exists_request(nick)) {setTimeout('profile.nick_exists_response('+id+', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 100);}
          }
      },

    nick_exists_timing_stop : function()
      {
        time = new Date();
        this.nick_exists_timestamp = time.getTime();
      },

    nick_exists_request : function(nick)
      {
        vars = [];
        vars['name_exists'] = nick;
        return ajax.request('/scripts/api/api.php', 'get', vars);
      },

    nick_exists_response : function(id, action_true, action_false, action_error)
      {
        if (answer = ajax.response(id))
          {
                 if (answer['error'])            eval(action_error);
            else if (answer['response'] == true) eval(action_true);
            else                                 eval(action_false);
          }
        else
          {
            setTimeout('profile.nick_exists_response('+id+', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 100);
          }
      },
    //NICK EXISTS: END

    //COUNTRY EXISTS: BEGIN
    country_exists : function(country, action_true, action_false, action_null, action_error)
      {
        time = new Date();
        this.country_exists_timestamp = time.getTime();
        setTimeout('profile.country_exists_timing(\''+country.replace(/\'/g, '\\\'')+'\', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_null.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 1000);
      },

    country_exists_timing : function(country, action_true, action_false, action_null, action_error)
      {
        time = new Date();
        if (time.getTime() - this.country_exists_timestamp >= 950)
          {
            if (id = this.country_exists_request(country)) {setTimeout('profile.country_exists_response('+id+', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_null.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 100);}
          }
      },

    country_exists_timing_stop : function()
      {
        time = new Date();
        this.country_exists_timestamp = time.getTime();
      },

    country_exists_request : function(country)
      {
        vars = [];
        vars['country_exists'] = country;
        return ajax.request('/scripts/api/api.php', 'get', vars);
      },

    country_exists_response : function(id, action_true, action_false, action_null, action_error)
      {
        if (answer = ajax.response(id))
          {
                 if (answer['error'])             eval(action_error);
            else if (answer['response'] == true)  eval(action_true);
            else if (answer['response'] == false) eval(action_false);
            else                                  eval(action_null);
          }
        else
          {
            setTimeout('profile.country_exists_response('+id+', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_null.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 100);
          }
      },
    //COUNTRY EXISTS: END

    //PHONE VERIFICATION: BEGIN
    phone_verification_send : function(login, country, area, local, action_true, action_false, action_error)
      {
        if (id = this.phone_verification_send_request(login, country, area, local)) {
            setTimeout('profile.phone_verification_send_response('+id+', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 100);
        }
      },

    phone_verification_send_request : function(login, country, area, local)
      {
        vars = [];
        vars['phone_verification_send'] = 1;
        vars['login'] = login;
        vars['country'] = country;
        vars['area'] = area;
        vars['local'] = local;
        return ajax.request('/scripts/api/api.php', 'get', vars);
      },

    phone_verification_send_response : function(id, action_true, action_false, action_error)
      {
        if (answer = ajax.response(id))
          {
                 if (answer['error'])            eval(action_error);
            else if (answer['response'] == true) eval(action_true);
            else                                 eval(action_false);
          }
        else
          {
            setTimeout('profile.phone_verification_send_response('+id+', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 100);
          }
      },

    phone_verification_check : function(login, country, area, local, code, action_true, action_false, action_error)
      {
        if (id = this.phone_verification_check_request(login, country, area, local, code)) {
            setTimeout('profile.phone_verification_check_response('+id+', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 100);
        }
      },

    phone_verification_check_request : function(login, country, area, local, code)
      {
        vars = [];
        vars['phone_verification_check'] = 1;
        vars['login'] = login;
        vars['country'] = country;
        vars['area'] = area;
        vars['local'] = local;
        vars['code'] = code;
        return ajax.request('/scripts/api/api.php', 'get', vars);
      },

    phone_verification_check_response : function(id, action_true, action_false, action_error)
      {
        if (answer = ajax.response(id))
          {
                 if (answer['error'])            eval(action_error);
            else if (answer['response'] == true) eval(action_true);
            else                                 eval(action_false);
          }
        else
          {
            setTimeout('profile.phone_verification_check_response('+id+', \''+action_true.replace(/\'/g, '\\\'')+'\', \''+action_false.replace(/\'/g, '\\\'')+'\', \''+action_error.replace(/\'/g, '\\\'')+'\')', 100);
          }
      }
    //PHONE VERIFICATION: END
  }
