// name:    adminutilities.js                                     
// author:  todd@hampson.us                                            
// date:    2007-09-19                                                  
// purpose: admin utilities for RISD     

// alert('in asco adminutilities');                                    

function refresh ()
{
  history.go(0);
}

function reload ()
{
  history.go(0);
}

function expireText ()
{
  if (document.mainform.abouttoexpire.checked == true)
  {
    alert ('This option shows jobs which are about to expire.');
  }
  else
  {
    alert ('This option shows all jobs and internships regardless of expiration date.');
  }
}

function toggleOnline (id, val) // single job, currently unused
{
  // alert('Toggling ' + id + ' to ' + val);
  document.mainform.action.value = 'toggle';
  document.mainform.id.value = id;
  document.mainform.togglevalue.value = val;
  document.mainform.submit();
}

function confirmMultipleOnline ()
{
  idlist = "0";
  invlist = "0";
  for (i=0; i < document.mainform.elements.length; i++)
  {
    if (document.mainform.elements[i].name )
    {
      if (document.mainform.elements[i].name.match("online_check") )
      {
        if (document.mainform.elements [i].checked)
        {
          idlist = idlist + ", " + document.mainform.elements[i].value;
        }
        else
        {
          invlist = invlist + ", " + document.mainform.elements[i].value;
        }
      }
    }
  }
  if ( confirm ('Do you want to update online status for these jobs?') == true )
  {
    document.mainform.idlist.value = idlist;
    document.mainform.invlist.value = invlist;
    document.mainform.action.value = 'onlinemultiple';
    document.mainform.submit();
  }
}

function confirmMultipleDelete ()
{
  idlist = "0";
  invlist = "0";
  for (i=0; i < document.mainform.elements.length; i++)
  {
    if (document.mainform.elements[i].name )
    {
      if (document.mainform.elements[i].name.match("delete_check") )
      {
        if (document.mainform.elements [i].checked)
        {
          idlist = idlist + ", " + document.mainform.elements[i].value;
        }
        else
        {
          invlist = invlist + ", " + document.mainform.elements[i].value;
        }
      }
    }
  }
  if ( confirm ('Do you want to update delete status for these jobs?') == true )
  {
    document.mainform.idlist.value = idlist;
    document.mainform.invlist.value = invlist;
    document.mainform.action.value = 'deletemultiple';
    document.mainform.submit();
  }
}

function confirmMultipleContact ()
{
  idlist = "0";
  invlist = "0";
  document.mainform.action.value = 'init';
  dosub = false;
  for (i=0; i < document.mainform.elements.length; i++)
  {
    if (document.mainform.elements[i].name )
    {
      if (document.mainform.elements[i].name.match("contact_check") )
      {
        if (document.mainform.elements [i].checked)
        {
          idlist = idlist + ", " + document.mainform.elements[i].value;
        }
        else
        {
          invlist = invlist + ", " + document.mainform.elements[i].value;
        }
      }
    }
  }
  if ( confirm ('Do you want to update contact status for these jobs?') == true )
  {
    document.mainform.idlist.value = idlist;
    document.mainform.invlist.value = invlist;
    document.mainform.action.value = 'contactmultiple';
    dosub = true;
  }
  if (confirm ('Do you want to sort these jobs by contact flag?') == true ) 
  {
    document.mainform.orderby.value = 'contactflag';
    document.mainform.ascdesc.value = 'desc';
    dosub = true;
  }
  if (dosub) {document.mainform.submit();}
}

function resizeWindow(w, h) // for single image
{
  if ( document.images[0] )
  {
    window.resizeTo(document.images[0].width+w,document.images[0].height+h);
  }
  self.focus();
}

function popupWindow (url,n,opt) // correct order
{
  // alert ('in popupWindow');
  if (opt == '' || opt == 'default')
  {
    opt = 'height=250,width=250,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,titlebar=no';
  }
  var newwin = window.open(url,n,opt);
}

function someDisciplineChecked ()
{
  for (i = 0; i < document.mainform.discipline.length; i++) 
  {
    if ( document.mainform.discipline[i].checked ) {return true;}
  }
  for (i = 0; i < document.mainform.majordisc.length; i++) 
  {
    if ( document.mainform.majordisc[i].checked ) {return true;}
  }
  return false;
}

function goodDate (d)
{
  return (d.match(/[0-2][0-9]\/[0-3][0-9]\/20[0-9][0-9]/));
}

function goodPhone (p)
{
  return (p.match(/[0-9.-]+/));
}

function fieldsValidated ()
{
  // alert('Confirming fields');
  errors = "";
  
  if (document.mainform.type.value == '') {errors = errors +  "\nType is required";}
  if (document.mainform.company.value == '') {errors = errors +  "\nCompany Name is required";}
  if (document.mainform.contact.value == '') {errors = errors +  "\nContact Name is required";}
  if (document.mainform.phone.value == '') {errors = errors +  "\nPhone number is required";}
  if (document.mainform.email.value == '') {errors = errors +  "\nE-mail address is required";}
  if (document.mainform.state.value == '' && (document.mainform.country.value == 'US' || document.mainform.country.value == 'USA')) {errors = errors +  "\nState is required if country is US or USA";}
  if (document.mainform.state.value != '' && (document.mainform.country.value != 'US' && document.mainform.country.value != 'USA')) {errors = errors +  "\nState is not required for foreign countries";}
  if (document.mainform.country.value == '' && document.mainform.othercountry.value == '') {errors = errors +  "\nCountry must be selected from the list or entered in 'other' field";}
  if (document.mainform.coverview.value == '') {errors = errors +  "\nOverview/Description is required";}
  if (document.mainform.title.value == '') {errors = errors +  "\nTitle is required";}
  if (document.mainform.title.value == 'Other' && document.mainform.othertitle.value == '') {errors = errors +  "\nIf 'other' title is selected, it must be specified in text box.";}
  if (document.mainform.deadline.value != '')
  {
    if ( goodDate(document.mainform.deadline.value) == false) {errors = errors +  "\nDeadline date, if given must be in mm/dd/yyyy format"; }
  }
  // if (document.mainform.compensation.value == '') {errors = errors +  "\nCompensation is required";}
  if (document.mainform.jdescription.value == '') {errors = errors +  "\nDescription is required";}
  if (document.mainform.jreqqual.value == '') {errors = errors +  "\nRequirements and Qualifications are required";}
  if (document.mainform.japply.value == '') {errors = errors +  "\nApplication Instructions are required";}
  if (!someDisciplineChecked()) {errors = errors + "\nSome discipline(s) or a major category must be selected";}
  if (errors != "")
  {
   alert('Please correct the following errors:\n'+errors);
   return false;
  }
  else
  {
    return true;
  }
}

function reorder (column,ascdesc,maxrows)
{
  document.mainform.orderby.value = column;
  document.mainform.ascdesc.value = ascdesc;
  document.mainform.maxrows.value = maxrows;
  document.mainform.submit();
}

function confirmExpire ()
{
  if ( confirm ('Do you want to take expired jobs offline?\n\n(This is 28 days for regular positions and 365 days for Internships.)') == true )
  {
    window.location.href='adminautooffline.cfm';
  }
}

function confirmLogout ()
{
  if ( confirm ('Do you want to logout?') == true )
  {
    window.location.href='adminlogout.cfm';
  }
}

function confirmCloseWindow ()
{
  if ( confirm ('Do you want to close this window?') == true )
  {
    window.close();
  }
}

function confirmUpdate (id)
{
  if ( confirm ('Do you want to update this record?') == true )
  {
    document.mainform.id.value = id;
    document.mainform.action.value = 'update';
    document.mainform.submit();
  }
}


function confirmDelete (id)
{
  if ( confirm ('Do you want to delete this record?') == true )
  {
    document.mainform.id.value = id;
    document.mainform.action.value = 'delete';
    document.mainform.submit();
  }
}

// end of utilities
