// Filename: incJSFuncs.js
// Copyright: 2002-4 CreativEngine Pty. Ltd.

// Browser Detect Lite  v2.1
// http://www.dithered.com/javascript/browser_detect/index.html
// modified by Chris Nott (chris@NOSPAMdithered.com - remove NOSPAM)
//
// modified by Michael Lovitt to include OmniWeb and Dreamcast

function BrowserDetectLite() {
	var ua = navigator.userAgent.toLowerCase(); 
	this.ua = ua;

	// browser name
	this.isGecko     = (ua.indexOf('gecko') != -1);
	this.isMozilla   = (this.isGecko && ua.indexOf("gecko/") + 14 == ua.length);
	this.isNS        = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('opera') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
	this.isIE        = ( (ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1) ); 
	this.isOpera     = (ua.indexOf("opera") != -1); 
	this.isKonqueror = (ua.indexOf("konqueror") != -1); 
	this.isIcab      = (ua.indexOf("icab") != -1); 
	this.isAol       = (ua.indexOf("aol") != -1); 
	this.isWebtv     = (ua.indexOf("webtv") != -1); 
	this.isOmniweb   = (ua.indexOf("omniweb") != -1);
	this.isDreamcast   = (ua.indexOf("dreamcast") != -1);
	
	// spoofing and compatible browsers
	this.isIECompatible = ( (ua.indexOf("msie") != -1) && !this.isIE);
	this.isNSCompatible = ( (ua.indexOf("mozilla") != -1) && !this.isNS && !this.isMozilla);
	
	// browser version
	this.versionMinor = parseFloat(navigator.appVersion); 
	
	// correct version number for NS6+ 
	if (this.isNS && this.isGecko) {
		this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('/') + 1 ) );
	}
	
	// correct version number for IE4+ 
	else if (this.isIE && this.versionMinor >= 4) {
		this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
	}
	
	// correct version number for Opera 
	else if (this.isOpera) {
		if (ua.indexOf('opera/') != -1) {
			this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera/') + 6 ) );
		}
		else {
			this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera ') + 6 ) );
		}
	}
	
	// correct version number for Konqueror
	else if (this.isKonqueror) {
		this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
	}
	
	// correct version number for iCab 
	else if (this.isIcab) {
		if (ua.indexOf('icab/') != -1) {
			this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab/') + 6 ) );
		}
		else {
			this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab ') + 6 ) );
		}
	}
	
	// correct version number for WebTV
	else if (this.isWebtv) {
		this.versionMinor = parseFloat( ua.substring( ua.indexOf('webtv/') + 6 ) );
	}
	
	this.versionMajor = parseInt(this.versionMinor); 
	this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
	
	// platform
	this.isWin   = (ua.indexOf('win') != -1);
	this.isWin32 = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1) );
	this.isMac   = (ua.indexOf('mac') != -1);
	this.isUnix  = (ua.indexOf('unix') != -1 || ua.indexOf('linux') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
	
	// specific browser shortcuts
	this.isNS4x = (this.isNS && this.versionMajor == 4);
	this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
	this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
	this.isNS4up = (this.isNS && this.versionMinor >= 4);
	this.isNS6x = (this.isNS && this.versionMajor == 6);
	this.isNS6up = (this.isNS && this.versionMajor >= 6);
	
	this.isIE4x = (this.isIE && this.versionMajor == 4);
	this.isIE4up = (this.isIE && this.versionMajor >= 4);
	this.isIE5x = (this.isIE && this.versionMajor == 5);
	this.isIE55 = (this.isIE && this.versionMinor == 5.5);
	this.isIE5up = (this.isIE && this.versionMajor >= 5);
	this.isIE6x = (this.isIE && this.versionMajor == 6);
	this.isIE6up = (this.isIE && this.versionMajor >= 6);
	
	this.isIE4xMac = (this.isIE4x && this.isMac);
}
var browser = new BrowserDetectLite();


//---------------------------------------------------------------
// load the netscape stylesheet this overrides some of the styles
// Netscape can't handle
//---------------------------------------------------------------
if ( browser.isNS4x ){
	document.write('<link href="css/defaultNS.css" rel="stylesheet" type="text/css">')
}


//---------------------------------------------------------------


//---------------------------------------------------------------
// Generic form validation functions
//---------------------------------------------------------------
  function clearq(text) {
    var old = "";
    var clear = text;
    var dudDash = "";
    
    dudDash = String.fromCharCode(8211);
    
    while(clear != old){
      old = clear;
    	clear = clear.replace("’", "'");
    	clear = clear.replace(dudDash, "-");
    }
		    
    return clear;
  }

function isEmpty(strValue) {
	if (strValue.length > 0)
		return false;
	return true;
}

function isEmail(strVal) {
	var strRegTestInteger = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	return strRegTestInteger.test(strVal);
}

function isPassword(strVal) {
	var strRegTestInteger = /^\w{6,}$/;
	return strRegTestInteger.test(strVal);
}

function isChecked(objField) {
	return objField.checked;
}

function notValidDate(intDay, intMonth, intYear) {
	/* returns 0 if date is valid
	** returns 1 if general invalid date
	** returns 2 if leap year broken
	*/
	switch (intMonth) {
		case "4":
		case "6":
		case "9":
		case "11":
			if (parseInt(intDay) > 30)
				return 1;
			break;
		case "2":
			if (parseInt(intDay) > 29)
				return 1;

			if (parseInt(intDay) == 29) {
				if (intYear.substr(intYear.length-2, intYear.length) == "00") {
					if (parseInt(intYear.substr(0, 2)) % 4 == 0)
						return 0;
					return 2;
				}
				else
					if ((parseInt(intYear) % 4) == 0)
						return 0;
					return 2;
			}
			else
				return 0;
	}
	return 0;
}

function openWin(strFilename, strWindowName, intWidth, intHeight) {
	var newWin = window.open(strFilename, strWindowName, "toolbar=0, location=0, directory=0, status=0, menubar=0, scrollbars=1, resizable=1, width=" + intWidth + ", height=" + intHeight);
}

function markupText(objField, strTag) {
	//Called by admin textbox format buttons
	var strSelectedText = document.selection.createRange().text;

	if (isEmpty(strSelectedText)){
		alert("You must highlight some text in the textbox first.");
		objField.focus();
		return false;
	}
	else {
		document.selection.createRange().text = "<" + strTag + ">" + strSelectedText + "</" + strTag + ">";
		objField.focus();
	}
}

function replaceLIs(strText) {
	var strRet = "";

	for (var i=strText.indexOf("\n") ; strText!="" ; i=strText.indexOf("\n")) {
		if (i == -1) {
			strLine = strText;
			strText = "";
			strRet = strRet + strLine.replace(/^(.*)$/, "<li>$1</li>");
		}
		else {
			strLine = strText.substr(0, i);
			strText = strText.substr(i+1, strText.length);
			strRet = strRet + strLine.replace(/^(.*)\r$/, "<li>$1</li>\n");
		}
	}
	return strRet;
}

function markupTextList(objField, strTag) {
	var strSelectedText = document.selection.createRange().text;

	if (isEmpty(strSelectedText)){
		alert("You must highlight some text in the textbox first.");
		objField.focus();
		return false;
	}
	else {
		document.selection.createRange().text = "<" + strTag + ">\n" + replaceLIs(strSelectedText) + "\n</" + strTag + ">";
		objField.focus();
	}
}

function markupTextLink(objField) {
	var strSelectedText = document.selection.createRange().text;

	if (isEmpty(strSelectedText)){
		alert("You must highlight some text in the textbox first.");
		objField.focus();
		return false;
	}
	else {
		var strURL = prompt("URL:", "http://");
		if (strURL != null)
			document.selection.createRange().text = "<a href=\"" + strURL + "\">" + strSelectedText + "</a>";
		objField.focus();
	}
}

function markupTextMailTo(objField) {
	var strSelectedText = document.selection.createRange().text;

	if (isEmpty(strSelectedText)){
		alert("You must highlight some text in the textbox first.");
		objField.focus();
		return false;
	}
	else {
		var strURL = prompt("Email Address:", "mailto:");
		if (strURL != null)
			document.selection.createRange().text = "<a href=\"" + strURL + "\">" + strSelectedText + "</a>";
		objField.focus();
	}
}


function markupTextEscapeChars(objField) {
	var strSelectedText = document.selection.createRange().text;
	var reQuote = /\"/g;
	reQuote.ignoreCase = true;
	reQuote.multiline = true;

	var reLT = /</g;
	reLT.ignoreCase = true;
	reLT.multiline = true;

	var reGT = />/g;
	reGT.ignoreCase = true;
	reGT.multiline = true;

	if (isEmpty(strSelectedText)){
		alert("You must highlight some text in the textbox first.");
		objField.focus();
		return false;
	}
	else {
		document.selection.createRange().text = strSelectedText.replace(reQuote, "&quot;").replace(reLT, "&lt;").replace(reGT, "&gt;");
		objField.focus();
	}
}
//---------------------------------------------------------------


//---------------------------------------------------------------
// Opacity Displayer, Version 1.0
// Copyright Michael Lovitt, 6/2002.
// Distribute freely, but please leave this notice intact.
//---------------------------------------------------------------

//---------------------------------------------------------------
// OPACITY OBJECT
//
// Instantiates the object, defines the properties and methods.
function OpacityObject(divId, strPath) {
	this.id = divId;
	this.path = strPath;
	if (ns){
		if (browserVersion>=5) {
			this.layerObject = document.getElementById(divId).style;
		} else { 
			this.layerObject = eval("document."+divId);
		}
	} else {
		this.layerObject = eval(divId + ".style");
	}
	this.setBackground = od_object_setBackground;
}
// Uses AlphaImageLoader filter, or the css background property,
// as appropriate, to apply a PNG or GIF as the background of the layerObject.
function od_object_setBackground() {
	if (pngAlpha) {
		this.layerObject.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.path+".png', sizingMethod='scale')";
	} else if (pngNormal) {
		this.layerObject.backgroundImage = 'url('+this.path+'.png)';
	} else {
		this.layerObject.backgroundImage = 'url('+this.path+'.gif)';
	}
}
//---------------------------------------------------------------

//---------------------------------------------------------------
// OPACITY DISPLAY FUNCTION
// Outputs the image as a div with the AlphaImageLoader, or with
// a standard image tag.
function od_displayImage(strId, strPath, intWidth, intHeight, strClass, strAlt, strURL) {
	var displayString = '<div  id="'+strId+'" class="'+strClass+'"';
	if ( strURL != "" || strURL != null ){
		displayString += ' onClick="parent.window.location=\''+strURL+'\'"';
	}
	if (pngAlpha) {
		displayString += ' style="height:'+intHeight+'px;width:'+intWidth+'px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+strPath+'.png\', sizingMethod=\'scale\')"></div>';
	} else if (pngNormal) {
		displayString += ' ><img src="'+strPath+'.png" width="'+intWidth+'" height="'+intHeight+'" name="'+strId+'" border="0" class="'+strClass+'" alt="'+strAlt+'" /></div>';
	} else {
		displayString += ' ><img src="'+strPath+'.gif" width="'+intWidth+'" height="'+intHeight+'" name="'+strId+'" border="0" class="'+strClass+'" alt="'+strAlt+'" /></div>';
	}
	document.write(displayString);
}
//---------------------------------------------------------------

//---------------------------------------------------------------
// OPACITY ROLL-OVER FUNCTIONS
function od_rollOver(strId, strColor) {	
	if (pngAlpha) {
		document.getElementById(strId).style.backgroundColor = strColor;
	} else {
	    if (document.images && (flag == true)) {
	        document[strId].src = eval(strId + "on.src");
	    }
	}
}
function od_rollOut(strId, strColor) {	
	if (pngAlpha) {
		document.getElementById(strId).style.backgroundColor = strColor;
	} else {
	    if (document.images) {
	        document[strId].src = eval(strId + "off.src");
	    }
	}
}
//---------------------------------------------------------------

//---------------------------------------------------------------
// global variables

// if IE5.5+ on win32, then display PNGs with AlphaImageLoader
if ((browser.isIE55 || browser.isIE6up) && browser.isWin32) {
	var pngAlpha = true;
	var strExt = ".png";
// else, if the browser can display PNGs normally, then do that. that list includes:
	//     -Gecko Engine: Netscape 6 or Mozilla, Mac or PC
	//     -IE5+ Mac (OpacityObject applies the background image at 100% opacity)
	//     -Opera 6+ PC
	//     -Opera 5+ Mac (Doesn't support dynamically-set background images)
	//     -Opera 6+ Linux 
	//     -Omniweb 3.1+ 
	//     -Icab 1.9+ 
	//     -WebTV 
	//     -Sega Dreamcast
} else if ((browser.isGecko) || (browser.isIE5up && browser.isMac) || (browser.isOpera && browser.isWin && browser.versionMajor >= 6) || (browser.isOpera && browser.isUnix && browser.versionMajor >= 6) || (browser.isOpera && browser.isMac && browser.versionMajor >= 5) || (browser.isOmniweb && browser.versionMinor >= 3.1) || (browser.isIcab && browser.versionMinor >= 1.9) || (browser.isWebtv) || (browser.isDreamcast)) {
	var pngNormal = true;
	var strExt = ".png";
	// otherwise, we use plain old GIFs
} else {
	var strExt = ".gif";
}

var ns = (document.all)?false:true;
var browserVersion = parseFloat(navigator.appVersion );
//---------------------------------------------------------------






/*
****************************************************************
NEW WYSIWYG FUNCTIONS \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/  \/ 
****************************************************************
*/

var selectionRange;

objWYSIWYG.Registry = [];

function objWYSIWYG(pstrIFrameId, pstrTextAreaId, pstrPath, popstrPath){
	this.id = pstrIFrameId;
	this.design = true;
	this.objIframe = window[pstrIFrameId].document;
	this.objTarea = document.getElementById(pstrTextAreaId);
	this.cssPath = "<link href=\"" + pstrPath + "../css/admin.css\" rel=\"stylesheet\" type=\"text/css\">"
	this.cssPathExtra = "<BODY style=\"background='..images/white.gif'\">"
	this.imagePath = pstrPath;
	if(popstrPath)
	  this.popUpPath = popstrPath;
	else
	  this.popUpPath = pstrPath;
	  
	objWYSIWYG.Registry[pstrIFrameId] = this;
	
	this.load();
	
}

// Inherited Method - Sets up editable area

objWYSIWYG.prototype.load = function() {
	
	var editControl_RichText = frames [this.id].document;
	this.objIframe.designMode = "On";
	
	this.objIframe.open();
	this.objIframe.write(this.cssPath + this.cssPathExtra + this.objTarea.value);
	this.objIframe.close();
	
	var tempVar = this.id;
	
	editControl_RichText.documentElement.attachEvent("onpaste", function(){objWYSIWYG.pasteWordText(tempVar);});
	
} 

objWYSIWYG.getIFrameDocument = function(aID)
{
	return document.frames[aID].document;
}

objWYSIWYG.pasteWordText = function(id) {
	
	
	var reg = objWYSIWYG.Registry;
	var obj = objWYSIWYG.Registry[id];
	
	//selectionRange = objWYSIWYG.getIFrameDocument(id).selection.createRange().duplicate();
	
	var strReturnString = "";

	document.getElementById("tempArea" + obj.id).value = window.clipboardData.getData("Text")
	
	strReturnString = document.getElementById("tempArea" + obj.id).value;
	
	strReturnString = strReturnString.replace(/\r/g, '\n');
	
	window.clipboardData.setData("Text", strReturnString)
	
	setTimeout('updateClipboard("' + obj.id + '")', 50) 
}

function updateClipboard(id){
	//alert(document.getElementById("tempArea" + id))
	window.clipboardData.setData("Text", document.getElementById("tempArea" + id).value)

}

// Method - Switched between HTML and Design Mode

objWYSIWYG.setAbbv = function(pstrFrameId) {
	selectionRange = objWYSIWYG.getIFrameDocument(pstrFrameId).selection.createRange().duplicate();
	var newString = selectionRange.htmlText;
	if ( newString.length > 0 ){
		var pstrTitle = prompt("Please enter the full description of " + newString + ":", "") ;
		if (pstrTitle != null)
			selectionRange.pasteHTML("<acronym title=\"" + pstrTitle + "\">" + newString + "</acronym>");
	}else
		alert("Please select some text first.");
}

objWYSIWYG.toggleDesign = function(id, objIFrame) {
	
	var reg = objWYSIWYG.Registry;
	var obj = objWYSIWYG.Registry[id];
	
	if (obj.design) {
		obj.design = false;
		var sContents = objIFrame.document.body.innerHTML;
		objIFrame.document.open();
		objIFrame.document.write("<BODY style=\"font:9pt courier, monospace\">");
		objIFrame.document.close();
		objIFrame.document.body.innerText = sContents;
	} else {
		obj.design = true;
		var sContents = objIFrame.document.body.innerText;
		objIFrame.document.open();
		objIFrame.document.write(obj.cssPath + obj.cssPathExtra);
		objIFrame.document.close();
		objIFrame.document.body.innerHTML = sContents;
	}
	
}

objWYSIWYG.setImageAlignment = function(pstrFrameId, alignment) {
	selectionRange = objWYSIWYG.getIFrameDocument(pstrFrameId).selection.createRange().duplicate();
	var newString = selectionRange.htmlText;
	if ( newString.length > 0 ){
		if (alignment == "left")
			selectionRange.pasteHTML("<div class=\"floatimageleft\">" + newString + "</div>");
		else
			selectionRange.pasteHTML("<div class=\"floatimageright\">" + newString + "</div>");
	}else
		alert("Please select some text first.");	
}

objWYSIWYG.setTitle = function(pstrTag, pstrFrameId) {
	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("FormatBlock", 0, "<" + pstrTag + ">");
}

objWYSIWYG.setColor = function(pstrHexColor, pstrFrameId) {
	if (pstrHexColor != "") {
		objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("bold");
		objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("ForeColor", 0, pstrHexColor);
	} else {
		objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("RemoveFormat");
	}
}

objWYSIWYG.setSpan = function(pstrTag, pstrAttr, pstrFrameId) {
	selectionRange = objWYSIWYG.getIFrameDocument(pstrFrameId).selection.createRange().duplicate();
	
	if (pstrAttr == "") {
		
		oldString = selectionRange.htmlText;
		
		//alert(oldString)
		
		var newString = "";
   		var inTag = false;
   		for(var i = 0; i < oldString.length; i++) {
   
        	if(oldString.charAt(i) == '<') inTag = true;
        	if(oldString.charAt(i) == '>') {
              	inTag = false;
              	i++;
        	}
   
        	if(!inTag) newString += oldString.charAt(i);

   		}
		
		// Hack to remove non standard tags
		selectionRange.pasteHTML("<span id=tempId>" + newString + "</span>");
		objWYSIWYG.getIFrameDocument(pstrFrameId).getElementById("tempId").parentNode.removeNode(false)
		objWYSIWYG.getIFrameDocument(pstrFrameId).getElementById("tempId").removeNode(false)
		
	} else {
		//selectionRange.htmlText
		

		selectionRange.pasteHTML("<" + pstrTag + " class='" + pstrAttr + "'>" + selectionRange.text + "</" + pstrTag + ">");
		tempStr = objWYSIWYG.getIFrameDocument(pstrFrameId).body.innerHTML;
		var objRegExp = new RegExp('<SPAN class=highlightgreen>' + selectionRange.text + '</SPAN>', 'gi') ;
		tempStr = tempStr.replace(objRegExp, '');
		objWYSIWYG.getIFrameDocument(pstrFrameId).body.innerHTML = tempStr;
	}
}

objWYSIWYG.setBold = function(pstrFrameId) {
	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("bold");
}


objWYSIWYG.cut_click = function(pstrFrameId) {
	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("cut");
}

objWYSIWYG.copy_click = function(pstrFrameId) {
	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("copy");
}

objWYSIWYG.paste_click = function(pstrFrameId) {
	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("paste");
}

objWYSIWYG.setItalic = function(pstrFrameId) {
	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("italic");
}

objWYSIWYG.setUnorderedList = function(pstrFrameId) {
	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("insertunorderedlist");
}

objWYSIWYG.setOrderedList = function(pstrFrameId) {
  	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("insertorderedlist");
}

objWYSIWYG.setHorizontalLine = function(pstrFrameId) {
  	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("inserthorizontalrule");
}

objWYSIWYG.setLinkExternal = function(pstrFrameId) {
  var objField = objWYSIWYG.getIFrameDocument(pstrFrameId);
	var strSel = objField.selection.createRange().text;
	if(strSel.length > 0){
    objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("CreateLink", true, false);
  }else{
    alert('Please select some text first.');
  }
}

objWYSIWYG.table_create_click = function(editor, sender, pDir)
  {
    if (window.frames[editor].document.selection.type != "Control")
    {
      // selection is not a control => insert table 
	  var nt = showModalDialog(pDir + '../includes/table.asp?lang=' + document.all['SPAW_'+editor+'_lang'].value + '&theme=' + document.all['SPAW_'+editor+'_theme'].value, null, 
        'dialogHeight:250px; dialogWidth:366px; resizable:no; status:no');  
       
      if (nt)
      {
        window.frames[editor].focus();     
    
        var newtable = document.createElement('TABLE');
        //try {
          newtable.width = (nt.width)?nt.width:'';
          newtable.height = (nt.height)?nt.height:'';
          newtable.border = (nt.border)?nt.border:'';
          if (nt.cellPadding) newtable.cellPadding = nt.cellPadding;
          if (nt.cellSpacing) newtable.cellSpacing = nt.cellSpacing;
          newtable.bgColor = (nt.bgColor)?nt.bgColor:'';
          
          // create rows
          for (i=0;i<parseInt(nt.rows);i++)
          {
            var newrow = document.createElement('TR');
            for (j=0; j<parseInt(nt.cols); j++)
            {
              var newcell = document.createElement('TD');
              newrow.appendChild(newcell);
            }
            newtable.appendChild(newrow);
          }
          var selection = window.frames[editor].document.selection.createRange();
        	selection.pasteHTML(newtable.outerHTML);      
          SPAW_toggle_borders(editor, window.frames[editor].document.body, null);
          //SPAW_update_toolbar(editor, true);    
        //}
       // catch (excp)
       // {
       //   alert('error');
      // }
      }
    }
  }


objWYSIWYG.table_prop_click = function(editor, sender, pDir)
  {
    window.frames[editor].focus();     

    var tTable
    // check if table selected
    if (window.frames[editor].document.selection.type == "Control")
    { 
      var tControl = window.frames[editor].document.selection.createRange();
      if (tControl(0).tagName == 'TABLE')
      {
        tTable = tControl(0);
      }
    }
    else
    {
      var tControl = window.frames[editor].document.selection.createRange();
      tControl = tControl.parentElement();
      while ((tControl.tagName != 'TABLE') && (tControl.tagName != 'BODY'))
      {
        tControl = tControl.parentElement;
      }
      if (tControl.tagName == 'TABLE')
        tTable = tControl;
      else
        return false;
    }

    var tProps = {};
    tProps.width = (tTable.style.width)?tTable.style.width:tTable.width;
    tProps.height = (tTable.style.height)?tTable.style.height:tTable.height;
    tProps.border = tTable.border;
    tProps.cellPadding = tTable.cellPadding;
    tProps.cellSpacing = tTable.cellSpacing;
    tProps.bgColor = tTable.bgColor;

    var ntProps = showModalDialog(pDir + '../includes/table.asp?lang=' + document.all['SPAW_'+editor+'_lang'].value + '&theme=' + document.all['SPAW_'+editor+'_theme'].value, tProps, 
      'dialogHeight:250px; dialogWidth:366px; resizable:no; status:no');  
    
    if (ntProps)
    {
      // set new settings
      tTable.width = (ntProps.width)?ntProps.width:'';
      tTable.style.width = (ntProps.width)?ntProps.width:'';
      tTable.height = (ntProps.height)?ntProps.height:'';
      tTable.style.height = (ntProps.height)?ntProps.height:'';
      tTable.border = (ntProps.border)?ntProps.border:'';
      if (ntProps.cellPadding) tTable.cellPadding = ntProps.cellPadding;
      if (ntProps.cellSpacing) tTable.cellSpacing = ntProps.cellSpacing;
      tTable.bgColor = (ntProps.bgColor)?ntProps.bgColor:'';

      SPAW_toggle_borders(editor, tTable, null);
    }

    //SPAW_update_toolbar(editor, true);    
    //SPAW_updateField(editor,"");
  }



objWYSIWYG.table_cell_prop_click = function(editor, sender, pDir)
  {
    var cd = SPAW_getTD(editor); // current cell
    
    if (cd)
    {
      var cProps = {};
      cProps.width = (cd.style.width)?cd.style.width:cd.width;
      cProps.height = (cd.style.height)?cd.style.height:cd.height;
      cProps.bgColor = cd.bgColor;
      cProps.align = cd.align;
      cProps.vAlign = cd.vAlign;
      cProps.className = cd.className;
      cProps.noWrap = cd.noWrap;
      cProps.styleOptions = new Array();
      if (document.all['SPAW_'+editor+'_tb_style'] != null)
      {
        cProps.styleOptions = document.all['SPAW_'+editor+'_tb_style'].options;
      }
      var ncProps = showModalDialog(pDir + '../includes/td.asp?lang=' + document.all['SPAW_'+editor+'_lang'].value + '&theme=' + document.all['SPAW_'+editor+'_theme'].value, cProps, 
        'dialogHeight:220px; dialogWidth:366px; resizable:no; status:no');  
      
      if (ncProps)  
      {
        cd.align = (ncProps.align)?ncProps.align:'';
        cd.vAlign = (ncProps.vAlign)?ncProps.vAlign:'';
        cd.width = (ncProps.width)?ncProps.width:'';
        cd.style.width = (ncProps.width)?ncProps.width:'';
        cd.height = (ncProps.height)?ncProps.height:'';
        cd.style.height = (ncProps.height)?ncProps.height:'';
        cd.bgColor = (ncProps.bgColor)?ncProps.bgColor:'';
        cd.className = (ncProps.className)?ncProps.className:'';
        cd.noWrap = ncProps.noWrap;
      }      
    }
    //SPAW_update_toolbar(editor, true);    
    //SPAW_updateField(editor,"");
  }



objWYSIWYG.table_row_insert_click = function(editor, sender)
  {
    var ct = SPAW_getTable(editor); // current table
    var cr = SPAW_getTR(editor); // current row

    if (ct && cr)
    {
      var newr = ct.insertRow(cr.rowIndex+1);
      for (i=0; i<cr.cells.length; i++)
      {
        if (cr.cells(i).rowSpan > 1)
        {
          // increase rowspan
          cr.cells(i).rowSpan++;
        }
        else
        {
          var newc = cr.cells(i).cloneNode();
          newr.appendChild(newc);
        }
      }
      // increase rowspan for cells that were spanning through current row
      for (i=0; i<cr.rowIndex; i++)
      {
        var tempr = ct.rows(i);
        for (j=0; j<tempr.cells.length; j++)
        {
          if (tempr.cells(j).rowSpan > (cr.rowIndex - i))
            tempr.cells(j).rowSpan++;
        }
      }
    }
    //SPAW_update_toolbar(editor, true);    
  } // insertRow




objWYSIWYG.table_column_insert_click = function(editor, sender)
  {
    var ct = SPAW_getTable(editor); // current table
    var cr = SPAW_getTR(editor); // current row
    var cd = SPAW_getTD(editor); // current row

    if (cd && cr && ct)
    {
      // get "real" cell position and form cell matrix
      var tm = SPAW_formCellMatrix(ct);
      
      for (j=0; j<tm[cr.rowIndex].length; j++)
      {
        if (tm[cr.rowIndex][j] == cd.cellIndex)
        {
          realIndex=j;
          break;
        }
      }
      
      // insert column based on real cell matrix
      for (i=0; i<ct.rows.length; i++)
      {
        if (tm[i][realIndex] != -1)
        {
          if (ct.rows(i).cells(tm[i][realIndex]).colSpan > 1)
          {
            ct.rows(i).cells(tm[i][realIndex]).colSpan++;
          }
          else
          {
            var newc = ct.rows(i).insertCell(tm[i][realIndex]+1)
            var nc = ct.rows(i).cells(tm[i][realIndex]).cloneNode();
            newc.replaceNode(nc);
          }
        }
      }
    }
    //SPAW_update_toolbar(editor, true);    
  } // insertColumn





objWYSIWYG.table_row_delete_click = function(editor, sender)
  {
    var ct = SPAW_getTable(editor); // current table
    var cr = SPAW_getTR(editor); // current row
    var cd = SPAW_getTD(editor); // current cell

    if (cd && cr && ct)
    {
      // if there's only one row just remove the table
      if (ct.rows.length<=1)
      {
        ct.removeNode(true);
      }
      else
      {
        // get "real" cell position and form cell matrix
        var tm = SPAW_formCellMatrix(ct);
        
        
        // decrease rowspan for cells that were spanning through current row
        for (i=0; i<cr.rowIndex; i++)
        {
          var tempr = ct.rows(i);
          for (j=0; j<tempr.cells.length; j++)
          {
            if (tempr.cells(j).rowSpan > (cr.rowIndex - i))
              tempr.cells(j).rowSpan--;
          }
        }
    
        
        curCI = -1;
        // check for current row cells spanning more than 1 row
        for (i=0; i<tm[cr.rowIndex].length; i++)
        {
          prevCI = curCI;
          curCI = tm[cr.rowIndex][i];
          if (curCI != -1 && curCI != prevCI && cr.cells(curCI).rowSpan>1 && (cr.rowIndex+1)<ct.rows.length)
          {
            ni = i;
            nrCI = tm[cr.rowIndex+1][ni];
            while (nrCI == -1) 
            {
              ni++;
              if (ni<ct.rows(cr.rowIndex+1).cells.length)
                nrCI = tm[cr.rowIndex+1][ni];
              else
                nrCI = ct.rows(cr.rowIndex+1).cells.length;
            }
            
            var newc = ct.rows(cr.rowIndex+1).insertCell(nrCI);
            ct.rows(cr.rowIndex).cells(curCI).rowSpan--;
            var nc = ct.rows(cr.rowIndex).cells(curCI).cloneNode();
            newc.replaceNode(nc);
            // fix the matrix
            cs = (cr.cells(curCI).colSpan>1)?cr.cells(curCI).colSpan:1;
            for (j=i; j<(i+cs);j++)
            {
              tm[cr.rowIndex+1][j] = nrCI;
              nj = j;
            }
            for (j=nj; j<tm[cr.rowIndex+1].length; j++)
            {
              if (tm[cr.rowIndex+1][j] != -1)
                tm[cr.rowIndex+1][j]++;
            }
          }
        }
        // delete row
        ct.deleteRow(cr.rowIndex);
      }
    }
    //SPAW_update_toolbar(editor, true);    
  } // deleteRow



objWYSIWYG.table_column_delete_click = function(editor, sender)
  {
    var ct = SPAW_getTable(editor); // current table
    var cr = SPAW_getTR(editor); // current row
    var cd = SPAW_getTD(editor); // current cell

    if (cd && cr && ct)
    {
      // get "real" cell position and form cell matrix
      var tm = SPAW_formCellMatrix(ct);

      // if there's only one column delete the table
      if (tm[0].length<=1)  
      {
        ct.removeNode(true);
      }
      else
      {
        for (j=0; j<tm[cr.rowIndex].length; j++)
        {
          if (tm[cr.rowIndex][j] == cd.cellIndex)
          {
            realIndex=j;
            break;
          }
        }
        
        for (i=0; i<ct.rows.length; i++)
        {
          if (tm[i][realIndex] != -1)
          {
            if (ct.rows(i).cells(tm[i][realIndex]).colSpan>1)
              ct.rows(i).cells(tm[i][realIndex]).colSpan--;
            else
              ct.rows(i).deleteCell(tm[i][realIndex]);
          }
        }
      }
    }
    //SPAW_update_toolbar(editor, true);    
  } // deleteColumn




objWYSIWYG.table_cell_merge_right_click = function(editor, sender)
  {
    var ct = SPAW_getTable(editor); // current table
    var cr = SPAW_getTR(editor); // current row
    var cd = SPAW_getTD(editor); // current row

    if (cd && cr && ct)
    {
      // get "real" cell position and form cell matrix
      var tm = SPAW_formCellMatrix(ct);
      
      for (j=0; j<tm[cr.rowIndex].length; j++)
      {
        if (tm[cr.rowIndex][j] == cd.cellIndex)
        {
          realIndex=j;
          break;
        }
      }
      
      if (cd.cellIndex+1<cr.cells.length)
      {
        ccrs = cd.rowSpan?cd.rowSpan:1;
        cccs = cd.colSpan?cd.colSpan:1;
        ncrs = cr.cells(cd.cellIndex+1).rowSpan?cr.cells(cd.cellIndex+1).rowSpan:1;
        nccs = cr.cells(cd.cellIndex+1).colSpan?cr.cells(cd.cellIndex+1).colSpan:1;
        // check if theres nothing between these 2 cells
        j=realIndex;
        while(tm[cr.rowIndex][j] == cd.cellIndex) j++;
        if (tm[cr.rowIndex][j] == cd.cellIndex+1)
        {
          // proceed only if current and next cell rowspans are equal
          if (ccrs == ncrs)
          {
            // increase colspan of current cell and append content of the next cell to current
            cd.colSpan = cccs+nccs;
            cd.innerHTML += cr.cells(cd.cellIndex+1).innerHTML;
            cr.deleteCell(cd.cellIndex+1);
          }
        }
      }
    }
    //SPAW_update_toolbar(editor, true);    
  } // mergeRight


objWYSIWYG.table_cell_merge_down_click = function(editor, sender)
  {
    var ct = SPAW_getTable(editor); // current table
    var cr = SPAW_getTR(editor); // current row
    var cd = SPAW_getTD(editor); // current row

    if (cd && cr && ct)
    {
      // get "real" cell position and form cell matrix
      var tm = SPAW_formCellMatrix(ct);
      
      for (j=0; j<tm[cr.rowIndex].length; j++)
      {
        if (tm[cr.rowIndex][j] == cd.cellIndex)
        {
          crealIndex=j;
          break;
        }
      }
      ccrs = cd.rowSpan?cd.rowSpan:1;
      cccs = cd.colSpan?cd.colSpan:1;
      
      if (cr.rowIndex+ccrs<ct.rows.length)
      {
        ncellIndex = tm[cr.rowIndex+ccrs][crealIndex];
        if (ncellIndex != -1 && (crealIndex==0 || (crealIndex>0 && (tm[cr.rowIndex+ccrs][crealIndex-1]!=tm[cr.rowIndex+ccrs][crealIndex]))))
        {
    
          ncrs = ct.rows(cr.rowIndex+ccrs).cells(ncellIndex).rowSpan?ct.rows(cr.rowIndex+ccrs).cells(ncellIndex).rowSpan:1;
          nccs = ct.rows(cr.rowIndex+ccrs).cells(ncellIndex).colSpan?ct.rows(cr.rowIndex+ccrs).cells(ncellIndex).colSpan:1;
          // proceed only if current and next cell colspans are equal
          if (cccs == nccs)
          {
            // increase rowspan of current cell and append content of the next cell to current
            cd.innerHTML += ct.rows(cr.rowIndex+ccrs).cells(ncellIndex).innerHTML;
            ct.rows(cr.rowIndex+ccrs).deleteCell(ncellIndex);
            cd.rowSpan = ccrs+ncrs;
          }
        }
      }
    }
    //SPAW_update_toolbar(editor, true);    
  } // mergeDown




objWYSIWYG.table_cell_split_horizontal_click = function(editor, sender)
  {
    var ct = SPAW_getTable(editor); // current table
    var cr = SPAW_getTR(editor); // current row
    var cd = SPAW_getTD(editor); // current cell

    if (cd && cr && ct)
    {
      // get "real" cell position and form cell matrix
      var tm = SPAW_formCellMatrix(ct);
  
      for (j=0; j<tm[cr.rowIndex].length; j++)
      {
        if (tm[cr.rowIndex][j] == cd.cellIndex)
        {
          realIndex=j;
          break;
        }
      }
      
      if (cd.rowSpan>1) 
      {
        // split only current cell
        // find where to insert a cell in the next row
        i = realIndex;
        while (tm[cr.rowIndex+1][i] == -1) i++;
        if (i == tm[cr.rowIndex+1].length) 
          ni = ct.rows(cr.rowIndex+1).cells.length;
        else
          ni = tm[cr.rowIndex+1][i];
          
        var newc = ct.rows(cr.rowIndex+1).insertCell(ni);
        cd.rowSpan--;
        var nc = cd.cloneNode();
        newc.replaceNode(nc);
  
        cd.rowSpan = 1;
      }
      else
      {
        // add new row and make all other cells to span one row more
        ct.insertRow(cr.rowIndex+1);
        for (i=0; i<cr.cells.length; i++)
        {
          if (i != cd.cellIndex)
          {
            rs = cr.cells(i).rowSpan>1?cr.cells(i).rowSpan:1;
            cr.cells(i).rowSpan = rs+1;
          }
        }
  
        for (i=0; i<cr.rowIndex; i++)
        {
          var tempr = ct.rows(i);
          for (j=0; j<tempr.cells.length; j++)
          {
            if (tempr.cells(j).rowSpan > (cr.rowIndex - i))
              tempr.cells(j).rowSpan++;
          }
        }
        
        // clone current cell to new row
        var newc = ct.rows(cr.rowIndex+1).insertCell(0);
        var nc = cd.cloneNode();
        newc.replaceNode(nc);
      }
    }
    //SPAW_update_toolbar(editor, true);    
  } // splitH
  
objWYSIWYG.table_cell_split_vertical_click = function(editor, sender)
  {
    var ct = SPAW_getTable(editor); // current table
    var cr = SPAW_getTR(editor); // current row
    var cd = SPAW_getTD(editor); // current cell

    if (cd && cr && ct)
    {
      // get "real" cell position and form cell matrix
      var tm = SPAW_formCellMatrix(ct);
  
      for (j=0; j<tm[cr.rowIndex].length; j++)
      {
        if (tm[cr.rowIndex][j] == cd.cellIndex)
        {
          realIndex=j;
          break;
        }
      }
      
      if (cd.colSpan>1)    
      {
        // split only current cell
        var newc = ct.rows(cr.rowIndex).insertCell(cd.cellIndex+1);
        cd.colSpan--;
        var nc = cd.cloneNode();
        newc.replaceNode(nc);
        cd.colSpan = 1;
      }
      else
      {
        // clone current cell
        var newc = ct.rows(cr.rowIndex).insertCell(cd.cellIndex+1);
        var nc = cd.cloneNode();
        newc.replaceNode(nc);
        
        for (i=0; i<tm.length; i++)
        {
          if (i!=cr.rowIndex && tm[i][realIndex] != -1)
          {
            cs = ct.rows(i).cells(tm[i][realIndex]).colSpan>1?ct.rows(i).cells(tm[i][realIndex]).colSpan:1;
            ct.rows(i).cells(tm[i][realIndex]).colSpan = cs+1;
          }
        }
      }
    }
    //SPAW_update_toolbar(editor, true);    
  } // splitV




objWYSIWYG.setLink = function(pstrFrameId) {
  //shows the link popup screen LinkSelectionPopup.asp
  //same as function markupTextAddLink() in non-wysiwyg CMS's
  // shows popup to selct a page via  Zone/Category/Page.  Put into textarea on _admin/PageEdit2.asp
  var objField = objWYSIWYG.getIFrameDocument(pstrFrameId);
  
  objField.focus();
  
	var strSel = objField.selection.createRange().text;
	var obj = objWYSIWYG.Registry[pstrFrameId];
	if(strSel.length > 0){
  	pDir = '';
  	//var strSel = document.selection.createRange().text;
		var strURL = window.showModalDialog(obj.popUpPath + 'LinkSelectionPopup.asp?t=' + strSel,'dialogWidth:500px;dialogHeight:500px;help:no');
    if(strURL){
      Zone = strURL.substring(0,strURL.indexOf('|'));
      strURL = strURL.substring(strURL.indexOf('|')+1);
      Cat = strURL.substring(0,strURL.indexOf('|'));
      strURL = strURL.substring(strURL.indexOf('|')+1);
      Page = strURL.substring(0,strURL.indexOf('|'));
      strURL = strURL.substring(strURL.indexOf('|')+1);
      //strURL is left with href text
      objField.selection.createRange().pasteHTML('<code type="link" src="' + Zone + '|' + Cat + '|' + Page + '">' + strURL + '</code>');
    }
    //markTextAreaPosition1(objField);
    objField.focus();
  }else{
    alert('Please select some text first.');
  }
}

objWYSIWYG.setEmail = function(pstrFrameId) {
  	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("CreateLink", true, "mailto:");
}

objWYSIWYG.toTop = function(pstrFrameId) {
	selectionRange = objWYSIWYG.getIFrameDocument(pstrFrameId).selection.createRange().duplicate();
	selectionRange.pasteHTML("<a href=\"javascript:setTop()\">top</a>");
}

//UNUSED AT THE MOMENT
objWYSIWYG.tableDialog = function(pstrFrameId){
	objWYSIWYG.getIFrameDocument(pstrFrameId).caretPos = objWYSIWYG.getIFrameDocument(pstrFrameId).selection.createRange().duplicate();
	selectionRange = objWYSIWYG.getIFrameDocument(pstrFrameId).caretPos;
	var reg = objWYSIWYG.Registry;
	var obj = objWYSIWYG.Registry[pstrFrameId];

	if (selectionRange != undefined) {
		showModalDialog(obj.imagePath + 'wEditorTable.asp',window,'status:true;dialogWidth:20em;dialogHeight:20em');
	}
}


objWYSIWYG.markupTextAddImageFile = function(pstrFrameId, pType, returnType, pDir){
	//alert(objField)
	var reg = objWYSIWYG.Registry;
	var obj = objWYSIWYG.Registry[pstrFrameId];
  var objField = objWYSIWYG.getIFrameDocument(pstrFrameId);
	var selectionRange = objField.selection.createRange().text;

  
  if(returnType=='text'){
    if(selectionRange != undefined){
  		var strURL = window.showModalDialog(obj.popUpPath + 'ImageFileSelection.asp?type=' + pType,'','dialogWidth:500px;dialogHeight:500px;help:no');
      
      if(strURL){
	  
	    strFileName = strURL.substring(strURL.indexOf('|')+1);
		if (strFileName.toLowerCase().indexOf(".pdf") != -1){
			strIcon = "IconPDF.gif";
		} else if (strFileName.toLowerCase().indexOf(".doc") != -1) {
			strIcon = "IconWORD.gif";
		} else if (strFileName.toLowerCase().indexOf(".xls") != -1) {
			strIcon = "IconXLS.gif";
		} else {
			strIcon = "icon_html.gif";
		}
		
		if (selectionRange == "") {
			selectionRange = strFileName;
		}
		
		if (pType == "file") {
			objField.selection.createRange().pasteHTML('<CODE type="' + pType + '" id="' + strURL.substring(0,strURL.indexOf('|')) + '" src="' + strFileName + '">' + selectionRange + '</CODE>');	 
	  	} else {
			objField.selection.createRange().pasteHTML('<img src="' + obj.imagePath + '../_upload/images/' + strFileName + '" />')
		}
		
		
	   }
      //objField.focus();
    }else{
      alert('Please put the cursor in a textbox where you wish to put the item.');
    }
  }
  if(returnType=='sp'){
  	var strURL = window.showModalDialog(obj.popUpPath + 'ImageFileSelection.asp?type=' + pType,'','dialogWidth:500px;dialogHeight:500px;help:no');
  	var objLabel;
  	var objHiddenID;
  	var objHiddenName;
  	var newID, newName;
  	
    if(strURL){
    	objLabel = document.getElementById('lbl' + objField + 'NewName');
    	objHiddenID = document.getElementById(objField); 
    	objHiddenName = document.getElementById('txt' + objField + 'NewName');
  	  
  	  newID = strURL.substring(0,strURL.indexOf('|'));
  	  newName = strURL.substring(strURL.indexOf('|')+1);
  	  objLabel.innerText = newName;
  	  objHiddenID.value = newID;
  	  objHiddenName.value = newName;
  	}
  }

}

/*
****************************************************************
NEW WYSIWYG FUNCTIONS /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\
****************************************************************
*/


function markTextAreaPosition1(txtObj){
  if(txtObj.createTextRange)
    txtObj.caretPos = document.selection.createRange().duplicate();
}

//select and add a file/image
function markupTextAddImageFile(objField, pType, returnType, pDir){
  /*
    pType: either 'file' or 'image'
    
    returnType: specify if it is either a textarea or label.  It would be a label when selecting images for structured pages
    returnType: values: 'text', 'sp'
    If returnType=='sp' then the objField is just the name of the field, not the object.
  */
  if(returnType=='text'){
    objField.focus();
    if(objField.createTextRange && objField.caretPos){
  		var strURL = window.showModalDialog(pDir + 'ImageFileSelection.asp?noCache=' + (new Date()).valueOf() + '&type=' + pType,'','dialogWidth:500px;dialogHeight:500px;help:no');
      if(strURL){
        var caretPos = objField.caretPos;
        caretPos.text = caretPos.text + '<CMS' + pType + ' id="' + strURL.substring(0,strURL.indexOf('|')) + '" src="' + strURL.substring(strURL.indexOf('|')+1) + '"></CMS' + pType + '>';
      }
      markTextAreaPosition(objField);
      objField.focus();
    }else{
      alert('Please put the cursor in a textbox where you wish to put the item.');
    }
  }
  if(returnType=='sp'){
  	var strURL = window.showModalDialog(pDir + 'ImageFileSelection.asp?noCache=' + (new Date()).valueOf() + '&type=' + pType,'','dialogWidth:500px;dialogHeight:500px;help:no');
  	var objLabel;
  	var objHiddenID;
  	var objHiddenName;
  	var newID, newName;
  	
    if(strURL){
    	objLabel = document.getElementById('lbl' + objField + 'NewName');
    	objHiddenID = document.getElementById(objField); 
    	objHiddenName = document.getElementById('txt' + objField + 'NewName');
  	  
  	  newID = strURL.substring(0,strURL.indexOf('|'));
  	  newName = strURL.substring(strURL.indexOf('|')+1);
  	  objLabel.innerText = newName;
  	  objHiddenID.value = newID;
  	  objHiddenName.value = newName;
  	}
  }

}

function roundAmount(n, bstrip) {
  var s = "" + Math.round(n * 100) / 100
  var i = s.indexOf('.')
  if (i < 0) return s + ".00"
  var t = s.substring(0, i + 1) + 
     s.substring(i + 1, i + 3)
  if (i + 2 == s.length) {
  	t += "0"
  }
  return t;
}

function stripChars(pNumberVal)
{
	var objRegExp = new RegExp(/[^0123456789.]/);
	sNew = pNumberVal.replace(objRegExp, '');
	if (sNew.length < pNumberVal.length) {
		stripChars(sNew);
	}
	return sNew;
}

function checkNumber(pValue)
{
	var strReturn;
	
	if (isNaN(pValue)) {
		alert("Must be a Numeric Value")
		strReturn = "";
	} else {
		strReturn = pValue;
	}
	
	return strReturn;

}

function ImageBuilder (imgSrc, imgLink){
	this.imgSrc = imgSrc;
	this.imgLink = imgLink;
}

// displays the random image on the home page
var theImages = new Array()

var CreativeYouthInit = "z=3&c=363&p=781"
var HowHelp = "z=2&c=370&p=775"
var CorpPartners = "z=2&c=247&p=777"
var TripleCare = "z=3&c=362&p=780"
var TripleCare = "z=3&c=362&p=780"

theImages[0] = new ImageBuilder('images/home_img_1.jpg', 'Page.asp?'+CorpPartners);
theImages[1] = new ImageBuilder('images/home_img_2.jpg', 'Page.asp?'+CreativeYouthInit);
theImages[2] = new ImageBuilder('images/home_img_3.jpg', 'Page.asp?'+HowHelp);
theImages[3] = new ImageBuilder('images/home_img_4.jpg', 'Page.asp?'+TripleCare);
theImages[4] = new ImageBuilder('images/home_img_5.jpg', 'Page.asp?'+TripleCare);

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i].imgSrc
}

var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<a href="'+theImages[whichImage].imgLink+'"><img src="'+theImages[whichImage].imgSrc+'" border="0" alt="Home image"></a>');
}

function validSearchForm(objForm, blnDoSubmit) {
	// this relies on the input being the first field but can be called anything
	var searchBox = document.getElementsByTagName('input')[0];
	if(isEmpty(searchBox.value)) {
		alert("Enter a comma separated list of keyword(s) to search for.");
		searchBox.focus();
		return false;
	}

	if(blnDoSubmit)
		objForm.submit();
	return true;
}

 // toggle borders worker function
  function SPAW_toggle_borders(editor, root, toggle)
  {
    // get toggle mode (on/off)
    var toggle_mode = toggle;
    if (toggle == null)
    {
      var tgl_borders = document.getElementById("SPAW_"+editor+"_borders");
      if (tgl_borders != null)
      {
        toggle_mode = tgl_borders.value;
      }
      else
      {
        toggle_mode = "on"
      }
    }
    
    var tbls = new Array();
    if (root.tagName == "TABLE")
    {
      tbls[0] = root;
    }
    else
    {
      // get all tables starting from root
      tbls = root.getElementsByTagName("TABLE");
    }
    
    var tbln = 0;
    if (tbls != null) tbln = tbls.length;
    for (ti = 0; ti<tbln; ti++)
    {
      if ((tbls[ti].style.borderWidth == 0 || tbls[ti].style.borderWidth == "0px") &&
          (tbls[ti].border == 0 || tbls[ti].border == "0px") &&
          (toggle_mode == "on"))
      {
        tbls[ti].runtimeStyle.borderWidth = "1px";
        tbls[ti].runtimeStyle.borderStyle = "dashed";
        tbls[ti].runtimeStyle.borderColor = "#aaaaaa";
      } // no border
      else 
      {
        tbls[ti].runtimeStyle.borderWidth = "";
        tbls[ti].runtimeStyle.borderStyle = "";
        tbls[ti].runtimeStyle.borderColor = "";
      }
        
      var cls = tbls[ti].cells;
      // loop through cells
      for (ci = 0; ci<cls.length; ci++)
      {
        if ((tbls[ti].style.borderWidth == 0 || tbls[ti].style.borderWidth == "0px") &&
            (tbls[ti].border == 0 || tbls[ti].border == "0px") && 
            (cls[ci].style.borderWidth == 0 || cls[ci].style.borderWidth == "0px") && 
            (toggle_mode == "on"))
        {
          cls[ci].runtimeStyle.borderWidth = "1px";
          cls[ci].runtimeStyle.borderStyle = "dashed";
          cls[ci].runtimeStyle.borderColor = "#aaaaaa";
        }
        else 
        {
          cls[ci].runtimeStyle.borderWidth = "";
          cls[ci].runtimeStyle.borderStyle = "";
          cls[ci].runtimeStyle.borderColor = "";
        }
      } // cells loop
    } // tables loop
  } // SPAW_toggle_borders

function SPAW_getTD(editor)
  {
    if (window.frames[editor].document.selection.type != "Control")
    {
      var tControl = window.frames[editor].document.selection.createRange();
      tControl = tControl.parentElement();
      while ((tControl.tagName != 'TD') && (tControl.tagName != 'TH') && (tControl.tagName != 'TABLE') && (tControl.tagName != 'BODY'))
      {
        tControl = tControl.parentElement;
      }
      if ((tControl.tagName == 'TD') || (tControl.tagName == 'TH'))
        return(tControl);
      else
        return(null);
    }
    else
    {
      return(null);
    }
  }

  // returns current table row  
  function SPAW_getTR(editor)
  {
    if (window.frames[editor].document.selection.type != "Control")
    {
      var tControl = window.frames[editor].document.selection.createRange();
      tControl = tControl.parentElement();
      while ((tControl.tagName != 'TR') && (tControl.tagName != 'TABLE') && (tControl.tagName != 'BODY'))
      {
        tControl = tControl.parentElement;
      }
      if (tControl.tagName == 'TR')
        return(tControl);
      else
        return(null);
    }
    else
    {
      return(null);
    }
  }
  
  // returns current table  
  function SPAW_getTable(editor)
  {
    if (window.frames[editor].document.selection.type == "Control")
    { 
      var tControl = window.frames[editor].document.selection.createRange();
      if (tControl(0).tagName == 'TABLE')
        return(tControl(0));
      else
        return(null);
    }
    else
    {
      var tControl = window.frames[editor].document.selection.createRange();
      tControl = tControl.parentElement();
      while ((tControl.tagName != 'TABLE') && (tControl.tagName != 'BODY'))
      {
        tControl = tControl.parentElement;
      }
      if (tControl.tagName == 'TABLE')
        return(tControl);
      else
        return(null);
    }
  }


//select and add a file/image
function markupTextAddImageFile_nonWYSIWYG(objField, pType, returnType, pDir){
  /*
    pType: either 'file' or 'image'
    
    returnType: specify if it is either a textarea or label.  It would be a label when selecting images for structured pages
    returnType: values: 'text', 'sp'
    If returnType=='sp' then the objField is just the name of the field, not the object.
  */

/*  if(returnType=='text'){
this type should use other function
    objField.focus();
    if(objField.createTextRange && objField.caretPos){
  		var strURL = window.showModalDialog(pDir + 'ImageFileSelection.asp?type=' + pType,'','dialogWidth:500px;dialogHeight:500px;help:no');
      if(strURL){
        var caretPos = objField.caretPos;
        caretPos.text = caretPos.text + '<code type="' + pType + '" id="' + strURL.substring(0,strURL.indexOf('|')) + '" src="' + strURL.substring(strURL.indexOf('|')+1) + '"></code>';
      }
      markTextAreaPosition(objField);
      objField.focus();
    }else{
      alert('Please put the cursor in a textbox where you wish to put the item.');
    }
  }*/
  if(returnType=='sp'){
  	var strURL = window.showModalDialog(pDir + 'ImageFileSelection.asp?type=' + pType,'','dialogWidth:500px;dialogHeight:500px;help:no');
  	var objLabel;
  	var objHiddenID;
  	var objHiddenName;
  	var newID, newName;
  	
    if(strURL){
    	objLabel = document.getElementById('lbl' + objField + 'NewName');
    	objHiddenID = document.getElementById(objField); 
    	objHiddenName = document.getElementById('txt' + objField + 'NewName');
  	  
  	  newID = strURL.substring(0,strURL.indexOf('|'));
  	  newName = strURL.substring(strURL.indexOf('|')+1);
  	  objLabel.innerText = newName;
  	  objHiddenID.value = newID;
  	  objHiddenName.value = newName;
  	}
  }
}