/* AJAX >>>
	Движок ajax, выполняемый различные типы ajax-запросов
    v. 1.00
    coryright Alex "Hoochie Men" Melikhov
*/

	var arr_list = new Array();





// Функция загружает html код из файла url в элемент div
function ajaxLoadHTML(url,qstring,div,code_ev) {

	window.status = "Запрос"; if ((div != "no_div") & ( url != "ajax/empty.php")){document.getElementById(div).innerHTML = "<p><b>Загрузка ..</b></p>";};
	var http_request = false;

		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
			http_request = new XMLHttpRequest();
				if (http_request.overrideMimeType) {
					http_request.overrideMimeType('text/html');
				}
			} else if (window.ActiveXObject) { // IE мля ...
			try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
			}

		if (!http_request) {
			window.status = "Ошибка Ajax запроса, Невозможно создать экземпляр класса XMLHTTP";
		return false;
		}


		http_request.onreadystatechange = function() { alertContents(http_request,div);};
		if (qstring != ''){url = url+'?'+qstring;}

		http_request.open('GET', url, true);	//alert(url);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded, charset=utf-8');
		http_request.setRequestHeader('pragma', 'no-cache');
		//http_request.overrideMimeType('text/html');
		http_request.send(null);


		function alertContents(http_request,div) {	
        		try {
		if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//alert(http_request.responseText);
				if (div != "no_div"){document.getElementById(div).innerHTML = http_request.responseText;}

	 		var http_request = false;
		window.status = "Ok";
				if (code_ev != ''){eval(code_ev);}
			var http_request = false;
		} else {      document.getElementById(div).innerHTML = "<p>Произошла Ошибка</p>";
				window.status = "Ошибка Ajax запроса";
				var http_request = false;
		}
		}
			} catch ( e ){
            			window.status = 'Произошло исключение: ' + e.description;
			}
	}

}






	function get_buyer_info(id){

		for (n=1; n<=num_of_buyers; n++){
			document.getElementById("buy_lnk_"+n).style.color="#2b5c6a";
		}
		document.getElementById("buy_lnk_"+id).style.color="#ea0b8c";
		ajaxLoadHTML("ajax/get_buyer_info.php","id="+id,"div_mid","");
	}

	function main_change_to_authors(){
		document.getElementById("btn_cal").style.color="#2b5c6a";
		document.getElementById("btn_aut").style.color="#ea0b8c";

		divEmpty("div_right");
		ajaxLoadHTML("ajax/get_authors.php","letter=star","div_mid","");	// при попадании в участников
		ajaxLoadHTML("ajax/authors_alfaomega.php","","div_left","");
	}


	function main_change_to_calendar(m,d){
		document.getElementById("btn_aut").style.color="#2b5c6a";
		document.getElementById("btn_cal").style.color="#ea0b8c";

		divEmpty("div_mid");
		
		//divEmpty("div_left_info");
 		if (d == ''){code_ev = "get_card_by_day(\""+m+"-01\")";}else{code_ev = "get_card_by_day(\""+m+"-"+d+"\")";}
		ajaxLoadHTML("ajax/calendar_it.php","","div_left",code_ev);

	}

	function get_cal (m){
		divEmpty("div_left_info");
		//divEmpty("div_mid");
		divEmpty("div_right");
		ajaxLoadHTML("ajax/calendar_it.php","m="+m,"div_left","get_card_by_day(\""+m+"-01\")");
		
	}

	function get_authors(letter){
		divEmpty("div_left_info");
		//divEmpty("div_mid");
		divEmpty("div_right");
		var let = new Array(54);
			let["A"] = 1;let["B"] = 2;let["C"] = 3;let["D"] = 4;let["E"] = 5;let["F"] = 6;
			let["G"] = 7;let["H"] = 8;let["I"] = 9;let["J"] = 10;let["K"] = 11;let["L"] = 12;
			let["M"] = 13;let["N"] = 14;let["O"] = 15;let["P"] = 16;let["Q"] = 17;let["R"] = 18;
			let["S"] = 19;let["T"] = 20;let["U"] = 21;let["V"] = 22;let["W"] = 23;let["X"] = 24;
			let["Y"] = 25;let["Z"] = 26;

 			let["rus_a"] = 27;let["rus_b"] = 28;let["rus_v"] = 29;let["rus_g"] = 30;let["rus_d"] = 31;let["rus_e"] = 32;
 			let["rus_zh"] = 33;let["rus_z"] = 34;let["rus_i"] = 35;let["rus_k"] = 36;let["rus_l"] = 37;let["rus_m"] = 38;
 			let["rus_n"] = 39;let["rus_o"] = 40;let["rus_p"] = 41;let["rus_r"] = 42;let["rus_s"] = 43;let["rus_t"] = 44;
 			let["rus_u"] = 45;let["rus_f"] = 46;let["rus_kh"] = 47;let["rus_ts"] = 48;let["rus_ch"] = 49;let["rus_sh"] = 50;
 			let["rus_shc"] = 51;let["rus_ie"] = 52;let["rus_iu"] = 53;let["rus_ia"] = 54;

			document.getElementById("let_star").src="./img/design/star_blue.gif";

		for (n=1; n<=let.length; n++){
			document.getElementById("let_"+n).style.color="#2b5c6a";
		}
		if (letter != "star"){
			document.getElementById("let_"+let[letter]).style.color="#ea0b8c";
		}
		else{
			document.getElementById("let_star").src="./img/design/star.gif";
		}
		ajaxLoadHTML("ajax/get_authors.php","letter="+letter,"div_mid","");
	}

	function select_author(id,num){
		for (n=0;n<=100;n++){
			if (document.getElementById("auth_"+n)){document.getElementById("auth_"+n).style.color="#2b5c6a";}
		}
		document.getElementById("auth_"+num).style.color="#ea0b8c";

		get_works_by_author(id);
	}

	function get_works_by_author(id){
		ajaxLoadHTML("ajax/get_works_by_author.php","id="+id,"div_right","");
		ajaxLoadHTML("ajax/get_card_info.php","author_id="+id,"div_left_info","");
	}

	function get_card_by_day(date){
		ajaxLoadHTML("ajax/get_card_by_day.php","date="+date,"div_mid","");
		ajaxLoadHTML("ajax/get_card_info.php","date="+date,"div_left_info","");
		ajaxLoadHTML("ajax/get_somecard_by_day.php","date="+date,"div_right","");
		for (n = 1; n <= 31; n++){
				if (n <= 9){
			if (document.getElementById("day_"+date.substring(0,2)+"-0"+n)){	
				document.getElementById("day_"+date.substring(0,2)+"-0"+n).style.background = "#fff;";
				document.getElementById("day_"+date.substring(0,2)+"-0"+n).style.color = "#2b5c6a;";
			}
			}else{
			if (document.getElementById("day_"+date.substring(0,2)+"-"+n)){	
				document.getElementById("day_"+date.substring(0,2)+"-"+n).style.background = "#fff;";
				document.getElementById("day_"+date.substring(0,2)+"-"+n).style.color = "#2b5c6a;";
			}
			}
		}
		document.getElementById("day_"+date).style.background = "#fff url('img/design/cal_currday.gif') top left no-repeat;";
		document.getElementById("day_"+date).style.color = "#fff;";
		//divEmpty("div_right");

	}

	function get_cards(id,aid){ // authors
		ajaxLoadHTML("ajax/get_cards.php","card_id="+id+"&author_id="+aid,"div_right","");
		ajaxLoadHTML("ajax/get_card_info.php","id="+id,"div_left_info","");
	}

	function get_card(id){ // calend
		ajaxLoadHTML("ajax/get_card.php","id="+id,"div_right","");
	}

	function divEmpty(div){
		ajaxLoadHTML("ajax/empty.php","",div,"");
	}


    function ansp(user, domain) {
    	window.location.href='mailto:'+user+'@'+domain+'';
    }


// Функция функция посылает post-ом данные из формы в элемент div
function ajaxSendPOST(url,qstring,div,post_data) {

	var http_request = false;

		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
			http_request = new XMLHttpRequest();
				if (http_request.overrideMimeType) {
					http_request.overrideMimeType('text/html');
				}
			} else if (window.ActiveXObject) { // IE
			try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
			}

		if (!http_request) {
			window.status = "Ошибка Ajax запроса, Невозможно создать экземпляр класса XMLHTTP";
		return false;
		}
            	//http_request.overrideMimeType('text/xml');
	            //http_request.overrideMimeType('text/html');

		http_request.onreadystatechange = function() { alertContents(http_request,div);};
		if (qstring != ''){url = url+'?'+qstring;}

		http_request.open('POST', url, true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded, charset=utf-8');
		http_request.setRequestHeader('pragma', 'no-cache');
		http_request.send(post_data);


		function alertContents(http_request,div) {
		if (http_request.readyState == 4) {
		if (http_request.status == 200) {
				if (div != "no_div"){document.getElementById(div).innerHTML = http_request.responseText;}
	 		var http_request = false;
		window.status = "Ok";
			var http_request = false;
		} else {      document.all[div].innerHTML = "<p class=bad>Произошла Ошибка</p>";
		window.status = "Ошибка Ajax запроса";
			var http_request = false;
		}
		}
	}


}

	// Отправка данных с формы POST
	function tmp_sendPost(url,div,fmID,eval_code){

		fm = document.forms[fmID].elements;
		post_data = '';
		for (n=0;n<fm.length;n++){
			if (fm[n].type != "button")	{
					post_data = post_data + fm[n].name + "=" + encodeURI( fm[n].value ) + "&";
				}
		}
		ajaxSendPOST(url,'',div,post_data);
		if (eval_code != '') {eval(eval_code);}
	}

	// Подгружает дополнительный .js файл
	function add_js(url){
    		var p_script = document.createElement("script");
		this.head = document.getElementsByTagName("head")[0];
    		p_script.setAttribute("src", url + "?" + Math.random());// добавление случайного числа для обмана кэша
    		p_script.setAttribute("type", "text/javascript");
    		this.head.appendChild(p_script);
	}



