// $Id: scripts.js,v 1.1.1.1 2005/12/05 15:29:30 marcin Exp $

// $Log: scripts.js,v $
// Revision 1.1.1.1  2005/12/05 15:29:30  marcin
// no message
//
// Revision 1.1.1.1  2005/08/17 10:38:44  darren
// no message
//
// Revision 1.1.1.1  2003/12/12 16:08:58  Rod
// no message
//

function showCal(eile){
   if (document.getElementById) {
      with (document.getElementById('date' + eile).style){
      display = 'inline';
       
      }
   }
}

function hideCal(eile){
   if (document.getElementById) {
      document.getElementById('date' + eile).style.display = 'none';
   }
}
function updateDateControl(controlName, dateValue) {
    if (document.getElementById) {
        alert("COntrolName = " + controlName + "") ;
        alert("Date Value = " + dateValue + "") ;
  eval('document.form1.' + controlName + 'day.value = ' + dayValue);
} 
} 
function sendVal(control, dayValue,monthValue,yearValue){
    if (document.getElementById) {
      eval('document.form1.'+ control +'day.value =' + dayValue) ;
      eval('document.form1.'+ control +'month.value =' + monthValue) ;
      eval('document.form1.'+ control +'year.value =' + yearValue) ;

      if (control == "arrive") { 
         hideCal(1);
      }
      else {
         hideCal(2);
      }
        }
   }  

   
function select_image() {
      var fileToLoad = "/HTMLEditor/ImageUploader/imageList.asp";
      if (document.all) {
            var xMax = screen.width, yMax = screen.height;
      }
      else {
            if (document.layers) {
               var xMax = window.outerWidth, yMax = window.outerHeight;
            }
            else {
               var xMax = 640, yMax=480;
            }
      }
      var xOffset = (xMax - 450)/2;
      var yOffset = (yMax - 290)/2;
      imageLoader = window.open(fileToLoad,'imgload','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=450,height=290,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');
      //if (!imageLoader.opener) imageLoader.opener=self;
      //imageLoader.focus();
      popup("/HTMLEditor/ImageUploader/selectImage.asp", "SelectImage");
}

function uploadDoc(fid) {
   popup("/HTMLEditor/ImageUploader/DocumentUpload.asp?fid="+fid, "UploadDoc");
}

function UploadAssignment(AssignmentID, UserID) {
   popup("/HTMLEditor/ImageUploader/AssignmentUpload.asp?aid=" + AssignmentID + "&uid=" + UserID, "UploadAssignment");
}

function popupSized(filetoopen, windowName,intHeight, intWidth) {
  if (document.all)
      var xMax = screen.width, yMax = screen.height;
  else
      if (document.layers)
          var xMax = window.outerWidth, yMax = window.outerHeight;
      else
          var xMax = 640, yMax=480;
  var xOffset = (xMax - 450)/2;
  var yOffset = (yMax - 290)/2;
  
  windowName = window.open(filetoopen,'popup','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=' + intWidth + ',height=' + intHeight + ',screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');
  return false;
}

function Redirect(NewUrl) {
   document.location.href=NewUrl;
}

function popup(filetoopen, windowName) {
  if (document.all)
      var xMax = screen.width, yMax = screen.height;
  else
      if (document.layers)
          var xMax = window.outerWidth, yMax = window.outerHeight;
      else
          var xMax = 640, yMax=480;
  var xOffset = (xMax - 450)/2;
  var yOffset = (yMax - 290)/2;
  
  windowName = window.open(filetoopen,'popup','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=450,height=290,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');
  return false;
}

function insert_link(OptValue) {
   var sText = HTMLEditor.document.selection.createRange();
   if (OptValue != null) {
      document.execCommand("CreateLink" , false, OptValue);
      msgWindow.close();
      //var sel = document.selection.createRange();
	   //sel.text = "<a href=\"" + linkVal + "\">" + str + "</a>";
  }
}

function DoCommand(editorName, command, OptValue) {
   var sText = eval(editorName + '.document.selection.createRange()');
   if(sText.boundingTop<5){
		alert('Please place the cursor inside the editable area and try again');
		return;
		}
   //if (command='InsertImage') {
   //   sText.pasteHTML("<div>");
   //   document.execCommand(command, false, OptValue);
   //   sText.pasteHTML("</div>");
   //}
   //else {
      if (OptValue == '') {
         document.execCommand(command);
         }
      else {
         document.execCommand(command, false, OptValue);
      }
   //}
   eval('frames.' + editorName + '.focus()');
}

function DoStyle(editorName)
{
   var sText = eval(editorName + '.document.selection.createRange()');
   theStyle = document.all.styles.value;
	if (theStyle != "")
   {
      sText.execCommand("FormatBlock", false, theStyle);
      //document.execCommand("FormatBlock", false, theStyle);
      eval('frames.' + editorName + '.focus()');
   }
}

function DoSubmit(editorName,blnNewPage)
{
   var html = eval(editorName +'.innerHTML');
   html = html.replace(/(<.*>\0)/g, '[$1]');
   document.all.EditorValue.value = html;
   if (blnNewPage==1) {
      document.HTMLEditorForm.NewPage.value=1;
   }
   else 
   {
      var x = document.HTMLEditorForm.pageTitle.value;
      x = (x.replace(/^\W+/,'')).replace(/\W+$/,'');
      if (x.length==0) {
         alert('You must enter a page title');
         return false;
      }
   }
   document.HTMLEditorForm.submit();
}

function ShowHTML(editorName)
{
   var html = eval(editorName +'.innerHTML');
   html = html.replace(/(<.*>\0)/g, '[$1]');
   document.all.EditorValue.value = html;
//   document.all.EditorValue.style.display = 'inline';   
   document.all.EditorValue.style.visibility = 'visible';   
}


function Toggle(action)
{
	// get button label
	
	if (action == "ContentEditable")
	{
		if (document.all("edit").contentEditable == "true")
		{
			document.all("edit").contentEditable = "false";
			window.event.srcElement.style.borderStyle = "outset";
		}
		else
		{
			document.all("edit").contentEditable = "true";
			window.event.srcElement.style.borderStyle = "inset";
		}
		
		return;
	}

	else if (action == "Bold"){
		document.execCommand("Bold");
		}

	else if (action == "Italic")
		document.execCommand("Italic");

	else if (action == "FontColor")
	{
		theColor = document.all.fontcolor.value;
		if (theColor != "")
			document.execCommand("ForeColor", false, theColor);
	}
		
	else if (action == "FontSize")
	{
		theSize = document.all.fontsize.value;
		if (theSize != "")		
			document.execCommand("FontSize", false, theSize);
	}
		
	else if (action == "FontName")
	{
		theName = document.all.fontname.value;
		if (theName != "")
			document.execCommand("FontName", false, theName);
	}

	else if (action == "InsertImage")
	{
		theImg = document.all.imagepath.value;
		if (theImg != "")
			document.execCommand("InsertImage", false, theImg);
	}

	window.event.srcElement.style.borderStyle = "outset";
}

function CreateWindow(filename,width,height,scrollbars,status) {
   msgWindow=window.open(filename,"displayWindow","toolbar=no,width="+width+",height="+height+",directories=no,status="+status+",scrollbars="+scrollbars+",resizable=yes,menubar=no")
}

   var cellSelect = null

   function insertSelector(r,contents) {
   // Create a selector cell
   var c
   if (arguments[2]!=null)  // Insert before a particular cell
      c = r.insertCell(arguments[2])
   else
      c = r.insertCell()
   c.className = "selector"
   c.innerText = contents
   }

   function insertRow() {
   // Insert Row
   var r
   if ((null==cellSelect) || (cellSelect.parentElement.id=="columns")) {
      r = document.all.editor.insertRow() // Insert at end of list
      insertSelector(r,"4")
   } else {
      // Insert before selected row
      var el = cellSelect.parentElement
      r = document.all.editor.insertRow(cellSelect.parentElement.rowIndex)
      insertSelector(r,"4")
      // Copy the currently selected row's layout
      for (var intLoop = 1; intLoop < el.cells.length; intLoop++) {
         var c = r.insertCell()
         c.innerHTML = ""
         c.colSpan = el.cells[intLoop].colSpan
      }
   }
   }

   function insertCell() {
   var el = document.all.editor
   var c
   if (null!=cellSelect) {
      var r = el.rows[0]
      if (cellSelect.parentElement.id == "columns") {
         for (var intLoop = 0 ; intLoop < el.rows.length; intLoop++) {
         if (0==intLoop) {
            insertSelector(r,"6", cellSelect.cellIndex)
         } else {
            if (cellSelect.cellIndex<=el.rows[intLoop].cells.length) {
               var c = el.rows[intLoop].insertCell( cellSelect.cellIndex)
               c.innerHTML = ""
            }
         }
         }
      } else
      {
         if (cellSelect.parentElement.cells.length == r.cells.length) 
         insertSelector(r,"6")
         if (cellSelect.cellIndex==0)
         c = el.rows[cellSelect.parentElement.rowIndex].insertCell()
         else
         c = el.rows[cellSelect.parentElement.rowIndex].insertCell(cellSelect.cellIndex)
         c.innerHTML = ""
      }
   }
   }

   function cleanupSelection() {
   if (null!=cellSelect) {
      if (cellSelect.className=="selector") {
         cellSelect.style.background = "#FFFFCC";
         cellSelect.style.color = "black";
         cellSelect.unselectable='on';
      } else {
         // cellSelect.innerHTML = cellSelect.children[0].value          
      
      }
   }
   }

   function docellSelect(el) {
   cleanupSelection()
   el.style.background = "black"
   el.style.color = "#FFFFCC"
   el.unselectable='on';
   cellSelect = el
   
   }

   function getCell() {
   var el = event.srcElement
   while (("TH"!=el.tagName) && ("TD"!=el.tagName) && ("TABLE"!=el.tagName))
      el = el.parentElement
   return el
   }

   function editCell(cell) {
   
   
   // if (cell.children[0].value!= null){
	//cell.children[0].focus()
   // }
   cellSelect = cell
   }

   function doClick() {
   var el = event.srcElement      
   if ("selector"==el.className) {
      // rs.disabled = cs.disabled = true
      docellSelect(el)
   }
   else {
      var cell = getCell()

      if (el.tagName=="TABLE") return
      if ((cell.tagName!="TH") && (cell!=cellSelect)) {
         cleanupSelection()
         editCell(cell)
      }
   }        
   }

   function deleteRow() {
   var el = cellSelect.parentElement
   if (("columns"!=el.id) && (document.all.editor.rows.length!=2)) {
      document.all.editor.deleteRow(el.rowIndex)
      docellSelect(document.all.editor.rows[1].cells[0])
   }
   }

   function deleteCell() {
   var col = cellSelect.cellIndex  
   if (0==col) return   
   if (cellSelect.parentElement.id!="columns") 
      cellSelect.parentElement.deleteCell(col)
   else
      for (var intLoop=0; intLoop < document.all.editor.rows.length; intLoop++)
         if (col<document.all.editor.rows[intLoop].cells.length)
         document.all.editor.rows[intLoop].deleteCell(col)
   cellSelect = null
   docellSelect(document.all.editor.rows[1].cells[0])
   }

   function viewSource() {
      docellSelect(document.all.editor.rows[1].cells[0])
      document.all.temp.innerHTML = document.all.editor.outerHTML
      var el  = document.all.temp.children.item("editor")
      el.onclick = null
      el.deleteRow(0)
      for (var i = 0; i < el.rows.length; i++)
         el.rows[i].deleteCell(0)
      document.all.output.innerText = el.outerHTML
      document.all.temp.innerHTML = ""
      }
      function InsertTable(editorName){
	   var sel = eval(editorName + '.document.selection.createRange()');
	   if(sel.boundingTop<5){
		   alert('Please place the cursor inside the editable area and try again');
		   return;
	   }
	   sel.pasteHTML("<div>");
	   sel.pasteHTML("<TABLE editcellonly align=left ID=editor BORDER ONCLICK=\"doClick()\" ><TR editcellonly ID='columns'><TH editcellonly unselectable='on'>&nbsp;</TH><TH editcellonly CLASS='selector' unselectable='on'>6</TH></TR editcellonly><TR><TH editcellonly CLASS='selector' unselectable='on'>4</TH><TD></TD></TR></TABLE>");
	   sel.pasteHTML(" </div>");
	   docellSelect(document.all.editor.rows[1].cells[0])
   }

function TableEditorToggle(){
	if (document.getElementById){
		EditorTable = document.getElementById( 'editor' );
		if (EditorTable!=null){
			ShowElement( 'edit_table_icon' );
			HideElement( 'insert_table_icon' );	
		}
		else{
			ShowElement( 'insert_table_icon' );
			HideElement( 'edit_table_icon' );
		}
	}
}
function ShowElement( targetId ){
	if (document.getElementById){
  		target = document.getElementById( targetId );
  		target.style.display = "";	
  	}
}

function HideElement( targetId ){
   if (document.getElementById){ 
  		target = document.getElementById( targetId );
		target.style.display = "none";
   }
}

// print function
var da = (document.all) ? 1 : 0;
var pr = (window.print) ? 1 : 0;
var mac = (navigator.userAgent.indexOf("Mac") != -1); 

function printPage() {
  if (pr) // NS4, IE5
    window.print()
  else if (da && !mac) // IE4 (Windows)
    vbPrintPage()
  else // other browsers
    alert("Sorry, your browser doesn't support this feature.");
  return false;
}
   


