﻿function rand(minimum,maximum)  
 {  
  var losowe = minimum+(Math.random()*(maximum-minimum));  
  return Math.round(losowe);  
}  


/*
// Custom drop action for the country boxes
function dropItems(idOfDraggedItem,targetId,x,y)
{
	var targetObj = document.getElementById(targetId);	// Creating reference to target obj
	var subDivs = targetObj.getElementsByTagName('DIV');	// Number of subdivs
	if(subDivs.length>0 && targetId!='puzzle_od')return;	// Sub divs exists on target, i.e. element already dragged on it. => return from function without doing anything
	var sourceObj = document.getElementById(idOfDraggedItem);	// Creating reference to source, i.e. dragged object
	var numericIdTarget = targetId.replace(/[^0-9]/gi,'')/1;	// Find numeric id of target
	var numericIdSource = idOfDraggedItem.replace(/[^0-9]/gi,'')/1;	// Find numeric id of source
	//aktualna_wartosc = document.getElementById('d_box' + numericIdSource).value; // aktualnie mamy
	//ilosc_wsadzonych = parseInt(document.getElementById('modul_gra_puzzle_wsadzono').value);  // wsadzonych
	alert('aktualna wartosc: ' + document.getElementById('d_box' + numericIdSource).value);
	//wsadzanie = "brak";
	if(numericIdTarget-numericIdSource==100){	// In the html above, there's a difference in 100 between the id of the country and it's capital(example:
												// Oslo have id "box1" and Norway have id "box101", i.e. 1 + 100.
		sourceObj.style.backgroundColor='#0F0';	// Set green background color for dragged object
		document.getElementById('d_box' + numericIdSource).value = '2';  // zaznaczono dobrze
		//alert(document.getElementById('d_box' + numericIdSource).value);
	  //if(aktualna_wartosc == '0') { wsadzanie = "plus"; } else {	wsadzanie = "brak"; }  // wsadzilismy element
	}else{
		sourceObj.style.backgroundColor='';	// Reset back to default white background color
		document.getElementById('d_box' + numericIdSource).value = '1';  // zaznaczono zle
		//alert(document.getElementById('d_box' + numericIdSource).value);
		//if(aktualna_wartosc == '0') { wsadzanie = "plus"; }	else {	wsadzanie = "brak"; }  // wsadzilismy element
	}
	if(targetId=='puzzle_od'){	// Target is the capital box - append the dragged item as child of first sub div, i.e. as child of <div id="dropContent">
		targetObj = targetObj.getElementsByTagName('DIV')[0];	
		//document.getElementById('d_box' + numericIdSource).value = '0';
		
	}
	alert('aktualna wartosc: ' + document.getElementById('d_box' + numericIdSource).value);
	targetObj.appendChild(sourceObj);	// Append	
}
*/


// Custom drop action for the country boxes
function dropItems(idOfDraggedItem,targetId,x,y)
{
	var targetObj = document.getElementById(targetId);	// Creating reference to target obj
	var subDivs = targetObj.getElementsByTagName('DIV');	// Number of subdivs
	if(subDivs.length>0 && targetId!='puzzle_od')return;	// Sub divs exists on target, i.e. element already dragged on it. => return from function without doing anything
	var sourceObj = document.getElementById(idOfDraggedItem);	// Creating reference to source, i.e. dragged object
	var numericIdTarget = targetId.replace(/[^0-9]/gi,'')/1;	// Find numeric id of target
	var numericIdSource = idOfDraggedItem.replace(/[^0-9]/gi,'')/1;	// Find numeric id of source
	
	if(numericIdTarget-numericIdSource==100){	// In the html above, there's a difference in 100 between the id of the country and it's capital(example:
												// Oslo have id "box1" and Norway have id "box101", i.e. 1 + 100.
		//sourceObj.style.backgroundColor='#0F0';	// Set green background color for dragged object
		sourceObj.style.fontWeight='bold';	
		document.getElementById('d_box' + numericIdSource).value = '2';
	}else {
		//sourceObj.style.backgroundColor='';	// Reset back to default white background color	
		sourceObj.style.fontWeight='bold';	
		document.getElementById('d_box' + numericIdSource).value = '1';
	}   
	if(targetId=='puzzle_od'){	// Target is the capital box - append the dragged item as child of first sub div, i.e. as child of <div id="dropContent">
		targetObj = targetObj.getElementsByTagName('DIV')[0];	
		sourceObj.style.fontWeight='normal';	
		document.getElementById('d_box' + numericIdSource).value = '0';
	}
		
	targetObj.appendChild(sourceObj);	// Append	
	
	// sprawdzamy ilosc zaznaczonych itd...
	ilosc_wyrazow = document.getElementById('modul_gra_puzzle_ilosc').value;
	var ilosc_0_status = 0;
	var ilosc_1_status = 0;
	var ilosc_2_status = 0;
	
	for(i=1; i<=ilosc_wyrazow; i++) {
    status_tmp = document.getElementById('d_box' + i).value;
    if(status_tmp == '2') {
      ilosc_2_status = ilosc_2_status + 1;
    } else if(status_tmp == '1') {
      ilosc_1_status = ilosc_1_status + 1;
    } else {
      ilosc_0_status = ilosc_0_status + 1;
    }
	}
	
	ilosc_zaznaczonych = ilosc_1_status + ilosc_2_status;
	
	if(ilosc_zaznaczonych == ilosc_wyrazow) {
    document.getElementById('modul_gra_puzzle_sprawdz').style.display = 'block';
    document.getElementById('modul_gra_puzzle_sprawdz2').style.display = 'block';
	}
}

// przechowuje odwołanie do obiektu XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject(); 

// zwraca obiekt XMLHttpRequest
function createXmlHttpRequestObject() 
{
  // przechowa odwołanie do obiektu XMLHttpRequest
  var xmlHttp;
  // jeśli uruchomiony jest Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // jeśli uruchomiona jest Mozilla lub inne przeglądarki
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // zwraca utworzony obiekt lub wyświetla komunikat o błędzie
  if (!xmlHttp)
    alert("Błąd podczas tworzenia obiektu XMLHttpRequest.");
  else 
    return xmlHttp;
}

// wysyła asynchroniczne żądanie protokołem HTTP korzystając z obiektu XMLHttpRequest
function modul_gra_puzzle_nowa(gdzie,sciezka)
{
  // kontynuuje jedynie jeśli obiekt xmlHttp nie jest zajęty
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // pobiera maila wpisanego przez użytkownika w formularzu
    //name = encodeURIComponent(document.getElementById("modul_newsletter_" + gdzie + "_input").value);
    //name = document.getElementById("modul_newsletter_" + gdzie + "_input").value;'
    //hasz
    hasz = Math.random() * Date.parse(new Date());
    // wykonuje skrypt newslettera
    xmlHttp.open("GET", sciezka + "modul_gra_puzzle.php?gdzie=" + gdzie + "&hasz=" + hasz, true);  
    // definiuje metodę obsługi odpowiedzi serwera
    xmlHttp.onreadystatechange = function(){ handleServerResponse(gdzie); };
    // wysyła żądanie do serwera
    xmlHttp.send(null);
    // komunikat loading
    wysokosc = document.getElementById("modul_gra_puzzle_" + gdzie).offsetHeight;
    document.getElementById("modul_gra_puzzle_" + gdzie).style.height = wysokosc + 'px';
    document.getElementById("modul_gra_puzzle_" + gdzie).innerHTML = '<div style="text-align:center; padding:30px 0px"><img alt="" src="' + sciezka + 'template/loading2.gif" /></div>';
    // ilosc wyrazow zapisujemy
    document.getElementById('modul_gra_puzzle_sprawdz').style.display = 'none';
    document.getElementById('modul_gra_puzzle_sprawdz2').style.display = 'none';
    document.getElementById('modul_gra_puzzle_wynika').style.display = 'none';
    document.getElementById('modul_gra_puzzle_wynika2').style.display = 'none';
    document.getElementById('modul_gra_puzzle_wynikb').style.display = 'none';
    document.getElementById('modul_gra_puzzle_wynikb2').style.display = 'none';
  }
}

// wysyła asynchroniczne żądanie protokołem HTTP korzystając z obiektu XMLHttpRequest
function modul_gra_puzzle_sprawdz()
{
  document.getElementById('modul_gra_puzzle_sprawdz').style.display = 'none';
  document.getElementById('modul_gra_puzzle_sprawdz2').style.display = 'none';
  document.getElementById('puzzle_od').style.display = 'none';
  document.getElementById('puzzle_wyniki').style.display = 'block';
  
  // wyswietlamy poprawne odpowiedzi dla zle zaznaczonych
	ilosc_wyrazow = document.getElementById('modul_gra_puzzle_ilosc').value;
	var ilosc_0_status = 0;
	var ilosc_1_status = 0;
	var ilosc_2_status = 0;
	
	for(i=1; i<=ilosc_wyrazow; i++) {
    status_tmp = document.getElementById('d_box' + i).value;
    if(status_tmp == '2') {
      ilosc_2_status = ilosc_2_status + 1;
      document.getElementById('box' + i).style.color = 'lightgreen';
    } else if(status_tmp == '1') {
      ilosc_1_status = ilosc_1_status + 1;
      document.getElementById('w_box' + i).style.display = 'block';
      document.getElementById('wp_box' + i).style.display = 'none';
      document.getElementById('box' + i).style.color = 'red';
    } else {
      ilosc_0_status = ilosc_0_status + 1;
    }
	}
	
	// tworzymy wynik
	if(ilosc_2_status == ilosc_wyrazow) {
    document.getElementById('modul_gra_puzzle_wynika').style.display = 'block';
    document.getElementById('modul_gra_puzzle_wynika2').style.display = 'block';
	} else {
    document.getElementById('modul_gra_puzzle_wynikb_n1').innerHTML = ilosc_wyrazow;
    document.getElementById('modul_gra_puzzle_wynikb2_n1').innerHTML = ilosc_wyrazow;
    
    if(ilosc_wyrazow == ilosc_1_status) {
      document.getElementById('modul_gra_puzzle_wynikb_n2').innerHTML = '';
      document.getElementById('modul_gra_puzzle_wynikb2_n2').innerHTML = '';
      document.getElementById('modul_gra_puzzle_wynikb_n3').style.display = 'inline';
      document.getElementById('modul_gra_puzzle_wynikb2_n3').style.display = 'inline';
      document.getElementById('modul_gra_puzzle_wynikb_n4').style.display = 'none';
      document.getElementById('modul_gra_puzzle_wynikb2_n4').style.display = 'none';
    } else {
      document.getElementById('modul_gra_puzzle_wynikb_n2').innerHTML = ilosc_1_status;
      document.getElementById('modul_gra_puzzle_wynikb2_n2').innerHTML = ilosc_1_status;
      document.getElementById('modul_gra_puzzle_wynikb_n3').style.display = 'none';
      document.getElementById('modul_gra_puzzle_wynikb2_n3').style.display = 'none';
      document.getElementById('modul_gra_puzzle_wynikb_n4').style.display = 'inline';
      document.getElementById('modul_gra_puzzle_wynikb2_n4').style.display = 'inline';
    }
    document.getElementById('modul_gra_puzzle_wynikb').style.display = 'block';
    document.getElementById('modul_gra_puzzle_wynikb2').style.display = 'block';
	}
	
}

// wykonywana automatycznie po otrzymaniu odpowiedzi z serwera
function handleServerResponse(gdzie) 
{
  // kontynuuje jedynie jeśli transakcja została zakończona
  if (xmlHttp.readyState == 4) 
  {
    // status 200 oznacza pomyślne ukończenie transakcji
    if (xmlHttp.status == 200) 
    {
      // wyodrębnia wiadomość XML wysłaną z serwera
      xmlResponse = xmlHttp.responseText;
      //alert(xmlResponse);
      // pobiera element nadrzędny ze struktury pliku XML
      //xmlDocumentElement = xmlResponse.documentElement;
      // pobiera wiadomość tekstową pierwszego potomka elementu document
      //helloMessage = xmlDocumentElement.firstChild.data;
      //var gdzie = xmlDocumentElement.getElementsByTagName ("gdzie")[0].firstChild.data; 
      //var komunikat = xmlDocumentElement.getElementsByTagName ("komunikat")[0].firstChild.data;
      //var przyciski = xmlDocumentElement.getElementsByTagName ("przyciski")[0].firstChild.data;
      //var blad = xmlDocumentElement.getElementsByTagName ("blad")[0].firstChild.data;
      document.getElementById("modul_gra_puzzle_" + gdzie).style.height = 'auto';
      document.getElementById("modul_gra_puzzle_" + gdzie).innerHTML = xmlResponse;
      
      ile = document.getElementById('modul_gra_puzzle_ilosc').value;
      
      delete dragDropObj;
      
      var dragDropObj = new DHTMLgoodies_dragDrop();	// Creating drag and drop object
      
      for(i=1; i<=ile; i++) {
      numer_drugi = 100 + i;
      dragDropObj.addSource('box' + i,true);
      dragDropObj.addTarget('box' + numer_drugi,'dropItems');
      }

      dragDropObj.addTarget('puzzle_od','dropItems');

      dragDropObj.init();	// Initizlizing drag and drop object

      //ilosc = getElementById("modul_gra_puzzle_ilosc_" + gdzie).value;
      //alert('hehe');
      
    } 
    // dla statusu protokołu HTTP innego niż 200 zgłasza błąd
    else 
    {
      alert("Wystąpił błąd podczas uzyskiwania dostępu do serwera: " + xmlHttp.statusText);
    }
  }
}




