

function OpenWin(page, winName, w, h, scroll, resize, left, top) {


    winOpen = window.open(page, winName, 'width=' + w + ',height=' + h + ',scrollbars=' + scroll + ',resizable=' + resize + ',left=' + left + ',top=' + top + '');
    winOpen.focus();

    return;
}

/**
 * Shows message, sets focus to desired element, and returns false.
 */
function messageHelper(el, msg) {
  el.select();
  el.focus();
  alert(msg);
  el.focus();
  return false;
}

/**
 * Implements the checkFloat and checkInt functions.
 */
function checkNumber(el, msg, minimum, maximum, allowDecimals, optional) {
  var val = el.value;
  return checkNumberValue(el, val, msg, minimum, maximum, allowDecimals, optional);
}

function checkNumberValue (el, val, msg, minimum, maximum, allowDecimals, optional) {
  var len = val.length;
  if (len == 0) 
    if (!optional)
        return messageHelper(el, msg);
  
  var negAlreadyFound = false;
  var decimalAlreadyFound = false;
  
  for (var i = 0; i < len; i++) {
    var ch = val.charAt(i);
    if ("0123456789".indexOf(ch) != -1) {
      // this is good... continue
    } else if (ch == '-') {
      if (negAlreadyFound) return messageHelper(el, msg);
      negAlreadyFound = true;
    } else if (ch == '.') {
      if (decimalAlreadyFound) return messageHelper(el, msg);
      if (!allowDecimals) return messageHelper(el, msg);
      decimalAlreadyFound = true;
    } else 
      return messageHelper(el, msg); // not a number, sign, or decimal
  }
  
  if (minimum != null) {
    if (parseFloat(val) < minimum) return messageHelper(el, msg);
  }
  if (maximum != null) {
    if (parseFloat(val) > maximum) return messageHelper(el, msg);
  }
  
  return true;
}

/**
 * Verifies that the element contains a valid floating point number, and
 * displays an error message if needed.
 */ 
function checkFloat(el, msg, minimum, maximum, optional) {
  return checkNumber(el, msg, minimum, maximum, true, optional);
}

/**
 * Verifies that the element contains a valid integer, and
 * displays an error message if needed.
 */ 
function checkInt(el, msg, minimum, maximum, optional) {
  return checkNumber(el, msg, minimum, maximum, false, optional);
}

function checkIntValue (el, val, msg, minimum, maximum, optional) {
  return checkNumberValue(el, val, msg, minimum, maximum, false, optional);
}

function arry() {}
var x_days_per_month = new arry();
x_days_per_month[1] = 31; x_days_per_month[2] = 29; x_days_per_month[3] = 31;
x_days_per_month[4] = 30; x_days_per_month[5] = 31; x_days_per_month[6] = 30;
x_days_per_month[7] = 31; x_days_per_month[8] = 31; x_days_per_month[9] = 30;
x_days_per_month[10] = 31; x_days_per_month[11] = 30; x_days_per_month[12] = 31;
function daysInMonth(m, y) {
  if (m == 2) return getFebDays(y);
  else return x_days_per_month[m];
}

// check date in mm/dd/yyyy format
function checkDateValue(obj, msg) {
    var m, d, y; // month, day, and year as integers
	obj_val = obj.value;
    if (obj_val.length == 0)
        return true;
    isplit = obj_val.indexOf('/');
    if (isplit == -1 || isplit == obj_val.length)
        return messageHelper(obj, msg);
    sMonth = obj_val.substring(0, isplit);
    isplit = obj_val.indexOf('/', isplit + 1);
    if (isplit == -1 || (isplit + 1 ) == obj_val.length)
        return messageHelper(obj, msg);
    sDay = obj_val.substring((sMonth.length + 1), isplit);
    sYear = obj_val.substring(isplit + 1);
	
  	if (!checkIntValue(obj, sMonth, msg+"\r\n"+"Please enter a valid month.", 1, 12)) return false;
  	m = parseInt(sMonth);
  
  	// february check is performed below, after we know the year
  	if (!checkIntValue(obj, sDay, msg+"\r\n"+"Please enter a valid day.", 1, x_days_per_month[m])) return false;
  	d = parseInt(sDay);

  	if (!checkIntValue(obj, sYear, msg+"\r\n"+"Please enter a valid year.", 0)) return false;
  	y = parseInt(sYear);
  	if (y < 100) return messageHelper(obj, msg+"\r\n"+"Please enter a four-digit year.");
  	if (y < 1900) return messageHelper(obj, msg+"\r\n"+"Please enter a year after 1900.");
  	if (y > 2100) return messageHelper(obj, msg+"\r\n"+"Please enter a year before 2100.");
  
  	// perform february check
  	if ((m == 2) && d > getFebDays(y)) return messageHelper(obj, "Please enter a valid day.");
  
  	return true;
}

/**
 * Verifies that the elements contain a valid date, and displays an 
 * error message if needed.
 */
function checkDate(mm, dd, yyyy, msg) {
  var m, d, y; // month, day, and year as integers
  
  if (!checkInt(mm, msg+"\r\n"+"Please enter a valid month.", 1, 12)) return false;
  m = parseInt(mm.value);
  
  // february check is performed below, after we know the year
  if (!checkInt(dd, msg+"\r\n"+"Please enter a valid day.", 1, x_days_per_month[m])) return false;
  d = parseInt(dd.value);

  if (!checkInt(yyyy, msg+"\r\n"+"Please enter a valid year.", 0)) return false;
  y = parseInt(yyyy.value);
  if (y < 100) return messageHelper(yyyy, msg+"\r\n"+"Please enter a four-digit year.");
  if (y < 1900) return messageHelper(yyyy, msg+"\r\n"+"Please enter a year after 1900.");
  if (y > 2100) return messageHelper(yyyy, msg+"\r\n"+"Please enter a year before 2100.");
  
  // perform february check
  if ((m == 2) && d > getFebDays(y)) return messageHelper(dd, "Please enter a valid day.");
  
  return true;
}

/**
 * Verifies that the SELECTs contain a valid date, and displays an
 * error message if needed.  This function assumes that the lists
 * only contain valid months, days, and years.
 */
function checkDateSelects(mm, dd, yyyy, msg, is_optional) {
  var m, d, y; // month, day, and year as integers
  m = parseInt(mm.options[mm.selectedIndex].value);
  d = parseInt(dd.options[dd.selectedIndex].value);
  y = parseInt(yyyy.options[yyyy.selectedIndex].value);
  
  if (is_optional && (m<=0) && (d<=0) && (y<=0))
    return true;
    
  if (m<=0) {
    alert(msg+"\r\n"+"Please select a valid month.");
    return false;
  } else if (d<=0) {
    alert(msg+"\r\n"+"Please select a valid day.");
    return false;
  } else if (y<=0) {
    alert(msg+"\r\n"+"Please select a valid year.");
    return false;
  }
  
  if ( (x_days_per_month[m] < d) || ((m == 2) && d > getFebDays(y)) ) {
    alert(msg+"\r\n"+"Please select a valid day.");
    return false;
  }
    
  return true;
}

// pass in year, get back days in that year's February
function getFebDays(y) {
  var febDays = 28;
  if (y % 4 == 0) {
    febDays = 29;
    if (y % 100 == 0) {
      febDays = 28;
      if (y % 400 == 0) {
        febDays = 29;
      }
    }
  }
  return febDays;
}

/*
  Ensures that people can't select an invalid day for the specified month/year.
*/
function repairDays(mm, dd, yyyy) {

  var frm = document.billfrm;
  var m = parseInt(mm.options[mm.selectedIndex].value);
  var y = parseInt(yyyy.options[yyyy.selectedIndex].value);
  if ((m <= 0) || (y <= 0)) return;
  var d_sel = dd.selectedIndex;
  var d_max_valid = daysInMonth(m, y);

  var offset = 1;
  if (parseInt(dd.options[0].value) <= 0) offset = 0;
  for (var i = 31; i >= 28; i--) {
    if (i > d_max_valid) {
//      dd.options.remove(i-1);
      dd.options[i-offset] = null;
    } else 
      dd.options[i-offset] = new Option(""+i, ""+i);
  }
  if (dd.options[d_sel] != null)
    dd.selectedIndex = d_sel;
  else
    dd.selectedIndex = dd.options.length - 1;
  
}

/*
  Trims left side spaces from the given value and returns trimed string
 */
function ltrim(value) {
	if (value == null) return value;
	if (value.length < 1) return value;
	var result = new String(value);
	while (result.charAt(0) == ' ')
	{
		result = result.substr(1);
	}
	return result;
}

/*
  Trims right side spaces in the given value and returns trimed string
 */
function rtrim(value) {
	if (value == null) return value;
	if (value.length < 1) return value;
	var result = new String(value);
	while (result.charAt(result.length - 1) == ' ')
	{
		result = result.substring(0, result.length - 1);
	}
	return result;
}

/*
  Trims spaces from both ends in the given value and returns trimed string
 */
function trim(value) {
	return rtrim(ltrim(value));
}

/*
  Verifies that the specified text element isn't empty.
  If element is empty, then displays the specified message in
  an alert box, and sets focus to the element.
  
  Returns true if the element isn't empty.
*/
function checkNotEmpty(el, msg) {
  if (el.value.length < 1) {
    el.focus();
    alert(msg);
    el.select();
    return false;
  } else
    return true; 
}
