/*
	Copyright Code Computerlove Ltd 2007-2009
	Build: 2.0.0.136
	Date: 03/07/2009 15:01:40
*/

//
// Set up globals
// 
jQuery.noConflict();
var $j = jQuery;
var isIE6 = false; var isSafari = false;

//
// Document ready. Let's go!
//
$j(document).ready(function() {
	// Fire local page initialize methods
	if (typeof initPage == "function"){
		initPage();
	}
});

//
// Appends "IsAjaxRequest" to a url
//
function appendIsAjaxRequest(url){
	if (url.indexOf("isajaxrequest") < 0){ if (url.indexOf("?") < 0) { url += "?isajaxrequest=true"; } else { url += "&isajaxrequest=true"; } }
	return url;
};

//
// Replaces the inner html of an element with the results of an ajax request
//
function ajaxReplace(href, containerId){
	$j.ajax({
		url: appendIsAjaxRequest(href),
		dataType: "html",
		success: function(response){
			$j(containerId).html(response);
		}
	});
};

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		};
	}
}
function hideme() {
	if (!document.getElementById("hideme")) return false;
	if(document.getElementById) {
		var objDiv = document.getElementById('hideme');
		if(objDiv)objDiv.style.display="none";
	}
}
function showme() {
	var objDiv = document.getElementById('hideme');
	if (objDiv != null) {
		if (objDiv.style.display=="none") {
			objDiv.style.display="";
		} else {
			objDiv.style.display="none";
		}
	}
}
function quickBasket() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("basket");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				};
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				};
			}
		}
	}
}
addLoadEvent(hideme);
addLoadEvent(quickBasket);

function screenResolution(targetObject,targetClass) {
	
	if ( !document.getElementById ) { return false; }
	if ( !document.getElementById(targetObject) ) { return false; }
	
	body = document.getElementById(targetObject);
	var bodyClass = body.className;
	var documentWidth = 0;
	
	if ( typeof( window.innerWidth ) == 'number' ) {
		documentWidth = window.innerWidth; /* Non-IE */
	} else if ( document.documentElement && ( document.documentElement.clientWidth ) ) {
		documentWidth = document.documentElement.clientWidth; /* IE 6+ in 'standards compliant mode' */
	} else if ( document.body && ( document.body.clientWidth ) ) {
		documentWidth = document.body.clientWidth; /* IE 4 compatible */
	}
	
	if ( documentWidth < "900" ) {
		bodyClass = bodyClass.replace(' ' + targetClass, '').replace(targetClass, '');
		body.className = bodyClass;
	} else {
		bodyClass = bodyClass + ' ' + targetClass;
		body.className = bodyClass;
	}
}

function initHomePage() { // empty stub to prevent homepage breaking
}

/* search form related - return value will determine if form is posted
		- blindTypeId, blindColourId, offerId, starRatingId
*/
function validateSearch(btId, bcId, oId, srId)
{
	if (!document.getElementById)
		return true;
	
	var bt = document.getElementById(btId);
	var bc = document.getElementById(bcId);
	var o = document.getElementById(oId);
	var sr = document.getElementById(srId);
	
	if (bt == null || bc == null || 0 == null || sr == null)
		return true;
	
	if (
		parseFloat(bt.options[bt.selectedIndex].value) == 0 &&
		parseFloat(bc.options[bc.selectedIndex].value) == 0 &&
		parseFloat(o.options[o.selectedIndex].value) == 0 &&
		parseFloat(sr.options[sr.selectedIndex].value) == 0)
	{
		alert('Please select at least 1 search option. Thank you.');
		return false;
	}
	
	return true;
}

/* preset the search fields given a blind type and blind colour (might be image range!) */
function presetSearch(btId, btVal, bcId, bcVal)
{
	if (!document.getElementById || btVal == 0)
		return;
	
	var bt = document.getElementById(btId);
	var bc = document.getElementById(bcId);
	
	if (bt == null || bc == null)
		return;
	
	var i;
	
	// Preset the blind type dropdown
	for (i = 0; i < bt.options.length; i++)
	{
		if (parseInt(bt.options[i].value) == btVal)
		{
			bt.options[i].selected = true;
			break;
		}
	}
	
	// Get the right set of options to appear
	temp = bc;
	redirect(bt.selectedIndex - 2); // Need to allow for 'Please select' in blind type dropdown
	
	// Preset the blind colour dropdown
	for (i = 0; i < bc.options.length; i++)
	{
		if (parseInt(bc.options[i].value) == bcVal)
		{
			bc.options[i].selected = true;
			break;
		}
	}
}

var cTT;

function get(id) {
	var e;
  if(document.getElementById) e=document.getElementById(id);
  else if(document.all) e=document.all[id];
  return e;
}

function showTT(id) {
	var e=get(id);
	if(e) {
		e.style.display="block";		
		cTT = e;
		document.onmousemove = posTT;		
	}
}
function hideTT(id) {
	var e=get(id);
	if(e) {
		e.style.display="none";
		cTT = null;
		document.onmousemove = null;
	}
}
function posTT(e) {
	if(cTT != null) {	
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY)	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) {
			posx = e.clientX;
			posy = e.clientY;
			if (document.all)
			{
				posx += document.body.scrollLeft;
				posy += document.body.scrollTop;
			}
		}	
		cTT.style.left = (posx+5) + "px";
		cTT.style.top = (posy+5) + "px";				
	}
}

// JavaScript Document

// Event when mouse moves over the calendar icon
function CalendarIconMouseOver(objItem){
	objItem.style.background="#666666";
}


// Event when mouse moves out of the calendar icon
function CalendarIconMouseOut(objItem){
	objItem.style.background="";
}

// Focus on an element
function focusOnElement( objName )
{
	obj = document.getElementById( objName );
	if ( obj )
	{
		obj.focus();
	}
}

// Calendar Date Check
var satOnly = false;
var SPECIAL_DAYS = {};
var earliestYear;
var earliestMonth;
var earliestDay;

function checkDate( dt, yr , mth, dy )
{	
	//alert( 'start ' + yr + ' ' + mth + ' ' + dy );
	if ( dt.getDay() == 0 )
	{
		return true;
	}
	if ( satOnly == true )
	{
		if ( dt.getDay() != 6 )
		{
			return true;
		}
	}
	else
	{
		if ( dt.getDay() == 6 )
		{
			return true;
		}
	}
	if ( dateIsSpecialv2( yr , mth, dy ) ) return true;
	//alert( 'date not special ' );

	if ( yr < earliestYear )
	{
		return true;
	}
	if ( yr == earliestYear )
	{
		//alert( 'year equal ' );
		if ( mth < earliestMonth )
		{
			return true;
		}
		if ( mth == earliestMonth )
		{
			//alert( 'month equal ' );
			if ( dy < earliestDay )
			{
				//alert( 'day less than ' + earliestDay );		
			
				return true;
			}
		}
	}
	return false;
}
		  
function dateIsSpecial(year, month, day)
	{
	var m = SPECIAL_DAYS[ month ];
	if (!m ) return false;
	for (var i in m) if ( m[i] == day ) return true;
	return false;
}

function dateIsSpecialv2(year, month, day)
{
	for(i=0;i<SPECIAL_DAYS.length;i++)
	{
		if( SPECIAL_DAYS[i].getFullYear()==year && SPECIAL_DAYS[i].getMonth()==month && SPECIAL_DAYS[i].getDate()==day)
		{
			return true;		
		}
	}
	return false;
}


function open_rc(options){
	var win="/flash/roomcreator/default.aspx"+ options;
	var Wdth=650;var Hgt=530;var cen_x=(screen.availWidth/2)-(Wdth/2);var cen_y=(screen.availHeight/2)-(Hgt/2);
	window.open( win, "rc", "screenX="+cen_x+",screenY="+cen_y+",top="+cen_y+",left="+cen_x+",width=" + Wdth + ",height=" + Hgt + ",toolbar=0,location=0,directories=0,status=1,menuBar=0,scrollBars=0,resizable=0");
}


function launchRCFromCompare() {
	var f=document.forms[0];
	if(f) {
		var rcqs='?room_id=-99';var j=0;
		for(var i=0; i<f.elements.length; i++) {
			if(f.elements[i].name=='RCCB' && f.elements[i].checked) {
				var sa=f.elements[i].value.split('|');
				j++;
				rcqs+='&c_c'+j+'id='+sa[0];
				rcqs+='&c_c'+j+'d_text='+escape(sa[1]);
				rcqs+='&c_c'+j+'file='+escape(sa[2]);
			}
		}
		if(j>0) {
			open_rc(rcqs);
		}
	}
}

function setSizesFromWindow(s, t1, t2, sr) {
	if(document.getElementById) {
		var sel = document.getElementById(s);
		var tx1 = document.getElementById(t1);
		var tx2 = document.getElementById(t2);
		var selRoom = document.getElementById(sr);
		if(sel&&tx1&&tx2&&selRoom) {
			var s=sel.options[sel.selectedIndex].value;
			var sa=s.split("|");
			if(sa.length==4) {
				tx1.value=sa[1];
				tx2.value=sa[2];
			}
			for (var i = 0; i < selRoom.options.length; i++)
			{
				if (parseInt(selRoom.options[i].value) == parseInt(sa[3]))
				{
					selRoom.options[i].selected = true;
					return;
				}
			}
		}
		
	}
}

function hideElement(id) {
	if(document.getElementById) {
		var objDiv = document.getElementById(id);
		if(objDiv)objDiv.style.display="none";
	}
}

var temp;
	
function redirect(x)
{
	if (temp == null || x < 0)
		return;
	
	for (m=temp.options.length-1;m>0;m--)
	{
		temp.options[m]=null;
	}
	
	for (i=0;i<group[x].length;i++)
	{
		temp.options[i]=new Option(group[x][i].text,group[x][i].value)
	}
	
	temp.options[0].selected=true;
}


/* Show / Hide Object */
function showObject(target) {
	var object = document.getElementById(target);
	object.style.display="block";
}
function hideObject(target) {
	var object = document.getElementById(target);
	object.style.display="none";
}

var btEl = null;
var bcEl = null;
var srEl = null;
var ofEl = null;
	
function setSearchValues(btId, bcId, srId, ofId, btV, bcV, srV, ofV)
{
	btEl = document.getElementById(btId);
	bcEl = document.getElementById(bcId);
	srEl = document.getElementById(srId);
	ofEl = document.getElementById(ofId);
	
	for (i = 0; i < btEl.options.length - 1; i++)
	{
		if (btEl.options[i].value == btV)
		{
			btEl.options[i].selected = true;
		}
		else
		{
			btEl.options[i].selected = false;
		}
	}

	setBlindColours(btId, bcId, srId, ofId, bcV, srV, ofV);
}

function setBlindColours(btId, bcId, srId, ofId, bcV, srV, ofV)
{
	if (btEl == null)
	{
		btEl = document.getElementById(btId);
	}
	if (bcEl == null)
	{
		bcEl = document.getElementById(bcId);
	}
	
	var key = btEl.options[btEl.selectedIndex].value;

	if (crg.containsKey(key))
	{
		// showObject(bcId);
		
		var arr = crg.get(key);

		for (i = bcEl.options.length - 1; i > 2; i--)
		{
			bcEl.options[i] = null;
		}
	
		for (i=0; i < arr.length;i++)
		{
			bcEl.options[i + 2] = new Option(arr[i].text,arr[i].value);
			if ((bcV != null) && (arr[i].value == bcV))
			{
				bcEl.options[i + 2].selected = true;
			}
		}
	}
	else
	{
		// hideObject(bcId);
	}
	
	setStarRating(btId, bcId, srId, ofId, srV, ofV);
}

function setStarRating(btId, bcId, srId, ofId, srV, ofV)
{
	if (btEl == null)
	{
		btEl = document.getElementById(btId);
	}
	if (bcEl == null)
	{
		bcEl = document.getElementById(bcId);
	}
	if (srEl == null)
	{
		srEl = document.getElementById(srId);
	}
	
	var key = btEl.options[btEl.selectedIndex].value + "-" + bcEl.options[bcEl.selectedIndex].value;
	
	if (rg.containsKey(key))
	{
		showObject(srId);
		
		var arr = rg.get(key);

		for (i = srEl.options.length - 1; i > 2; i--)
		{
			srEl.options[i] = null;
		}
	
		for (i=0; i < arr.length;i++)
		{
			srEl.options[i + 2] = new Option(arr[i].text,arr[i].value);
			if ((srV != null) && (arr[i].value == srV))
			{
				srEl.options[i + 2].selected = true;
			}
		}
	}
	else
	{
		hideObject(srId);
	}
	
	setOffers(btId, bcId, srId, ofId, ofV);
}

function setOffers(btId, bcId, srId, ofId, ofV)
{
	if (btEl == null)
	{
		btEl = document.getElementById(btId);
	}
	if (bcEl == null)
	{
		bcEl = document.getElementById(bcId);
	}
	if (srEl == null)
	{
		srEl = document.getElementById(srId);
	}
	if (ofEl == null)
	{
		ofEl = document.getElementById(ofId);
	}
	
	var key = btEl.options[btEl.selectedIndex].value + "-" + bcEl.options[bcEl.selectedIndex].value + "-" + srEl.options[srEl.selectedIndex].value;

	for (i = ofEl.options.length - 1; i > 2; i--)
	{
		ofEl.options[i] = null;
	}
			
	if (og.containsKey(key))
	{
		showObject(ofId);
		
		var arr = og.get(key);

		for (i=0; i < arr.length;i++)
		{
			ofEl.options[i + 2] = new Option(arr[i].text,arr[i].value);
			if ((ofV != null) && (arr[i].value == ofV))
			{
				ofEl.options[i + 2].selected = true;
			}
		}
	}
	else
	{
		hideObject(ofId);
	}
}
