// JavaScript Document
var ajax = false;
var pos = 0;
var societe_select = [];
var continue_attentente = false;

function onKeyPressFondName(e)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);

	//alert(key);
	
	var a = key != 13;
	
	if(!a)
	{
		executeForm(1, 1, {submit: 'execute'});
	}
	return a;
}

function show(id,max_, more_param) 
{
	max_ = 5;
	for(j=1;j<=max_;j++)
	{
		$("detail"+j).className = "onglet_off";
	}
	
	$("detail"+id).className = "onglet_on";
	
	var p = {rating: id};
	if(more_param != undefined)
	{
		for(var i in more_param)
		{
			p[i] = more_param[i];
		}
	}
	close_dialog();
	
	
	
	//if($("rating-action")) $("rating-action").innerHTML = "";
	
	setChargement()
	
	new Ajax.Request( base_url + '/rating_.php',{
		method:'post',
		parameters: p,
		onComplete: function(requester){
			
			ajax = false;
			continue_attentente = false;
			$("rating-msg").setStyle({display: "block"});
			$("rating-action").replace("<div id='rating-action'>"+requester.responseText+"</div>");
			update_search_forms(base_url + '/rating_.php', id);
			setDebug(base_url + '/rating_.php', id);
			
	}});
}

function setDebug(url, id)
{
	new Ajax.Request( url,{
		method:'post',
		parameters: {rating:id, debug: 1},
		onComplete: function(requester){
			$("rating-debug").innerHTML = requester.responseText;
	}});
}


function setChargement()
{
	//window.console.log("rating-msg: "  + $("rating-msg"));
	if(ajax) return;
	ajax = true;
	continue_attentente = true;
	try
	{
		$("rating-msg").setStyle({display: "block", height: ($("detail_rating").getHeight() + 60) +"px"});
		if(Prototype.Browser.IE)
		{
			$("rating-msg").setStyle({marginTop: "-40px", height: ($("detail_rating").getHeight() + 60) +"px"});
		}
	}
	catch(e){
		//return
	}
 
	pos = 0;
	var a=["Chargement ", "Chargement .", "Chargement ..", "Chargement ...", "Chargement ....", "Chargement .....", "Chargement ......", "Chargement ......."];
	var tempoin =  new PeriodicalExecuter(function(pe) {
		if(ajax)
		{
			pos ++;
			if($('attente-ajax') != undefined && continue_attentente)
			{
				$('attente-ajax').innerHTML = a[pos];
				if(pos >= (a.length-1))
				{
					pos = 0;
				}
			}
			else
			{
				
				$("rating-msg").setStyle({display: "none"});
				pe.stop();
			}
		}
		else
		{
			$("rating-msg").setStyle({display: "none"});
			pe.stop();
		}
	}, 0.10);
}

function update_search_forms(url, id)
{
	new Ajax.Request( url,{
		method:'post',
		parameters: {rating: id, variable: "update"},
		onComplete: function(val){
			
			var json = null
			try
			{
				json = val.responseText.evalJSON(true);
			}
			catch(exp)
			{
				json = null;
				//window.console.log(exp);
 			}
			
			if(json == null)
			{
				return;
			}
			
			setSearchDefaultValue(json.fond_name, json.societe, json.pays_sell, json.pays, 
								  json.trie, json.order, json.categories, json.rating, json.persitance, json.tendance)
			
	}});
}

function executeForm(id, page, more_param)
{
	
	var p = {rating: id};
	
	
	if(more_param != undefined)
	{
		for(var i in more_param)
		{
			p[i] = more_param[i];
		}
	}
	
	
	p["fond_name"]=$("fond_name").value;
	p["page"] = 1;
	if(page != undefined) p["page"] = page;
	
	var s ="";
	var sep ="";
	var sel = $("fromaction").elements["gestion"];
	for(i=0;i<sel.length;i++)
	{
		if(sel[i].selected )
		{
			s += sep + sel[i].value;
			sep = ",";
		}
	}
	
	p["gestion"]= s;
	
	p["pays_sell"]=$("pays_sell").value;
	p["pays"]=$("pays").value;
	p["trie"]=$("trie").value;
	
	p["order"]=$("fromaction").elements["order"].value;
	var v = $("fromaction").elements["order"];
	for(j = 0; j < v.length; j ++)
	{
		var a = v[j];
		
		if(a.checked) p["order"]= a.value;
	}
	
	p["categories"]=$("fromaction").elements["categories"].value;
	var v = $("fromaction").elements["tendance"];
	for(j = 0; j < v.length; j ++)
	{
		var a = v[j];
		
		if(a.checked) p["tendance"]= a.value;
	}
	
	var v = $("fromaction").elements["rating"];
	for(j = 0; j < v.length; j ++)
	{
		var a = v[j];
		
		if(a.checked) p["rating_value"]= a.value;
	}
	
	
	if($("fromaction").elements["persitance"].checked)
	{
		p["persitance"]= "on";
	}
	
	//return;
	setChargement();
	
	new Ajax.Request( base_url + '/rating_.php',{
		method:'post',
		parameters: p,
		onComplete: function(requester){
		
		ajax = false;
		$("rating-msg").setStyle({display: "none"});
		
		$("rating-action").replace("<div id='rating-action'>"+requester.responseText+"</div>");
		update_search_forms(base_url + '/rating_.php', id);
		setDebug(base_url + '/rating_.php', id);
			
	}});
	
	
}


function executeEvolution(id, tendance, page, more_param)
{
	close_dialog();
	var p = {rating: id};
	
	
	if($("categorie") != undefined) p["categorie"] = $("categorie").value;
	
	p["tendance"] = tendance;	
	if(page != undefined) p["page"] = page;
	
	if(more_param != undefined)
	{
		for(var i in more_param)
		{
			p[i] = more_param[i];
		}
	}
	
	//return;
	setChargement();
	new Ajax.Request( base_url + '/rating_.php',{
		method:'post',
		parameters: p,
		onComplete: function(requester){

		ajax = false;
		$("rating-msg").setStyle({display: "none"});

		$("rating-action").replace("<div id='rating-action'>"+requester.responseText+"</div>");
		update_search_forms(base_url + '/rating_.php', id);
		setDebug(base_url + '/rating_.php', id);
	}});
}
		
function setSearchDefaultValue(fond_name, societe, pays_sell, pays, trie, order, categories, rating, persitance,  tendance)
{
	$("fond_name").value = fond_name;
	var sel = $("fromaction").elements["gestion"];
	for(j = 0; j < societe.length; j ++)
	{
		for(i=0;i<sel.length;i++)
		{
			if(sel[i].value == societe[j])
			{
				sel[i].selected = true;
			}
		}
	}
	
	//alert(fond_name +","+ societe+","+ pays_sell+","+ pays+","+ trie+","+ order+","+ categories+","+ rating+","+ tendance)
	
	$("fromaction").elements["pays_sell"].value = pays_sell;
	$("fromaction").elements["pays"].value = pays;
	$("fromaction").elements["trie"].value = trie;
	$("fromaction").elements["order"].value = order;
	
	var v = $("fromaction").elements["order"];
	for(j = 0; j < v.length; j ++)
	{
		if(v[j].value == order) v[j].checked = true;
	}
	
	$("fromaction").elements["categories"].value = categories;
	
	var v = $("fromaction").elements["rating"];
	for(j = 0; j < v.length; j ++)
	{
		if(v[j].value == rating) v[j].checked = true;
	}
	$("fromaction").elements["tendance"].value = tendance;
	
	if(persitance == "on") $("fromaction").elements["persitance"].checked = true;
	
	var v = $("fromaction").elements["tendance"];
	for(j = 0; j < v.length; j ++)
	{
		if(v[j].value == tendance) v[j].checked = true;
	}
	
}

function show_pre(id) 
{	
	var master = "";
	for(j=0;j<=20;j++)
	{
		var master = 'content'+j;
		if( $(master) != undefined ) $(master).className = "block-presentation";
		if( $('content'+j+'-pre') != undefined ) 
				$('content'+j+'-pre').setStyle({display: "none"});  
	}
	master = id.replace("-pre", "");
	if( $(id) != undefined ) 
		$(id).setStyle({display: "block"});

	if( $(master) != undefined ) $(master).className = "block-presentation-on";
	
}

function show_ava(id,max_) 
{		
	var master = "";
	for(j=0;j<=20;j++)
	{
		var master = 'texte'+j;
		if( $(master) != undefined ) $(master).className = "block-avantage";
		if( $('texte'+j+'-ava') != undefined ) 
				$('texte'+j+'-ava').setStyle({display: "none"});  
	}
	master = id.replace("-ava", "");
	if( $(id) != undefined ) 
		$(id).setStyle({display: "block"});

	if( $(master) != undefined ) $(master).className = "block-avantage-on";
}

function confirmDocumentDialog(id, doc, form,  isin, titre, societe)
{
	if(isin == undefined) isin = "";
	if(titre == undefined) titre = "";
	if(societe == undefined) societe = "";
	var p = {rating: id, document: doc, isin: isin, titre: titre, societe: societe};
	
	if(form == "setup")
	{
		p["login"] = $("user_login").value;
		p["pass"] = $("user_code").value;
		p["action"] = "login";
	}
	else
	if(form == "subscribe")
	{
		if($("civilite1").checked) p["civilite"] = $("civilite1").value 
		if($("civilite2").checked) p["civilite"] = $("civilite2").value
		if($("civilite3").checked) p["civilite"] = $("civilite3").value
		p["nom"] = $("user_nom").value;
		p["prenom"] = $("user_prenom").value;
		p["user_societe"] = $("user_societe").value;
		p["email"] = $("user_email").value;
		p["telephone"] = $("user_telephone").value;
		p["fonction"] = $("user_fonction").value;
		p["action"] = "subscription";
	}
	else
	if(form == 'rescu')
	{
		p["email"] = $("user_email").value;
		p["action"] = "rescu";
	}
	
	manageDocument(p);
}

function uploadDocument(id, doc, action, isin, titre, societe)
{
	$("rating-dialog-mid").update("");
	
	if(action == undefined) action = "";
	if(isin == undefined) isin = "";
	if(titre == undefined) titre = "";
	if(societe == undefined) societe = "";
	
	manageDocument({rating: id, document: doc, action: action, isin: isin, titre: titre, societe: societe});
	/*
	new Ajax.Request( base_url + '/rating_.php',{
		method:'post',
		parameters:  {rating: id, document: doc, action: action},
		onComplete: function(requester){
		try
		{
			$("rating-dialog-mid").update(requester.responseText);
			open_dialog();
			$("rating-dialog-mid").update(requester.responseText)
		}
		catch(e)
		{
			alert(e);
		}
	}});*/
}

function manageDocument(p)
{
	
	new Ajax.Request( base_url + '/rating_.php',{
		method:'post',
		parameters:  p,
		onComplete: function(requester){
		try
		{
			$("rating-dialog-mid").update(requester.responseText);
			open_dialog();
			$("rating-dialog-mid").update(requester.responseText)
		}
		catch(e)
		{
			alert(e);
		}
	}});
}

function open_dialog() 
{
	w = Math.floor(($("content_rating").getWidth() - $("rating-dialog").getWidth())/2);
	h = Math.floor(($("content_rating").getHeight() - $("rating-dialog").getHeight())/2) - 100;
	
	$("rating-dialog").setStyle({display: "block", marginLeft: w+"px", marginTop: h+"px"});
	
	
	if($("rating-dialog-back") != null) $("rating-dialog-back").setStyle({display: "block", height: ($("detail_rating").getHeight() + 60) +"px"});
	if(Prototype.Browser.IE)
	{
		if($("rating-dialog-back") != null) $("rating-dialog-back").setStyle({marginTop: "-40px", height: ($("detail_rating").getHeight() + 60) +"px"});
	}
	
	
}

function close_dialog() 
{
	$("rating-dialog").setStyle({display: "none"});
	
	if($("rating-dialog-back") != null) $("rating-dialog-back").setStyle({display: "none"});
}

function subscribe(id)
{
	$(id).submit();
}


function selectEvolution()
{
	$("detail1").className="onglet_off";
	$("detail2").className="onglet_off";
	$("detail3").className="onglet_off";
	$("detail4").className="onglet_off";
	$("detail5").className="onglet_off";
	
	$("detail2").className="onglet_on";
}
function submitcsv(id1, id2)
{
	var old = $(id1).action;
	var action = Math.floor((200*Math.random())) + $(id2).value;
	$(id1).action = action;
	
	//alert(old + ", " + $(id1).action + ", "+  action);
}