var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera')  > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);
var email_exists        = 0;
var email_incorrect     = 0;

function check_registration_form()
{
    var password        = $('#reg_password');
    var password_md5    = $('#reg_password_md5');

    if( /*!email || */!password || !password_md5)
        return false;
    else
    {
        if(email_exists)
        {
            return false;
        }

        //password_md5.attr("value", MD5(password.attr("value")));

        //password.attr("value", 'password');
        //repeat_password.attr("value", 'password');

        return true;
    }
}

function onEmailChange(email)
{
    var re;
	re = /^[\w\.-]+@[\w\.-]+\.\w+$/;
	if ( email != '' && !email.match(re) )
	{
        return false;
	}

	$.post("/res/ajax_server/check_email.php", {'email' : email}, afterEmailCheck);
}

function afterEmailCheck(data)
{
    if( data == "1" )
    {
        $("#email_exists_mess").css({display : ''});
        email_exists = 1;
    }
    else
    {
        $("#email_exists_mess").css({display : 'none'});
        email_exists = 0;
    }
}

function checkForm()
{
	var cf_utype 		= document.getElementById("profile_user_type");
	var cf_loc 			= document.getElementById("user_documents_locality");
	var cf_l_name 		= document.getElementById("profile_last_name");
	var cf_f_name 		= document.getElementById("profile_first_name");
	var cf_m_name 		= document.getElementById("profile_middle_name");
	var cf_activity 	= document.getElementById("business_activity_select");
	var cf_prof 		= document.getElementById("profile_profession_select");
	var cf_org_title 	= document.getElementById("profile_organization_title");
	var cf_org_old 		= document.getElementById("profile_organization_old");
	var cf_pos 			= document.getElementById("profile_position");	
	var cf_day 			= document.getElementById("profile_day_select");
	var cf_month 		= document.getElementById("profile_month_select");
	var cf_year 		= document.getElementById("profile_year_select");
	var cf_ioffer 		= document.getElementById("investition_offers");
	var cf_error = 0;
	
	if (!cf_utype.value)
	{
		alert("Выберите свой раздел!");
		return 0;
	}
	if (cf_l_name.value != '' && cf_f_name.value != '' && cf_m_name.value != '')
	{
		cf_tmp_str1 = removeSpaces(cf_l_name.value);
		cf_tmp_str2 = removeSpaces(cf_f_name.value);
		cf_tmp_str3 = removeSpaces(cf_m_name.value);
		if ( cf_tmp_str1.length == 0 || cf_tmp_str2.lenght == 0 || cf_tmp_str3.lenght == 0 || cf_tmp_str1 == 0 || cf_tmp_str2 == 0 || cf_tmp_str3 == 0)
		{
			alert("Заполните правильно Ф.И.О.!");
			return 0;
		}
	} else
	{
		alert("Заполните правильно Ф.И.О.!");
		return 0;
	}
	
	if(cf_utype.value == 3 && cf_ioffer.value != '')
	{
		cf_tmp_str1 = removeSpaces(cf_ioffer.value);
		if ( cf_tmp_str1.length == 0 || cf_tmp_str1 == 0)
		{
			alert("Заполните правильно ивестиционное предложение!");
			return 0;	
		}
	}else if (cf_utype.value == 3 && cf_ioffer.value == '')
	{
		alert("Заполните правильно ивестиционное предложение!");
		return 0;
	}
	if (cf_loc.value == 0)
	{
		alert("Заполните населенный пункт!");
		return 0;
	}
	
	if (cf_utype.value != 4 && (!cf_day.value || !cf_month.value || !cf_year.value))
	{
		alert("Выберите дату рождения!");
		return 0;
	}
	
	if (cf_utype.value == 1 && !cf_prof.value)
	{
		alert("Выберите профессию!");
		return 0;
	} else
	if ( (cf_utype.value == 2 || cf_utype.value == 4) && !cf_activity.value)
	{
		alert("Выберите сферу деятельности!");
		return 0;
	}else
	if (cf_utype.value == 3 && !cf_activity.value )
	{
		alert("Выберите сферу инвестиционного предложения!");
		return 0;
	}
	
	if (cf_utype.value == 4)
	{
		if (cf_org_title.value != '' && cf_pos.value != '')
		{
			cf_tmp_str4 = removeSpaces(cf_org_title.value);
			cf_tmp_str5 = removeSpaces(cf_pos.value);
			if ( cf_tmp_str4.length == 0 || cf_tmp_str4 == 0 || cf_tmp_str5.length == 0 || cf_tmp_str5 == 0)
			{
				alert("Заполните правильно название организации и должность!");
				return 0;
			}
		} else
		{
			alert("Заполните правильно название организации и должность!");
			return 0;	
		}

		if (!cf_org_old.value )
		{
			alert("Заполните правильно возраст организации!");
			return 0;
		}
	}
	
	$("#registration_form").submit();
	return 1;
}

function OnUserTypeChange(user_type)
{
    var profession          = $("#profile_profession_area");
    var personal_activity   = $("#profile_personal_activity_area");
    var business_activity   = $("#profile_business_activity_area");
    var web_page            = $("#profile_web_page_area");
    var organization_slogan = $("#profile_organization_slogan_area");
    var investition_offers  = $("#profile_investition_offers_area");
    var organization_title  = $("#profile_organization_title_area");
    var address             = $("#profile_address_area");
    var activity_description= $("#profile_activity_description_area");
    var price_list          = $("#profile_price_list_area");

    var organization_old    = $("#profile_organization_old");
    var pposition           = $("#profile_position");
    
    var birth_date          = $("#profile_birth_date");
    var birth_date_4        = $("#profile_birth_date_4");

    var investment_activity = $("#profile_investment_activity");
    var businesst_activity_3= $("#profile_business_activity");
    var day_3               = $("#profile_day");
    var month_3             = $("#profile_month");
    var year_3              = $("#profile_year");
	
    var ioffer              = $("#profile_investition_offers");
	
    switch (user_type)
    {
        // Работающие по найму
        case "1":
            profession.css("display", "");
            personal_activity.css("display", "");
            business_activity.css("display", "none");

            investment_activity.css("display", "none");
            businesst_activity_3.css("display", "");
			ioffer.css("display", "none");

            organization_slogan.css("display", "none");
            investition_offers.css("display", "none");
            organization_title.css("display", "none");
            address.css("display", "none");
            birth_date.css("display", "");
            birth_date_4.css("display", "none");
            activity_description.css("display", "none");
            price_list.css("display", "none");
            break;

        // Владельцы собственного бизнеса
        case "2":
            profession.css("display", "none");
            personal_activity.css("display", "none");
            business_activity.css("display", "");

            investment_activity.css("display", "none");
			businesst_activity_3.css("display", "");
			ioffer.css("display", "none");
            organization_slogan.css("display", "");
            investition_offers.css("display", "none");
            organization_title.css("display", "none");
            address.css("display", "none");
            birth_date.css("display", "");
            birth_date_4.css("display", "none");
            activity_description.css("display", "");
            price_list.css("display", "");
            break;

        // Инвесторы
        case "3":
            profession.css("display", "none");
            personal_activity.css("display", "none");
            business_activity.css("display", "");

			investment_activity.css("display", "");
			ioffer.css("display", "");
			businesst_activity_3.css("display", "none");

            organization_slogan.css("display", "none");
            investition_offers.css("display", "");
            organization_title.css("display", "none");
            address.css("display", "none");
            birth_date.css("display", "");
            birth_date_4.css("display", "none");
            activity_description.css("display", "");
            price_list.css("display", "");
            break;

        // Организации
        case "4":
            profession.css("display", "none");
            personal_activity.css("display", "none");
            business_activity.css("display", "");

            investment_activity.css("display", "none");
			ioffer.css("display", "none");
			businesst_activity_3.css("display", "");

            organization_slogan.css("display", "none");
            investition_offers.css("display", "none");
            organization_title.css("display", "");
            address.css("display", "");
            birth_date.css("display", "none");
            birth_date_4.css("display", "");
            activity_description.css("display", "");
            price_list.css("display", "");
            break;

        default:
            profession.css("display", "none");
            personal_activity.css("display", "none");
            business_activity.css("display", "none");

            investment_activity.css("display", "none");
			ioffer.css("display", "none");
			ioffer.removeClass("required");
			businesst_activity_3.css("display", "");

            organization_slogan.css("display", "none");
            investition_offers.css("display", "none");
            organization_title.css("display", "none");
            address.css("display", "none");
            birth_date.css("display", "");
            birth_date_4.css("display", "none");
            activity_description.css("display", "none");
            price_list.css("display", "none");
            break;
    }
}
////////////////////////////////////////////////////////
var isIE = (!isOpera && ua.indexOf('msie') > -1);
var selected_country_text = '';
var selected_region_text = '';
var selected_locality_text = '';
var selected_country_id = 0;
var selected_region_id = 0;
var selected_locality_id = 0;

function OnCountryChange(country_id, language)
{
	$("#region_area").html('<img src="/res/images/chocolate/ajax-loader.gif">');
	$("#locality_area").html('<select class="gray px_12" name="locality" id="user_documents_locality" onchange="edit_locality(this);"><option value="0"> - Выберите населенный пункт - </option></select>');
	var obj_country = document.getElementById("user_documents_country");
	selected_country_text = (obj_country.selectedIndex > 0 ? obj_country.options[obj_country.selectedIndex].text : '');
	selected_region_text = '';
	selected_locality_text = '';
	selected_country_id = obj_country.value;
	selected_region_id = 0;
	selected_locality_id = 0;
	if (isIE)
	{
		$.post("/res/ajax_server/get_region.php", {'country' : country_id, 'language' : language, 'ie' : 1}, fillRegion);
	}
	else
	{
		$.post("/res/ajax_server/get_region.php", {'country' : country_id, 'language' : language}, fillRegion);
	}
}

function fillRegion(data)
{
    var ArrRegins   = $('region', data);

	var fr_sel_txt = '<select class="lightgray px_12" name="region" id="user_documents_region" onchange="edit_region(this);OnRegionChange(this.value,'+ u_lang +')">\r\n<option value="0"> - Выберите регион - </option>\r\n';
	var fr_opt_txt = '';
	
    if(ArrRegins.length > 0)
    {
        for(var i=0; i<ArrRegins.length; i++)
        {
			fr_opt_txt ='';
            for( var j=0; ArrRegins[i].attributes.length; j++ )
            {
                if( ArrRegins[i].attributes[j].nodeName == 'id' )
                {
					fr_opt_txt = ArrRegins[i].attributes[j].nodeValue;
                    break;
                }
            }
			fr_sel_txt += '<option value="'+ fr_opt_txt +'">'+ArrRegins[i].childNodes[0].nodeValue +'</option>\r\n';
        }
    } else
		fr_sel_txt += '<option value="0"> На данный момент нет данных. Для добавления региона обратитесь в <span class="blue_12px">техподдержку</span>.</option>'
	fr_sel_txt += '</select>';
	
	$("#region_area").html(fr_sel_txt);
}

function OnRegionChange(region_id, language)
{
	var obj_region = document.getElementById("user_documents_region");
	selected_region_text = obj_region.options[obj_region.selectedIndex].text;
	selected_locality_text = '';
	selected_region_id = obj_region.value;
	selected_locality_id = 0;
	$("#locality_area").html('<img src="/res/images/chocolate/ajax-loader.gif">');
	if (isIE)
	{
		$.post("/res/ajax_server/get_locality.php", {'region' : region_id, 'language' : language, 'ie' : 1}, fillLocality);
	}
	else
	{
		$.post("/res/ajax_server/get_locality.php", {'region' : region_id, 'language' : language}, fillLocality);			
	}
}

function fillLocality(data)
{
    var ArrLocality = $('locality', data);

	var fr_sel_txt = '<select class="lightgray px_12" id="user_documents_locality" name="locality" onchange="edit_locality(this); OnLocalityChange()">\r\n<option value="0"> - Выберите населенный пункт - </option>\r\n';
	var fr_opt_txt = '';	
	
    if(ArrLocality.length > 0)
    {
        for(var i=0; i<ArrLocality.length; i++)
        {
            for( var j=0; ArrLocality[i].attributes.length; j++ )
            {
                if( ArrLocality[i].attributes[j].nodeName == 'id' )
                {
					fr_opt_txt = ArrLocality[i].attributes[j].nodeValue;
                    break;
                }
            }
			fr_sel_txt += '<option value="'+ fr_opt_txt +'">'+ArrLocality[i].childNodes[0].nodeValue +'</option>\r\n';
        }
    }else
		fr_sel_txt += '<option value="0"> На данный момент нет данных. Для добавления населенного пункта обратитесь в техподдержку.</option>'
	fr_sel_txt += '</select>';
	
	$("#locality_area").html(fr_sel_txt);
}
function OnLocalityChange()
{
	var obj_locality = document.getElementById("user_documents_locality");
	selected_locality_text = obj_locality.options[obj_locality.selectedIndex].text;
	selected_locality_id = obj_locality.value;	
}