/*  
 *             CREDITS
 *
 * 		AJAX Obiect Oriented
 *
 *				Part of
 *		IceCode - The Next Web Expression
 *
 *		Author  : Daniele Contarino
 *		Version : 1.02
 *		Contact : http://www.danielecontarino.it
 *
 */
 
function Ajax (file, method){
	this.file = file;
	this.method = method;
	this.conteiner = false;
	this.waiting = false;
	this.data = "";
	this.secureTicket = "";
	this.waitingMessagge;
	this.delay = -1;
	this.responseText;
	this.request=getXHR();
	this.onCompleteRequestFunction = null;
	
	this.setConteiner = function (conteiner){
		if(typeof conteiner == "object") this.conteiner = conteiner;
		else if(typeof conteiner != "null")this.conteiner = document.getElementById(conteiner);
	}

	this.setWaiting = function (waiting, waitingMessage){
		if(typeof waiting == "object") this.waiting = waiting;
		else if(typeof waiting != "null") this.waiting = document.getElementById(waiting);
		if(waiting) this.waitingMessage = waitingMessage;
	}

	this.setData = function (data){
		if( data != null &&  typeof data != "undefined"){
			this.data = data;
			//Se ho passato un intero form allora lo passo al parser
			if(typeof this.data == "object") this.data= form2data(this.data); 
		}	
	}

	this.setSecureTicket = function (secureTicket){
		if(typeof secureTicket != "null" &&  typeof secureTicket != "undefined") this.secureTicket = secureTicket;
	
	}

	this.setDelay = function (delay){	if(delay > 1) this.delay = delay; }
	
	this.getResponse = function (){ return this.responseText; }
	
	this.sendRequest = function (async){
		if(this.request){
			if(async) {
				var _self = this;
				this.request.onreadystatechange = function(){ loadAsyncData(this, _self); };

			}else if(this.waiting) this.waiting.innerHTML=this.waitingMessage;
			
			var params = "";
			
			if(this.data != "" && this.secureTicket != "") params = this.data+"&secureTicket="+this.secureTicket ;  
			else if(this.data != "" ) params = this.data ;  
			else if(this.secureTicket != "") params = "secureTicket="+this.secureTicket ;  
						
			try{
				if(this.method.toUpperCase() == "GET" ) {
					this.request.open(this.method, this.file+"?"+params, async);
					this.request.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
					this.request.setRequestHeader("connection", "close");
					this.request.send(null);
					
				}else {
					this.request.open(this.method, this.file, async);
					this.request.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
					this.request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
					this.request.send(params);
				}
				
			}catch (e) { return false;}
			
			if(!async) loadSyncData(this);

			return true;
		}
		return false;		
	}
	
	//Metodi Privati
	
	function loadAsyncData(ajaxRequest, callbackFunction) {
		if(ajaxRequest.readyState == 1 && callbackFunction.waiting) callbackFunction.waiting.innerHTML = callbackFunction.waitingMessage; 
		else if(ajaxRequest.readyState == 4)
			if(ajaxRequest.status == 200){
				callbackFunction.responseText = ajaxRequest.responseText.replace(/^\s+|\s+$/g,"");
				if( callbackFunction.onCompleteRequestFunction != null) 
					setTimeout(callbackFunction.onCompleteRequestFunction, 0);
				else{
					if(callbackFunction.conteiner){
						if(callbackFunction.delay == -1)	callbackFunction.conteiner.innerHTML = callbackFunction.responseText;
						else setTimeout( function() { callbackFunction.conteiner.innerHTML = callbackFunction.responseText;}, callbackFunction.delay);

					}
				}
				if(callbackFunction.conteiner != callbackFunction.waiting) callbackFunction.waiting.innerHTML = '';
				return true;
				
			}else return false;	
	}

	function loadSyncData(ajaxRequest) {
		ajaxRequest.responseText = ajaxRequest.request.responseText.replace(/^\s+|\s+$/g,"");
		if(ajaxRequest.conteiner){
			if(ajaxRequest.delay == -1) ajaxRequest.conteiner.innerHTML = ajaxRequest.responseText;
			else setTimeout( function() { ajaxRequest.conteiner.innerHTML = ajaxRequest.responseText;},this.delay);
		}		
	}

	function form2data(form) {
		var data="";
		for (var i=0;i<form.length;i++)
			if(form.elements[i].name!="")			
				if(form.elements[i].nodeName.toLowerCase() =="input" && form.elements[i].type == "checkbox" && !form.elements[i].checked  ) ;
				else if(form.elements[i].nodeName.toLowerCase() =="input" && form.elements[i].type == "radio" && !form.elements[i].checked  ) ;
				else data+=form.elements[i].name+"="+form.elements[i].value+"&";

		return data.substring(0,data.length-1);
	} 

	function getXHR () {
		var XHR = null, browserUtente = navigator.userAgent.toUpperCase();

		if (window.XMLHttpRequest) 	XHR = new XMLHttpRequest();
		else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0){
			if(browserUtente.indexOf("MSIE 5") < 0) XHR = new ActiveXObject("Msxml2.XMLHTTP");
			else XHR = new ActiveXObject("Microsoft.XMLHTTP");				
		}
		return XHR;
	}
}


var AjaxUtils = new function AjaxUtils(){
	this.checkBrowser = function(applicationName){
							var browserUtente = navigator.userAgent.toUpperCase();
							return (browserUtente.indexOf(applicationName) < 0) ;
						};

	this.useAsync = function(){
		var useAsync = true;
		if(this.checkBrowser("MSIE 4") || this.checkBrowser("MSIE 5")|| this.checkBrowser("MSIE 6")) useAsync = false;
		return useAsync;
	}
}

/*  
 *             CREDITS
 *
 * 		IceCodeCrossBrowser - Cross Browser layer
 *
 *				Part of
 *		IceCode - The Next Web Expression
 *
 *		Author  : Daniele Contarino
 *		Version : 1.00
 *		Contact : http://www.danielecontarino.it
 *
 */
 
//Singleton Instance
var crossBrowser = new function IceCodeCrossBrowserLayer(){

	this.innerHTML = function (object, content) { 
			if(object.innerHTML)	 object.innerHTML = content; 	
			else if(object.textContent) object.textContent = content;
	}

}/*  
 *             CREDITS
 *
 * 		IceCodeEventHandler - a cross browser event management
 *
 *				Part of
 *		IceCode - The Next Web Expression
 *
 *		Author  : Daniele Contarino
 *		Version : 1.00
 *		Contact : http://www.danielecontarino.it
 *
 */
 
//Singleton Instance
var eventHandler = new function IceCodeEventHandler(){
	var grabber_diffX;
	var grabber_diffY;
	var grabber_element;

	//Chiamate di gestione del event handler tipico di Internet Explorer
	this.attachEvent = function (event, eventName, functionName, object) { addEventListener(event, eventName, functionName, true, object);}
	this.detachEvent = function (event, eventName, functionName, object) { removeEventListener(event, eventName, functionName, true, object);}
	this.cancelBubble = function (event) {stopPropagation(event);}
	this.returnValue = function (event) { preventDefault(event); }

	//Chiamate di gestione del event handler tipico di NetScape / Mozilla Firefox
	this.addEventListener = function (event,eventName,functionName,aUseCapture,object) {
		if(object.attachEvent) object.attachEvent("on"+eventName, functionName);		
		else if(object.addEventListener) object.addEventListener(eventName, functionName, aUseCapture);
	}

	this.removeEventListener = 	function (event,eventName,functionName,aUseCapture,object) {
		if(object.detachEvent) object.detachEvent("on"+eventName, functionName);		
		else if(object.removeEventListener) object.removeEventListener(eventName, functionName, aUseCapture);
	}										
												
	this.stopPropagation = 	function (event) {
		if(event.cancelBubble) event.cancelBubble= true;
		else if(event.stopPropagation) event.stopPropagation();
	}
												
	this.preventDefault = function (event) {
		if(event.returnValue) event.returnValue=true;
		else if(event.preventDefault) event.preventDefault();	
	}

	this.grabber = function (event,object) {
		this.grabber_element = object;

		if(this.grabber_element.style.left){
			this.grabber_diffX = event.clientX - parseInt(this.grabber_element.style.left);
			this.grabber_diffY = event.clientY - parseInt(this.grabber_element.style.top);
			
		}else{
			this.grabber_diffX = parseInt(event.clientX) - parseInt(this.grabber_element.offsetLeft);
			this.grabber_diffY = parseInt(event.clientY) - parseInt(this.grabber_element.offsetTop);
		}
		this.addEventListener(event,"mousemove", this.mover, true,document);
		this.addEventListener(event,"mouseup", this.dropper, true,document);
		this.stopPropagation(event);
		this.preventDefault(event);
	}

	this.mover = function (event) {
		eventHandler.grabber_element.style.left = (event.clientX - eventHandler.grabber_diffX) + "px";
		eventHandler.grabber_element.style.top = (event.clientY - eventHandler.grabber_diffY) + "px";
		eventHandler.preventDefault(event);
	}

	this.dropper = function (event) {
		eventHandler.removeEventListener(event,"mousemove", eventHandler.mover, true, document);
		eventHandler.removeEventListener(event,"mouseup", eventHandler.dropper, true, document);
		eventHandler.stopPropagation(event);
	}
}




function getGoogleAnalyticsMonitor(account, isLite){
	if(!isLite){
		var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
		document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	}
	try{
		var pageTracker = _gat._getTracker("UA-"+account);
		pageTracker._trackPageview();
	} catch(err) {}
}
var typeBrowser=(navigator.appName.indexOf("Netscape")!=-1  && parseInt(navigator.appVersion)>=5)?'M':'I';

function moveOption(sourceId, destinationId) {
	var source=document.getElementById(sourceId);
	var destination=document.getElementById(destinationId);

	var movedIndex=new Array();
	if(source.selectedIndex!=-1){
		for (i=0;i<source.length;i++)
			if(source.options[i].selected)
				movedIndex.push(source.options[i]);
			
		for (i=0;i<movedIndex.length;i++){
				destination.add(movedIndex[i],null);
				movedIndex[i].selected=false;
		}
	}
}

function openWindow(file){
	window.open(file,'_blank','toolbar=no, width=600, height=500');
	return('#');
}

function checkAll(fields,name){
	if(typeof name == "object"){
		for (i=0; i<fields.length; i++)
			for (j=0; j<name.length; j++)
				if(fields[i].name==name[j]) fields[i].checked = true ;
	}else		
		for (i=0; i<fields.length; i++)
			if(fields[i].name==name) fields[i].checked = true ;
}

function uncheckAll(fields,name){
	if(typeof name == "object"){
		for (i=0; i<fields.length; i++)
			for (j=0; j<name.length; j++)
				if(fields[i].name==name[j]) fields[i].checked = false ;
				
	}else
		for (i = 0; i < fields.length; i++)
			if(fields[i].name==name) fields[i].checked = false ;
}

function trim (str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}
/*  
 *             CREDITS
 *
 * 		Slider - Javascript rollon div and menu
 *
 *				Part of
 *		IceCode - The Next Web Expression
 *
 *		Author  : Daniele Contarino
 *		Version : 1.00
 *		Contact : http://www.danielecontarino.it
 *
 */
 
function Slider (time, delay){
	this.time = time
	this.delay = delay;
	this.currentItem = null;
	this.previousItem = null;
	this.sliderOpen = Array();
	
	this.appendChildSlider = function (slider){
		this.childSlider.append(slider);
	}

	this.open = function (element, parent) {
		if(this.currentItem != null) this.previousItem = this.currentItem;

		//se ho cliccato per la seconda volta sul menu
		if(this.currentItem == this.getObject(element)){
			//Chiudo tutto
			while(this.sliderOpen.length > 0) 
				this.rapidClose(this.sliderOpen.pop());

			this.currentItem = null;
			return;
		}

		this.currentItem = this.getObject(element);
		parent = this.getObject(parent);

		//Se non sono in un sottomenu
		if(parent== null){
			//Chiudo tutti
			while(this.sliderOpen.length > 0) 
				this.rapidClose(this.sliderOpen.pop());
			
		}else if(parent != this.previousItem){
			//nascondo il vecchio menu
			do{
				//Ottengo l'ultimo menu aperto
				aux = this.sliderOpen.pop();
				
				//Se l'ultimo menu aperto non è quello padre ( quindi è un figlio ) lo chiudo, altrimenti lo rimetto al suo posto
				if( parent != aux) this.slideUp(aux);
				else this.sliderOpen.push(aux);
			}while(parent != aux);
				
		}
		
		//Aggiungo lo slider alla lista dei slider aperti
		this.sliderOpen.push(this.currentItem);
		
		//mostro il nuovo Menu
		this.slideDown();
	}
		
	this.slideDown = function (element){
		if(typeof( element ) == "undefined" ) element = this.currentItem;

		this.currentItem.style.display = "inline-table";
		maxHeight = (parseInt(this.currentItem.style.height).toString()  != "NaN")? parseInt(this.currentItem.style.height) : this.currentItem.clientHeight;
		this.currentItem.style.height= "0px";
		step = (maxHeight / time) * delay;
		this.moveDown(this.currentItem, step, maxHeight, step);
	}

	this.slideUp = function (element){
		if(typeof( element ) == "undefined" ) element = this.previousItem;

		element.style.overflow = "hidden";
		maxHeight = (parseInt(element.style.height).toString()  != "NaN")? parseInt(element.style.height) : element.clientHeight;
		step =  maxHeight/time  * delay;
		
		this.moveUp(this.previousItem, maxHeight, maxHeight, step );
	}

	this.moveDown = function (element, currentHeight, maxHeight, step){
		var _self = this;		
		if(currentHeight < maxHeight){
			currentHeight += step;
			element.style.height= currentHeight +"px";
			setTimeout(function(){_self.moveDown(element, currentHeight, maxHeight, step); }, _self.delay);

		}else{
			element.style.height= "auto";
			element.style.overflow = "visible";

		}
	}

	this.moveUp = function (element, currentHeight, maxHeight, step){
		var _self = this;		

		if(currentHeight > 0){
			currentHeight -= step;
			element.style.height= currentHeight +"px";
			setTimeout(function(){_self.moveUp(element, currentHeight, maxHeight, step); }, _self.delay);

		}else if(element != null){
			element.style.display="none";
			element.style.overflow = "hidden";
			element.style.height= maxHeight +"px";			
		}
	}
	
	this.rapidClose = function (element){
		maxHeight = (parseInt(element.style.height).toString()  != "NaN")? parseInt(element.style.height) : element.clientHeight;

		element.style.display="none";
		element.style.overflow = "hidden";
		element.style.height= maxHeight +"px";
	}
	
	this.getObject = function (element){
		if(typeof element == "object") return element;
		else if(typeof conteiner != "null")return document.getElementById(element);
		else return null;
	
	}

}
function hand(object){ object.style.cursor = 'hand';}

function arrow(object){object.style.cursor = 'pointer';}

function loadCSS(filename, media) {
	var css = document.createElement('link');
	css.href = filename;
	css.type = 'text/css';
	css.rel = 'stylesheet';
	if(typeof media == "undefined") css.media="screen";
	else css.media = media;
	document.getElementsByTagName('head')[0].appendChild(css);
}

function getWindowSize(){
	var width = document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
	var height = document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight; 
	return { 'width' : width, 'height' : height };
}

function getPageSize(){	        
	 var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return { 'width' : pageWidth, 'height' : pageHeight };
}/*  
 *             CREDITS
 *
 * 		XMLParser crossbrowser layer
 *
 *				Part of
 *		IceCode - The Next Web Expression
 *
 *		Author  : Daniele Contarino
 *		Version : 1.00
 *		Contact : http://www.danielecontarino.it
 *
 */

function getXMLDOM (stringDocument) {
	try {
		//Internet Explorer
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(stringDocument);
		return xmlDoc;
	}catch(e){
		parser=new DOMParser();
		xmlDoc=parser.parseFromString(stringDocument,"text/xml");
		return xmlDoc;
	}
}


function setTextContent(element, value){
	if( typeof element != "object" && typeof element != "null") element = document.getElementById(element);
	if(typeof element.innerText == "undefined") element.textContent = value;
	else element.innerText = value;
}

function getTextContent(element){
	
	if( typeof element != "object" && typeof element != "null") element = document.getElementById(element);
	if(typeof element.textContent != "undefined") return element.textContent;
	else if(typeof element.text != "undefined") return element.text;
	else return element.innerText;
}
var ABSOLUTE_PATH = 'http://www.trovafarmacie.it/';
