﻿
 //przechowuje obiekt XMLHttpRequest
 var xmlHttp= createXmlHttpRequestObject();

 //tworzy obiekt XMLHtprequest
 function createXmlHttpRequestObject()	{

  // przechowa odwolanie do obiektu XMLHttpRequest
  var xmlHttp;

  // powinno działac dla wszystkich przegladarek z wyjatkiem IE6 i starszych
  try	{

   // probuje utworzyc obiekt XMLHTTpRequest
   xmlHttp= new XMLHttpRequest();

	}
  catch (e)	{

   // w przypadku gdy przeglądarką jest IE6 lub starsza
   var XmlHttpVersions= new Array("MSXML2.XMLHTTP.6.0",
				  "MSXML2.XMLHTTP.5.0",
				  "MSXML2.XMLHTTP.4.0",
				  "MSXML2.XMLHTTP.3.0",
				  "MSXML2.XMLHTTP",
				  "Microsoft.XMLHTTP");

   // sprzawdza każdy identyfikator programu, aż jeden zadziała
   for(var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)	{
  
    try	{

     // probuje utowrzyć obiekt XMLHttpRequest
     xmlHttp= new ActiveXObject(XmlHttpVersions[i]);
	}

    catch (e)	{}
								}

		}

  if(!xmlHttp)	{

   alert("Błąd podczas tworzenia obiektu XMLHttpRequest.");
		}

  else	{
 
   return xmlHttp;
	}
					}



 function rateMessage(mark, id, selector, userid)	{

  if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{

   var params= "mark=" + mark + "&id=" + id + "&selector=" + selector + "&userid=" + userid;
   var ratingArea= document.getElementById("ratingArea" + id);
   ratingArea.innerHtml= "ładuję ...";
   xmlHttp.ratingArea= ratingArea;

   try	{

    xmlHttp.open("POST", "functions/functions.rating.php", true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange= handleRequestStateChange;
    xmlHttp.send(params);
	}
   catch (e)	{

    // w przypadku niepowodzenia wyswietla komunikato bledzie
    alert("Nie mogę połączyć się z serwerem:\n" + e.toString());
		}

								}
			}


 function handleRequestStateChange()	{

  if(xmlHttp.readyState == 4)	{
   if(xmlHttp.status == 200)	{

    try	{
     handleServerResponse();
	}
    catch (e)	{
     alert("Błąd podczas odczytu odpowiedzi: " + e.toString());
		}
				}
   else	{
    alert("Pojawił się problem w uzyskaniu danych:\n" +
	   xmlHttp.statusText);
	}
					}


					}


 function handleServerResponse()	{

  var response= xmlHttp.responseText;
  //myDiv= document.getElementById("ratingArea" + ratingAreaId);

  xmlHttp.ratingArea.innerHTML= response;


					}

function ratingImgSrcOn(i, id)	{

 for(j=1;j<=i;j++)	{
			document.getElementById("rating"+j+id).src= "images/rating_bar_on.gif";		
			}
				}

function ratingImgSrcOff(id)	{

  for(j=1;j<=5;j++)	{
			document.getElementById("rating"+j+id).src= "images/rating_bar.gif";		
			}
				}


// MESSAGES--------------------


 function showMsgSection(section)	{

  announceLoading("lightbox_loading.gif", "Ładuję ...", "messages_content");


  if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{

   var params= "action=show_section&section=" + section ;

   try	{

    xmlHttp.open("POST", "functions/functions.messages.php", true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange= handleMsgSections;
    xmlHttp.send(params);
	}
   catch (e)	{

    // w przypadku niepowodzenia wyswietla komunikato bledzie
    alert("Nie mogę połączyć się z serwerem:\n" + e.toString());
		}

								}
  
					}

// --------------------------------------------------------------------------------------

 function handleMsgSections()	{

  if(xmlHttp.readyState == 4)	{
   if(xmlHttp.status == 200)	{

    try	{
     handleMsgResponse();
	}
    catch (e)	{
     alert("Błąd podczas odczytu odpowiedzi: " + e.toString());
		}
				}
   else	{
    alert("Pojawił się problem w uzyskaniu danych:\n" +
	   xmlHttp.statusText);
	}
				}

				}

// --------------------------------------------------------------------------------------

 function handleMsgResponse()	{

  var response= xmlHttp.responseText;
  var msgArea= document.getElementById("messages_content");

  msgArea.innerHTML= response;

				}

// --------------------------------------------------------------------------------------


 function showMessage(msg_id)		{
  
  announceLoading("lightbox_loading.gif", "Ładuję ...", "messages_content");

  if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{

   var params= "action=show_message&msg_id=" + msg_id;

   try	{

    xmlHttp.open("POST", "functions/functions.messages.php", true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange= handleMsgSections;
    xmlHttp.send(params);
	}
   catch (e)	{

    // w przypadku niepowodzenia wyswietla komunikato bledzie
    alert("Nie mogę połączyć się z serwerem:\n" + e.toString());
		}

								}
  
					}

// --------------------------------------------------------------------------------------

function chceckSelectedMsgs(section)		{

 var ids= document.getElementsByName("msgs_array[]");
 var counter= ids.length;
 var msgsarray= new Array();

 for (var i=0;i<counter;i++)	{

    if(ids[i].checked== true)	{msgsarray.push(ids[i].value);}

				}

 if(msgsarray.length > 0)	{

   deleteMessages(msgsarray, section);
				}

					}

// --------------------------------------------------------------------------------------

 function deleteMessages(msgsarray, section)		{

//alert(typeof msgsarray);

  announceLoading("lightbox_loading.gif", "Ładuję ...", "messages_content");

  if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{

   var params= "action=delete_messages&msgsarray=" + msgsarray + "&show_section=" + section;

   try	{

    xmlHttp.open("POST", "functions/functions.messages.php", true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange= handleMsgDelete;
    xmlHttp.send(params);
	}
   catch (e)	{

    // w przypadku niepowodzenia wyswietla komunikato bledzie
    alert("Nie mogę połączyć się z serwerem:\n" + e.toString());
		}

								}
  
							}

// --------------------------------------------------------------------------------------

 function handleMsgDelete()	{

  if(xmlHttp.readyState == 4)	{
   if(xmlHttp.status == 200)	{

    try	{
     handleMsgDelResponse();
	}
    catch (e)	{
     alert("Błąd podczas odczytu odpowiedzi: " + e.toString());
		}
				}
   else	{
    alert("Pojawił się problem w uzyskaniu danych:\n" +
	   xmlHttp.statusText);
	}
				}

				}

// --------------------------------------------------------------------------------------

 function handleMsgDelResponse()	{

  var response= xmlHttp.responseText;
  var msgArea= document.getElementById("msgbox_container");

  msgArea.innerHTML= response;

					}


// --------------------------------------------------------------------------------------

 function sendMessage(title, id, content)		{

  var errormsg= "Popraw błędy:\n";
  var flag=0;
  if(title.length < 5)	{errormsg= errormsg + "- temat musi zawierać przynajmniej 5 znaków\n"; flag=1; }
  if(content.length < 10)	{errormsg= errormsg + "- tekst musi zawirać przynajmniej 10 znaków\n"; flag=1;}

  if(flag== 1)	{
  alert(errormsg);
		}
  else	{

   var msgArea= document.getElementById("messages_content");
   msgArea.innerHTML= "<p style='text-align: center;'><img src='../images/lightbox_loading.gif'></p>";

  if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{

   var params= "action=send_message&msg_title=" + title + "&msg_content=" + content + "&msg_recipient_id=" +id;

   try	{

    xmlHttp.open("POST", "functions/functions.messages.php", true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange= handleMsgDelete;
    xmlHttp.send(params);
	}
    catch (e)	{

    // w przypadku niepowodzenia wyswietla komunikato bledzie
    alert("Nie mogę połączyć się z serwerem:\n" + e.toString());
		}

								}
	} // else
  
						}



 function announceLoading(pic, msg, id)	{

  var selectedArea= document.getElementById(id);
  var picture= (pic != "" ? "<img src='../images/" + pic +"'>":"");
  var message= (msg != "" ? "<br /><br />" + msg:"");

  selectedArea.innerHTML= "<p style='text-align: center;'>" + picture + message + "</p>";

					}



