Import('other/ImageLayer.js');
Import('native/String.js');
Import('myLib/layers/LayerManager.js');
Import('myLib/form/CheckForm.js');

PageService = {};

PageService.$_ = function(id){
	return document.getElementById(id);
}

Import.clone(Service, PageService);

//---

PageService.openPage = function(href, target){
	var href = href.replaceAll('@', '&');
	if(target == '_self')
		document.location.href = href;
	else
		window.open(href);
};

PageService.closeLayer = function(id) {
	LayerManager.close(id);
};

//---

PageService.openImageById = function(id, description){
	var path = Config.getPathToPage() + Config.file.VIEWER + '?id=' + id;
	ImageLayer.showOnce(path, description);
};

PageService.openImage = function(image, description){
	var path = Config.getPathToData() + image;
	ImageLayer.showOnce(path, description);
};

PageService.openAddedImageById = function(id){
	ImageLayer.showById(id);
};

PageService.setImage = function(id, image, description){
	var path = Config.getPathToData() + image;
	ImageLayer.setValues(id, path, description);
};

//---

PageService.openPrintVersion = function(url){
	var params = {
		left:		20,
		top:		20,
		width: 		627, //faktyczna: 587
		height: 	550,
		scrollbars: 'yes',
		menubar: 'yes'
	};
	Service.openWindow(url, params);
};

PageService.openViewNewsletter = function(){
	var params = {
		left:		20,
		top:		20,
		width: 		580,
		height: 	550,
		scrollbars: 'yes',
		resizable:	'yes'
	};
	var path = Config.PATH + Config.dir.PAGE + Config.file.NEWSLETTER;
	Service.openWindow(path, params);
};

PageService.openFormNotifier = function(){
	var params = {
		left:		100,
		top:		100,
		width: 		520,
		height: 	300
	};
	var path = Config.PATH + Config.dir.PAGE +
	Config.file.NOTIFIER + document.location.search;
	Service.openWindow(path, params);
};

/* Form input bacground color change */
PageService.bgOnFocus = function (obj) {
	obj.style.background = "#FFFFD7";
}
PageService.bgOnBlur = function (obj) {
	obj.style.background = "#FFFFFF";
}

/* Validation Notification form  */
PageService.submitNotificationsForm = function(form){
	var fields = ['rodzaj_id','opis','osoba_email']; //,'mapa_punkt_x','mapa_punkt_y'
	for (var i = 0; i < fields.length; i++ ) {

		if (form[fields[i]].value == '') {
		    var alertTXT = PageService.$_('labels.' + fields[i]).innerHTML;

		    if (fields[i] == 'mapa_punkt_x') {
			  alertTXT =   'Określ punkt na mapie';
		    }
		    AlertBox.alert('<h1>Błąd</h1><em>Wypełnij pole:</em><p>' + alertTXT + '</p>');
		  return false;

	  }
	 if (fields[i] == 'osoba_email' && !CheckForm.checkMail(form[fields[i]].value)) {
		    alertTXT = 'E-mail jest niepoprawny';
		    AlertBox.alert('<h1>Błąd</h1><em>Wypełnij pole:</em><p>' + alertTXT + '</p>');
		  return false;
	  }

	}
	form.submit();
};

