// Funzione di apertura popup BlueZebra

function webVoicePop(AuthorizeURL, templateID)
{
	//var URL = 'http://chiamagratis.abbeyphone.com/services/bin/seatconvoi_third_call.php?accountid=pgol&var1=SEAT&template=' + templateID + '&AuthorizeURL=' + AuthorizeURL;
	//window.open(URL, 'chiamagratis', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=1, width=550, height=350, left=0, top=0');
	//var URL = 'http://as00.estara.com/UI/gui.php?accountid=200106296229&template=' + templateID + '&var1=383513700001&authorizeurl=' + AuthorizeURL + '&calltype=webvoicepop&linkfile=%2FOneCC%2F200106296229%2F440919.js&referrer=Email&donotcache=622718764&emaillink=1&guiid=43834a54eac25&timestamp=1236618776';
	var URL = 'http://www.sitofono.com/cc/seat/seatconvoi.php';
	var w=440;
	var h=400
	var l=Math.floor((screen.width-w)/2);
	var t=Math.floor((screen.height-h)/2);
	window.open(URL, 'chiamagratis', 'toolbar=no, scrollbars=yes, location=no, menubar=no, resizable=yes, width='+w+' ,height='+h+', top='+t+', left='+l);
	//window.open(URL, 'chiamagratis', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=1, width=430, height=378, left=0, top=0');
	//window.open(AuthorizeURL, 'chiamagratis', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=1, width=550, height=350, left=0, top=0');
}

// Trim whitespace from left and right sides of s.

function trim(s) {
    return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}

function IsNumeric(stringa) {
	var numeri = "0123456789";
	var carattere;
	var numerico=true;
	for (i = 0; i < stringa.length && numerico == true; i++) { 
		carattere = stringa.charAt(i); 
		if (numeri.indexOf(carattere) == -1) {
			numerico = false;
		}
	}
	return numerico; 
}

function equalStrings( s1, s2 )
{
	try{
		var str1 = new String( s1 );
		var str2 = new String( s2 );
		if( str1.length != str2.length )
		{
			return false;
		}
		else
		{
			var charIndex = 0;
			for( charIndex = 0; charIndex < str1.length; charIndex++ ){
				if ( str1.charAt( charIndex ) != str2.charAt( charIndex ) ){
					return false;
				}
			}
			return true;
		}
	}
	catch(E)
	{
		return false;
	}
}

// Normalizes a string by removing any superfluous whitespace

var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
var whtSpMult = new RegExp("\\s\\s+", "g");

function normalizeString(s) {
   s = s.replace(whtSpMult, " ");
   // Collapse any multiple whitespace.
   s = s.replace(whtSpEnds, "");
   // Remove leading or trailing whitespace
   return s;
}

// Function that hide <DIV>'s referenced by Id

var ns4=document.layers
var ie4=document.all
var ns6=document.getElementById&&!document.all

function hidebox(divId)
{
	var st = "document.all."+divId
	var st2 = "document."+divId+".visibility='hide'"
	crossobj=ns6? document.getElementById(divId) : eval(st)
	if (ie4||ns6)
		crossobj.style.visibility="hidden"
	else if (ns4)
		eval(st2)
}

function showbox(divId)
{
	var st = "document.all."+divId
	var st2 = "document."+divId+".visibility='show'"

	crossobj=ns6? document.getElementById(divId) : eval(st)
	if (ie4||ns6)
		crossobj.style.visibility="visible"
	else if (ns4)
		eval(st2)
}

// Funzioni per la gestione della tendina provincie sulla pagina di distribuzione elenchi

function changeProvincia_(id, tipo, nocode)
{
	var f_auth = document.URL.search(/auth/);  
	var f_all = document.URL.search(/all/); 
	var locUrl = "";
	if(f_auth != -1 || f_all != -1)
	{
		locUrl = "../localita?prov="+id.options[id.selectedIndex].value;
	}
	else
	{
		locUrl = "localita?prov="+id.options[id.selectedIndex].value;
	}
	
	if(nocode == 'Y')
		locUrl +='&nocode=Y';
		
	var xmlHttp = XmlHttp.create();
	xmlHttp.open("GET", locUrl, false);
	xmlHttp.send(null);	
	createTree(xmlHttp.responseXML,tipo); 
}

function changeProvincia(id,tipo)
{
	changeProvincia_(id, tipo, null);	
}

function changeProvinciaNoCode(id,tipo)
{
	changeProvincia_(id, tipo, 'Y');	
}

function initLocalita_(idprov, locSel, tipo, nocode)
{
	var f_auth = document.URL.search(/auth/);  
	var f_all = document.URL.search(/all/); 
	var locUrl = "";
	if(f_auth != -1 || f_all != -1)
	{
		locUrl = "../localita?prov="+idprov.options[idprov.selectedIndex].value;
	}
	else
	{
		locUrl = "localita?prov="+idprov.options[idprov.selectedIndex].value;	
	}
	
	if(nocode == 'Y')
		locUrl += '&nocode=Y';
		
	var xmlHttp = XmlHttp.create();
	xmlHttp.open("GET", locUrl, false);
	xmlHttp.send(null);	
	createTree(xmlHttp.responseXML,tipo);
	
	if(locSel != null && locSel != "")
	{
		var cit=document.getElementById("citta");	
		cit.options[locSel].selected=true;
	}
}

function initLocalita(idprov, locSel, tipo)
{
	initLocalita_(idprov, locSel, tipo);
}

function initLocalitaNoCode(idprov, locSel, tipo)
{
	initLocalita_(idprov, locSel, tipo, 'Y');
}

function initLocalita(idprov,locSel,tipo)
{
	var f_auth = document.URL.search(/auth/);  
	var f_all = document.URL.search(/all/); 
	var locUrl = "";
	if(f_auth != -1 || f_all != -1)
	{
		locUrl = "../localita?prov="+idprov.options[idprov.selectedIndex].value;
	}
	else
	{
		locUrl = "localita?prov="+idprov.options[idprov.selectedIndex].value;	
	}
	var xmlHttp = XmlHttp.create();
	xmlHttp.open("GET", locUrl, false);
	xmlHttp.send(null);	
	createTree(xmlHttp.responseXML,tipo);
	var cit=document.getElementById("citta");
	
	if(locSel != null && locSel != "")
	{
		cit.options[locSel].selected=true;
	}
}

function createTree(xmlDoc,tip)
{
	var items = xmlDoc.getElementsByTagName("item");
	var cittaSelect=document.getElementById("citta");	
	removeChildren(cittaSelect);
	for(var i=0; i<items.length; i++)
	{
		var option=document.createElement("option");
		if(tip == 0)
		{
			option.value=items[i].getAttribute("value");
		}
		else
		{
			option.value=items[i].getAttribute("name");
		}
		option.appendChild (document.createTextNode(normalizeString(items[i].getAttribute("name"))));		
		cittaSelect.appendChild(option);
	}	
}

function removeChildren(t)
{
	var child = t.firstChild;
	while(child)
	{
		var nextChild = child.nextSibling;
		t.removeChild(child);
		child = nextChild;
	}
}
