// JavaScript library of functions for the Portfolio 3 project.
// 04/05/2000 modifed by Kristin Kinzler Deal

var TutorialWindow = null;
var MyWindow = null;
var FooterWindow = null;
var TipWindow = null;
var MaterialWindow = null;
var ReportWindow = null;


//  Opens a "TutorialWindow" to the location specified
//  by String "URL"
function openTutorialWindow(URL) {

   sFeatures = "width=610,height=400,titlebar=yes,status=yes,scrollbars=yes,resizable=yes";
   tutorialWin = window.open (URL, "Tutorial", sFeatures);
   if (tutorialWin.opener == null) {  // for Nav 2.0x
      tutorialWin.opener = self  // this creates and sets a new property
   }
   if (navigator.userAgent.indexOf("MSIE") == -1)  {
      // MSIE doesn't support focus() method.
      tutorialWin.focus();
   }
}


// Closes the "TutorialWindow".
function closeTutorialWindow() {
    TutorialWindow.close();
}


//  Opens "MyWindow" to the location specified
//  by String "URL"
function openMyWindow(URL) {

   sFeatures = "width=610,height=400,titlebar=yes,status=yes,scrollbars=yes,resizable=yes";
   headerWin = window.open (URL, "My", sFeatures);
   if (headerWin.opener == null) {  // for Nav 2.0x
      headerWin.opener = self  // this creates and sets a new property
   }
   if (navigator.userAgent.indexOf("MSIE") == -1)  {
      // MSIE doesn't support focus() method.
      headerWin.focus();
   }
}

// Closes "MyWindow".
function closeMyWindow() {
    MyWindow.close();
}


//  Opens "FooterWindow" to the location specified
//  by String "URL"
function openFooterWindow(URL) {
   sFeatures = "width=550,height=500,titlebar=yes,status=yes,scrollbars=yes,resizable=yes";
   footerWin = window.open (URL, "Footer", sFeatures);
   if (footerWin.opener == null) {  // for Nav 2.0x
      footerWin.opener = self  // this creates and sets a new property
   }
   if (navigator.userAgent.indexOf("MSIE") == -1)  {
      // MSIE doesn't support focus() method.
      footerWin.focus();
   }
}


// Opens tip window
function openTipWindow(URL) {
   sFeatures = "width=600,height=400,screenX=400,screenY=10,left=400,top=10,titlebar=yes,status=yes,scrollbars=yes,resizable=yes";
   tipWin = window.open (URL, "Tip", sFeatures);
   if (tipWin.opener == null) {  // for Nav 2.0x
      tipWin.opener = self  // this creates and sets a new property
   }
   if (navigator.userAgent.indexOf("MSIE") == -1)  {
      // MSIE doesn't support focus() method.
      tipWin.focus();
   }
}


// Opens material window for viewing - ca't use this right now
function openMaterialWindow(URL) {
   sFeatures = "width=600,height=400,screenX=300,screenY=30,left=300,top=30,titlebar=yes,status=yes,scrollbars=yes,resizable=yes";
   materialWin = window.open (URL, "Material", sFeatures);
   if (materialWin.opener == null) {  // for Nav 2.0x
      materialWin.opener = self  // this creates and sets a new property
   }
   if (navigator.userAgent.indexOf("MSIE") == -1)  {
      // MSIE doesn't support focus() method.
      materialWin.focus();
   }
}

//  Opens "ReportWindow" to the location specified
//  by String "URL"
function openReportWindow(URL) {
   sFeatures = "width=600,height=600,titlebar=yes,toolbar=yes,location=yes,status=yes,scrollbars=yes,resizable=yes";
   reportWin = window.open (URL, "Report", sFeatures);
   if (reportWin.opener == null) {  // for Nav 2.0x
      reportWin.opener = self  // this creates and sets a new property
   }
   if (navigator.userAgent.indexOf("MSIE") == -1)  {
      // MSIE doesn't support focus() method.
      reportWin.focus();
   }
}

//  Opens "CommentWindow" to the location specified
//  by String "URL"
function openCommentWindow(URL) {
   sFeatures = "width=400,height=300,titlebar=no,toolbar=no,location=yes,scrollbars=no,resizable=no";
   commentWin = window.open (URL, "Comment", sFeatures);
   if (commentWin.opener == null) {  // for Nav 2.0x
      commentWin.opener = self  // this creates and sets a new property
   }
   if (navigator.userAgent.indexOf("MSIE") == -1)  {
      // MSIE doesn't support focus() method.
      commentWin.focus();
   }
}

//  Pops up a deletion confirmation and
//  returns TRUE if confirmed or FALSE
//  if not confirmed.

function confirmDeletion(element) {
   if (element == null || element.length == 0) element = 'this item';
   return confirm("Are you sure you want to permanently delete " + element + "?");
}

function confirmDetach() {
   return confirm("Are you sure you want to detach this file or web page?");
}

//  Checks that the length of the text entered
//  in the form textarea "Textarea" does not exceed
//  the maximum "Maximumsize".
//  If the maximum is not exceeded, this returns TRUE.
//  Otherwise it pops up an alert with the text "Message",
//  focuses on "Textarea", selects all of the text in it,
//  and returns FALSE;
function checkTextareaMaximum(Textarea, Maximumsize) {
  if (Textarea.value.length > Maximumsize) {
    var Message="You have entered more characters " +
      "than the text area allows. Please remove " +
      (Textarea.value.length - Maximumsize) +
      " characters before you continue.";
    alert(Message);
    Textarea.focus();
    Textarea.select();
    return false;
  }
  else
    return true;
}


function limitChar(field, maxlimit){
    var msg1 = "You have reached the maximum character limit of "
    var msg2 = " characters.\n\nExtra characters will be removed."
    var strValue = field.value
    var strLen = strValue.length
    if (strLen > maxlimit){
        field.blur();
        field.focus();
        field.select();
        alert(msg1 + maxlimit + msg2);
    var strNew = strValue.substring(0, maxlimit)
        field.value = strNew;
        return false;
    } else {
       return true;
    }

}

function setTitle(newTitle) {
   document.title=newTitle;
}

//Script created by Jim Young (www.requestcode.com)
//Submitted to JavaScript Kit (http://javascriptkit.com)
//Visit http://javascriptkit.com for this script

//Set the tool tip message you want for each link here.
function showtip(current,e,tip) {
   if (document.layers) // Netscape 4.0+
      {
       theString="<DIV CLASS='ttip'>"+tip+"</DIV>"
       document.tooltip.document.write(theString)
       document.tooltip.document.close()
       document.tooltip.left=e.pageX+14
       document.tooltip.top=e.pageY+2
       document.tooltip.visibility="show"
      }
   else
   {
      if(document.all || document.getElementById) // Netscape 6.0+ and Internet Explorer 5.0+
      {
          var mouseX, mouseY, db=document.body;
          elm=document.getElementById("tooltip")
          elm.innerHTML='<table width="250" cellpadding="0" cellspacing="0" border="0"><tr><td><div>' + tip + '</div></td></tr></table>';

          if (document.getElementById && !document.all) {
              mouseX = e.pageX; mouseY = e.pageY;
              elm.style.left = ((mouseX + current.offsetWidth) > (window.innerWidth-20 + window.pageXOffset)) ? mouseX - current.offsetWidth -10 : window.pageXOffset+mouseX;
          } else {
              mouseX = window.event.clientX + db.scrollLeft;
              mouseY = window.event.clientY + db.scrollTop;
              elm.style.left = ((e.x + current.clientWidth) > (db.clientWidth + db.scrollLeft)) ? (db.clientWidth + db.scrollLeft) - current.clientWidth-10 : mouseX;
          }

          elm.style.top=mouseY+20
          elm.style.visibility = "visible"
      }
   }
}

function hidetip(){
   if (document.layers) { // Netscape 4.0+
      document.tooltip.visibility="hidden"
   } else {
      if(document.getElementById) // Netscape 6.0+ and Internet Explorer 5.0+
         elm.style.visibility="hidden"
   }
}

function portfolioGetElementById(id){
	if (document.getElementById) {
		return (document.getElementById(id));
	} else if (document.all) {
		return (document.all[id]);
	} else {
		if ((navigator.appname.indexOf("Netscape") != -1) && parseInt(navigator.appversion == 4)) {
			return (document.layers[id]);
		}
	}
}

function changeDisplay(id) {
	var elestyle = portfolioGetElementById(id).style;
	if (elestyle.visibility != "hidden") {
		elestyle.visibility = "hidden";
	} else {
		elestyle.visibility = "visible";
	}
}

function recreateTablesConfirm(){
  if (confirm('You are about to recreate your database tables, this will delete any existing data. ' +
        'If you need to preserve information, please backup your database before committing this action. ' +
        'Are you sure you want to do this ?')){
     document.forms[0].action.value = "createTables";
     document.forms[0].submit();
   }
}