function get_sel(where){
	var txt = "";

	if(window.getSelection){
		txt = window.getSelection();
	}
	else{
		if(document.getSelection){
			txt = document.getSelection();
		}
		else{
			if(document.selection){
				txt = document.selection.createRange().text;
			}
		}
	}

	if(txt == ""){
		var el=document.getElementById("inlagg" + where);
		if(el.selectionStart){
			txt = el.value.substr(el.selectionStart, el.selectionEnd - el.selectionStart);
		}
	}
	if(txt == ""){

		return false;
	}
	else{
		return txt;
	}
}


function skapa_tag(start,slut,where){

	

	var insert;
	var url;
	var nytext;
	
	try{

		if(get_sel(where)) {
			insert = get_sel(where);
			if(insert == null){
				return false;
			}

			if(start == "[url="){
				url = prompt("URL:","http://");
				if(url.substr(0,7) != "http://"){
					url = "http://" + url;
				}
				start = start + url + "]";
			}

			var nytext = start+insert+slut;
			
			if(insert.length>0){
				element = document.getElementById("inlagg" + where);

				if(element.createTextRange){
					element.focus();
					element.createRange().duplicate().text = nytext;

					statusbardiv = document.getElementById("bbmenustatusbar" + where);
					statusbardiv.innerHTML = '<p>Lade till: ' + start + slut + '</p>';
					
					setTimeout ("resetbbmenustatusbar('" + where + "')",2500);
					
				}
				else{

					field = document.getElementById("inlagg" + where);

					startPos = field.selectionStart;
					endPos = field.selectionEnd;
					before = field.value.substr(0, startPos);
					selected = field.value.substr(field.selectionStart, (field.selectionEnd - field.selectionStart));
					after = field.value.substr(field.selectionEnd, (field.value.length - field.selectionEnd));
					field.value = before + start + selected + slut + after;
					
					statusbardiv = document.getElementById("bbmenustatusbar" + where);
					statusbardiv.innerHTML = '<p>Lade till: ' + start + slut + ' kring ' + selected + '</p>';
					
					setTimeout ("resetbbmenustatusbar('" + where + "')",2500);

				}
			}
		}
		else{

		    field = document.getElementById("inlagg" + where)
			
			field.value += start + slut;
			
			statusbardiv = document.getElementById("bbmenustatusbar" + where);
			statusbardiv.innerHTML = '<p>Lade till: ' + start + slut + ' sist i dokumentet</p>';

			setTimeout ("resetbbmenustatusbar('" + where + "')",2500);
		}
	}
	catch(e){

	}
}


function resetbbmenustatusbar(foobar) {

	statusbardiv = document.getElementById("bbmenustatusbar" + foobar);
	statusbardiv.innerHTML = '';

}


function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


	function quickpicsearch(quicksearchvalue,quicksearchtype,picturetypeid) 
		{
		  
		  if (quicksearchtype == 'id' ) {
		    
			/* BY ID */
			if (quicksearchvalue.length >= 5) {
			   if (IsNumeric(quicksearchvalue) == true) {
			     document.getElementById("searchpicbyid" + picturetypeid).className = 'noerrors';
			     sendajax(quicksearchvalue,'/_commondata/ajax/quicksearchpic.asp?submit=true&stype=id&picturetypeid=' + picturetypeid,'searchpicturedisplay')
			   } else {
			     document.getElementById("searchpicbyid" + picturetypeid).className = 'error';
			   }
			} else {
			
			  document.getElementById("searchpicbyid" + picturetypeid).className = 'error';
			  
			}
		  }
		  else
		  {
		
		     /*  BY NAME */
			 
			 sendajax(quicksearchvalue,'/_commondata/ajax/quicksearchpic.asp?submit=true&stype=name&picturetypeid=' + picturetypeid,'searchpicturedisplay')
		
		  }
		
		   
		}

	function quickpicsearchdisplay(pictureid) 
		{
	
			if (pictureid.length >= 5) {
				 
			   if (IsNumeric(pictureid) == true) {
			     sendajax(pictureid,'/_commondata/ajax/quicksearchpicdisplay.asp?submit=true','searchpicturedisplayinsert')
			   }
			}
		}


	function quicksearchinsert(typeofinsert,idfield) 
		{
	
			if (idfield.length >= 4) {
				 
			   if (IsNumeric(idfield) == true) {
			     
				 //picturecomment = document.getElementById("quicksearchpiccomment").value;
				 
			     pictureidstring = '[% ' + typeofinsert + ',' + idfield + ' %]';
				 
				 document.bbeditor.inlagg.value += pictureidstring;
				 statusbardiv = document.getElementById("bbmenustatusbar");
				 
				 statusbardiv.innerHTML = '<p>Lade till: ' + pictureidstring + ' sist i dokumentet</p>';

				 
			   }
			}
		}
		
		
var Preview_Win_Name;
var Work_Form_Name;
var Work_Field_Name;

function PreviewWindow(form_name,field_name) 
{
  
  var textform = self.document[form_name][field_name].value;
  
  if ( ! Preview_Win_Name || Preview_Win_Name.closed || (Work_Form_Name != form_name || Work_Field_Name != field_name))
  {
  Preview_Win_Name = win_pop('');
  if (Preview_Win_Name.focus) { Preview_Win_Name.focus(); }
  
  self.document.hidden_form.form_name.value = form_name; 
  self.document.hidden_form.field_name.value = field_name; 
  self.document.hidden_form.first_time_text.value = textform;
  self.document.hidden_form.submit();
  
  Work_Form_Name=form_name;
  Work_Field_Name=field_name;
  }
  else
  {
  if (Preview_Win_Name.focus) { 

  
  self.document.hidden_form.form_name.value = form_name; 
  self.document.hidden_form.field_name.value = field_name; 
  self.document.hidden_form.first_time_text.value = textform;
  self.document.hidden_form.submit();
  

    Preview_Win_Name.focus(); 
	}
  }

}


function PreviewEmailWindow(form_name,CSSText,HeaderText,FooterText) 
{
  
  var textformCSSText = self.document[form_name][CSSText].value;
  var textformHeaderText = self.document[form_name][HeaderText].value;
  var textformFooterText = self.document[form_name][FooterText].value;
  
  if ( ! Preview_Win_Name || Preview_Win_Name.closed || (Work_Form_Name != form_name || Work_Field_Name != HeaderText))
  {
  Preview_Win_Name = win_pop('');
  if (Preview_Win_Name.focus) { Preview_Win_Name.focus(); }
  
  self.document.hidden_form.form_name.value = form_name; 
  self.document.hidden_form.CSSText.value = textformCSSText; 
  self.document.hidden_form.HeaderText.value = textformHeaderText;
  self.document.hidden_form.FooterText.value = textformFooterText;
  self.document.hidden_form.submit();
  
  Work_Form_Name=form_name;
  Work_Field_Name=field_name;
  }
  else
  {
  if (Preview_Win_Name.focus) { 

  
  self.document.hidden_form.form_name.value = form_name; 
  self.document.hidden_form.field_name.value = field_name; 
  self.document.hidden_form.first_time_text.value = textform;
  self.document.hidden_form.submit();
  

    Preview_Win_Name.focus(); 
	}
  }

}



function PreviewEmail(form_name,SendMailBody,EmailTemplateID) 
{
  
  var textformSendMailBody = self.document[form_name][SendMailBody].value;
  var textformEmailTemplateID = self.document[form_name][EmailTemplateID].value;
  
  if ( ! Preview_Win_Name || Preview_Win_Name.closed || (Work_Form_Name != form_name || Work_Field_Name != SendMailBody))
  {
  Preview_Win_Name = win_pop('');
  if (Preview_Win_Name.focus) { Preview_Win_Name.focus(); }
  
  self.document.hidden_form.form_name.value = form_name; 
  self.document.hidden_form.SendMailBody.value = textformSendMailBody; 
  self.document.hidden_form.EmailTemplateID.value = textformEmailTemplateID;
  self.document.hidden_form.submit();
  
  Work_Form_Name=form_name;
  Work_Field_Name=field_name;
  }
  else
  {
  if (Preview_Win_Name.focus) { 

  
  self.document.hidden_form.form_name.value = form_name; 
  self.document.hidden_form.SendMailBody.value = textformSendMailBody; 
  self.document.hidden_form.EmailTemplateID.value = textformEmailTemplateID;
  self.document.hidden_form.submit();
  

    Preview_Win_Name.focus(); 
	}
  }

}



		function win_pop(URL)
		{	
    		winname=window.open(URL,'previewwindow','width=990,height=500,left=210,top=210,resizable=yes,scrollbars=yes,status=yes'); 
		return winname;
		}


		
		
		
		
function xstooltip_findPosX(obj) 
{
  var curleft = 0;
  if (obj.offsetParent) 
  {
    while (obj.offsetParent) 
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}


function xstooltip_findPosY(obj) 
{
    var curtop = 0;
    if (obj.offsetParent) 
    {
        while (obj.offsetParent) 
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}


function xstooltip_show(tooltipId, parentId, posX, posY)
{

    it = document.getElementById(tooltipId);
    
    if ((it.style.top == '' || it.style.top == 0) 
        && (it.style.left == '' || it.style.left == 0))
    {
        // need to fixate default size (MSIE problem)
        it.style.width = it.offsetWidth + 'px';
        it.style.height = it.offsetHeight + 'px';
        
        img = document.getElementById(parentId); 
    
        // if tooltip is too wide, shift left to be within parent 
        if (posX + it.offsetWidth > img.offsetWidth) posX = img.offsetWidth - it.offsetWidth;
        if (posX < 0 ) posX = 0; 
        
        x = xstooltip_findPosX(img) + posX;
        y = xstooltip_findPosY(img) + posY;
        
        it.style.top = y + 'px';
        it.style.left = x + 'px';
    }
    
    it.style.visibility = 'visible'; 
}


function xstooltip_hide(id)
{
    it = document.getElementById(id); 
    it.style.visibility = "hidden";
}



