
jQuery.fn.center = function (absolute) {
	return this.each(function () {
		
		var t = jQuery(this);

		t.css({
			position:	absolute ? 'absolute' : 'fixed', 
			left:		'50%', 
			top:		'50%', 
			zIndex:		'99'
		}).css({
			marginLeft:	'-' + (t.outerWidth() / 2)  + 'px', 
			marginTop:	'-' + (t.outerHeight() / 2) + 'px'
		});

		if (absolute) {
			t.css({
				marginTop:	parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
				marginLeft:	parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
			});
		}

	});
};


jQuery.fn.centerI6 = function() {
	return this.each(function() {
		
		
	this.style.display = 'block';
	  
	var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }


	   var IpopTop = (myHeight - this.offsetHeight)/2;
	   var IpopLeft = (myWidth - this.offsetWidth)/2;

	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }

	   IpopLeft = IpopLeft + scrOfX;
	   IpopTop = IpopTop + scrOfY;


	   this.style.left = IpopLeft + 'px';
	   this.style.top =  IpopTop + 'px';		
		
		
		
		
	});
}



function urlencode( str ) {
                                  
    var ret = str;
    
    ret = ret.toString();
    ret = encodeURIComponent(ret);
    ret = ret.replace(/%20/g, '+');
 
    return ret;
}

function logSearch(ip,action,ref,out) {
	var out = urlencode(out);
	var rurl = '/action.php?a=cl&ip='+ip+'&action='+action+'&ref='+urlencode(ref)+'&out='+out;

	jQuery.get(rurl);	
}

function refineSearch() {
	showRefineSearchDiv();
	jQuery.each($('#refinesearchForm .search_inputs'), function() {
		if(this.value == '')
			this.disabled=true;
	});
	$('#refinesearchForm').submit();
}

function ResetFilters() {
	$('#idDisplayCurrency').attr('value','');
	$('#idMinPrice').attr('value','');
	$('#idMaxPrice').attr('value','');
	$('#idPropertyTypeFilter').attr('value','');
	$('#idAvailableOnly').attr('value','false');
	$('#idStarRatingFilter').attr('value','');
	$('#idChainIDFilter').attr('value','');
	$('#idFacilityFilter').attr('value','');
	$('#idHotelNameContains').attr('value','');
	$('#idLongitude').attr('value','');
	$('#idLatitude').attr('value','');
	$('#idDistance').attr('value','');
	refineSearch();
}


function showRefineSearchDiv() {

	var winH = $(window).height();  
	var winW = $(window).width();  
	
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		$('#refineSearchDiv').centerI6();
	} else 
		$('#refineSearchDiv').center();
	$('#refineSearchDiv').show();

}


function showGetRates(hotelid) {
	
	$('#searchHotelIDvalue').val(hotelid);
	
	var winH = $(window).height();  
	var winW = $(window).width();	

	var maskHeight = $(document).height();
    var maskWidth = $(window).width();

	if (($.browser.msie && $.browser.version.substr(0,1)<7) || ($.browser.opera)) {
		$('#getRatesDiv').centerI6();
	 	$('#getRatesDiv').fadeIn(500);
		$('#getRatesDiv').bgiframe();	
	} else {
		$('#mask').css({ 'display' : 'block', opacity : 0,width: maskWidth,height: maskHeight});  
		$('#mask').fadeTo(500,0.8);  
		$('#getRatesDiv').center();
	 	$('#getRatesDiv').fadeIn(500);
	}
	

}

function closeGetRates() {
	$('#mask').fadeOut(500);
	$('#getRatesDiv').fadeOut(500);
}


function toggleFullAmenities() {
	$('#expandedAmenities').toggle();
	$('#expandedAmenitiesLink').toggle();
	$('#shortAmenities').toggle();
}

function toggleFullChains() {
	$('#expandedChains').toggle();
	$('#expandedChainsLink').toggle();
	$('#shortChains').toggle();
}

function PageIndex(page) {
	$('#idPageIndex').attr('value',page);
	refineSearch();
}

function DisplayCurrency(current) {
	var curr = $('#sCurrency :selected').attr('value');
	if(curr != current)  {
		$('#idDisplayCurrency').attr('value',curr);
		$('#idMinPrice').attr('value','');
		$('#idMaxPrice').attr('value','');
	}
	refineSearch();
}

function SearchHotelAtTop() {
	var ao = $('#sSearchHotelAtTop').attr('checked');
	if(ao) 
		$('#idSearchHotelAtTop').attr('value','true');
	else
		$('#idSearchHotelAtTop').attr('value','false');

	refineSearch();
}

function AvailableOnly() {
	var ao = $('#sAvailableOnly').attr('checked');
	if(ao) 
		$('#idAvailableOnly').attr('value','true');
	else
		$('#idAvailableOnly').attr('value','false');

	refineSearch();
}

function SortField(field, asc) {
	if(asc == 1) 
		$('#idSortAscending').attr('value','true');
	else
		$('#idSortAscending').attr('value','false');
	$('#idSortField').attr('value',field);
	$('#idPageIndex').attr('value','0');
	refineSearch();
}

function StarRating(star) {

	var ch = $('#sStarRating_'+star).attr('checked');
	if(!ch && star == 0) {$('#sStarRating_'+star).attr('checked','true'); return false;}
	var filter='';
	
	jQuery.each($("#tStarRating :checkbox"), function() {
		if(ch && star == 0) {
			if(this.value!=0) this.checked=false;	
		} else {
			if(ch && this.value == 0) this.checked=false;
			if(this.checked == true && this.value != 0) {
				if(this.value == star && !ch) {} 
					else
					  filter = filter + this.value + ',';
			}
		}
	});
	filter = filter.substring(0,filter.length-1);	
	if(filter.length > 0) {
		$('#idStarRatingFilter').attr('value',filter);
	} else 
		$('#idStarRatingFilter').attr('value','');
	refineSearch();
}

function Distance() {
	var distance = $('#distance :selected').val();
	var current_distance = $('#idDistance').val();
	
	if(distance != current_distance) {
		if(distance > 0)
			$('#idDistance').val(distance);
		else
			$('#idDistance').val('');
	}	
	refineSearch();
}

function ChainCode(chain, prefix) {
	var ch = $('#'+prefix+'sChainCode_'+chain).attr('checked');

	if(!ch && chain == 0) {$('#'+prefix+'sChainCode_'+chain).attr('checked','true'); return false;}
	var filter='';
	
	jQuery.each($("#hotelBrandsFilter .tChainCode :checkbox"), function() {
		if(ch && chain == 0) {
			if(this.value != 0) this.checked=false;	
		} else {
			if(ch && this.value == 0) this.checked=false;
			if(this.checked == true && this.value != 0) {
				if(this.value == chain && !ch) {} 
					else
					  filter = filter + this.value + ',';
			}
		}
	});
	filter = filter.substring(0,filter.length-1);	

	if(filter.length > 0) {
		$('#idChainIDFilter').attr('value',filter);
	} else 
		$('#idChainIDFilter').attr('value','');
	refineSearch();
}

function FacilityCode(code, prefix) {
	
	var ch = $('#'+prefix+'sFacilityCode_'+code).attr('checked');

	if(!ch && code == 0) {$('#'+prefix+'sFacilityCode_'+code).attr('checked','true'); return false;}
	
	var filter='';
	
	jQuery.each($("#hotelFacilityFilter .tFacilityCode :checkbox"), function() {
		if(ch && code == 0) {
			if(this.value != 0) this.checked=false;	
		} else {
			
			if(ch && this.value == 0) this.checked=false;
			

			if(this.checked == true && this.value != 0) {
				if(this.value == code && !ch) {} 
					else
					  filter = filter + this.value + ',';
			}
		}
	});
	
	filter = filter.substring(0,filter.length-1);

	if(filter.length > 0) {
		$('#idFacilityFilter').attr('value',filter);
	} else 
		$('#idFacilityFilter').attr('value','');
	refineSearch();
}

function PropertyCode(code, prefix) {
	var ch = $('#'+prefix+'sPropertyCode_'+code).attr('checked');
	if(!ch && code == 99) {$('#'+prefix+'sPropertyCode_'+code).attr('checked','true'); return false;}
	
	var filter='';
	
	jQuery.each($("#hotelPropertyTypeFilter .tPropertyCode :checkbox"), function() {
		if(ch && code == 99) {
			if(this.value != 99) this.checked=false;	
		} else {
			if(ch && this.value == 99) this.checked=false;
			if(this.checked == true && this.value != 99) {
				if(this.value == code && !ch) {} 
					else
					  filter = filter + this.value + ',';
	  		}
		}
	});
	
	filter = filter.substring(0,filter.length-1);
	if(filter.length > 0) {
		$('#idPropertyTypeFilter').attr('value',filter);
	} else 
		$('#idPropertyTypeFilter').attr('value','');
	refineSearch();
}


function PriceFilter(lowprice, highprice) {
	$('#idMinPrice').attr('value',lowprice);
	$('#idMaxPrice').attr('value',highprice);	
	refineSearch();
}

function ResetHotelNameContains() {
	$('#vHotelNameContains').val('');
	$('#idHotelNameContains').val('');
	$('#idPageIndex').val('');
	refineSearch();
}

function HotelNameContains() {
	var query = $('#vHotelNameContains').val();
	if(query.length > 3) {
		$('#idHotelNameContains').val(query);
		$('#idPageIndex').val('');
		refineSearch();
	} else {
		alert('The search query is empty or too short');
	}
}

function searchCompareAllHotels() {
	$('#idSearchHotelID').val('');
	$('#idSearchHotelID2').val('');
	$('#search_hotel_msg_id').hide();
	$('#hotelListDiv .searched_hotel').hide();
	refineSearch();
}

function submitGetRatesForm2() {
	// validate
	var errormsg = "Oops, something went wrong:\n\n";
	var error = false;

	if($('#start-date2').val().length < 10) {
		errormsg=errormsg+"- Checkin Date Missing or Wrong Format\n";
		error = true;
	}	
	
	if($('#start-date2').val().length < 10) {
		errormsg=errormsg+"- Checkin Date Missing or Wrong Format\n";
		error = true;
	}
	if($('#end-date2').val().length < 10) {
		errormsg=errormsg+"- Checkout Date Missing or Wrong Format\n";
		error = true;
	}		
	errormsg = errormsg + "\nPlease fix the errors above and try again.";

	if(!error) jQuery('#getRatesForm2').submit();
	else
		alert(errormsg);
}

function submitGetRatesForm() {
	// validate
	var errormsg = "Oops, something went wrong:\n\n";
	var error = false;
	
	if(($('#start-date').val().length < 10) || $('#start-date').val() == 'yyyy-mm-dd') {
		errormsg=errormsg+"- Checkin Date Missing or Wrong Format\n";
		error = true;
	}
	
	if(($('#end-date').val().length < 10) || $('#end-date').val() == 'yyyy-mm-dd') {
		errormsg=errormsg+"- Checkout Date Missing or Wrong Format\n";
		error = true;
	}		
	errormsg = errormsg + "\nPlease fix the errors above and try again.";

	if(!error) jQuery('#getRatesForm').submit();
	else
		alert(errormsg);
}