var host = window.location.hostame;
var intranet = '/intranet/';
var returnlocation = intranet+"workspace.php";
var workspace = intranet+"workspace.php";



function goNextPage(actualurl) {
	
	var doclimit = document.getElementById('wksp_docs_limit').value;
	var gothispage = document.getElementById('gothispage').value;	
	var currentpage = document.getElementById('s_current_page').value;	
	var allpages = document.getElementById('s_pages').value;
	var selpage = parseInt(gothispage)+1; 
	var pstart = 1; 
	if(parseInt(gothispage) != parseInt(currentpage)) {
		selpage = parseInt(gothispage);
	}
	if(selpage <= allpages) {
		pstart = (selpage*parseInt(doclimit))+1;	
		window.location.href=actualurl+pstart;
		}

}

function goPreviousPage(actualurl) {
	
	var doclimit = document.getElementById('wksp_docs_limit').value;
	var gothispage = document.getElementById('gothispage').value;	
	var currentpage = document.getElementById('s_current_page').value;	
	var allpages = document.getElementById('s_pages').value;
	var selpage = parseInt(gothispage)-1; 
	var pstart = 1; 
	if(parseInt(gothispage) != parseInt(currentpage)) {
		selpage = parseInt(gothispage);
	}
	if(selpage >= 1) {
		pstart = (selpage*parseInt(doclimit))+1;	
		window.location.href=actualurl+pstart;
		}

}


function mH(id) {
	
	var m1 = document.getElementById('hm_'+id);
	var m2 = document.getElementById('hml_'+id);
	var cls = m1.className;
	var cls = cls.slice(cls.lastIndexOf('_')+1,cls.length);
	if(cls) {
		if(cls == "on") 
			{
			m1.className ="h_menu_off";
			m2.className ="h_menu_off";
			}
			else {
			m1.className="h_menu_on";				
			m2.className="h_menu_on";				
			}
	}//if tsect
}


//Check selected documents in workspace
function copy_into_new_document() {
var docs;
var fld;
var sdocs;
sdocs = "";
var table;
var dv;
//Prevent clicking more than once
var selcptr;
selcptr = 0;
docs = document.getElementById('wksp_docs_number').value;
var idd;
for (i=1;i<=docs;i++) {
		idd = 'wksp_docs_'+i;
		if(document.getElementById(idd).checked==true) {
			dv = document.getElementById(idd).value;
			sdocs = sdocs+dv+'@';
			selcptr++;
		}//if document
	} // for
		if(sdocs != "") {
			if(selcptr == 1) { 
				table = document.getElementById('wksp_table_reference').value;
				//strip ending @
				sdocs = sdocs.substring(0, (sdocs.length)-1);
				wsendto = '/intranet/tools/copy_into_new_document.php?table='+table+'&id='+sdocs;
				window.location.href = wsendto;
			} 
			else 
			{
				get_error_message('too_many_docs_selected');
			} // selcptr == 1		
		}//if sdocs
		else
		{
		get_error_message('no_docs_selected');
		}
} // function

//Check selected documents in workspace
function delete_selected_docs() {
var docs;
var fld;
var sdocs;
sdocs = "";
var table;
var dv;
docs = document.getElementById('wksp_docs_number').value;
var idd;
for (i=1;i<=docs;i++) {
		idd = 'wksp_docs_'+i;
		if(document.getElementById(idd).checked==true) {
			dv = document.getElementById(idd).value;
			sdocs = sdocs+dv+'@';
		}//if document
	} // for
		if(sdocs != "") {
				table = document.getElementById('wksp_table_reference').value;
				//strip ending @
				sdocs = sdocs.substring(0, (sdocs.length)-1);
				wsendto = '/intranet/tools/delete_selected_documents.php?table='+table+'&id='+sdocs;
				if(confirm(showMessages('confirm_deletion'))) { 
						window.location.href = wsendto;
						} 
						else 
						{
						alert(showMessages('operation_cancelled'));
						 }//if confirm
		}//if sdocs
		else
		{
		get_error_message('no_docs_selected');
		}
} // function

function access_show_restricted(fieldvalue) {
	
		return true;	
	
}

function doc_is_being_edited() {
	
		document.getElementById('has_document_being_edited').value == 1;	
	
}


function get_error_message(message) {


	var lang;
	//if(document.getElementById('wksp_language')) {
	//lang = document.getElementById('wksp_language').value;
	//} else {
	//lang = document.getElementById('s_user_language').value;
	//}
	message = message+'_en';
	switch(message)
    {
	case "user_has_already_clicked_en" :
        alert('Operation already in progress and should resume in a few seconds.');
        break;			
	case "user_has_already_clicked_fr" :
        alert('Opération en cours. Veuillez patienter quelques instants.');
        break;		
	case "file_not_saved_yet_fr" :
        alert('Vous devez enregistrer ce document avant de visualiser le fichier.');
        break;	
	case "file_not_saved_yet_en" :
        alert('You must save your document before viewing this file.');
        break;	
    case "too_many_docs_selected_fr" :
        alert('Vous ne pouvez sélectionner qu\'un document pour cette opération.');
        break;
    case "too_many_docs_selected_en" :
        alert('This operation requires you to select only 1 document.');
        break;
	case "no_docs_selected_fr" :
        alert('Vous devez sélectionner au moins un document pour cette opération.');
        break;
    case "no_docs_selected_en" :
        alert('This operation requires you to select at least 1 document.');
        break;
	case "user_not_authorized_en" :
        alert('You are not authorized to access the page you requested.');
        break;		
	case "user_not_authorized_fr" :
        alert('Vous n\'êtes pas autorisé à accéder à la page demandée.');
        break;
	case "not_available_en" :
        alert('Sorry, this section is not available at this time.\nThank for your visit.');
        break;			
	case "not_available_fr" :
        alert('Désolé, cette section est en développement.\nMerci de votre patience.');
        break;		
		
    } //switch
} //function


function showMessages(message) {
	
	var lang;
	if(document.getElementById('wksp_language')) {
	lang = document.getElementById('wksp_language').value;
	} else {
	lang = document.getElementById('s_user_language').value;
	}
	message = message+'_'+lang;
	switch(message)
    {
    case "confirm_deletion_en" :
        return 'Selected documents will be deleted. Do you wish to continue.';
        break;
	case "confirm_document_not_saved_yet_en" :
        return 'This document has not been saved. Do you wish to continue.';
        break;
	case "confirm_document_not_saved_yet_fr" :
        return 'Ce document n\'a pas encore été enregistré.\n Souhaitez-vous continuer ?';
        break;
	case "confirm_deletion_fr" :
        return 'Les documents sélectionnés vont être supprimés. Souhaitez-vous continuer?';
        break;
	case "operation_cancelled_en" :
        return 'Requested operation has been cancelled.';
        break;
	case "operation_cancelled_fr" :
        return 'L\'opération a été annulée.';
        break;	
    } //switch
} //function

function replace_file(status,shownstatus) {
		var docid = document.getElementById('idpermanent').value;	
		var nurl = "/files/replace_file.php?id="+docid;
		window.location.href = nurl; 
	}

function replace_private_file(status,shownstatus) {
		var docid = document.getElementById('idpermanent').value;	
		var nurl = "/private/replace_private_file.php?id="+docid;
		window.location.href = nurl; 
	}

function replace_user_file(status,shownstatus) {
		var docid = document.getElementById('idpermanent').value;	
		var nurl = "/users/replace_user_file.php?id="+docid;
		window.location.href = nurl; 
	}

function replace_workgroup_file(status,shownstatus) {
		var docid = document.getElementById('idpermanent').value;	
		var nurl = "/workgroups/replace_workgroup_file.php?id="+docid;
		window.location.href = nurl; 
	}
	
function replace_image(status,shownstatus) {
	
		docid = document.getElementById('idpermanent').value;
		window.location.href = "/images/replace_image.php?id="+docid;
	}
	
function Popup(doc)
{
var u = doc;
var w = window.open(u,"","width=800,height=600, toolbar=0, addressbar=1, directories=1, location=1, menubar=1, resizable=1,scrollbars=1,status=0,left=1,top=1");
w.window.focus();
}

function PopFile(doc)
{
var u = doc;
var w = window.open(u,"","width=600,height=450, toolbar=0, addressbar=0, directories=0, location=0, menubar=0, resizable=0,scrollbars=0,status=0,left=200,top=200");
w.window.focus();
}

function PopAudio(doc)
{
var u = doc;
var w = window.open(u,"","width=300,height=200, toolbar=0, addressbar=1, directories=1, location=1, menubar=1, resizable=1,scrollbars=1,status=0,left=1,top=1");
w.window.focus();
}

function close_window(v1,v2)
{
window.close();
}

// Saving and editing document
function processDocument(statusnum,statustxt) {

	 var clicks = document.getElementById('user_has_clicked').value;
	 if(clicks == '0') {
	 document.getElementById('user_has_clicked').value == 1;	
	 document.forms[0].status.value = statusnum;
	 document.forms[0].shownstatus.value = statustxt;
	 document.forms[0].submit();
	 }
		else {
		return get_error_message('user_has_already_clicked');
	}
	 
}


function cancel(id,status) {
	 var table = document.getElementById('s_table').value;
	 if(table) {
	 window.location.href = returnlocation+'?type='+table;
	 } else {
	 window.location.href = returnlocation;
	 }
}

function document_cancel(id,status) {
	 var return_page = document.getElementById('s_return_page').value;
	 var table = document.getElementById('s_table').value;
	 var s_wks_url = "";
	 if(document.getElementById('s_last_wks_url')) {
	 	var s_wks_url = document.getElementById('s_last_wks_url').value;
	 	}

	if(return_page == '' || return_page == null) {
	var r_page = workspace;
	} else {
	var r_page = intranet+return_page;		
	}
	if(table) nurl = r_page+'?type='+table;
	//if(s_wks_url) nurl = return_page+'?'+s_wks_url;
		
	if(confirm(showMessages('confirm_document_not_saved_yet'))) 
					{ 
					window.location.href = nurl;
					}
}

function public_cancel(id,status) {
	 var return_page = document.getElementById('s_return_page').value;
	 var table = document.getElementById('s_table').value;
	 var s_wks_url = "";
	 if(document.getElementById('s_last_wks_url')) {
	 	var s_wks_url = document.getElementById('s_last_wks_url').value;
	 	}

	if(return_page == '' || return_page == null) {
	var r_page = '/';
	} else {
	var r_page = return_page;		
	}
	nurl = return_page;
		
	if(confirm(showMessages('confirm_document_not_saved_yet'))) 
					{ 
					window.location.href = nurl;
					}
}

function goSelectedPage(url) {
	if(url) {
	 window.location.href = url;
	}
}



function showSection(id) {
	
	var tsect = document.getElementById(id);
	var msect = document.getElementById('lpm_'+id);
	if(tsect) {
		if(tsect.className == "on") 
			{
			tsect.className ="off";
			msect.className ="left-pane-section_off";
			}
			else {
			tsect.className="on";				
			msect.className ="left-pane-section_on";
			}
	}//if tsect
}

function mh1(id, etat) {
	
	var cemh = document.getElementById('mh1_' + id);
	var cls = cemh.className;
	var cls = cls.slice(cls.lastIndexOf("_") + 1, (cls.length));
	if(cls != 'on') {
	cemh.className = 'mh1_' + etat;
	}
}

function mh2(id, etat) {
	
	var cemh = document.getElementById(id);
	var cls = cemh.className;
	var cls = cls.slice(cls.lastIndexOf("_") + 1, (cls.length));
	if(cls != 'on') {
	cemh.className = 'mh_ss_' + etat;
	}
}



/* IMAGES */
function show_selected_image(img) {
if (img.length > 5) {
document.images.doc_icon.src = img;
document.images.doc_icon.visibility = "visible";
}
else {
document.images.doc_icon.src = '/ressources/e.gif';	
document.images.doc_icon.visibility = "hidden";

return(null);
}
}

function report_image(status,shownstatus) {
		var thisUrl = document.getElementById('image_id').value;
		if(window.opener) {
			var txtUrl = window.opener.document.getElementById('txtUrl');
			txtUrl.value = thisUrl;
			window.opener.UpdatePreview();
		}
		window.close();
}

function selected_image(id,format) {
	if(!format) {format = 'large';}
	if(id) {
	window.parent.document.getElementById('txtUrl').value= '/images/show_image.php?id='+id+'&format='+format;
	window.parent.UpdatePreview();
	}
}
function close_and_report_image(status,shownstatus) {
	
	var id = document.getElementById('idpermanent').value;
	var h = document.getElementById('medium_height').value;
	var w = document.getElementById('medium_width').value;
	
	if(id) {
	var oimg = window.opener.document.getElementById('pimg')
	oimg.src= '/images/show_image.php?id='+id+'&format=medium';
	if(h) oimg.style.height = h;
	if(w) oimg.style.width = w;
	var oimg_f = window.opener.document.getElementById('hidden_image');
	oimg_f.value = id;
	//buttons
	var ch_b = document.getElementById('bt_change_image');
	ch_b.className = 'on';
	var add_b = document.getElementById('bt_add_image');
	add_b.className = 'off';
	window.close();
	}
}
function selected_link(url) {
	if(url) {
	window.parent.document.getElementById('txtUrl').value= url;
	}
}	

function pop_selected_link(title,url,filetype) {
	if(url) {
	window.parent.document.getElementById('txtUrl').value= title;
	var docinfo = 'title@'+title+'%url@'+url+'%filetype@'+filetype;
	window.parent.document.getElementById('linkInfo').value= docinfo;
	}
}	

function pop_selected_link_ok() {
	var ws = window.location.href;
	var field_1 = ws.slice(ws.indexOf('field_1=') +8,ws.indexOf('&field_2='));
	var field_2 = ws.slice(ws.indexOf('field_2=') +8,(ws.length));
	var doctitle = document.getElementById('txtUrl').value;
	if(field_1.length > 1) {
	var f1 = window.opener.document.getElementById(field_1);
	f1.value = doctitle;
	}
	if(field_2.length > 2) {
	var f2 = window.opener.document.getElementById(field_2);
	var docinfo = document.getElementById('linkInfo').value;
	f2.value= docinfo;
	}
	window.close();
}	

function pop_add_image() {
	var uA = navigator.userAgent;
	var host = window.location.host;
	var gurl = "http://"+host+"/images/pop_add_image.php";
	var ieurl = "http://"+host+"/images/ie_pop_add_image.php";

	if(uA.indexOf('Gecko') > 0) {
		var gurl = "http://"+host+"/images/pop_add_image.php";
		window.parent.location.href = gurl;
	} else {
		PopIEImage(ieurl);
	}
}

function PopIEImage(doc)
{
var u = doc;
var w = window.open(u,"","width=650,height=500, toolbar=0, addressbar=0, directories=0, location=0, menubar=0, resizable=1,scrollbars=1,status=0,left=1,top=1");
w.window.focus();
}


function pub_add_image() {
	
	var u = '/images/pub_add_image.php';
	var w = window.open(u,"","width=500,height=400, toolbar=0, addressbar=0, directories=0, location=0, menubar=0, resizable=1,scrollbars=0,status=0,left=150,top=150");
	w.window.focus();
}

function pub_change_image() {

	var dimg = document.getElementById('pimg');
	dimg.src= '/e.gif';
	dimg.style.height = 24;
	dimg.style.width = 24;
	var u = '/images/pub_add_image.php';
	var w = window.open(u,"","width=500,height=400, toolbar=0, addressbar=0, directories=0, location=0, menubar=0, resizable=1,scrollbars=0,status=0,left=150,top=150");
	w.window.focus();
}

function pop_add_link() {
	var uA = navigator.userAgent;
	var nurl = '/files/pop_add_file.php';
	if(uA.indexOf('Gecko') > 0) {
		window.location.href = nurl;
		} else {
		alert('Not available yet for IExplorer navigators. We\'re trying to fix the bug.');
	}
}


function goIndexMenu(table) {
	if(table) {
	var nurl = '/links/create_link.php?table='+table;
	window.location.href = nurl;
	}
}

function goSearchIndex() {
	var query = document.getElementById('searchIndex').value;
	var wurl = window.location.href;
	var pquery = '?';
	if(window.location.search) {
		pquery = '';	
	}
	if(query) {
	var nurl = wurl+pquery+'&search='+query;
	window.location.href = nurl;
	}
}



function goPopSearchIndex() {
	var query = document.getElementById('searchIndex').value;
	var wurl = window.location.href;
	var pquery = '?';
	if(window.location.search) {
		pquery = '';	
	}
	if(query) {
	var nurl = wurl+pquery+'&search='+query;
	window.location.href = nurl;
	}
}


function goSearchImageIndex() {
	var query = document.getElementById('searchIndex').value;
	var wurl = window.location.href;
	if(window.location.search) {
		wurl = wurl.substring(0,wurl.indexOf('?'));	
	}
	if(query) {
	var nurl = wurl+'?search='+query;
	window.location.href = nurl;
	}
}

function goSearchWorkspace(table) {
	var query = document.getElementById('wks_search').value;
	var wurl = window.location.href;
	if(query != "") {
		if(window.location.search) {
			wurl = wurl.substring(0,wurl.indexOf('?'));	
		}
		var nurl = wurl+'?type='+table+'&search='+replaceAccents(query);
		window.location.href = nurl;
	}//if query
}

function clearSearchWorkspace(table) {
	var wurl = window.location.href;
		if(window.location.search) {
			wurl = wurl.substring(0,wurl.indexOf('?'));	
		}
		var nurl = wurl+'?type='+table;
		window.location.href = nurl;
}

function goSearchAddressBook(table) {
	var query = document.getElementById('wks_search').value;
	var wurl = window.location.href;
	if(query != "") {
		if(window.location.search) {
			wurl = wurl.substring(0,wurl.indexOf('?'));	
		}
		var nurl = '/intranet/addressbook.php?search=search_index&query='+replaceAccents(query);
		window.location.href = nurl;
	}//if query
}

function clearSearchAddressBook(table) {
	var wurl = window.location.href;
		if(window.location.search) {
			wurl = wurl.substring(0,wurl.indexOf('?'));	
		}
		var nurl = "/intranet/addressbook.php";
		window.location.href = nurl;
}

function goPopIndexMenu(table) {
	if(table) {
	var nurl = '/links/create_link_pop.php?table='+table;
	window.location.href = nurl;
	}
}

function PopLinkIndex(field_1,field_2) {
		var url = '/links/pop_create_link.php?field_1='+field_1+'&field_2='+field_2;
		PopFile(url);
}

function cleanLinkInfo(fieldvalue,sfield) {
		if(fieldvalue == "") {
			document.getElementById(sfield).value = "";	
		}
}

function cleanLinkInfoField(field_1,field_2) {
		document.getElementById(field_1).value = "";	
		document.getElementById(field_2).value = "";	
}


//Accents
function replaceAccents(c) {

while(c.indexOf(" ") != -1) c = c.replace(" ","+");

while(c.indexOf("à") != -1) c = c.replace("à","a");
while(c.indexOf("â") != -1) c = c.replace("â","a");
while(c.indexOf("ä") != -1) c = c.replace("ä","a");
while(c.indexOf("À") != -1) c = c.replace("À","A");
while(c.indexOf("Â") != -1) c = c.replace("Â","A");
while(c.indexOf("Ä") != -1) c = c.replace("Ä","A");

while(c.indexOf("ç") != -1) c = c.replace("ç","c");
while(c.indexOf("Ç") != -1) c = c.replace("Ç","c");

while(c.indexOf("é") != -1) c = c.replace("é","e");
while(c.indexOf("è") != -1) c = c.replace("è","e");
while(c.indexOf("ê") != -1) c = c.replace("ê","e");
while(c.indexOf("ë") != -1) c = c.replace("ë","e");
while(c.indexOf("É") != -1) c = c.replace("É","E");
while(c.indexOf("Ê") != -1) c = c.replace("Ê","E");


while(c.indexOf("Ë") != -1) c = c.replace("Ë","E");
while(c.indexOf("È") != -1) c = c.replace("È","E");

while(c.indexOf("î") != -1) c = c.replace("î","i");
while(c.indexOf("ï") != -1) c = c.replace("ï","i");
while(c.indexOf("Î") != -1) c = c.replace("Î","i");
while(c.indexOf("Ï") != -1) c = c.replace("Ï","i");

while(c.indexOf("ô") != -1) c = c.replace("ô","o");
while(c.indexOf("ö") != -1) c = c.replace("ö","o");
while(c.indexOf("Ô") != -1) c = c.replace("Ô","O");
while(c.indexOf("Ö") != -1) c = c.replace("Ö","O");

while(c.indexOf("ù") != -1) c = c.replace("ù","u");
while(c.indexOf("ü") != -1) c = c.replace("ü","u");
while(c.indexOf("û") != -1) c = c.replace("û","u");
while(c.indexOf("Ù") != -1) c = c.replace("Ù","U");
while(c.indexOf("Ü") != -1) c = c.replace("Ü","U");
while(c.indexOf("Û") != -1) c = c.replace("Û","U");

return c;
}
