//ąćęłńóśżź
/**********************************************************
 * 
 * categ v0.01 Copyright (c) 2008 ruler.pl
 * Contact us at the@ruler.pl
 * This copyright notice MUST stay intact for use.
 * 
 * Obiekt Managera Wyboru Sklepu
 * 
 **********************************************************/

//skończony

var START={

	isMSIE:navigator.appName=="Microsoft Internet Explorer"?true:false,//wykrycie przeglądarki IE
	isFF:!document.all && document.getElementById && !this.isOpera,//wykrycie przeglądarki FF
	isOpera:navigator.appName=="Opera"?true:false,//wykrycie przeglądarki Opera

	browser:function() {
		if(this.isOpera) return 3;
		else if(this.isFF) return 2;
		else if(this.isMSIE) return 1;
		else return 0;
	},

	//funkcja zewnętrzna - ustawia nową konfigurację (definiuje wymagane zmienne)
	configure:function() {//brak parametrów - zdefiniowane macierze muszą zostać wypełnione oddzielnie (patrz collect i control)

		if(this.sElem) this.restart();

		this.sElem='';//sElem=idElementu
		this.sUpdateStart='';
		this.sUpdateStop='';
		this.sUpdateCzas='';
		this.sUpdateAkcja='';
		this.sUpdateNazwa='';

	},

	//funkcja zewnętrzna - ustawia nowe dane drzewa (calego)
	collect:function(oData) {//START.collect({});

	},

	//funkcja zewnętrzna - uzupelnia macierze identyfikatorami rozmieszczenia
	control:function(oData) {//START.control({sElem:sElem});

		if(oData){
			if(oData.sElem) this.sElem=oData.sElem;
			if(oData.sUpdateStart) this.sUpdateStart=oData.sUpdateStart;
			if(oData.sUpdateStop) this.sUpdateStop=oData.sUpdateStop;
			if(oData.sUpdateCzas) this.sUpdateCzas=oData.sUpdateCzas;
			if(oData.sUpdateAkcja) this.sUpdateAkcja=oData.sUpdateAkcja;
			if(oData.sUpdateNazwa) this.sUpdateNazwa=oData.sUpdateNazwa;
		}

	},

	//funkcja zewnętrzna - inicujuje obiekt LOGIN
	build:function() {
		var newCode='';
		var url=location.href;
		var host,hash;
		var sysStop,sysTime;
		//var today=new Date();
		//var curTime={0:checkTime(today.getHours()),1:checkTime(today.getMinutes()),2:checkTime(today.getSeconds())};

		//sysStop=this.sUpdateStop.split(':');
		sysTime=this.sUpdateCzas.split(':');

		if(this.sUpdateAkcja=='blocking'){
			if(document.getElementById(('iCont01'))){
				newCode='<p style="height:auto;"><img style="position:relative;top:0px;left:0px;" src="images/ServiceBreak.png" alt="Przerwa Serwisowa"/></p>';
				document.getElementById(('iCont01')).innerHTML=newCode;
			}
			if(document.getElementById(('iCont02'))){
				newCode='<p><b>'+this.sUpdateNazwa+'</b></p>';
				document.getElementById(('iCont02')).innerHTML=newCode;
			}
			if(document.getElementById(('iCont03'))){
				newCode='<p>Przerwa zakończy się o godzinie <span id="sUpdateStop">'+this.sUpdateStop+'</span></p>';
				newCode+='<p>Szacunkowy czas do końca przerwy to <span id="h">'+sysTime[0]+'</span>:<span id="m">'+sysTime[1]+'</span>:<span id="s">'+sysTime[2]+'</span></p>';
				document.getElementById(('iCont03')).innerHTML=newCode;
			}
			//this.setEvent();
		}
		else if(url.indexOf("?shopHash=")!=-1){
			host=url.substr(0,url.indexOf("?shopHash="));
			hash=url.substr((url.indexOf("?shopHash=")+10));
			if(document.getElementById(('iCont01'))){
				newCode='<p></p>';
				document.getElementById(('iCont01')).innerHTML=newCode;
			}
			if(document.getElementById(('iCont02'))){
				newCode='<p>Wczytywanie ustawień. Proszę czekać...</p>';
				document.getElementById(('iCont02')).innerHTML=newCode;
			}
			if(document.getElementById(('iCont03'))){
				newCode='<p></p><form style="display:none;" action="'+host+'" id="HashForm" name="HashForm" method="post"><input style="display:none;" type="text" name="HashInput" id="HashInput" value="'+hash+'"/><input style="display:none;" type="text" name="browser" id="browser" value="'+this.browser()+'"/></form>';
				document.getElementById(('iCont03')).innerHTML=newCode;
			}
			document.HashForm.submit();
		}
		else{
			if(document.getElementById(('iCont01'))){
				newCode='<p>Wpisz nazwę sklepu i potwierdź przyciskiem "OK" lub klawiszem "ENTER".</p>';
				document.getElementById(('iCont01')).innerHTML=newCode;
			}
			if(document.getElementById(('iCont02'))){
				newCode='<form action="javascript:void(null)" id="StartForm" name="StartForm"><input class="start" type="text" name="StartInput" id="StartInput" value=""/><input type="submit" name="StartSubmit" id="StartSubmit" value="OK"/></form>';
				document.getElementById(('iCont02')).innerHTML=newCode;
			}
			if(document.getElementById(('iCont03'))){
				newCode='<p></p><form style="display:none;" action="" id="HashForm" name="HashForm" method="post"><input style="display:none;" type="text" name="HashInput" id="HashInput" value=""/><input style="display:none;" type="text" name="browser" id="browser" value="'+this.browser()+'"/></form>';
				document.getElementById(('iCont03')).innerHTML=newCode;
			}
			this.setEvent();
		}

	},

	//funkcja wewnętrzna
	startTime:function() {
		var today=new Date();
		var h=today.getHours();
		var m=today.getMinutes();
		var s=today.getSeconds();
		//add a zero in front of numbers<10
		h=checkTime(h);
		m=checkTime(m);
		s=checkTime(s);
		document.getElementById('txt').innerHTML=h+":"+m+":"+s;
		t=setTimeout('startTime()',500);
	},

	//funkcja wewnętrzna
	checkTime:function(i) {
		if(i<10) i="0" + i;
		return i;
	},

	//funkcja wewnętrzna
	reset:function() {

		if(document.StartForm){
			document.StartForm.StartInput.value='';
		}

	},

	//funkcja wewnętrzna i zewnętrzna - usuwa niepotrzebne eventy
	restart:function() {

		if(this.sElem && document.getElementById((this.sElem))) this.unsetEvent();

	},

	//funkcja wewnętrzna
	startPress:function(e) {
		var keyNum,keyChar,charCheck;

		if(window.event) keyNum=e.keyCode;//ie
		else if(e.which) keyNum=e.which;//reszta
		keyChar=String.fromCharCode(keyNum);
		if(keyChar.length>0 && keyNum>46){
			charCheck=/[^a-zA-Z0-9._-]/;
			if(charCheck.test(keyChar)) CORE.cancelEvent(e);
		}
		if(keyNum==13) START.startClick(e);

	},

	//funkcja wewnętrzna
	startUp:function(e) {

		document.StartForm.StartInput.value=document.StartForm.StartInput.value.replace(/[^a-zA-Z0-9._-]/gi,"","gi");

	},

	//funkcja wewnętrzna
	startClick:function(e) {
		var sLink='';

		if(typeof(document.StartForm.StartInput)=='object' && document.StartForm.StartInput.value){
			document.StartForm.StartInput.value=document.StartForm.StartInput.value.replace(/[^a-zA-Z0-9._-]/gi,"","gi");
			if(!document.StartForm.StartInput.value.length>0){
				alert('Wpisz nazwę sklepu\n');
				document.StartForm.StartInput.focus();
				document.StartForm.StartInput.select();
			}
			else{
				document.HashForm.HashInput.value=CORE.sha1(document.StartForm.StartInput.value);
				if(document.getElementById(('iCont01'))){
					newCode='<p></p>';
					document.getElementById(('iCont01')).innerHTML=newCode;
				}
				if(document.getElementById(('iCont02'))){
					newCode='<p>Wczytywanie ustawień. Proszę czekać...</p>';
					document.getElementById(('iCont02')).innerHTML=newCode;
				}
				document.HashForm.submit();
			}
		}

	},

	getCookie:function(cName) {

		if(document.cookie.length>0){
			cStart=document.cookie.indexOf(cName+"=");
			if(cStart!=-1){ 
				cStart=cStart+cName.length+1; 
				cEnd=document.cookie.indexOf(";",cStart);
				if(cEnd==-1) cEnd=document.cookie.length;
				return unescape(document.cookie.substring(cStart,cEnd));
			} 
		}
		return "";
	},

	//funkcja wewnętrzna - ustawia eventListenery (związane obiekty, eventy oraz funkcje sterujące obiektem i wywołaniami zewnętrznymi)
	setEvent:function() {

		CORE.addEvent(window,'load',function(e){
			document.StartForm.StartInput.focus();
			document.StartForm.StartInput.select();
		});
		CORE.addEvent(document.StartForm,'submit',function(e){
			return false;
		});
		CORE.addEvent(document.StartForm.StartInput,'keypress',function(e){
			START.startPress(e);
		});
		CORE.addEvent(document.StartForm.StartInput,'keyup',function(e){
			START.startUp(e);
		});
		CORE.addEvent(document.StartForm.StartSubmit,'click',function(e){
			START.startClick(e);
		});

	},

	//funkcja wewnętrzna - usuwa eventListenery
	unsetEvent:function() {

		CORE.removeEvent(window,'load',function(e){
			document.StartForm.StartInput.focus();
			document.StartForm.StartInput.select();
		});
		CORE.removeEvent(document.StartForm,'submit',function(e){
			return false;
		});
		CORE.removeEvent(document.StartForm.StartInput,'keypress',function(e){
			START.startPress(e);
		});
		CORE.removeEvent(document.StartForm.StartInput,'keyup',function(e){
			START.startUp(e);
		});
		CORE.removeEvent(document.StartForm.StartSubmit,'click',function(e){
			START.startClick(e);
		});

	}

}
