var xmlhttp = getXmlHttpRequest();
var page1 = 1;
var page2 = 1;

setInterval("setTheTime()", 60000);


function play_after() {
	xmlhttp.open("POST", 'cams/'+beach_to_show+'.php', true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState==4){
			document.getElementById('camdiv').innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}

function setTheTime () {
	var curtime = new Date();
	var curhour = curtime.getHours();
	var curmin = curtime.getMinutes();
	var time = "";
	if(curhour == 0) curhour = "00";
	time = curhour + ":" + (curmin < 10 ? "0" : "") + curmin;
	document.getElementById("daytime").innerHTML = time;
}

function toggle_visibility(div1,div2,height1,top2) {
       var e = document.getElementById(div1);
       var e2 = document.getElementById(div2);

	if (div1=='themes')
	{
		document.getElementById('registar').style.height = 0;
		document.getElementById('registar').style.visibility = 'hidden';
		document.getElementById('registar').style.display = 'none';
		document.getElementById('login').style.height = 0;
		document.getElementById('login').style.visibility = 'hidden';
		document.getElementById('login').style.display = 'none';
	}

	if (div1=='registar')
	{
		document.getElementById('themes').style.height = 0;
		document.getElementById('themes').style.visibility = 'hidden';
		document.getElementById('themes').style.display = 'none';
		document.getElementById('login').style.height = 0;
		document.getElementById('login').style.visibility = 'hidden';
		document.getElementById('login').style.display = 'none';
	}

	if (div1=='login')
	{
		document.getElementById('themes').style.height = 0;
		document.getElementById('themes').style.visibility = 'hidden';
		document.getElementById('themes').style.display = 'none';
		document.getElementById('registar').style.height = 0;
		document.getElementById('registar').style.visibility = 'hidden';
		document.getElementById('registar').style.display = 'none';
	}


       if(e.style.display == 'block') {
		e2.style.top = 0;
		e.style.height = 0;
       	 	e.style.visibility = 'hidden';
          	e.style.display = 'none'; }
       else {
    		e2.style.top = top2;
	   	e.style.height = height1;    
          	e.style.display = 'block';
       	 	e.style.visibility = 'visible';
	}
}

function setTheme(background_theme,top_theme) {
	document.getElementById('wbody').className = background_theme;
	document.getElementById('back_header').className = top_theme;
	set_cookie ( "bg_theme", background_theme,2, "www.beachcam.pt" );
}

//COOKIES
function set_cookie ( cookie_name, cookie_value,lifespan_in_days, valid_domain )
{
    var domain_string = valid_domain ? ("; domain=" + valid_domain) : '' ;
    document.cookie = cookie_name + "=" + encodeURIComponent( cookie_value ) + "; max-age=" + 60 * 60 * 24 * lifespan_in_days + "; path=/" + domain_string ;
}

function get_cookie ( cookie_name )
{
    var cookie_string = document.cookie ;
    if (cookie_string.length != 0) {
        var cookie_value = cookie_string.match ('(^|;)[\s]*' + cookie_name + '=([^;]*)' );
        return decodeURIComponent(cookie_value[2]);
    }
    return '' ;
}

function delete_cookie ( cookie_name, valid_domain )
{
    var domain_string = valid_domain ? ("; domain=" + valid_domain) : '' ;
    document.cookie = cookie_name + "=; max-age=0; path=/" + domain_string ;
}



//AJAX
function getXmlHttpRequest() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function getVideos(dir) {
		document.getElementById('dcont').innerHTML = "<img src='img/loader.gif' border='0'>";
		page2 = page2 + (dir);
		if (page2==1) 
			document.getElementById('dant').innerHTML = "<div style='background:url(img/setas_videos.png);width:15px;height:35px;'></div>";
		else
			document.getElementById('dant').innerHTML = "<div style='background:url(img/setas_videos.png) no-repeat -30px 0;width:15px;height:35px;'  onmouseover='document.body.style.cursor='pointer';' onmouseout='document.body.style.cursor='default';' onclick='getVideos(-1);'></div>";

		if (page2==3) 
			document.getElementById('dpos').innerHTML = "<div style='background:url(img/setas_videos.png) -15px 0;width:15px;height:35px;'></div>";
		else
			document.getElementById('dpos').innerHTML = "<div style='background:url(img/setas_videos.png) no-repeat -45px 0;width:15px;height:35px;'  onmouseover='document.body.style.cursor='pointer';' onmouseout='document.body.style.cursor='default';' onclick='getVideos(1);'></div>";

		xmlhttp.open("POST", 'getVideos.php?page='+page2, true);
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState==4){
				document.getElementById('dcont').innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
}


function getNews(dir) {
		document.getElementById('dcontn').innerHTML = "<img src='img/loader.gif' border='0'>";
		page1 = page1 + (dir);
		if (page1==1) 
			document.getElementById('dantn').innerHTML = "<div style='background:url(img/setas_noticias_videos.png);width:15px;height:35px;'></div>";
		else
			document.getElementById('dantn').innerHTML = "<div style='background:url(img/setas_noticias_videos.png) no-repeat -30px 0;width:15px;height:35px;'  onmouseover='document.body.style.cursor='pointer';' onmouseout='document.body.style.cursor='default';' onclick='getNews(-1);'></div>";

		if (page1==3) 
			document.getElementById('dposn').innerHTML = "<div style='background:url(img/setas_noticias_videos.png) no-repeat -15px 0;width:15px;height:35px;'></div>";
		else
			document.getElementById('dposn').innerHTML = "<div style='background:url(img/setas_noticias_videos.png) no-repeat -45px 0;width:15px;height:35px;'  onmouseover='document.body.style.cursor='pointer';' onmouseout='document.body.style.cursor='default';' onclick='getNews(1);'></div>";

		xmlhttp.open("POST", 'getNews.php?page='+page1, true);
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState==4){
				document.getElementById('dcontn').innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
}

function getVideo(id) {
		document.getElementById('video').innerHTML = "<img src='img/loader.gif' border='0'>";

		xmlhttp.open("POST", 'video.php?id='+id, true);
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState==4){
				document.getElementById('video').innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
}

function moreVideos(lpage) {
		document.getElementById('dcont').innerHTML = "<img src='img/loader.gif' border='0'>";

		xmlhttp.open("POST", 'videos_list.php?page='+lpage, true);
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState==4){
				document.getElementById('dcont').innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
}

function moreNews(lpage) {
		document.getElementById('dcont').innerHTML = "<img src='img/loader.gif' border='0'>";

		xmlhttp.open("POST", 'news_list.php?page='+lpage, true);
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState==4){
				document.getElementById('dcont').innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
}


function login() {

	ok=0;
	document.form1.lemail.style.border='none';
	document.form1.lpassword.style.border='none';

	email = document.form1.lemail.value;
	senha = document.form1.lpassword.value;

	if (email=="")
	{
		ok=1;
		document.form1.lemail.style.border='1px red solid';
	}
	else
	{
		if (echeck(email)==false)
		{
			ok=1;
			document.form1.lemail.style.border='1px red solid';
		}
	}
	if (senha=="")
	{
		ok=1;
		document.form1.lpassword.style.border='1px red solid';
	}
	if (ok==0) {
		document.getElementById('login').innerHTML = "<img src='img/loader.gif' border='0'>";
		var poststr = "email=" + escape(encodeURI(email)) + "&senha="+ escape(encodeURI(senha));
		makePOSTRequest2('login.php', poststr);
	}


}

function registar() {

	ok=0;
	document.form1.nome.style.border='none';
	document.form1.email.style.border='none';
	document.form1.ipassword.style.border='none';

	nome = document.form1.nome.value;
	email = document.form1.email.value;
	senha = document.form1.ipassword.value;

	if (nome=="")
	{
		ok=1;
		document.form1.nome.style.border='1px red solid';
	}
	if (email=="")
	{
		ok=1;
		document.form1.email.style.border='1px red solid';
	}
	else
	{
		if (echeck(email)==false)
		{
			ok=1;
			document.form1.email.style.border='1px red solid';
		}
	}
	if (senha=="")
	{
		ok=1;
		document.form1.ipassword.style.border='1px red solid';
	}
	if (ok==0) {
		document.getElementById('registar').innerHTML = "<img src='img/loader.gif' border='0'>";
		var poststr = "nome=" + escape(encodeURI(nome)) +"&email=" + escape(encodeURI(email)) + "&senha="+ escape(encodeURI(senha));
		makePOSTRequest('registar.php', poststr);
	}

}

   var http_request = false;

   function makePOSTRequest(url, parameters) {

      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }


   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;            
		//window.location = 'formulario_ok.html';
		document.getElementById('registar').innerHTML = result;
         } else {
            alert(http_request.status);
         }
      }
   }


   function makePOSTRequest2(url, parameters) {

      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContentsl;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }


   function alertContentsl() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;            
		//window.location = 'formulario_ok.html';
		document.getElementById('login').innerHTML = result;
         } else {
            alert(http_request.status);
         }
      }
   }


function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }

	 return true					
}






function registar_sbb() {

	ok=0;


	document.form1.sbbnome.style.border='1px #000000 solid';
	document.form1.sbbemail.style.border='1px #000000 solid';
	document.form1.sbbtelefone.style.border='1px #000000 solid';
	document.form1.sbbano.style.border='1px #000000 solid';
	document.form1.sbbnpax.style.border='1px #000000 solid';

	nome = document.form1.sbbnome.value;
	email = document.form1.sbbemail.value;
	telefone = document.form1.sbbtelefone.value;
	ano = document.form1.sbbano.value;
	pax = document.form1.sbbnpax.value;


	if (nome=="")
	{
		ok=1;
		document.form1.sbbnome.style.border='1px red solid';
	}
	if (email=="")
	{
		ok=1;
		document.form1.sbbemail.style.border='1px red solid';
	}
	else
	{
		if (echeck(email)==false)
		{
			ok=1;
			document.form1.sbbemail.style.border='1px red solid';
		}
	}
	if (telefone=="")
	{
		ok=1;
		document.form1.sbbtelefone.style.border='1px red solid';
	}
	else
	{
		if (!IsNumeric(telefone)) {
			ok=1;
			document.form1.sbbtelefone.style.border='1px red solid';
		}
		else
		{
			if (telefone.length!=9) {
				ok=1;
				document.form1.sbbtelefone.style.border='1px red solid';
			}
		}	
	}
	if (ano=="")
	{
		ok=1;
		document.form1.sbbano.style.border='1px red solid';
	}
	else
	{
		if (!IsNumeric(ano)) {
			ok=1;
			document.form1.sbbano.style.border='1px red solid';
		}
		else
		{
			if (ano.length!=4) {
				ok=1;
				document.form1.sbbano.style.border='1px red solid';
			}
		}	
	}

	if (pax=="")
	{
		ok=1;
		document.form1.sbbnpax.style.border='1px red solid';
	}
	else
	{
		if (!IsNumeric(pax)) {
			ok=1;
			document.form1.sbbnpax.style.border='1px red solid';
		}
	}

	if (ok==0) {
		document.form1.submit();
	}

}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }




