function deletesession(){
  window.location='index.php?dsess=1';
}

function SubmitToStep(step){
  
  var _step = step; 
  document.getElementById('step').value=_step;
  
  if(_step){
    _step.value = step;
  	document.frm.submit();
  }
  
}

function getXmlHttpRequest() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function _getReportInfo(pass){
  
  var url = "_getReportInfo.php";  
  var params = "pass="+pass;
  
  var cmsajaxhttp = getXmlHttpRequest();
  
  cmsajaxhttp.open("POST", url, true);
  cmsajaxhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  cmsajaxhttp.setRequestHeader("Content-length", params.length);
  cmsajaxhttp.setRequestHeader("Connection", "close");
  
  cmsajaxhttp.onreadystatechange = function(){
	
		if (cmsajaxhttp.readyState == 4){
						
			var resultado = cmsajaxhttp.responseText;
			//alert(resultado);
			var linha = new Array();
			linha = resultado.split("|");

			var comando = linha[1];

			if(comando == 'OK'){
			  //SubmitToStep(1);
			  	window.location='index.php';
      } else {
        alert('A chave que introduziu não é válida!');
        document.frm.pass.value = '';
        document.frm.pass.focus();
      }
			
		}
		
	}

	cmsajaxhttp.send(params);

}

function show_zoom(_img, _obj){

  var url = "_makeZoomImage.php";  
  var params = "img=" + escape(_img);
  
  var cmsajaxhttp = getXmlHttpRequest();
  
  cmsajaxhttp.open("POST", url, true);
  cmsajaxhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  cmsajaxhttp.setRequestHeader("Content-length", params.length);
  cmsajaxhttp.setRequestHeader("Connection", "close");
  
  cmsajaxhttp.onreadystatechange = function(){
	
		if (cmsajaxhttp.readyState == 4){


			_obj.href = cmsajaxhttp.responseText;
			return hs.expand(_obj);

			
		}
		
	}

	cmsajaxhttp.send(params);
  
}

function selectImg(idx, flag_chkbox, opacity, imgName, chkboxName) {
	
  if(!imgName) 	imgName  = "img";	
  if(!chkboxName) chkboxName  = "imgsel";
  
  var img    = document.getElementById(imgName + idx).style;	 	  	
  var chkbox = document.getElementById(chkboxName + "[" + idx + "]");
  var box    = document.getElementById('a' + idx).style;
  
  if(flag_chkbox) chkbox.checked = !chkbox.checked;
  
  if(chkbox.checked = !chkbox.checked)
  {
 		img.filter = "alpha(opacity=50)";	 
	  img.opacity = ".50";
		//box.backgroundColor = '#2552A5';
		var params = "put=1&img=" + chkbox.name;
  }
  else
  {
    
	  img.filter = "alpha(opacity=100)";	
		img.opacity = "1";
	  //box.backgroundColor = 'transparent';
	  var params = "put=0&img=" + chkbox.name;
  }	

  var url = "selectImg.php";

  var cmsajaxhttp = getXmlHttpRequest();
  
  cmsajaxhttp.open("POST", url, true);
  cmsajaxhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  cmsajaxhttp.setRequestHeader("Content-length", params.length);
  cmsajaxhttp.setRequestHeader("Connection", "close");
  /*
  cmsajaxhttp.onreadystatechange = function(){
	
		if (cmsajaxhttp.readyState == 4){ alert(cmsajaxhttp.responseText); }
		
	}
  */
	cmsajaxhttp.send(params);

}

function SelectAll(layerObj, imgName, chkboxName){
	
	if(!imgName) 	imgName  = "img";	
	if(!chkboxName) chkboxName  = "imgsel";
	
 	var idx = 0, img, chkbox;			

  do {
  
    try {
       
      selectImg(idx, false, true);
      
      chkbox = document.getElementById(chkboxName + "[" + idx + "]");
      
      if(chkbox.checked){
        layerObj.innerHTML = "<div>desmarcar todas</div>";
      } else {
        layerObj.innerHTML = "<div>seleccionar todas</div>";
      }  
      
      idx++;

    } catch(e) { alert("Não existem itens para selecionar."); return; }	
  
                                                                
  
  } while(document.getElementById(chkboxName + "[" + idx + "]"));

}

function _uploadImages(flag, user){
  
  if(flag == 0){
    
    var answer = confirm("Para prosseguir, terá que fazer login. Se não tem conta, registe-se em www.citylab.pt. Deseja prosseguir?");
    if(answer){
      window.location = "http://www.citylab.pt";
    }
    
  } else {
    
    /*var answer = confirm("Deseja fazer o upload das imagens seleccionadas para o seu laboratório?");
    if(answer){*/
      
      var url = "_uploadImages.php";
      var params = "user=" + user;

      var cmsajaxhttp = getXmlHttpRequest();
      
      cmsajaxhttp.open("POST", url, true);
      cmsajaxhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      cmsajaxhttp.setRequestHeader("Content-length", params.length);
      cmsajaxhttp.setRequestHeader("Connection", "close");
      
      cmsajaxhttp.onreadystatechange = function(){
        
        var msgload = document.getElementById('msgloader');
        msgload.style.display = 'block';
        
        //var _first = msgload.firstChild.firstChild.firstChild.firstChild;
        //var _last  = msgload.firstChild.firstChild.firstChild.lastChild;
        
        if (cmsajaxhttp.readyState < 4){
          //_first.innerHTML = '<img src="sysimages/loader.gif">';
          //_last.innerHTML = '&nbsp;&nbsp;&nbsp;As fotos estão a ser carregadas para o seu laboratório.';
        } else {
          //_first.parentNode.removeChild(_first);
          //_last.setAttribute("align", "center");
          //_last.innerHTML = 'As fotos foram carregadas com sucesso.';
          setTimeout('document.getElementById("msgloader").style.display = "none";',2000);
          
          /*var answer2 = confirm("Deseja ir para o seu laboratório?");
          if(answer2){*/
            window.location = 'http://www.citylab.pt/reportagens2/reportagens3/main2.php?id=30&step=2';
          //}
          
        }

      }
    	cmsajaxhttp.send(params);
    	
    //}
    
  }
  
}
