CMS = {

	file : null,

	popupOpen : function (title, html, width, closable){

		var title        = title;
		var html         = html;
		var dialogWidth  = (width) ? width : '500';
		var nonClosable  = (closable) ? 'block' : 'none';

		$('dialog-close-button').setStyle({
		  display: nonClosable
		});

		$('dialog-top-center').innerHTML = title;

		PopupHandler.showDialogBox(html, dialogWidth);
	},

	downloadFrom : function (file)
	{
		CMS.file = file;

		if ((city = CMS.getCookie('city')) != false)
		{
			new Ajax.Request(
				'gateway.php', {
					method     : "post",
					parameters : {
						todo : 'store',
						city : city,
						file : CMS.file
					},
					onComplete : function (request){
						window.location = request.responseText;
					}
				}
			);
		}
		else
		{
			new Ajax.Request(
				'gateway.php', {
					method     : "post",
					parameters : {
						todo     : 'get'
					},
					onComplete : function (request){
						CMS.popupOpen('<strong>Download request</strong>', request.responseText);
					}
				}
			);
		}
	},
	
	downloadFile : function (thisform)
	{
		city = $F('city');

		if (city.length < 3 || city.length > 22 )
		{
			alert('A helyseg neve minimum 3, maximum 22 karakter legyen!');
			return false;
		}

		CMS.setCookie('city', city, 365);

		new Ajax.Request(
			'gateway.php', {
				method     : "post",
				parameters : {
					todo : 'store',
					city : city,
					file : CMS.file
				},
				onComplete : function (request){
					window.location = request.responseText;
				}
			}
		);
	},

	setCookie : function(c_name, value, expiredays) 
	{
		var exdate = new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie = c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	},

	getCookie : function(c_name) 
	{
		if (document.cookie.length>0) 
		{
			c_start=document.cookie.indexOf(c_name + "=");
			
			if (c_start != -1) 
			{ 
				c_start = c_start + c_name.length+1;
				c_end   = document.cookie.indexOf(";" , c_start);
				
				if (c_end == -1) 
				{
					c_end = document.cookie.length;
				}

				valami = unescape(document.cookie.substring(c_start,c_end));
				return (valami == undefined) ? false : valami;
			} 
		}

		return false;
	}
};