function selectAllEntries(checkbox_name) 
{
  $("input[name='" + checkbox_name + "']").attr("checked",true) ;
} // selectAllEntries

function deselectEntries(checkbox_name) 
{
  var all_check_boxes = $("input[name='" + checkbox_name + "']") ; // They all have this class name
  
  for(var i = 0 ; i < all_check_boxes.length ; i++)
  {
	if (all_check_boxes.get(i).checked == true)
 	  all_check_boxes.get(i).checked = false ;
	else  
 	  all_check_boxes.get(i).checked = true ;
  }
} // deselectEntries

// Prepares the ids of the selected entries and puts them in the hidden input which is necessary in list forms  
function presubmitListForm(checkbox_name , result_input_id , warning_msg) 
{
  var result = true ;         // final result
  var selected_entries = "" ; // ids of the selected items
  
  // Ask the warning msg before proceeding
  if (warning_msg.length != 0)
    if (! confirm(warning_msg)) 
	  return false ; 

  var selected_check_boxes = $("input[name='" + checkbox_name + "']") ; // They all have this class name
  for(var i = 0 ; i < selected_check_boxes.length ; i++)
  {
	cur_check_box = selected_check_boxes.get(i) ;
    if (cur_check_box.checked != true)
	  continue ;
	  
	if (selected_entries != "")
	  selected_entries = selected_entries + "','" + cur_check_box.value ;
	else
	  selected_entries = "'" + cur_check_box.value ;  
  }
	
  if (selected_entries == "")
  {
    alert ("Please select the items by clicking on the check box next to them.") ;
    result = false ;
  }
  else
    $("#" + result_input_id).attr("value",selected_entries + "'") ;
  
  return result ;
} // presubmitListForm

// Causes the form to navigate to a specific page
function doPageNavigation(form_name , new_page_no)
{
  var cur_page_path = location.pathname ;  
  var all_args = location.search ;
  
  all_args = all_args.substr(1) ; // remove the starting ?
  all_args = placeFilterArg(all_args,form_name,"{np:" + new_page_no + "}") ;
	  
  document.location = cur_page_path + "?" + all_args ;
} // doPageNavigation

// Causes the form to show specified number of records if user changes the value of a show_record type of element
function handleShowRecords(form_name , show_records)
{
  var cur_page_path = location.pathname ;  
  var all_args = location.search ;
  
  all_args = all_args.substr(1) ; // remove the starting ?
  all_args = placeFilterArg(all_args,form_name,"{sr:" + show_records + "}") ;
	  
  document.location = cur_page_path + "?" + all_args ;
} // handleShowRecords

function handleListSorting(form_name , cur_sort_arg , new_sort_arg)
{
  var cur_page_path = location.pathname ;  
  var all_args = location.search ;
  
  all_args = all_args.substr(1) ; // remove the starting ?
  all_args = placeFilterArg(all_args,form_name,cur_sort_arg) ;
  all_args = placeFilterArg(all_args,form_name,new_sort_arg) ;
	  
  document.location = cur_page_path + "?" + all_args ;
} // handleListSorting

// A filter argument is a value like {filter_id:some value} that should be placed inside of the arguments passed to a form
// This function either adds or replaces the new filter arg into the arguments for that form
function placeFilterArg(page_cur_args , form_name , new_filter_arg)
{
  var form_new_args = "" ;
  
  // debug for now. this is a bug that blocks #:  form_new_args = form_new_args.replace('#','&#35') ;
  
  if (page_cur_args.length == 0)
  {
    page_new_args = form_name + "={" + new_filter_arg + "}" ;
  }
  else
  {
    // First find the filter id for this filter arg so we can check if the filter_id is already in the args or not
    var filter_colon_pos = new_filter_arg.indexOf(":") ;
	var filter_id = new_filter_arg.substr(1,filter_colon_pos - 1) ;
	
    // Now see if there is any previous argument passed for thif form
    var form_params_pos = page_cur_args.indexOf(form_name + "=") ;
    if (form_params_pos == -1) // If there was no previuos argument for this form then build it from scratch
    {
      page_new_args =  page_cur_args + "&" + form_name + "={" + new_filter_arg  + "}" ;
    } // this form params not passed
    else
    {
      //***** the arguments for this form are already in the command line and I should either add or replace the value for this new filter
	  // to the arguments for this form
	  var open_bracket_pos = page_cur_args.indexOf("{",form_params_pos) ;
	  var and_sign_pos = page_cur_args.indexOf("&",form_params_pos) ;
	  if (and_sign_pos == -1)
	    and_sign_pos = page_cur_args.length - 1;
	  var form_cur_args = page_cur_args.substr(open_bracket_pos + 1,and_sign_pos - open_bracket_pos - 1) ; // all the arguments passed to this form
	  
	  // Now find if a value for this filter was already in the form filter values or not
	  var filter_val_start_pos = form_cur_args.indexOf("{" + filter_id + ":") ;
	  if (filter_val_start_pos == -1)
	  {
	    // a value for this filter was not already in the filters for this form, so we need to add the new filter to the end of filter values for this form
		form_new_args = form_cur_args + new_filter_arg ;
	  }
      else
      {
	    // a value for this filter was already in the filters for this form, so we need to replace it with the new filter value
		// find the current value for this filter
		filter_val_start_pos += filter_id.length + 1 ;
		filter_val_end_pos = form_cur_args.indexOf("}",filter_val_start_pos) ;
		filter_cur_value = form_cur_args.substr(filter_val_start_pos + 1,filter_val_end_pos - filter_val_start_pos - 1) ;
		
		// now replace the cur value with the new value
		cur_filter_exp = "{" + filter_id + ":" + filter_cur_value + "}" ;
		form_new_args = form_cur_args.replace(cur_filter_exp,new_filter_arg) ; // new_filter_arg is passed to this function
      }	
      
	  // now replace the form old arguments with the new one
	  page_new_args = page_cur_args.replace(form_cur_args,form_new_args) ;
	  
    } // this form params were already passed
  } // There is already argument 
  
  return page_new_args ;
} // placeFilterArg

// Clears all the filters for a form by simply removing the form_name={.....} from command line
function clearFormFilters(form_name)
{
  var all_args = location.search ; // current arguments for this page
  all_args = all_args.substr(1) ; // remove the starting ?
  
  // See if there is any previous argument passed for thif form
  var form_params_pos = all_args.indexOf(form_name + "={") ;
  if (form_params_pos != -1) // If there  is a filter argument for this form then remove it
  {
	var and_sign_pos = all_args.indexOf("&",form_params_pos) ;
	if (and_sign_pos == -1)
	  and_sign_pos = all_args.length - 1;
	var form_cur_args = all_args.substr(form_params_pos,and_sign_pos - form_params_pos + 1) ; // all the arguments passed to this form
	all_args = all_args.replace(form_cur_args,"") ; // remove current filter arguments

	// Now reload the document with new arguments
    var cur_page_path = location.pathname ;  
    if (all_args.length != 0)
      document.location = cur_page_path + "?" + all_args ;
    else
      document.location = cur_page_path ;
  }
} // clearFormFilters

// When user clicks on an item on a list kind of filter, this function is called to filter the form
// debug should change to do the filtering in the command line like other ones
function handleListClick(list_input_name , new_val)
{
  cur_form = document.forms[0] ;
  cur_form.elements[list_input_name].value = new_val ; 
  cur_form.submit() ;
}

function trim(sString) 
{
  while (sString.substring(0,1) == ' ')
    sString = sString.substring(1, sString.length);
  while (sString.substring(sString.length-1, sString.length) == ' ')
    sString = sString.substring(0,sString.length-1);
  return sString;
} // trim

// Rounds a float number to contain only decimals number of digitst after decimal point. Also adds extra zeros
// if needed
function roundFloat(in_number , decimals , add_zeroes)
{
  if (decimals == undefined)
    decimals = 2 ; // good for taxes
  if (add_zeroes == undefined)	
    add_zeroes = true ;
	
  in_number = Math.round(in_number * 10 * decimals) / (10 * decimals) ; 
  
  return in_number ;  
} // roundFloat

// Helps with positioning of objects
function positionInfo(cur_control) 
{

  var p_elm = cur_control ;

  this.getElementLeft = getElementLeft;
  function getElementLeft() 
  {
    var x = 0;
    var elm;
    if(typeof(p_elm) == "object")
      elm = p_elm;
    else 
      elm = document.getElementById(p_elm);
    
	while (elm != null) 
	{
      x += elm.offsetLeft;
      elm = elm.offsetParent ;
    }
    return parseInt(x);
  }

  this.getElementWidth = getElementWidth;
  function getElementWidth()
  {
    var elm;
    if(typeof(p_elm) == "object")
      elm = p_elm;
    else 
      elm = document.getElementById(p_elm);
    
	return parseInt(elm.offsetWidth);
  }

  this.getElementRight = getElementRight;
  function getElementRight()
  {
    return getElementLeft(p_elm) + getElementWidth(p_elm);
  }

  this.getElementTop = getElementTop;
  function getElementTop() 
  {
    var y = 0;
    var elm;
    if(typeof(p_elm) == "object")
      elm = p_elm;
    else 
      elm = document.getElementById(p_elm);

	while (elm != null) 
	{
      y += elm.offsetTop;
      elm = elm.offsetParent;
    }
    return parseInt(y);
  }

  this.getElementHeight = getElementHeight;
  function getElementHeight()
  {
    var elm;
    if(typeof(p_elm) == "object")
      elm = p_elm;
    else 
      elm = document.getElementById(p_elm);
    
	return parseInt(elm.offsetHeight);
  }

  this.getElementBottom = getElementBottom;
  function getElementBottom()
  {
    return getElementTop(p_elm) + getElementHeight(p_elm);
  }
} // positionInfo

// fromats a number according to format string and returns the string
function formatNum(cur_num , new_format) 
{
  // For now only accept "99"
  if (new_format == "99")
  {
    result = cur_num.toString() ;
	if (result.length < 2)
	  result = "0" + result;
  }
  return result ;
}

// Handles group selection on the checkboxes at the side of the lists
function handleRowSelect(cur_check_box , checkbox_name , event)
{
  var start_index = -1 , end_index = -1 ; 
  var cur_idx,             // Index of the current checkbox being clicked
      before_idx = -1,     // Index of the closest check box before current one which is checked
      after_idx = -1 ;     // Index of the closest check box after current one which is checked
  var cur_form = document.forms[0] ; // There is only one form per page  

  if (event.shiftKey == true)
  {
    // Find index of current selection
	for (i = 0 ; i < cur_form.elements[checkbox_name].length ; i++)
      if (cur_form.elements[checkbox_name][i] == cur_check_box)
	  {
	    cur_idx = i ;
        break ; // break after finding current selection		
	  }
	  
    // Find index of nearest selection before current selection
	for (i = cur_idx - 1 ; i >= 0  ; i--)
      if (cur_form.elements[checkbox_name][i].checked == true)
	  {
	    before_idx = i ;
        break ; // break after finding 		
	  }
	  
    // Find index of nearest selection after current selection
	for (i = cur_idx + 1 ; i < cur_form.elements[checkbox_name].length  ; i++)
      if (cur_form.elements[checkbox_name][i].checked == true)
	  {
	    after_idx = i ;
        break ; // break after finding 		
	  }
	
	// Priority is with after_idx as selection usually moves down 
	if (after_idx != -1)
	{
	  start_index = cur_idx ;
	  end_index = after_idx ;
	}
    else
    {
	  start_index = before_idx ;
	  end_index = cur_idx ;
    }
    // Now do the selection only if both start and end were found. Prevent shifts + single row selecion
    if (start_index != -1 && end_index != -1) 	
      for (i = start_index ; i <= end_index ; i++)
        cur_form.elements[checkbox_name][i].checked = true ;
  } 
}

function changePicture(img_name , new_src) 
{
  document.getElementById(img_name).src = new_src ;
}

// Displays the short help in the hidden div
function displayShortHelp(help_text) 
{
  var short_help_obj = document.getElementById('short_help') ;
  
  short_help_obj.style.display = "block" ;
  short_help_obj.style.position = "absolute" ;
  short_help_obj.style.backgroundColor = "#FFFFFF" ;
  short_help_obj.innerHTML = help_text ;
}

// Bookmarks current page
function bookmarkThisPage()
{
  if (window.sidebar) 
  { // Mozilla Firefox Bookmark		
	window.sidebar.addPanel(title,url,"");	
  } 
  else if( window.external ) 
  { // IE Favorite		
	window.external.AddFavorite(url,title); 
  }  
} // bookmarkThisPage
 
// Bookmarks the whole web site
function bookmarkThisSite(url,title)
{
  if (window.sidebar) 
  { // Mozilla Firefox Bookmark		
	window.sidebar.addPanel(title,url,"");	
  } 
  else if( window.external ) 
  { // IE Favorite		
	window.external.AddFavorite(url,title); 
  }  
} // bookmarkThisSite

// When an action gadget fires, this function sets the action into the gadget_action and submits the form
function doGadgetAction(gadget_action_input_name,action)
{
  cur_form = document.forms[0] ;
  cur_form.elements[gadget_action_input_name].value = action ; //gadget_action_input_name  is name of the hidden input that holds the action to take
  cur_form.submit() ;
}

// Updates a character counter for an element when a key is pressed for that element
// max_chars is max the number of characters that user can put in the element
function updateCharCounter()
{
  var cur_input_id = this.id ;
  var ccounter_id = "cc_" + cur_input_id ;
  var cur_input_len = $("#" + cur_input_id).attr("value").length ;
  var max_len = $("#" + cur_input_id).attr("maxlength") ;
  
  $("#" + ccounter_id).html(parseInt(max_len - cur_input_len)) ;
} // updateCharacterCounter

// Ads the event fn_name to an element given by obj_id
function addEvent(obj_id , event_type , fn_name)
{ 
  var result ;
  
  cur_obj = document.getElementById(obj_id) ;  
  if (cur_obj.addEventListener) // Non IE
  { 
    cur_obj.addEventListener(event_type,fn_name,false) ; 
    result = true; 
  } 
  else if (cur_obj.attachEvent) // IE
    result = cur_obj.attachEvent("on" + event_type,fn_name); 
  else  
    result = false; 

  // alert("result: " + result) ; //debug
  return result ;	
} // addEvent

// Finds and returns the object that has triggered the event 
function eventSourceObj(event)
{
  var source_obj ;
  
  if (event.target) 
    source_obj = event.target;
  else if (event.srcElement) 
    source_obj = event.srcElement;
  if (source_obj.nodeType == 3) // defeat Safari bug
	source_obj = source_obj.parentNode;

  return source_obj ;
} //eventSourceObj 

function handleCalendarNavigation(form_name , new_nav)
{
  var cur_page_path = location.pathname ;  
  var all_args = location.search ;
  
  all_args = all_args.substr(1) ; // remove the starting ?
  all_args = placeFilterArg(all_args,form_name,"{sm:" + new_nav + "}") ;
	  
  document.location = cur_page_path + "?" + all_args ;
} // handleCalendarNavigation

// Delays or sleeps for naptime is mili seconds. Warning: It can not be used to delay between JQuery effects
function sleep(naptime)
{
  naptime = naptime ;
  
  var sleeping = true;
  var now = new Date();
  var alarm;
  var startingMSeconds = now.getTime();
  
  while(sleeping)
  {
    alarm = new Date();
    alarmMSeconds = alarm.getTime();
    if(alarmMSeconds - startingMSeconds > naptime)
	  sleeping = false ;
  }      
} // sleep

// Converts an input date time string to a string like 2010-01-11 12:12:00 so we can use it in the applications
function convertDateTime(in_str , show_only)
{
  var in_date = new Date(in_str) ;
  var result = "" ;
		  
  in_month = in_date.getMonth() + 1 ; // month starts from zero in javascript
  if (in_month < 10) 
	in_month = "0" + in_month ;
	
  in_day = in_date.getDate() ; // month starts from zero in javascript
  if (in_day < 10) 
	in_day = "0" + in_day ;
		  
  // see if we have to put date in the result and if yes, put it
  if (show_only == undefined || show_only == "date") 
    result += in_date.getFullYear() + "-" + in_month + "-" + in_day ;
  
  if (show_only == undefined) // means we should show both
    result += " " ;
  
  if (show_only == undefined || show_only == "time")
  {
    in_hours = in_date.getHours() ; // month starts from zero in javascript
    if (in_hours < 10) 
	  in_hours = "0" + in_hours ;
	  
    in_minutes = in_date.getMinutes() ; // month starts from zero in javascript
    if (in_minutes < 10) 
	  in_minutes = "0" + in_minutes ;
	  
    in_seconds = in_date.getSeconds() ; // month starts from zero in javascript
    if (in_seconds < 10) 
	  in_seconds = "0" + in_seconds ;
    
	result += in_hours + ":" + in_minutes + ":" + in_seconds ; 
  }
  
  return result ;
} // convertDateTime

// Converts a given file size in bytes to readable file size
function toFileSize(file_size)
{
  if (file_size < 1024)
    result = file_size + "B" ;
  else if(file_size < 1024 * 1024) // less than one meg
    result = Math.floor(file_size / 1024) + "KB" ; 
  else if(file_size < 1024 * 1024 * 1024) // less than one gig
    result = Math.floor(file_size / (1024 * 1024)) + "MB" ; 
  else if(file_size < 1024 * 1024 * 1024 * 1024) // less than one gig
    result = Math.floor(file_size / (1024 * 1024 * 1024)) + "GB" ; 

  return result ;	
} // toFileSize
