// Mockup QuickTags version 1.0
//
// Copyright (c) 2004 Sebastian Schocke
// Original Coding by Alex King, modified for use with mockup code by Sebastian Schocke
//
// Licensed under the LGPL license
// http://www.gnu.org/copyleft/lesser.html
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
// **********************************************************************
//
// This JavaScript will insert the tags below at the cursor position in IE and 
// Gecko-based browsers (Mozilla, Camino, Firefox, Netscape). For browsers that 
// do not support inserting at the cursor position (Safari, OmniWeb) it appends
// the tags to the end of the content.
//
// The variable 'edCanvas' must be defined as the <textarea> element you want 
// to be editing in. See the accompanying 'index.html' page for an example.

var edButtons = new Array();
var edLinks = new Array();
var edOpenTags = new Array();

function edButton(id, display, tagStart, tagEnd, open) {
	this.id = id;				// used to name the toolbar button
	this.display = display;	// label on button
	this.tagStart = tagStart; 	// open tag
	this.tagEnd = tagEnd;		// close tag
	this.open = open;			// set to -1 if tag does not need to be closed
}

//-------------------Formatting TEXT BUTTONS-----------------------//
edButtons[edButtons.length] = new edButton('ed_bold'
                                          ,'Bold'
                                          ,'[b]'
                                          ,'[/b]'
                                          );

edButtons[edButtons.length] = new edButton('ed_italic'
                                          ,'Italic'
                                          ,'[i]'
                                          ,'[/i]'
                                          );

edButtons[edButtons.length] = new edButton('ed_under'
                                          ,'Underline'
                                          ,'[u]'
                                          ,'[/u]'
                                          );
                                          
 edButtons[edButtons.length] = new edButton('ed_title'
                                          ,'Title'
                                          ,'[title]'
                                          ,'[/title]'
                                          );

edButtons[edButtons.length] = new edButton('ed_code'
                                          ,'Code'
                                          ,'[code]'
                                          ,'[/code]'
                                          );   
                                        
edButtons[edButtons.length] = new edButton('ed_list'
                                          ,'List'
                                          ,'[list]'
                                          ,'[/list]'
                                          );
                                          
edButtons[edButtons.length] = new edButton('ed_listitem'
                                          ,'List Item'
                                          ,'[*]'
                                          ,''
                                          ,-1
                                          );

//-------------------Alignment BUTTONS-----------------------//                                          
edButtons[edButtons.length] = new edButton('ed_break'
                                          ,'Line Break'
                                          ,'[br]'
                                          ,''
                                          ,-1
                                          );

edButtons[edButtons.length] = new edButton('ed_paragraph'
                                          ,'Paragraph'
                                          ,'[p]'
                                          ,''
                                          ,-1
                                          );

edButtons[edButtons.length] = new edButton('ed_left'
                                          ,'Align Left'
                                          ,'[left]'
                                          ,'[/left]'
                                          );
                                          
edButtons[edButtons.length] = new edButton('ed_center'
                                          ,'Align Center'
                                          ,'[center]'
                                          ,'[/center]'
                                          );

edButtons[edButtons.length] = new edButton('ed_right'
                                          ,'Align Right'
                                          ,'[right]'
                                          ,'[/right]'
                                          );
/*                                          
edButtons[edButtons.length] = new edButton('ed_font'
                                          ,'Font'
                                          ,'[font]'
                                          ,'[/font]'
                                        );
*/
edButtons[edButtons.length] = new edButton('ed_link'
                                          ,'Hyperlink'
                                          ,''
                                          ,'[/url]'
                                          ); // special case
										  
edButtons[edButtons.length] = new edButton('ed_maillink'
										  ,'E-Mail Link'
                                          ,''
                                          ,'[/email]'
                                          ); // special case

/*
edButtons[edButtons.length] = new edButton('ed_lmlink'
                                          ,'Menu Link'
                                          ,''
                                          ,'[/lmlink]'
                                          ); // special case
*/
edButtons[edButtons.length] = new edButton('ed_download'
										  ,'Download'
                                          ,''
                                          ,'[/download]'
                                          ); // special case

edButtons[edButtons.length] = new edButton('ed_img'
										  ,'Image'
                                          ,''
                                          ,'[/image]'
                                          ); // special case
                                

edButtons[edButtons.length] = new edButton('ed_table'
                                          ,'Table'
                                          ,'[table]'
                                          ,'[/table]'
                                          );
edButtons[edButtons.length] = new edButton('ed_tr'
                                          ,'Table Row'
                                          ,'[tr]'
                                          ,'[/tr]'
                                          );
edButtons[edButtons.length] = new edButton('ed_th'
                                          ,'Table Header'
                                          ,'[th]'
                                          ,'[/th]'
                                          );
edButtons[edButtons.length] = new edButton('ed_td'
                                          ,'Table Cell'
                                          ,'[td]'
                                          ,'[/td]'
                                          );
                                          
edButtons[edButtons.length] = new edButton('ed_dbgrid'
                                          ,'Database Grid'
                                          ,''
                                          ,'[/grid]'
                                          );
                                          
function buttonOn(btnName) 
{
	document.getElementById(btnName).style.backgroundColor = '#AAAAAA';
	document.getElementById(btnName).style.borderColor = '#000000';
}

function buttonOff(btnName) {
	document.getElementById(btnName).style.backgroundColor = '#EEEEEE';
	document.getElementById(btnName).style.borderColor = '#AAAAAA';
}

function edShowButton(button, i) {
    /*document.write('<input type="button" id="' + button.id + '" class="ed_button" onclick="');
    document.write('<td width=70 align=center style="cursor:pointer; border-style:solid; border-width:1; border-color:#AAAAAA" id="' + button.id + '" bgcolor=EEEEEE onMouseOver="buttonOn(\''+button.id+'\');" onMouseOut="buttonOff(\''+button.id+'\');" onclick="');
	if (button.id == 'ed_link') {
		document.write('edInsertLink(edCanvas, ' + i + ');');
	}
	else if (button.id == 'ed_maillink') {
		document.write('edInsertMailLink(edCanvas, ' + i + ');');
	}
	else if (button.id == 'ed_lmlink') {
		document.write('edInsertLMLink(edCanvas, ' + i + ');');
	}
    else if (button.id == 'ed_download') {
    document.write('ed_download(edCanvas, ' + i + ');');
	}
    else if (button.id == 'ed_img') {
    document.write('ed_img(edCanvas, ' + i + ');');
	}
    else if (button.id == 'ed_dbgrid') {
    document.write('ed_dbgrid(edCanvas, ' + i + ');');
	}
	else {
		document.write('edInsertTag(edCanvas, ' + i + ');');
	}
    //document.write('" value="' + button.display + '" />');
    document.write('">' + button.display + '</td>');*/
}

function edAddTag(button) {
	if (edButtons[button].tagEnd != '') {
		edOpenTags[edOpenTags.length] = button;
		document.getElementById(edButtons[button].id).textContent = '/' + document.getElementById(edButtons[button].id).textContent;
	}
}

function edRemoveTag(button) {
	for (i = 0; i < edOpenTags.length; i++) {
		if (edOpenTags[i] == button) {
			edOpenTags.splice(i, 1);
			document.getElementById(edButtons[button].id).textContent = document.getElementById(edButtons[button].id).textContent.replace('/', '');
		}
	}
}

function edCheckOpenTags(button) {
	var tag = 0;
	for (i = 0; i < edOpenTags.length; i++) {
		if (edOpenTags[i] == button) {
			tag++;
		}
	}
	if (tag > 0) {
		return true; // tag found
	}
	else {
		return false; // tag not found
	}
}	

function edCloseAllTags() {
	var count = edOpenTags.length;
	for (o = 0; o < count; o++) {
		edInsertTag(edCanvas, edOpenTags[edOpenTags.length - 1]);
	}
}

function edToolbar() {
/*	document.write('<div id="ed_toolbar">');
    document.write('<table width=70% border=0><tr>');
	for (i = 0; i < edButtons.length; i++) {
		edShowButton(edButtons[i], i);
        if( (i+1)%5 == 0 )
        {
            document.write('</tr><tr>');
        }
	}
    document.write('<td width=70 align=center style="cursor:pointer; border-style:solid; border-width:1; border-color:#AAAAAA" id="ed_close" bgcolor=EEEEEE  onMouseOver="buttonOn(\'ed_close\');" onMouseOut="buttonOff(\'ed_close\');" onclick="edCloseAllTags();">Close Tags</td>');
    document.write('</tr></table>');
	document.write('</div>');*/
}

// insertion code
function edInsertTag(myField, i) {
	//IE support
	if (document.selection) {
		myField.focus();
	    sel = document.selection.createRange();
		if (sel.text.length > 0) {
			sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd;
		}
		else {
			if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
				sel.text = edButtons[i].tagStart;
				edAddTag(i);
			}
			else {
				sel.text = edButtons[i].tagEnd;
				edRemoveTag(i);
			}
		}
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var cursorPos = endPos;
		var scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos)
			              + edButtons[i].tagStart
			              + myField.value.substring(startPos, endPos) 
			              + edButtons[i].tagEnd
			              + myField.value.substring(endPos, myField.value.length);
			cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
		}
		else {
			if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
				myField.value = myField.value.substring(0, startPos) 
				              + edButtons[i].tagStart
				              + myField.value.substring(endPos, myField.value.length);
				edAddTag(i);
				cursorPos = startPos + edButtons[i].tagStart.length;
			}
			else {
				myField.value = myField.value.substring(0, startPos) 
				              + edButtons[i].tagEnd
				              + myField.value.substring(endPos, myField.value.length);
				edRemoveTag(i);
				cursorPos = startPos + edButtons[i].tagEnd.length;
			}
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
		myField.scrollTop = scrollTop;
	}
	else {
		if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
			myField.value += edButtons[i].tagStart;
			edAddTag(i);
		}
		else {
			myField.value += edButtons[i].tagEnd;
			edRemoveTag(i);
		}
		myField.focus();
	}
}

function edInsertContent(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
	} else {
		myField.value += myValue;
		myField.focus();
	}
}

function edInsertLink(myField, i, defaultValue) {
	if (!defaultValue) {
		defaultValue = 'http://';
	}
	if (!edCheckOpenTags(i)) {
		var URL = prompt('Enter the URL' ,defaultValue);
		if (URL) {
			edButtons[i].tagStart = '[url ' + URL + ']';
			edInsertTag(myField, i);
		}
	}
	else {
		edInsertTag(myField, i);
	}
}

function edInsertLMLink(myField, i) {
	if (!edCheckOpenTags(i)) {
		var area = prompt('Enter the area name');
		var page = prompt('Enter the page name');
		if ( (area) && (page) ) {
			edButtons[i].tagStart = '[lmlink ' + area + ',' + page + ']';
			edInsertTag(myField, i);
		}
	}
	else {
		edInsertTag(myField, i);
	}
}
/*

function edInsertImage(myField) {
	var myValue = prompt('Enter the URL of the image', 'images/');
	if (myValue) {
		myValue = '[img ' 
				+ myValue 
				+ ']';
		edInsertContent(myField, myValue);
	}
}
*/
function edInsertMailLink(myField, i, defaultValue) {
	if (!defaultValue) {
		defaultValue = '';
	}
	if (!edCheckOpenTags(i)) {
		var email = prompt('Enter E-Mail Adress' ,defaultValue);
		if (email) {
			edButtons[i].tagStart = '[email ' + email + ']';
			edInsertTag(myField, i);
		}
	}
	else {
		edInsertTag(myField, i);
	}
}

function ed_download(myField, i) {
	if (!edCheckOpenTags(i)) {
		var downloadfile = prompt('Enter the download file name');
		var objectsecname = prompt('Enter the download file security name');
		//var text = prompt('Enter text to display');
		if ( (downloadfile) && (objectsecname)  ) 
        {
			edButtons[i].tagStart = '[download ' + downloadfile + ',' + objectsecname + ']';
			//edButtons[i].tagStart = '[download ' + downloadfile + ',' + objectsecname + ',' + text +  ']';
			edInsertTag(myField, i);
		}
	}
	else {
		edInsertTag(myField, i);
	}
}

function ed_img(myField, i) {
	if (!edCheckOpenTags(i)) {
		var downloadfile = prompt('Enter the URL of the image', 'images/');
		var objectsecname = prompt('Enter the download file security name');
		//var text = prompt('Enter text to display');
		if ( (downloadfile) && (objectsecname)  ) 
        {
			edButtons[i].tagStart = '[image ' + downloadfile + ',' + objectsecname + ']';
			//edButtons[i].tagStart = '[download ' + downloadfile + ',' + objectsecname + ',' + text +  ']';
			edInsertTag(myField, i);
		}
	}
	else {
		edInsertTag(myField, i);
	}
}
function ed_dbgrid(myField, i) {
	if (!edCheckOpenTags(i)) {
		var grid = prompt('Enter the Database Grid Name');
		//var text = prompt('Enter text to display');
		if (grid) 
        {
			edButtons[i].tagStart = '[grid ' + grid + ']';
			edInsertTag(myField, i);
		}
	}
	else {
		edInsertTag(myField, i);
	}
}

function PopUp_Export(id) 
{
    popUp_Form(id);
}
function popUp_Form( url ) {
	var xpos = 10;
	var ypos = 10;
	eval("lookuppage" + " = window.open('"+ url +"', 'lookuppage', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=980,height=600,left = "+xpos+",top = "+ypos+"'); lookuppage.opener = self");
}
function popUp_Custom( url, xpos, ypos, width, height ) {
	eval("reportpage" + " = window.open('"+ url +"', 'reportpage', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+width+",height="+height+",left = "+xpos+",top = "+ypos+"');");
}
function popUp_Report( url ) {
	var xpos = 10;
	var ypos = 10;
	eval("reportpage" + " = window.open('"+ url +"', 'reportpage', 'toolbar=1,scrollbars=1,location=0,statusbar=1,menubar=1,resizable=1,width=980,height=600,left = "+xpos+",top = "+ypos+"');");
}

