// JavaScript Document

text = uploadingPrompt = null;
toolBarObj = null;
xmlObj = currentFolder = allImages = findImage = sel = null;
//Objext Controlling Functions, act as garbage collectors


function editNewTextBox(div) {
	if(text != null) return;
	text = new textBox(div);   
	text.Edit();
}
function replaceAll(text, strA, strB)
{
    while ( text.indexOf(strA) != -1)
    {
        text = text.replace(strA,strB);
    }
    return text;
}
function safeText(text) {
	text = text.replace(/'/g,"[\']");
	text = text.replace(/"/g,'[\"]');
	text = text.replace(/&/g,'[and]');
	text = text.replace(/=/g,'[equal]');
	return text;
}
function saveOldTextBox() {
	text.Save();
	text.toolBar.timeCount = 0;
	text.toolBar.dir = 1;
	text.toolBar.fade();
	if(findImage) {
		findImage.timeCount = 0;
		findImage.dir = 1;
		findImage.fade();
		findImage = null;
	}
	if(text.linkPrompt) {
		text.linkPrompt.timeCount = 0;
		text.linkPrompt.dir = 1;
		text.linkPrompt.fade();
	}
	text = null;
	page = getAnchorValue();
}
function cancelOldTextBox() {
	text.Cancel();
	text.toolBar.timeCount = 0;
	text.toolBar.dir = 1;
	text.toolBar.fade();
	if(findImage) {
		findImage.timeCount = 0;
		findImage.dir = 1;
		findImage.fade();
		findImage = null;
	}
	if(text.linkPrompt) {
		text.linkPrompt.timeCount = 0;
		text.linkPrompt.dir = 1;
		text.linkPrompt.fade();
	}
	text = null;
}
function getImage() {
	findImage = new customPrompt('custom', document.getElementById('dimmer2Front'), document.getElementById('dimmer2Back'));
	
	dir = "../imgbin/";
	var folderXML = null; imageHTML = folderHTML = '';
	var loadImage = createXmlHttpRequestObject();  
	try {
		loadImage.overrideMimeType('text/xml');
	} catch(e) {
		//start creating an error log!
	}
	loadImage.onreadystatechange = function() {
		if(loadImage.readyState != 4) {return false;}
		if(loadImage.responseXML) {
			folderXML = loadImage.responseXML;
					
			folders = folderXML.getElementsByTagName('dir');
			for(var fi = 0; fi < folders.length; fi++) {
				folderHTML += '<table width="34" border="0" cellspacing="0" cellpadding="0">'
		 +'       <tr>'
		 +'         <td width="34">'+folders[fi].attributes.getNamedItem("name").value +'</td>'
		 +'       </tr>'
		 +'       <tr>'
		 +'         <td><img src="http://www.celebrityeventsnetwork.ca/Img-bin/folder.gif" width="35" height="35" alt="Folder" onclick="setImageHTML(\''+folders[fi].attributes.getNamedItem("id").value+'\');" /></td>'
		 +'       </tr>'
		 +'       <tr>'
		 +'         <td>&nbsp;</td>'
		 +'       </tr>'
		 +'   </table>';
			}
			fillHTML = '<table width="570px" border="0" cellspacing="0" cellpadding="0">'
 +' <tr>'
 +'   <td colspan="3"><div id="imgDragHandler" class="mover">Currently Showing: <span id="whichDir">'+dir+'<s/pan></div></td>'
 +' </tr>'
 +' <tr>'
 +'   <td width="65px"><div id="folderContainer" class="folderContainer">'+folderHTML+'</div></td>'
 +'   <td width="300px"><div id="imageContainer" class="imageContainer">'+imageHTML+'</div></td>'
 +'   <td width="205px"><div id="preview" class="imagePreview"></div></td>'
 +' </tr>'
 +' <tr>'
 +'   <td colspan="1"><input type="button" name="ok" value="Accept" id="close"/></td>'
 +'   <td colspan="2" align="center"><form action="../backbone/upload.php" target="fileUpload" method="post" enctype="multipart/form-data">Upload to <span id="whichDir2">'+dir+'</span>: <input name="img" type="file" id="uploadImg" size="35" />'
 +'<input name="folder" type="hidden" id="folder" value="'+dir+'" /><input type="submit" name="upload" value="Upload" onclick="uploading()" /></form></td>'
 +' </tr>'
+'</table>';
			findImage.setInnerHTML(fillHTML);
			findImage.init();
			xmlObj = folderXML;
			setImageHTML(dir);
			var iga = new Array(document.getElementById('dimmer2Front'), document.getElementById('dimmer2Back'));
			makeDraggable(iga, document.getElementById('imgDragHandler'))
			
			document.getElementById('close').onclick = function() {
				findImage.dir=1;
				findImage.timeCount=0;
				findImage.fade();
			}
		}
	}
	URL = geFolderValue() + 'backbone/files.php?url='+dir;
	loadImage.open("GET", URL, true);
	loadImage.send(null);
	
}
function setImageHTML(folder) {
	var holder = document.getElementById('imageContainer');
	document.getElementById('whichDir').innerHTML = document.getElementById('whichDir2').innerHTML = document.getElementById('folder').value = folder;
	//xmlObj = xml.documentElement;
	var imageHTML = '';
	images = getElementByIdMXL(xmlObj, folder);
	allImages = images.getElementsByTagName('*');
	var x = 1, n = 5;
	imageHTML += '<tr>';
	for(var i = 0; i < allImages.length; i++)	{
		if (allImages[i].hasAttribute('id')) {
			divNum = x/n;
			if((divNum) == Math.ceil(divNum)) 	imageHTML += '</tr><tr>';
			imageHTML += '<td align="center"><table width="34" border="0" cellspacing="0" cellpadding="0">'
			 +'       <tr>'
			 +'         <td width="34">'+allImages[i].childNodes[0].nodeValue +'</td>'
			 +'       </tr>'
			 +'       <tr>'
			 +'         <td><img src="'+allImages[i].getAttribute("id")+'" width="35" height="35" alt="Folder" onclick="preview(allImages['+i+'])" /></td>'
			 +'       </tr>'
			 +'       <tr>'
			 +'         <td>&nbsp;</td>'
			 +'       </tr>'
			 +'   </table></td>';
			x++;
			
		}
		if((i+1) == allImages.length) {
			  while((divNum) != Math.ceil(divNum)) {
				  divNum = x/n;
				  imageHTML += '<td></td>';
				  x++;
			  }
		  }
		
	}
	imageHTML += '</tr>'
	holder.innerHTML = '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+imageHTML+'   </table>';
	currentFolder = folder;
	
}
function preview(imgXml) {
	holder = document.getElementById('preview');
	holder.innerHTML = '<table width="100%" border="1" cellspacing="0" cellpadding="1">'
	+'  <tr>'
	+'    <td><strong>'+imgXml.getElementsByTagName('name')[0].firstChild.nodeValue+'</strong></td>'
	+'    <td><div align="center"><strong>'+imgXml.getElementsByTagName('type')[0].firstChild.nodeValue+'</strong></div></td>'
	+'    <td><div align="right">'+imgXml.getElementsByTagName('size')[0].firstChild.nodeValue+' kb</div></td>'
	+'  </tr>'
	+'  <tr>'
	+'    <td>Dimensions</td>'
	+'    <td colspan="2"><div align="center">'+imgXml.getElementsByTagName('width')[0].firstChild.nodeValue + ' px by '+imgXml.getElementsByTagName('height')[0].firstChild.nodeValue+' px</div></td>'
	+'  </tr>'
	+'</table>'
	+'<img src="'+imgXml.getAttribute("id").replace('../','http://www.cellardoorproductions.ca/')+'" />';
}
function uploading() {
	uploadingPrompt = new customPrompt('alert', document.getElementById('dimmerFront'), document.getElementById('dimmerBack'));
	uploadingPrompt.doNotShowButton = true;
	uploadingPrompt.setFadeTime(225);
	uploadingPrompt.setMsg('<p align="center"><strong>Please wait while the image uploads</strong></p>');
	uploadingPrompt.setOpacity(0.55);
	uploadingPrompt.setTitle(' ');
	uploadingPrompt.init();
}
function stopUploading(msg) {
	alert(msg);
	uploadingPrompt.resetFade(1);
	uploadingPrompt.fade();
	getImage();
	setImageHtml(document.getElementById('folder').value);
	
}
//OOP Model of the Textbox
function textBox(div) {
	this.div = div;
	this.boxHeight = div.offsetHeight + 'px';
	this.boxWidth = div.offsetWidth + 'px';
	this.toolBar = new customPrompt('custom', document.getElementById('toolBarFloater_front'), document.getElementById('toolBarFloater'));
}

textBox.prototype.Edit = function() {
	
	
	this.holder = this.div.innerHTML, area = "tb_" + this.div.id, bodyTag="<BODY MONOSPACE STYLE=\"font:10pt arial,sans-serif\">";
	this.div.innerHTML = '<iframe class="myText" style="width:'+this.boxWidth+'; height:'+this.boxHeight+';" id="'+area+'" ALLOWTRANSPARENCY="true"></iframe>';
	this.iframe = document.getElementById(area);
	this.frame = this.iframe.contentWindow;
	this.frame.document.open();
	this.frame.document.write('<link href="style/styleIFRAME.css" rel="stylesheet" type="text/css">');
	this.frame.document.write(bodyTag);
	this.frame.document.write(this.holder);
	try { this.frame.setAttribute("allowTransparency","true"); } catch(e) {
		try {  this.iframe.setAttribute("allowTransparency","true"); } catch(e) {
			alert('Didn\'t work!');
		}
	}
	this.frame.document.close();
	this.frame.document.designMode="On";
	this.iframe.focus();
	
	
	inHTML = '<table style="display: inline-table;" border="0" cellpadding="0" cellspacing="0" width="600">'
+'<!-- fwtable fwsrc="toolbar.png" fwpage="Page 1" fwbase="toolbar.gif" fwstyle="Dreamweaver" fwdocid = "2097613855" fwnested="0" -->'
+'<tr>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="55" height="1" border="0" alt="" /></td>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="54" height="1" border="0" alt="" /></td>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="60" height="1" border="0" alt="" /></td>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="55" height="1" border="0" alt="" /></td>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="65" height="1" border="0" alt="" /></td>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="58" height="1" border="0" alt="" /></td>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="122" height="1" border="0" alt="" /></td>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="87" height="1" border="0" alt="" /></td>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="44" height="1" border="0" alt="" /></td>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="1" height="1" border="0" alt="" /></td>'
+'</tr>'
+'<tr>'
+'<td colspan="8"><div id="dragHandler" class="mover"><img name="toolbar_r1_c1" src="http://cellardoorproductions.ca/images/toolbar_r1_c1.gif" width="556" height="40" border="0" id="toolbar_r1_c1" alt="" /></div></td>'
+'<td><img name="toolbar_r1_c9" src="http://cellardoorproductions.ca/images/toolbar_r1_c9.gif" width="44" height="40" border="0" id="toolbar_r1_c9" onclick="cancelOldTextBox()" alt="" /></td>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="1" height="40" border="0" alt="" /></td>'
+'</tr>'
+'<tr>'
+'<td><img name="toolbar_r2_c1" src="http://cellardoorproductions.ca/images/toolbar_r2_c1.gif" width="55" height="46" border="0" id="toolbar_r2_c1" onclick="RunCom(\'bold\')" alt="" /></td>'
+'<td><img name="toolbar_r2_c2" src="http://cellardoorproductions.ca/images/toolbar_r2_c2.gif" width="54" height="46" border="0" id="toolbar_r2_c2" onclick="RunCom(\'italic\')" alt="" /></td>'
+'<td><img name="toolbar_r2_c3" src="http://cellardoorproductions.ca/images/toolbar_r2_c3.gif" width="60" height="46" border="0" id="toolbar_r2_c3" onclick="RunCom(\'underline\')" alt="" /></td>'
+'<td><img name="toolbar_r2_c4" src="http://cellardoorproductions.ca/images/toolbar_r2_c4.gif" width="55" height="46" border="0" id="toolbar_r2_c4" onclick="RunCom(\'justifyleft\')" alt="" /></td>'
+'<td><img name="toolbar_r2_c5" src="http://cellardoorproductions.ca/images/toolbar_r2_c5.gif" width="65" height="46" border="0" id="toolbar_r2_c5" onclick="RunCom(\'justifycenter\')" alt="" /></td>'
+'<td><img name="toolbar_r2_c6" src="http://cellardoorproductions.ca/images/toolbar_r2_c6.gif" width="58" height="46" border="0" id="toolbar_r2_c6" onclick="RunCom(\'justifyright\')" alt="" /></td>'
+'<td><img name="toolbar_r2_c7" src="http://cellardoorproductions.ca/images/toolbar_r2_c7.gif" width="122" height="46" border="0" id="toolbar_r2_c7" onclick="text.createLink()" alt="" /></td>'
+'<td colspan="2"><img name="toolbar_r2_c8" src="http://cellardoorproductions.ca/images/toolbar_r2_c8.gif" width="131" height="46" border="0" id="toolbar_r2_c8" onclick="getImage()" alt="" /></td>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="1" height="46" border="0" alt="" /></td>'
+'</tr>'
+'<tr>'
+'<td colspan="7" width="469" background="http://cellardoorproductions.ca/images/toolbar_r3_c1.gif"><strong>&nbsp;&nbsp;&nbsp;&nbsp;Font color: <span style="color:#b9e1f8;" onclick="changeColor(\'#b9e1f8\')">[blue]</span> <span style="color:#ffd96d;" onclick="changeColor(\'#ffd96d\')">[yellow]</span> <span style="color:#939598;" onclick="changeColor(\'#939598\')">[normal]</span></strong></td>'
+'<td colspan="2"><img name="toolbar_r3_c8" src="http://cellardoorproductions.ca/images/toolbar_r3_c8.gif" width="131" height="59" border="0" id="toolbar_r3_c8" onclick="saveOldTextBox()" alt="" /></td>'
+'<td><img src="http://cellardoorproductions.ca/images/spacer.gif" width="1" height="59" border="0" alt="" /></td>'
+'</tr>'
+'</table>';
	this.toolBar.setInnerHTML(inHTML);
	this.toolBar.setFadeTime(150);
	this.toolBar.setFadeColor('#000');
	this.toolBar.setOpacity(0.55);
	this.toolBar.init();
	this.toolBarDynSec = document.getElementById('toolBatExtra');
	makeDraggable(this.toolBar.getToolbarFloater(), document.getElementById('dragHandler'))
	
}
textBox.prototype.showDyNPartList = function(div) {
	this.toolBarDynSec.innerHTML = '<table width="100%" border="0" cellspacing="0" cellpadding="0">'
+'  <tr>'
+'    <td><a onclick="insert(\'clientName\')">Client Name</a></td>'
+'  </tr>'
+'  <tr>'
+'    <td><a onclick="insert(\'clientEmail\')">Client Email<a onclick=""></td></tr>'
+'</table>';
}
textBox.prototype.showTextList = function(div) {
	this.toolBarDynSec.innerHTML = '<table width="100%" border="0" cellspacing="0" cellpadding="0">'
+'  <tr>'
+'    <td>Font Type</td>'
+'    <td>Font Size</td>'
+'    <td>Font Color</td>'
+'  </tr>'
+'  <tr>'
+'    <td><select onchange="RunCom(\'FontName\',this.value)">'
+'		<option value="Verdana">Verdana</option>'
+'		<option value="Arial">Arial</option>'
+'		<option value="time new roman">Times New Roman</option>'
+'		<option value="Tahoma">Tahoma</option></select></td>'
+'    <td><select onchange="RunCom(\'FontSize\',this.value)">'
+'		<option value="1">1</option>'
+'		<option value="2">2</option>'
+'		<option value="3">3</option>'
+'		<option value="4">4</option>'
+'		<option value="5">5</option>'
+'		<option value="6">6</option>'
+'		<option value="7">7</option>'
+'		<option value="8">8</option></select></td>'
+'    <td><select onchange="RunCom(\'forecolor\',this.value)">'
+'		<option value="#000000">Black</option>'
+'		<option value="#FFFFFF">White</option></select></td>'
+'  </tr>'
+'</table>';
}
function changeColor(colour)
{
	text.frame.document.execCommand("foreColor",false,colour);
  
  pureText = false;
  text.frame.focus();
}
function insert(part) {
	switch(part) {
		case 'clientName':
			text.addHtml('[name]');
			text.updateDynParts = true;
		break;
		case 'clientEmail':
			text.addHtml('[email]');
			text.updateDynParts = true;
		break;
		default:
		alert('Pending');
		break;
	}
}
textBox.prototype.Save = function() {
	
	if(!createXmlHttpRequestObject) {
		alert('Need AJAX enabled');
		return false;
	}
	newHTML = this.getHtml();
	
	xmlHttp = new createXmlHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if(xmlHttp.readyState != 4) return false;
		if(xmlHttp.responseText) alert("Save error:" + xmlHttp.responseText);
		if(xmlHttp.status != '400' && xmlHttp.status != 400) {
		}
	}
	/*hiddenForm = '<form action="../backbone/textBox.php" target="fileUpload" id="hiddenForm" method="post" enctype="multipart/form-data">'
	+'<input name="page" type="hidden" value="'+globalPageValue+'" />'
	+'<input name="id" type="hidden" value="'+this.div.id+'" />'
	+'<input name="html" type="hidden" value="'+newHTML+'" /></form>';
	document.getElementById('toolBarFloater_front').innerHTML += hiddenForm;
	document.getElementById(hiddenForm).submit();*/
	//.replace('=','[equal]').replace('&','[and]')
	if(globalPageValue == 'web')
	{
		info = this.div.id.split("#");
		parameters = 'page='+info[0] +'&area='+info[1]+'&id='+info[2]+'&html='+safeText(newHTML)+'&action=save';
	}
	else if(globalPageValue == 'books')
	{
		info = this.div.id.split("#");
		parameters = 'page='+info[0] +'&area='+info[1]+'&id='+info[2]+'&html='+safeText(newHTML)+'&action=save';
	}
	else if(globalPageValue == 'television_sub')
	{
		info = this.div.id.split("#");
		parameters = 'page='+info[0] +'&area='+info[1]+'&id='+info[2]+'&html='+safeText(newHTML)+'&action=save';
	}
	else if(globalPageValue == 'appearances')
	{
		info = this.div.id.split("#");
		parameters = 'page='+info[0] +'&area='+info[1]+'&id='+info[2]+'&html='+safeText(newHTML)+'&action=save';
	}
	else if(globalPageValue == 'restaurants')
	{
		info = this.div.id.split("#");
		parameters = 'page='+info[0] +'&area='+info[1]+'&id='+info[2]+'&html='+safeText(newHTML)+'&action=save';
	}
	else
	{
		if(globalPageValue == "home")
			parameters = 'page=index&id='+this.div.id+'&html='+safeText(newHTML)+'&action=save';
		else
			parameters = 'page='+globalPageValue+'&id='+this.div.id+'&html='+safeText(newHTML)+'&action=save';

	}
		
	xmlHttp.open("POST", '../backbone/textBox.php', true);
	try {  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); } catch(e) { alert("Content type NA - " + e); }
    try { xmlHttp.setRequestHeader("Content-length", parameters.length); } catch(e) { alert("Content length NA - " + e); }
    try { xmlHttp.setRequestHeader("Connection", "close"); } catch(e) { alert("connection type NA - " + e); }
	try { xmlHttp.overrideMimeType('text/html'); } catch(e) {  }
	xmlHttp.send(parameters);
		
	this.div.innerHTML = newHTML;
}
textBox.prototype.Cancel = function() {

	this.div.innerHTML = this.holder;
	
}
textBox.prototype.setText = function(text) {
	text = text.replace(/\n/g, "<br>")
	this.frame.document.body.innerHTML=text;
}
textBox.prototype.getText = function() {
	return this.frame.document.body.innerText;
}
textBox.prototype.setHtml = function(html) {
  	this.frame.document.body.innerHTML=html;
}
textBox.prototype.addHtml = function(html) {
  	this.frame.document.body.innerHTML=this.frame.document.body.innerHTML + html;
}
textBox.prototype.getHtml = function() {
	return this.frame.document.body.innerHTML;
}
textBox.prototype.getFocus = function() {
	this.frame.focus();
}
textBox.prototype.InitBtn = function(btn) {
  	btn.onclick = textBox.CMD(btn.id);
}
function RunCom (what,opt) {

  if (!opt) text.frame.document.execCommand(what, false, null);
  else text.frame.document.execCommand(what,false,opt);
  
  pureText = false;
  text.frame.focus();
  
}
function insertSpecialCharacter(char)
{
	//IE support

  if (text.frame.document.selection) {

    text.frame.focus();

    sel = text.frame.document.selection.createRange();

    sel.text = char;

  }

  //MOZILLA/NETSCAPE support

  else if (text.frame.selectionStart || text.frame.selectionStart == '0') {

    var startPos = text.frame.selectionStart;

    var endPos = text.frame.selectionEnd;

    text.frame.value = text.frame.value.substring(0, startPos)

                  + char

                  + text.frame.value.substring(endPos, char.value.length);

  } else {

    text.frame.value += char;

  }
}
function getEl(sTag,start) {
 //Copy the selected character to "start" string while start!=NULL && tagName doesn't have 'A' 
  while ((start!=null) && (start.tagName!=sTag)) start = start.parentElement;
  return start;
}
textBox.prototype.createLink = function() {
	this.linkPrompt = new customPrompt('link', document.getElementById('dimmerFront'), document.getElementById('dimmerBack'));
	this.linkPrompt.setFadeTime(250);
	this.linkPrompt.setOpacity(55);
	this.linkPrompt.init();
	sel = (window.getSelection)? this.frame.window.getSelection() : this.frame.document.selection.createRange();
	
}
function genClientLogin() {
	return '<table width="300" border="0" cellpadding="0" cellspacing="0">'
 +'                 <tr>'
 +'                   <td width="77">Client Name</td>'
 +'                   <td width="213"><div align="center">'
 +'                     <input type="text" name="clientName" id="clientName">'
 +'                   </div></td>'
 +'                 </tr>'
 +'                 <tr>'
 +'                   <td>Password</td>'
 +'                   <td><div align="center">'
 +'                     <input type="password" name="clientPassword" id="clientPassword">'
 +'                   </div></td>'
 +'                 </tr>'
 +'                 <tr>'
 +'                   <td colspan="2"><div align="center">'
 +'                     <input type="button" name="Login" id="Login" value="Login" onClick="login()">'
 +'                   </div></td>'
 +'                   </tr>'
 +'               </table>'
 +'                 <strong><p id="loginErrorMsg" align="center">&nbsp;</p></strong>'
 +'                   <p><br>'
 +'                   </p>';
}
function getElementByIdMXL(the_node,the_id) {
	//get all the tags in the doc
	try {
		node_tags = the_node.getElementsByTagName('*');
	for (i=0;i<node_tags.length;i++) {
	//is there an id attribute?
			if (node_tags[i].hasAttribute('id')) {
				//if there is, test its value
				if (node_tags[i].getAttribute('id') == the_id) {
					//and return it if it matches
					return node_tags[i];
				}
			}
		}
	} catch(e) {  }
}
// JavaScript Document
