

//////////////////////////////////////////////////////////////////
//		 START SITE-SPECIFIC FUNCTIONS			//
//////////////////////////////////////////////////////////////////


//////////////////////////
///*gets a page element ID (id_x) and then modifies its content with the HTML string x*/
//////////////////////////

function actualizar_formato(id_x,x){
	var  obj_x = document.getElementById(id_x);
	obj_x.innerHTML = x;
}

function actualizar_bienvenida(id_n,nombre, id_f,fecha, id_g,genero){
	actualizar_formato(id_n,nombre);
	actualizar_formato(id_g,genero);
	actualizar_formato(id_f,fecha);
}


function borrar_usuario(id_n, id_f) {
  	deleteCookie("username");
	deleteCookie("lastvisit");
	deleteCookie("counter");

	var fecha = new Date(); 

	actualizar_formato(id_n,"");
	actualizar_formato(id_f,Date2String(fecha));

  return 1;
}


//////////////////////
/* startup function */
//////////////////////
function bienvenida(id_nombre,id_fecha,id_genero){


	var usuario = reconocer_nombre(id_nombre);
	var fecha   = new Date();
	    fecha.setTime(Date.parse(reconocer_ultima_visita()));

	fixDate(fecha);

//	var fecha_str = getString(fecha);
	var fecha_str = Date2String(fecha);

	var genero = "@"; //actualmente usa un generico, despues segun usuario o nombre

	actualizar_bienvenida(id_nombre, usuario, id_fecha, fecha_str, id_genero, genero);
	if( usuario=="webmaster" ) 
		alert("# visitas: "+contar_visitas() );
	if( usuario=="estephi"||usuario=="estephanie" ) 
		alert("STEFFI!!\t:-D\nĄGracias por venir!" );
}


/////////////	NAME FUNCTIONS	////////////////////////////////
//////////////////////////////////////
//this function retrieves the username-cookie, and the last-visited-cookie and fills 
//	div.bienvenida's elements with the appropiate data. For the time being, the gender-span //	must be filled with either generic form: "a/o" OR @ .


function pedir_nombre() {
	 name = prompt("Por favor escriba su nombre:", "");
	if(name=="null") name="";

  return name;
}


function cambiar_nombre(id_donde) {

	var nuevo = pedir_nombre(); //falta checar si no pusieron 'ESC-key'
	
	// set the new cookie
	if (nuevo) {
	  changeCookieField("username", nuevo);
	} else {
	  nuevo = "";}

	actualizar_formato(id_donde,nuevo);

    return nuevo
}

function reconocer_nombre(id_nombre) {

	var username = getCookie("username");

	// if the cookie wasn't found, ask for the name
	if (username) 	{changeCookieField("username",username);}
	 else 		{username = cambiar_nombre(id_nombre);}

return username
}


//////////////	DATE FUNCTIONS	///////////////////////////

function reconocer_ultima_visita() {

	var dia = 24*60*60*1000; //milisegundos
	var timewindow = (1/12) * dia; //session expiration length

	var now      = new Date();
	var lastdate = new Date();
	var thisdate = new Date();
	var old_date = new Date();

	var lastdateC = getCookie("lastvisit");	
	var thisdateC = getCookie("thisvisit");	

	lastdate.setTime(Date.parse(lastdateC));
	thisdate.setTime(Date.parse(thisdateC));

	//check and fix date integrity
	if( (!thisdate) || ( isNaN(thisdate) ) )
		{thisdate=now;}
	if( (!lastdate) || ( isNaN(lastdate) ) || (lastdate > thisdate) ){
		lastdate = thisdate;
		thisdate = now;}


	//if current session has expired
	if( (now - thisdate) >= timewindow ){
		old_date = (lastdate) ? lastdate : 0;
		lastdate = thisdate;
		thisdate = now;
//		alert("caduco la sesion");
	}

/*
	var logdvisit = getCookie("sessionlog");
	logdvisit = logdvisit + "," + old_date;
	changeCookieField("sessionlog", logdvisit);
*/
	changeCookieField("lastvisit", lastdate);
	changeCookieField("thisvisit", thisdate);

return lastdate;
}


function contar_visitas(){

	var now = new Date();
	var year_ms = 365 * 24 * 60 * 60 * 1000; //milliseconds
	fixDate(now);

	now.setTime(now.getTime() + year_ms);
	var visits = getCookie("counter");

// if the cookie wasn't found, this is your first visit
	if (!visits) {
  		visits = 1; // the value for the new cookie
	} else {
  // increment the counter
  		visits = parseInt(visits) + 1;
	}
// set the new cookie
	changeCookieField("counter", visits);

return visits;
}

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

