var newwindow;

var selectedTab = null;
var selectedStatsTab = null;
var imageTimer = null;

var df,dt;      

function clipboardStatusForApartment(id,addedMessage,addMessage){
	if(cookieHasValue('clipboard',id,'|')==0) {
		var cbUrl = "<a href=\"javascript:void(0)\" onClick=\"ajaxSetContent('ClipboardStatus','/fragments/components/addToClipboard.jsp?id="+id+"','');this.parentNode.innerHTML='"+addedMessage+"'; return false;\">"+addMessage+"</a>";
		document.write(cbUrl);
	} else {
		document.write(addedMessage);	
	}
}

function init(){
	if(cookieCountValue("clipboard","|")>0){
		document.getElementById("ClipboardStatus").innerHTML='<a href="/schowek.jsp" title="SCHOWEK">SCHOWEK(' + cookieCountValue("clipboard","|") + ')</a>';
	}	
	if(cookieCountValue("recommended","|")>0){
		document.getElementById("RecommendedStatus").innerHTML='<a href="/listaPolecanych.jsp" title="POLECONE">POLECONE(' + cookieCountValue("recommended","|") + ')</a>';
	}
	document.getElementById("fraza").value=decodeURI(gup("fraza"));	
}

function initCalendars(){
	if( (document.getElementById('df')) && (document.getElementById('dt')) ) {
		df  = new Epoch('epoch_popup','popup',document.getElementById('df'),false);
		dt  = new Epoch('epoch_popup','popup',document.getElementById('dt'),false);
	}	
}

function searchKeyUp(e){
	var keynum;
	if(window.event) {
		//ie	
		keynum = e.keyCode;
	} else if(e.which) {
		//netscape/ff/opera 
		keynum = e.which;
	}
	if(keynum==13){
		search();
	}
}

function search(){
	if(document.getElementById("fraza").value!=""){
		window.location = '/wyszukaj.jsp?fraza=' + encodeURI(document.getElementById("fraza").value);
	} else {
		alert("Podaj kryteria wyszukiwania");
	}
}

function setGalleryImage(idx){
	document.images.largeImage.src = '/e-spanie-resources/' + images[idx][0];
	document.getElementById("imageDescription").innerHTML = images[idx][1];
	if(idx==0){
		document.getElementById("prev").href="javascript:void(0);";
		document.getElementById("prev").className="linkCommonDisabled";
		document.getElementById("next").href="javascript:setGalleryImage("+(idx+1)+")";
		document.getElementById("next").className="linkCommon";
		document.getElementById("prev2").href="javascript:void(0);";
		document.getElementById("prev2").className="linkCommonDisabled";
		document.getElementById("next2").href="javascript:setGalleryImage("+(idx+1)+")";
		document.getElementById("next2").className="linkCommon";
		document.getElementById("counter").innerHTML=(idx+1) + "/" + (images.length);
		document.getElementById("counter2").innerHTML=(idx+1) + "/" + (images.length);
	} else if(idx==(images.length-1)) {
		document.getElementById("prev").href="javascript:setGalleryImage("+(idx-1)+")";
		document.getElementById("prev").className="linkCommon";
		document.getElementById("next").href="javascript:void(0)";
		document.getElementById("next").className="linkCommonDisabled";
		document.getElementById("prev2").href="javascript:setGalleryImage("+(idx-1)+")";
		document.getElementById("prev2").className="linkCommon";
		document.getElementById("next2").href="javascript:void(0)";
		document.getElementById("next2").className="linkCommonDisabled";
		document.getElementById("counter").innerHTML=(idx+1) + "/" + (images.length);
		document.getElementById("counter2").innerHTML=(idx+1) + "/" + (images.length);
	} else if(idx<(images.length-1)) {
		document.getElementById("prev").href="javascript:setGalleryImage("+(idx-1)+")";
		document.getElementById("prev").className="linkCommon";
		document.getElementById("next").href="javascript:setGalleryImage("+(idx+1)+")";
		document.getElementById("next").className="linkCommon";
		document.getElementById("prev2").href="javascript:setGalleryImage("+(idx-1)+")";
		document.getElementById("prev2").className="linkCommon";
		document.getElementById("next2").href="javascript:setGalleryImage("+(idx+1)+")";
		document.getElementById("next2").className="linkCommon";
		document.getElementById("counter").innerHTML=(idx+1) + "/" + (images.length);
		document.getElementById("counter2").innerHTML=(idx+1) + "/" + (images.length);
	}
}

function afterDateSet(owner){
	var dfInput, dtInput;
	var dateFrom, dateTo;
	dfInput = document.getElementById("df");
	dtInput = document.getElementById("dt");
	if(dtInput && dfInput){
		if(dtInput.value==''){
			if(dfInput.value!=''){
				dtInput.value = dfInput.value;
				dt  = new Epoch('epoch_popup','popup',document.getElementById('dt'),false,dfInput.value);
			}
		} else if(dfInput.value==''){
			if(dtInput.value!=''){
				dfInput.value = dtInput.value;
				df  = new Epoch('epoch_popup','popup',document.getElementById('df'),false,dtInput.value);
			}
		} else {
			dateFrom = str2dt(dfInput.value);
			dateTo = str2dt(dtInput.value);
			if(owner.id=='df'){
				if(dateFrom>dateTo){
					dt  = new Epoch('epoch_popup','popup',document.getElementById('dt'),false,dfInput.value);				
					dtInput.value = dfInput.value;
				}
			} else if(owner.id=='dt'){
				if(dateFrom>dateTo){
					df  = new Epoch('epoch_popup','popup',document.getElementById('df'),false,dtInput.value);				
					dfInput.value = dtInput.value;
				}
			}
		}
	}
}

function handleReservationSend(){
	if(location.href.indexOf("#wyslanozapytanie")!=-1){
		//document.getElementById("reservationForm").style.display = 'none';
		document.getElementById("reservationCompleteImage").style.display = 'block';
		document.getElementById("reservationComplete").style.display = 'block';
	} else {
		if(document.getElementById("reservationCompleteImage")){
			document.getElementById("reservationCompleteImage").style.display = 'none';
		}
		if(document.getElementById("reservationComplete")){
			document.getElementById("reservationComplete").style.display = 'none';
		}
	
	}
	if(location.href.indexOf("#blad")!=-1){
		document.getElementById("reservationErrorImage").style.display = 'block';
		document.getElementById("reservationError").style.display = 'block';
	} else {
		if(document.getElementById("reservationErrorImage")){
			document.getElementById("reservationErrorImage").style.display = 'none';
		}
		if(document.getElementById("reservationError")){
			document.getElementById("reservationError").style.display = 'none';
		}
	}
}


function handleReservationSubmit(){
	if(!document.reservationForm.accept.checked){
		alert('Korzystanie z serwisu wymaga akceptacji regulaminu');
		return;
	}
	if(document.reservationForm.pd.value == ''){
		alert('Prosz\u0119 poda\u0107 imi\u0119 i nazwisko');	
		document.reservationForm.pd.focus();
		return;
	}
	if(document.reservationForm.email.value == ''){
		alert('Prosz\u0119 poda\u0107 adres email');	
		document.reservationForm.email.focus();
		return;
	}
	if(document.reservationForm.email.value != ''){
		if( !check_email(document.reservationForm.email.value) ){
			alert('Prosz\u0119 poda\u0107 prawid\u0142owy adres email');	
			document.reservationForm.email.focus();
			return;
		}
	}
	if(document.reservationForm.persons.value == ''){
		alert('Prosz\u0119 poda\u0107 ilo\u015B\u0107 os\u00F3b');	
		document.reservationForm.persons.focus();
		return;
	}
	if(document.reservationForm.persons.value != ''){
		var intPersons = parseInt(document.reservationForm.persons.value);
		if( isNaN(intPersons) || (intPersons==0) ){
			alert('Prosz\u0119 poda\u0107 ilo\u015B\u0107 os\u00F3b jako liczb\u0119 ca\u0142okowit\u0105');	
			document.reservationForm.persons.focus();
			return;
		}
	}
	if(document.reservationForm.df.value == ''){
		alert('Prosz\u0119 wybra\u0107 pocz\u0105tkow\u0105 dat\u0119 planowanego pobytu');	
		document.reservationForm.df.focus();
		return;
	}
	if(document.reservationForm.dt.value == ''){
		alert('Prosz\u0119 wybra\u0107 ko\u0144cow\u0105 dat\u0119 planowanego pobytu');	
		document.reservationForm.dt.focus();
		return;
	}
	if( (document.reservationForm.df.value != '') && (document.reservationForm.dt.value != '') ){
		var start = str2dt(document.reservationForm.df.value);
		var end = str2dt(document.reservationForm.dt.value);
		if(start>end) {
			alert('Prosz\u0119 wybra\u0107 prawid\u0142owy zakres dat');	
			document.reservationForm.dt.focus();
			return;
		}
	}
	if(document.reservationForm.imageCode.value == ''){
		alert('Przepisz kod z obrazka w pole tekstowe.');	
		document.reservationForm.imageCode.focus();
		return;
	}
	document.reservationForm.submit();
}

function checkLength(maxLength,formElem,msgControl){
 if (formElem.value.length > maxLength) {
	formElem.value = formElem.value.substring(0,maxLength) 
	charleft = 0 
   	clipped = true 
 } else {
   charleft = maxLength - formElem.value.length 
 }	
   msgControl.value = charleft 
} 


function selectImage(nr, clearTimer)
{
	for(i=0; i<IMG_ITEMS_COUNT; i++)
	{
		var content = document.getElementById('ImageContainer_' + i);
		if(i==nr)
		{
			content.style.display = 'block';
		}
		else
		{
			content.style.display = 'none';
		}
	}
	selectedImage = nr;
	if(clearTimer)
	{
		if(imageTimer != null)
		{
			clearTimeout(imageTimer);
		}
	}
}

function rotateImages(nr)
{
	if(nr<IMG_ITEMS_COUNT)
	{
		selectImage(nr,false);
	}
	else
	{
		selectImage(0, false);
	}
	imageTimer = setTimeout('rotateImages(' + (selectedImage+1) + ')', 6000);
}

function showPanel(tabName, name, noOfPanels)
{
	var panelName = "panel";
	if (selectedTab){
	  	selectedTab.className='tab';
	}
	if(document.getElementById(tabName)){
		selectedTab = document.getElementById(tabName);
		selectedTab.className='tabSelected';
	}
	
	for(i = 0; i < noOfPanels; i++){
		if(document.getElementById(panelName+(i+1))){
	  		document.getElementById(panelName+(i+1)).className = (name == (panelName+(i+1))) ? 'panelActive':'panel';
	  	}
	}
	return false;
}

function showStatsPanel(tabName, name, noOfPanels)
{
	var panelName = "panelStats";
	if (selectedStatsTab){
	  	selectedStatsTab.className='tabStats';
	}
	if(document.getElementById(tabName)){
		selectedStatsTab = document.getElementById(tabName);
		selectedStatsTab.className='tabStatsSelected';
	}
	
	for(i = 0; i < noOfPanels; i++){
		if(document.getElementById(panelName+(i+1))){
	  		document.getElementById(panelName+(i+1)).className = (name == (panelName+(i+1))) ? 'panelStatsActive':'panelStats';
	  	}
	}
	return false;
}

function handleRegionSelectionChange(){
	var value = document.regionSearch.selectedRegion.value;
	if(value != "-1" ){
		window.location = value;
	}
}
function handleCitySelectionChange(){
	var value = document.citySearch.selectedCity.value;
	if(value != "-1" ){
		window.location = value;
	}
}

function banerOver(id){
	elem1 = document.getElementById("advertiseBanerImage" + id);
	elem1.className='apartmentAdvertise_over';
	elem2 = document.getElementById("advertiseBanerText" + id);
	elem2.className='apartmentAdvertise_over';		
}

function banerOut(id){
	elem1 = document.getElementById("advertiseBanerImage" + id);
	elem1.className='advertiseTableImages';
	elem2 = document.getElementById("advertiseBanerText" + id);
	elem2.className='advertiseTableText';		
}
	      
function handleNewsletterSubmit(){
	var email;
	email = document.getElementById('email').value;
	//email = document.forms['newsletterForm'].email.value;
	if(check_email(email)){
		ajaxSetContent('newsletterContainer','/fragments/components/addToNewsletter.jsp?email=' + email + '&dt=' + new Date(), '');
	} else {
		alert('Podaj adres email');
	}
}

function popupWithParams(url,params)
{
	newwindow=window.open(url,'resources',params);
	if (window.focus) {newwindow.focus()}
}




function check_email(e) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(e)){
		return (true)
	}
	return (false)
}

function gup( name ){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null )    
		return "";  
	else    
		return results[1];
}

	
	
	
	
	