
// ========= Return element id =============================
function getEl(sId) {
  return (document.all? document.all(sId): (document.getElementById? document.getElementById(sId): null));
}


// ========= Submit form - add unit to wish list ===========
function addToWishList() {
	addProperty.submit();
}


// ========= Submit form - rermove unit from wish list =====
function removeFromWishList( nID ) {
	location.href = 'addToWishList.asp?del='+ nID;
}


// ========= Create array - dinamic filled options =========
function makeListOfCities( nRegion ) {
 niz = eval( 'aVar'+ nRegion );
 document.mainSearch.fCity.options.length = 1;
 if (nRegion != "") {
	document.mainSearch.fCity.options[0] = new Option('---------', 0);
  for (count=0; count< niz.length; count++) {
    option = new Option(niz[count].city, niz[count].id);
    document.mainSearch.fCity.options[count+1] = option;
  }
 } else {
    document.mainSearch.fCity.options[0] = Option('---------', '');
 }
}


// ========= search paging

sActiveDiv	= 'page1';
sPagingCon	= 'pagingContainer';

function showFirstResult() {
	getEl( sActiveDiv ).style.display = 'block';
}

function goToPage( nID, nTotal ) {
	getEl( sActiveDiv ).style.display 	= 'none';
	sActiveDiv													= 'page'+ nID;
	getEl( sActiveDiv ).style.display 	= 'block';
	getEl( sPagingCon ).innerHTML 			= createPaging( nID, nTotal );
}

function createPaging( nID, nTotal ) {
	sResult	 = '';
	if ( nTotal > 1 ) {
		sResult += ( nID >1 ) ? formatPagingDefault( nID-1, nTotal, '&laquo;' ): '';
		for ( i=1; i<=nTotal; i++ ) {
			sResult += ( i == nID ) ? formatPagingActive( i ): formatPagingDefault( i, nTotal );
			sResult += formatPagingSeparator()
		}
		sResult += ( nID < nTotal ) ? formatPagingDefault( nID+1, nTotal, '&raquo;' ): '';
	}
	else {
		sResult
	}
	return ( sResult );
}

function formatPagingActive( nVar ) {
	sHTMLResult = ' <span>'+ nVar +'</span> ';
	return ( sHTMLResult );
}

function formatPagingDefault( nVar, nTotal, sOption ) {
	sValue			= ( sOption ) ? sOption: nVar;
	sHTMLResult = ' <a href="javascript:goToPage( '+ nVar +', '+ nTotal +' )">'+ sValue +'</a> ';
	return ( sHTMLResult );
}

function formatPagingSeparator() {
	sHTMLResult = '|';
	return ( sHTMLResult );
}


function zoomPic( sPicName ) {
	var winURL 			= '/pic.asp?s=' + sPicName
  var winIme 			= 'Pic'
  var winAtributi 	=
    'width=360'+ 
    ',height=270'+ 
    ',toolbar=no'     +
    ',location=no'    +
    ',directories=no' +
    ',status=no'      +
    ',menubar=no'     +
    ',scrollbars=yes' +
    ',resizable=no'  ;

  var win = eval("window.open(winURL, winIme, winAtributi)");
	win.focus();
}

function openEmailPrivacy() {
	var winURL 			= '/popup_email_privacy.asp'
  var winIme 			= 'Pic'
  var winAtributi 	=
    'width=500'+ 
    ',height=400'+ 
    ',toolbar=no'     +
    ',location=no'    +
    ',directories=no' +
    ',status=no'      +
    ',menubar=no'     +
    ',scrollbars=yes' +
    ',resizable=no'  ;

  var win = eval("window.open(winURL, winIme, winAtributi)");
	win.focus();
}

// ========= Refresh prozor za fotke ========================
// ==========================================================
   function refresh()
   {
     var picWidth 		= getEl( 'bigPic' ).width;
     var picHeight 		= getEl( 'bigPic' ).height;
		 if ( navigator.appName == 'Netscape' ) {
	   	 self.resizeTo( picWidth+66, picHeight+102 );
	   }
	   else {
	    	self.resizeTo( picWidth+76, picHeight+100 );
	   }
   }	


// ========= Fill title for page ========================
// ==========================================================
   function SetPageTitle( sVar )
   {
     document.title += sVar;
   }	

	 
	 
// ========= Check valid e-mail ========================
// ==========================================================	 
function checkMail( sEmail )
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(sEmail)) return true;
	else return false;
}
	 
	 
// ========= Ajax functions ========================
// ==========================================================	 


// Process newsletter

function sendNewsletter( sName, sEmail1, sEmail2 ) 
{
	if ( sName == '' )
	{
		alert ('Please enter your name!');
	}
	else {
		if ( checkMail( sEmail1 ) )
		{	
			if ( sEmail1 == sEmail2 )
			{
				ProcessLoading();
			  if( window.XMLHttpRequest ) { http_request = new XMLHttpRequest(); }
			  else if( window.ActiveXObject ) { http_request = new ActiveXObject( 'Microsoft.XMLHTTP' ); }
			  http_request.onreadystatechange = processNewsletterForm;
			  http_request.open( 'GET', '/form_sendNewsletter.asp?email='+ sEmail1 +'&name='+ sName, true );
			  http_request.send( null );
			}
			else {
				alert ('Fields "E-mail" and "Confirm e-mail" must be identical!');
			}
		}
		else {
			alert ('Please enter a valid e-mail address!');
		}
	}
}

function processNewsletterForm() {
  if( http_request.readyState == 4 ) {
    if( http_request.status == 200 ) {
			getEl('newsletter').innerHTML = http_request.responseText;
    }
    else { alert( 'there was a problem with the request!'+ http_request.status ); }
  }
}

function ProcessLoading()
{
    getEl('newsletter').innerHTML = '<img src="/images/loading.gif" width="32" height="32" alt="" border="0" />';
}


// Process main search engine
function fillCitiesForRegion( sID, nSelected ) 
{
	  if( window.XMLHttpRequest ) { http_request = new XMLHttpRequest(); }
	  else if( window.ActiveXObject ) { http_request = new ActiveXObject( 'Microsoft.XMLHTTP' ); }
	  http_request.onreadystatechange = processCities;
	  http_request.open( 'GET', '/ajax/fill_cities.asp?reg='+ sID +'&sel='+ nSelected, true );
	  http_request.send( null );
}

function processCities() {
  if( http_request.readyState == 4 ) {
    if( http_request.status == 200 ) {
			getEl('search_cities').innerHTML = http_request.responseText;
    }
    else { alert( 'there was a problem with the request!'+ http_request.status ); }
  }
}


// format number
	function format_prize( x ) {
    var ostatak = Math.round( 100 * (x - Math.round( x-0.5 )) );
    var cjelobrojno = Math.round( x-0.5 )+'';
    var broj = '';
    var nCount = 0;
    for( var i=cjelobrojno.length-1; i>=0; i-- ) {
      broj = cjelobrojno.substr( i, 1 ) + broj;
      ++ nCount;
      if( (nCount == 3) && (i>0) ) {
        broj = '.' + broj;
        nCount = 0;
      }
    }
		//ostatak	= ( ostatak != '00' ) ? ostatak : '-';
		//ostatak	= ( (ostatak+'').length > 2 ) ? (ostatak+'').substr(0, 2) : ostatak;
		result	= ( ostatak >= 50 ) ? parseInt(broj) + 1 : broj;
    return (result);
  }