/*
 (c) V. Subhash, 2009.
 www.vsubhash.com
 
 Created:       14 June 2009
 Updated:       18 July 2009 
 Description:   Common Javascript functions
 License:       GNU General Public License Version 3 - http://www.gnu.org/licenses/gpl-3.0.txt

*/


/*
 * To pass XHTML Strict validation
 */
window.onload = AddNewWindowTargetToRelExternalLinks;


/*
 * Identifies links with rel="external" attribute. 
 * Adds target="_blank" attribute.
 */
function AddNewWindowTargetToRelExternalLinks() {
  var iKey, oAnchors, oAnchor;
  
  oAnchors = document.getElementsByTagName("a");
  
  for (iKey in oAnchors) {
    oAnchor = oAnchors[iKey];
    
    try {                                 // getAttribute does not work on links
      if (oAnchor.getAttribute("href") && // with no attributes other than href
         (oAnchor.getAttribute("rel") == "external")) {
        oAnchor.setAttribute("target", "_blank");
      }
    } catch(e) { /* Ignore named anchors */ };
    
  }   
}



function GetMaxDaysInAMonth(iYear, iMonth) {
 var iMaxDays;

 if ((iMonth == 1) || (iMonth == 3) || (iMonth == 5) || 
     (iMonth == 7) || (iMonth == 8) || (iMonth == 10) || (iMonth == 12)) {
   iMaxDays = 31;
 } else {
   iMaxDays = 30;  
 }

 if (iMonth == 2)  {
   iMaxDays = 28;  
   if (((iYear % 4)==0) && ((iYear % 100)!=0) || ((iYear % 400)==0)) {
     iMaxDays = 29;
   } 
 } 
 
 return(iMaxDays); 
}




function findPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
      while(1) 
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;
  return curtop;
}

 
function AjaxCallBackRenderer(oElement, oConn) {
  if (oConn.readyState == 4) {
   oElement.innerHTML = oConn.responseText;   
  }
}



function AjaxRenderer(oElement, sUrl) {
 var oResponse = null;

 var oConn = new XMLHttpRequest();

 try {
   oConn.open("GET", sUrl, true);
   oConn.onreadystatechange
      = function() {
          AjaxCallBackRenderer(oElement, oConn);
        };
   oConn.send(null);
 } catch(e) {
   return(null);
 }

}



function SetCookie(name, value, expiry) {
  var dt = new Date();
  dt.setYear(dt.getFullYear()+1);
  document.cookie = name + "=" + escape(value) +
                    "; expires=" + ((expiry)?expiry:dt.toGMTString());
}

function GetCookie(name) {
  var cookie = document.cookie;
  var offset = cookie.indexOf(name);
  if (offset ==-1) return null;
  offset =  offset + name.length + 1;
  var end = cookie.indexOf(";",offset);
  if (end ==-1) end =cookie.length;
  return cookie.substr(offset,end-offset);
}

function HighLight(q)  {          //  Hi!
  if (q =="" || q == undefined) { //  The Highlight() function accepts a string of words,
    return;                       //  which are then searched on the current HTML document.
  };                              //  Instances of the words are then found and highlighted.
                                  //  If you are planning to use the HighLight() code, feel
  var pn = /\s+/gim;              //  free to do so. If you make this code smaller or better,
  var rs =q.replace(pn, " ");     //  please mail me a copy. Whatever you do, just provide a
  pn = /(\s*)(\w+)/;              //  link back to http://www.vsubhash.com/. Or, you could simply
  rs = rs.replace(pn, "$2");      //  include this comment in the source code.
                                  //  Thank you.
  var ss = rs.split(" ");         //  V. Subhash (http://www.vsubhash.com/)
  var colarray = new Array("lime", "aqua", "deepskyblue", "fuchsia", "aquamarine", "plum", "hotpink", "gold", "lime", "aqua", "deepskyblue", "fuchsia", "aquamarine", "plum", "hotpink", "gold");
  for (i =0; i < ss.length && i < 17; i++) {
    if (ss[i].length !=0) {
      var rng = document.body.createTextRange();
      while (rng.findText(ss[i])) {
        rng.execCommand("BackColor", false, colarray[i]);
        rng.collapse(false);
      }
    }
  }
}

function ClientSniff() {
  var ug;
  var z;
  var dt = new Date();
  var t = dt.getTime();
  t =-dt.getTimezoneOffset()/60;

  ug = navigator.userAgent.toLowerCase();

  z = "B: " + GetBrowser(ug);

  z = z + " O: " + GetOS(ug);

  w =parseFloat(navigator.appVersion);
  if (w > 2.0) {
    z = z +" S: " + screen.width + "x" + screen.height;
    if (navigator.appName != "Netscape") {
      z = z + "@" + screen.colorDepth + "bit";
    } else {
      z = z + "@" + screen.pixelDepth + "bit";
    }
  }

  if (t >=0) {
   z = z + " Z: \+" + t + "hrs";
  } else {
   z = z + " Z: " + t + "hrs";
  }
  z = z + " T: " + dt.getHours() + ":" + dt.getMinutes();
  z = z + " H: " + history.length;
  return z;
}
