 jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
 
 
 
 
$(function() {

            var name = $("#user_name_ajax"),
			  email = $("#user_email"),
			  password = $("#user_password1"),
		      allFields = $([]).add(name).add(email).add(password),
		   	tips = $("#validateTips");
	     		
 
  function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}
 
		function checkLength(o,n,min,max) {
             
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Длина " + n + " должна быть не менее "+min+" символов");
				return false;
				alert(o.val().length);
			} else {
			
				return true;
			}
 
		}
 
		function checkRegexp(o,regexp,n) {
 
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}
 
		}

  
  
  
  
  function go_reg() {
 
    $("#reg").dialog({ 
    title: 'Регистрация',
 
    width: 360,
 //    modal: true,
    resizable: false,
   // bgiframe: true,
     buttons: {
				
				'Отмена': function()  { 
				$(this).dialog('close');
								},
				'Регистрация': function()  { 
				
				var name = $("#user_name_ajax"),
			 email = $("#user_email"),
			password = $("#user_password1"),
			password2 = $("#user_password2"),
			allFields = $([]).add(name).add(email).add(password),
			tips = $("#validateTips");

				
			      	
							    var bValid = true;
							    
							    	allFields.removeClass('ui-state-error');
							    	
				   bValid = bValid && checkLength(name,"логина",3,16);
					bValid = bValid && checkLength(email,"email",6,80);
					bValid = bValid && checkLength(password,"пароля",4,16);
 
					bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Имя пользователя может содержать только символы [a-z, 0-9, _] и начинаться с буквы.");
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Некорректный формат e-mail. Для примера pupkin2@yandex.ru");
					bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"В пароле разрешенны только символы [a-z,0-9]");
                    
                    if (password.val() != password2.val())  
                    {
                    bValid= false;
                    updateTips('Пароли не совпадают');
                    } 
 
 
					if (bValid) {
					
						var uname = document.getElementById("user_name_ajax").value,
						 email = document.getElementById("user_email").value,
						 captcha = document.getElementById("check").value,
						 user_password1 = document.getElementById("user_password1").value,
						 user_password2 = document.getElementById("user_password2").value;
						
						
						$.post("http://netstroi.ru/index.php?name=account", {
						  op:"ajax_user", user_name:uname,    user_email:email,	 user_password:user_password1,	user_password2:user_password2, rulescheck:"1", check:captcha
						  			},  function(data){
   
   if ( !( /script/.test( data ) ) ) {
      tips.html(data);
   }else  
   {
    $("#reg").html(data);
    setTimeout( 'location.reload()', 3000);
    }    
               });
 
 
 
						
											}

					
							
							
								}
								  
			  } 
			
			  
				
				
    });
    
    }
       
     
    
 


	
		$('#link_reg, #link_reg_file').click(function() {
				
		$.getScript("http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js", function(){
		
		loadOurCss('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css');
		
				
 go_reg();
 			 if (isIE()) 	 $('#feedback').focus();																						});
		
			})





 function loadOurCss(source){
    var head = document.getElementsByTagName("head")[0];
    Css=document.createElement('link');
    Css.rel='stylesheet';
    Css.type='text/css';
    Css.href=source;
    head.appendChild(Css);
   }








 	});
 	
 	
 	
 



 	
