// JavaScript Document

function methodRequest(strURL, strParams, callOperation, selMethod){
	var xmlHttp;
	
	if(window.XMLHttpRequest){
		var xmlHttp = new XMLHttpRequest();
	} else if(window.ActiveXObject){
		var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if(selMethod == "POST"){
		xmlHttp.open('POST', strURL, true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttp.setRequestHeader("Content-length", strParams.length);
		xmlHttp.setRequestHeader("Connection", "close");
	} else {
		xmlHttp.open("GET", strURL+"?"+strParams, true);
	}
	
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4){
			if(callOperation == "weathers"){
				document.getElementById('weathers').innerHTML = xmlHttp.responseText;
			} else if(callOperation == "votepoll"){
				votePollUpdatePage(xmlHttp.responseText);
			} else if(callOperation == "comment"){
				commentsUpdatePage(xmlHttp.responseText);
			} else if(callOperation == "contacts"){
				contactsUpdatePage(xmlHttp.responseText);
			} else if(callOperation == "filter"){
				filterUpdatePage(xmlHttp.responseText);
			} else if(callOperation == "helpone"){
				helpFormUpdatePage(xmlHttp.responseText, 1);
			} else if(callOperation == "helptwo"){
				helpFormUpdatePage(xmlHttp.responseText, 2);
			} else if(callOperation == "helpthree"){
				helpFormUpdatePage(xmlHttp.responseText, 3);
			} else if(callOperation == "helpfour"){
				helpFormUpdatePage(xmlHttp.responseText, 4);
			} else if(callOperation == "portal"){
				itPortalUpdatePage(xmlHttp.responseText);
			} else if(callOperation == "formtwo"){
				updateSendHelpForm(xmlHttp.responseText, 2);
			} else if(callOperation == "formthree"){
				updateSendHelpForm(xmlHttp.responseText, 3);
			} else if(callOperation == "formfour"){
				updateSendHelpForm(xmlHttp.responseText, 4);
			} else if(callOperation == "formfive"){
				updateSendHelpForm(xmlHttp.responseText, 5);
			} else if(callOperation == "formsix"){
				updateSendHelpForm(xmlHttp.responseText, 6);
			} else if(callOperation == "formseven"){
				updateSendHelpForm(xmlHttp.responseText, 7);
			} else if(callOperation == "formnine"){
				updateSendHelpForm(xmlHttp.responseText, 9);
			} else if(callOperation == "likenews"){
				updateLikeNews(xmlHttp.responseText, 9);
			}
		}
	}
		
	if(selMethod == "POST"){
		xmlHttp.send(strParams);
	} else {
		xmlHttp.send(null);
	}
}


$(document).ready(function(){
	$(".login_tab_content").hide();
	$("ul.login_tab li:first").addClass("active").show();
	$(".login_tab_content:first").show();
	
	$("ul.login_tab li").click(function(){
		$("ul.login_tab li").removeClass("active");
		$(this).addClass("active");
		$(".login_tab_content").hide();
		
		var activeTab = $(this).find("a").attr("href");
		$(activeTab).fadeIn();
		return false;
	});
	
	$('#first_accordion').addClass('on');
	
	$('.accordionButton').click(function(){
		$('.accordionButton').removeClass('on');
	 	$('.accordionContent').slideUp('normal');

		if($(this).next().is(':hidden') == true) {
			$(this).addClass('on');
			$(this).next().slideDown('normal');
		 } 
	 });
	
	$('.accordionContent').hide();
	$('#first_accordion').addClass('on');
	$('#first_accordion').next().slideDown('normal');
	
	$("#slider").easySlider({
		auto: false, 
		continuous: true,
		numeric: true
	});
		
	$('a.msg_popup').click(function(e){
		e.preventDefault();
		
		var id = $(this).attr('href');
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
				
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		$('#mask').fadeTo("slow",0.8);	
    	$(id).fadeIn(1000); 
	});
	
	$('#msg_bg, .msg_close_popup').click(function (e){
		e.preventDefault();
		$('#mask').hide();
		$('.window').hide();
	});
	
	$('a.addcomment').click(function(e){
		$("#add_comment").animate({"height": "toggle"}, { duration: 1000 });
	});
	
	$("#add_comment").animate({"height": "toggle"}, { duration:0 });
	
	$(".popupnewsimg").fancybox({
		openEffect	: 'none',
		closeEffect	: 'none'
	});
});

function printNews(){
	window.print();
	self.close()
}

function contactUs(){
	var cname = document.getElementById("cname").value;
	var cmail = document.getElementById("cmail").value;
	var ctext = document.getElementById("ctext").value;

	if(cname == "" || cname == "Вашите имена"){
		document.getElementById('cstatus').style.display = "block";
		document.getElementById('cstatus').style.backgroundColor = "#ffe3e3";
		document.getElementById("cstatus").style.color = "#d21b1b";
		document.getElementById('cstatus').innerHTML = "Не сте посочили Вашето име!";
		return;
	}
	
	if(cmail == "" || cmail == "Вашият e-mail адрес"){
		document.getElementById('cstatus').style.display = "block";
		document.getElementById('cstatus').style.backgroundColor = "#ffe3e3";
		document.getElementById("cstatus").style.color = "#d21b1b";
		document.getElementById('cstatus').innerHTML = "Не сте посочили e-mail адрес!";
		return;
	} else {
		if ((cmail.indexOf("@") == -1) || (cmail.indexOf(".") == -1)){
			document.getElementById('cstatus').style.display = "block";
			document.getElementById('cstatus').style.backgroundColor = "#ffe3e3";
			document.getElementById("cstatus").style.color = "#d21b1b";
			document.getElementById('cstatus').innerHTML = 'Въвели сте грешен e-mail адрес!';
			return;
		}
	}
	
	if(ctext == ""){
		document.getElementById('cstatus').style.display = "block";
		document.getElementById('cstatus').style.backgroundColor = "#ffe3e3";
		document.getElementById("cstatus").style.color = "#d21b1b";
		document.getElementById('cstatus').innerHTML = "Не сте попълнили Вашият коментар!";
		return;
	}
	
	var params = "name="+cname+"&mail="+cmail+"&text="+ctext;
	methodRequest('Functions/Requests/contacts.php', params, 'contacts', 'POST');
}

function contactsUpdatePage(str){
	if(str == "success"){
		document.getElementById('cstatus').style.display = "block";
		document.getElementById('cstatus').style.backgroundColor = "#c7efc7";
		document.getElementById("cstatus").style.color = "#197422";
		document.getElementById('cstatus').innerHTML = "Вашето заитване е успешно изпратено";
	} else {
		document.getElementById('cstatus').style.display = "block";
		document.getElementById('cstatus').style.backgroundColor = "#ffe3e3";
		document.getElementById("cstatus").style.color = "#d21b1b";
		document.getElementById('cstatus').innerHTML = "Възникна грешка, моля опитайте отново!";
	}
}

function createBookmark(){
	var title = 'Химик Online - електроно издание на "Неохим" АД';
	var url = "http://ns.neochim.bg/";

	if (window.sidebar){
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ){
		window.external.AddFavorite( url, title);
	} else if(window.opera && window.print){
		return true;
	}
}

function searchFeild(){
	var searchvalue = document.getElementById("searchvalue").value;

	if(searchvalue != "търси..." && searchvalue != ""){
		document.forms["search"].submit();
	}
}
