// vim: set encoding=utf-8:
function removeDiacritics(s) {
	var diacritics = [
	//valors en octal
		[/[\300-\306]/g, 'A'],
		[/[\340-\346]/g, 'a'],
		[/[\310-\313]/g, 'E'],
		[/[\350-\353]/g, 'e'],
		[/[\314-\317]/g, 'I'],
		[/[\354-\357]/g, 'i'],
		[/[\322-\330]/g, 'O'],
		[/[\362-\370]/g, 'o'],
		[/[\331-\334]/g, 'U'],
		[/[\371-\374]/g, 'u'],
		[/[\321]/g, 'N'],
		[/[\361]/g, 'n'],
		[/[\307]/g, 'C'],
		[/[\347]/g, 'c'],				
	];
	for (var i = 0; i < diacritics.length-1; i++) {
		s = s.replace(diacritics[i][0], diacritics[i][1]);
		s = s.replace("’","'");
	}
	return s;
}

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/vissir3/";
	
  //idioma
  var loc = new String(document.location.href);
  
  if (-1 != loc.indexOf("/cat/")) {
    windowURL += "?lang=cat";
  } else if (-1 != loc.indexOf("/esl/")) {
    windowURL += "?lang=esl";
  } else if (-1 != loc.indexOf("/eng/")) {
    windowURL += "?lang=eng";  
  } else {
    windowURL += "?lang=cat";
  }  
/*
  if (-1 != loc.indexOf(".es")) {
	loc.replace(".es",".cat")
  }
*/
  if(sToponim) {
		sToponim = removeDiacritics(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 openmapserver(url){
	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;
  }
  window.open(url,"","width="+ w +",height="+ h +",resizable=yes,top=0,left=0,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0");
	//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) {

	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 sUrl = "http://auriga.icc.cat/linklokurl/downloadzip52.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(); 
		*/
		var form = document.createElement("form");
		form.setAttribute("method", "post");
		form.setAttribute("action", "http://auriga.icc.cat/linklokurl/downloadzip52.php");
		form.setAttribute("target", "formresult");
		
        var hiddenField1 = document.createElement("input");
        hiddenField1.setAttribute("type", "hidden");
        hiddenField1.setAttribute("name", "img");
        hiddenField1.setAttribute("value", img);
        form.appendChild(hiddenField1);

        var hiddenField2 = document.createElement("input");
        hiddenField2.setAttribute("type", "hidden");
        hiddenField2.setAttribute("name", "full");
        hiddenField2.setAttribute("value", full);
        form.appendChild(hiddenField2);

        var hiddenField3 = document.createElement("input");
        hiddenField3.setAttribute("type", "hidden");
        hiddenField3.setAttribute("name", "user");
        hiddenField3.setAttribute("value", username);
        form.appendChild(hiddenField3);
		
		document.body.appendChild(form);
		form.submit();
		
	}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/downloadnou22.php"+"?img="+img+"&full="+full+"&lang="+lang,"windowValForm", sWindowValFormSettings); 
		var sWindowValForm = window.open("http://auriga.icc.cat/linklokurl/downloadnou22.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" : "");        
}

