// vim: set encoding=utf-8:
function NormalizeText(sText) {

	/*sText = sText.replace(/[áàãâ]/g,"a");
	sText = sText.replace(/[ÁÀÃÂ]/g,"A");
	sText = sText.replace(/[éè]/g,"e");
	sText = sText.replace(/[ÉÈÊ]/g,"E");
	sText = sText.replace(/[íì]/g,"i");
	sText = sText.replace(/[ÍÌ]/g,"I");
	sText = sText.replace(/[óòõô]/g,"o");
	sText = sText.replace(/[ÓÒÕÔ]/g,"O");
	sText = sText.replace(/[úù]/g,"u");
	sText = sText.replace(/[ÚÙ]/g,"U");
	sText = sText.replace(/[ý]/g,"y");
	sText = sText.replace(/[Ý]/g,"Y");
	sText = sText.replace(/[đ]/g,"d");
	sText = sText.replace(/[ç]/g,"c");
	sText = sText.replace(/[Ç]/g,"C");
	sText = sText.replace(/[ñ]/g,"n");
	sText = sText.replace(/[Ñ]/g,"N");
	sText = sText.replace(/[·]/," ");
*/
	sText = escape(sText);
	return sText;
}


function OpenVISSIR(sToponim) {

  var wFinestra;
  var wScreen = screen.width;
  var hScreen = screen.height;
  
  if (self.innerHeight) // all except Explorer
  {
    wFinestra = self.innerWidth;
	hFinestra = self.innerHeight;
  }
  else if (document.documentElement)
  // Explorer 6 Strict Mode
  {
	wFinestra = document.documentElement.offsetWidth;
	hFinestra = document.documentElement.offsetHeight;
  }
  else if (document.body) // other Explorers
  {
	wFinestra = document.body.clientWidth;
	hFinestra = document.body.clientHeight;
  }

if(wFinestra > wScreen - 25){
// Finestra maximitzada i s'obre vissir a tota la pantalla
	var w = wScreen;
	var h = hScreen;
}else{
// Finestra NO maximitzada i s'obre vissir amb finestra similar a la de l'usuari
	var w = wFinestra;
	var h = hFinestra;
}

  var windowURL = "http://www.icc.cat/vissir2/";
	
  //idioma
  var loc = new String(document.location.href);
  
  if (-1 != loc.indexOf("/ca/")) {
    windowURL += "?lang=ca_ES";
  } else if (-1 != loc.indexOf("/es/")) {
    windowURL += "?lang=es_ES";
  } else {
    windowURL += "?lang=en_UK";
  }  
	
  if(sToponim) {
		sToponim = NormalizeText(sToponim);
		windowURL += '&toponim='+sToponim;
	}
  
	myWindow = window.open(windowURL,'vissir','width='+ w +',height='+ h +',resizable=yes,top=0,left=0');
	myWindow.focus();
}

function openmapserver(url){
		window.open(url,"","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=0,width=800,height=550");
}

function openwin(catalog,img){
		//Obre mapes antics
       var win = window.open('http://shagrat.icc.es/lizardtech/iserv/calcrgn?cat=' + catalog +'&item=' + img +'&wid=420&hei=400&style=default/view.xsl','','scrollbars=no,location=no,width=500,height=520');
		win.focus();
}

function openValidationForm(img,full,lang) {

	alert("img:"+img+" full:"+full+" lang:"+lang);
	var username = getCookie('icc_user_name');
	if (username!=null && username!="")
	{
		// Usuari amb cookie, passa a downloadzip.php directament sense validar-se
		var sUrl = "http://auriga.icc.cat/linklokurl/downloadzip.php?img="+ img +"&full="+ full +"&user="+ username ;
		var sWindowZipDialogProps = "menubar=no,scrollbars=no,titlebar=no,width=1, height=1";  
		var sWindowZipDialog = window.open(sUrl,"windowZipDialog", sWindowZipDialogProps); 
		sWindowZipDialog.focus(); 
	}else{
		// Usuari sense cookie, ha de validar-se
		// Finestra javascript per usuaris Internet Explorer
		var sWindowValFormSettings = "menubar=no,toolbar=no,scrollbars=no,location=no,width=390, height=230";  
		var sWindowValForm = window.open("http://auriga.icc.cat/linklokurl/downloadnou.php"+"?img="+img+"&full="+full+"&lang="+lang,"windowValForm", sWindowValFormSettings); 
		sWindowValForm.focus(); 
	}
}

// Cookies
function getCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function setCookie(name,value,expires,path,domain,secure) {
var today = new Date();
var expires = new Date(today.getTime() + (24 * 60 * 60 * 1000 * 365));
var path = '/';
    document.cookie = name + "=" +value +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "/") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");        
}

function delCookie(name,value,expires,path,domain,secure) {
var today = new Date();
var expires = new Date(today.getTime() - (24 * 60 * 60 * 1000 * 365));
var path = '/';
    document.cookie = name + "=" +value +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "/") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");        
}
