var sInitColor = null;

function callFormatting(sFormatString, option){
	showInterface=true;
	top.frames["mainframe"].document.execCommand(sFormatString,showInterface,option);
}

function changeFontSize(size){
	//var sSelected=thisForm.fontsize.selectedIndex + 1;
	//alert (sSelected);
	top.frames["mainframe"].document.execCommand("FontSize", false, size);
}

function ChangeFont(face){	
	//var myIndex = thisForm.fontface.selectedIndex;
	//var sSelected=thisForm.fontface.options[myIndex].value;
	top.frames["mainframe"].document.execCommand("FontName", false, face);
}
function ChangeFormat(type){
	//var myIndex = thisForm.BlockFormat.selectedIndex;
	//var sSelected=thisForm.BlockFormat.options[myIndex].value;
	top.frames["mainframe"].document.execCommand("FormatBlock", false, type);
}	

function ChangeFontColor(sColorType,thisForm) {
	//var myIndex = thisForm.FontColor.selectedIndex;
	//var sColor=thisForm.FontColor.options[myIndex].value;
	var sColor = callColorDlg();
	top.frames["mainframe"].document.execCommand(sColorType, false, sColor);
}

function ChangeForeColor(sColor) {
	top.frames["mainframe"].document.execCommand('ForeColor', false, sColor);
}

function ChangeBackColor(sColorType,thisForm) {
	var myIndex = thisForm.BackColor.selectedIndex;
	var sColor=thisForm.BackColor.options[myIndex].value;
	top.frames["mainframe"].document.execCommand(sColorType, false, sColor);
}

//This variable needs to have global scope for the callColorDlg function to persist the chosen color
function callColorDlg(){
	//if sInitColor is null, the color dialog box has not yet been called
	if (sInitColor == null) {
		var sColor = dlgHelper.ChooseColorDlg();
	}else{
		//call the dialog box and initialize the color to the color previously chosen
		var sColor = dlgHelper.ChooseColorDlg(sInitColor);
	}
	sColor = sColor.toString(16);
	if (sColor.length < 6) {
	  var sTempString = "000000".substring(0,6-sColor.length);
	  sColor = sTempString.concat(sColor);
	}
	return sColor;
}



function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
  if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function cmOpenSource(){
	var cb_ID = top.frames['mainframe'].document.forms['ContentBlockForm'].cb_ID.value;
	if (!cb_ID == ''){
		var test = '/wp15/ContentManager/EditPage.asp?cb_ID=' + cb_ID
		window.open(test,'PopUp_EditPage','alwaysRaise=yes,scrollbars=yes,resizable=yes,width=800,height=500');
	}else{
		alert('Page must be saved at least once prior to editing the source.')
	}
}

		

//This needs to be fixed for news
function cmSaveChanges() {
	if (top.frames['mainframe'].document.forms['ContentBlockForm']) {
		top.focus();
		//top.frames['mainframe'].document.forms['ContentBlockForm'].test.focus();
		top.frames['mainframe'].document.forms['ContentBlockForm'].submit();
	} else {
		alert ("Sorry, you cannot save changes to this page. It is not an editable page.")
	}	
}


function cmRevert() {
	if (top.frames['mainframe'].document.forms['ContentBlockForm']) {
		if (confirm('Are you sure you want to revert to the last saved version of this page? You will lose any changes you have made since then.')) {
			top.frames['mainframe'].document.forms['ContentBlockForm'].action='/wp15/ContentManager/revertContentBlocks.asp';
			top.frames['mainframe'].document.forms['ContentBlockForm'].submit();
		}
	} else {
		alert ("Sorry, this is not an editable page.")
	}
}


function cmToggleMode() {
	top.frames['mainframe'].location = '/wp15/ContentManager/ToggleMode.asp'
	alert("Edit mode changed.");
	top.frames['mainframe'].history.back();
	top.frames['mainframe'].window.location.reload();
}


function cmWrapHTML(openHTML,closeHTML) {

	openHTML = window.prompt( 'Enter opening HTML.' );
	closeHTML = window.prompt( 'Enter closing HTML.' );

	var sel = top.frames["mainframe"].document.selection;
	if (sel!=null) {
		var rng = sel.createRange();
		if (rng!=null) {
			rng.pasteHTML(' ' + openHTML + rng.htmlText + closeHTML);
		}
	}

	//var r = top.frames["mainframe"].document.createTextRange();
	//alert("Wrapping " + openHTML + " and " + closeHTML);
	//r.insertAdjacentHTML('afterBegin', openHTML);
	//r.insertAdjacentHTML('beforeEnd', closeHTML);
}


function cmCheckBold() {
	var sel = top.frames["mainframe"].document.selection;
	if (sel!=null) {
		var rng = sel.createRange();
		if ( rng.queryCommandState('Bold') ) {
			alert("It's Bold.");
		} else {
			alert("It's NOT Bold.");
		}
	}
}


function cmSetImgAttribute(name,value) {
	var sel = top.frames["mainframe"].document.selection;
	if (sel!=null) {
		var rng = sel.createRange();
		//var myhspace = top.frames["mainframe"].document.createAttribute("hspace");
		//sel.add(myhspace);
		//myhspace.value = "10";
		rng.item(0).setAttribute(name, value)
	}
}


//function cmChooseColor() {
//	window.showModalDialog("/wp15/ContentManager/ChooseColor.asp","Dialog Box Arguments # 1","dialogHeight: 300px; dialogWidth: 300px; dialogTop: 100px; dialogLeft: 100px; edge: Raised; center: Yes; help: No; resizable: No; status: No; scroll: no;");
//}

function cmEditor() {
	window.showModalDialog("/wp15/ContentManager/ContentManager.asp?windowAlreadyOpened=yes","Dialog Box Arguments # 1","dialogHeight: 600px; dialogWidth: 800px; dialogTop: 100px; dialogLeft: 100px; edge: Raised; center: Yes; help: No; resizable: Yes; status: Yes; scroll: no;");
}

//function cmInsertImage() {
//	seed = Math.random();
//	window.showModelessDialog("/wp15/ContentManager/ListImages.asp?" + seed,window,"dialogHeight: 300px; dialogWidth: 300px; dialogTop: 100px; dialogLeft: 100px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No; scroll: yes;");
//}

function cmInsertImage(Alignment) {
	//alert(Alignment);
	seed = Math.random();
	window.showModelessDialog("/wp15/ContentManager/ListImages.asp?Align=" + Alignment, window, "dialogHeight: 300px; dialogWidth: 300px; dialogTop: 100px; dialogLeft: 100px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No; scroll: yes;");
}

function cmUploadImage() {
	seed = Math.random();
	window.open('/wp15/ContentManager/UploadImage.asp?FolderPath=','UploadImage','alwaysRaised=yes,resizable=yes,scrollbars=no,toolbar=no,width=400,height=140');
}


function cmUploadFile() {
	seed = Math.random();
	window.open('/wp15/ContentManager/UploadFile.asp','UploadFile','alwaysRaised=yes,resizable=yes,scrollbars=no,toolbar=no,width=400,height=140');
}

function cmToggleEditMode() {
	seed = Math.random();
	targetPath = top.frames["mainframe"].location;
	top.frames["mainframe"].location = "/wp15/ContentManager/ToggleMode.asp?seed=" + seed + "&targetPath=" + escape(targetPath)
}

//function cmWrapHTML(openHTML, closeHTML) {
//	alert("Wrapping " + openHTML " and " + closeHTML);
//	
//}

//top.frames["mainframe"].onbeforeunload = "confirm('You sure?');"
function cmMakeEditable() {
	top.frames["mainframe"].document.designMode='on';
}

//document.onmouseup = hideActiveMenu;



