//common to all pages
var reloading=false
function keyvalue(el){
	if (el){
		var unicode=el.keyCode? el.keyCode : el.charCode
		return unicode
	}
	return 0
}
function wait_signal_on(){
	reloading=true
	e("img_ajax").style.display = "block";
	w=f_scrollTop()+85
	e("img_ajax").style.top=w+"px"
}
function wait_signal_off(){
	e("img_ajax").style.display = "none";
	reloading=false
}
function e(id){
	return document.getElementById(id)
}
function el(id){
	return document.getElementById(id)
}
function v(id){
	if (! e(id)){
		alert("undefined tag: "+id)
		return ""
	}
	return e(id).value
}

function f_scrollTop() {
    return f_filterResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	)
}
function f_filterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
if (L=="it"){
	caption_close_panel="chiudi questo pannello"
	caption_wait_load="Prego attendere il carimento della prossima pagina..."
} else {
	caption_close_panel="close this panel"
	caption_wait_load="Please wait while the next page is being loaded..."
}
function edit_page(){
	var edit_icon_span = document.getElementsByClassName("hasTip")
	var edit_icon_href
	if (edit_icon_span){
			w= edit_icon_span[0].innerHTML
			if (w.indexOf("edit.png")>0){
				var ael=edit_icon_span[0].getElementsByTagName("a")
				if (ael){
					edit_icon_href=ael[0].href
				}
			}
	}
	wait_signal_on()
	window.location =edit_icon_href
}
function flag_clicked(img){
  if (img.title=="Français"){
    //alert ("L'interface du site en Français n'est pas encore disponible. Nou vous prions d'essayer de nouveau dans quelques semaines.")
    return
  }
  if (img.title=="Deutsch"){
    //alert ("Leider ist die Seite in Deutsch noch nicht verfügbar. Bitte versuchen Sie es in einigen Wochen wieder.")
    return
  }
  if (img.title=="Español"){
    //alert ("Sorry, the site iterface in Spanish is not yet available. Please try again in a few weeks.")
    return
  }
}

function flag_clicked_partial(lang_code){
  if (lang_code=="fr"){
    //alert ("L'interface du site en Français n'est pas encore disponible. Nou vous prions d'essayer de nouveau dans quelques semaines.")
	//alert ("The site iterface in french is under construction. Some text is still in English. Corrections and help for further translations welcome.")
    return
  }
  if (lang_code=="de"){
    //alert ("Leider ist die Seite in Deutsch noch nicht verfügbar. Bitte versuchen Sie es in einigen Wochen wieder.")
	//alert ("The site iterface in German is under construction. Some text is still in English. Corrections and help for further translations welcome.")
    return
  }
  if (lang_code=="es"){
    //alert ("The site iterface in Spanish is under construction. Some text is still in English. Corrections and help for further translations welcome.")
    return
  }
}

function common_unload_routine(){
    msg("caption_wait_load");
}


function hide_help(){
	popups=e('div_popups')
	popups.innerHTML=""
	popups.style.display="none"
	previous_content=""
	if (e('div_quick_search')) e('div_quick_search').style.display="block";	
}

var previous_content=""

function msg(content, top, large){
	w1="500";w2="270";w3="250"
	if (large){w1="898";w2="457";w3="448"}
	if (!top){top=115}
	popups=e('div_popups')
	popups.className="popups_ext"
	if (content === previous_content){
		popups.innerHTML=""
		popups.style.display="none"
		previous_content=""
		return
	}
	previous_content=content	
	var w="<div style='position:absolute; top:"+top+"px;width:"+w1+"px;left:-"+w2+"px;background-color:white; text-align:center; padding:20px; border: 3px solid silver; z-index:999999' align='center'>"	
    w+="<div style='position:relative;display:inline; vertical-align:top;'>&nbsp;"
	w+="<div style='position:absolute;top:-15px;left:"+w3+"px;cursor:pointer'><img src='/app/images/close_icon.gif' onclick='hide_help()'/>"
	w+="</div></div>"
	w+="<div style='text-align:left;margin-top:-10px '>"
	w+=content
	w+="</div>"
	w+="<div style='text-align:center; margin-top:15px;font-size:10px'><a href='javascript:hide_help()' >"+caption_close_panel+"</a></div>"
	w+="</div>"
	popups.innerHTML=w
	popups.style.display="block"
}


function msg_old(content, top, large){
	w1="500";w2="270";w3="250"
	if (large){w1="898";w2="469";w3="448"}
	if (!top){top=115}
	popups=e('div_popups')
	popups.className="popups_ext"
	if (content === previous_content){
		popups.innerHTML=""
		popups.style.display="none"
		previous_content=""
		return
	}
	previous_content=content	
	var w="<div style='position:absolute; top:"+top+"px;width:"+w1+"px;left:-"+w2+"px;background-color:white; text-align:center; padding:20px; border: 3px solid silver; z-index:999999' align='center'>"	
    w+="<div style='position:relative;display:inline; vertical-align:top;'>&nbsp;"
	w+="<div style='position:absolute;top:-15px;left:"+w3+"px;cursor:pointer'><img src='app/images/close_icon.gif' onclick='hide_help()'/>"
	w+="</div></div>"
	w+="<div style='text-align:left;margin-top:-10px '>"
	w+=content
	w+="</div>"
	w+="<div style='text-align:center; margin-top:15px;font-size:10px'><a href='javascript:hide_help()' >"+caption_close_panel+"</a></div>"
	w+="</div>"
	popups.innerHTML=w
	popups.style.display="block"
}

function addnew(){
	
	if (! edit_allowed()){return}
	if(typeof open_edit_box_for_add_record == 'function' && tinymce_initialized ){
		open_edit_box_for_add_record()
		return
	} 
	wait_signal_on();
	if (L=="it"){ 
		window.location ="/it?addnew=1";
	} else {
		window.location ="/?addnew=1";
	}
	
}
function unavailable(){
	if (L=="it"){
		alert("Funzione non disponibile in corso di sviluppo.")
	} else {
		alert("Function unavailable, under development.")
	}
}
function change_lang(href){
	wait_signal_on()
	window.location=href
}

function on_tb_find_change(el){

	if (keyvalue(el)==27){  //esc
		if(typeof close_welcome == 'function'  ){
			close_welcome()
		}
	}
	if (keyvalue(el)==13 || keyvalue(el)==32){
		w=trim(e('tb_find_filter').value)
		if (w !=""){
			load_and_find()	
		}else {
			if(typeof getquote == 'function'  ){
				getquote()
				e('tb_find_filter').value=""
			}
		}
	}
}	

function load_and_find(){
	
	var pt="&pt=" // page title
	if (L=='it'){
		loca="/it"
	}else{
		loca="/"
	}
	find_filter=e('tb_find_filter')
	if (find_filter.value!=""){
		loca+="?find="+encodeURI(find_filter.value)
		//loca+="?find="+find_filter.value //already encoded
		//pt+="*"+encodeURI(find_filter.value)+"*"
		//loca=loca+pt
		loca=loca
	} else {
		//loca+="?sort=id&order=DESC"
	}
	wait_signal_on();
	document.location.href=loca
}


function open_welcome_panel(){
	if(typeof open_welcome == 'function'  ){
			open_welcome()
			e("tb_find_filter").focus()
	} else {
//		window.location = window.location + "?&welcome=1"
		var w=""+window.location
		wait_signal_on();
		if (w.indexOf("?")<0){
			window.location = window.location + "?welcome=1"
		} else {
			window.location = window.location + "&welcome=1"
		}
	}
}
var nr_blinks=0
var chat_advise_to_be_deleted=false
function chat_check(){
	if (chat_advise_to_be_deleted){
			e('chat_info').style.display="none";
			return
	}
	if ( typeof( window[ 'admin_in_chat' ] ) == "undefined" ) {
//		e('chat_info').style.display="none";
		return
	}
	//alert(typeof( window[ 'admin_in_chat' ] ))
	e('chat_info').innerHTML=""
	if (admin_in_chat && user!='admin' && uri!='/chat' && uri.indexOf('/staff')< 0){
		e('chat_info').innerHTML="An assistant is in <a href='/chat'>chat</a> for any questions or comments. "
		
	}
	if (someone_in_chat && uri!='/chat' && (user=='admin' || user=='demo')){
		e('chat_info').innerHTML="Someone is in <a href='/chat'>chat</a>."
	}
	if (e('chat_info').innerHTML!=""){
			e('chat_info').style.display="block";
	}
		
	nr_blinks++
	if ((parseFloat(nr_blinks/2) == parseInt(nr_blinks/2)) && nr_blinks<50 ) {
		e('chat_info').innerHTML="";
		setTimeout ('chat_check()',500)	
		return
	}
	if (nr_blinks<10){ //set to >51 to stop it
		setTimeout ('chat_check()',500)	
	}else{
		//chat_advise_to_be_deleted=true
		//setTimeout ('chat_check()',10000)	
	}
}

function hide(id){
	e(id).style.display="none"
}
function open_bulk_panel(found_or_checked){
	if (grid_ok()){open_bulk_panel2(found_or_checked)}
}
function delete_found_records(){
	if (grid_ok()){delete_found_records2()}
}
function print_found_records(){
	if (grid_ok()){window.open("print")}
}

function export_found_records(){
	if (grid_ok()){window.open("export");}
}

function print_checked_records(){
	if (grid_ok()){print_checked_records2()}
}

function dedicated_page_checked_records(){
	if (grid_ok()){dedicated_page_checked_records2()}
}

function export_checked_records(){
	if (grid_ok()){export_checked_records2()}
}
function delete_checked_records(){
	if (grid_ok()){delete_checked_records2()}
}
function grid_ok(){
	if (! e('div_grid')){
		alert("Function available only on record pages.")
		return false
	}
	return true
}
function edit_allowed(){
	if (demouser){
		if (L=="it"){
			alert("Operazione non consentita per l'utente \"demo\". Registrati, fai login (\"accesso\") e riprova.")
		} else {
			alert("Operation not allowed for \"demo\" user. Please register, login and retry.")
		}
		return false
	} else {
		return true
	}
}
var row_previously_edited=0
function close_edit_panel(){
	//nolight_row (row_being_edited)
	show_navi_buttons()
	row_previously_edited=row_being_edited
	set_scroll(click_scroll_top)
	e('edit_box').style.display="none"
	if (NR==0){
		e("tab_3_button").style.display=""
	}
	if(typeof check_overflows == 'function') { 
		check_overflows(); 
	} 
}
function change_sec_vis(){
	wait_signal_on();
	if (L=="it"){
			document.location.href="/it-settings"	
		} else {
			document.location.href="/settings"	
		}
}
function get_session_cookie(Name) {
	//Example of setting session cookie: document.cookie="bgcolor=#233333"
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		// if cookie exists
		if (offset != -1) {
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}
function ch_cryp_pw2(){
	chg_cryp_pw()
}
function chg_cryp_pw(){
	w1 = prompt("Enter password for encryption\n(will not be sent to the server or written anywhere)","");
	if (! w1) {
	  if (confirm("Do you want to clear the password?")){
		crypt_pw=""
		document.cookie="crypt_pw="
	  }
	  return false
	}
	w2 = prompt("Enter password again","");
	if (! w2) {
	  alert("You canceled");
	  return false
	}
	if (w1 != w2) {
	  alert("The two passwords are different.");
	  return false
	}
	crypt_pw=w1
	document.cookie="crypt_pw="+crypt_pw
	return true
}
function open_edit_box_for_add_record(){
	if (e('edit_box')) {
		if (!get_url_param("author") && window.location.href.indexOf("subject_")<0){
			open_edit_box_for_add_record2()
			return
		 }
	}
	if (user=="admin"){
		wait_signal_on()
		document.location.href = "/?addnew"
	} else {
		alert("Add-record not allowed for unregistered users.")
	}
	
}

function setfocus_after_load(){
	if (! e('edit_box')){
		setTimeout('e("tb_find_filter").focus()',1)
	}
}
function trim(str) {
      return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function set_language(obj){
	quote_language= obj.options[obj.selectedIndex].value
	if(typeof reload == 'function'  ){
		reload("lang="+quote_language+"&savelang")
	} else {
		currenturl=document.location.href
		qmarkpos=currenturl.indexOf("?") 
		url_clean = currenturl.substr(0,qmarkpos) 
		wait_signal_on()
		
		if (homepage){  //defined in patch_10_after_head_start.php
			document.location=url_clean+"?home&lang="+quote_language+"&savelang"
		}else{
			document.location=url_clean+"?lang="+quote_language+"&savelang"
		}
	}
}
function select_by_refkey(refkey){
	wait_signal_on()
	window.open("?refkey="+refkey)
}
function get_url_param(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

