/*
* zwraca:
* 1-udane dodanie
* 2-termin już w schowku
* 3-za dużo w schowku
*
*/
function clipboardAdd(id_term,id_biura){
	var terminy_cookie = YAHOO.util.Cookie.get('clipboardOffers');
	terminy_cookie=""+terminy_cookie+"";
	if(terminy_cookie)
	{
		var tab = terminy_cookie.split("=true&");

		if(tab.length>=25){
			return(3);
		}

		for(i=0;i<tab.length;i++)
		{

			if(tab[i].indexOf("=true") > 0)
			{
				var dl = tab[i].length - 5;
				tab[i] = tab[i].substr(0, dl);
			}

			//currentIdTerminy=tab[i].split(':')[0];
			//currentIdBiura=tab[i].split(':')[1];

	  		if((tab[i] == id_term+":"+id_biura)||(tab[i] == id_term+escape(':')+id_biura))
	  		{
	  			return(2);
	  		}
		}

		YAHOO.util.Cookie.setSub('clipboardOffers', id_term+':'+id_biura, "true", {path: "/", expires: new Date("January 12, 2025"), domain: ".wakacje.pl"});
		updateClipboardOffersCount();
		return(1);
	}
	else
	{
		YAHOO.util.Cookie.setSub('clipboardOffers', id_term+':'+id_biura, 'true',{path: "/", expires: new Date("January 12, 2025"), domain: ".wakacje.pl"});
		updateClipboardOffersCount();
		return(1);
	}
}

function clipboardRemove(id_term,id_biura){
	YAHOO.util.Cookie.removeSub('clipboardOffers',id_term+':'+id_biura,{path: "/",domain: ".wakacje.pl"});
	updateClipboardOffersCount();
}


/*
	0 - jest w schowku
	1 - nie ma w schowku
 */
function clipboardTest(id_term,id_biura){
	var terminy_cookie = readCookie('clipboardOffers');
	if(terminy_cookie)
	{
		terminy_cookie=""+terminy_cookie+"";
		var tab = terminy_cookie.split("=true&");
		for(i=0;i<tab.length;i++)
		{
			if(tab[i].indexOf("=true") > 0)
			{
				var dl = tab[i].length - 5;
				tab[i] = tab[i].substr(0, dl);
			}

			//currentIdTerminy=tab[i].split(':')[0];
			//currentIdBiura=tab[i].split(':')[1];


	  		if((tab[i] == id_term+":"+id_biura)||(tab[i] == id_term+escape(':')+id_biura))
	  		{
	  			return(0);
	  		}
		}
	}
	return(1);
}




function addTermToCookie() {
	document.getElementById('dodaj_do_schowka').style.display='none';
	document.getElementById('dodaj_do_schowka_selected').style.display='block';

	var id_term = false;

	if(document.getElementById('clipboard_id_terminy')){
		id_term = document.getElementById('clipboard_id_terminy').value;
	}

	if(document.getElementById('clipboard_id_terminy').value>0){
		id_term = document.getElementById('clipboard_id_terminy').value;
	}

	if(document.getElementById('id_biura')){
		var id_biura = document.getElementById('id_biura').value;
	}

	if(id_term && id_biura){
		var callback =
		{
			success: function(o) {
				document.getElementById('clipboardTerm1').innerHTML=o.responseText;
				document.getElementById('clipboardTerm2').innerHTML=o.responseText;
			},
			failure: function(o) {}
		};

		var ajaxTermUrl="/?page=schowekAjax&id_terminy="+id_term+"&id_biura="+id_biura;
		YAHOO.util.Connect.asyncRequest('GET', ajaxTermUrl, callback, null);



		ret=clipboardAdd(id_term,id_biura);

		if(ret==1){
			document.getElementById('schowekInfo').className='dodano';
		}else if(ret==3){
			document.getElementById('schowekInfo').className='zaDuzo';
		}else{
			document.getElementById('schowekInfo').className='nieDodano';
		}


		document.getElementById('schowekInfo').style.display='block';
	}
}


function refreshNavigClipboard(){
	var clipboardCallback =
	{
		success: function(o) {
			document.getElementById('navigClipboard').innerHTML=o.responseText;
		},
		failure: function(o) {}
	};
	document.getElementById('navigClipboard').innerHTML='Mój schowek: <b>...</b>';
	var navigClipboardUrl="/?page=navigClipboardContainer&rand="+Math.random();
	YAHOO.util.Connect.asyncRequest('GET', navigClipboardUrl, clipboardCallback, null);
}



function hideClipboard(){
	if(document.getElementById('schowekInfo')){
		document.getElementById('schowekInfo').style.display='none';
	}
}

function centerOfferDescrShortClipboardRemove(){
	var id_term = window.URL.hashTable['id_terminy'];
	if(!id_term){
		id_term = window.URL.searchTable['id_terminy'];
	}

	var idOfertyIdBiura = document.location.href.match(/([0-9]{1,},[0-9]{1,})(\.html)/)[1].split(',');
	var id_biura=idOfertyIdBiura[1];

	clipboardRemove(id_term,id_biura);

	var callback =
	{
		success: function(o) {
			document.getElementById('clipboardTerm3').innerHTML=o.responseText;
		},
		failure: function(o) {}
	};
	var ajaxTermUrl="/?page=schowekAjax&id_terminy="+id_term+"&id_biura="+id_biura;
	YAHOO.util.Connect.asyncRequest('GET', ajaxTermUrl, callback, null);

	document.getElementById('schowekInfo').className='usunieto';

	document.getElementById('schowekInfo').style.display='block';

	document.getElementById('dodaj_do_schowka').style.display='block';
	document.getElementById('dodaj_do_schowka_selected').style.display='none';
}








var offersToRemove=[];
	function showCheck()
	{
		var nazwa_cookie = 'clipboardOffers';

		var checkboxes = document.getElementById('tablicaOfert').getElementsByTagName('input');
		var reload=false;

		for (i = 0; i < checkboxes.length; i++)
		{

			if (checkboxes[i].checked)
			{
				var id_term_id_biura = checkboxes[i].value;
				YAHOO.util.Cookie.removeSub(nazwa_cookie,id_term_id_biura,{domain: ".wakacje.pl"});
				reload=true;
			}

		}

		if(reload){
			window.location.reload(true);
		}else{
			showMonit('Zaznacz oferty, które chcesz usun±ć');
		}
	}

	function compareOffers()
	{
		var nazwa_cookie = 'clipboardCompare';
		YAHOO.util.Cookie.remove(nazwa_cookie,{domain: ".wakacje.pl"});
		var selected=0;
		var checkboxes = document.getElementById('tablicaOfert').getElementsByTagName('input');
		var id_term_id_biura=0;

		for (i = 0; i < checkboxes.length; i++)
		{

			if (checkboxes[i].checked)
			{
				id_term_id_biura = checkboxes[i].value;
				YAHOO.util.Cookie.setSub(nazwa_cookie, id_term_id_biura, 'true',{path: "/" , domain: ".wakacje.pl"});
				selected++;
			}

			if(checkboxes[i].className=="nieaktualny"){
				showMonit('Nie można porównywac nieaktualnych ofert.');
				exit;
			}

		}

		if(selected<2){
			showMonit('Wybierz z listy od 2 do 4 ofert');
			YAHOO.util.Cookie.remove(nazwa_cookie,{domain: ".wakacje.pl"});
		}else if(selected>4){
			showMonit('Do porównania można wybrać maksymalnie 4 oferty');
			YAHOO.util.Cookie.remove(nazwa_cookie,{domain: ".wakacje.pl"});
		}else{
			var min = 1;
			var max = 9999999;
			var ran = Math.floor(Math.random() * (max - min + 1)) + min;
			window.location="/?page=schowekCompare&r="+ran;
		}
	}

	function showMonit(tresc){
		document.getElementById('monit').style.display='block';
		document.getElementById('monitTresc').innerHTML=tresc;
		window.scrollTo(0,findPos(document.getElementById('clipboardHeader'))[1]);
	}


	function redirectToClipboard() {
		if (document.getElementById('redirectClipboard') && document.getElementById('redirectClipboard').value == 1) {
			window.location.reload(1);
		}
	}



function writeOrderInCookie(order) {

	YAHOO.util.Cookie.set('clipboardCompareOrder',order, "true", {path: "/", expires: new Date("January 12, 2025"), domain: ".wakacje.pl"});

}






function setOrder()
	{
		var ord = document.getElementById('orderBy').value;

		var min = 1;
		var max = 9999999;
		var ran = Math.floor(Math.random() * (max - min + 1)) + min;

		if(ord != "")
		{
			writeOrderInCookie(ord);
			window.location="/?page=schowekCompare&r="+ran+"&order="+ord;
		}
	}


	function removeFromClipboard(id_term)
	{
		nazwa_cookie='clipboardCompare';
		clipboardCompare=YAHOO.util.Cookie.get(nazwa_cookie,{domain: ".wakacje.pl"});
		offers=clipboardCompare.split("&").length;

		if(offers>2){
			var nazwa_cookie = 'clipboardCompare';
			YAHOO.util.Cookie.removeSub(nazwa_cookie,id_term,{domain: ".wakacje.pl"});
			window.location.reload(true);
		}

	}



/* przeniesione do header.js
function updateClipboardOffersCount(){
	if(document.getElementById('clipboardOfferCount')){
		var clipboardOffers = readCookie('clipboard');
		clipboardOffers=clipboardOffers+"";
		var clipboardTable = clipboardOffers.split('&');
		if(clipboardTable.length>0){
			document.getElementById('clipboardOfferCount').innerHTML='('+clipboardTable.length+')';
		}else{
			document.getElementById('clipboardOfferCount').innerHTML='';
		}
	}
}*/



