function getXMLHttpRequest(){
    var request = false;
    try{
        request = new XMLHttpRequest(); 
    }catch( err1 ){
        try{
            request = new ActiveXObject('Msxm12.XMLHTTP');
        }catch(err2){
            try{
                request = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(err3){
                request = false;
            }
        }
    }
    //request.setRequestHeader('X_REQUESTED_WITH', "XMLHttpRequest");
    return request;
}

function getFileName(){
     
     //vars
     sMyUrl = window.location.href;
     sHostName = window.location.hostname;
     sAvoid = "index";
     sMySub2 = ".php";   
   
     if((iIndex = sMyUrl.indexOf(sMySub2)) > -1){ 
        if(sMyUrl.indexOf(sAvoid + sMySub2) < 0 ){ 
            return sMyUrl.substr(0, iIndex + sMySub2.length);
        } 
     } 
      return sMyUrl.substr(0, (sMyUrl.indexOf(sHostName) + sHostName.length));
     
}

function disableElement(sId, bDisable){

    var oObject = document.getElementById(sId);
    
    if(bDisable){
        oObject.setAttribute('disabled', 'disabled');
    } else{
        oObject.removeAttribute('disabled');
    }
    
}

function changeButtonImg(oSource, sPath){
    oSource.src = getFileName() + '/' + sPath;

}

function link_underline(iId, bUnderline){
    
    if(bUnderline){
        document.getElementById(iId).style.textDecoration = 'underline';
    } else {
        document.getElementById(iId).style.textDecoration = 'none';
    }

}

function changeSwitcher(iIds, iCountInteresting, bResetTime){
    
    //get iCurrentSwitch global
        
    if(iCurrentSwitch != iIds){
    iCurrentSwitch = iIds;
    jQuery('#switcher_loader').show();
    
    //set timer again to 0
    if(bResetTime != undefined){
      clearTimeout(oTimer); 
      switchNextInterval(iCountInteresting)
    }
    
    for(var i = 0; i < iCountInteresting; i++){
        
        if(i == iIds){

		    var oDiv = document.getElementById('switcher_box' + iIds);
		    var oLink = document.getElementById('swither_link_id' + iIds);

		    oLink.className =  'inactive'
		    oDiv.className = 'active holder'; 
		    oDiv.blur();
		    
		    oLink.onmouseover = function(){
             };
             
            oLink .onmouseout = function(){
               this.style.textDecoration = 'none';
             };
             
            oDiv.onmouseover = function(){
             };
             
            oDiv.onmouseout = function(){
            this.style.textDecoration = 'none';
             };
             
            oLink.style.textDecoration = 'none';
		    
		    
        }else {
          try{//sometimes error accour
            
		    var oDiv = document.getElementById('switcher_box' + i);
            var oLink = document.getElementById('swither_link_id' + i); 

            oLink.className = ''; 

            oDiv.className = 'holder';
            
            oLink.onmouseover = function(){
             this.style.textDecoration = 'underline';
             };
             
            oLink .onmouseout = function(){
             this.style.textDecoration = 'none';
             };
             
            oDiv.onmouseover = function(){
             this.style.textDecoration = 'underline';
             };
             
            oDiv.onmouseout = function(){
             this.style.textDecoration = 'none';
             };
          
		    
		    oLink.style.textDecoration = 'none';

		    oDiv.className = 'holder'; 
		    oDiv.blur();
		    
		   }catch(err){}
        }
        
    }
    
   loadNextInterestingOffer(iIds); 
   }    
}


function _generateStars(iNumber, sDestination){
        
          var sPath = '';  
          
          if(iNumber > 1){
              var iStars  = parseInt((iNumber/ 10));
              for(var i = 0; i < iStars; i++){
              sPath +='<img src="/images/frontend/icons/star.gif" class="star">';
              }
              if((iNumber)%10){ 
               sPath +='<img src="/images/frontend/icons/star-half.gif" class="star-half.gif">';
              }
          }
          
          jQuery('#' + sDestination).html(sPath);
}

function switchNextInterval(iCountInteresting){
    
       oTimer = setTimeout(function () {
        
            var iNext = iCurrentSwitch+1;
            
            if((iNext) >= iCountInteresting){  
                iNext = 0;
            } 

            changeSwitcher(iNext, iCountInteresting);
            iCurrentSwitch = iNext; 
            
            switchNextInterval(iCountInteresting);
        }, iTimeout);

}

function getHostName(){
  sHostName = 'http://' + window.location.hostname;
  return sHostName;
}

function Set_Cookie( name, value, expires, path, domain, secure ){
	
	var today = new Date();
	    today.setTime( today.getTime() );
	
		if ( expires ){
		     expires = expires * 1000 * 60 * 60 * 24;
		}
		
	var expires_date = new Date( today.getTime() + (expires) );
	
		document.cookie = name + "=" +escape( value ) +
						( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
						( ( path ) ? ";path=" + path : "" ) + 
						( ( domain ) ? ";domain=" + domain : "" ) +
						( ( secure ) ? ";secure" : "" );

}

function Get_Cookie( check_name ) {

    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f
    
    for ( i = 0; i < a_all_cookies.length; i++ )
    {
        a_temp_cookie = a_all_cookies[i].split( '=' );
        
        
        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
    
        if ( cookie_name == check_name )
        {
            b_cookie_found = true;
            if ( a_temp_cookie.length > 1 )
            {
                cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
            }
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if ( !b_cookie_found )
    {
        return null;
    }
}

function Delete_Cookie( name, path, domain ) {
 
  if ( Get_Cookie( name ) ) {
     document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

function getFullWebSitePath(){
    return window.location.href;
}
  
function rememberWebSitePath(){

    var sCurrentPath = getFullWebSitePath();
    Set_Cookie( getWebSiteRefererName(), getFullWebSitePath(), '', '/', '', '' );

}

function rememberWebSiteSearch(){

    var sCurrentPath = getFullWebSitePath();
    Set_Cookie( getWebSiteRefererNameSearch(), getFullWebSitePath(), '', '/', '', '' );

}

function getWebSiteRefererNameSearch(){
    var sName = 'web_site_referer_search' + getUserWindowName();
    return sName;

} 
function getWebSiteRefererName(){
    var sName = 'web_site_referer' + getUserWindowName();
    return sName;

}

function checkIsRememberPathSearch(){

    if(Get_Cookie(getWebSiteRefererNameSearch())){
        jQuery("#back_referer_path").show();
    }
} 

function returnToSavedWebSitePath(){
    
    window.location.href = Get_Cookie(getWebSiteRefererName());
    Delete_Cookie(getWebSiteRefererName(), '/', '');
    
} 

function returnToSavedWebSitePathSearch(){
    
    window.location.href = Get_Cookie(getWebSiteRefererNameSearch());
    Delete_Cookie(getWebSiteRefererNameSearch(), '/', '');
    
} 

function checkIsRememberPath(){

    if(Get_Cookie(getWebSiteRefererName())){
        jQuery("#back_referer_path").show();
    }
}

function getWebSiteRefererName(){
    var sName = 'web_site_referer' + getUserWindowName();
    return sName;

}

function getUserWindowName(){

              var curdate = new Date();
              var hours = curdate.getHours(); 
              var minutes = curdate.getMinutes(); 
              var seconds = curdate.getSeconds();
       
	          if(window.name == ''){
	             window.name =  (hours + '_' + minutes + '_' + seconds);
	           }
            
            return window.name;
}

function blurThis(oObject){
    oObject.blur();
}

function onlydec(event){
	if (event.srcElement) {kc = event.keyCode;} else {kc = event.which;}
	if ((kc < 47 || kc > 57) && kc != 8 && kc != 0) return false;
 return true;
}

function checkIsHigher(oObject, iValue){
     oObject.value = (parseInt(oObject.value) > iValue || parseInt(oObject.value) < 0) ? iValue : oObject.value;
}

function echeck(str) {

        var at="@"
        var dot="."
        var lat=str.indexOf(at)
        var lstr=str.length
        var ldot=str.indexOf(dot)
        if (str.indexOf(at)==-1){
           //alert("Invalid E-mail ID")
           return false
        }

        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
           return false
        }

        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
            return false
        }

         if (str.indexOf(at,(lat+1))!=-1){
            return false
         }

         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
            return false
         }

         if (str.indexOf(dot,(lat+2))==-1){
            return false
         }
        
         if (str.indexOf(" ")!=-1){
            return false
         }
         
         //TODO dopisac niedowzwolne litery

         return true                    
}

function validateDate(sDate){
        
      var sDate = sDate.replace(/^\s+|\s+$/g, '');
      var regExp = /^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/i;
        
      var matches = sDate.match(regExp);
      if(matches == null){
       return false;
      } else {
       return true;
      }
}

function checkAllCheckbox(sId, bCheck){
    
    if(bCheck){
        jQuery('#' + sId + ' :checkbox').attr('checked', 'true');
    } else {
        jQuery('#' + sId + ' :checkbox').removeAttr('checked');
    }

}
function checkDifferenceCheckbox(sId){
    
        jQuery('#' + sId + ' :checkbox').each(function(i){
	      if(jQuery(this).attr('checked')){
	            jQuery(this).removeAttr('checked');
	        } else {
	            jQuery(this).attr('checked', 'true');
          }
        });
}

function createMultipleBrowseFileButton(sDestinationId, sCountIdListener, sClass, sDel){
        
        var iCounter = parseInt(jQuery('#' + sCountIdListener).val());
        iCounter++;
        
        if(sDel == undefined){
           sDel = false;     
        }
        
        jQuery('#' + sCountIdListener).val(iCounter);
        var sFile = '<div id="file_holder_' + iCounter + '"><input type="file" name="file_browser_' + iCounter + '" id="file_browser_' + iCounter + '" value="" class="' + sClass +'" /> ' 
        + ( sDel ? '<a href="#" onclick="createDelFileButton(' + iCounter + ', \'' +  sCountIdListener + '\'); return false;"><img src="/images/frontend/icons/usun.gif" /></a>' : '' )  + '</div>';
        jQuery('#' + sDestinationId).append(sFile);    
}

function createDelFileButton(iCounter, sCountIdListener){
    
    var iCounter = parseInt(jQuery('#' + sCountIdListener).val());
    
        jQuery('#file_holder_' + iCounter).remove();
        
        jQuery('#' + sCountIdListener).val(--iCounter);
        
        jQuery('#file_counter_el_span').html(parseInt(iCounter) == '' ? '0' : iCounter);
        
        if(iCounter < 9){
           jQuery('#upload-file-link').show();
        }
}



function doHoverEffectImg(oObject, sFormat){
    var sSrc = '';

    jQuery(oObject).hover(
        function () {
	        sSrc = $(this).attr('src');
	        sSrc = sSrc.replace('.' + sFormat ,'_hover.' + sFormat);
	        $(this).attr('src', sSrc);
        }, 
        function () {
            sSrc = $(this).attr('src');
	        sSrc = sSrc.replace('_hover.' + sFormat, '.' + sFormat);
	        $(this).attr('src', sSrc);
        }
    );
}

function rememberCookiePath(sName){
    Set_Cookie((sName  + getUserWindowName()), getFullWebSitePath(), '', '/', '', '' );
} 

function checkIsRememberPathCookie(sName){

    if(Get_Cookie((sName  + getUserWindowName()))){
        jQuery("#back_referer_path").show();
    }
} 

function returnToSavedWebSitePathCookie(sName){
    window.location.href = Get_Cookie((sName  + getUserWindowName()));
    Delete_Cookie((sName  + getUserWindowName()), '/', '');
}

function getPercent(iCost, iPercent){
    var aData = new Array();
    
    if(iPercent == undefined){
       iPercent = 5; 
    }
    
    //aData[0] =  Math.floor((iCost * iPercent)/100); 
    //aData[1] =  Math.floor((iCost - ((iCost * iPercent)/100))); 
   
    aData[0] =  (iCost * iPercent)/100; 
    aData[1] =  (iCost - ((iCost * iPercent)/100)); 
    
    return aData;
} 

function addToFavorites(){

	 var sTitle = document.title; 
	 var sUrl = window.location.href;
	 
	 //ie
	 try{
	      if(document.all){
	        window.external.AddFavorite(sUrl, sTitle); 
	        return false;
	      }
	 }catch(e){}
     
    //firefox 
    try{ 
         if (window.sidebar) {
             window.sidebar.addPanel(sTitle, sUrl, ""); 
             return false;
        }
     }catch(e){}
    
    //opera    
     try{ 
         if (window.opera && window.print){   
		    var a = document.createElement('a');
		        a.href=location.href;
		        a.rel='sidebar';
		        a.title=document.title;
	            a.onclick=function() {
			        //return void(0); 
			      }
		        a.click();
		        /* this is bug for opera? who knows :)? it works
		           you need to trigger error to open favourite box   
		        */
		        var newErrorObj = new Error('', ''); 
	     }   
      }catch(e){}

}

function getCurrencyFormat(iCost, sCulture){

    if(sCulture == 'pl'){
        return ( iCost + 'zł' );
    } else {
        return ( '€' + iCost);
    }

}

function validateTerm(sId){
    
    var sDate = jQuery('#' + sId + '').val();
        sDate = sDate.replace(/^\s+|\s+$/g, '') ;
    var patt = new RegExp(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i);
        return patt.test(sDate);

}

function redirectToReservation(sPath){

    window.location.href= sPath;
}

function setCroatiaDate(iNormal, iCroatia){
   //alert('' + iNormal + ',' + iCroatia);
   if(parseInt(jQuery('#country').val()) == 12){
      jQuery('#arrival_time').val(iCroatia);  
   } else {
      jQuery('#arrival_time').val(iNormal);  
   }
}
