// var root_url = 'http://'+location.host+'/zvb2/';
var root_url = 'http://'+location.host+'/';

// var modul_url = 'http://'+location.host+'/zvb2/_application/automation/ajax_methods/worksheet_check/index.php';
var modul_url = 'http://'+location.host+'/_application/automation/ajax_methods/worksheet_check/index.php';

// var kerdes_valasz_tarol_url = 'http://'+location.host+'/zvb2/_application/automation/ajax_methods/assoc_kerdes_valasz/index.php';
var kerdes_valasz_tarol_url = 'http://'+location.host+'/_application/automation/ajax_methods/assoc_kerdes_valasz/index.php';

// var sarokpont_url = 'http://'+location.host+'/zvb2/_application/automation/ajax_methods/sarokpont/index.php';
var sarokpont_url = 'http://'+location.host+'/_application/automation/ajax_methods/sarokpont/index.php';

// var kepforgato_url = 'http://'+location.host+'/zvb2/_application/automation/ajax_methods/kepforgato/index.php';
var kepforgato_url = 'http://'+location.host+'/_application/automation/ajax_methods/kepforgato/index.php';

// var loadtodb_url = 'http://'+location.host+'/zvb2/_application/automation/loadtodb/index.php';
var loadtodb_url = 'http://'+location.host+'/_application/automation/loadtodb/index.php';

// var reg_karok = 'http://'+location.host+'/zvb2/_application/automation/ajax_methods/reg_karok_cserelo/index.php';
var reg_karok = 'http://'+location.host+'/_application/automation/ajax_methods/reg_karok_cserelo/index.php';

// var torles_url = 'http://'+location.host+'/zvb2/_application/automation/ajax_methods/tesztlap_adatlap_torles/index.php';
var torles_url = 'http://'+location.host+'/_application/automation/ajax_methods/tesztlap_adatlap_torles/index.php';

var http_request;
var result_format = 'xml';

var worksheet_count = 0;
var act_worksheet_num = 0;
var correct_count = 0;
var uncorrect_count = 0;

function createHTTPRequest(eventhandler){
	 http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		 http_request = new XMLHttpRequest();
			// http_request.setRequestHeader("Content-Type", "text/xml");
	} 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) {
		 alert('Cannot create XMLHTTP instance');
		 return false;
	}
	http_request.onreadystatechange = function(){ handleAjaxResponse(eventhandler); }
}

function handleAjaxResponse(eventhandler){
	var results;
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			if (result_format=='json'){
				eval(eventhandler+('http_request.responseText'));
			} else {
				eval(eventhandler+'(http_request.responseXML);');
			}
		}
	}
}

function getResultException(){
	ex=http_request.responseXML.documentElement.getElementsByTagName('exception');
	if (ex[0]){
		item = ex[0].getElementsByTagName('message')[0].firstChild.nodeValue;
		attr = ex[0].getElementsByTagName('message')[0].attributes.getNamedItem("id").nodeValue;
		ret_datas = new Array(item,attr);
		return ret_datas;
		// return 'Exception: '+ex[0].getElementsByTagName('message')[0].firstChild.nodeValue;
	}
	else return '';
}

function checkCombos(){
	if(document.getElementsByName('vizsgafeladatsor_admin[lk_feladatgyujtemeny_nev]')[0].value == '' || document.getElementsByName('vizsgafeladatsor_admin[lk_feladatgyujtemeny_nev]')[0].value == '---'){
		alert('Nincs feladatgyűjtemény választva!');
		return false;
	}
	else if(document.getElementsByName('vizsgafeladatsor_admin[l_nyelv_nev]')[0].value == '' || document.getElementsByName('vizsgafeladatsor_admin[l_nyelv_nev]')[0].value == '---'){
		alert('Nem választottál nyelvet!');
		return false;
	}
	else return true;
}

function checkWorksheet(){
	if(checkCombos())
		checkExerciseId('worksheet_result');
}

function checkExerciseId(eventhandler){
	createHTTPRequest(eventhandler);
	paramstr = 'id='+document.getElementById('check_value_' + act_worksheet_num).innerHTML+'&tag_id=check_symbol_' + act_worksheet_num+'&fgyujtemeny_id=' + document.getElementsByName('vizsgafeladatsor_admin[lk_feladatgyujtemeny_nev]')[0].value;
	// alert(modul_url+'/?ret_format='+result_format+'&'+paramstr);
	http_request.open('GET', modul_url+'/?ret_format='+result_format+'&'+paramstr, true);
	http_request.send(null);
}


function worksheet_result(response){
	var str = getResultException();
	
	if (str==''){
		items = response.documentElement.getElementsByTagName('datas')[0].getElementsByTagName('data')[0].firstChild.nodeValue;
		attr = response.documentElement.getElementsByTagName('datas')[0].getElementsByTagName('data')[0].attributes.getNamedItem("id").nodeValue;
		document.getElementById(attr).className = 'correct';
		correct_count++;
		document.getElementById('correct_count').innerHTML = correct_count;
	}
	else{
		document.getElementById(str[1]).className = 'uncorrect';
		document.getElementById(str[1]).innerHTML = '<span class="uncorrect_text">'+str[0]+'</span>';
		uncorrect_count++;
		document.getElementById('uncorrect_count').innerHTML = uncorrect_count;
	}
	
	if((act_worksheet_num+1) < worksheet_count){
		act_worksheet_num = act_worksheet_num + 1;
		checkWorksheet();
	}
	else if(uncorrect_count>0){
		document.getElementById('CPPSubmitControl_okbutton').onclick=function(){alert('Hibás adatok!'); return false;};
	}
	else{
		document.getElementById('CPPSubmitControl_okbutton').onclick=function(){this.childNodes[1].click();};
	}
}

function checkTextAreaValue(p_name){
	if(checkCombos()==false) return false;
	else if(strip_tags(document.getElementsByName(p_name)[0].value) == ''){
		alert('Nem adtál meg kérdés azonosítót!');
		return false;
	}
	else{
		return true;
	}
}

function cleanTextArea(p_this,p_worksheet_list_id){
	var duplikacios_tomb = new Array();
	var pieces = p_this.value.split("\n");
	var len = pieces.length;
	var cleaned_value = '';
	
	for (var i = 0; i < len; i++){
		duplikacios_hiba = true;
		if(strip_tags(pieces[i]) != ''){
			for (var j = 0; j < duplikacios_tomb.length; j++){
				if(duplikacios_tomb[j]==pieces[i]){
					alert('Az alábbi már volt a listában: ' + pieces[i]);
					duplikacios_hiba = false;
				}
			}
			
			if(duplikacios_hiba){
				duplikacios_tomb_hossz = duplikacios_tomb.length;
				duplikacios_tomb[duplikacios_tomb_hossz] = pieces[i];
				cleaned_value = cleaned_value + pieces[i] + "\n";
			}
		}
	}
	// alert(duplikacios_tomb);
	//alert(cleaned_value.substr((cleaned_value.length)-2,cleaned_value.length));
	cleaned_value = cleaned_value.substr(0,(cleaned_value.length)-1);
	// cleaned_value = strip_tags(cleaned_value);
	p_this.value = cleaned_value;
	// alert(cleaned_value);
}

function changeWorksheet(p_this,p_worksheet_list_id){
	/*
http://whale/zvb2/_application/automation/ajax_methods/worksheet_check/index.php/?ret_format=xml&id=PSY-5.0&tag_id=check_symbol_0&fgyujtemeny_id=1
	
NET-13.0
BGY-1.63
BGY-1.66
BGY-1.92
BGY-2.10
BGY-2.20
BGY-2.37
BGY-2.65
BGY-4.15
BGY-4.32
BGY-4.49
	*/
	
	cleanTextArea(p_this,p_worksheet_list_id);
	
	if(document.getElementById('worksheet_table')!=null)
		document.getElementById(p_worksheet_list_id).removeChild(document.getElementById('worksheet_table'));
	
	var pieces = p_this.value.split("\n");
	var len = pieces.length;
	
	if(len == 1 && strip_tags(pieces[0]) == '') return false;
	
	var objTable = document.createElement("table");
	objTable.setAttribute('id','worksheet_table');
	
	for (var i = 0; i < len; i++){
		var objTR = document.createElement("tr");
		objTable.appendChild(objTR);
		
		var objTD = document.createElement("td");
		objTR.appendChild(objTD);
		
		var objDIV = document.createElement("div");
		objDIV.setAttribute('id','check_value_' + i);
		objTD.appendChild(objDIV);
		
		var objText = document.createTextNode(pieces[i]);
		objDIV.appendChild(objText);
		
		var objTD = document.createElement("td");
		objTR.appendChild(objTD);
		
		var objDIV = document.createElement("div");
		objDIV.setAttribute('id','check_symbol_' + i);
		objDIV.setAttribute('class','unchecked');
		objTD.appendChild(objDIV);
		
	}
	
	document.getElementById('CPPSubmitControl_okbutton').onclick=function(){alert('Not checked datas!'); return false;};
	
	act_worksheet_num = 0;
	document.getElementById('correct_count').innerHTML = 0; correct_count = 0;
	document.getElementById('uncorrect_count').innerHTML = 0; uncorrect_count = 0;
	worksheet_count = len;
	document.getElementById(p_worksheet_list_id).appendChild(objTable);
}

function azonositoEllenorzes(){
	sorszam_combo = document.getElementsByName('vizsgafeladat[l_feladat_csorszam]')[0];
	lista = document.getElementById('vizsgafeladat_kerdes_litas').value;
	kerdesek = lista.split(",");
	
	len = kerdesek.length;
	for (var i = 0; i < len; i++){
		if(kerdesek[i]==sorszam_combo.value){ alert('Már szerepel a listában!'); return false; }
	}
	
	//alert(sorszam_combo.value + '------' + kerdesek);
	return true;
}

//---------------------------------------------------ASSZICIÁCIÓS FELADATOK KEZELÉSE---------------------------------------------------

function assocKerdesValaszTarolas(p_tetel_combo_id, p_kerdes_id, p_tetel_container_id){
	if(document.getElementById(p_tetel_combo_id).selectedIndex == 0){
		alert('Nincs kérdés kiválasztva!');
		return false;
	}
	adatokTarolasa('kerdesAdatokVisszairasa', document.getElementById(p_tetel_combo_id).options[document.getElementById(p_tetel_combo_id).selectedIndex].value, p_kerdes_id, p_tetel_container_id);
}

function adatokTarolasa(eventhandler,p_tetel_id,p_kerdes_id, p_tetel_container_id){
	createHTTPRequest(eventhandler);
	paramstr = '&tetel_id='+p_tetel_id+'&kerdes_id=' + p_kerdes_id + '&tetel_container_id='+p_tetel_container_id;
	/* http://whale/zvb2/_application/automation/ajax_methods/assoc_kerdes_valasz/index.php/?ret_format=xml&&tetel_id=299&kerdes_id=79&tetel_container_id=assoc_tetelek_2_container */
	// alert(kerdes_valasz_tarol_url+'/?ret_format='+result_format+'&'+paramstr);
	http_request.open('GET', kerdes_valasz_tarol_url+'/?ret_format='+result_format+'&'+paramstr, true);
	http_request.send(null);
}

function kerdesAdatokVisszairasa(response){
	var str = getResultException();

	if (str==''){
		items = response.documentElement.getElementsByTagName('datas')[0].getElementsByTagName('data')[0].firstChild.nodeValue;
		attr = response.documentElement.getElementsByTagName('datas')[0].getElementsByTagName('data')[0].attributes.getNamedItem("container_id").nodeValue;
		
		div = document.createElement("DIV");
		div.className = 'assoc_kerdes';
		div.innerHTML = items;
		document.getElementById(attr).appendChild(div);
	}
	else{
		alert(str[0]);
	}
}

//---------------------------------------------------SAROKPONTOK KEZELÉSE---------------------------------------------------

function sarokpontFeldolgozas(p_tesztlap_id, p_x1, p_y1, p_x2, p_y2, p_x3, p_y3, p_x4, p_y4){
	sarokpontElkuldese('sarokpontVisszaigazolas', p_tesztlap_id, p_x1, p_y1, p_x2, p_y2, p_x3, p_y3, p_x4, p_y4);
}

function sarokpontElkuldese(eventhandler, p_tesztlap_id, p_x1, p_y1, p_x2, p_y2, p_x3, p_y3, p_x4, p_y4){
	createHTTPRequest(eventhandler);
	paramstr = 'tesztlap_id=' + p_tesztlap_id + '&x1=' + p_x1 + '&y1=' + p_y1 + '&x2=' + p_x2 + '&y2=' + p_y2 + '&x3=' + p_x3 + '&y3=' + p_y3 + '&x4=' + p_x4 + '&y4=' + p_y4;
	/*
	http://whale/zvb2/_application/automation/ajax_methods/sarokpont/index.php/?ret_format=xml&tesztlap_id=255&x1=170&y1=364&x2=570&y2=3150&x3=1347&y3=382&x4=1767&y4=2055
	*/
	// alert(sarokpont_url+'/?ret_format='+result_format+'&'+paramstr);
	http_request.open('GET', sarokpont_url+'/?ret_format='+result_format+'&'+paramstr, true);
	http_request.send(null);
}

function sarokpontVisszaigazolas(response){
	var str = getResultException();

	if (str==''){
		items = response.documentElement.getElementsByTagName('datas')[0].getElementsByTagName('data')[0].firstChild.nodeValue;
		document.getElementById('detektalas_eredmeny').innerHTML = items;
		document.getElementById('detektalas_eredmeny').style.display = 'inline';
		pontokTorlese();
	}
	else{
		alert(str[0]);
		document.getElementById('detektalas_eredmeny').innerHTML = str[0];
		document.getElementById('detektalas_eredmeny').style.display = 'inline';
		pontokTorlese();
	}
}

//---------------------------------------------------------KÉP FORGATÁSA---------------------------------------------------------

function kepForgatasa(p_url,p_irany,p_id,p_tablanev){
	forgatasiAdatokElkuldese('forgatottKepFrissitese', p_url, p_irany, p_id, p_tablanev, 'feltolto');
}

function forgatasiAdatokElkuldese(eventhandler, p_url, p_irany, p_id, p_tablanev, p_tipus){
	createHTTPRequest(eventhandler);
	paramstr = 'url=' + p_url + '&irany=' + p_irany + '&id=' + p_id + '&tablanev=' + p_tablanev + '&tipus=' + p_tipus;
	/*
	http://whale/zvb2/_application/automation/ajax_methods/kepforgato/index.php/?ret_format=xml&url=../&irany=bal&id=259&tablanev=tesztlap
	http://whale/zvb2/_application/automation/ajax_methods/kepforgato/index.php/?ret_format=xml&url=../&irany=jobb&id=259&tablanev=tesztlap
	http://whale/zvb2/_application/automation/ajax_methods/kepforgato/index.php/?ret_format=xml&url=../&irany=forgat&id=259&tablanev=tesztlap
	*/
	// alert(kepforgato_url+'/?ret_format='+result_format+'&'+paramstr);
	http_request.open('GET', kepforgato_url+'/?ret_format='+result_format+'&'+paramstr, true);
	http_request.send(null);
}

function forgatottKepFrissitese(response){
	var str = getResultException();

	if (str==''){
		items = response.documentElement.getElementsByTagName('datas')[0].getElementsByTagName('data')[0].firstChild.nodeValue;
		// alert(items);
		window.location.href=window.location.href;
	}
	else{
		alert(str[0]);
	}
}

function kepForgatasaHibasak(p_url,p_irany,p_id,p_tablanev,p_tipus){
	// document.getElementById('adminform').submit();
	forgatasiAdatokElkuldese('forgatottKepFrissiteseHibasak', p_url, p_irany, p_id, p_tablanev, p_tipus);
}
function forgatottKepFrissiteseHibasak(response){
	var str = getResultException();

	if (str==''){
		items = response.documentElement.getElementsByTagName('datas')[0].getElementsByTagName('data')[0].firstChild.nodeValue;
		// alert(items);
		document.getElementById('adminform').submit();
	}
	else{
		alert(str[0]);
	}
}

//---------------------------------------------------------LATTICE ADATOK TÁROLÁSA---------------------------------------------------------

function getResultExceptionChanger(){
	ex=http_request.responseXML.documentElement.getElementsByTagName('exception');
	if (ex[0]){
		return 'Exception: '+ex[0].getElementsByTagName('message')[0].firstChild.nodeValue;
	}
	else return '';
}

function ajaxLoadToDB(p_this,p_method_tag_id,p_test_id){
	var update_rows_data = '';
	for (var i = 0; i < test_modify_datas.length; i++){
		if(update_rows_data == '') update_rows_data = String(test_modify_datas[i]);
		else update_rows_data = update_rows_data + '_' + String(test_modify_datas[i]);
	}
	
	// http://whale/zvb2/_application/automation/loadtodb/index.php?ret_format=xml&update_rows_data=ctrl,427149,-1,,6,2,4_ctrl,427149,-1,,6,2,3,4_none,427148,1,12376,5,1&tesztlap_id=192
	var params = 'ret_format='+result_format+'&update_rows_data='+update_rows_data+'&tesztlap_id='+p_test_id;
	// alert(loadtodb_url+'?'+params);
	createHTTPRequest('table_update_result');
	var ajax_ret = http_request.open('POST', loadtodb_url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", params.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(params);
	
	// if(ajax_ret==true){
		// document.getElementById(p_method_tag_id).value = 'ok';
		// document.check_test.submit();
	// }
	// else{
		// document.getElementById(p_method_tag_id).value = 'ok';
		// document.check_test.submit();
	// }
}

function table_update_result(response){
	var str = getResultExceptionChanger();
	if (str==''){
		// alert('a');
		if(document.getElementById('tesztlap_ok_url').value != ''){
			document.getElementById('lattice_tarolas_eredmenye').style.display = 'none';
			window.location.href = document.getElementById('tesztlap_ok_url').value;
		}
		else{
			document.getElementById('lattice_tarolas_eredmenye').style.display = 'none';
			window.location.href = root_url + 'admin/?adminmenu[adminmenu][panel]=lapok_karbantartasa&adminmenu[adminmenu][item]=tesztlap';
		}
		// return true;
	}
	else{
		// alert('b');
		document.getElementById('lattice_tarolas_eredmenye').innerHTML = 'Nem sikerült az adatokat letárolni!';
		document.getElementById('lattice_tarolas_eredmenye').style.display = 'inline';
		// return false;
	}
}

function ajaxCancel(p_this,p_method_tag_id,p_test_id){
	if(document.getElementById('tesztlap_cancel_url').value != ''){
		window.location.href = document.getElementById('tesztlap_cancel_url').value;
	}
	else{
		document.getElementById(p_method_tag_id).value = 'cancel';
		document.check_test.submit();
	}
}

//---------------------------------------------------------FELTÖLTÉSEK TÖRLÉSE---------------------------------------------------------

function feltoltesekTorlese(p_id, p_csomagnev, p_tipus){
	torlesiAdatokElkuldese('feltoltottListaFrissitese', p_id, p_csomagnev, p_tipus);
}

function torlesiAdatokElkuldese(eventhandler, p_id, p_csomagnev, p_tipus){
	createHTTPRequest(eventhandler);
	paramstr = 'id=' + p_id + '&csomagnev=' + p_csomagnev + '&tipus=' + p_tipus;
	/*
	http://whale/zvb2/_application/automation/ajax_methods/tesztlap_adatlap_torles/index.php/?ret_format=xml&id=&csomagnev=GYTK_HU_18_tesztlap123&tipus=2
	http://whale/zvb2/_application/automation/ajax_methods/tesztlap_adatlap_torles/index.php/?ret_format=xml&id=a_timeline04.jpg&csomagnev=GYTK_HU_18_tesztlap123&tipus=2
	*/
	// alert(torles_url+'/?ret_format='+result_format+'&'+paramstr);
	http_request.open('GET', torles_url+'/?ret_format='+result_format+'&'+paramstr, true);
	http_request.send(null);
}

function feltoltottListaFrissitese(response){
	var str = getResultException();

	if (str==''){
		alert('orulunk');
	}
	else{
		alert(str[0]);
	}
}

//---------------------------------------------------------REGISZTÁCIÓ KAROK PANELLISTA CSERÉLŐ---------------------------------------------------------

function karokCsere(p_this){
	p_combo_value = p_this.options[p_this.selectedIndex].value;
	karokCsereMeghivas('karokVisszairasa', p_combo_value);
}

function karokCsereMeghivas(eventhandler, p_combo_value){
	createHTTPRequest(eventhandler);
	paramstr = 'combo_value=' + p_combo_value;
	/*
	http://whale/zvb2/_application/automation/ajax_methods/reg_karok_cserelo/index.php/?ret_format=xml&combo_value=3
	*/
	// alert(reg_karok+'/?ret_format='+result_format+'&'+paramstr);
	http_request.open('GET', reg_karok+'/?ret_format='+result_format+'&'+paramstr, true);
	http_request.send(null);
}

function karokVisszairasa(response){
	var str = getResultException();

	var szamlalo = 0;
	if (str==''){
		document.getElementById('regisztracio_karok_lista').innerHTML = '';
		
		items=response.documentElement.getElementsByTagName('datas')[0];
		var container = document.getElementById('regisztracio_karok_lista');
		
		var objSELECT = document.createElement("select");
		objSELECT.setAttribute('size','1');
		objSELECT.setAttribute('name','registration[egyetem_kar]');
		container.appendChild(objSELECT);
		
		for (i=0;i<items.childNodes.length;i++){
			if(navigator.appName=='Microsoft Internet Explorer'){
				szamlalo++
				value = items.childNodes[i].firstChild.nodeValue;
				attr_id = items.childNodes[i].getAttribute("id");
				
				/*var objDIV = document.createElement("div");
				container.appendChild(objDIV);

				var objCheck = document.createElement("input");
				objCheck.setAttribute('type','checkbox');
				objCheck.setAttribute('name','registration[egyetem_kar]['+szamlalo+']');
				objCheck.setAttribute('value',attr_id);
				objDIV.appendChild(objCheck);

				var objSPAN = document.createElement("span");
				objDIV.appendChild(objSPAN);

				var objText = document.createTextNode(value);
				objSPAN.appendChild(objText);*/
				
				objSELECT.add(new Option(value, attr_id));
			}
			else{
				if(items.childNodes[i] == '[object Element]'){
					szamlalo++
					value = items.childNodes[i].firstChild.nodeValue;
					attr_id = items.childNodes[i].getAttribute("id");
					
					/*var objDIV = document.createElement("div");
					container.appendChild(objDIV);
					
					var objCheck = document.createElement("input");
					objCheck.setAttribute('type','checkbox');
					objCheck.setAttribute('name','registration[egyetem_kar]['+szamlalo+']');
					objCheck.setAttribute('value',attr_id);
					objDIV.appendChild(objCheck);
					
					var objSPAN = document.createElement("span");
					objDIV.appendChild(objSPAN);
					
					var objText = document.createTextNode(value);
					objSPAN.appendChild(objText);*/
					
					objSELECT.add(new Option(value, attr_id));
				}
			}
		}
	}
	else{
		alert(str[0]);
	}
}
