// Cookie Class

//Crea un oggetto cookie per il documento specificato
function Cookie(document, name, hours, path, domain, secure){
	this.$document = document;
	this.$name = name;
	if(hours){
		this.$expiration = new Date((new Date()).getTime()+hours*3600000);
	} 
	else {
		this.$expiration = null;
	}
	if(path) this.$path = path;	 		else this.$path = null;
	if(domain) this.$domain = domain;   else this.$domain = null;
	if(secure) this.$secure = false;		else this.$secure =false;
}
	
function storeCookie(){
	var cookieval = "";
	
	//Lettura dell'oggetto Cookie e estrazione dati da memorizzare
	for(var prop in this){
			
	//Ignora le proprietà i cui nomi iniziano per $ e anche i metodi
		if((prop.charAt(0)=="$") || ((typeof this[prop]) == "function"))
			continue;
		if(cookieval != "") cookieval += "|";
		cookieval += prop +"=" + escape(this[prop]);
	}
	
	//Assemblaggio stringa
	var cookie = this.$name + "=" + cookieval;
	if(this.$expiration){
		cookie += "; expires=" + this.$expiration.toGMTString();
	}
	if(this.$path) cookie += "; path=" + this.$path;
	if(this.$domain) cookie += "; domain=" + this.$domain;
	if(this.$secure) cookie += "; secure";
	
	//Memorizzazione
	this.$document.cookie = cookie;
}

function loadCookie(){
	//Lettura di tutti i cookie collegati al documento		
	var allcookies = this.$document.cookie;
	
	if(allcookies == "") return false; //Non ci sono cookies
	//Estrazione del cookie specificato

	var start = allcookies.indexOf(this.$name + "=");
	
	if(start == -1) return false; //Cookie nn definito per questa pagina
	start += this.$name.length+1
	var end = allcookies.indexOf(";",start);
	if(end == -1) end = allcookies.length;
	var cookieval = allcookies.substring(start, end);
	
	
	//Creo un array di coppie nome/valore, a sua volta ogni coppia è un array
	var a = cookieval.split("|");
	for(var i=0;i<a.length;i++){
		a[i] = a[i].split("=");
	}
	for(var i=0;i<a.length;i++){
	this[a[i][0]] = unescape(a[i][1]);
	}
	
	return true;
}


function removeCookie(){
	var cookie;
	cookie = this.$name + "=";
	if(this.$path) cookie += "; path=" + this.$path;
	if(this.$domain) cookie += "; domain=" + this.$domain;
	cookie += "; expires=Fri, 02-Jan-1970 00:00:00 GMT";
	
	this.$document.cookie = cookie;
}


Cookie.prototype.store = storeCookie;
Cookie.prototype.loadCk = loadCookie;
Cookie.prototype.remove = removeCookie;




// Utilities
function calcolaTempo(time){
   var temp = new Date().getTime();
   var difference = temp-time;
   
   // decompose difference into days, hours, minutes and seconds parts
   var ore   = parseInt((difference % 86400000) / 3600000) + '';
   var minuti = parseInt((difference % 3600000) / 60000) + '';
   var secondi = parseInt((difference % 60000) / 1000) + '';
   
   // negative values should be set to 0
   if (isNaN(ore) || ore.charAt(0) == '-') ore = 0;
   if (isNaN(minuti) || minuti.charAt(0) == '-') minuti = 0;
   if (isNaN(secondi) || secondi.charAt(0) == '-') secondi = 0;
   return padout(ore)+":"+padout(minuti)+":"+padout(secondi);
	//alert(minuti+":"+secondi)
}

// Formatta con lo 0 i numeri inferiori a 10
function padout(number){
	return (number<10)? "0"+number : number;
}
// Converte una data in stringa (per la memorizzazione nel cookie)
function dateToString(date){
	return ""+
		(date.getFullYear())+ padout(date.getMonth()+1)+ padout(date.getDate())
}

// Converte una stringa yyyymmdd in una data
function stringToDate(string){
	return new Date(string.substring(0,4),string.substring(4,6)-1,string.substring(6,8));
}

// Trova un oggetto in un array tramite una coppia proprietà/valore
Array.prototype.findByValue = function(prop, value) { 
	var i = this.length; 
	while (i--) { 
	  if(this[i]){
		if (this[i][prop] == value) return this[i]; 
	} }
} 

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
        return Math.ceil(rnd()*number);
};


// Search Engine & Uri Utilities
var  dominio = "traveleurope."

// Estrae il valore di una variabile da dentro l'url
function getUrlVar(url,param){
	var theUrl = url;
	var segno="";
	//solo per wanadoo
	if(url.indexOf("wanadoo")!=-1) segno="%3D"; 
    else segno="=";
	var lookFor = param+segno;
	if (url.indexOf(lookFor)!= -1){
		var start = url.indexOf(lookFor) + lookFor.length;
		var end = url.indexOf("&", start)
		if(end==-1) end=url.length;
		var result = url.substring(start, end);
		return result;
	}
	else {
	return 0;
	}
}
// Restituisce il referrer
function getReferrer(doc){
	var refer;
	if (doc.referrer&&document.referrer!=""){ 
		refer = doc.referrer;
		// variante discovery // da variare se cambia il processo... http>>https!
		var affil = getUrlVar(refer,"AFFIL");
		if(affil != 0){
			refer = affil+refer;
			}
		}
	else {
		refer = "bookmark"+location.href; //Bookmark o Digitato
		}
	return refer;
}
function getSearchEngine(url){
	var regexp=/(\w+):\/\/([\S.]+)\/(\S*)/;
	var result = url.match(regexp);
	if(result!=null){
	var host = result[2]
	}
	ref = 0;
	for(var i=0; i<arSE.length; i++){
		if(host.indexOf(arSE[i].dominio) != -1){
		ref = host;
		break;
		}
	}
	return ref;
}
function getKeys(refer, dominio){
	var p1, p2;
	var keys, k1, k2;
	var strDominio = dominio;
	if(!strDominio) return 0;
	var end = strDominio.lastIndexOf(".");
	strDominio = strDominio.substring(0, end);
	for(var i=0; i<arSE.length; i++){
		if(strDominio.indexOf(arSE[i].dominio) != -1){
		p1 = arSE[i].key;
	    (arSE[i].key2!=undefined) ? p2=arSE[i].key2 : p2=0;
		break;
		}
	}
	k1 = getUrlVar(refer,p1);
	k2 = getUrlVar(refer,p2);
	(k2!=0) ? keys=k2 : keys=k1;
	return keys;
}

function getPath(url){
	//senza parametri
	//var regexp=/(\w+):\/\/([\w.]+)\/([^\?]*)/;
	var regexp=/(\w+):\/\/([\w.]+)\/(\S*)/;
	var result = url.match(regexp);
	//return result = (!result[3]) ? "index.shtml" : result[3];
	result = elabora((!result[3]) ? "index.shtml" : result[3]);
	return  result;
}
function elabora(str){	
	if(str.indexOf("?sponsor")!=-1) return str.split("?sponsor",1);
	else return str;
}

var arSE = new Array();
arSE[0] = {dominio:"google", key:"q"};
arSE[1] = {dominio:"yahoo", key:"p"};
arSE[2] = {dominio:"webcrawler", key:"q"};
arSE[3] = {dominio:"metacrawler", key:"qkw"};
arSE[4] = {dominio:"about", key:"terms"};
arSE[5] = {dominio:"alltheweb", key:"q"};
arSE[6] = {dominio:"altavista", key:"q", key2:"aqa"};
arSE[7] = {dominio:"aolsearch", key:"query"};
arSE[8] = {dominio:"search.aol", key:"query"};
arSE[9] = {dominio:"askjeeves.co", key:"ask"};
arSE[10] = {dominio:"askjeeves", key:"ask"};
