/* ----------------------------------------------------------------------------
	WINDOW FUNCTIONS
---------------------------------------------------------------------------- */ 

/*  open link in main window. 
    this will determine if link is being opened from a popup window 
    and if so, it will close the popup and open link in window that 
    created it.  
    
    link: Link object or url String 
 */
function openInMain(link) {
    if (window.opener!=null) { 
		if (typeof(link)=="Link") url = link.href; 
		else url = link; 
		window.opener.top.location.href = url; 
        window.opener.focus(); 
        self.close(); 
    }
}


/* 
   window components that can be turned on/off: 
   scrollbars, resizable, status, toolbar, menubar, location, directories (netscape only)
*/


/* creates a popup window with specified width and height  */
function popUpWindow(url,w,h) { 
   props = "scrollbars=no,resizable=yes,status=no,menubar=no,toolbar=no,location=no,width=" + w + ",height=" + h; 
   //specify blank window name (2nd arg) in order to have create multiple popups 
   //with this function. 
   win = window.open(url,"",props); 
   win.focus();
}


/* creates a new window with given props. the last 2 args are optional
   
   url: the url 
   mb: menubar? 
   tb: toolbar? 
   l: location bar?
   w: width
   h: height
*/
function newWindow(url,mb,tb,l,w,h) {
   props = "scrollbars=yes,resizable=yes,status=yes,menubar="+mb+",toolbar="+tb+",location="+l;
   if (w!=null) props += ",width="+w;
   if (h!=null) props += ",height="+h; 
   win = window.open(url,"",props); 
   win.focus();
} 

/* closes the current window */
function closeWindow() {
   self.close();	
}

/* 
 Redirect to the specified url String. 
 If this function is called from popup window then
 url will be opened in parent window and popup(self)
 will be closed. 
 
 Args: url 
*/
function redirect(url) {
	if (window.opener==null) { 
		window.top.location.href = url; 
	}
	else {
		window.opener.top.location.href = url; 
        window.opener.focus(); 
        self.close(); 
    }	
}

/* 
  display confirmation message to user. redirect based on their answer. 
  args:
    msg - the msg to display to user
    yesUrl - url to redirect to when answer is YES
    noUrl - url to redirect to when answer is NO
*/
function popUpRedirect(msg,yesUrl,noUrl) {
//msg = yesUrl+"\n"+noUrl+"\n"+msg;   
    if (confirm(msg)) {
    	location.href = yesUrl;		
    }
    else {
		location.href = noUrl;
    }
}

/*
  display confirmation message to user. redirect based on their answer.
  args:
    msg - the msg to display to user
    yesUrl - url to redirect to when answer is YES
*/
function popUpRedirectTrueOnly(msg,yesUrl) {
//msg = yesUrl+"\n"+noUrl+"\n"+msg;
    if (confirm(msg)) {
    	location.href = yesUrl;
    }
}

/* ----------------------------------------------------------------------------
	ADD/DROP/REPLACE COURSE FUNCTIONS
---------------------------------------------------------------------------- */ 

/* displays drop course warning to user 
   and redirects to the given url based on response
   to the dialog box. 
   ok - redirects , cancel/no leaves them at current page
   
   args: 
      c - the course string 
      theUrl - url to redirect to 
*/
function dropCourseWarning(c,theUrl) {
   var msg = "You are about to drop " + c; 
   if(confirm(msg)) {
      parent.location.href=theUrl; 
   }   
}

function confirmDrop(c, url)
{
    var msg = "You are about to drop " + c;
    if (confirm(msg))
    {
        location.href=url;
    }
    else
    {
        location.href="Registration?noListing=true";
    }
}

/* displays replace course warning to user
   and redirects to the given url based on reponse
   to the dialog box
   ok - redirects, cancel/no leaves them at current page
   
   args: 
      c1 - replace course string
      c2 - drop course string
      theUrl - url to redirect to 
 */
function replaceCourseWarning(c1,c2,theUrl) {
   var msg = "You are about to replace " + c2 + " with " + c1; 
   if(confirm(msg)) {
      parent.location.href=theUrl; 
   }   
}


/* ----------------------------------------------------------------------------
	FORM FUNCTIONS
---------------------------------------------------------------------------- */ 

/* 
   gives focus to the first element 
   of the first form on the page. 
   
   optional args: 
     arg 1: an elements[] index 
     arg 2: a forms[] index 
*/
function firstFocus() {
	
   _elem = firstFocus.arguments[0];
   if (_elem==null) _elem = 0; 
   
   _form = firstFocus.arguments[1]; 
   if (_form==null) _form = 0; 

   // are there forms on page? 
   if (document.forms.length > 0) {
   	  //the element to focus on 
      e = document.forms[_form].elements[_elem]; 
      //is this element focusable? 
      if (e.type != "hidden") {
      	e.focus(); 
      }
   }
}


/* global var used by initForm(), formChanged(), checkFormChanged() */
var changed = false;


/* set changed flag to 0 and call first focus 
  args: 
     f - the form to watch changes on. uses global var when no form given
*/
function initForm(f) {
   if (f==null) changed = false; 
   else f.changed = false; 
}

/* turn changed flag on, called by onChange event listener 
   args: 
      f - the form changing. if no form given sets global var 
*/ 
function formChanged(f) {
   if (f==null) changed = true; 	
   else f.changed = true; 
}


/* if form has not changed display the given msg and
   return false so that the form submittal will not go through
  
  args:
     f - the form 
     alertMsg - the message to display when form has not changed 
*/
function checkFormChanged(f,alertMsg) { 
   var b; 
   if (f==null) b = changed 
   else b = f.changed; 
   
   if (alertMsg==null) {
      alertMsg = "no changes have been made"; 
   }

   //form changed, return true so form submittal will continue
   if (b)  {
      return true;
   }
   //form not changed, alert user and 
   //return false so form submittal will not go through
   else {
      alert(alertMsg);
      return false; 
      
   }	
}


/* 
   check the form to see if any fields are empty, alert user accordingly. 
   args: 
      form - the form object
      alertMsg - (optional) the message to display
 */
function checkAllFieldsForEmpty(form, alertMsg) {
   if (alertMsg==null) {
   	alertMsg = "please enter a value";
   }
  
   var e; //form element 
  
   //loop through each element of the form
   for (i = 0; i < form.length; i++) {
      e = form.elements[i]; 

      //only check text fields
      if ( (e.type=="text"||e.type=="password") && e.value.length==0) {

         //alert user , set focus to the empty field 
         //and return false so form submittal will not go through
         alert(alertMsg);
         e.focus(); 	
         return false; 
      }
  }	
   
   return true; 
}


/* 
   check the form to see if any of the given fields are empty 
   
   args: 
     arg1 - the form 
     arg2 - (optional) alert msg
     tail args - form.element indexes
*/
function checkFormFieldsForEmpty(form) {  
   //see if 2nd arg is alert msg 
   secondArg = checkFormFieldsForEmpty.arguments[1]; 
   
   //it is not alertMsg , must be form.element index
   if (typeof (secondArg) != "string") {
      //element indexes start with 2nd arg
      startIndex = 1; 
      //set default alert msg
      alertMsg = "please enter a value"; 
   }
   //2nd arg is alertmsg
   else {
      //element indexes start with 3rd arg
      startIndex = 2; 
      //set alert msg accordingly
      alertMsg = secondArg;
   }
   
   var e; //form element
   
   for (i=startIndex; i < checkFormFieldsForEmpty.arguments.length; i++) {
      index = checkFormFieldsForEmpty.arguments[i]; 
      e = form.elements[index]; 
      
      //see if length of value of element ==0 , alert user,
      //give focus to the offending field and return false 
      //to prevent the form submittal from going through
      if ( (e.type=="text"||e.type=="password") && e.value.length==0) {
         alert(alertMsg);
         e.focus(); 
         return false; 
      }
   }	
   
   return true; 
}


/* 
 arg: e the form element to remove spaces from 
 */
function removeSpaces(e) {
   var s = ""
   var a = e.value.split(' '); 

   for (i=0;i<a.length;i++)
      s += a[i]; 
      
    //alert("final string="+s); 
   e.value = s; 
}


/*
  returns true if field is empty  
  
  arg
     e - the form element 
  
*/
function isFieldEmpty(e) { 
     return ( (e.type=="text"||e.type=="password") && e.value.length==0);
}


/* 
  converts the contents of the given text field to uppercase
  args: 
     f - the field
 */
function fieldToUpperCase(f) {
   f.value = f.value.toUpperCase(); 
}

/* 
  converts the contents of the given text field to uppercase
  args: 
     f - the field
 */
function fieldToLowerCase(f) {
   f.value = f.value.toLowerCase();
}

/* ----------------------------------------------------------------------------
	MISC 
---------------------------------------------------------------------------- */ 
/* print the current window. 
   call this function from the onLoad event of the BODY of 
   a print friendly page
*/
function printPage() {
   if (window.print) {
      window.print()
   }
}


/* trim the given char/string c from the string s.
   if c is not given then assume ',' 
 */
function trim(s,c) {
     if (c==null) c = ',' 
     
     i = s.lastIndexOf(c); 
     if (i!=-1) return s.substring(0,i); 
     else return s; 
}


/* 
  check the value to see if it is a valid number. 
  return true if number, false otherwise. 
  args: val the field value 
 */
function isValidNumber(val) {
	i = parseInt(val);
	    
	//older browsers i is set to 0 
	//instead of NaN when value is not int 
	if (isNaN(i) || i==0) {
		return false; 
	}
	else {
		return true; 
	}
}



/* 
  Fix the value of the given field to be a properly formed decimal.
  args: 
    f: the field to validate 
    scale: (optional) number of digits to the right of decimal, defaults to 2 
 */ 
function fixDecimal(f,scale) {
	val = f.value; 
	if (scale==null) scale = 2; 
	
	ind = val.indexOf("."); 
	tail = ind==-1 ? "" : val.substring(ind+1); 

//alert("\nval:"+val+"\nscale:"+scale+"\nind:"+ind+"\ntail:"+tail);
	
	if (tail.length != scale) {
		pad = ""; 
		if (ind==-1) pad = "."; 
		if (val.length==0) pad = "0."; 
		
		for (i=1; i <= scale - tail.length; i++) {
			pad += "0"; 
		}
		
		val = val + pad; 
	}
	
	f.value = val; 
//alert (val); 	

}

function toggleBox(obj) { // 1 visible, 0 hidden
var el = document.getElementById(obj);
	if(el.style.display != "block")
	{
	el.style.display = "block";
	}
	else
	{
	el.style.display = "none";
	}
}


