/*
 (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 call
 * window.onload = add_NewWindowTargetToRelExternalLinks;
 *
 * Identifies links with rel="external" attribute. 
 * Adds target="_blank" attribute.
 */
function add_NewWindowTargetToRelExternalLinks() {
  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 get_MaxDaysInAMonth(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 get_RoundNumber(aNumber, aDecPlaces) {
  var iRoundValue = Math.round(aNumber * Math.pow(10, aDecPlaces))/Math.pow(10, aDecPlaces);
  return iRoundValue;
}


function find_PosX(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 find_PosY(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 do_RenderOnAjaxCallBack(oElement, oConn, oCustomCallBack) {
  if (oConn.readyState == 4) {
   oElement.innerHTML = oConn.responseText;   
   if (oCustomCallBack != undefined) {
     oCustomCallBack();
   }
  }
}



function do_RenderElementWithAjax(oElement, sUrl, oCustomCallBack) {
 var oResponse = null;

 var oConn = new XMLHttpRequest();

 try {
   oConn.open("GET", sUrl, true);
   oConn.onreadystatechange
      = function() {
	     do_RenderOnAjaxCallBack(oElement, oConn, oCustomCallBack);
        };
   oConn.send(null);
 } catch(e) {
   return(null);
 }

}


function set_Cookie(name, value, expiry) {
  var dt = new Date();
  dt.setYear(dt.getFullYear()+1);
  document.cookie = name + "=" + escape(value) +
                    "; expires=" + ((expiry)?expiry:dt.toGMTString());
}

function get_Cookie(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 do_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 set_FileSizeOnElement(sURL, oElement) {
  var oConn = new XMLHttpRequest();
  try {
    oConn.open("HEAD", sURL, true);
    oConn.onreadystatechange
       = function() {
	       var iFileSize;
	       iFileSize = oConn.getResponseHeader("Content-Length");
	       if (iFileSize > 0) {
	         oElement.innerHTML = get_RoundNumber(iFileSize/(1024*1024),2);
	       }	       
         };
    oConn.send(null);
  } catch (e) {
    return(null);
  }
}



