/*
 *$Header: /bit10/htdocs/support.dev.intranet.bit10.net/javascript/support.js 4     27/04/05 12:10 Stuartj $
 *Short:
 *$Log: /bit10/htdocs/support.dev.intranet.bit10.net/javascript/support.js $
 * 
 * 4     27/04/05 12:10 Stuartj
 * Added Support for Firefox under w3c
 * 
 * 3     22/04/05 16:34 Stuartj
 * updated for support credits
 * 
 * 2     1/05/03 17:27 Steveg
 * 
 * 1     21/11/02 14:17 Sglendinning
 * added file to source control

*/

var _B10_wndChild;
var _B10_boolOldBrowser = parseInt(navigator.appversion) < 3;

/* Variables used in reports.cfm for the handling 
  of selection and deselection of clients and projects
*/
var arrClientID = new Array();  //Aray to hold the selected client ids
var arrClientText = new Array(); //Array to hold corresponding client names (for display)
var arrProjectID = new Array(); //Array to hold the selected project ids
var arrProjectText = new Array(); //Array to hold corresponding project names (for display)
var selectedCount  = 0; //Variable to keep track of number of selected items (in Netscape 4 this is restricted to 12)

var arrToggledSection = new Array(); //Array to hold ids of sections toggled in a page (for passing to other pages and state control)

/*=======================================================================================================================
  General bit10 functions for spawning popups etc.
  =======================================================================================================================
*/

// B10_spawnWindow() -- spawn a child browser window
function B10_spawnWindow(name, content, width, height, scrollbars) {
  _B10_wndChild = B10_spawnWindowAndReturn(name, content, width, height, scrollbars);
}

// B10_spawnWindowAndReturn() -- spawn a child browser window and return a reference to the new window
function B10_spawnWindowAndReturn(name, content, width, height, scrollbars) {
  var strAttribs = 'toolbar=no,width=' + width + ',height=' + height + ',directories=no,status=no,scrollbars=' + ((scrollbars)?'yes':'no') + ',resizeable=yes,menubar=no,location=no,screeny=50,screenx=50,alwaysRaised';
  var objWindow = window.open(content, name, strAttribs);
  if (!objWindow.opener) { objWindow.opener = window; }
  if (!_B10_boolOldBrowser)  { objWindow.focus();         }
  return objWindow;
}

// B10_spawnWindowAndWrite() -- spawn a child browser window and write the given contents to it
function B10_spawnWindowAndWrite(name, content, width, height, scrollbars) {
  var objWindow = B10_spawnWindowAndReturn(name, '', width, height, scrollbars);
  with (objWindow.document) {
    open("text/html", "replace");
    write(content)
    close();
  }
}

// B10_spawnPrintWindow() -- spawn a child browser window and return a reference to the new window
function B10_spawnPrintWindow(name, content, width, height, scrollbars) {
  var strAttribs = 'toolbar=no,width=' + width + ',height=' + height + ',directories=no,status=no,scrollbars=' + ((scrollbars)?'yes':'no') + ',resizeable=yes,menubar=yes,location=no,screeny=50,screenx=50,alwaysRaised';
  var objWindow = window.open(content, name, strAttribs);
  if (!objWindow.opener) { objWindow.opener = window; }
  if (!_B10_boolOldBrowser)  { objWindow.focus();         }
  
}

/*=======================================================================================================================
   General display functions
  =======================================================================================================================
*/


/*toggleSection - used for changing the visibility of a layer.
    Takes 3 parameters 
        strNode  - the name of the table to toggle (currently tables names of the form of the form table_strNode
        strImageClosed - the image to show on the toggle button when closed (hidden)
        strImageOprn- the image to show on the toggle button when open (visible)
        Optional 4th parameter lists the other sections to close when opening the specified one.
*/
function toggleSection(strNode,strImageClosed,strImageOpen)
  {
    this.w3c=(document.getElementById)?true:false;
    this.ns4=(document.layers)?true:false;
    this.ie4=(document.all && !this.w3c)?true:false;
    this.ie5=(document.all && this.w3c)?true:false;
    this.ns6=(this.w3c && navigator.appName.indexOf("Netscape")>=0 )?true:false;

	if (this.w3c)
    {
      if (document.getElementById('table_' + strNode).style.display  == '')
      {
        document.getElementById('table_' + strNode).style.display  = 'none';
        document.getElementById('tblimg_' + strNode).src = strImageClosed;
        //Check if its already in the array of objects
        for (i = 0; i < arrToggledSection.length; i++)
        {
          if( arrToggledSection[i] == strNode )
          {
            //Remove the item from arrays.                 
            arrStart = arrToggledSection.slice(0,i);
            arrEnd = arrToggledSection.slice(i + 1,arrToggledSection.length);
            arrToggledSection = arrStart.concat(arrEnd);
          }
        }
      }
      else{
        document.getElementById('table_' + strNode).style.display  = '';
        document.getElementById('tblimg_' + strNode).src = strImageOpen;
        //add to toggledSection array;
        arrToggledSection[arrToggledSection.length] = strNode;
        document.frmRequest.hdnOpenSection.value = strNode;

        //if necessary hide the other sections
          if (arguments.length >= 4)
          {
            closesections = new Array();
            for (i = 3; i < arguments.length; i ++)
            {
              document.getElementById('table_' + arguments[i]).style.display = 'none';            
              document.getElementById('tblimg_' + arguments[i]).src = strImageClosed; 
            } 
          }
       }
    }
    else if (this.ie4 || this.ie5){
//        document.all['toc_' + strNode].style.display
      if (document.all['table_' + strNode].style.display == ''){
        document.all['table_' + strNode].style.display = 'none';            
        document.all['tblimg_' + strNode].src = strImageClosed;
        //Check if its already in the array of objects
              for (i = 0; i < arrToggledSection.length; i++)              
              {
                if( arrToggledSection[i] == strNode )
                {
                    //Remove the item from arrays.                 
                    arrStart = arrToggledSection.slice(0,i);
                    arrEnd = arrToggledSection.slice(i + 1,arrToggledSection.length);
                    arrToggledSection = arrStart.concat(arrEnd);
                    document.all.hdnOpenSection.value = arrToggledSection;    
                }
              }
      }
      else{
        document.all['table_' + strNode].style.display = ''        
        document.all['tblimg_' + strNode].src = strImageOpen;
        //add to toggledSection array;       
        arrToggledSection[arrToggledSection.length] = strNode
        //if necessary hide the other sections
          if (arguments.length >= 4)
          {
            closesections = new Array();
            for (i = 3; i < arguments.length; i ++)
            {
              document.all['table_' + arguments[i]].style.display = 'none';            
              document.all['tblimg_' + arguments[i]].src = strImageClosed; 
            } 
          }
        document.all.hdnOpenSection.value = arrToggledSection;    
      }
    }
    else if (this.ns6)
    {
      if (document.frmRequest['table_' + strNode].style.display  == '')
      {
        document.frmRequest['table_' + strNode].style.display  = 'none';
        document.frmRequest['tblimg_' + strNode].src = strImageClosed;
        //Check if its already in the array of objects
        for (i = 0; i < arrToggledSection.length; i++)
        {
          if( arrToggledSection[i] == strNode )
          {
            //Remove the item from arrays.                 
            arrStart = arrToggledSection.slice(0,i);
            arrEnd = arrToggledSection.slice(i + 1,arrToggledSection.length);
            arrToggledSection = arrStart.concat(arrEnd);
          }
        }
      }
      else{
        document.frmRequest['table_' + strNode].style.display  = '';
        document.frmRequest['tblimg_' + strNode].src = strImageOpen;
        //add to toggledSection array;
        arrToggledSection[arrToggledSection.length] = strNode;
        document.frmRequest.hdnOpenSection.value = strNode;

        //if necessary hide the other sections
          if (arguments.length >= 4)
          {
            closesections = new Array();
            for (i = 3; i < arguments.length; i ++)
            {
              document.frmRequest['table_' + arguments[i]].style.display = 'none';            
              document.frmRequest['tblimg_' + arguments[i]].src = strImageClosed; 
            } 
          }
       }
    }
  }
  

/*=======================================================================================================================
   Set of functions used for the clickable tree selection in reports.cfm
 =======================================================================================================================
*/
  
  
  /*Function for writing to the selected layer box in reports.cfm
    */
    function updateDisplay()
    {
       strSelected = "";
       //Loop  through the project/client array pairs in turn - adding the relavent information to the text variable
       for (i = 0; i < arrClientID.length; i++)
       {
          truncText = arrClientText[i]
         if (AT.ns4 && truncText.length > 36)
         {
         truncText = truncText.substr(0,33) + "..."
         }
         strSelected = strSelected + "<a href=\"javascript:objDeselect(\'" + arrClientID[i] + "\',\'client\',\'" + arrClientText[i]  + "'\)\" class='toc2'> " + truncText + "</a>";
         if (AT.ie4 || AT.ie5 || AT.ns6 )
         {
          strSelected = strSelected + "<br>";
         }
       }
       for (i = 0; i < arrProjectID.length; i++)
       {
          truncText = arrProjectText[i]
         if (AT.ns4 && truncText.length > 36)
         {
         truncText = truncText.substr(0,33) + "..."
         }

         strSelected = strSelected + "<a href=\"javascript:objDeselect(\'" + arrProjectID[i] + "\',\'project\',\'" + arrProjectText[i]  + "'\)\" class='toc2'  > " + truncText + "</a> ";
         if (AT.ie4 || AT.ie5 || AT.ns6 )
         {
          strSelected = strSelected + "<br>";
         }
       }

           //output the variable holding the display info
        if (strSelected == "") { strSelected = "&nbsp;";}
			if(AT.w3c)
			{
				document.getElementById('projectTitle').innerHTML = unescape(strSelected);
			}
            else if( AT.ie4 || AT.ie5 )
            {
              document.all.projectTitle.innerHTML  = strSelected;
            }
            else if (AT.ns6)
            {
              document.frmRequest.projectTitle.innerHTML  = unescape(strSelected);
            }
            else if (AT.ns4)
            {
             //    alert(AT.w_y);
               AT.w_x=(AT.ie5||AT.ie4)?document.body.clientWidth:window.innerWidth;
             document.layers.projectTitle.left = (AT.w_x / 2) + 60;
             if (AT.w_x < 1000)
             {
              document.layers.projectTitle.left = document.layers.projectTitle.left + 40;
             }
             if (document.layers.projectTitle.left < 550)
             {
             document.layers.projectTitle.left = 550
             }
              document.layers.projectTitle.document.open();
              strSelected = strSelected.replace("/%20/", "&nbsp;");
              re = /%20/gi;
              str = strSelected;
              str=str.replace(re, "&nbsp;");
              re = /%2E/gi;
              newstr=str.replace(re, "&nbsp;");
              document.layers.projectTitle.document.write(unescape(newstr));
              document.layers.projectTitle.document.close();

         
            }

            
    }

    /*    Function for handling the selection of an object in reports.cfm
         Parameters:
          objID - The UID of the selected item
          objType - Whether its a project or a client
          objTitle -  The display text 
    */
    function objSelect(objID,objType,objTitle)
    {   
      if (selectedCount == 12 && AT.ns4)
      {
        alert("Too many items selected, please deselect some items first.");
        return;
      }
          //Add to the relevant array of objects.
           if (objType == 'client')
           {
              //Check if its already in the array of objects
              for (i = 0; i < arrClientID.length; i++)
              {
                if( arrClientID[i] == objID )
                {
                  alert("This client is already selected");
                  return;
                }
              }
              //Add the id to the id array
               arrClientID[arrClientID.length] = objID;
              //Add the text to the text array    
               arrClientText[arrClientText.length] = objTitle;
          }
           else 
           {
           //Check if its already in the list of objects
              //Check if its already in the array of objects
              for (i = 0; i < arrProjectID.length; i++)
              {
                if( arrProjectID[i] == objID )
                {
                  alert("This project is already selected");
                  return;
                }
              }
              //Add the id to the id array
               arrProjectID[arrProjectID.length] = objID;
              //Add the text to the text array    
               arrProjectText[arrProjectText.length] = objTitle;

              }
           //Update the display
           selectedCount ++;
           updateDisplay();

    }

    /*    Function for handling the deselection of an object in reports.cfm
         Parameters:
          objID - The UID of the selected item
          objType - Whether its a project or a client
          objTitle -  The display text 
    */
    function objDeselect(objID,objType,objTitle)
    {
        //Take out of the relevant array of objects.
           if (objType == 'client')
           {
              //Check if its already in the array of objects
              for (i = 0; i < arrClientID.length; i++)
              {
                if( arrClientID[i] == objID )
                {
                    //Remove the item from both arrays.                 
                    arrStart = arrClientID.slice(0,i);
                    arrEnd = arrClientID.slice(i + 1,arrClientID.length);
                    arrClientID = arrStart.concat(arrEnd);
                    arrStart = arrClientText.slice(0,i);
                    arrEnd = arrClientText.slice(i + 1,arrClientText.length);
                    arrClientText = arrStart.concat(arrEnd);
                }
              }
           }
           else 
           {
              //Check if its already in the array of objects
              for (i = 0; i < arrProjectID.length; i++)
              {
                if( arrProjectID[i] == objID )
                {
                    //Remove the item from both arrays.                 
                    arrStart = arrProjectID.slice(0,i);
                    arrEnd = arrProjectID.slice(i + 1,arrProjectID.length);
                    arrProjectID = arrStart.concat(arrEnd);
                    arrStart = arrProjectText.slice(0,i);
                    arrEnd = arrProjectText.slice(i + 1,arrProjectText.length);
                    arrProjectText = arrStart.concat(arrEnd);

                }
              }
           
           }
           //Update the display
           selectedCount --;
           updateDisplay();
    }


     /* ReportSearch() - Function to validate the inputs to the reports search page, and submit the form to generate a report.
     */
     function ReportSearch() 
     {
      //Check there are some projects / clients selected
      if (arrClientID.length == 0 && arrProjectID.length == 0)
      {
        alert("Please select projects / clients you wish to appear in the report.");
        return;
      }
      
        //Add them to the hidden fields.
        for (i= 0; i < arrClientID.length; i ++)
        {
         document.frmRequest.hdnClientID.value =  document.frmRequest.hdnClientID.value + "'"  + arrClientID[i] + "'";

         if (i+ 1 < arrClientID.length)
         {
          document.frmRequest.hdnClientID.value = document.frmRequest.hdnClientID.value + ',';
         }
         
        }
        for (i= 0; i < arrProjectID.length; i ++)
        {
         document.frmRequest.hdnProjectID.value =  document.frmRequest.hdnProjectID.value + "'" + arrProjectID[i] + "'";

         if (i+ 1 < arrProjectID.length)
         {
          document.frmRequest.hdnProjectID.value = document.frmRequest.hdnProjectID.value + ',';
         }

        }
        //Check the dates are either fully selected or null.
        if (!((document.frmRequest.selStartDay.selectedIndex == 0 && document.frmRequest.selStartMonth.selectedIndex  == 0 && document.frmRequest.selStartYear.selectedIndex  == 0) || (!document.frmRequest.selStartDay.selectedIndex == 0 && !document.frmRequest.selStartMonth.selectedIndex  == 0 && !document.frmRequest.selStartYear.selectedIndex  == 0)))
        {
          alert("Enter either nothing or a full start date, partial dates are not allowed.");
          return;
        }
        if (!((document.frmRequest.selEndDay.selectedIndex == 0 && document.frmRequest.selEndMonth.selectedIndex  == 0 && document.frmRequest.selEndYear.selectedIndex  == 0) || (!document.frmRequest.selEndDay.selectedIndex == 0 && !document.frmRequest.selEndMonth.selectedIndex  == 0 && !document.frmRequest.selEndYear.selectedIndex  == 0)))
        {
          alert("Enter either nothing or a full end date, partial dates are not allowed.");
          return;
        }
        
        //All other fields are optional at the minute.        
        document.frmRequest.submit();

     } 
     
     
 /*=======================================================================================================================
   Functions used in the process of adding a new support request (using logRequest.cfm)
 =======================================================================================================================
*/

/* ReportSearch() - Function to validate the inputs to the add a request page, and submit the form to create a new support request.
 */
function addRequest()
{

//close the sections - expanding the correct ones for the missing data.
toggleSection('1','img/plus_icon.gif','img/minus_icon.gif','1','2','3')
toggleSection('2','img/plus_icon.gif','img/minus_icon.gif','1','2','3')
toggleSection('3','img/plus_icon.gif','img/minus_icon.gif','1','2','3')


var stralert="";

 //section 1
    //Check that a project has been chosen.
    if (document.frmRequest.hdnProjectCode.value == "")
    {
      alert('Please enter the project this request is to do with.');
      toggleSection('1','img/plus_icon.gif','img/minus_icon.gif')
      return;
    }
 //section 2
    //Check that a category has been specified , Check reproducibility, Check summary, Check description, Check how to reproduce.
    
    if (document.frmRequest.cboSupportType.selectedIndex == 0) { stralert = 'Please enter the category this request falls into.\n'}
	var found = false;
	for(var i = 0; i < document.frmRequest.cboPriority.length; i++) {
		if(document.frmRequest.cboPriority[i].checked) found = true;
	}
	if(document.frmRequest.cboPriority.value != '') found = true;
	if (!found && projectStatus[document.frmRequest.hdnProjectCode.selectedIndex] == 200000) { stralert = 'Please select a resolution level for this request.\n'}
	if (document.frmRequest.cboSupportRepeatability.selectedIndex == 0) {  stralert = stralert + 'Please indicate if the problem is repeatable.\n';}
    if (document.frmRequest.txtSupportTitle.value == "") {  stralert = stralert + 'Please enter a summary of the issue.\n';}
    if (document.frmRequest.txtSupportDescription.value == "") {  stralert = stralert + 'Please enter a description of the issue.\n';}
    if (document.frmRequest.txtSupportStepsToReproduce.value == "" && document.frmRequest.cboSupportRepeatability.selectedIndex < 3) {  stralert = stralert + 'Please indicate the steps which can be taken  to replicate this issue..\n';}
	if( stralert != "")
    {  alert(stralert);
       toggleSection('2','img/plus_icon.gif','img/minus_icon.gif');
      return;
    }
 //section 3
    //No compulsory fields
//submit the form
    document.frmRequest.action = 'logRequest_action.cfm';
    document.frmRequest.submit();
}


/*=======================================================================================================================
   Functions used to enable tracking the open parts of the home page when linking from it.
 =======================================================================================================================
*/

function linkTo(url)
{
 
 if( url.search(/\?/) != -1)
 {
  strSection = "&open=";
 }
 else
 {
   strSection = "?open=";
   }
 
  for (i=0;i<arrToggledSection.length;i++)
  {
       strSection = strSection + arrToggledSection[i] + ",";
  }
  //add the csv to the url
  url = url + strSection;
  //redirect to the url.
 document.location.href = url;
}


//
//  B10_toggleTOC() -- toggle table of contents
//
function B10_toggleTOC() {
if(AT.w3c)
{
	if (document.getElementById('tbl_toc').style.display == 'none') 
  {
    document.getElementById('ctlShow_toc').style.display  = 'none';
    document.getElementById('ctlHide_toc').style.display  = '';
    document.getElementById('tbl_toc').style.display      = '';
  } 
  else 
  {
    document.getElementById('ctlShow_toc').style.display  = '';
    document.getElementById('ctlHide_toc').style.display  = 'none';
    document.getElementById('tbl_toc').style.display      = 'none';
  }
}
 else if( AT.ie4 || AT.ie5 )
 {
  if (document.all['tbl_toc'].style.display == 'none') 
  {
    document.all['ctlShow_toc'].style.display  = 'none';
    document.all['ctlHide_toc'].style.display  = '';
    document.all['tbl_toc'].style.display      = '';
  } 
  else 
  {
    document.all['ctlShow_toc'].style.display  = '';
    document.all['ctlHide_toc'].style.display  = 'none';
    document.all['tbl_toc'].style.display      = 'none';
  }
 }
 else if( AT.ns6)
 {

  if (document.frmQuickSearch['tbl_toc'].style.display == 'none') 
  {
    document.frmQuickSearch['ctlShow_toc'].style.display  = 'none';
    document.frmQuickSearch['ctlHide_toc'].style.display  = '';
    document.frmQuickSearch['tbl_toc'].style.display      = '';
   } 
   else 
   {
    document.frmQuickSearch['ctlShow_toc'].style.display  = '';
    document.frmQuickSearch['ctlHide_toc'].style.display  = 'none';
    document.frmQuickSearch['tbl_toc'].style.display      = 'none';
    }
  }
}
