function checksuggested( control )
{
}
function checkrequired( control )
{
	if (control.value.length==0)
		control.className='error';
	else
		control.className='';
}
function checkurl( control )
{
	if (control.value.length==0)
		control.className='error';
	else
		control.className='';
}
function checkuri( control )
{
	if (control.value.length==0)
		control.className='error';
	else
		control.className='';
}


function checkint( control )
{
	if (control.value.length==0 || isint(control.value))
		control.className='';
	else
		control.className='error';
}
function isint(value)
{
	for (i=0; i<value.length; i++)
	{
		ch=value.substring(i,i+1);
		if (!isIntChar(ch))
			return false;
	}
	return true;
}
function getkey(e)
{
if (window.event)
   return window.event.keyCode;
else if (e)
   return e.which;
else
   return null;
}

/*********************************** uris *******************************/

function isUriChar( keychar )
{
	return ((keychar>='a' && keychar<='z') || (keychar>='0' && keychar<='9') || (keychar=='_') || (keychar=='-'));
}
function keyPressUri(e)
{
	var key, keychar;
	key = getkey(e);
	
	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	   return true;
	   	
	// get character
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	if (isUriChar( keychar ))
	{
		window.event.keyCode=keychar.charCodeAt(0);
		return true;
	}

	return false;
}

var pasteControl=null;
function pasteUri( control )
{
	pasteControl=control;
	setTimeout("doPasteUri()",100);
}

function doPasteUri()
{
	var s=pasteControl.value;
	var t='';
	for (i=0; i<s.length; i++)
	{
		ch=s.substring(i,i+1).toLowerCase();
		if (isUriChar(ch))
			t+=ch;
	}
	pasteControl.value=t;
}

/*********************************** integers *******************************/

function isIntChar( keychar )
{
	return (keychar>='0' && keychar<='9');
}
function keyPressInt(e)
{
	var key, keychar;
	key = getkey(e);
	
	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	   return true;
	   	
	// get character
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	if (isIntChar( keychar ))
	{
		window.event.keyCode=keychar.charCodeAt(0);
		return true;
	}

	return false;
}
function pasteInt( control )
{
	pasteControl=control;
	setTimeout("doPasteInt()",100);
}

function doPasteInt()
{
	var s=pasteControl.value;
	var t='';
	for (i=0; i<s.length; i++)
	{
		ch=s.substring(i,i+1).toLowerCase();
		if (isIntChar(ch))
			t+=ch;
	}
	pasteControl.value=t;
}

/*********************************** dates *******************************/

function checkdatetime( control )
{
	if (control.value.length==0 || isdatetime(control.value))
		control.className='';
	else
		control.className='error';
}

function checkdate( control )
{
	if (control.value.length==0 || isdate(control.value))
		control.className='';
	else
		control.className='error';
}

function isdatetime(value)
{
	return true;
}

function isdate(value)
{
	 var result;

     var elems = value.split("/");
     
     result = (elems.length == 3); // should be three components
     
     if (result)
     {
       var dayen = elems[0];
        var monthen = elems[1];
       var yearen = elems[2];
      result = !isNaN(dayen) && (dayen > 0) && (dayen < 32) &&
            !isNaN(monthen) && (monthen > 0) && (monthen < 13) &&
   !isNaN(yearen) && (elems[2].length == 4);
     }
      if (!result)
     {
		 return false;
    }
    return true;
}

function isDateChar( keychar )
{
	return ((keychar>='0' && keychar<='9') || (keychar=='/'));
}
function keyPressDate(e)
{
	var key, keychar;
	key = getkey(e);
	
	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	   return true;
	   	
	// get character
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	if (isDateChar( keychar ))
	{
		window.event.keyCode=keychar.charCodeAt(0);
		return true;
	}

	return false;
}
function pasteDate( control )
{
	pasteControl=control;
	setTimeout("doPasteDate()",100);
}

function doPasteDate()
{
	var s=pasteControl.value;
	var t='';
	for (i=0; i<s.length; i++)
	{
		ch=s.substring(i,i+1).toLowerCase();
		if (isDateChar(ch))
			t+=ch;
	}
	pasteControl.value=t;
}

/*********************************** floats *******************************/

function checkfloat( control )
{
	if (control.value.length==0 || isfloat(control.value))
		control.className='';
	else
		control.className='error';
}

function isfloat(value)
{
	for (i=0; i<value.length; i++)
	{
		ch=value.substring(i,i+1);
		if (!isFloatChar(ch))
			return false;
	}
	return true;
}

function isFloatChar( keychar )
{
	return ((keychar>='0' && keychar<='9') || (keychar=='.'));
}
function keyPressFloat(e)
{
	var key, keychar;
	key = getkey(e);
	
	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	   return true;
	   	
	// get character
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	if (isFloatChar( keychar ))
	{
		window.event.keyCode=keychar.charCodeAt(0);
		return true;
	}

	return false;
}
function pasteFloat( control )
{
	pasteControl=control;
	setTimeout("doPasteFloat()",100);
}

function doPasteFloat()
{
	var s=pasteControl.value;
	var t='';
	for (i=0; i<s.length; i++)
	{
		ch=s.substring(i,i+1).toLowerCase();
		if (isFloatChar(ch))
			t+=ch;
	}
	pasteControl.value=t;
}


/***************************/

var openId;
var closeId;

var queueTimeout;
var previewFrameShown=false;

function showPreviewPane(flag)
{
	if (flag)
	{
		picpreview.style.display='inline';
		previewFrameShown=true;
	}
	else
	{
		picpreview.style.display='none';
		previewFrameShown=false;
		previewUrl.innerHTML='';
	}
	resizeScrolltable()
}

function resizeScrolltable()
{
	var width=(document.body.clientWidth-30-(previewFrameShown?400:0)) + 'px';
	scrolltable.style.width=width;
}

function prepareShowPreviewUrl(url)
{
	if (picpreview.style.display!='none')
	{
		clearTimeout( queueTimeout );
		queueTimeout=setTimeout( "showPreviewUrl('"+url+"')", 300 );
	}
}

function showPreviewUrl(url)
{
	previewUrl.innerHTML="<a href='"+url+"'>http://"+window.location.host+"/"+url+"</a>";
	document.getElementById('picpreview').style.top  = 10;
	document.getElementById('picpreview').style.left =	document.body.clientWidth-300;
	document.getElementById('framepreview').src=url;
	document.getElementById('picpreview').style.visibility ='visible';
}

function prepareToOpenPreviewPic(imageName,control,width,height) 
{
	document.getElementById('picpreviewimg').onload=showPreviewPic;
	document.getElementById('picpreviewimg').src=imageName;
	document.getElementById('picpreviewimg').width=width;
	document.getElementById('picpreviewimg').height=height;
	document.getElementById('picpreview').style.top  = getElementPosition(control).top + 20;
	document.getElementById('picpreview').style.left = getElementPosition(control).left + 10;
}
function showPreviewPic() 
{
	document.getElementById('picpreview').style.visibility ='visible';
}
function closePreviewPic() 
{
	document.getElementById('picpreviewimg').onload=null;
	document.getElementById('picpreview').style.visibility ='hidden';
}
function getElementPosition(control)	
{
	var offsetTrail = control;
	var offsetLeft =	0;
	var offsetTop	= 0;
	while (offsetTrail)	
	{
		offsetLeft	+= offsetTrail.offsetLeft;
		offsetTop	+= offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if	(navigator.userAgent.indexOf("Mac")	!= -1 && typeof	document.body.leftMargin !=	"undefined") 
	{
		offsetLeft +=	document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	return {left:offsetLeft,top:offsetTop};
}

function replace(string,text,by) 
{
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

var resourceId=0;
var resourceTitle=null;
var resourceBodyInsert = false;

function callbackInsertResource( id, title )
{
	if(resourceBodyInsert)
	{
		var href = "/" + id + ".file.dld"
		callbackInsertLink( href, title )
	}
	else
	{
		resourceId=id;
		resourceTitle=title;
	}
}

function insertBodyResource( contentId )
{
	resourceBodyInsert = true;
	window.showModalDialog("insertResourceDialog.aspx?classId=resource&contentId="+contentId,self,"dialogHeight:550px;dialogWidth:750px;center:yes;resizable:yes;status:no");
}

function onAddResource( contentId, classId, div, name )
{
	resourceId=0;
	var control=eval('document.forms[0].'+name );
	window.showModalDialog("insertResourceDialog.aspx?classId="+classId+"&contentId="+contentId,self,"dialogHeight:550px;dialogWidth:750px;center:yes;resizable:yes;status:no");
	
	if (resourceId==0)
		return;
	
	var value=resourceId;
	var html=resourceTitle;
	var values=control.value.split(',');
	for (i=0; i<values.length; i++)
	{
		if (values[i]==value)
		{
			alert('Already selected!');
			return;
		}	
	}	
	
	
	if (value!='')
	{
		if (control.value=='')
			control.value=value;
		else
			control.value=control.value+','+value;
	
		var input=' <div id=';
		input+=name;
		input+=value;
		input+='><input type="checkbox" value="';
		input+=value;
		input+='" checked="checked" onclick="onRemoveMultiSelect( this, ';
		input+="'"+name+"'";
		input+=", "+value+')" />'
		input+=replace( html, "&nbsp;","");
		input+='</div>';
		div.innerHTML=div.innerHTML+input;	
	}
}

function onAddMultiSelect( select, div, name )
{
	//alert("here333");
	var control=eval('document.forms[0].'+name );
	var value=select.options[select.selectedIndex].value;
	var values=control.value.split(',');
	for (i=0; i<values.length; i++)
	{
		if (values[i]==value)
		{
			alert('Already selected!');
			return;
		}	
	}	
	
	var html=select.options[select.selectedIndex].innerHTML;
	if (value!='')
	{
		if (control.value=='')
			control.value=value;
		else
			control.value=control.value+','+value;
		select.selectedIndex=0;
	
		var input=' <div id=';
		input+=name;
		input+=value;
		input+='><input type="checkbox" value="';
		input+=value;
		input+='" checked="checked" onclick="onRemoveMultiSelect( this, ';
		input+="'"+name+"'";
		input+=", "+value+')" />'
		input+=html;
		input+='</div>';
		div.innerHTML=div.innerHTML+input;	
	}
}

function onRemoveMultiSelect( input, name, value )
{
	//alert("here444");
	var control=eval('document.forms[0].'+name );
	var values=control.value.split(',');
	control.value='';
	for (i=0; i<values.length; i++)
	{
		if (values[i]!=value)
		{
			if (control.value=='')
				control.value=values[i];
			else
				control.value=control.value+','+values[i];
		}
	}
	var inputDiv=eval( name+value );
	var allDiv=eval( name+'All' );
	cont=allDiv.innerHTML;
	cont=replace( cont, inputDiv.outerHTML, "" );
	allDiv.innerHTML=cont;

}

function onRemoveMultiSelectResource( input, contentId, name, value, fullname )
{
	var control=eval('document.forms[0].'+fullname );
	var values=control.value.split(',');
	control.value='';
	for (i=0; i<values.length; i++)
	{
		if (values[i]!=value)
		{
			if (control.value=='')
				control.value=values[i];
			else
				control.value=control.value+','+values[i];
		}
	}
	var inputDiv=eval( 'list_'+value );
	var allDiv=document.getElementById('cms_'+contentId+'_ResourcesAll');
	cont=allDiv.innerHTML;
	cont=replace( cont, inputDiv.outerHTML, "" );
	allDiv.innerHTML=cont;
}

var currentFrame=null;	

		
// Swap between WYSIWYG mode and raw HTML mode
function swapModes()
{
	var frame=currentFrame;
	frame.focus();
	var format=eval( frame.id+'_Format' );
	var frameWindow = document.frames[frame.id];
	if (format=="WYSIWYG")
	{
		frameWindow.document.body.innerText=frameWindow.document.body.innerHTML;
		frameWindow.document.body.className='html';
		eval( frame.id+'_Format="Text"' );
		panelToolbar.innerHTML=panelHTML.innerHTML;
	}
	else
	{
		frameWindow.document.body.innerHTML=frameWindow.document.body.innerText;
		eval( frame.id+'_Format="WYSIWYG"' );
		frameWindow.document.body.className='wysiwyg';
		panelToolbar.innerHTML=panelWysiwyg.innerHTML;
	}
}

function onContentPaste()
{
	var frame=currentFrame;
	frame.focus();
	var format=eval( frame.id+'_Format' );
	if (format=="WYSIWYG")
		setTimeout( "cleanContent()", 50 );
}
function cleanContent()
{
  var frame=currentFrame;
  frame.focus();
  var format=eval( frame.id+'_Format' );
  var frameWindow = document.frames[frame.id];
  var cont = frameWindow.document.body.innerHTML;
  // get rid of xml headers
  cont = cont.replace((new RegExp("<\\?xml[^>]*>","ig")),"");
  
  // get rid of namespaced tags
  cont = cont.replace((new RegExp("<\/?[a-z]+:[^>]*>","ig")),"");
  
  // get rid of style attributes
  cont = cont.replace((new RegExp("(<[^>]+) style=\"[^\"]*\"([^>]*>)","ig")),"$1 $2");
  
  // get rid of class attributes except class="page" and class=page
  cont = cont.replace((new RegExp("<hr class=page>","ig")),"<hr c_l_a_s_s=page>");
  cont = cont.replace((new RegExp("<hr class=\"page\">","ig")),"<hr c_l_a_s_s=page>");
  cont = cont.replace((new RegExp("(<[^>]+) class=[^ |^>]*([^>]*>)","ig")),"$1 $2");
  cont = cont.replace((new RegExp("<hr c_l_a_s_s=page>","ig")),"<hr class=page>");
  
  // get rid of spans
  cont = cont.replace((new RegExp("<span[^>]*>","ig")),"");
  cont = cont.replace((new RegExp("<\/span>","ig")),"");
  
  // get rid of divs
  cont = cont.replace((new RegExp("<div[^>]*>","ig")),"");
  cont = cont.replace((new RegExp("<\/div>","ig")),"");
  
  // get rid of insertions
  cont = cont.replace((new RegExp("<ins[^>]*>","ig")),"");
  cont = cont.replace((new RegExp("<\/ins>","ig")),"");
  
  // get rid of dels, including inner text
  cont = cont.replace((new RegExp("<del(?:.|\s)*?</del>","ig")),"");
  
  // get rid of comment links, including inner text, and footer comments
  cont = cont.replace((new RegExp("<a language=javascript(?:.|\s)*?</a>","ig")),"");
  cont = cont.replace((new RegExp("<hr align=left width=\"33%\" size=1>","ig")),"");
  
  
  // get rid of font tags
  cont = cont.replace((new RegExp("<font[^>]*>","ig")),"");
  cont = cont.replace((new RegExp("<\/font>","ig")),"");
  
  // get rid of weird lotus tt tags
  cont = cont.replace((new RegExp("<tt[^>]*>","ig")),"");
  cont = cont.replace((new RegExp("<\/tt>","ig")),"");
  
  // get rid of empty paras
  cont = cont.replace((new RegExp("<p[^>]*>&nbsp;<\/p>","ig")),"");
  cont = cont.replace("·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;","&middot;");
  cont = cont.replace("&lsquo;","'");
  cont = cont.replace("&rsquo;","'");
  cont = cont.replace("&nbsp;"," ");
  frameWindow.document.body.innerHTML=cont;

}


function doCommand( command )
{
	var frame=currentFrame;
	frame.focus();
	frameWindow = document.frames[frame.id];
	frameWindow.document.execCommand(command, true);
}

function insertImage( classId, contentId )
{
	window.showModalDialog("insertImageDialog.aspx?classId="+classId+"&contentId="+contentId,self,"dialogHeight:550px;dialogWidth:750px;center:yes;resizable:yes;status:no");
}

function callbackInsertImage( align, serverFilename, title, width, height )
{
	if (align=="Inline")
		align="";
	var tag='<img src="'+serverFilename+'" width="'+width+'" height="'+height+'" alt="'+title+'" align="'+align+'" class="wysiwyg'+align+'" border=\"0\" onresizestart="return false" />';
	var frame=currentFrame;
	frame.focus();
	frameWindow = document.frames[frame.id];
	 
	 var obj_Selection = frameWindow.document.selection;
	 var obj_Tag = obj_Selection.createRange();
	 obj_Tag.pasteHTML( tag );
}

function insertInternalLink(href, title)
{
	window.showModalDialog("insertLinkDialog.aspx?contentId="+contentId,self,"dialogHeight:"+screen.height+"px;dialogWidth:"+screen.width+"px;center:yes;resizable:yes;status:no;toolbar:no;");
}

function insertExternalLink(href, title)
{
	window.showModalDialog("insertLink.aspx?2",self,"dialogHeight:"+screen.height+"px;dialogWidth:"+screen.width+"px;center:yes;resizable:yes;status:no;toolbar:no;");
}

function insertFileLink()
{
	window.showModalDialog("insertFileLink.aspx",self,"dialogHeight:170px;dialogWidth:700px;center:yes;resizable:yes;status:no;toolbar:no;");
}

function InsertItem(item) 
{
	var frame=currentFrame;
	frame.focus();
	var obj_Selection = document.selection;
	var obj_Tag = obj_Selection.createRange();  
	var cont=obj_Tag.htmlText;
	cont=item;
	obj_Tag.pasteHTML(cont);
 }

function callbackInsertFileLink( href, title )
{
	if (href=="")
		return;
		
	var frame=currentFrame;
	frame.focus();
	frameWindow = document.frames[frame.id];
	 
	 var obj_Selection = frameWindow.document.selection;
	 var obj_Tag = obj_Selection.createRange();
	 var cont=obj_Tag.htmlText;
	 if ((typeof cont) =="string")
	 {
		if (cont!= "") 
		{
			cont = cont.replace((new RegExp("<a[^>]*>","ig")),"");
			cont = cont.replace((new RegExp("<\/a>","ig")),"");
			cont = cont.replace((new RegExp("<p[^>]*>","ig")),"");
			cont = cont.replace((new RegExp("<\/p>","ig")),"");		
			cont="<a href=" + href + " >"+cont+"</a>";
			obj_Tag.pasteHTML(cont);
		} 
		else 
		{
			if (title==null || title=="")
			{
				title=prompt("Please choose a title for your link", href );
			}
			if (title!=null)
			{
				cont="<a href=" + href + " >"+title+"</a>";
				obj_Tag.pasteHTML(cont);
			}
			
		}
	}
	else
	{
		frameWindow.document.execCommand( 'CreateLink', false, href.toString());
		
	}
} 

function callbackInsertLink( href, title )
{
	
	if (href=="http://" || href=="")
		return;
		
	var frame=currentFrame;
	frame.focus();
	frameWindow = document.frames[frame.id];
	 
	 var obj_Selection = frameWindow.document.selection;
	 var obj_Tag = obj_Selection.createRange();
	 var cont=obj_Tag.htmlText;
	 
	
	
	
	 if ((typeof cont) =="string")
	 {
		if (cont!= "") 
		{
			cont = cont.replace((new RegExp("<a[^>]*>","ig")),"");
			cont = cont.replace((new RegExp("<\/a>","ig")),"");
			cont = cont.replace((new RegExp("<p[^>]*>","ig")),"");
			cont = cont.replace((new RegExp("<\/p>","ig")),"");		
			cont="<a href=" + href + " >"+cont+"</a>";
			obj_Tag.pasteHTML(cont);
		} 
		else 
		{
			if (title==null || title=="")
			{
				title=prompt("Please choose a title for your link", href );
			}
			if (title!=null)
			{
				cont="<a href=" + href + " >"+title+"</a>";
				obj_Tag.pasteHTML(cont);
			}
			
		}
	}
	else
	{
		
		frameWindow.document.execCommand( 'CreateLink', false, href.toString());
		
	}
} 

function insertEmailAddress()
{
	var frame=currentFrame;
	frame.focus();
	frameWindow = document.frames[frame.id];
	 
	var obj_Selection = frameWindow.document.selection;
	var obj_Tag = obj_Selection.createRange();
	var cont=obj_Tag.htmlText;
	
	//Need to make sure the selected content isnt just empty spaces or carriage returns.
	cont = cont.replace("&nbsp;","");
	cont = cont.replace("\n","");
	cont = cont.replace("\r","");
	cont = cont.replace((new RegExp("<a[^>]*>","ig")),"");
	cont = cont.replace((new RegExp("<\/a>","ig")),"");
	cont = cont.replace((new RegExp("<p[^>]*>","ig")),"");
	cont = cont.replace((new RegExp("<\/p>","ig")),"");	
	
	var textSelected = false;
	if ((typeof cont) =="string" && cont!= "")
		textSelected = true;

	window.showModalDialog("insertEmail.aspx?textSelected="+textSelected,self,"dialogHeight:250px;dialogWidth:400px;center:yes;resizable:yes;status:no;toolbar:no;");
	//callbackInsertEmail('steve.brewer@sequence.co.uk','');
}

function callbackInsertEmail( email , name )
{
	
	var frame=currentFrame;
	frame.focus();
	frameWindow = document.frames[frame.id];
	 
	 var obj_Selection = frameWindow.document.selection;
	 var obj_Tag = obj_Selection.createRange();
	 var cont=obj_Tag.htmlText;
	 if ((typeof cont) =="string")
	 {
		cont = cont.replace((new RegExp("<a[^>]*>","ig")),"");
		cont = cont.replace((new RegExp("<\/a>","ig")),"");
		cont = cont.replace((new RegExp("<p[^>]*>","ig")),"");
		cont = cont.replace((new RegExp("<\/p>","ig")),"");	
		
		//Need to make sure the selected content isnt just empty spaces or carriage returns.
		var noSpacesCont = cont.replace("&nbsp;","");
		noSpacesCont = noSpacesCont.replace("\n","");
		noSpacesCont = noSpacesCont.replace("\r","");
		
		if (cont!= ""&&noSpacesCont!="") 
			cont="<a href=mailto:" + email + " >"+cont+"</a>"; 
		else 
		{
			if(name == "")
				name = email;
			cont="<a href=mailto:" + email + " >"+name+"</a>";
		}
		obj_Tag.pasteHTML(cont);
	}
}

function stripFormatting( cont )
{
	cont = cont.replace((new RegExp("<h[^>]*>","ig")),"");
	cont = cont.replace((new RegExp("<\/h[^>]*>","ig")),"");
	cont = cont.replace((new RegExp("<span[^>]*>","ig")),"");
	cont = cont.replace((new RegExp("<\/span>","ig")),"");
	cont = cont.replace((new RegExp("<div[^>]*>","ig")),"");
	cont = cont.replace((new RegExp("<\/div>","ig")),"");
	cont = cont.replace((new RegExp("<font[^>]*>","ig")),"");
	cont = cont.replace((new RegExp("</font>","ig")),"");
	cont = cont.replace((new RegExp("<strong[^>]*>","ig")),"");
	cont = cont.replace((new RegExp("</strong>","ig")),"");
	cont = cont.replace((new RegExp("<em[^>]*>","ig")),"");
	cont = cont.replace((new RegExp("</em>","ig")),"");
	cont = cont.replace((new RegExp("<p[^>]*>","ig")),"");
	cont = cont.replace((new RegExp("</p>","ig")),"");
	cont = cont.replace((new RegExp("<br[^>]*>","ig")),"");
	return cont;
}

function isHeaderTag( tag )
{
	return isHeaderTagName( tag.tagName );
}

function isHeaderTagName( tagName )
{
	tagName=tagName.toLowerCase();
	return tagName=='h1' || tagName=='h2' || tagName=='h3' || tagName=='h4' || tagName=='h5' || tagName=='h6';
}

function isSpanTag( tag )
{
	return tag.tagName=='span' || tag.tagName=='SPAN';
}

function doChar( extendedchar )
{
	var frame=currentFrame;
	frame.focus();
	frameWindow = document.frames[frame.id];
	 
	 var obj_Selection = frameWindow.document.selection;
	 var obj_Tag = obj_Selection.createRange();
	 obj_Tag.pasteHTML(extendedchar);	 
}

// Style Picker
function doStyle( className )
{
	var frame=currentFrame;
	frame.focus();
	var frameWindow = document.frames[frame.id];

	var obj_Selection = frameWindow.document.selection;
	var obj_Tag = obj_Selection.createRange();  
	var parent = obj_Tag.parentElement();
		 
	if (className == "_Remove") 
	{

		if (isHeaderTag(parent) || isSpanTag(parent))
		{
			parent.outerHTML=stripFormatting(parent.innerHTML);
		}	 
		else
		{
			obj_Tag.pasteHTML(stripFormatting(obj_Tag.htmlText));
		}
	}
	else
	{

		 var isHeader=(isHeaderTagName(className))?1:0;
	  	if (isHeaderTag(parent) && isHeader)
	 	{
		 	obj_Tag=parent;
			obj_Tag.outerHTML="<"+className + ">"+stripFormatting( obj_Tag.innerHTML )+"</"+className+">";
			return;
	 	}

		 var cont=stripFormatting( obj_Tag.htmlText );
		  
		  if (isHeader)
		  {
		    cont="<" + className + ">"+cont+"</"+className+">";
		  }
		  else
		    cont="<span class=" + className + ">"+cont+"</span>";
		 obj_Tag.pasteHTML(cont);
	}
}

function getDocHeight(doc) {
  var docHt = 0, sh, oh;
  if (doc.height) docHt = doc.height;
  else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
  }
  return docHt;
}

function OnMouseWheel( e )
{
	return false;
}

function onLoad(frame, frameName, fieldId, OnKeyPress, BaseUrl)
{
	var doc=frame.contentWindow.document;
	
	doc.createStyleSheet('wysiwyg.css');
	doc.createStyleSheet('diablo/wysiwyg.css');
	doc.body.className='wysiwyg';
	doc.body.style.margin='2px';
	doc.body.contentEditable=true;
	//Need to find ALL forms on the page and look for the input(fieldId) in each form Because this is now used on the front end (as well as the cms) and there is the possibility that there is more than one form on the page !!!1 shock horror....
	var value = null;
	for(i=0; i<document.forms.length;i++)
	{
		if(document.forms[i][fieldId]!=null)
		{
			value=document.forms[i][fieldId].value;
			document.forms[i][fieldId].value="<null>";
		}
	}
	if (value=='') value='<p></p>';
	value=replace(value,'src="/','src="'+BaseUrl);
	value=replace(value,'<IMG ', '<img onresizestart="return false" ');
	value=replace(value,'<img ', '<img onresizestart="return false" ');
	doc.body.innerHTML=value;
	doc.body.onpaste=onContentPaste;
	doc.body.onkeypress=eval(OnKeyPress);
	doc.body.onmousewheel=OnMouseWheel;
	eval( frame.id+'_Format="WYSIWYG"');
	var idealHeight=getDocHeight( doc );
	if (frame.height<idealHeight)
	{
		frame.style.height=idealHeight+30+"px";
	}
} 

function focusFrame( frame, fieldId )
{
	currentFrame=frame;
	var doc=frame.contentWindow.document;

	var format=eval( frame.id+'_Format' );
	var frameWindow = document.frames[frame.id];
	
	if (document.all["panelToolbar"]!=null)
	{
		if (format=="WYSIWYG")
		{
			panelToolbar.innerHTML=panelWysiwyg.innerHTML;
		}
		else
		{
			panelToolbar.innerHTML=panelHTML.innerHTML;		
		}	
		
		var field=document.forms[0][fieldId];
		panelToolbar.style.top=getElementPosition( field ).top-30;
		var offsetLeft=getElementPosition( field ).left;
		var barWidth=500;
		var clientWidth=document.body.clientWidth;
		var maxLeft=clientWidth-barWidth;
		if (maxLeft<offsetLeft)
			offsetLeft=maxLeft;
		panelToolbar.style.left=offsetLeft;	
		panelToolbar.style.display='block';
	}
}

function blurFrame( frame, rows, fieldId )
{
	var value=null;
	var format=eval( frame.id+'_Format' );
	var doc=frame.contentWindow.document;
	if (format=='WYSIWYG')
	{
		// check for empty text, just tags
		if (doc.body.innerText=="" && doc.body.innerHTML.indexOf("<img")==-1 && doc.body.innerHTML.indexOf("<IMG")==-1)
			value="";
		else
			value=doc.body.innerHTML;
	}
	else
	{
		value=doc.body.innerText;	
	}
	if (rows==1)
	{
		value = value.replace((new RegExp("<p[^>]*>","ig")),"");
		value = value.replace((new RegExp("<\/p>","ig")),"");
	}
	if (value=='<p></p>') value='';
	value=value.replace(' onresizestart="return false"','');
	// get rid of the .../editcontent.aspx?contentId=etc, to allow for #links
	while( value.indexOf( window.location )!=-1 )
	{
		// need a while loop as javascript replace function only replaces first instance - DOH!
		value=value.replace(window.location,'');
	}
	//Need to find ALL forms on the page and look for the input(fieldId) in each form Because this is now used on the front end (as well as the cms) and there is the possibility that there is more than one form on the page !!!1 shock horror....
	for(i=0; i<document.forms.length;i++)
	{
		if(document.forms[i][fieldId]!=null)
			document.forms[i][fieldId].value=value;
	}
	//document.forms[0][fieldId].value=value;
}

function hideToolbar() {
	currentFrame=null;
	if (document.all["panelToolbar"]!=null)
	{
		panelToolbar.innerHTML="";	
		panelToolbar.style.display='none';	
	}
}

function keyPressSingleRow(e)
{
	return true;	
}

function keyPressMultiRow(e)
{
	/*
	var frame=currentFrame;
	var frameWindow = document.frames[frame.id];
	var doc=frameWindow.document;
	var idealHeight=getDocHeight( doc );
	if (frame.height<idealHeight)
	{
		frame.style.height=idealHeight+30+"px";
	}
	*/

	return true;
}

function buttonover( button )
{
	button.className='hover';
}

function buttonout( button )
{
	button.className='';
}


/*********************************** CheckBox Tree *******************************/

function changeTicks( contentId , mainContentId , name  )
{
	//var inputId = "cms_bureau" + contentId;
	var inputId = "cms_"+mainContentId+"_"+name + contentId;
	var input = document.getElementById(inputId);
	var checked = input.checked;
	var arrayElement = GetArrayElement( treeArray[0], contentId );

	if(checked)
		SetChildren(arrayElement, checked , mainContentId , name);
	else
	{
		UncheckParents(treeArray[0], contentId , mainContentId , name);
		SetChildren(arrayElement, checked , mainContentId , name);
	}
}

function SetChildren(parent, value , mainContentId , name)
{
	if (parent.children && parent.children.length > 0)
	{
		for(var x=0; x<parent.children.length; x++)
		{
			thisCheckbox = GetCheckbox( parent.children[x].contentId , mainContentId , name );
			thisCheckbox.checked = value;

			SetChildren(parent.children[x], value , mainContentId , name);
		}
	}
}

function GetCheckbox( contentId , mainContentId , name )
{
	var inputId = "cms_"+mainContentId+"_"+name + contentId;

	if (document.getElelementById)
		return document.getElementById(inputId);
		//return document.getElementById("cms_bureau" + contentId);

	else if (document.layers)
		return document.layers[inputId];
		//return document.layers["cms_bureau" + contentId];

	else
		return document.all[inputId];
		//return document.all["cms_bureau" + contentId];
}

function GetArrayElement( parent, contentId )
{
	var elem = null;
	
	if (parent.contentId == contentId)
		return parent;

	if (parent.children && parent.children.length>0)
	{
		for(var x=0; x<parent.children.length; x++)
		{
			if (parent.children[x].contentId == contentId)
				elem = parent.children[x];
			else
				elem = GetArrayElement(parent.children[x], contentId)

			if (elem != null)
				return elem;
		}
	}
	
	return elem;
}

function UncheckParents( parent, contentId , mainContentId , name )
{
	var AmIAParent=false;

	if (parent.contentId == contentId)
		return true;

	if (parent.children && parent.children.length>0)
	{
		for(var x=0; x<parent.children.length; x++)
		{
			if (parent.children[x].contentId == contentId)
				AmIAParent = true;
			else
			{
				if ( UncheckParents(parent.children[x], contentId , mainContentId , name) )
					AmIAParent = true;
			}
		}

		if (AmIAParent)
		{
			chkParent = GetCheckbox( parent.contentId , mainContentId , name )
			chkParent.checked = false;
		}
	}

	return AmIAParent;
}

/*********************************** CheckBox Tree END *******************************/

function setCookie(cookieName,cookieValue,nDays) 
{
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}
function getCookie(cookieName) 
{
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

/*********************************** Scriptaculous Order code *******************************/
function StripMe( myString ) {
	var result = '';
	
	result = myString;
	result = result.replace(/&/g, ',');
	result = result.replace(/list/gi, '');
	result = result.replace(/amp;/gi, '');
	result = result.replace(/=/gi, '');
	result = result.replace(/\[\]/gi, '');
	
	return result;
}
/***************************** EditContent Extended "Confirm" boxes **************************/
var confirmPromptSender=null;
function confirmPrompt( sender, cprompt, options )
{
	cprompt=cprompt.replace('{0}',jsClassSingular);
	cprompt=cprompt.replace('{1}',window.location.host);
	confirmPromptSender=sender;
	var confirmBar=document.getElementById("confirmBar");
	var actionBar=document.getElementById("actionBar");
	var bar="<span class=\"confirmPrompt\">"+cprompt+"</span>"
	if (options==null)
		options="onConfirmYes=Yes,onConfirmNo=No";
	options=options.split(',');
	for( i=0; i<options.length; i++ )
	{
		var kv=options[i].split('=' );
		var cssClass="";
		if (kv.length>2)
			cssClass=kv[2];
		bar=bar+" <a class=\""+cssClass+"\" href=\"javascript:"+kv[0]+"();\">"+kv[1]+"</a>";
	}
	
	confirmBar.innerHTML=bar;
	actionBar.style.display='none';
	confirmBar.style.display='block';

}
function onConfirmYes()
{
	confirmBar.innerHTML='Please wait...';
	if (confirmPromptSender.indexOf('.aspx')!=-1)
	{
		window.location=confirmPromptSender;
	}
	else
	{
		__doPostBack( confirmPromptSender, '' );
	}
}
function onConfirmNo()
{
	confirmBar.style.display='none';
	actionBar.style.display='block';
}

/*****************************  Survey and Collapsing List **************************/

function ListExpandToggle(sender, target)
{
	if (sender.childNodes[0].src.indexOf('plus.png')>-1)
	{
		sender.childNodes[0].src='./diablo/images/input/minus.png';
		new Effect.Appear(target);
		
		
		
		return false;

	}
	else
	{
		sender.childNodes[0].src='./diablo/images/input/plus.png';
		new Effect.SlideUp(target);
		return false;
	}
}


function SurveyChangeQuestionType(sender, target)
{
	alert(sender.value);
	
	if (sender.value!='text' && sender.value!='textarea')
	{
		new Effect.Appear(target);
	}
	else
	{
		new Effect.SlideUp(target);
	}
}

function GetNameValuePairValues(text)
{
	result = "";
	split = text.split("=");
	for(i=1;i<split.length;i++)
	{
		if (i>1) result += ",";
		result += split[i].split("&")[0];
	}
	return result;
}
