﻿//***************
function strReplaceAll ( strSource, strTarg, strRepl )
    {
      if (null == strSource ) return "";
      intStrPos = strSource.indexOf( strTarg );
      while (intStrPos != -1)
        {
          if (intStrPos < strSource.length -1 )
            strSource = strSource.substring(0, intStrPos ) + strRepl +
            strSource.substring(intStrPos+1, strSource.length);
          else
            strSource = strSource.substring(0, intStrPos );
          intStrPos = strSource.indexOf( strTarg, intStrPos + strRepl.length ); }
          // alert("string is [" + strSource + "]");
          return strSource;
    }

//***************
function define_objTrim( name )
  {
    this.name = name;
    this.string = "";
    this.intLeadSpace = -1;
    this.intTrailSpace = -1;
  }

//***************
function trimStringRef( objTrim )
  {
    var str = "" + objTrim.string;
    var intLeadSpace = 0 + objTrim.intLeadSpace;
    var intTrailSpace = 0 + objTrim.intTrailSpace;
    
    while (str.charAt(0) == ' ')
    	{ 
				str = str.substring(1); 
 				intLeadSpace = intLeadSpace + 1;
 				//intLeadSpace += 1;
			}
      
    while (str.charAt(str.length - 1) == ' ')
      { 
  		  str = str.substring(0, str.length - 1);
	  	  intTrailSpace = intTrailSpace + 1;
			}
           
    //if ( intLeadSpace > 0 ) intLeadSpace = 1;
    //if ( intTrailSpace > 0 ) intTrailSpace = 1;
    
    objTrim.string = str;
    objTrim.intLeadSpace = intLeadSpace;
    objTrim.intTrailSpace = intTrailSpace;
      
    return objTrim;
  }


//***************
function insertLinkInParent( strTarget )
	{ 
	  var strInsertion = "";
	  var strLink = document.childForm.cf1.value;
	  var object1 = opener.document.selection.createRange(); 
		//var strSelection = document.selection.createRange().text;
		//var strSelection = opener.document.selection.createRange().text;
		var strSelection = object1.text;
		var strLeadSpace = "";
    var strTrailSpace = "";
    
    // declare new object
    var objTrim = new define_objTrim( "objTrim" );
    objTrim.string = strSelection;
  	objTrim.intLeadSpace = 0;
  	objTrim.intTrailSpace = 0;

		if (strSelection == "") 
			{ 
				return false; 
			} 
		else
	    {
  	    objTrim = trimStringRef( objTrim );
  	    strSelection = objTrim.string;
        for ( var i = 0; i < objTrim.intLeadSpace; i++ )
          strLeadSpace += " ";
        for ( var i = 0; i < objTrim.intTrailSpace; i++ )
          strTrailSpace += " "; 
		
// spremenljivka za izbiro dodatkov ///////////////
var strIzbira = document.childForm.fldIzbira.value;
///////////////////////////////////////////////////

////// test vrednosti /////////////////////////////	
// alert(strIzbira);
///////////////////////////////////////////////////
	
			if (strIzbira == "linki")
				{
					strInsertion = strInsertion + strLeadSpace;        
				    strInsertion = strInsertion + "<#sp_lnk('";
			        strInsertion = strInsertion + strLink;
			        strInsertion = strInsertion + "')#>";
			        strInsertion = strInsertion + strSelection;
					strInsertion = strInsertion + "<#zd_lnk('";
					strInsertion = strInsertion + strLink;
			        strInsertion = strInsertion + "')#>";
			        strInsertion = strInsertion + strTrailSpace;
				 }
			
			if (strIzbira == "email")
				{
					strInsertion = strInsertion + strLeadSpace;        
				    strInsertion = strInsertion + "<#sp_email('";
			        strInsertion = strInsertion + strLink;
			        strInsertion = strInsertion + "')#>";
			        /*strInsertion = strInsertion + strSelection;
					strInsertion = strInsertion + "<#zd_email('";
					strInsertion = strInsertion + strLink;
			        strInsertion = strInsertion + "')#>";
			        strInsertion = strInsertion + strTrailSpace;*/
				 }

			if (strIzbira == "povezave")
				{
					strInsertion = strInsertion + strLeadSpace;        
				    strInsertion = strInsertion + "<a href='";
			        strInsertion = strInsertion + strLink;
			        strInsertion = strInsertion + "' class=plavi target=_blank>";
			        //strInsertion = strInsertion + strSelection;
					strInsertion = strInsertion + strLink;
			        strInsertion = strInsertion + "</a>";
			        strInsertion = strInsertion + strTrailSpace;
				 }
				
			if (strIzbira == "priponke")
				{
					strInsertion = strInsertion + strLeadSpace;        
				    strInsertion = strInsertion + "<#sp_prp('";
			        strInsertion = strInsertion + strLink;
			        strInsertion = strInsertion + "')#>";
			        strInsertion = strInsertion + strSelection;
					strInsertion = strInsertion + "<#zd_prp('";
					strInsertion = strInsertion + strLink;
			        strInsertion = strInsertion + "')#>";
			        strInsertion = strInsertion + strTrailSpace;
				}

			if (strIzbira == "foto")
				{
					strInsertion = strInsertion + strLeadSpace;        
				    strInsertion = strInsertion + "<#foto('";
			        strInsertion = strInsertion + strLink;
			        strInsertion = strInsertion + "')#>";
			        strInsertion = strInsertion + strTrailSpace;
				}
				
				strInsertion = strReplaceAll( strInsertion, '#', '?' );
				
				       
				object1.text = strInsertion;
				
 			} 
		return true;
	}


//***************
function funAlerty()
	{ 
	  var strSelection = "Hello!";
	  
    alert("string is [" + strSelection + "]");
		return;
	}

	
//***************	
function updateParent()
  {
    var strTarget = "_blank";
    var object1 = opener.document.parentForm; 
    var object2 = document.childForm; 
        
    //object1.pf1.value = object2.cf1.value;
    //object1.pf2.value = object2.cf2.value;
        
    insertLinkInParent( strTarget );
    //funAlerty();
    
    self.close();
    return false;
  }
  
function updateParentFoto()
  {
//    var strTarget = "_blank";
opener.document.parentForm.mod_vsebina.value = document.childForm.cf1.value;
//    var object2 = document.childForm; 
        
    //object1.pf1.value = object2.cf1.value;
    //object1.pf2.value = object2.cf2.value;
        
//    insertLinkInParent( strTarget );
    //funAlerty();
    
    self.close();
    return false;
  }

  // ***************
function openChild(file,window) // open new window +
  {
    childWindow=open(file,window,'resizable=no,width=500,height=10');
    if (childWindow.opener == null) childWindow.opener = self;
  }
  
// *************** bold, italic, ... , url, mail +
function changeTextThis( tag_text ) 
	{ 
		strSelection = document.selection.createRange().text
		
		if (strSelection == "") 
			{ 
				return false; 
			} 
		else if (tag_text == "mail")
			{
			document.selection.createRange().text = "<a href=\"mailto\:" + strSelection + "\">" + strSelection + "</a>";
			}
		else if (tag_text == "url")
			{
			document.selection.createRange().text = "<a href=\"" + strSelection + "\" target=\"_blank\">" + strSelection + "</a>";
			}
		else
			{ 
			// document.selection.createRange().text = "<b>" + strSelection + "</b>";
		  document.selection.createRange().text = "<" + tag_text + ">" + strSelection + "</" + tag_text + ">";
			}		  		
		return;
	}
//**************** open new window for foto +
	function foto(id, width, height)
		{
			window.open("./util/foto_page.php?id="+id, "_blank", "width =" +width +",height =" +height +",location=no,menubar=no,directories=no,toolbar=no,scrollbars=no,resizable=no,status=no");
		}

	