var domain = window.location.host.substr(window.location.host.lastIndexOf(".", window.location.host.lastIndexOf(".")-1));
function Cookie(name, value)
{
  this.name = name;
  this.value = value;
  this.expires = new Date();
  this.path = "/";
  this.domain = domain;
  this.CookieSet = CookieSet;
  this.CookieRemove = CookieRemove;
  return this;
}
function CookieSet()
{
  var c = "";
  c += this.name + "=" + escape(this.value);
  if (this.expires != null)
    c += "; expires=" + this.expires.toGMTString();
  if (this.path != null)  
    c += "; path=" + this.path;
  if (this.domain != null)  
    c += "; domain=" + this.domain;
  document.cookie = c;
}
function CookieRemove()
{
  var c = "";
  c += this.name + "=" + escape(this.value);
  if (this.expires != null)
    c += "; expires=" + new Date(1975,1,1);
  if (this.path != null)  
    c += "; path=" + this.path;
  if (this.domain != null)  
    c += "; domain=" + this.domain;
  document.cookie = c;
}
function CookieGet(name)
{
  var cookies = document.cookie;
  var pos1 = cookies.indexOf(name + "=");
  if (pos1==-1) return null;
  var pos2 = cookies.indexOf(";", pos1);  
  if (pos2!=-1) {
    return unescape(cookies.substring(pos1 + name.length + 1, pos2));
  } else {
    return unescape(cookies.substr(pos1 + name.length + 1));
  }
}
function ValidEmail(email) {
    if (window.RegExp) {
        var re1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)");
        var re2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
        if (!re1.test(email) && re2.test(email)) return true;
        return false;
    } else {
        if(str.indexOf("@") >= 0) return true;
        return false;
    }
}
/*Validate http and a dot*/
function ValidURL(url)
{
    if(url.indexOf("http://") == 0){
		var urlNoProtocol = url.substring(7, url.length);
		var hostEnd = urlNoProtocol.indexOf("/");
		if (hostEnd == -1) hostEnd = urlNoProtocol.length;
		var host = urlNoProtocol.substring(0, hostEnd);
		if (host.indexOf(".") != -1){
			return true;
		}
	}
	return false;
}


function Trim(s)
{
  if (s==null) return null;
  var r = s;
  while (r.length>0 && (r.charAt(0)==' '||r.charAt(0)=='\r'||r.charAt(0)=='\n'||r.charAt(0)=='\t')) {
    r = r.substring(1, r.length);
  }
  while (r.length>0 && (r.charAt(r.length-1)==' '||r.charAt(r.length-1)=='\r'||r.charAt(r.length-1)=='\n'||r.charAt(r.length-1)=='\t')) {
    r = r.substring(0, r.length-1);
  }
  return r;
}
function CharacterEncode(s)
{
  var length = s.length;
  var c, output = '';
  for(var i=0; i<length; i++) {
    c = s.charCodeAt(i);
    if (c < 32 && c!=10 && c!=13) {
      output += '&#' + c + ';';
    } else {
      output += s.charAt(i);
    }
  }
  return output;
}
function PostIt(f)
{
  if (Trim(f.CN.value).length > 25){
  	alert("El nombre no puede exceder los 25 caracteres.");
	f.CN.focus();
	f.CN.select();
  } else if (Trim(f.CE.value).length > 0 && !ValidEmail(f.CE.value)){
  	alert("Por favor, introduce un email válido");
	f.CE.focus();
	f.CE.select();
  } else if (Trim(f.CW.value).length > 0 && Trim(f.CW.value) != "http://" && !ValidURL(f.CW.value) && !ValidURL("http://" + f.CW.value)){
  	alert("Por favor, introduce una dirección web válida");
	f.CW.focus();
	f.CW.select();
  } else if (Trim(f.CT.value).length == 0 || Trim(f.CT.value).length == 2.500){
  	alert("El texto del comentario tiene que tener entre 0 y 2500 caracteres.");
	f.CT.focus();
  } else {
  	var optionChecked;
	for(var i=0; i<f.CSD.length; i++){
		if(f.CSD[i].checked){
			optionChecked = i;	
		}	
	}
    if (optionChecked == 0){
		var cookieValue = f.CN.value + "|" + f.CE.value + "|" + f.CW.value
		//alert("Poniendo la cookie: " + cookieValue);
		var commentCookie = new Cookie("COMMENT_BLOGS", cookieValue);
		commentCookie.expires = new Date(new Date().getTime() + 1000*60*60*24*365);
		commentCookie.CookieSet();
		//alert("commentCookie: " + CookieGet("COMMENT_BLOGS"));
	} else {
		//alert("Borrando la cookie");
		var commentCookie = new Cookie("COMMENT_BLOGS", "");
		commentCookie.CookieRemove();
		//alert("commentCookie: " + CookieGet("COMMENT_BLOGS"));
	}
    f.CT.value = CharacterEncode(f.CT.value);
    f.RU.value = window.location.href;
	if(Trim(f.CW.value) == "http://"){
		f.CW.value = '';
	}
	/*
    if(Trim(f.CW.value).length > 0 && f.CW.value.indexOf("http://") != 0){
  	  f.CW.value = "http://" + f.CW.value;
    }
	*/	
    f.submit();
  }
}
function DoOnLoadComments(){
	var cookieValue = CookieGet("COMMENT_BLOGS");
	var f = document.forms["F_COMMENT1"];
	if (cookieValue != null){
		var splitted = cookieValue.split("|");
		if (splitted.length == 3){
			var commentName = splitted[0];
			var commentEmail = splitted[1];
			var commentWeb = splitted[2]; 
			f.CN.value = commentName;
			f.CE.value = commentEmail;
			f.CW.value = commentWeb;
			f.CSD[0].checked = true;
		}
	} else {
		f.CW.value = "http://";
		f.CSD[1].checked = true;
	}
}

