
// Running locally set url to your local ip
//var url = 'http://192.168.1.118:8988/LIF/servlet';

var url = '/LIF/servlet';

// Global variables for clickable word function
var mouseX = '0';
var mouseY = '0';

// From now on every clickable word on a page share one div tagg. This tag is named here. It must match the static div tag 
// found in the jsp page where a clickable word document is presented.
var div_id = "clickableword";
 if (document.layers) document.captureEvents(Event.MOUSEMOVE);
      document.onmousemove=mtrack;     

function killWindow(notUsed)
{
  getElementById_s(div_id).style.display = 'none' ;
}

function showClickDescription(word, lang) {
    
     if (getElementById_s(div_id).style.display == 'none')
     {
          getWordDescription(word, lang, null);
          getElementById_s(div_id).style.display = 'inline';          
          // Fix possible floater page overflow situations
          mouseX = noPageOverflow(mouseX, 270);       
          getElementById_s(div_id).style.top = mouseY+'px';
          getElementById_s(div_id).style.left = mouseX+'px';
     }
     else{
      // Put a short delay here!
          setTimeout("killWindow('"+div_id+"')",2000);                  
     }
}

/*
    The constant should be the left hand position of the floater when the right hand side is positioned at the end of the text column.    
*/
function noPageOverflow(i_mouseX, constant)
{
          var mouseX_no_overflow = i_mouseX;
          
          if (i_mouseX>constant)
          {
              mouseX_no_overflow = i_mouseX - (i_mouseX - constant);
          }
          return mouseX_no_overflow;
}
function createWindowRS(rsword) 
{
    settings=
    "toolbar=no,location=no,directories=no,"+
    "status=no,menubar=no,scrollbars=no,"+
    "resizable=no,width=300,height=160,";

    if ((parseInt(navigator.appVersion)>3) && (navigator.appName=="Netscape") || 
       (navigator.appName.indexOf("Microsoft")!=-1) && (parseInt(navigator.appVersion)>=4) ||
       (navigator.appVersion.indexOf("MSIE") != -1) && (parseInt(navigator.appVersion))>=4) 
    {     
           settings = settings + "screenX=" + (screen.width/2+100) + ",screenY=200,left=" + ((screen.width/2)+100) + ",top=200";
    }
    else
    {
        settings = settings + "screenX=0,screenY=0,left=0,top=0";   
    }    
   window.open('../home/lexRS.jsp?readWord='+rsword, 'lexRS', settings);
   
}
function getElementById_s(id){
	var obj = null;
	if(document.getElementById){
		/* Prefer the widely supported W3C DOM method, if
		available:-
		*/
		obj = document.getElementById(id);
	}else if(document.all){
		/* Branch to use document.all on document.all only
		browsers. Requires that IDs are unique to the page
		and do not coincide with NAME attributes on other
		elements:-
		*/
		obj = document.all[id];
	}
	/* If no appropriate element retrieval mechanism exists on
	this browser this function always returns null:-
	*/
	return obj;
}

function popupwindow(id, lang)
{

  var testwindow = window.open ("http://www.fass.se/LIF/produktfakta/cw-popup.jsp?word="+escape(encodeUTF8(id))+"&lang="+lang, "FASS",  "width=450,height=300,scrollbars=no,status=no,toolbar=no,resizable=yes,location=no,menubar=no,directories=no");
  testwindow.moveTo(80,80);
}

function ReplaceChar(String, OldChar, NewChar){ 
  for(n=0; n<String.length; n++){ 
  if (String.charAt(n) == OldChar) String = String.substring(0,n) + NewChar + String.substring(n+1,String.length); 
  } 
  return String; 
}
function encodeUTF8(string){
 	string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
 }

function mtrack(e) 
{
      	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		mouseX = e.pageX;
		mouseY = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
        try{
            mouseX = e.clientX + document.body.scrollLeft
			    + document.documentElement.scrollLeft;
		    mouseY = e.clientY + document.body.scrollTop
			    + document.documentElement.scrollTop;
        } catch (err) {}
    }
}

function getWordDescription(clickedWord, lang, windowSize)
{ 
  var req = null; 
  if(window.XMLHttpRequest){  
    req = new XMLHttpRequest(); 
  }
  else
  { 
    if (window.ActiveXObject){  
      req = new ActiveXObject('Microsoft.XMLHTTP'); 
      }
  }
  
  req.onreadystatechange = function()
  { 
    if(req.readyState == 4)    
    {
        if(req.status == 200){             
             document.getElementById(div_id).innerHTML = req.responseText;
             window.status = "";
             if (windowSize=='print')
             {
              window.print();
              window.close();
             }
       }else {
          window.status ="Error code " + req.status;
      }
    }else{      
           document.getElementById(div_id).innerHTML = "<p>H"+unescape("%E4")+"mtar ordbeskrivningen...</p>";
    }
  }; 

  var cw_url = "";
  if (navigator.userAgent.indexOf("Firefox")!=-1) 
  { 
	  if (windowSize==null)
	    cw_url = url + '/CWService?word='+clickedWord+'&lang='+lang;
	  else
	    cw_url = url + '/CWService?word='+clickedWord+'&lang='+lang+'&windowSize='+windowSize;
	}else{
	  if (windowSize==null)
	    cw_url = url + '/CWService?word='+encodeUTF8(clickedWord.toLowerCase())+'&lang='+lang;
	  else
	    cw_url = url + '/CWService?word='+encodeUTF8(clickedWord.toLowerCase())+'&lang='+lang+'&windowSize='+windowSize;
	}    

  req.open( 'GET', cw_url, true);
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");    
  req.send(clickedWord);  
} 

