var palette_window;

function getCookie (name) {
  var prop = name + "="; 
  var plen = prop.length;
  var InCookie=document.cookie;
  var clen = InCookie.length;
  if (clen>0)
    {
      i= InCookie.indexOf(prop, 0) ;
      if( i!=-1)
	{
	  j= InCookie.indexOf (";" , i+plen) ;
	  if(j!=-1)
	    {
	      return unescape(InCookie.substring(i+plen,j)) ;
	    }
	  else
	    {
	      return unescape(InCookie.substring(i+plen,clen));
	    }
	}
      else
	{
	  return "";
	}
    }
  else
    {
      return "";
    }
}

function setCookie(name,value, path) {
  var date=new Date();
  date.setTime(date.getTime() + 365*86400);
  if ( path.length==0 )
    {	
      path="/";
    }	
  date.toGMTString();
  document.cookie= name + '=' + value + ';expires=' + date.toGMTString() + ';path=' + path;
}


function setTempCookie(name,value,path)
{
  var date=new Date();
  date.setTime(date.getTime() + 1000 * 60);
  if ( path.length < 1 )
    path="/";
  document.cookie= name + '=' + value + ';expires=' + date.toGMTString() + ';path=' + path;
}

function showCategoryImg(category) {
  var img = '/icons/com/';
  var mycat_visitor = getCookie("cat_visitor");
  if (category.lastIndexOf("(none)")==-1 )
    {
      setTempCookie('cat_visitor',category,'/');
      return (img +  category + '.gif');
    }
  if ( mycat_visitor.length > 2 )
    return ( img +  mycat_visitor + '.gif');
  return (img + 'respublica.gif');
}


function openCategoryPage(category) {
  var mycat_visitor = getCookie("cat_visitor");
 if ( category != '(none)' )
    {
      setTempCookie('cat_visitor',category,'/');
      self.location =  '/site/category/' + category ;
    }
  else if ( mycat_visitor.length > 2 )
    {
      self.location =  '/site/category/' + mycat_visitor ;
    }
  else
    {
      self.location =  '/site/' ;
    }
  return true;
}

function showDate() {
  var today= new Date();
  var weekday=new Array('dimanche','lundi','mardi','mercredi','jeudi','vendredi','samedi');
  var month=new Array('janvier','février','mars','avril','mai','juin','juillet','août', 'septembre', 'octobre' , 'novembre', 'décembre');
  var valeur = weekday[today.getDay()] + ' ' + today.getDate() + ' ' + month[today.getMonth()] + ' - ' + today.getHours() + ':' + (today.getMinutes()<10?'0':'') + today.getMinutes() ;    
  return valeur ;
}


function FormatPrmString(instring)
{
var i, curc, res = '';
for (i = 0; i < instring.length; i++)
{
curc = instring.substring(i,i+1);
if (curc == ' ') curc = '+';
res += curc;
}
return res;
}


function window_chat(nick, channel, type, size, age, sex, city)
{
var livechatmode = 0;

if ((nick.length < 1) || (nick == "Votre pseudo"))
{
nick = prompt('Veuillez rentrer un pseudo : ','');
if ((nick == null) || (nick.length < 1)) return;
}
setCookie("chat_pseudo", nick, "");
if ((! channel) || (channel.length < 1))
{
channel = prompt('Veuillez rentrer un canal : ','');
if ((channel == null) || (channel.length < 1)) return;
setCookie("chat_canal", channel, "");
}
else
{
setCookie("chat_canal", channel, "");
}

var width, height, moder, lcwin, url = '';
height=490; width=568;
if (navigator.appVersion.indexOf('3.') != -1) { width += 20; height += 20; }

if (livechatmode > 2) { moder = 'on'; }
if (livechatmode > 1) { channel = 'livechat'; }
if ((livechatmode > 0) || (channel == 'livechat')) { lcwin = 'on'; }
if (lcwin == 'on')
	{
	url = 'http://chat.respublica.fr/chat_win_even.html'; width = 768;
	}
else { url = 'http://chat.respublica.fr/chat_win.html'; }

url += '?nick=' + FormatPrmString(nick) + '&channel=' + channel + '&type=' + type
	+ '&sx=' + width + '&sy=' + height + '&asv='
	+ '&lcwin=' + lcwin + '&moder=' + moder;

var window_app = 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,';
window_app += 'resizable=no,width=' + width + ',height='+ height;
var chat_win=window.open(url,'res_chat',window_app);
}

function get_preferences(doc) {
  var i,j,k,l;
  if(!doc) doc=window.document;
  var pathName = window.location.pathname;
  if( (pathName.indexOf('inscription',0)<1) && (pathName.indexOf('modification',0)<1) && (pathName.indexOf('admin',0)<1) )
  {
  var var_list=new Array("user","passwd","nick","email");
  for(i=0;i<doc.forms.length;i++)
    {	
      for(j=0;j<4;j++)
	{	
	  for(k=0;k<doc.forms[i].length;k++)
	    if(doc.forms[i].elements[k].name==var_list[j])
	      {
		l = getCookie(var_list[j]);
		if ( l.length > 3 )
		  doc.forms[i].elements[k].value=l;
	      }
	}
    }
  }
}

function start() {
    var user = getCookie("user") ;
    var passwd = getCookie("passwd") ;
//    var start = getCookie("start") ;
//    var started = getCookie("started") ;
    get_preferences() ;
    if ((user.length > 1) && ( passwd.length > 1)) {
        if (! getCookie('status')) {
            open_frame(user,passwd) ;
        }
    }
}	

function open_frame(user,passwd){
  user=user.toLowerCase();

  // Force cookies...
  if (! getCookie('user')) {
      if (confirm("Voulez-vous sauvegarder vos informations sur votre disque ?")) {
          setCookie('user', user, "/") ;
          setCookie('passwd', passwd, "/site") ;
          setCookie('passwd', passwd, "/admin") ;
      }
  }

  var path = '/admin/generic/palette?user=' + user;
  if (passwd != '') {
    path += '&passwd=' + passwd; 
  }

  if (navigator.appName.search("Netscape") != -1) {
    width=260;
    height=340;
  } else {
    width=255;
    height=330;
  }

  palette_window=window.open(path,'frame_palette','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=' + width + ',height=' + height);
  return false;
} 

function isEmailOk(email) {
  var ok = true;
  ok = ok && (email.indexOf('@')>0) && (email.length - email.lastIndexOf('.')<5);
  if (!ok) alert('votre adresse email est incorrecte');
  return ok;
}

function setHomePage(home_page)
{ 
  var browser=navigator.userAgent.toLowerCase();
  if(browser.indexOf("4.")>0)
    {
      if(browser.indexOf("msie",0)>0)
	{
	  alert('Selectionnez "Ouvrir ce fichier a partir de son emplacement actuel" dans la prochaine fenetre');
	  page="make_default_msie.reg";
	}
      else page="make_default_unknown.html";
    }
  else if(browser.indexOf("5.")>0)
    {
      if(browser.indexOf("msie",0)>0)
	{
	  alert('Selectionnez "Ouvrir ce fichier a partir de son emplacement actuel" dans la prochaine fenetre');
	  page="make_default_msie.reg";
	}
      else page="make_default_unknown.html";
    }
  else page="make_default_unknown.html";
  setCookie('home_page',home_page,'/');
  var url="/site/admin/" + page;
  window.open(url,'make_default','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=380,height=200');
}

