


/**
* Utilities script file
*/
	function createXMLHttpRequest(){
		var xmlHttp;
		if(window.ActiveXObject){
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else if(window.XMLHttpRequest){
			xmlHttp = new XMLHttpRequest()
		}
		return xmlHttp;
	}

	/**
	* Gets the distance from the bottom of the objectId to the bottom of the visible
	* screen. If the result is negative, then the bottom of the object is off the screen,
	* and we need to scroll down.
	*/
	function getDistanceFromBottom(objectId){
		
		//the screen height (the height of what we're actually looking at)
		var windowHeight = window.getHeight();
		//the position of the top of the window in relation to the whole page
		var windowTopOffset = window.getScrollTop();
		//now we've got the y pos of the bottom of the window in relation to the whole page
		var windowBottomPos = windowHeight + windowTopOffset;
		
		//get all the coordinates of the object
		var objCoords = $(objectId).getCoordinates();
		
		return windowBottomPos - objCoords.bottom;
	}


	/**
	 * Given the distance from the bottom of the bottom of the object (distanceFromBottom),
	 * which must be a negative number, otherwise there's no point in calling this method,
	 * this creates a scroller and scrolls the window to just below the bottom of the object
	 * whose distanceFromBottom we've just passed
	 * @param distanceFromBottom - distance from the bottom of the object to the bottom of
	 * the window (must be negative)
	 */
	function scrollToBottomOfObject(distanceFromBottom){

		
		var scrollIt = new Fx.Scroll(window,{
       		duration: 1000
		});
		
		//add the distanceFromBottom to the scrollTop (remember distanceFromBottom is a negative number)
		//and add another 20 to give a bit of distance from the bottom
		scrollIt.start(0, window.getScrollTop() - distanceFromBottom + 30);
	}

/**
* takes a String value as parameter
*/
function isANumber(theValue){                                                                                                                                                                                  
                                                                                                                                                                                                      
	var isaNumber = true;                                                                                                                                                                                        
    for(var i = 0; i < theValue.length; i = i+1){                                                                                                                                                                
		var y = theValue.substring(i, i+1);                                                                                                                                                              
		if(!isFinite(y) && y != "." && y != ","){                                                                                                                                                        
			isaNumber = false;                                                                                                                                                                               
		}//end if                                                                                                                                                                                        
    }//end for                                                                                                                                                                                                   
                                                                                                                                                                                                                 
	if(!isaNumber){                                                                                                                                                                                              
		return false;                                                                                                                                                                                    
	}//end if                                                                                                                                                                                                
	return true;                                                                                                                                                                                                 
                                                                                                                                                                                                                 
}//end isANumber                                                                                                                                                                                                  
                                                                                                                                                                                                                 


/**
* gets the keycode for the key pressed, for most browsers
*/
		function getTheKey(evt){
	      	evt = (evt) ? evt : ((window.event) ? window.event : "");
			var theKey = 0;
			if (navigator.appName == 'Microsoft Internet Explorer'){
     			theKey = evt.keyCode;
     		}else{
				theKey = evt.which;
	 		}//end if
	 		return theKey;
		}//end getTheKey()

/**
* Removes a named child node from the document body
*/
function removeChildFromDocBody(childsName){
	var kids = document.body.childNodes;
	for(var i = 0; i < kids.length; i = i + 1){
		if(kids[i].id == childsName){
			document.body.removeChild(kids[i]);
			break;
		}//end if
	}//end for
}//end function

/**
* Removes a named child node from the document body
*/
function removeChildFromNamedObj(childsName, namedObjId){
	var namedObj = document.getElementById(namedObjId);
	var kids = namedObj.childNodes;
	for(var i = 0; i < kids.length; i = i + 1){
		if(kids[i].id == childsName){
			namedObj.removeChild(kids[i]);
			break;
		}//end if
	}//end for
}//end function

/**
* Calculates the offset of an object on a page
*/
function calculateOffset(field, attr){
	var offset = 0;
	while(field){
//	alert('field - ' + field + ":" + field[attr])
		offset += field[attr];
		field = field.offsetParent;
	}//end while
	return offset;
}

/**
* Removes all children from a node,
* if there aren't any children, it doesn't make a fuss
*/
function removeAllChildren(idName){
	var o = document.getElementById(idName);
	var oKids = o.childNodes;
	if(oKids){
//		for(var i = 0; i < oKids.length; i = i + 1){
		for(var i = oKids.length - 1; i >= 0; i = i - 1){
			o.removeChild(o.childNodes[i]);
		}//end for
	}//end if
}

/**
* Scrolls the window to the given anchor, using the anchorId and can offset up or down
* using the uOffset variable.
*/
function goToAnchor(anchorId, yOffset){
	var anchorObj = document.getElementById(anchorId);
	if(anchorObj == null){
		return;
	}
	var anchorOffsetTop = calculateOffset(anchorObj, "offsetTop");
	window.scroll(0, anchorOffsetTop + yOffset);
}

/**
* Scrolls the window to the given anchor, using the anchorId. Needs mootools
*/
function goToAnchorSmooth(anchorId){
		var scrollIt = new Fx.Scroll(window,{
       		duration: 500,
       		offset: {
       			'x': 0,
       			'y': -80
       		}
       	});
       scrollIt.toElement($(anchorId));
	
}

	/**
	* saves the buttonId for timeout call
	*/
	var savedButtonId;
	var savedRaisedButtonName;

	/**
	* Changes the button src file so that it looks pressed (tmy only)
	*/
	function pressButton(buttonId, pressedButtonName, raisedButtonName){
		var theButtonObj = document.getElementById(buttonId);
		theButtonObj.src = "/ts/images/inputs/" + pressedButtonName;
		savedButtonId = buttonId;
		savedRaisedButtonName = raisedButtonName;
		setTimeout(raiseButton, 200);
	}
	
	/**
	* Changes the button src file so that it looks raised - called
	* by setTimeout
	*/
	function raiseButton(){
		var theButtonObj = document.getElementById(savedButtonId);
		theButtonObj.src = "/ts/images/inputs/" + savedRaisedButtonName;
	}

	function removeString(theString, stringToRemove){
		var theIndex = theString.indexOf(stringToRemove);
		while(theIndex > -1){
		    theString = theString.substring(0, theIndex) + theString.substring(theIndex + 1);
		    theIndex = theString.indexOf(stringToRemove, theIndex - 1);
		}//end while
		return theString;
	}//end function
	
	function replaceString(theString, stringToReplace, replaceWithString){
		var theIndex = theString.indexOf(stringToReplace);
		while(theIndex > -1){
		    theString = theString.substring(0, theIndex) + replaceWithString + theString.substring(theIndex + stringToReplace.length);
		    theIndex = theString.indexOf(stringToReplace, theIndex + replaceWithString.length);
		}//end while
		return theString;
	}//end function

	/**
	 * Scrolls to the top of the window
	 */
	function scrollToTheTop(){
	   var scrollIt = new Fx.Scroll(window,{
       		duration: 500
       });
       scrollIt.toTop();
   }
   
   /**
    * If there's a plus sign in some text sent in a request it will get decoded
    * as a space during a request, so we replace it, the same for a % percent sign, but
    * the % percent sign must get replaced before the others!!
    */
   	function myEncoding(theValue){
   		//this % one must come first!! don't change the order
		theValue = replaceString(theValue, '%', '%25');
		theValue = replaceString(theValue, '+', '%2B');
		theValue = replaceString(theValue, '\"', '%22');
		theValue = replaceString(theValue, '\r\n', '%0D');
		theValue = replaceString(theValue, '\n', '%0D');
		//grave accent (type of apostrophe)
		theValue = replaceString(theValue, '\u2019', '%27');
		return theValue;
	}
   
   
   
   var CurrentHouseShower = new Class({
   
   		initialize: function(borderCol, emptyCol, scrollNicely){
   			this.borderCol = borderCol;
   			//This is a replacement colour for transparent (the normal background color)
   			//for IE6
   			this.emptyCol = emptyCol;
   			this.scrollNicely = scrollNicely;
   		},

   		/**
		 * Finds the photo of the current house from the hidden field, and scrolls the window there
		 */
		goToCurrentHouse: function(){
			var currentHouseFotoIndicator = $('currentHouseIndicator');
			
			if(!$defined(currentHouseFotoIndicator)){
				return;
			}
			
/* fx.scroll CAN'T BE USED UNTIL WE HAVE STRICT HTML
	
			if($defined(currentHouseFotoIndicator)){
				var scrollIt = new Fx.Scroll(window,{
				    duration: 500,
		       	});
		       	var toScrollTo = $($(currentHouseFotoIndicator.value));
		       	alert(toScrollTo.id);
		       	scrollIt.toElement($($(currentHouseFotoIndicator.value)));
			}
*/
			//these three lines until we have strict html
			var fotoObj = $(currentHouseFotoIndicator.value);
			var anchorOffsetTop = calculateOffset(fotoObj, "offsetTop");
			window.scroll(0, anchorOffsetTop - 200);

			if(currentHouseFotoIndicator.value != "smallFoto0"){
				this.makeBorderColoured.delay(500, this);
				this.makeBorderTransparent.delay(1000, this);
				this.makeBorderColoured.delay(1500, this);
				this.makeBorderTransparent.delay(2000, this);
				this.makeBorderColoured.delay(2500, this);
			}
		},

		makeBorderColoured: function(){
			var currentHouseFotoIndicator = document.getElementById("currentHouseIndicator");
			var fotoObj = document.getElementById(currentHouseFotoIndicator.value);
			fotoObj.style.borderStyle = "solid";
			fotoObj.style.borderWidth = "3px";
			fotoObj.style.borderColor = this.borderCol;
		},
		
		makeBorderTransparent: function(fotoObj){
			var currentHouseFotoIndicator = document.getElementById("currentHouseIndicator");
			var fotoObj = document.getElementById(currentHouseFotoIndicator.value);
			fotoObj.style.borderStyle = "solid";
			fotoObj.style.borderWidth = "3px";
			fotoObj.style.borderColor = "transparent";
			//for MSIE, otherwise it seems to show a bit of the bottom of the photo
			if(navigator.appName.indexOf("Microsoft") > -1){
				fotoObj.style.borderColor = "#efe9de";
			}//end if
		},


  		/**
		 * Finds the photo of the current house from the hidden field, and scrolls the window there
		 */
		goToCurrentHouse2: function(){
			//finds the codice we're going to look for, which was placed there by java in the .jsp
			var codiceIndicator = $('codiceIndicator');
			
			if(!$defined(codiceIndicator)){
				return;
			}
			
	
			if(this.scrollNicely){
				if($defined($(codiceIndicator.value + '.foto'))){
					var scrollIt = new Fx.Scroll(window,{
					    duration: 800,
					    offset: {
       						'x': 0,
       						'y': -80
       					}
			       	});
			       	//var toScrollTo = $($(codiceIndicator.value + '.foto')));
			       	//alert(toScrollTo.id);
			       	scrollIt.toElement($($(codiceIndicator.value + '.foto')));
				}
			}else{
				//these four lines until we have strict html
				var fotoObj = $(codiceIndicator.value + '.foto');
				if(!$defined(fotoObj)){
					return;
				}
				var anchorOffsetTop = calculateOffset(fotoObj, "offsetTop");
				window.scroll(0, anchorOffsetTop - 200);
			}//end if

			this.makeBorderColoured2.delay(500, this);
			this.makeBorderTransparent2.delay(1000, this);
			this.makeBorderColoured2.delay(1500, this);
			this.makeBorderTransparent2.delay(2000, this);
			this.makeBorderColoured2.delay(2500, this);

		},

   
   		makeBorderColoured2: function(){
			var codiceIndicator = $('codiceIndicator');
			var fotoObj = $(codiceIndicator.value + '.foto');
			if($defined(fotoObj)){
				fotoObj.style.borderStyle = "solid";
				fotoObj.style.borderWidth = "3px";
				fotoObj.style.borderColor = this.borderCol;
			}
		},
		
		makeBorderTransparent2: function(fotoObj){
			var codiceIndicator = $('codiceIndicator');
			var fotoObj = $(codiceIndicator.value + '.foto');
			if($defined(fotoObj)){
				fotoObj.style.borderStyle = "solid";
				fotoObj.style.borderWidth = "3px";
				fotoObj.style.borderColor = "transparent";
				//for MSIE, otherwise it seems to show a bit of the bottom of the photo
				if(navigator.appName.indexOf("Microsoft") > -1){
					//fotoObj.style.borderColor = "#efe9de";//timeaway
					fotoObj.style.borderColor = this.emptyCol;
				}//end if
			}//end if
		},
   		
   		 /**
		 * Finds the photo of the current house from the hidden field, and scrolls the window there
		 */
		goToCurrentHouseAg: function(){
			//finds the codice we're going to look for, which was placed there by java in the .jsp
			var codiceIndicator = $('codiceIndicator');
			
			if(!$defined(codiceIndicator)){
				return;
			}
			
			if($defined($(codiceIndicator.value + '.foto'))){
				var scrollIt = new Fx.Scroll(window,{
				    duration: 1000,
				    offset: {
      						'x': 0,
      						'y': -200
      					}
		       	});
		       	//var toScrollTo = $($(codiceIndicator.value + '.foto')));
		       	//alert(toScrollTo.id);
		       	scrollIt.toElement($($(codiceIndicator.value + '.foto')));
			}

			this.makeBorderColouredAg.delay(500, this);
			this.makeBorderTransparentAg.delay(1000, this);
			this.makeBorderColouredAg.delay(1500, this);
			this.makeBorderTransparentAg.delay(2000, this);
			this.makeBorderColouredAg.delay(2500, this);

		},
		
		  
   		makeBorderColouredAg: function(){
			var codiceIndicator = $('codiceIndicator');
			var fotoObj = $(codiceIndicator.value + '.foto');
			if($defined(fotoObj)){
				fotoObj.style.borderColor = this.borderCol;
			}
		},
		
		makeBorderTransparentAg: function(fotoObj){
			var codiceIndicator = $('codiceIndicator');
			var fotoObj = $(codiceIndicator.value + '.foto');
			if($defined(fotoObj)){
				fotoObj.style.borderLeftColor = "#ffffff";
			}//end if
		}
		
		
   		
   		
   		
   });//end currentHouseShower class
   
 
