// JavaScript Document
//
// Scripts con los escritores y lectores de cookies

var expDays = 365;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function Who(info){
	var VisitorName = GetCookie('VisitorName')
	if (VisitorName == null) {
		VisitorName = "Invitado";
//		VisitorName = prompt("Who are you?");
//		SetCookie ('VisitorName', VisitorName, exp);
	}
	return VisitorName;
}

function Comunidad(info){
	var VisitorName = GetCookie('CodeDir')
	if (VisitorName == null) {
		CodeDir = "demo";
//		VisitorName = prompt("Who are you?");
//		SetCookie ('VisitorName', VisitorName, exp);
	}
	else
		CodeDir = VisitorName;
	return CodeDir;
}

function redireccionar(info){
	var VisitorName = GetCookie('CodeDir')
	if (VisitorName == null) {
		CodeDir = "demo";
//		VisitorName = prompt("Who are you?");
//		SetCookie ('VisitorName', VisitorName, exp);
	}
	else {
		CodeDir = VisitorName;
	}
	
	document.location="http://www.kohlerfincas.com/comunidades/" + CodeDir + "/" + info +".html";
}

function When(info){
	var rightNow = new Date()
	var WWHTime = 0;

	WWHTime = GetCookie('WWhenH')
	WWHTime = WWHTime * 1
	var lastHereFormatting = new Date(WWHTime);
	var intLastVisit = (lastHereFormatting.getYear() * 10000)+(lastHereFormatting.getMonth() * 100) + lastHereFormatting.getDate()
	var lastHereInDateFormat = "" + lastHereFormatting;
	var dayOfWeek = lastHereInDateFormat.substring(0,3)
	var dateMonth = lastHereInDateFormat.substring(4,11)
	var timeOfDay = lastHereInDateFormat.substring(11,16)
	var year = lastHereInDateFormat.substring(23,25)
	var WWHText = dayOfWeek + ", " + dateMonth + " at " + timeOfDay
	SetCookie ("WWhenH", rightNow.getTime(), exp)
	return WWHText
}

function Count(info){
	var WWHCount = GetCookie('WWHCount')
	if (WWHCount == null) {
		WWHCount = 0;
	}
	else{
		WWHCount++;
	}
	SetCookie ('WWHCount', WWHCount, exp);
	return WWHCount;
}

function set(){
	var clave = 0;
	
	VisitorName = prompt("Introduzca su nombre");
	VisitorCode = prompt("Introduzca su clave de acceso");
	
	// Control de Elizondo 12
	if (VisitorCode.match("KF01-LZND-12")) {
		SetCookie ('VisitorName', VisitorName, exp);
		SetCookie ('WWHCount', 0, exp);
		SetCookie ('WWhenH', 0, exp);
		SetCookie ('CodeDir', 'elizondo12', exp);
		document.location="http://www.kohlerfincas.com/clientes.html";
		clave = 1;
	}
	
	// Control de Clarinetes 9 - 21
	if (VisitorCode.match("KF02-CLRT-921")) {
		SetCookie ('VisitorName', VisitorName, exp);
		SetCookie ('WWHCount', 0, exp);
		SetCookie ('WWhenH', 0, exp);
		SetCookie ('CodeDir', 'clarinetes921', exp);
		document.location="http://www.kohlerfincas.com/clientes.html";
		clave = 1;
	}
	
	// Control de Periana 19
		// Control de Clarinetes 9 - 21
	if (VisitorCode.match("KF03-PRNA-19")) {
		SetCookie ('VisitorName', VisitorName, exp);
		SetCookie ('WWHCount', 0, exp);
		SetCookie ('WWhenH', 0, exp);
		SetCookie ('CodeDir', 'periana19', exp);
		document.location="http://www.kohlerfincas.com/clientes.html";
		clave = 1;
	}

	// Si no es ninguno.
	if (clave == 0) {
		alert("Lo sentimos, pero la clave no es correcta. Compruebe con su administrador su validez.");
	}
}

function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
	endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
	var j = i + alen;    
	if (document.cookie.substring(i, j) == arg)      
		return getCookieVal (j);    
	i = document.cookie.indexOf(" ", i) + 1;    
	if (i == 0) break;   
	}  
	return null;
}

function SetCookie (name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function Carrusel()
{
	var foto = Math.floor(Math.random() * 5);
	var usuario = Who();
	var comunidad = Comunidad();
	
	if(comunidad.match("demo")) {
		if (foto == 0 || foto == 1 || foto == 4) {
			if (foto == 4) {
				var foto = 1;
			}
			else {
				var foto = 0;
			}
			var enlace = "<a href=\"servicios-administracion-fincas.html\">";
		}
		else {
			if (foto == 2) {
				var enlace = "<a href=\"servicios-mancomunidades.html\">";
			}
			else {
				if (foto == 3) {
					var enlace = "<a href=\"/servicios-alquileres.html\">";
				}
				else {
					var enlace = "";
				}
			}
		}
	}
	else {
		var enlace = "<a href=\"#\">";
		var foto = foto + 5;
	}
	
	var imagen = enlace + "<img src=\"/comunidades/"+Comunidad()+"/imagenes/portal"+ foto + ".jpg\" alt=\"Imagen de " + Comunidad() + "- Foto "+ foto +"\" width=\"190\" /></a>";

	return imagen;
}

function Puerta()
{
//		document.write("<p><b>" +Who()+"</b>("+Comunidad()+")</p>"+Carrusel());
		document.write(Carrusel());
}

function Gracias() {

		var usuario = Who();
		
		if ( usuario.match("Invitado") ) {
			document.write("<p>El mensaje al administrador se ha enviado correctamente. Nos pondremos en contacto con usted lo antes posible.</p><p>Muchas gracias por contactar con nosotros.</p>");
		}
		else
		{
			document.write("<p>"+Who()+":</p><p>El mensaje se envi&oacute; correctamente. Le contactaremos lo antes posible.</p><p>Gracias por emplear este servicio</p>");
		}
}

function GraciasPresupuesto() {

		var usuario = Who();
		
		if ( usuario.match("Invitado") ) {
			document.write("<p>La solicitud de presupuesto se ha enviado correctamente. Nos pondremos en contacto con usted lo antes posible.</p><p>Muchas gracias por confiar en nosotros.</p>");
		}
		else
		{
			document.write("<p>"+Who()+":</p><p>La solicitud de presupuesto se envi&oacute; correctamente. Le contactaremos lo antes posible.</p><p>Gracias por emplear este servicio</p>");
		}
}

function GraciasWeb() {

		var usuario = Who();
		
		if ( usuario.match("Invitado") ) {
			document.write("<p>El mensaje al webmaster se ha enviado correctamente. Nos pondremos en contacto con usted lo antes posible.</p><p>Muchas gracias por contactar con nosotros.</p>");
		}
		else
		{
			document.write("<p>"+Who()+":</p><p>El mensaje se envi&oacute; correctamente. Le contactaremos lo antes posible.</p><p>Gracias por emplear este servicio</p>");
		}
}

//  End -->

