/* Copyright Kayak.com 2007
 * Kayak Developer Network
 * common dhtml-ish operators.
 */

//
function getObj(objId) {
  var obj = null;
  if (document.getElementById) {
    obj = document.getElementById(objId);
  } else if (document.layers) {
    obj = document.objId;
  } else {
    obj = document.all.objId;
  }
  return obj;
}

//
function setDivText(divid, txt) {
  var obj = getObj(divid);
  obj.innerHTML = txt;
}


//
// Convenience to quickly set same class on set of objects.
//
function setClassListById(lst, cName) {
  for (var i = 0; i < lst.length; i++ ) {
    setClassById(lst[i], cName);
  }
}

//
function setClassById(itm, cName) {
  var p = getObj(itm);
  if (!p) {
    alert("Unable to get " + itm) + " within setClassById";
    return;
  }
  p.className = cName;
}
/*
*/
function hideDivById(divId) {
  var id1 = getObjStyle(divId);
  if(null == id1) {
    alert("unable to get div " + divId);
  }
  id1.visibility='hidden';
  id1.display='none';
}

//
// wrapper for hideDivById. works on entire list of id.
//
function hideDivListById(lst) {
  if (!lst || (0 == lst.length)){
    return;
  }
  for (var i = 0; i < lst.length; i++) {
    hideDivById(lst[i]);
  }
  return;
}

//
function unhideDivListById(lst) {
  for (var i = 0; i < lst.length; i++) {
    unhideDivById(lst[i]);
  }
}

/*
  Locate an object by its id and return handle to its
  style.
*/
function getObjStyle(divId) {
  var obj = null;

  if (document.getElementById)    // DOM3 = IE5, NS6
  {
    if (null == document.getElementById(divId)) {
      return null;
    }
    obj = document.getElementById(divId).style;
  }
  else if (document.layers) // Netscape 4
  {
    obj = document.divId;
  }
  else // IE 4
  {
    if (null == document.all.divId) {
      return null;
    }
    obj = document.all.divId.style;
  }
  return obj;
}


/*
*/
function unhideDivById(divId) {
  var id1 = getObjStyle(divId);
  if(null == id1) {
    alert("unable to get div " + divId);
  }
  id1.visibility='visible';
  id1.display='block';
}

/* Given the id of a <LINK> element holding style sheet, as well
 * as url of css, apply it.
 */
function applyStyleSheet(linkIdName, url) {
  var o = getObj(linkIdName);
  if (! o) {
    return false;      
  }
  o.href = url;
  return true;
}

////////////////////////////////////////////////////////
// pad string s to length of l by inserting character p 
// at left.
////////////////////////////////////////////////////////
function padLeftStr(l,p,s) {
  while (s.length < l) {
    s = p + s;
  }
  return s;
}
