var childWindow;

function openCenteredWindow(url, w, h)
{	
  var windowFeatures;
	var width = w,
	    height = h,
			left, top;
	
	if ( screen.availWidth <= width )
	{
	  width = screen.availWidth;
	}
	else
	{
	  left = parseInt((screen.availWidth/2) - (width/2));
	}
	
	if ( screen.availHeight <= height )
	{
	  height = screen.availHeight;
	}
	else
	{
	  top = parseInt((screen.availHeight/2) - (height/2));
	}

  windowFeatures = "width=" + width + ",height=" + height + ",status=0,scrollbars=1,resizable=1,left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top;
	
  childWindow = window.open(url, "childWin", windowFeatures);
}

function addDays(myDate, days)
{
  // myDate = a Date object
  // days = +/- days from date
  
  d = new Date(myDate.getTime() + days * 24 * 60 * 60 * 1000);
  
	// Under FireFox, getYear() returns 106 for 2006 for instance, so add 1900
	var year;
	if ( d.getYear() < 1900 )
	  year = d.getYear() + 1900;
	else
	  year = d.getYear();
		
  return (d.getMonth()+1) + "/" + d.getDate() + "/" + year;
}

function goPage(src)
{
  window.location.href = src;
}

function trim(s)
{
  while (s.substring(0,1) == ' ')
    s = s.substring(1,s.length);

  while (s.substring(s.length-1,s.length) == ' ')
    s = s.substring(0,s.length-1);

  return s;
}

function isValidEmail(str)
{
  var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
  
	if ( !str.match(re) )
    return false;
	else
    return true;
}

function validateZip(field)
{
  var valid = "0123456789-";
  var hyphencount = 0;

  if (field.length!=5 && field.length!=10)
  {
    //alert("Please enter your 5 digit or 5 digit+4 zip code.");
    return false;
  }
  
  for (var i=0; i < field.length; i++)
  {
    temp = "" + field.substring(i, i+1);
    if (temp == "-")
	  hyphencount++;
    
	if (valid.indexOf(temp) == "-1")
	{
      //alert("Invalid characters in your zip code.  Please try again.");
      return false;
    }

    if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-"))
	{
      //alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
      return false;
    }
  }
  return true;
}

function isValidIPAddress(ipaddr)
{
  var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
  
	if ( re.test(ipaddr) )
	{
    var parts = ipaddr.split(".");
    if ( parseInt(parseFloat(parts[0])) == 0 )
		{
			return false;
		}
    
		for (var i = 0; i < parts.length; i++)
		{
      if ( parseInt(parseFloat(parts[i])) > 255 )
			{
				return false;
			}
    }
    
		return true;
  }
	else
	{
    return false;
  }
}

function validatePhone(field)
{
	// Regex pattern for verifying a phone number
	var regex = /[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]/
	
	if ( field.value.search(regex) == -1 )
	  return false;
	else
	  return true;
}

function isInteger(s)
{
  var i
  for (i = 0; i < s.length; i++)
  {   
    // Check that current character is number.
    var c = s.charAt(i)
    if ( (c < "0") || (c > "9") )
      return false
  }
  
  // All characters are numbers.
  return true
}

function isNumeric(sText)
{
  var validChars = "0123456789";
  var isNumber = true;
	var decimalCount = 0;
  var ch;

  for (i = 0; i < sText.length && isNumber == true; i++) 
  { 
    ch = sText.charAt(i);
		
    if ( validChars.indexOf(ch) == -1 )
    {
			// Makre sure only 1 decimal point exists
			if ( ch == "." && decimalCount == 0 )
			  decimalCount++;
			else
        isNumber = false;
    }
  }
  
	return isNumber;
}

function capitalizeWords(obj)
{
  val = obj.value;
  newVal = '';
  val = val.split(' ');
  
  for (var c = 0; c < val.length; c++)
  {
    newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length).toLowerCase() + ' ';
  }
  
  obj.value = trim(newVal);
}

function lowerCase(obj)
{
  obj.value = obj.value.toLowerCase();
}

// Expected 12-hour format: hh:mm
function isTime(field)
{
	if ( field.length != 5 )
	{
		//alert("Not 5 chars long");
	  return false;
	}
	// If ':' not found or there is more than one return false
	if ( field.indexOf(":") == -1 || (field.indexOf(":") != -1 && field.indexOf(":") != field.lastIndexOf(":")) )
	{
		//alert("No colon");
	  return false;
	}
	
	// If there are spaces in field then return false
	if ( field.indexOf(" ") != -1 )
	{
		//alert("Space found");
	  return false;
	}
		
	// Split the time into an array
	timeParts = field.split(":");
	
	if ( !isInteger(timeParts[0]) || !isInteger(timeParts[1]) )
	{
		//alert("Not integer");
	  return false;
	}
	
	// if the first element (hour) is >= 1 and <= 12 AND the 2nd element (minutes) is >= 0 and <= 59
	if ( parseInt(timeParts[0], 10) >= 1 && parseInt(timeParts[0], 10) <= 12 && parseInt(timeParts[1], 10) >= 0 && parseInt(timeParts[1], 10) <= 59 )
    return true;
	else
	{
		//alert("Not within bounds. [0] = " + parseInt(timeParts[0], 10) + " and [1] = " + parseInt(timeParts[1]));
	  return false;
	}
}

function newWindow(url, target, w, h, scroll, resizable)
{
  var win = null;
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
	var menubar = 1;
	var toolbar = 1;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += 'scrollbars=' + scroll + ',';
  settings += 'resizable=' + resizable + ',';
	settings += 'menubar=' + menubar + ',';
	settings += 'toolbar=' + toolbar;

  win = window.open(url, target, settings);

  if ( parseInt(navigator.appVersion) >= 4 )
  {
    win.window.focus();
  }
}

function isChecked(objCheckbox)
{
  var count = 0;
  
	if ( objCheckbox.length == undefined )
	{
		if ( objCheckbox.checked )
      count++;
	}
	else
	{
    for (var i = 0; i < objCheckbox.length; i++)
      if ( objCheckbox[i].checked )
        count++;
	}
	
	if ( count )
	  return true;
	else
	  return false;
}

// Toggle the state of a group of checkboxes
function toggleState(state, objCheckbox)
{
  var i;
	
	// If there's only one checkbox then it's not an array so create one adding this as the first element
  if ( objCheckbox.length == undefined )
	{
	  objCheckbox = new Array(objCheckbox);
	}
	
  for (i = 0; i < objCheckbox.length; i++)
  {
	  // if this checkbox's state doesn't match state that was passed, change
		if ( objCheckbox[i].checked != state ) 
		  objCheckbox[i].click();
  }
}