// $Author: edc $
// $Date: 2008/11/14 19:53:18 $
// $Source: /usr2/cvsroot/ticketsystem_webfiles_bin/perl_overview.js,v $
// $Revision: 1.16 $
//
// $Log: perl_overview.js,v $
// Revision 1.16  2008/11/14 19:53:18  edc
// changed from prompt() to IEprompt()
//
// Revision 1.15  2008/10/09 17:22:45  edc
// added populateOverviewWithCustnum
//
// Revision 1.14  2004/05/28 13:30:16  goble
// No ticket
//
// Added function openNodeSearch()
//
// Revision 1.13  2004/04/15 14:40:51  goble
// Ticket 53536
//
// Minor fixes and allowing scrollbars for the Add to Log window
//
// Revision 1.12  2004/03/04 20:16:23  goble
// Ticket 51291
//
// Added 'Opened on' to the Time pulldown.
//
// Choosing this option will hide the Time Value pulldown and display the
// Date text box. JavaScript will prevent someone from refreshing the
// Overview screen if 'Opened on' is chosen and the Date box is empty.
// JavaScript also checks to see if the date is not in a good format or
// has data that is off. If so, it clears the Date box and alerts the user
// to the problem.
//
// Revision 1.11  2004/01/28 18:01:57  goble
// Removed an debug alert line
// Added CVS Header tags to top of file
//

function refreshOverview()
   {
   var search           = document.myform.check_search.checked;
   var search_for       = document.myform.search_for.value;
   var re               = new RegExp(/^\s*$/);
   var check_time       = document.myform.check_time.checked;
   var time_what        = document.myform.time_what.selectedIndex;
   var date             = document.myform.date.value;

   if (search && re.test(search_for))
      {
      alert("You must specify a search term to do a Quick or Complete Search");
      }
   else if (check_time && time_what == 5 && re.test(date))
      {
      alert("You must fill in a date (mm/dd/yyyy or mm-dd-yyyy).");
      }
   else
      {
      document.myform.submit();
      }
   }

function view_history_popup(ticket)
	{
	var windowname = "viewhist" + ticket;
	var target = "history.cgi?ticketnumber=" + ticket;
	dummy=window.open(target,windowname,'toolbar=no,scrollbars=yes,status=yes,directories=no,location=no,menubar=no,resizable=yes,width=800,height=600');
	dummy.focus();
	}

function view_history2(ticket)
	{
	if ( ! document.myform.exact_ticket.value )
		{
		var dummy="blah";
		alert('Please specify the Ticket Number that you want to open the View History window for.');
		}
	else
		{
		var windowname="viewhist" + document.myform.exact_ticket.value;
		var target = "history.cgi?ticketnumber=" + document.myform.exact_ticket.value;
		dummy=window.open(target,windowname,'toolbar=no,scrollbars=yes,status=yes,directories=no,location=no,menubar=no,resizable=yes,width=800,height=600');
		dummy.focus();
		}
	}

function window_popup(ticket,program)
	{
	var windowname = "ticket" + ticket;
	var target = program + ".cgi?" + ticket;
	dummy=window.open(target,windowname,'toolbar=no,scrollbars=yes,status=yes,directories=no,location=no,menubar=no,resizable=yes,width=800,height=600');
	dummy.focus();
	}	

function add2log_popup(ticket)
	{
	var Windowname = "add2log" + ticket;
	var target = "add_to_log.cgi?" + ticket;
	dummy=window.open(target,Windowname,'toolbar=no,scrollbars=yes,status=yes,directories=no,location=no,menubar=no,resizable=yes,width=800,height=215');
	dummy.focus();
	}

function add2log_popup2()
	{
	if ( ! document.myform.exact_ticket.value )
		{
		var dummy="blah";
		alert('Please specifiy the Ticket Number that you want to open the ADD2LOG window for.');
		}
	else
		{
		var Windowname = "add2log" + document.myform.exact_ticket.value;
		var target = "add_to_log.cgi?" + document.myform.exact_ticket.value;
		dummy=window.open(target,Windowname,'toolbar=no,scrollbars=no,height=215,width=800');
		dummy.focus();
		}
	}

function select(pic)
	{
	if (document.images)
		{
		var picsrc = pic.src;
		var off = picsrc.lastIndexOf("_off.gif");
		if (off != -1)
			{
			var newpic = picsrc.substring(0,off)+"_on.gif";
			pic.src=newpic;
			}
		}
	window.status='Save Changes';
	}

function deselect(pic)
	{
	if (document.images)
		{
		var picsrc=pic.src;
		var off = picsrc.lastIndexOf("_on.gif");
		if (off != -1)
			{
			var newpic = picsrc.substring(0,off)+"_off.gif";
			pic.src=newpic;
			}
		}
	window.status="PenTeleData Ticket System";
	}

function gotoTicket()
	{
	if ( ! document.myform.exact_ticket.value )
		{
		var dummy="blah";
		alert('Please specifiy the Ticket Number to jump to.');
		}
	else
		{
		url='view.cgi?' + document.myform.exact_ticket.value;
		newWindow=window.open(url,'nodeWindow','toolbars=no,scrollbars=yes,status=yes,directories=no,location=no,menubar=no,resizable=yes,width=800,height=600');
//		newWindow.newForm = this;
		newWindow.focus();
		}
	}

function modifyTicket()
	{
	if ( ! document.myform.exact_ticket.value )
                {
                var dummy="blah";
                alert('Please specifiy the Ticket Number to Edit.');
                }
        else
		{
		windowname='nodeWindow' + document.myform.exact_ticket.value;
		url='modify.cgi?' + document.myform.exact_ticket.value;
		newWindow=window.open(url,windowname,'toolbars=no,scrollbars=yes,status=yes,directories=no,location=no,menubar=no,resizable=yes,width=800,height=600');
//		newWindow.newForm = this;
		newWindow.focus();
		}
	}

function openCustomerSearch() {
    var promptmessage = 'Please enter a Company Name to search for.';
    var default_val = '';

    var cb = function(val) {
        if (!val) {
            alert('You need to enter a Company Name to use this feature.');
            return false;
        }

        var url = 'perl_customernumber.cgi?searchfor=' + val;
        var winprops = 'toolbar=no,scrollbars=yes,status=yes,directories=no,';
        winprops    += 'locations=no,menubar=no,resizable=yes,width=800,';
        winprops    += 'height=600';

        var newWindow = window.open(url,'customerSearch',winprops);
        newWindow.newForm = this;
        newWindow.focus();
    };

    IEprompt(promptmessage,default_val,cb);
}

function openNodeSearch()
   {
   var url        = 'nodes.cgi?what=search';
   var options    = 'toolbar=no, scrollbars=no, status=no, directories=no, ';
   options        += 'location=no, menubar=no, resizable=no, width=270, height';
   options        += '=260';

   var nodeWindow = window.open(url, 'nodeSearchWin', options);
   nodeWindow.focus();
   }

function startClock()
	{ 
	if (document.myform.time.value == null)
		{
		document.myform.time.value = Interval;
		}

   if (document.myform.refresh[0].checked)
      {
   	Interval=Interval-1;
	   if (Interval == -1)
		   {
		   //location.reload();
         refreshOverview();
		   }
	   else
		   {
		   document.myform.time.value=Interval;
		   timeID=setTimeout("startClock()",1000)
		   }
      }
	}

// function newTicket
// This function opens a new edit screen for a new, unique ticket number
// This was created to use JavaScript's Date function to generate a unique window name.

function newTicket()
	{
	var now = new Date();
	var rndnum = now.getTime();
	var windowname = "new" + rndnum;
	var target = "new.cgi";

	dummy=window.open(target,windowname,'toolbar=no,scrollbars=yes,status=yes,directories=no,location=no,menubar=no,resizable=yes,width=800,height=600');
	dummy.focus();
	}

// This function takes the user to login.cgi which authenticates the user and
// redirects them to the appropriate screen.
function logIn(what)
   {
   url   = 'login.cgi?screen=' + what;
   window.location = url;
   }

function logOut()
   {
   url   = 'logout.cgi';
   window.location = url;
   }

function checkDate(date)
   {
   var re            = new RegExp(/(\d\d\/\d\d\/\d\d\d\d)|(\d\d-\d\d-\d\d\d\d)/);
   var error         = '';

   if (! re.test(date.value))
      {
      var error      = 'Date must be in the format: mm/dd/yyyy or mm-dd-yyyy.';
      alert(error);
      date.value     = '';
      }
   else
      {
      var raw_date   = date.value;
      var regexp     = /\//g;

      raw_date       = raw_date.replace(regexp, "-");

      var split_on   = /-/;
      var date_arr   = raw_date.split(split_on);
      var month      = parseInt(date_arr[0]);
      var day        = parseInt(date_arr[1]);
      var year       = parseInt(date_arr[2]);

      if (month > 12 || month < 1)
         {
         error       = 'Months must be entered between the range of 01';
         error       += ' (January) and 12 (December).';
         }
      else if (year > 2037)
         {
         error       = 'The year cannot be greater than 2037.';
         }
      else if (year < 1902)
         {
         error       = 'The year cannot be less than 1902.';
         }
     
      if (error != '')
         {
         alert(error);
         date.value  = '';
         }
      else
         {
         if (month == 2 && ! checkLeapMonth(month, day, year))
            {
            date.value = '';
            }

         if (! checkMonthLength(month, day))
            {
            date.value = '';
            }
         }
      }

   }

// This function determines if the specified month and day can exist.
function checkMonthLength(mm,dd)
   {
   var months = new Array("", "January", "February", "March", "April", "May",
                           "June", "July", "August", "September", "October",
                           "November", "December");
 
   if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30)
      {
      alert(months[mm] + " has only 30 days.");
      return false;      
      }
   else if (dd > 31)
      {
      alert(months[mm] + " has only 31 days.");
      return false;
      }

   return true;
   }

// This function determines if this Feb is a leap year or not. Then
// it determines if the day can exist in that Feb.
function checkLeapMonth(mm,dd,yyyy)
   {
   if (yyyy % 4 > 0 && dd > 28)
      {
      alert("February of " + yyyy + " has only 28 days.");
      return false;
      }
   else if (dd > 29)
      {
      alert("February of " + yyyy + " has only 29 days.");
      return false;
      }

   return true;
   }

// This function helps when searching by Customer. It provides the glue to 
// update the Overview form with Customer # and submit the form.
function populateOverviewWithCustnum(custnum) {
    if (!custnum) return false;

    var f = window.opener.document.myform;
    f.check_search.checked = true;
    f.search_type.value = 6;
    f.search_for.value = custnum;

    f.submit();

    window.close();
}
