nsHTMLSelectElement::GetSelectedIndex(PRInt32* aValue)
nsHTMLOptionCollection::GetSelectedIndex(PRInt32 *aSelectedIndex)
nsHTMLSelectElement::GetSelectedIndex(PRInt32* aValue){  nsIFormControlFrame* formControlFrame = nsnull;  nsresult rv = GetPrimaryFrame(this, formControlFrame, PR_FALSE);  if (NS_SUCCEEDED(rv)) {    nsAutoString value;    rv = formControlFrame->GetProperty(nsHTMLAtoms::selectedindex, value);    if (NS_SUCCEEDED(rv) && (value.Length() > 0)) {      PRInt32 retval = 0;      PRInt32 error = 0;      retval = value.ToInteger(&error, 10); // Convert to integer, base 10      if (!error) {        *aValue = retval;      } else {        rv = NS_ERROR_UNEXPECTED;      }    }  } else { // The frame hasn't been created yet.  Use the options array    *aValue = -1;    // Call the helper method to get the PresState and     // the SupportsArray that contains the value    //    nsCOMPtr<nsIPresState> presState;    nsCOMPtr<nsISupportsArray> value;    nsresult res = GetPresState(getter_AddRefs(presState),                                getter_AddRefs(value));    if (NS_SUCCEEDED(res) && presState) {      nsCOMPtr<nsISupports> supp;      presState->GetStatePropertyAsSupports(NS_LITERAL_STRING("selecteditems"),                                            getter_AddRefs(supp));      res = NS_ERROR_NULL_POINTER;      if (supp) {        nsCOMPtr<nsISupportsArray> svalue = do_QueryInterface(supp);        if (svalue) {          PRUint32 count = 0;          svalue->Count(&count);          nsCOMPtr<nsISupportsPRInt32> thisVal;          for (PRUint32 i=0; i<count; i++) {            nsCOMPtr<nsISupports> suppval = getter_AddRefs(svalue->ElementAt(i));            thisVal = do_QueryInterface(suppval);            if (thisVal) {              res = thisVal->GetData(aValue);              if (NS_SUCCEEDED(res)) {                // if we find the value the return NS_OK                return NS_OK;              }            } else {              res = NS_ERROR_UNEXPECTED;            }            if (NS_FAILED(res))              break;          }        }      }      // At this point we have no frame      // and the PresState didn't have the value we were looking for      // so now go get the default value.      if (NS_FAILED(res)) {        // If we are a combo box, our default selectedIndex is 0, not -1;        // XXX The logic here is duplicated in        // nsCSSFrameConstructor::ConstructSelectFrame and        // nsSelectControlFrame::GetDesiredSize        PRBool isMultiple;        rv = GetMultiple(&isMultiple);         // Must not be multiple        if (NS_SUCCEEDED(rv) && !isMultiple) {          PRInt32 size = 1;          rv = GetSize(&size);                 // Size 1 or not set          if (NS_SUCCEEDED(rv) &&              ((1 >= size) || (NS_CONTENT_ATTR_NOT_THERE == size))) {            *aValue = 0;          }        }        nsCOMPtr<nsIDOMHTMLCollection> options;        rv = GetOptions(getter_AddRefs(options));        if (NS_SUCCEEDED(rv) && options) {          PRUint32 numOptions;          rv = options->GetLength(&numOptions);          if (NS_SUCCEEDED(rv)) {            for (PRUint32 i = 0; i < numOptions; i++) {              nsCOMPtr<nsIDOMNode> node;              rv = options->Item(i, getter_AddRefs(node));              if (node) {                nsCOMPtr<nsIDOMHTMLOptionElement> option(do_QueryInterface(node));                if (option) {                  PRBool selected;                  // DefaultSelected == HTML Selected	                rv = option->GetDefaultSelected(&selected);                  if (NS_SUCCEEDED(rv) && selected) {                    *aValue = i;                    break;                  }                }              }            }          }        }      }    }  }  return NS_OK;}