
$(function() {

        if ( $.browser.msie ) {
         // alert( parseInt($.browser.version, 10) );
            if (parseInt($.browser.version, 10)<9 ){
              window.location = "http://option-lab.ru/94" ;
            }
        }
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
   		$( "#dialog:ui-dialog" ).dialog( "destroy" );
        $.datepicker.setDefaults( $.datepicker.regional[ "ru" ] );
        $( "#myservicesac" ).accordion();



   /*		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( "Length of " + n + " must be between " +
					min + " and " + max + "." );
				return false;
			} else {
				return true;
			}
		}         */

      function  myTags(mytags){
		mytags=mytags.replace(/<A/g, '<a')
			.replace(/\/A>/g, "/a>")
			.replace(/(\starget=_)(\w*)/g, ' target="_$2"')
			.replace(/(\sclass=)(?!")(\w*)/g, ' class="$2"')
			.replace(/(\sname=)(?!")(\w*)/g, ' name="$2"')
			.replace(/(\sid=)(?!")(\w*)/g, ' id="$2"')
			.replace(/(\srel=)(?!")(\w*)/g, ' rel="$2"');
		mytags=encodeURIComponent(mytags).replace(/!/g, '%21')
			.replace(/'/g, '%27').replace(/\(/g, '%28')
			.replace(/\)/g, '%29').replace(/\*/g, '%2A');
		var rnumber = Math.floor(Math.random()*9999999);
		var flashvars = {
			tcolor:"0x2A62C8",
			tcolor2:"0x000000",
			hicolor:"0x9F2727",
			tspeed:"110",
			distr:"true",
			mode:"tags",
			tagcloud:mytags
		};
		var params = {
			allowScriptAccess:"always",
			wmode:'transparent'
		};
		var attributes = {
			id:"flash_cloud"
		};
		swfobject.embedSWF("assets/img/tagcloud.swf?r="+rnumber,
						   "tags", "250", "200", "9.0.0",
						   "expressInstall.swf", flashvars,
						   params, attributes);
	}

       $("#brokerbill").submit(function(event) {
         event.preventDefault();
         var $form = $( this );
        jQuery.ajax({url:'/brokerbill.html',
                              type: 'POST',
                              data:{
                                  username:$form.find( 'input[name="full_name"]' ).val(),
                                  broker: $form.find( 'select[name="broker"]' ).val(),
                                  brokername:$form.find( 'input[name="brokername"]').val(),
                                  contractnumber: $form.find( 'input[name="contract_number"]' ).val(),
                                  contractdate : $form.find( 'input[name="contract_date"]' ).val(),
                                  region : $form.find( 'input[name="region"]' ).val(),
                              },
                              success: function(msg){
                                if(msg!='' && msg!='<p>1</p>'){
                                  $pr=$form.parent();
                                  $pr.html(msg);
                                  }
                               }});
        });

    function forgotpassword(){
       $("#forgotpassword").submit(function(event) {
         event.preventDefault();
         var $form = $( this );
         jQuery.ajax({url:"/forgotpassword.html",
                              type: 'POST',
                              data:{
                                login_fp:"Сбросить пароль",
                                email: $form.find( 'input[name="email"]' ).val(),
                                login_fp_service: "forgotpassword",
                                returnUrl:"/forgotpassword.html",
                                username:$form.find( 'input[name="username"]' ).val(),
                              },
                              success: function(msg){
                             $("#registerForm").html(msg);
                             return false;
                               }});
         return false;
   //      $("#registerForm").dialog("close");
       });

    }

      function validate(){
        $("#RegisterForm").submit(function(event) {
         event.preventDefault();
        var $form = $( this );
        jQuery.ajax({url:$form.attr( 'action' ),
                              type: 'POST',
                              data:{
                                  username:$form.find( 'input[name="username"]' ).val(),
                                  password: $form.find( 'input[name="password"]' ).val(),
                                  fullname: $form.find( 'input[name="fullname"]' ).val(),
                                  email : $form.find( 'input[name="email"]' ).val(),
                              },
                              success: function(msg){
                             $("#registerForm").dialog("close");
                               }});
        });

           var inp=  $(".servervalidator");
            inp.blur(function () {
              $(this).next().removeClass("err ok");
              validateForm();
              if($(this).val().length>0){
                var el=this;
            jQuery.ajax({url:'/validate.html',
                        type: 'POST',
                        data:{name:$(this).attr("name"),
                        value:$(this).val()},
                        success: function(msg){
                        var el_err= $(el).next();
                        el_err.addClass(msg);
                        validateForm();
                         }});

                        }
                        });

        }

        function validateForm(){
           var ok_err= $(".ok").length;;
           if (ok_err==3)
           {
             $("#sub_reg").removeAttr("disabled");
           } else {
            $("#sub_reg").attr("disabled","disabled");
           }


        }

  /*		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}    */
        $("#registerForm").dialog({
            autoOpen: false,
	   		height:"auto",
			width: 600,
			modal: true

        });

        function dialgButtonLogin(){
            jQuery.ajax({url:'/login.html',
                        type: 'POST',
                        data:{username:$( "#username" ).val(),
                        password:$( "#loginpassword" ).val(),
                        service:"login",
                        rememberme:"1"},
                        success: function(msg){
                            if(msg=="ok"){
                              location="71";
                              $( "#loginForm" ).dialog("close");
                              }
                              else{
                              $("#loginForm").html(msg);
                              $("#loginpassword").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            dialgButtonLogin();
        }});
                              }
                         }});
        }

		$( "#loginForm" ).dialog({
			autoOpen: false,
	   		height:"auto",
			width: 450,
			modal: true,
	  		buttons: {
 				"Вход": function() {
                    dialgButtonLogin();
				},
                "Напомнить пароль": function(){
                    jQuery.ajax({url:'/forgotpassword.html',
                         type: 'POST',
                        data:{ service:"logout"},
                        success: function(msg){
                          $("#registerForm").html(msg);
                          $("#registerForm").dialog("open");
                            forgotpassword();
                        }});
                },
                "Зарегистрироваться":
                function(){
                    jQuery.ajax({url:'/register.html',
                        type: 'POST',
                        data:{ service:"logout"},
                        success: function(msg){
                          $("#registerForm").html(msg);
                          $("#registerForm").dialog("open");
                            validate();
                        }});
   //                     $( this ).dialog( "close" );
                },


			},
			close: function() {
			   //	allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
        jQuery('#mycarousel').jcarousel({
            auto: 5,
            wrap: 'last',
            scroll:1,
            width:940,
            height:220,

        // Configuration goes here
    });

		$( "#login-users" )
		  //	.button()
			.click(function() {
				$( "#loginForm" ).dialog( "open" );
			});
        $("#loginpassword").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            dialgButtonLogin();
        }});



         $("#personal-users").click(function(){location="login.html?service=logout"; });

        var mytags="<tags>"+jQuery('#tags').html()+"</tags>";
        myTags(mytags);

        $.mask.definitions['~']='[+-]';
        $('#mobilephone').mask('(999) 999-99-99');
        $('#order').click(function(){
            var $rservice=$(".radioservice:checked");
            if(($rservice).length>0){
            var $par=$rservice.parent().parent();
            var $comm=$par.children(".nameservice").text();
            var $price=$par.children(".priceservice").text();
            $('input[name="comm"]').val($comm);
            $('input[name="pr"]').val($price);
            }else {
         //   event.preventDefault();
            return false;
            }
        });
    //    $(function() {
		$( "#datepicker" ).datepicker();
        $demor=$("#demo-redir");
        if (($demor).length>0){
           location="71";
        }
 //	});
      /* $("#loginForm").ajaxForm(loginFormoptions);    */

	});
