/********************************************************************
* Filename:      functions.js
* Version:       
* Description:   Some functions are from:
*	          $Id: functions.js,v 1.5 2002/08/19 20:21:35 
*                djresonance Exp $
*                Copyright 2002 Brandon Tallent
*
*                Others are from Mike Von Platen and Kathleen Kihmm
* Author:        Kathleen H. Kihmm <kihmm@hawaii.edu>
* Created at:    Mon Feb 10 14:45:06 2003
* Modified at:   Thu Aug 28 10:37:02 2003
* Modified by:   Kathleen H. Kihmm <kihmm@hawaii.edu>
**********************************************************************/

  var up, down;
  var min1, sec1;
  var cmin1, csec1, cmin2, csec2;

function scrollingwindow(fname,wname,w,h) {
	var wn;
	if (wname == '') {
	wn = 'scrolling_window'
	}
	else wn = wname;
	wn = window.open(fname,wn,'width='+w+',height='+h+',status=no,toolbar=no,scrollbars=yes,resizable=yes');
	wn.window.focus();
}

function plainwindow(fname,wname,w,h) {
	var wn;
	if (wname == '') {
		wn = 'popup_window'
	}
	else wn = wname;
	wn = window.open(fname,wn,'width='+w+',height='+h+',status=no,toolbar=no');
	wn.window.focus();

}

// popup with toolbar
function plainwindow2(fname,wname,w,h) {
	var wn;
	if (wname == '') {
		wn = 'popup_window'
	}
	else wn = wname;
	wn = window.open(fname,wn,'width='+w+',height='+h+',status=no,toolbar=yes');
	wn.window.focus();

}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

  function popup(url, width, height)
  {
      day = new Date();
      id = day.getTime();
      eval("page" + id + " = window.open(url, '" + id + "', 'toolbar = 0, scrollbars = 1, location = 0, statusbar = 0, menubar = 0, resizable = 1, width = " + width + ", height = " + height + "');");
  }

  function popup2(url, width, height)
  {
      day = new Date();
      id = day.getTime();
      eval("page" + id + " = window.open(url, '" + id + "', 'toolbar = 1, scrollbars = 0, location = 0, statusbar = 0, menubar = 0, resizable = 1, width = " + width + ", height = " + height + "');");
  }

  function goto_url(object)
  {
      window.location.href = object.options[object.selectedIndex].value;
  }

  function minutes(data)
  {
      for (var i = 0; i <data.length; i++) if (data.substring(i, i + 1) == ":") break;
      return (data.substring(0, i));
  }

  function seconds(data)
  {
      for (var i = 0; i <data.length; i++) if(data.substring(i, i + 1) == ":") break;
      return (data.substring(i + 1, data.length));
  }

  function Display(min, sec)
  {
      var disp;
      if (min <= 9) disp = "0";
      else disp = " ";
      disp += min + ":";
      if (sec <= 9) disp += "0" + sec;
      else disp += sec;
      return (disp);
  }

  function Down()
  {
      cmin2 = 1 * minutes(document.countdown.start_time.value);
      csec2 = 0 + seconds(document.countdown.start_time.value);
      DownRepeat();
  }

  function DownRepeat()
  {
      if (--csec2 == -1) {
          csec2 = 59;
          cmin2--;
      }

      document.countdown.time.value = Display(cmin2, csec2);
      if ((cmin2 == 0) && (csec2 == 0)) alert("Time's up!");
      else down = setTimeout("DownRepeat()", 1000);
  }
 
// form validation function
// by Kathleen Kihmm
// last modified:  November 6, 2002
function validate(form)
{
  var msg = "";
  var answered = "false";
  var q_num = 1;
  var name;
  
  // start the name equal to first element
  name = form.elements[0].name; 

  // iterate through all elements except for last two:  submit and test_id elements
  for (var y = 0; y < form.length -2; y++) {    
    // this loop interates through all the choices with the same name
    while(form.elements[y].name == name && form.elements[y].type == "radio"){
      if (form.elements[y].type == "radio" && form.elements[y].checked == true)
        answered = "true";  
      y++;
    } 

    if (answered  == "false")     
      msg = msg + "Question number " + q_num + " is blank.\n";
    
    // Reset answered to false 
    // In the case where checked was true, set name to current iteration.  
    answered = "false";
    name = form.elements[y].name;
  
    // decrement counter so the new element name matches with the first occurance
    y = y-1;    
    
    // increment question number
    q_num++;
  }
			
  // Alert the user if incomplete questions are present.  
  // If there is no message return true--form is submitted.
  if (msg > "") {
    alert("The following questions(s) were incomplete:\n\n" 
           + msg + "\n\n Please complete the questions and submit again.")
    return false;			
  }
  else     
    return true;
}	

// form validation function for the ps1 survey forms
// by Kathleen Kihmm
// last modified:  July 17, 2003
function validate_survey(form, sections)
{
  var msg = "";
  var answered = "false";
  var checked = "false";
  var check = 0;
  var q_num = 1;
  var name;

  // start the name equal to first element
  name = form.elements[0].name;

  // iterate through all the radio button questions to see if any are left blank
  for (var y = 0; y < form.length-18; y++) {    
    // this loop interates through all the choices that are radio buttons with the same name
    if (form.elements[y].type == "radio") {
       while(form.elements[y].name == name && form.elements[y].type == "radio"){
          if (form.elements[y].type == "radio" && form.elements[y].checked == true)
             answered = "true";  
          y++;
       }
       if (answered  == "false")     
          msg = msg + "Question number " + q_num + " is blank.\n";
    }

   // iterate through all the checkbox questions to see if any do not have 3 selections 
    else if (form.elements[y].type == "checkbox") {
      // y--;
       for (var z = 0; z < sections; z++) {
         if (form.elements[y].checked == true) 
            check++;
         //msg = msg + "Question number " + form.elements[y].value + "\n";
         y++;
       }
       if (check != 3)
         msg = msg + "Question number " + q_num + " must have EXACTLY 3 seletion selected.\n";
    }

    // Reset answered to false 
    // In the case where checked was true, set name to current iteration.  
    answered = "false";
    checked = "false";
    check = 0;
    name = form.elements[y].name;
  
    // decrement counter so the new element name matches with the first occurance
    y--;    
    
    // increment question number
    q_num++;
  }
			
  // Alert the user if incomplete questions are present.  
  // If there is no message return true--form is submitted.
  if (msg > "") {
    alert("The following questions(s) were incomplete:\n\n" 
           + msg + "\n\n Please complete the questions and submit again.")
    return false;			
  }
  else     
    return true;
}

function validate_delete(form)
{

 var where_to= confirm("Do you really want to go to this page??");
 if (where_to== true)
 {
   window.location="http://yourplace.com/yourpage.htm";
 }
 else
 {
  window.location="http://www.barbie.com";
  }

}


