function Set_Cookie(name, value, expires, path, domain, secure) {
	var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value);
	if (expires instanceof Date) {
		cookieText += '; expires=' + expires.toGMTString();
	}
	if (path) {
		cookieText += '; path=' + path;
	}
	if (domain) {
		cookieText += '; domain=' + domain;
	}
	if (secure) {
		cookieText += '; secure';
	}
	document.cookie = cookieText;
}
	
$(document).ready(function() {
	$("img#socialAuthVkontakte").click(function(){
		Set_Cookie( 'last', window.location.href, 1800000, '/', '', '' );
		client_id    = document.getElementById("socAuth_vk_client_id").value;
		display      = document.getElementById("socAuth_display").value;
		redirect_uri = document.getElementById("socAuth_vk_redirect_uri").value;
		NewWindow=window.open("http://api.vk.com/oauth/authorize?client_id="+client_id+"&display="+display+"&redirect_uri="+redirect_uri+"?network=vk", "AuthWindow", "width=900,height=500,left=200,top=100");
		NewWindow.focus();
	});
	
	$("img#socialAuthTwitter").click(function(){
		Set_Cookie( 'last', window.location.href, 1800000, '/', '', '' );
		our_site    = document.getElementById("socAuth_twitter_popup").value;
		NewWindow=window.open(our_site, "AuthWindow", "width=900,height=500,left=200,top=100");
		NewWindow.focus();
	});
	
	$("img#socialAuthFacebook").click(function(){
		Set_Cookie( 'last', window.location.href, 1800000, '/', '', '' );
		client_id    = document.getElementById("socAuth_facebook_client_id").value;
		redirect_uri = document.getElementById("socAuth_facebook_redirect_uri").value;
		NewWindow=window.open("https://www.facebook.com/dialog/oauth?client_id="+client_id+"&redirect_uri="+redirect_uri+"&scope=email", "AuthWindow", "width=900,height=500,left=200,top=100");
		NewWindow.focus();
	});
	
	$("img#socialAuthBigVkontakte").click(function(){
		Set_Cookie( 'last', window.location.href, 1800000, '/', '', '' );
		client_id    = document.getElementById("socAuth_vk_client_id").value;
		display      = document.getElementById("socAuth_display").value;
		redirect_uri = document.getElementById("socAuth_vk_redirect_uri").value;
		NewWindow=window.open("http://api.vk.com/oauth/authorize?client_id="+client_id+"&display="+display+"&redirect_uri="+redirect_uri+"?network=vk", "AuthWindow", "width=900,height=500,left=200,top=100");
		NewWindow.focus();
	});
	
	$("img#socialAuthBigTwitter").click(function(){
		Set_Cookie( 'last', window.location.href, 1800000, '/', '', '' );
		our_site    = document.getElementById("socAuth_twitter_popup").value;
		NewWindow=window.open(our_site, "AuthWindow", "width=900,height=500,left=200,top=100");
		NewWindow.focus();
	});
	
	$("img#socialAuthBigFacebook").click(function(){
		Set_Cookie( 'last', window.location.href, 1800000, '/', '', '' );
		client_id    = document.getElementById("socAuth_facebook_client_id").value;
		redirect_uri = document.getElementById("socAuth_facebook_redirect_uri").value;
		NewWindow=window.open("https://www.facebook.com/dialog/oauth?client_id="+client_id+"&redirect_uri="+redirect_uri+"&scope=email", "AuthWindow", "width=900,height=500,left=200,top=100");
		NewWindow.focus();
	});
	
	$("#registerYes").click(function(){
		document.getElementById("registerForEnd").style.display    = "none";
		document.getElementById("registerForEndNo").style.display  = "none";
		document.getElementById("registerForEndYes").style.display = "block";
	});
	
	$("#registerNo").click(function(){
		document.getElementById("registerForEnd").style.display    = "none";
		document.getElementById("registerForEndYes").style.display = "none";
		document.getElementById("registerForEndNo").style.display  = "block";
	});
	
	$(".registerBack").click(function(){
		document.getElementById("registerForEnd").style.display    = "block";
		document.getElementById("registerForEndYes").style.display = "none";
		document.getElementById("registerForEndNo").style.display  = "none";
	});
	
	$("#registerYesLogin").click(function(){
		if($('#yesPassword').val()=='' || $('#yesEmail').val()=='') {
			$('div#registerForEndYesResult').html("Пожалуйста, заполните все поля<br/><br/>");
		}
		else {
			$('div#registerForEndYesResult').html('<img src="/client_files/images/preloader.gif" alt="" border=""/><br/><br/>');
			document.getElementById ("registerYesLogin").disabled=true;
			
			$.ajax({
			'url'     : '/client_files/ws_soc/authorize.php',
			'type'    : 'POST',
			'data'    : {
							'email' : $('#yesEmail').val(),
							'password' : $('#yesPassword').val(),
							'network' : $('#yesNetwork').val(),
							'uid' : $('#yesUid').val()
						},
			'cache'   : false,
			'success' : function( response ){
							document.getElementById ("registerYesLogin").disabled=false;
							var data = eval("(" + response + ")");
							
							if(data.response==0) {
								$('div#registerForEndYesResult').html("Пожалуйста, заполните все поля<br/><br/>");
							}
							else if(data.response==1) {
								$('div#registerForEndYesResult').html(data.text+" <a href='"+data.redirect+"'>"+data.here+"</a>"+"<br/><br/>");
								document.location.href=data.redirect;
							}
							else if(data.response==2) {
								$('div#registerForEndYesResult').html(data.text+"<br/><br/>");
							}
			}, 
			'error'   : function(){
				document.getElementById ("registerYesLogin").disabled=false;
				$('div#registerForEndYesResult').html("Авторизация не удалась<br/><br/>");
			}
			});
		}
	});
	
	$("#registerNoLogin").click(function(){
		if($('#noName').val()=='' || $('#noEmail').val()=='') {
			$('div#registerForEndNoResult').html("Пожалуйста, заполните все поля<br/><br/>");
		}
		else {
			$('div#registerForEndNoResult').html('<img src="/client_files/images/preloader.gif" alt="" border=""/><br/><br/>');
			document.getElementById ("registerNoLogin").disabled=true;
			
			$.ajax({
			'url'     : '/client_files/ws_soc/register.php',
			'type'    : 'POST',
			'data'    : {
							'email' : $('#noEmail').val(),
							'name' : $('#noName').val(),
							'photo' : $('#noPhoto').val(),
							'network' : $('#yesNetwork').val(),
							'uid' : $('#yesUid').val()
						},
			'cache'   : false,
			'success' : function( response ){
							document.getElementById ("registerNoLogin").disabled=false;
							var data = eval("(" + response + ")");
							
							if(data.response==0) {
								$('div#registerForEndNoResult').html("Пожалуйста, заполните все поля<br/><br/>");
							}
							else if(data.response==1) {
								$('div#registerForEndNoResult').html(data.text+" <a href='"+data.redirect+"'>"+data.here+"</a>"+"<br/><br/>");
								document.location.href=data.redirect;
							}
							else if(data.response==2) {
								$('div#registerForEndNoResult').html(data.text+"<br/><br/>");
							}
			}, 
			'error'   : function(){
				document.getElementById ("registerNoLogin").disabled=false;
				$('div#registerForEndNoResult').html("Регистрация не удалась<br/><br/>");
			}
			});
		}
	});
});
