﻿// JScript File
var controls = new Array();

// Region  Images, Smiles, Documents panels Expand / Colapse
// class - control images, smiles, documents panel
function Cisd(panelID)
{
  var panelElement = document.getElementById(panelID);
  if (!panelElement)
    alert("Failed to find panel element in method Cisd '" + panelID + "'");
  // Use id names, can't think of a better way right now
  var expanderId =
    panelID == "postingSmileysCollapsed" ? "smilesExpander" :
    panelID == "postingImagesCollapsed" ? "imagesExpander" :
    "documentsExpander";
  var contentId =
    panelID == "postingSmileysCollapsed" ? "smiles" :
    panelID == "postingImagesCollapsed" ? "images" :
    "documents";
  var iconImageId =
    panelID == "postingSmileysCollapsed" ? "smiliesExpandImage" :
    panelID == "postingImagesCollapsed" ? "imagesExpandImage" :
    "documentsExpandImage";
  var titleId =
    panelID == "postingSmileysCollapsed" ? "titleSmiles" :
    panelID == "postingImagesCollapsed" ? "titleImage" :
    "titleDocuments";

	this.expandLink = document.getElementById(expanderId);
  this.contentContainer = document.getElementById(contentId);
  this.visible = false;
  this.expandIcon = document.getElementById(iconImageId);
  this.titleBar = document.getElementById(titleId);
  this.titleBar.onclick = toogle;
  this.expandLink.onclick = toogle;
/*old, causes javascript error: this.parent.children has no properties
  this.parent = getDOMObject(panelID);
  this.expandLink = this.parent.children[0];
  this.contentContainer = this.parent.children[2];
  this.visible = false;
  this.expandIcon = this.expandLink.children[0];
  this.titleBar =  this.parent.children[1];
  this.titleBar.onclick = toogle;
  this.expandLink.onclick = toogle;
*/
}

function toogle()
{
  for(var i=0;i<controls.length;i++)
  {
		if( controls[i].expandLink.id == this.id ||
			controls[i].titleBar.id == this.id)
		{    
			if(controls[i].visible)    
			{
				controls[i].expandIcon.src = "../Icons/ExpandIcon.png";
				controls[i].contentContainer.style.display = "none";    
			}
			else 
			{
				controls[i].expandIcon.src = "../Icons/CollapseIcon.png"
				controls[i].contentContainer.style.display = "block";
			}
			
			controls[i].visible = !controls[i].visible;
		}
		else 
		{
			controls[i].visible = false;
			controls[i].expandIcon.src = "../Icons/ExpandIcon.png";
			controls[i].contentContainer.style.display = "none";
		}
  }
}

// END REGION
function IsIE()
{
  if(navigator.userAgent.indexOf("MSIE")!=-1)
		return true;
  return false;
}


function getDOMObject(documentID)
{
	// ie4
	if (document.all)
	{
		return document.all[documentID];
	}
	// isIE6
	else if(document.getElementById)
	{
		return document.getElementById(documentID);
	}
	// isNS4
	else if (document.layers)
	{	
		return document.layers[documentID];
	}
	// isNS6
	else if (document.getElementById && !document.all)
	{
		return document.getElementById(documentID);
	}
}

// Command
var command = "";

function InitToolbarButtons() 
{
	var kids = document.getElementsByTagName('DIV');
	var k=0;
	for (var i=0; i < kids.length; i++) 
	{
		if (kids[i].className.indexOf("button") != -1) 
		{
			kids[i].onmouseover = tbmouseover;
			kids[i].onmouseout = tbmouseout;
			kids[i].onmousedown = tbmousedown;
			//kids[i].onmouseup = tbmouseup;
			kids[i].onclick = tbclick;
		}
		else if (kids[i].className.indexOf("postingFormExtras") != -1)
		{
			// create objects for Docs, Smiles and Images
			var control = new Cisd(kids[i].id);
			controls[k] = control;
			k++;
		}
	}
	
	// Make sure only one panel is expanded
	toogle();
}

function tbmousedown(e)
{
  var evt = e ? e : window.event; 
 
  if (evt.returnValue)
  {
    evt.returnValue = false;
  }
  else if (evt.preventDefault)
  {
    evt.preventDefault( );
  }
  else
  {
    return false;
  }
}

function tbmouseup()
{
  // do nothing
}

function tbmouseout()
{ 
	this.style.backgroundColor = "rgb(255,255,255)";
}

function tbmouseover()
{ 
	this.style.backgroundColor = "rgb(215,227,239)";
}

function tbclick()
{  
  if ((this.id == "forecolor") ||
		(this.id == "hilitecolor"))
	{
    parent.command = this.id;
    parent.iframeID = iframeID;
    var buttonElement = document.getElementById(this.id);        
    var oldcolor = GetEditBoxColor("forecolor");
    var color = GetColorFromUser(oldcolor, buttonElement);    
    selectColor(color);
  }
  else if (this.id == "createlink")
  {
    var szURL = prompt("Gib eine URL ein:", "http://");
    if ((szURL != null) && (szURL != "")) 
    {    
			//var aa =
			//  document.getElementById(iframeID).contentWindow.getSelection() ||
			//  "null";
			//alert(aa);
			if (IsIE() &&
				document.getElementById(iframeID).contentWindow.document.selection.
				type == "None")
			{
				document.getElementById(iframeID).contentWindow.focus();
				var sel = document.getElementById(iframeID).contentWindow.document.
					selection.createRange();
				sel.pasteHTML("<a href="+szURL+">"+szURL+"</a>");
				sel.select();
			}
			else
				if(IsIE() == false &&
					document.getElementById(iframeID).contentWindow.getSelection() == "")
				{
					var linkObj = textboxDocumentObject.createElement("a");
					linkObj.href = szURL;
					linkObj.innerHTML = szURL;
					textboxDocumentObject.body.appendChild(linkObj);
				}
				else
					document.getElementById(iframeID).contentWindow.document.execCommand(
						"CreateLink",false,szURL);
			}
		}
    else if(this.id == "mailto")
    {
			// For email addresses
      var szURL = prompt("Gib eine Email Addresse ein:", "");
      if ((szURL != null) && (szURL != "")) 
      {            
        if (IsIE() &&
					document.getElementById(iframeID).contentWindow.document.selection.
					type == "None")
        {
          document.getElementById(iframeID).contentWindow.focus();
          var sel = document.getElementById(iframeID).contentWindow.document.
						selection.createRange();
          sel.pasteHTML(
						"<a style='border-bottom:dashed 1px;text-decoration:none' href=mailto:"+
						szURL+">"+szURL+"</a>");
          sel.select();
        }
        else
        {
          if(IsIE() == false &&
						document.getElementById(iframeID).contentWindow.getSelection() ==
						"")
          {
            var linkObj = textboxDocumentObject.createElement("a");
            linkObj.href = "mailto:" + szURL;
            linkObj.style.textDecoration = "none";
            linkObj.style.borderBottom = "dashed 1px rgb(0,0,239)";
            linkObj.innerHTML = szURL;
            textboxDocumentObject.body.appendChild(linkObj);
          }
          else
          {
              szURL = "mailto:" + szURL;
              document.getElementById(iframeID).contentWindow.document.
								execCommand("CreateLink",false,szURL);
          }
        }
      }
  }
  else if(this.id == "codesnippet")
  {
    parent.command = this.id;
    parent.iframeID = iframeID;
    var buttonElement = document.getElementById(this.id);
    var code = GetSnipetFromUser(buttonElement);
    if ((code != null) &&
			(code != "") &&
			IsIE()) 
    {
      if(textboxDocumentObject.selection.type != "None") 
        textboxDocumentObject.selection.clear();
      document.getElementById(iframeID).contentWindow.focus();
      var sel = document.getElementById(iframeID).contentWindow.document.
				selection.createRange();
      sel.pasteHTML(code);
      sel.select();
    }
  }
  else
  {
    document.getElementById(iframeID).contentWindow.focus();
    document.getElementById(iframeID).contentWindow.document.execCommand(
			this.id, false, null);
    document.getElementById(iframeID).contentWindow.focus();
  }
}

function dismisscolorpalette()
{
	// Nothing in here
}

var iframeID;
var once = 0;

function Start(iframe_id) 
{
  iframeID = iframe_id;
  IFrameInit();
  
//  if(IsIE())
//  {
//    var head = document.getElementById(iframeID).contentWindow.document.
//			createElement("head");
//    document.getElementById(iframeID).contentWindow.document.appendChild(
//			head);
//  
//    var style = document.getElementById(iframeID).contentWindow.document.
//			createElement("link");//.href = "PostTextBox.css";
//    style.href = "PostTextBox.css";
//    style.type = "text/css";
//    style.rel = "stylesheet";
//    head.appendChild(style);  
//  }
//  else
//  {
//    if(once != 0)  return;
//    var styleObj = textboxDocumentObject.createElement("link");
//    styleObj.href = "PostTextBox.css";
//    styleObj.type = "text/css";
//    styleObj.rel = "stylesheet";
//    
//    //var headObj = textboxDocumentObject.getElementsByTagName("head");
////    if(headObj[0])
////    {
////        once++;
////        headObj[0].appendChild(styleObj);
////    }
//  }
  
  InitToolbarButtons();
  if (document.addEventListener)
  {
    document.addEventListener("mousedown", dismisscolorpalette, true);
    document.getElementById(iframeID).contentWindow.document.addEventListener(
			"mousedown", dismisscolorpalette, true);
    document.addEventListener("keypress", dismisscolorpalette, true);
    document.getElementById(iframeID).contentWindow.document.addEventListener(
			"keypress", dismisscolorpalette, true);
  }
  else if (document.attachEvent)
  {
    document.attachEvent("mousedown", dismisscolorpalette, true);
    document.getElementById(iframeID).contentWindow.document.attachEvent(
			"mousedown", dismisscolorpalette, true);
    document.attachEvent("keypress", dismisscolorpalette, true);
    document.getElementById(iframeID).contentWindow.document.attachEvent(
			"keypress", dismisscolorpalette, true);
  }
}

function viewsource(source)
{
  var html;
  if (source)
  {
    html = document.createTextNode(document.getElementById(iframeID).
			contentWindow.document.body.innerHTML);
    document.getElementById(iframeID).contentWindow.document.body.innerHTML =
			"";
    html = document.getElementById(iframeID).contentWindow.document.importNode(
			html,false);
		document.getElementById(iframeID).contentWindow.document.body.appendChild(
			html);
    document.getElementById("toolbar1").style.visibility="hidden";
    document.getElementById("toolbar2").style.visibility="hidden";  
  }
  else
  {
    html = document.getElementById(iframeID).contentWindow.document.body.
			ownerDocument.createRange();
    html.selectNodeContents(document.getElementById(iframeID).contentWindow.
			document.body);
    document.getElementById(iframeID).contentWindow.document.body.innerHTML =
			html.toString();
    document.getElementById("toolbar1").style.visibility="visible";
    document.getElementById("toolbar2").style.visibility="visible";
  }
}

function usecss(source)
{
  document.getElementById(iframeID).contentWindow.document.execCommand(
		"useCSS", false, !(source));
}

function readonly(source)
{
  document.getElementById(iframeID).contentWindow.document.execCommand(
		"readonly", false, !(source));
}

function SetHiddenVar()
{
	var result = getDOMObject(iframeID).contentWindow.document.body.innerHTML;   
	return result;
}

var emoticons = null;
function ExecCommand(command, value, valueIsID)
{
	//if( null == emoticons) emoticons = new Hashtable();
	var extraObj;
	if(null != valueIsID && valueIsID==true)
	{
		extraObj = getDOMObject(value);
		value = extraObj.value;
	}
	document.getElementById(iframeID).contentWindow.focus();
	document.getElementById(iframeID).contentWindow.document.execCommand(
		command, false, value);
	   
	document.getElementById(iframeID).contentWindow.focus();
}

function InsertSmile(smileSrc)
{
	var textToBeInserted = "<img src='"+smileSrc+"' />";    
	if(IsIE())
	{
		var sel = textboxDocumentObject.createElement(textToBeInserted);
		//.selection.createRange();                    
		textboxDocumentObject.body.insertBefore(sel);
		sel.unselectable = "on";
	}
	else
	{
		textboxDocumentObject.execCommand("InsertImage",false,smileSrc);
	}   
}

var textboxDocumentObject = null;
var textboxObject = null;

function IFrameInit()
{    
	textboxDocumentObject =
		document.getElementById(iframeID).contentWindow.document;
	textboxObject = document.getElementById(iframeID).contentWindow;    
	textboxDocumentObject.designMode = "on";
	
	try 
	{
		textboxDocumentObject.execCommand("undo", false, null);
	}
	catch (e) 
	{
		alert("This forum is not supported on your level of Mozilla.");
	}
}


function over()
{
  this.style.border='2px dotted white';
}

function out()
{
  this.style.border='1px solid gray';
}

function click()
{
  selectColor(this.id);
}

//FUNCTIONS FOR TEXT COLOR
function selectColor(color)
{
  textboxObject.focus();
  textboxDocumentObject.execCommand("forecolor", false, color);              
  textboxObject.focus();        
}

function GetEditBoxColor(colorCommand)
{    
	return DecimalToRGB(textboxDocumentObject.queryCommandValue(colorCommand));
}
function DecimalToRGB(value)
{
	var hex_string = '';
	for (var hexpair = 0; hexpair < 3; hexpair++) {
   	var byteVar = value & 0xFF;            // get low byte
	    value >>= 8;                        // drop low byte
	    var nybble2 = byteVar & 0x0F;          // get low nybble (4 bits)
	    var nybble1 = (byteVar >> 4) & 0x0F;   // get high nybble
	    hex_string += nybble1.toString(16); // convert nybble to hex
	    hex_string += nybble2.toString(16); // convert nybble to hex
   }
	return hex_string.toUpperCase();
}

function GetColorFromUser(oldcolor, buttonElement)
{    
	var posX    = event.screenX;
	var posY    = event.screenY + 20;
	var screenW = screen.width;                           // screen size
	var screenH = screen.height - 20;                     // take taskbar into account
	if (posX + 232 > screenW) { posX = posX - 232 - 40; } // if mouse too far right
	if (posY + 164 > screenH) { posY = posY - 164 - 80; } // if mouse too far down
	var wPosition   ;
	var newcolor;	
	if (window.showModalDialog)
	{
    wPosition = 'dialogLeft:' +posX+ '; dialogTop:' +posY;
    newcolor = showModalDialog('../html/ColorPicker.html',
			oldcolor,
			'dialogWidth:238px; dialogHeight: 195px; '
      + 'resizable: no; help: no; status: no; scroll: no; '
      + wPosition);
	}
	// firefox
	else
	{	
    var colorPicker = getDOMObject("colorpalette");	    

    if (colorPicker.style.visibility=="visible") 
    {
      colorPicker.style.visibility="hidden";
      return;
    }
    
    posX = getOffsetLeft(buttonElement);    	
    posY = getOffsetTop(buttonElement)+24;
		//screen.height - (document.body.clientHeight - getOffsetTop(buttonElement))-33;
    
    parent.colorPaletePosX = posX;
    parent.colorPaletePosY = document.body.clientHeight -
			getOffsetTop(buttonElement)+195;
    	   
    colorPicker.style.left = posX+"px";
    colorPicker.style.top = posY+"px";	    
    colorPicker.style.position = "absolute";
    colorPicker.style.visibility="visible";
    
    return;	    	                           
	}	
	return newcolor
}

// Get code snipet functions
function GetSnipetFromUser(buttonElement)
{
	var posX    = event.screenX;
	var posY    = event.screenY + 20;
	var screenW = screen.width;                             // screen size
	var screenH = screen.height - 20;                       // take taskbar into account
	//if (posX + 232 > screenW) { posX = posX - 232 - 40; } // if mouse too far right
	//if (posY + 164 > screenH) { posY = posY - 164 - 80; } // if mouse too far down
	posX = screenW/2 - 190;
	posY = screenH/2 - 130;
	var wPosition;	
	var code;
	if (window.showModalDialog)
	{
    wPosition = 'dialogLeft:' +posX+ '; dialogTop:' +posY;	    
    code = showModalDialog('../html/InsertCodeSnipet.html',
			"Get Code Snipet",
      'dialogWidth:380px; dialogHeight: 265px; '
      + 'resizable: no; help: no; status: no; scroll: no; '
      + wPosition);	    
	}
	else//firefox
	{	    		        
		posX = getOffsetLeft(buttonElement)+24;    	
		posY = getOffsetTop(buttonElement)-227;

		var codeSnipet = getDOMObject("codeSnipet");	    
		codeSnipet.style.left =posX +"px";
		codeSnipet.style.top = posY+"px";	    
		codeSnipet.style.position = "absolute";
		codeSnipet.contentWindow.document.getElementById("txtCodeSnipet").focus();
		codeSnipet.style.visibility="visible";	    	                        
	}	
	return code;
}

// Add images
function GetImgUrl(obj)
{
	var imgSrc = obj.src;
	if(null != imgSrc)
	{
		var textToBeInserted = "<img src='"+imgSrc+"' />";

		if(IsIE())
		{
			var sel = textboxDocumentObject.createElement(textToBeInserted);
			textboxDocumentObject.body.insertBefore(sel);
			sel.unselectable = "on";
		}
		else
		{  
			textboxDocumentObject.execCommand("InsertImage",false,imgSrc);            
		}
	}    
}

function InsertDocument(pdfPic, docDescription, docURL)
{    
	//<a href='"+docURL+"'><img src="+pdfPic+" />"+docDescription+"</a>       
	var pdfImgText = "<img src="+pdfPic+" />";
	var linkText = "<a href='"+docURL+"'>ggg</a>";
	if(IsIE())
	{            
		var linkObj = textboxDocumentObject.createElement(linkText);
		linkObj.innerHTML = pdfImgText + docDescription;
		textboxDocumentObject.body.insertBefore(linkObj);                               	}
	else
	{                                                  
		var el = textboxDocumentObject.createElement("a");
		el.innerHTML = pdfImgText + docDescription;
		el.href = docURL;
		textboxDocumentObject.body.appendChild(el);
	}
}

// Firefox methods
function getOffsetTop(elm)
{
  var mOffsetTop = elm.offsetTop;
  var mOffsetParent = elm.offsetParent;

  while(mOffsetParent)
  {
    mOffsetTop += mOffsetParent.offsetTop;
    mOffsetParent = mOffsetParent.offsetParent;
  }
 
  return mOffsetTop;
}

function getOffsetLeft(elm)
{
  var mOffsetLeft = elm.offsetLeft;
  var mOffsetParent = elm.offsetParent;

  while(mOffsetParent)
  {
    mOffsetLeft += mOffsetParent.offsetLeft;
    mOffsetParent = mOffsetParent.offsetParent;
  }
 
  return mOffsetLeft;
}

// Edit, quote, reply
function ProcessRequest(text)
{
  if(null == text || "" == text)
		return;
  document.getElementById(iframeID).contentWindow.focus();
  var sel = document.getElementById(iframeID).contentWindow.document.selection.
		createRange();
  sel.pasteHTML(text);
  sel.select();
}

var action = "post";
var postID = null;

function GetElement(sender)
{
	// debugger
  var postText;
  
  if(sender.id.indexOf("reply") != -1)
  {
    SetSubmitPost_Text("Neuen Beitrag abschicken");
    SetPostHeader_Text("Antworten");
    postText = "";
  }
  if(sender.id.indexOf("edit") != -1)
  {
    action = "edit";
    postID = sender.id.split("_")[1];//obj.parentNode.children[0].innerHTML;    
    postText = getDOMObject(sender.id.replace("hrefedit","postText")).innerHTML;
    //obj.parentNode.parentNode.children[2].innerHTML;              
    //set text for submit post button    
    SetSubmitPost_Text("Editiere Beitrag");
    SetPostHeader_Text("Editieren");
  }   
  else if(sender.id.indexOf("quote") != -1)  
  {    
    action = "quote";
    postID = sender.id.split("_")[1];
    var postUserName = getDOMObject(sender.id.replace("hrefquote","userInfo")).
			children[0].innerText;
    postText = "<div class=\"quoteContainer\">"+
			"<span class=\"quoteTitle\">"+postUserName + " schrieb: </span>"+
			"<div class=\"quoteText\"> "+
			getDOMObject(sender.id.replace("hrefquote","postText")).innerHTML +
			"</div></div><br /><br />";
    SetPostHeader_Text("Zitieren");
  }      
  if(IsIE())
  {
    textboxObject.focus();
    textboxDocumentObject.execCommand("SelectAll", false, null);
    textboxDocumentObject.selection.clear();    
    
    var range = textboxDocumentObject.selection.createRange();
    range.pasteHTML(postText);   
    range.select();        
    textboxDocumentObject.execCommand("SelectAll", false, null);
    textboxDocumentObject.selection.empty();  
  }
  // Mozilla Firefox
  else
  {
		textboxObject.focus();
		textboxDocumentObject.body.innerHTML = postText;

		var newP = textboxDocumentObject.createElement("p");
		newP.innerHTML = "<br/>&nbsp;<br/>&nbsp;";
		textboxDocumentObject.body.appendChild(newP);                                   
		// textboxObject.focus();
  }   
}

function btnCancel_Click()
{
  var obj = getDOMObject(iframeID);
  obj.contentWindow.document.body.innerHTML = "";
  SetSubmitPost_Text("Neuen Eintrag abschicken");
  SetPostHeader_Text("Neuen Beitrag schreiben");
}

function btnSumbitPost_Click(postEditTextBox_ClientID)
{
//  try {
	//	if (getDOMObject('postTextBoxHiddenID') == null) {
	//		return;
	//	}
	
		getDOMObject('postTextBoxHiddenID').value =
			getDOMObject(postEditTextBox_ClientID).contentWindow.document.body.innerHTML;
		//set postTextBoxHiddenID field. FORMAT: actionName|postID
		var value = "post|";
		if(null != postID)
		{
			value = action + "|" + postID;
			postID = null;
		}
		if (getDOMObject('postActionHiddenField') != null) {
			getDOMObject("postActionHiddenField").value = value;
		}
//	}
//	catch() {
//		// A "nullRefenceException makes no sense, but it happens :( 
//		// -> simply ignore anny errors
//	}
}

// Modifies the label on the submit post button in concordance with the action
// taken(eg: edit, post, quote)
function SetSubmitPost_Text(text)
{
  var inputObjs = document.getElementsByTagName("input");    
  for(var i=inputObjs.length-1 ; i>=0;i--)
  {
    if(inputObjs[i].type=="submit" &&
			inputObjs[i].onclick.toString().indexOf("btnSumbitPost_Click")!=-1)
    {        
      inputObjs[i].value = text;
      return;
    }
  }
}

function SetPostHeader_Text(text)
{
  var spanObjs = document.getElementsByTagName("span");    
  for(var i=spanObjs.length-1 ; i>=0;i--)
  {
    if(spanObjs[i].onclick.toString().indexOf("postcontrol1_Label1")!=-1)
    {        
      spanObjs[i].value = text;
      return;
    }
  }
}

function FocusTxtBox()
{
  getDOMObject(iframeID).contentWindow.focus();
  getDOMObject(iframeID).contentWindow.document.selection.createRange();
}

