﻿function checkUser(all_box, className) {
  var users = document.getElementsByClassName(className);
  if (all_box.checked) {
    for (i = 0; i < users.length; i++) { users[i].checked = true; }
  } else {
    for (i = 0; i < users.length; i++) { users[i].checked = false; }
  }
}
function checkAllBox(checkbox, top_box) {
	top_box = $(top_box)
  if(!top_box) return
  if (top_box.checked && !checkbox.checked) {
    top_box.checked = false;
  }
}

function removeFileBox(id) {
  Element.remove("message_file_box_" + id);
  Element.remove("paragr_file_" + id);
  if(document.getElementById("add_new_file_link")) {
  	Element.show("add_new_file_link"); 
  }
}

function addCategory(ask,type) {
    var name = prompt(ask, "");
    if(!name || name == 'null') return;
    if(name) name = name.strip()
    new Ajax.Request('/'+type+'/new', {asynchronous:true, evalScripts:true,
        parameters:'add_categorie_name=' + encodeURIComponent(name) });
}
function addCustomer(ask) {
	var name = prompt(ask, "");
    if(!name || name == 'null') return;
    if(name) name = name.strip()
    new Ajax.Request('/customers/new', {asynchronous:true, evalScripts:true,
        parameters:'customer_name=' + encodeURIComponent(name) });
}

function afterAddComment(comment_id) {
	new Ajax.Request('/comments/add', {asynchronous:true, evalScripts:true, parameters: 'comment_id='+comment_id});
}
function afterAddMessage(message_id) {
	new Ajax.Request('/messages/new', {asynchronous:true, evalScripts:true, parameters: 'message_id='+message_id});
}
function afterAddMessageError() {
	new Ajax.Request('/messages/new/error', {asynchronous:true, evalScripts:true});
}
function afterEditMessage(message_id) {
	new Ajax.Request('/messages/edit', {asynchronous:true, evalScripts:true, parameters: 'message_id='+message_id});
}
function afterAddMessageNoSpace() {
	new Ajax.Request('/messages/new/space', {asynchronous:true, evalScripts:true});
}
function afterEditMessageNoSpace(message_id) {
	new Ajax.Request('/messages/edit/space', {asynchronous:true, evalScripts:true, parameters: 'message_id='+message_id});
}
function afterAddDocument(document_id) {
	new Ajax.Request('/documents/new', {asynchronous:true, evalScripts:true, parameters: 'document_id='+document_id});
}
function afterAddDocumentError() {
	new Ajax.Request('/documents/new/error', {asynchronous:true, evalScripts:true});
}
function afterAddDocumentNoSpace() {
	new Ajax.Request('/documents/new/space', {asynchronous:true, evalScripts:true});
}
function afterAddCommentDocumentNoSpace() {
	new Ajax.Request('/comments/add/space', {asynchronous:true, evalScripts:true});
}

function afterEditDocument(document_id) {
	new Ajax.Request('/documents/edit', {asynchronous:true, evalScripts:true, parameters: 'document_id='+document_id});
}

function afterEditDocumentNoSpace(document_id) {
	new Ajax.Request('/documents/edit/space', {asynchronous:true, evalScripts:true, parameters: 'document_id='+document_id});
}

function displayHideActions(id, display)
{
	if (display && document.getElementById('actions_'+id))
	{
		//document.getElementById('actions_'+id).style.visibility = 'visible';
	}
	else
	{
		if (document.getElementById('actions_'+id))
		{
			//document.getElementById('actions_'+id).style.visibility = 'hidden';
		}
	}
}

function selectAllProjects() {
	var projects = document.getElementsByClassName('user_permission_project_box');
	
	if(document.getElementById('user_permission_project_all').checked == false) {
		for (i = 0; i < projects.length; i++) { projects[i].checked = false; }
	}
	else {
		for (i = 0; i < projects.length; i++) { projects[i].checked = true; }
	}
}

function hideAddForm(id) {
	new Effect.BlindUp(id);
}


function onLoadPage() {
	//new Ajax.Request('/users/check_js', {asynchronous:true, evalScripts:true});
}

function hideActions() {
	var actions = document.getElementsByClassName('actions');
    for (i = 0; i < actions.length; i++) { actions[i].style.visibility = 'hidden'; }
}
function oncloseactions() {
	//alert('are you sure?');
}


function showHideProjectDetails(bloc) {
	if (document.getElementById(bloc).style.display == 'none') {
        new Effect.BlindDown(bloc); 
		document.getElementById(bloc+'_link').className = 'open';
		 
    }
	else {
		new Effect.BlindUp(bloc);  
		document.getElementById(bloc+'_link').className = 'close';
	}
}


function alert_quit_message(ask){
	if(document.getElementById('message_title').value != '' || document.getElementById('message_contents').value != '') {
//   	  return ask + "messge_title = " + document.getElementById('message_title').value;
   	  return ask;
    }
	else {
		return null;
	}
}


function showSearchClosedTasks() {
	var tasks = document.getElementsByClassName('search_tasks_closed');
    for (i = 0; i < tasks.length; i++) { tasks[i].style.display = 'block'; }
	
	document.getElementById('search_display_closed').style.display = 'none'; 
}

function showSearchArchivedMessages() {
	var tasks = document.getElementsByClassName('search_archived_messages');
    for (i = 0; i < tasks.length; i++) { tasks[i].style.display = 'block'; }
	
	document.getElementById('search_display_archived').style.display = 'none'; 
}

function pageClick() {
	var tips_to = document.getElementsByClassName('more_infos_tip');
    for (i = 0; i < tips_to.length; i++) { 
		tips_to[i].prototip.remove();
	}
	
	var boxes = document.getElementsByClassName('statusbox');
    for (i = 0; i < boxes.length; i++) { 
		boxes[i].style.display = 'none';
		if(boxes[i].id == 'statuscolleagues' || boxes[i].id == 'statusactivity' || boxes[i].id == 'status_help_content') {
			boxes[i].innerHTML = '<img src="/images/loader-big.gif" style="border: 0;" alt="Loader" />';
		}
	}
	
	var boxes = document.getElementsByClassName('status_li current');
    for (i = 0; i < boxes.length; i++) { boxes[i].className = 'status_li'; }

}
function showStatusBox(li_id, box_id, part) {
	
	if (document.getElementById(box_id).style.display != 'block') {
		pageClick();
		//new Effect.SlideDown(box_id);  
		if (box_id == 'status_feedback_link') {
			new Ajax.Request('/contact/show_feedback', {
				asynchronous: true,
				evalScripts: true
			});
		}
		
		document.getElementById(box_id).style.display = 'block';
		document.getElementById(li_id).className = "status_li current";
		
		if (box_id == 'statuscolleagues') {
			new Ajax.Request('/users/show_colleagues_status', {
				asynchronous: true,
				evalScripts: true
			});
			new Ajax.Request('/users/see_users_status', {
				asynchronous: true,
				evalScripts: true
			});			
		}
		
		if (box_id == 'statusactivity') {
			new Ajax.Request('/users/show_recent_activity', {
				asynchronous: true,
				evalScripts: true
			});
		}
		
		if(box_id == 'status_help_content') {
			new Ajax.Request('/help/show/'+part, {
				asynchronous: true,
				evalScripts: true
			});
		}
	}
	else {
		pageClick();
	}
	
}

function buttonLoading(button, text, disabled) {
	document.getElementById(button).disabled = disabled;
	document.getElementById(button).className = 'button loading';
	document.getElementById(button).value = text;
}

function strip_spaces(mystr) {
  return (mystr.replace(/^\W+/,'')).replace(/\W+$/,'');
} 

function commentDocuments(display) {
	if(display) {
		document.getElementById('add_comment_documents_link').style.display = 'none';
		document.getElementById('add_comment_documents_form').style.display = 'block';
	}
	else {
		document.getElementById('add_comment_documents_link').style.display = 'block';
		document.getElementById('add_comment_documents_form').style.display = 'none';
	}
}

function showNewUserWelcomeMessage() {
	document.getElementById('add_user_welcome_message_link').style.display = 'none';
	document.getElementById('add_user_welcome_message_form').style.display = 'block';
	document.getElementById('new_user_message').focus();
	
}

function showHiddenRecentActivity() {
	document.getElementById('show_hidden_activities').style.display = 'none';
	elements_to_display = document.getElementsByClassName('hide_recent_activity');
    for (i = 0; i < elements_to_display.length; i++) { 
		if (elements_to_display[i]) {
			elements_to_display[i].className = 'display';
		}
	}
	elements_to_display = document.getElementsByClassName('hide_recent_activity');
    for (i = 0; i < elements_to_display.length; i++) { 
		if (elements_to_display[i]) {
			elements_to_display[i].className = 'display';
		}
	}
	$('last_changed_list').scrollTop = $('last_changed_list').scrollHeight;
}


function addTooltip(task_id, type, show) {
	offsetX = 0;
	offsetY = 0;
	 
	 new Tip('task_comments_link_'+task_id, {
		title: "Commentaires concernant cette tâche",
		ajax: {
			url: '/tasks/comments/'+task_id+'/'+type
		},
		closeButton: true,
		showOn: 'click',
		hideOn: {element: '.close', event: 'click'},
		stem: 'leftTop',
		hook: { target: 'bottomRight', tip: 'topLeft' },
		width: '250',
		onComplete: function() { $('scroll_comments_'+task_id).scrollTop = $('scroll_comments_'+task_id).scrollHeight; },
		offset: { x: offsetX-5, y: offsetY-20 }
	});

	$('task_comments_link_'+task_id).observe('prototip:hidden',function() {
		addTooltip(task_id,type);
	});
	
	if(show) {
		click_on_elmt('task_comments_link_'+task_id);
	}
}


function toogleTooLateTasks(hide_text,display_text) {
	if($('echeances_late_to_do').style.display == 'none') {
		new Effect.SlideDown('echeances_late_to_do', {duration:0.3,transition : Effect.Transitions.sinoidal});
		$('display_to_late_link_content').innerHTML = hide_text;
	}
	else {
		$('echeances_late_to_do').style.display = 'none';
		$('display_to_late_link_content').innerHTML = display_text;
	}
}

function click_on_elmt(elementtoclick){
	var evt = null, elm = null;
	
	if (document.getElementById) {
		elm = document.getElementById(elementtoclick);
	}
	if (document.createEvent) {
		evt = document.createEvent('MouseEvents');
	}
	if (elm && elm.dispatchEvent && evt && evt.initMouseEvent) {
		evt.initMouseEvent(
		'click', 
		true, // Click events bubble
 		true, // and they can be cancelled
 		document.defaultView, // Use the default view
 		1, // Just a single click
 		0, // Don't bother with co-ordinates
 		0, 
		0, 
		0, 
		false, // Don't apply any key modifiers
 		false, 
		false, 
		false, 
		0, // 0 - left, 1 - middle, 2 - right
 		null); // Click events don't have any targets other than
		
		// the recipient of the click
		elm.dispatchEvent(evt);
	}
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }



function clickOnCalendarCell(date,day,from_controller) {
	
	
	var full_contents = document.getElementsByClassName('full_content');
    for (i = 0; i < full_contents.length; i++) { full_contents[i].style.display = 'none'; full_contents[i].innerHTML = ''}
	
	var loaders = document.getElementsByClassName('calendarloader');
    for (i = 0; i < loaders.length; i++) { loaders[i].style.display = 'none'; }
	var resume_contents = document.getElementsByClassName('resume');
    for (i = 0; i < resume_contents.length; i++) { resume_contents[i].style.display = 'block'; }
	
	$('calendar_full_content_'+day).style.width = '100%';
	$('calendar_cell_loader_'+day).style.display = 'block';
	
	$('calendar_full_content_'+day).show();
	new Ajax.Request('/tasks/load_calendar_cell_full_content/'+date+'/'+day+'/'+from_controller, {
		asynchronous: true,
		evalScripts: true
	});
}


function setVatExemple(country_id) {
	new Ajax.Request('/companies/set_vat_exemple/'+country_id, {
		asynchronous: true,
		evalScripts: true
	});
}


function showChooseProjectPan() {
	$('showchooseprojectpan').show();
}

function hideChooseProjectPan() {
	setTimeout("document.getElementById('showchooseprojectpan').style.display = 'none';",1000);
}


function addToCalendar(cell_id,date, trad_title, right_left) {
	new Tip('planning_cell_link_'+cell_id, {
		title: trad_title,
		width: '550',
		ajax: {
			url: '/events/new/'+cell_id+'/'+date
		},
		closeButton: true,
		showOn: 'click',
		hideOn: {element: '.close', event: 'click'},
		stem: (right_left.toLowerCase())+'Top',
		className: 'event',
		hook: { target: 'bottomRight', tip: 'top'+right_left },
		offset: { x: -10, y: -17 }
	});
}

function toogleChooseProject() {
	if($('project-chooser-container').style.display == 'none') {
		$('project-chooser-container').style.display = 'block';		
		$('search_project_term').focus();
	}
	else {
		$('project-chooser-container').style.display = 'none';
	}
}

var timeoutprojectpan;

function showChooseProject() {
	$('project-chooser-container').style.display = 'block';
	
	eval ("clearTimeout(timeoutprojectpan);" );
}

function hideChooseProject() {
	eval("timeoutprojectpan = window.setTimeout(hideChooseProjectNoTimer, 2000 );");
}

function hideChooseProjectNoTimer() {
	new Effect.Fade('project-chooser-container',{duration:0.5});
}

var accountsubmenu_shown = false;
function toggleAccountSubMenu() {
	if (accountsubmenu_shown) {
		document.getElementById('accountsubmenu').style.display = 'none';
	}
	else {
		document.getElementById('accountsubmenu').style.display = 'block';
	}
	accountsubmenu_shown = !accountsubmenu_shown;
}


function addEventTooltip(id,from_companies, trad_title) {
	if(from_companies == true)
	{
		theurl = '/events/show/'+id+'/companies'
	}
	else {
		theurl = '/events/show/'+id
	}
	new Tip('link-to-event-'+id, {title: trad_title, width: '550',ajax: {url: theurl},closeButton: true,showOn: 'click',hideOn: {element: '.close', event: 'click'},stem: 'leftTop',className: 'event',hook: { target: 'bottomLeft', tip: 'topLeft' },offset: { x: 0, y: -17 }});
}
