// Copyright 2005 Google Inc.  All Rights Reserved.

var agt = navigator.userAgent.toLowerCase();
var isIe = (agt.indexOf('msie') != -1);
var isIe5 = (agt.indexOf('msie 5') != -1);
var uniqueCounter = (new Date).getTime();

/**
 * creates a request object for AJAX calls. This is a helper function for
 * SendRequest.
 *
 * @param handler is the callback for the request.
 * @return a xmlhttp, used in SendRequest.
 */
function createXmlHttpReq(handler) {
  var xmlhttp = null;
  if (isIe) {
    var control = (isIe5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
    try {
      xmlhttp = new ActiveXObject(control);
      xmlhttp.onreadystatechange = handler;
    } catch (e) {
    }
  } else {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onload = handler;
    xmlhttp.onerror = handler;
  }
  return xmlhttp;
}

/**
 * helper function for SendRequest
 *
 * @param xmlhttp is xml object used to make ajax request, it must support open
 *                and send.
 * @param url is the url/command to execute against the server.
 */
function xmlHttpGET(xmlhttp, url) {
  xmlhttp.open('GET', url, true);
  xmlhttp.send(null);
}

/**
 * used to build AJAX request. The only page using this function is only used
 * in Friends.aspx, however - it could easily be extended to make calls for
 * other pages as well.
 *
 * @param url is the command to execute against the server.
 */
function sendRequest(url) {
  var xmlhttp = createXmlHttpReq(function() {});
  ++uniqueCounter;
  xmlHttpGET(xmlhttp, url + "&rand=" + uniqueCounter);
}

/**
 * @param itemName is the value to check.
 * @param itemPrefix checks if itemName starts with itemPrefix
 * @return true if the value itemName starts with itemPrefix.
 */
function doesNameStartWith(itemName, itemPrefix) {
  return itemName.indexOf(itemPrefix) == 0;
}

/**
 * opens up a format help box for pages that use the orkut formatting language.
 */
function openTips() {
  var meta = window.open("Format.aspx", "meta", 
    "menubar=no,toolbar=no,status=no,scrollbars=yes,width=400,height=560"); 
  meta.focus();
}

/**
 * updates a counter for a standard text area with a maximum length of
 * opt_maxSize.
 *
 * @param opt_countedTextBox is the id of the text area that has a max length.
 *        Default = "countedTextBox"
 * @param opt_countBody is the id of the div with the text displaying remaining 
 *        character count. Default = "countedBody"
 * @param opt_maxSize is the maximum size allowed for the text area.
 *        Default = 1024
 */
function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {
  var countedTextBox = opt_countedTextBox ?
    opt_countedTextBox : "countedTextBox";
  var countBody = opt_countBody ? opt_countBody : "countBody";
  var maxSize = opt_maxSize ? opt_maxSize : 1024;
    
  var field = document.getElementById(countedTextBox);
  if (field && field.value.length >= maxSize) {
    field.value = field.value.substring(0, maxSize);
  }
  var txtField = document.getElementById(countBody);
  if (txtField) {  
    txtField.innerHTML = field.value.length;
  }
}

/**
 * shows the help div.
 */
function showHelp() {
  var obj = document.getElementById("help");
  obj.style.display = "";
  obj = document.getElementById("show_help");
  obj.style.display = "none";
}

/**
 * hides the help div.
 */
function hideHelp() {
  var obj = document.getElementById("help");
  obj.style.display = "none";
  obj = document.getElementById("show_help");
  obj.style.display = ""; 
}

/**
 * sets the checked property of matching form elements to the value provided by
 * the checked parameter.
 * 
 * @param frm is a reference to the form in which the elements to check exist.
 * @param checkboxesPrefix the prefix of the form elements you wish to check.
 *        for example, "check_" will match every form element that start with
 *        check_.
 * @param checked defines whether to check or uncheck the matching elements.
 */
function checkAll(frm, checkboxesPrefix, checked) {
  for (var i = 0; i < frm.elements.length; i++) {
    if (doesNameStartWith(frm.elements[i].name, checkboxesPrefix)) {
      frm.elements[i].checked = checked;
    }
  }
}

/**
 * removes all items from a select list
 *
 * @param obj is an HTML select list
 */
function removeAllOptions(obj){
  if (obj && obj.options) {
    for (var i = (obj.options.length - 1); i >= 0; i--) {
      obj.options[i] = null;
    }
    obj.selectedIndex = -1;
  }
}

/**
 * adds a new item to the end of a select list
 *
 * @param obj is the HTML select list
 * @param text is the text of the new item
 * @param value is the value of the new item
 * @param selected is whether or not the item should be selected.
 */
function addOption(obj, text, value, selected){
  if (obj && obj.options) {
    obj.options[obj.options.length] = new Option(text, value, false, selected);
  }
}

/**
 * populates an HTML select list from a javascript array.
 *
 * @param selectList is the HTML select list
 * @param values is a javascript array with values.
 * @param selectedItem is the item that should be selected
 */
function populateDropDown(selectList, values, selectedItem) {
  removeAllOptions(selectList);
  for (var i = 0; i < values.length; i++) {
    addOption(selectList, values[i], i, (i == selectedItem));
  }
}
   
/**
 * forces a text area to maintain a certain length
 *
 * @param obj is the HTML text area
 * @param maxLength is the maximum length of the text area
 */ 
function enforceLength(obj, maxLength) {
  if (obj.value.length >= maxLength) {
    obj.value = obj.value.substring(0, maxLength);
  }
}

/**
 * Setup prompt in text box specified
 *
 * @id the id of the text box, it is expected to have prompt attribute assigned
 */ 
function promptSetup(id) {
  var el = document.getElementById(id);
  if (!el || el.type != 'text' || el.value != "") { 
    return;
  }
  var pr = el.getAttribute("prompt");
  if (pr && pr != "") {
    el.value = pr;
    el.style.color = '#888';
    el.is_focused = 0;
    el.onfocus = function () { promptFocus(id); }
    el.onblur = function () { promptBlur(id); }
  }
}

/**
 * Test whether the current value in textbox is the default prompt
 *
 * @objid the id of the text box, it is expected to have prompt attribute assigned
 */ 
function testDefault(objid) {
  var obj = document.getElementById(objid); 
  if (!obj) {
    return false;
  }
  var ph = obj.getAttribute("prompt");
  return obj.value == "" || obj.value == ph;
}

/**
 * Called when a text box loses focus
 *
 * @objid the id of the text box, it is expected to have prompt attribute assigned
 */ 
function promptBlur(objid) {
  var obj = document.getElementById(objid); 
  var ph = obj.getAttribute("prompt");
  if (obj.is_focused && ph && obj.value == "") {
    obj.is_focused = 0;
    obj.value = ph;
    obj.style.color = '#888';
  }
}

/**
 * Called when a text box gains focus
 *
 * @objid the id of the text box, it is expected to have prompt attribute assigned
 */ 
function promptFocus(objid) {
  var obj = document.getElementById(objid); 
  if (!obj.is_focused) {
    obj.value = '';
    obj.is_focused = 1;
    obj.style.color = '#000';
  }
}
