/* Function to addEventListener to onload
 * @param func - a function which should be executed once the page has loaded
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * it will work even if something has previously been assigned to window.onload
 * without using addLoadEvent itself. 
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/*
 * Function to clear a text field
 * @param thefield
 *     the field to clear
 */
function cleartext(thefield) {
	if (thefield.defaultValue == thefield.value) {
		thefield.value = "";
	}
}

function GetAnchors() {
  if (document.getElementById) {
    var elements = new Array('a', 'area');
    for (var j=0; j < elements.length; j++) {
      var x = document.getElementsByTagName(elements[j]);
      for (var i=0;i<x.length;i++) {
        if (x[i].className.indexOf('newWindow') != -1) {
          x[i].onkeypress = openNewWindow;
          x[i].onclick = openNewWindow;
          x[i].setAttribute("title", "New Window");
        } else if (x[i].className.indexOf('popup') != -1) {
          x[i].onkeypress = openNewWindow;
          x[i].onclick = openNewWindow;
          x[i].setAttribute("title", "Pop-up Window");
        } else if (x[i].className.indexOf('download') != -1) {
          x[i].onkeypress = openNewWindow;
          x[i].onclick = openNewWindow;
          x[i].setAttribute("title", "Download");
        } else if (x[i].className.indexOf('downReportRR') != -1) {
          x[i].setAttribute("title", "Download Report");
        } else if (x[i].className.indexOf('html') != -1) {
          x[i].onkeypress = openNewWindow;
          x[i].onclick = openNewWindow;
          x[i].setAttribute("title", "Download");
        } else if (x[i].className.indexOf('podcast') != -1) {
          x[i].setAttribute("title", "Podcast");
        } else if (x[i].className.indexOf('rss') != -1) {
          x[i].setAttribute("title", "RSS Feed");
        } else if (x[i].className.indexOf('external') != -1) {
          x[i].setAttribute("title", "Link to External Site");
        } else if (x[i].className.indexOf('reqPrintRR') != -1) {
          x[i].setAttribute("title", "Request Printed Copy");
        } else if (x[i].className.indexOf('reqPrint') != -1) {
          x[i].setAttribute("title", "Request Printed Copy");
        } else if (x[i].className.indexOf('wmv') != -1) {
					//for Ivestor section only
          x[i].setAttribute("title", "Listen to the webcast in Windows Media Player");
        } else if (x[i].className.indexOf('realAudio') != -1) {
					//for Ivestor section only
          x[i].setAttribute("title", "Listen to the webcast in Real Audio");
        } else if (isAssetDoc(x[i])) { // make sure all file extensions you wish to set custom title for are in isAssetDoc() below
          var fileExt = getFileExt(x[i]);
          x[i].onkeypress = openNewWindow;
          x[i].onclick = openNewWindow;
          x[i].className=fileExt
         switch(fileExt) {
          case "doc":
            x[i].setAttribute("title", "Word document");
            break 
          case "ppt":
            x[i].setAttribute("title", "PowerPoint File");
            break            
           case "xls":
            x[i].setAttribute("title", "Excel File");
            break 
           default:
            x[i].setAttribute("title", fileExt.toUpperCase()+" file");
          }
        }
      }
    }
  }
}

function openNewWindow(e) {
    if (e == undefined || e.keyCode != 9) {
        if (isAssetDoc(this)) {
            var url = this.href;
            var parentHost = window.location.protocol + "//" + window.location.host;
            if (url.substring(0, parentHost.length) == parentHost) {
                url = url.substr(parentHost.length);
            } else if (url.substring(0, 23) == "http://www.chevron.com/") {
                url = url.substr(22);
            }
        }
        var features = '';
        var start;
        var end;
        var width;
        var height;
        var thisurl = this.href ? this.href : "";
        width = parseInt(getValueFromClass(this, 'w')) > 0 ? getValueFromClass(this, 'w') : '';
        height = parseInt(getValueFromClass(this, 'h')) > 0 ? getValueFromClass(this, 'h') : '';
        if (height.length > 0 || width.length > 0) {
            features += height.length > 0 ? 'height=' + height + ',' : '';
            features += width.length > 0 ? 'width=' + width + ',' : '';
        }
        features += getFeatures(this);

        if (features.length > 0) {
            if (features.substr(features.length - 1, 1) == ",")
                features = features.substr(0, features.length - 1);
            window.open(this.href, '_new', features);
        } else {
            window.open(this.href);
        }
        return false;
    }
    return true;
}

function getValueFromClass(obj, attrib) {
  if (obj.className.indexOf(" "+attrib) != -1) {
    start = obj.className.indexOf(" "+attrib) + 1
    end = obj.className.indexOf(" ", start);
    end = (end == -1) ? obj.className.length - start : end - start;
    var aLength = attrib.length;
    return obj.className.substr(start+aLength, end-aLength);
  } else {
    return "";
  }
}

function isAssetDoc(obj) {
  var types = new Array("pdf", "doc", "xls", "ppt", "cvxn");
  var fileExt = getFileExt(obj);
  
  for(i=0; i<types.length;i++) {
    if (types[i] == fileExt) {
      return true;
    }
  }
  return false;
}

function getFileExt(obj) {
    var url = obj.href;
    var ext = url.substr(url.lastIndexOf(".") + 1);

    if (ext == "cvxn") {
        ext = url.substr(url.lastIndexOf(".") - 3, 3);
    }

    return ext;
}

function getFeatures(obj) {
  var features = "";
  if (obj.className.indexOf(" scroll") != -1) {
    features += "scrollbars=yes,";
  }
  return features;
}
addLoadEvent(GetAnchors);
