﻿// JavaScript Document

function myXMLHttpRequest ()
{
	var xmlhttplocal;
	try {
		xmlhttplocal = new ActiveXObject ("Msxml2.XMLHTTP")}
	catch (e) {
		try {
			xmlhttplocal = new ActiveXObject ("Microsoft.XMLHTTP")
		}
		catch (E) {
			xmlhttplocal = false;
		}
  	}
	if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
		try {
			var xmlhttplocal = new XMLHttpRequest ();
		}
		catch (e) {
	  		var xmlhttplocal = false;
			alert ('couldn\'t create xmlhttp object');
		}
	}
	return (xmlhttplocal);
}

	function subscribe()
	{
			var xmlHttp;

			xmlHttp = new myXMLHttpRequest ();
			
			a= document.getElementById("sub_email");
					
			
			url="./support/subscribe.php";
			content = "sub_email="+a.value;
    		xmlHttp.open ("POST", url, true);
    		xmlHttp.setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');
			
			
			
  			xmlHttp.send (content); 
			
			
			xmlHttp.onreadystatechange = function (){
				if(xmlHttp.readyState==4)
				{
					
						
						string = xmlHttp.responseText;
					
						if(string!="^ERROR:1" && string!="^ERROR:2")
						{
							t = document.getElementById("sub_mes");
							t.innerHTML= "Ваш адрес добавлен.";
						}
						if(string=="^ERROR:1" )
						{
							t = document.getElementById("sub_mes");
							t.innerHTML= "Пустое поле адреса!";
						}
						
						if(string=="^ERROR:2" )
						{
							t = document.getElementById("sub_mes");
							t.innerHTML= "Указанный адрес не существует!";
						}

				}
			
			}
		
			
	}
	
	function feedback()
	{
			var xmlHttp;

			xmlHttp = new myXMLHttpRequest ();
			
			a= document.getElementById("text");
					
			
			url="./support/feedback.php";
			content = "text="+a.value;
    		xmlHttp.open ("POST", url, true);
    		xmlHttp.setRequestHeader ('Content-Type','application/x-www-form-urlencoded; charset=utf-8');
			
			
			
  			xmlHttp.send (content); 
			
			
			xmlHttp.onreadystatechange = function (){
				if(xmlHttp.readyState==4)
				{
						string = xmlHttp.responseText;
					
						if(string!="^ERROR:1")
						{
							t = document.getElementById("text_mes");
							t.innerHTML= "Ваше сообщение было отправлено!";
						}
						else
						{
							t = document.getElementById("text_mes");
							t.innerHTML= "Пустое поле сообщения! Ничего не отправлено.";
						}

				}
			
			}
		
	}

	
	
	function ideas(where, what, params)
	{
			var xmlHttp;
			
			xmlHttp = new myXMLHttpRequest ();
			
		 
					
			
			url="ideas.php";
			content = "what="+what+"&"+params;
    		xmlHttp.open ("POST", url, true);
    		xmlHttp.setRequestHeader ('Content-Type','application/x-www-form-urlencoded; charset=utf-8');
			
			
			
  			xmlHttp.send (content); 
			
			
			xmlHttp.onreadystatechange = function (){
				if(xmlHttp.readyState==4)
				{
						document.getElementById(where).innerHTML = xmlHttp.responseText;
						
				}
			
			}
	}
	
	function idea_add_comment(comment_idea_id, author)
	{
		comment_text = document.getElementById("comment_text").value;
		
					var xmlHttp;
			
			xmlHttp = new myXMLHttpRequest ();
			
		
					
			
			url="ideas.php";
			content = "what=add_comment&comment_text="+comment_text+"&author="+author+"&comment_idea_id="+comment_idea_id;
    		xmlHttp.open ("POST", url, true);
    		xmlHttp.setRequestHeader ('Content-Type','application/x-www-form-urlencoded; charset=utf-8');
			
			
			
  			xmlHttp.send (content); 
			
			
			xmlHttp.onreadystatechange = function (){
				if(xmlHttp.readyState==4)
				{
					string  =  xmlHttp.responseText;
						old_str = window.location;
						if(string == "OK") document.getElementById("add_comment_stat").innerHTML = "Отлично. Ваш комментарий добавлен.";
						else
						document.getElementById("add_comment_stat").innerHTML = "Ошибка. Пустой комментарий. <a href=\""+old_str+"\" >Еще раз</a>";
						
						ideas("idea_comments","show_comments","comment_idea_id="+comment_idea_id+"&page=1");
						
				}
			
			}
	}
	
	function idea_vote(idea_id, idea_voting_user, _goto)
	{
		a = document.getElementById("rate_val").value;	
		h = document.getElementById("hidden").checked==true?1:0;	
	
			var xmlHttp;
			
			xmlHttp = new myXMLHttpRequest ();
		
		
			url="ideas.php";
			content = "what=vote_for_idea&idea_id="+idea_id+"&idea_voting_user="+idea_voting_user+"&rate="+a+"&hidden="+h;

    		xmlHttp.open ("POST", url, true);
    		xmlHttp.setRequestHeader ('Content-Type','application/x-www-form-urlencoded; charset=utf-8');
			
			
			
  			xmlHttp.send (content); 
			
			
			xmlHttp.onreadystatechange = function (){
				if(xmlHttp.readyState==4)
				{
						string  =  xmlHttp.responseText;
						old_str = window.location;
						if(string != "error") document.getElementById("idea_voting").innerHTML = "Отлично. Ваша ставка принята.";
						else
						document.getElementById("idea_voting").innerHTML = "Ошибка. Недопустимое значение. Введите долю правильно. <a href=\""+old_str+"\" >Еще раз</a>";
						
						
				}
			
			}
	}
	
	function cancel(idea_id, idea_voting_user, _goto)
	{
		
			
			var xmlHttp;
			
			xmlHttp = new myXMLHttpRequest ();
		

			url="ideas.php";
	
			content = "what=cancel&idea_id="+idea_id+"&idea_voting_user="+idea_voting_user;
    		xmlHttp.open ("POST", url, true);
    		xmlHttp.setRequestHeader ('Content-Type','application/x-www-form-urlencoded; charset=utf-8');
			
		    xmlHttp.send (content); 
			
			
			xmlHttp.onreadystatechange = function (){
				if(xmlHttp.readyState==4)
				{
					
								window.location = _goto;
				}
			
			}
	}
	
	function hash()
	{

		document.getElementById("hash_good4me").value  = "Секундочку! Мы рассчитываем Ваш КОД!";
		surname = document.getElementById("surname").value;
    	 name = document.getElementById("kop_name").value;
		middlename = document.getElementById("middlename").value;
		day =document.getElementById("day").value;
		month =document.getElementById("month").value;
		year =document.getElementById("year").value;		
		err1 = new RegExp(/\d\d/);
    	err2 = new RegExp(/\d\d\d\d/);

		if(err1.test(day)  && err1.test(month)&& err2.test(year) && name!="" && surname!="" && middlename !="") 
		{
			
			text = surname+"/"+name+"/"+middlename+"/"+day+"/"+month+"/"+year;
						
			var xmlHttp;
			
			xmlHttp = new myXMLHttpRequest ();
		

			url="andrew/hash.php";
	
			content = "text="+text;
    		xmlHttp.open ("POST", url, true);
    		xmlHttp.setRequestHeader ('Content-Type','application/x-www-form-urlencoded; charset=utf-8');
			
		    xmlHttp.send (content); 
			
			
			xmlHttp.onreadystatechange = function (){
				if(xmlHttp.readyState==4)
				{
					string  =  xmlHttp.responseText;
					if(string!="error") 
					{
						document.getElementById("hash_good4me").value = string;
						document.getElementById("hash_is_ok").value = 'true';

					}
					else  document.getElementById("hash_good4me").value = "Такой пользователь уже есть!!!";
								
				}
			
			}
			
	}	else  document.getElementById("hash_good4me").value = "Заполните все поля для расчета Кода";

	}
	
	function showHide_invite()
	{
		b= document.getElementById("invite");
		b.style.visibility = (b.style.visibility=='hidden')?'visible':'hidden';
	}


	function get_vote_stat(where, link_id)
	{
			var xmlHttp;
			
			xmlHttp = new myXMLHttpRequest ();
			
		 
					
			
			url="./andrew/vote_info.php";
			content = "link_id="+link_id;
    		xmlHttp.open ("POST", url, true);
    		xmlHttp.setRequestHeader ('Content-Type','application/x-www-form-urlencoded; charset=utf-8');
			
			
			
  			xmlHttp.send (content); 
			
			
			xmlHttp.onreadystatechange = function (){
				if(xmlHttp.readyState==4)
				{
						document.getElementById(where).innerHTML = xmlHttp.responseText;

						
				}
			
			}
	}

function show_draft_list(type)
	{
			var xmlHttp;

			xmlHttp = new myXMLHttpRequest ();
			
			a= document.getElementById("draft");
			if(a.value == "") 	
			{
				t = document.getElementById("draft_list");
				t.style.visibility = "hidden";
			}	
			
			url="./andrew/drafts.php";
			content = "draft_word="+a.value+"&type="+type;
    		xmlHttp.open ("POST", url, true);
    		xmlHttp.setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');
			
			
			
  			xmlHttp.send (content); 
			
			
			xmlHttp.onreadystatechange = function (){
				if(xmlHttp.readyState==4)
				{
					
						
					string = xmlHttp.responseText;
					
					t = document.getElementById("draft_list");
					if(string=="") t.style.visibility = "hidden";
					else
					{
						t.innerHTML= string;
						t.style.visibility = "visible";
					}

				}
			
			}
		
			
	}

	function hidden_draft(event)
	{
		if(!event) event = window.event;
	
		b = document.getElementById("draft_list");
		
		if(event.clientX < 143 || event.clientX > 335) b.style.visibility = "hidden";

	}

// start of HELPER 

array = new Array();

function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function getBodyScrollTop()
{
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
  return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function show_helper(html_id,event)
{
	if(!event) event = window.event;
	helper = document.getElementById("helper");
	helper.style.visibility = "visible";
	
	if(event.clientX+370<getClientWidth())
	{
		helper.style.left =event.clientX+10+"px";	
	}
	else	
	{
		helper.style.left = event.clientX-360+"px";	
	}	
	helper.style.top = event.clientY+10+getBodyScrollTop()+"px";

	help_text = document.getElementById("help_text");
	help_text.innerHTML = array[html_id][1];

		
	
}



function load_help_array()
{

	
	var xmlHttp;
			
	xmlHttp = new myXMLHttpRequest ();
			
	url="./andrew/load_help_array.php";
	
	page = window.location.pathname;
	
	content = "page_path="+page;
	xmlHttp.open ("POST", url, true);
	xmlHttp.setRequestHeader ('Content-Type','application/x-www-form-urlencoded; charset=utf-8');

	xmlHttp.send (content); 
			
	xmlHttp.onreadystatechange = function (){
		if(xmlHttp.readyState==4)
		{
			string = xmlHttp.responseText 
			items = string.split("[||]");
			num = items.length;
			
			for(i=0; i<num; i++)
			{
				
				array_item = items[i].split("[|]");
				array[array_item[0]] = array_item;
				
			
			}
		}
	}
}



function hide_helper()
{
	helper = document.getElementById("helper");
	helper.style.visibility = "hidden";

}



// end of HELPER

	function show_hide(html_id)
	{
		b = document.getElementById("vote-"+html_id);
		b.style.visibility = (b.style.visibility=="hidden")?"visible":"hidden";

	}

	function change_background(id)
	{
		b = document.getElementById(id);
		b.style.backgroundColor = "#F0F000";
		b.style.display.cursor = "hand";
		
	}
