/**
	Functions for Login
**/

function to_password(inp_txt) {
	if (inp_txt.type != "password") {
		inp = document.createElement("input");
		inp.type="password";
		inp.name="pass";
		inp.id="inputpass";
		inp.className="inputtext";
		inp_txt.parentNode.insertBefore(inp,inp_txt);
		inp_txt.parentNode.removeChild(inp_txt);
		inp.focus();
		return inp;
	}
}
function vide_input(inp) {
	if (inp.value == "Votre Identifiant") {
		inp.value ="";
		inp.className="inputtext";
	}
}


/** Function for rememberMe feature */
function cogitae_setValFromDBCookie( name, value) {

	var form = document.forms['login'];
	if (form) {
		var inp = form.elements[name];
		switch (name) {
			case 'user' : {
				vide_input(inp);
				break;
			}
			case 'pass' : {
				inp = to_password(inp);
//				 form.elements[name];
				break;
			}
		}
		inp.value = value;
	}
}


/**
	Cookie handling
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
		if the expires variable is set, make the correct
		expires time, the current script below will set
		it for x number of days, to make it for hours,
		delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" )
		;
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ ) {
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name ) {
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 ) {
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )	{
		return null;
	}
}


var cgt_redirectOnDone = null;


var cogitae_login = {
	loginbox: null,
	idloginbox: "cogitaeloginbox",
	pidinfologin: 831,
	redirecttourl: null,
	baseurl: null,

	init : function(){
		if (this.loginbox == null) {
		//constructor is a configuration object:
			this.loginbox = new YAHOO.widget.Panel(this.idloginbox, {
				width:"680px", 
				height:"260px", 
				visible:false, 
				modal: true,
				fixedcenter: true,
				underlay: "shadow",
				draggable: false

				}
			);
			document.getElementById(this.idloginbox).style.display = 'block';
			this.loginbox.render();
			this.loginbox.close.innerHTML = "Fermer <span>X</span>";
		}
	},

	showremind: function() {
		if (this.showHide('remindpasswd')) {
			this.loginbox.cfg.setProperty("height", "470px"); 
		} else {
			this.loginbox.cfg.setProperty("height", "260px"); 
		}
	},


	showLogin: function(){
		this.init();
		this.loginbox.show();
		return false;
	},

	
	reloadOnDone : function(xhr) {
		if (!xhr) xhr = cgxhr.xhr;
		if(xhr.readyState  == 4) {
			if(xhr.status  == 200) {
				if (typeof cgt_redirectOnDone == "string" ) {
					window.location.href = cgt_redirectOnDone;
				} else {
					window.location = window.location;
				}
			}
		}
	},


	logout: function() {
//		var urltogo = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + window.location.pathname;
		var urltogo = window.location.protocol + '//' + window.location.hostname + window.location.pathname;

		var params = "logintype=logout";
		
		if (typeof cgt_redirectafterlogouturl == "string" ) {
			cgt_redirectOnDone = cgt_redirectafterlogouturl;
//			this.debug("cgt_redirectOnDone : " + cgt_redirectOnDone);
			
			// because page might be unreachable
//			urltogo = cgt_redirectafterlogouturl;
		}
		cgxhr.doPOST(urltogo, params, this.reloadOnDone );
	},


	sendForm: function(frmN) {
		try {
			if (cgt_redirectOnDone) {
				var frm = document.forms[frmN];
				if (frm)
					frm.action = cgt_redirectOnDone;
			}
		} catch ( e ) {
		}
		return cgxhr.sendForm(frmN, this.reloadOnDone);
	},


	showHide: function(id) {
		var elm = document.getElementById(id);
		if (elm) {
			if (elm.style.display == 'none') {
				elm.style.display = 'block';
				return true;
			} else {
				elm.style.display = 'none';
				return false;
			}
		}
	},

	cogitae_set_callbacks: function() {
		YAHOO.util.Event.on("loginButtona", "click", cogitae_login.showLogin);
	},
	
	debug: function(txt) {
		var divdbg = document.getElementById('cgdebug');
		if (divdbg) {
			divdbg.innerHTML += txt + "<br>";
			return true;
		} else {
			return false;
		}
	}
	

};


function cogitae_cb_logininfos( xhr ) {
	if (!xhr) xhr = cgxhr.xhr;
	if(xhr.readyState  == 4) {
		if(xhr.status  == 200) {
			var nav = document.getElementById("headnav");

			var userinfo = cgxhr.getContentByIds(xhr.responseText, 'content_right', 'content_border');
			var testIsConnected = new RegExp(/logintype=logout/);
			if ( testIsConnected.test(userinfo) ) {
				if (nav) {
					nav.innerHTML = nav.innerHTML + '&nbsp;&#124;&nbsp;<a rel="nofollow" href="mon-compte/mon-compte/" class="extrafirst extralink">Mon compte</a>';
				}
				var contenthead = document.getElementById("contenthead");
				if (contenthead) {
					contenthead.innerHTML = contenthead.innerHTML + userinfo;
				}
			} else {
				if (nav) {
					nav.innerHTML = nav.innerHTML + '&nbsp;&#124;&nbsp;<a id="loginButtona" onclick="cogitae_login.showLogin(); return false;" rel="nofollow" href="#" class="extrafirst extralink">Mon compte</a>';
				}
				var loginform = cgxhr.getContentByIds(xhr.responseText, 'content_center', 'content_right');
				document.getElementById('login_content').innerHTML=loginform;
			}
		}
	}
};


function cogitae_active_login( pid ) {

if (typeof pid != "undefined") cogitae_login.pidinfologin = parseInt(pid);

//	document.write('<div id="cogitaeloginbox" style="display: none;"><div class="hd"></div><div class="bd" id="login_content"></div><div class="ft"></div></div>');
//	document.body.innerHTML += '<div id="cogitaeloginbox" style="display: none;"><div class="hd"></div><div class="bd" id="login_content"></div><div class="ft"></div></div>';
	var params = "now=" + new Date().getTime();

	cogitae_login.baseurl = window.location.protocol + '//' + window.location.hostname + '/';

	cgxhr.doPOST(cogitae_login.baseurl + '?id=' + cogitae_login.pidinfologin, params, cogitae_cb_logininfos );
}

function cogitae_do_login( frmN, redirectto ) {
	if (redirectto)
		cgt_redirectOnDone = redirectto;
	
	cogitae_login.sendForm(frmN);

}



function showAlerteCrise() {
    var crise = document.getElementById('infoCrise');
    if (crise) crise.style.display='inline-block';
}
