/**
 * orderStep2.js
 * Skrypt obslugi sciezki zamowienia synaptis.eu
 * Autor: Arkadiusz Lisiecki (ark[]data.pl)
 * Wlasnosc: synaptis.eu, wszelkie prawa zastrzezone
 * (c) Copyright SynapTis.eu
 */

///----

var subStep = 1; //aktualne podmenu

var _currentSubStep; //< przechowuje idx biez. podpunktu */
var _IDProdField; //< wybrana kategoria produktu */
var _IDCity; //< wybrane miasto */
var _IDPlace; //< id wybranej placowki */
var _isOperationSelected = false;
var _isOperationSelectedFlag;
var _isOperationSelectedFlagFinish = true;

function imgLoadProdsOn() {
	document.getElementById('imgOperationsLoading').style.display = "block";
}

function imgLoadProdsOff() {
	document.getElementById('imgOperationsLoading').style.display = "none";
}

function isPlaceSelected() {
	if (!_IDPlace) 
		return false;

	if (_IDPlace == 0)
		return false;
		
	return true;
}

function testIsOperationSelected(resp) {
	if (resp == 'OK') 
		_isOperationSelected = true;
	else 
		_isOperationSelected = false;


	_isOperationSelectedFlag = false;
}

function goOperationSelected() {
	if (!_isOperationSelectedFlagFinish)
		return;

	_isOperationSelectedFlag = true;
	_isOperationSelectedFlagFinish = false;
	ajax('Action=testOrderProds', testIsOperationSelected, true);
}

//ustawia dany ID na aktywne, reszta na nieaktywne..
function switchSubmenu(ID) {
	//ustawienie nru subMenu
	switch(ID) {
		case "CityAndPlace": {
			subStep = 1;
		} break;
		case "Operations": {
			subStep = 2;
		} break;
		case "Hotels": {
			subStep = 3;
		} break;
		case "Cars": {
			subStep = 4;
		} break;
		case "Translators": {
			subStep = 5;
		} break;
		case "Next" : {
			subStep++;
		} break;
		case "Prev" : {
			subStep--;
		} break;
		case "Current" : { } break;
	}//switch 

	//granice
	if (subStep <= 0)
		subStep = 1;

	if (subStep > 2)
		subStep = 2;


	switch (subStep) {
	case 1: {
		ID = "CityAndPlace";
			if (isPlaceSelected()) {
				document.getElementById('imgGoToStep3').src = 'img/nextStepOperationActive.jpg';
				document.getElementById('imgGoToStep1').src = 'img/prevStep.jpg';
			} else {
				document.getElementById('imgGoToStep3').src = 'img/nextStepOperationDeactive.jpg';
				document.getElementById('imgGoToStep1').src = 'img/prevStep.jpg';
			}
		} break;	
	case 2: {
		ID = "Operations";
			if (_isOperationSelected) {
				document.getElementById('imgGoToStep3').src = 'img/nextStepOperationActive.jpg';
				document.getElementById('imgGoToStep1').src = 'img/prevStepOrderPlace.jpg';
			} else {
				document.getElementById('imgGoToStep3').src = 'img/nextStepDeactive.jpg';	
				document.getElementById('imgGoToStep1').src = 'img/prevStepOrderPlace.jpg';	
				
			}
		} break;
	case 3: {
		ID = "Hotels";	
		} break;
	case 4: {
		ID = "Cars";	
		} break;
	case 5: {
		ID = "Translators";	
		//document.getElementById("nextStepButtSub").style.display="none";
		//document.getElementById("nextStepButt").style.display="inline";
		} break;
	}//switch

	//reset  wszystkich stylow
	document.getElementById( "submenuCityAndPlace" ).style.display ="none";
	document.getElementById( "submenuOperations" ).style.display ="none"; 
	document.getElementById( "submenuHotels" ).style.display ="none"; 
	document.getElementById( "submenuCars" ).style.display ="none"; 
	document.getElementById( "submenuTranslators" ).style.display ="none"; 
	
	setClassStyle("linkCityAndPlace","tab");
	setClassStyle("linkOperations","tab");
	setClassStyle("linkHotels","tab")
	setClassStyle("linkCars","tab");
	setClassStyle("linkTranslators","tab");
	
	//ustawienie stylu i widocznosci
	setClassStyle("link"+ID,"tab_active");
	document.getElementById("submenu"+ID ).style.display ="block";

	//setClassStyle("submenu"+ID, 'display');
return;
}


///----



///Wykonywany w momencie zmiany kategorii produktow.
///Jesli nastapila zmiana kategorii produktu, to nastepuje
///disabled wszystkich pol oraz ustawienie pola wyboru miasta
///na biezace, a wyboru kategorii produktu na nowo wybrana
function chooseProdField(newIDProdField) {
	if (newIDProdField == _IDProdField) 
		return;
		
	_IDProdField = newIDProdField;

	refreshCities();
	refreshPlaces();
}

function clearIDCity() {
	var form = document.getElementById('IDCityForm');
//	delete form.CityName.options;
	form.CityName.options.length = 0; //nie obslugiwane przez IE 6.0
	_IDCity = null;
}

//Na podstawie odebranych danych ustala dostepne IDCity
function changeIDCity(resp) {
        if (resp.length == 0)
                return;

        var form = document.getElementById('IDCityForm');

        form.CityName.options[0] = new Option('Cała Polska');
        form.CityName.options[0].value = '0';

        var Cities = resp.split(";");
        var CitiesLang;

        for (var i=0; i < Cities.length; i++) {
                CitiesLang = Cities[i].split(",");
                form.CityName.options[i+1] = new Option(CitiesLang[1]);
                form.CityName.options[i+1].value = CitiesLang[0];
        }
}


function refreshCities() {
	clearIDCity();	
	//wyslanie zapytanie do bazki o liste miast
	//w odpowiedzi oczekiwany jest lancuch listy miast oddzielony srednikami
	ajax('Action=getCityByProdField&IDProdField='+_IDProdField+'&IDLang=1045', changeIDCity, true);
}

function refreshActiveCityByPlace(idplace) {
	//wyslanie zapytanie do bazki o id miasta dla odpowiedniej placowki
	//w odpowiedzi oczekiwany jest idmiasta
	ajax('Action=getCityByPlace&IDPlace='+idplace, setActiveCity, true);
}

///Wykonywany w momencie wyboru miasta
function setActiveCity(idcity) {
	if (idcity.length == 0)
		return;
	if (idcity == _IDCity) 
		return;

	clearActiveMap(_IDCity); //clear old, from orderMap.js

	_IDCity = idcity;

	setActiveCityOnForm(idcity);
	setActiveCityOnMap(idcity);

	ajax('Action=setCity&City='+idcity, nothing, true);
}


function setActiveCityOnForm(idcity) {
	if (idcity.length == 0)
                return;

        var form = document.getElementById('IDCityForm');

	for (var i=0; i < form.CityName.length; i++) {
                if (form.CityName.options[i].value == idcity) {
			form.CityName.selectedIndex = i;
			break;
		}
        }
}

function setActiveCityOnMap(idcity) {
	if (idcity.length == 0)
                return;

	setActiveMap(idcity); //from orderMap.js

}

function submitIDCityChange() {
	var form = document.getElementById('IDCityForm');
        var data = form.elements['CityName'].value;
	
	setActiveCity(data);

	refreshPlaces();
}



function refreshPlaces() {
	clearPlaces();

	//wyslanie zapytanie do bazki o liste osrodkow
	//w odpowiedzi oczekiwany jest lancuch listy osrodkow oddzielony srednikami
	ajax('Action=getPlaces&IDProdField='+_IDProdField+'&IDLang=1045'+'&IDCity='+_IDCity, changePlaces, true);
}

function clearPlaces() {
	var form = document.getElementById('IDPlaceForm');
//	delete form.IDPlace.options;
	form.IDPlace.options.length = 0; //Nie obslugiwane przez IE 6.0
	_IDPlace = null;
	chooseIDPlace(0); 
	imgLoadProdsOff();
}

function changePlaces(resp) {
	if (resp.length == 0)
		return;

	var form = document.getElementById('IDPlaceForm');	
	form.IDPlace.options[0] = new Option('--- Proszę wybrać ośrodek ---');
	form.IDPlace.options[0].value = '0';
	
	var Places = resp.split(";");
	var PlacesLang;
	
	for (var i=0; i < Places.length; i++) {
		PlacesLang = Places[i].split(",");
		form.IDPlace.options[i+1] = new Option(PlacesLang[1]);
		form.IDPlace.options[i+1].value = PlacesLang[0];
	}

}


///Wybiera placowke
function chooseIDPlace(idplace) {
	_IDPlace = idplace;
	
	imgLoadProdsOn();
	clearProds("ProductsTable");
 	ajax('Action=getProds&IDProdField='+_IDProdField+'&IDLang=1045'+'&IDPlace='+_IDPlace, changeProds, true);

	switchSubmenu("Current");
}

function submitIDPlaceChange() {
	var form = document.getElementById('IDPlaceForm');
        var data = form.elements['IDPlace'].value;
	
	chooseIDPlace(data);

	refreshActiveCityByPlace(data);
}

function removeChildrenFromNode(node) {
	if(node === undefined || node === null) {
		return;
	}

	var len = node.childNodes.length;
	while (node.hasChildNodes()) {
		node.removeChild(node.firstChild);
	}
}

//czysci tabelke produktow
function clearProds(tableid) {
	var table = document.getElementById(tableid);
 	removeChildrenFromNode(table);

 	ajax('Action=clearProd', nothing, true);
}


//Filtruje miejsca na podstawie wybranego produktu w zaleznosci od stanu klikniecia
//IDProd - ID Produktu
//isInput - true, filtr w zaleznosci od stanu input buttonu, false - najpierw przelacz stan input button na przeciwny
function ProdFilter(IDProd, isInput) {
	///if (document.getElementById( "P" + IDProd ).getAttribute("className") == "disabled")
	if (document.getElementById("P" + IDProd).className == "disabled")
		return;

var _Pc = eval("document.getElementById('Pc"+IDProd+"' )");
	if (! isInput) {
		_Pc.checked = !_Pc.checked;
	}
	//test, czy produkt zostal wlasnie wybrany:	
	if (_Pc.checked) {
		ajax('Action=addProd&IDProd='+IDProd, nothing, true);
		setClassStyle("P"+IDProd,"selected");
	} else { //odznaczenie
		ajax('Action=delProd&IDProd='+IDProd, nothing, true);
		setClassStyle("P"+IDProd,"a0");
	}
}

function changeProds(resp) {
	var tableid = "ProductsTable";

	var table = document.getElementById(tableid);
	var ProdsTwo = resp.split("$$");
	var Prods = ProdsTwo[1].split("$");
	var ProdsSpec = ProdsTwo[0].split("$");
	var Prod;
	
	var tr;
	var td1, td2, td3;
	var input, span, a, img, txt;
	var h;
	var n = Prods.length;
	var m = ProdsSpec.length;
	var lastIDProdSubField = -1;
	var isNewSubProd = false;
	var num;
////////////////////////////////////////////////////////	
	if (m > 0 && ProdsSpec[0]!='')
	{
	for (var i=0; i < m; i++) {
	num2 = i;
		//Prod[0] - IDProd
		//Prod[1] - ClassName NameSufix
		//Prod[2] - IDProdSubField
		//Prod[3] - ProdSubFieldName
		//Prod[4] - Price
		
		Prodspecial = ProdsSpec[i].split("#")
		Prodspecial[2] = 0;
//wyswietlenie tytulu podkategorii
		if (Prodspecial[2] != lastIDProdSubField) {
			
			lastIDProdSubField = Prodspecial[2] ; 
			if (lastIDProdSubField > 0) { //wyswietlamy tylko nazwane
			
				//zakonczenie kolumny, jesli nie jest pierwsza, tzn. tr juz istnieje:
				if ((i % 2) && (i > 0)) {
				td1 = document.createElement("td");
				if (isIE()) {
				td1.colSpan = "3";
				} else {
				td1.setAttribute('colspan', '3');
				}
				tr.appendChild(td1);
				}
			
				//wyswietlenie nazwy:
				/// odstep:
				tr = document.createElement("tr");
                                table.appendChild(tr);
                                td1 = document.createElement("td");
                                if (isIE()) {
                                td1.colSpan = "6";
                                } else {
                                td1.setAttribute('colspan', '6');
                                }
                                h = document.createElement("p");
                                txt = document.createTextNode(" ");
                                h.appendChild(txt);
                                td1.appendChild(h);
                                tr.appendChild(td1);
	

				tr = document.createElement("tr");
				table.appendChild(tr);
				td1 = document.createElement("td");
				if (isIE()) {
				td1.colSpan = "6";
				} else {
				td1.setAttribute('colspan', '6');
				}
				h = document.createElement("h4");
				txt = document.createTextNode(Prodspecial[3]);
				h.appendChild(txt);
				td1.appendChild(h);
				tr.appendChild(td1);
				
				isNewSubProd = true;
			}
		} else {
			isNewSubProd = false;
		}

//nazwy zabiegow	
		if (((i % 2) == 0) || (isNewSubProd==true)) {
		tr = document.createElement("tr");
		table.appendChild(tr);	
		}
		
		td1 = document.createElement("td");
		if (isIE()) {
		td1.width = '20';
		} else {
		td1.setAttribute('width','20');
		}	
		if (isIE()) {
		var id = 'Pc'+Prodspecial[0];
		var aaaaa = '<input id="'+id+'" onchange="(document.getElementById(\''+id+'\')).checked = true;" onclick="(document.getElementById(\''+id+'\')).checked = true;" checked="checked" type="checkbox"/>';
		var input = document.createElement(aaaaa);
		} else {
		input = document.createElement("input");
		input.setAttribute('checked', 'checked');
		input.setAttribute('onclick', 'checked = true;');
		input.setAttribute('onchange', 'checked = true;');
		input.setAttribute('type', 'checkbox');
		input.setAttribute('id', 'Pc'+Prodspecial[0]);
		}
		td1.appendChild(input);
		
		td2 = document.createElement("td");
		if (isIE()) {
		td2.width = '270';
		} else {
		td2.setAttribute('width','270');
		}
		
		span = document.createElement("span");
		if (isIE()) {
		span.className = 'a0';
		span.id = 'P'+Prodspecial[0];
		} else {
		span.setAttribute('class', 'a0');
		span.setAttribute('id','P'+Prodspecial[0]);
		}
		
		
		txt = document.createTextNode(Prodspecial[1]);
		span.appendChild(txt);
		td2.appendChild(span);
		
		td3 = document.createElement("td");
		if (isIE()) {
		td3.align= 'center';
		} else {
		td3.setAttribute('align','center');
		}
		
		a = document.createElement("a");
		img = document.createElement("img");
		if (isIE()) {
		a.href= 'javascript:openInfo(\'info.php?IDProd='+Prodspecial[0]+'&IDPField='+_IDProdField+'\')';
		img.src= 'img/info.jpg';
		img.border= '0';
		img.alt = Prodspecial[1];
		} else {
		a.setAttribute('href', 'javascript:openInfo(\'info.php?IDProd='+Prodspecial[0]+'&IDPField='+_IDProdField+'\')');
		img.setAttribute('src', 'img/info.jpg');
		img.setAttribute('border', '0');
		img.setAttribute('alt', Prodspecial[1]);
		}
		a.appendChild(img);
		td3.appendChild(a);
		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td3);
				ProdFilter(Prodspecial[0],true);
	}
	}
	/////////////////////////////////////////////////////////////////////////////
	for (var i=0; i < n; i++) {
	num = i;
		//Prod[0] - IDProd
		//Prod[1] - ClassName NameSufix
		//Prod[2] - IDProdSubField
		//Prod[3] - ProdSubFieldName
		//Prod[4] - Price
		Prod = Prods[i].split("#")
//wyswietlenie tytulu podkategorii
		
		if (Prod[2] != lastIDProdSubField) {
			
			lastIDProdSubField = Prod[2] ; 
			if (lastIDProdSubField > 0) { //wyswietlamy tylko nazwane
			
				//zakonczenie kolumny, jesli nie jest pierwsza, tzn. tr juz istnieje:
				if ((i % 2) && (i > 0)) {
				td1 = document.createElement("td");
				if (isIE()) {
				td1.colSpan = "3";
				} else {
				td1.setAttribute('colspan', '3');
				}
				tr.appendChild(td1);
				}
			
				//wyswietlenie nazwy:
				/// odstep:
				tr = document.createElement("tr");
                                table.appendChild(tr);
                                td1 = document.createElement("td");
                                if (isIE()) {
                                td1.colSpan = "6";
                                } else {
                                td1.setAttribute('colspan', '6');
                                }
                                h = document.createElement("p");
                                txt = document.createTextNode(" ");
                                h.appendChild(txt);
                                td1.appendChild(h);
                                tr.appendChild(td1);
	

				tr = document.createElement("tr");
				table.appendChild(tr);
				td1 = document.createElement("td");
				if (isIE()) {
				td1.colSpan = "6";
				} else {
				td1.setAttribute('colspan', '6');
				}
				h = document.createElement("h4");
				txt = document.createTextNode(Prod[3]);
				h.appendChild(txt);
				td1.appendChild(h);
				tr.appendChild(td1);
				
				isNewSubProd = true;
			}
		} else {
			isNewSubProd = false;
		}

//nazwy zabiegow	
		if (((i % 2) == 0) || (isNewSubProd==true)) {
		tr = document.createElement("tr");
		table.appendChild(tr);	
		}
		
		td1 = document.createElement("td");
		if (isIE()) {
		td1.width = '20';
		} else {
		td1.setAttribute('width','20');
		}

		input = document.createElement("input");
		if (isIE()) {
		input.onclick = new Function (
				'ProdFilter('+Prod[0]+',false)'
				)
		document
		input.type= 'checkbox';
		input.id= 'Pc'+Prod[0];
		} else {
		input.setAttribute('onclick', 'ProdFilter('+Prod[0]+',true)');
		input.setAttribute('type', 'checkbox')
		input.setAttribute('id', 'Pc'+Prod[0]);
		}
		td1.appendChild(input);
		
		td2 = document.createElement("td");
		if (isIE()) {
		td2.width = '270';
		} else {
		td2.setAttribute('width','270');
		}
		
		span = document.createElement("span");
		if (isIE()) {
		input.onclick = new Function (
                               'ProdFilter('+Prod[0]+',true)'
                	       )
		span.className = 'a0';
		span.id = 'P'+Prod[0];
		} else {
		span.setAttribute('onclick', 'ProdFilter('+Prod[0]+',false)');
		span.setAttribute('class', 'a0');
		span.setAttribute('id','P'+Prod[0]);
		}
		
		
		txt = document.createTextNode(Prod[1] + ',  cena: '+Prod[4]); //nazwa + cena
		span.appendChild(txt);
		td2.appendChild(span);
		
		td3 = document.createElement("td");
		if (isIE()) {
		td3.align= 'center';
		} else {
		td3.setAttribute('align','center');
		}
		
		a = document.createElement("a");
		img = document.createElement("img");
		if (isIE()) {
		a.href= 'javascript:openInfo(\'info.php?IDProd='+Prod[0]+'&IDPField='+_IDProdField+'\')';
		img.src= 'img/info.jpg';
		img.border= '0';
		img.alt = Prod[1];
		} else {
		a.setAttribute('href', 'javascript:openInfo(\'info.php?IDProd='+Prod[0]+'&IDPField='+_IDProdField+'\')');
		img.setAttribute('src', 'img/info.jpg');
		img.setAttribute('border', '0');
		img.setAttribute('alt', Prod[1]);
		}
		a.appendChild(img);
		td3.appendChild(a);
		
		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td3);
	}
	
	if (n % 2) {
		td1 = document.createElement("td");
		if (isIE()) {
		td1.colSpan = "3";
		} else {
		td1.setAttribute('colspan', '3');
		}
		tr.appendChild(td1);
	}
	imgLoadProdsOff();
}

//--------

//wymaga formularza (form_step2) z polami:
function gotoOrderStep3() {
	document.getElementById('imgGoToStep3').src = 'img/loadingStep.gif';
/*
if (!_IDCity)  {
	alert('Proszę wybrać miejscowość.');
	return;
}*/
	document.getElementById('form_step2').IDCity.value = _IDCity;
	
	if (! isPlaceSelected()) {
		alert('Proszę wybrać ośrodek.');
		document.getElementById('imgGoToStep3').src = 'img/nextStep.jpg';
		return;
	} else {
		document.getElementById('form_step2').IDPlace.value = _IDPlace;

		//jesli jestesmy w nie ostatnim kroku, to switch na nastepny
		if (subStep < 2) {
			switchSubmenu('Next');
			document.getElementById('imgGoToStep3').src = 'img/nextStep.jpg';
		} else {
			//jesli ostatni krok, test czy wsio ok
			goOperationSelected();

			if (_isOperationSelectedFlag == true) {
				setTimeout("gotoOrderStep3()", 500);
				return;
			} else {
				_isOperationSelectedFlagFinish = true;

				if (_isOperationSelected) {
					commitStep2();
				} else {
					alert('Proszę wybrać co najmniej jeden zabieg.');
					document.getElementById('imgGoToStep3').src = 'img/nextStep.jpg';
				}
			}
	
			
		}
	
	}
}


function commitStep2() {
	document.getElementById('form_step2').submit();	
}


//return true, jesli 1 krok
function gotoOrderStep1() {
	if (subStep == 1)
		return true;

	switchSubmenu('Prev');
	return false;
}
