function windowThere(theURL,winName,features) {
	if (top.opener && !top.opener.closed) {
		top.opener.location.href = theURL;
	} else {
	if (window.winName) {
			//change href of {winName}
			winName.location.href = theURL;
		} else {
			PopWin(theURL,winName,features);
		}
	}
}
/* heres the href to use...
<a href="javascript:windowThere('/over60s/','popWindow','titlebar=1,status=1,toolbar=1,location=1,scrollbars=1,resizable=yes,menubar=yes');">test</a>
*/

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
// submit the form to the specified page
function goToPage( pageUrl ) {
  // the form object that will be used
  var form_object = document.postform;

  validation_success = validate_form( form_object );
  if ( validation_success ) {
    form_object.action = pageUrl;

	if (form_object.elements['form_id']) {
		form_object.elements['form_id'].value = '';
		form_object.elements['form_id'].name = 'not_form_id';
	}
	
	form_object.submit();
  } else {
    window.alert( 'Sorry, you did not complete the form correctly' );
  }
}

function PopWin( theURL, winName, features ) {
  window.open( theURL, winName, features );
}

function PopW(theURL,winName,features) { 
	window.close();
	window.open(theURL,winName,features); 
}

// required_elements contains the names of elements that have to be
// filled in before the form can be submitted 
required_elements = new Array();

// check_format_elements contains the names and formats of elements
// that have to be filled in with the correct format before
// the form can be submitted 
check_format_elements = new Array();

// add an element to the required_elements array
function add_required_element( element_name ) {
  required_elements[ required_elements.length ] = element_name; 
}

// add an element to the check_format_elements array
function add_check_format_element( element_name, element_format ) {
  element_to_format = new Array( element_name, element_format )
  check_format_elements[ check_format_elements.length ] = element_to_format;
}

/**
 * returns true if all the required elements are filled in and all
 * the formated elements have the right format. otherwise returns false.
 */
function validate_form() {
  var form_is_valid = true;

  for ( i = 0; i < required_elements.length; i++ ) {
    elements = document.getElementsByName( required_elements[ i ] );
    // there may be more than one element with the same name,
    // eg. radiobuttons
    // NOTE: not sure what the effect of a hidden variable with
    // the same name as a required/formatted element will have
    for ( j = 0; j < elements.length; j++ ) {
      //window.alert( elements[ j ].name + " - " + elements[ j ].type );
      var valid = false;
      valid = validate_required_element( elements[ j ].name, elements[ j ].type );
      if ( !valid ) {
        form_is_valid = false;
      }
      
    }
  }

  for ( i = 0; i < check_format_elements.length; i++ ) {
    var element_name;
    element_name = check_format_elements[ i ][ 0 ];
    var element_format;
    element_format = check_format_elements[ i ][ 1 ];

    elements = document.getElementsByName( element_name );
    for ( j = 0; j < elements.length; j++ ) {
      //window.alert( elements[ j ].name + " - " + elements[ j ].type );
      valid = validate_formatted_element( elements[ j ].name, elements[ j ].type, element_format );
      if ( !valid ) {
        form_is_valid = false;
      }
    }
  }

  // TODO: remove
  //form_is_valid = false;

  return form_is_valid;
}


/**
 * returns true if the required element is filled in, false otherwise
 */
function validate_required_element() {
  // TODO: implement
  return true;
}

/**
 * returns true if the formatted element is filled in with the correct
 * format, false otherwise.
 * note: it will return true if the element
 * is not filled in at all. this is so that optional elements can be
 * tested for formatting.
 */
function validate_formatted_element() {
  // TODO: implement
  return true;
}

function saveResult() {
  goToPage( 'save-to-myplan.php' );
}
