function MM_reloadPage(init) 
{  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

MM_reloadPage(true);

function launch(url, options) 
{ 
   self.name = "opener";
   remote = open(url, "remote", options);
}

function changeBGC(targetID,bgcolor){
	if (document.getElementById) {
		document.getElementById(targetID).style.backgroundColor = bgcolor;
	}
	
	else if(document.all){
		document.all(targetID).style.backgroundColor = bgcolor;
	}
	else if(document.layers){
		document.layers[targetID].bgColor=bgcolor;
	}
}

String.prototype.trim = function()
{
  var x=this;

  x=x.replace( /^\s*/, "" );
  x=x.replace( /\s*$/, "" );

  return x;
}

function alphanumericString( checkStr )
{
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var allValid = true;
	
	for ( var i = 0; i < checkStr.length; i++ )
	{
		ch = checkStr.charAt(i);
		for ( var j = 0;  j < checkOK.length;  j++ )
			if ( ch == checkOK.charAt(j) )
				break;
				
		if ( j == checkOK.length )
		{
			allValid = false;
			break;
		}
	}
	return allValid;
}


function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} 

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
   return "";
}

/** Same as validate but for marriages only (no gender check). */
function validateMarriage( theForm )
{
	// combine date fields
	for( i = 0; i < theForm.elements.length; i++ )
	{
		if ( theForm.elements[i].name.indexOf( "_date_1" ) > 0 )
		{
			var month = theForm.elements[i];
			var day = theForm.elements[i + 1];
			var year = theForm.elements[i + 2];
			
			theForm.elements[i + 3].value = year.value + "-" + month.value + "-" + day.value;
			
			var test = theForm.elements[i + 3];
			
			if ( test.value == "-0-0" || test.value == "0-0-0" )
				test.value = "";
			
			month.value = "";
			day.value = "";
			year.value = "";
			
			i = i + 3;
		}
	}
	document.theForm.submitForm.value = "1";
	document.theForm.submit();
	return true;
}

function validateSpouseSelection( myForm )
{
	// myForm.ID = spouse selection from drop-list
	if ( myForm.ID.selectedIndex == 0 ) {
		alert( "Please choose an individual from the list." );
	}
	else {
		myForm.submitForm.value=1;
		myForm.submit();
	}
}

function validateLogin( theForm )
{
	var newUsername = theForm.newUsername.value.trim();
	var newPassword = theForm.newPassword.value.trim();
	var readOnlyPassword = theForm.readOnlyPassword.value.trim();
	var adminPassword = theForm.adminPassword.value.trim();
	var email = theForm.email.value.trim();
	
	// modify the form contents to reflect the trim
	theForm.newUsername.value = newUsername;
	theForm.newPassword.value = newPassword;
	theForm.readOnlyPassword.value = readOnlyPassword;
	theForm.adminPassword.value = adminPassword;
	theForm.email.value = email;

	// check to make sure login passwords contain no special characters, etc.
	if ( newUsername == "" )
	{
		alert( "Please enter a user name." );
		return;
	}	
	if ( newPassword == "" )
	{
		alert( "Please enter a general access password." );
		return;
	}
	if ( readOnlyPassword == "" )
	{
		alert( "Please enter a read-only password." );
		return;
	}
	if ( adminPassword == "" )
	{
		alert( "Please enter an administrative password." );
		return;
	}
	if ( email == "" )
	{
		alert( "Please enter an administrative e-mail address." );
		return;
	}
	// check for non-alphanumeric characters
	if ( !alphanumericString( newUsername) )
	{
		alert( "The user ID (tree name) may contain only alphanumeric characters" );
		return;
	}	
	if ( !alphanumericString( newPassword ) )
	{
		alert( "The general access password may contain only alphanumeric characters" );
		return;
	}
	if ( !alphanumericString( readOnlyPassword ) )
	{
		alert( "The read-only password may contain only alphanumeric characters" );
		return;
	}
	if ( !alphanumericString( adminPassword ) )
	{
		alert( "The administrative password may contain only alphanumeric characters" );
		return;
	}
	
	// check to make sure all passwords are unique
	//if ( $newPassword == $readOnlyPassword || $adminPassword == $readOnlyPassword || $newPassword == $adminPassword )
	if ( newPassword == readOnlyPassword || adminPassword == readOnlyPassword || newPassword == adminPassword )
	{
		alert( "All passwords must be unique." );
		return;
	}
	
	theForm.newTree.value = 1;
	theForm.submit();
}

function validate( theForm )
{
	// combine date fields
	for( i = 0; i < theForm.elements.length; i++ )
	{
		if ( theForm.elements[i].name.indexOf( "_date_1" ) > 0 )
		{
			var month = theForm.elements[i];
			var day = theForm.elements[i + 1];
			var year = theForm.elements[i + 2];
			
			theForm.elements[i + 3].value = year.value + "-" + month.value + "-" + day.value;
			
			var test = theForm.elements[i + 3];
			
			// skip empty or invalid dates
			if ( test.value == "-0-0" || test.value == "0-0-0" || test.value == "--" )
				test.value = "";
			
			month.value = "";
			day.value = "";
			year.value = "";
			
			i = i + 3;
		}
	}
	
	var gender = getSelectedRadioValue( theForm.sex );
	
	// if gender is a hidden field, get the value
	
	if ( !gender || gender == "undefined" )	
		gender = theForm.sex.value;
	
	if ( theForm.name.value.trim() == "" ) {
		alert( "You must enter a valid name (first & middle)." );
		return false;
	}
	else if ( theForm.surn.value.trim() == "" ) {
		alert( "You must enter a valid surname or family name." );
		return false;
	}
	else if ( gender == "" || !gender ) {
		alert( "You must specify a gender." );
		return false;
	}
	else {
		document.theForm.submitForm.value = "1";
		document.theForm.submit();
		return true;
	}
}

