
   var isIE = (navigator.appVersion.indexOf("MSIE") != -1)? true : false;

   var ANX = (isIE)? "A" : "a";
   var ULX = (isIE)? "UL" : "ul";
   var LIX = (isIE)? "LI" : "li";
   var DIVX = (isIE)? "DIV" : "div";

/*
 * onLoad functions
 * Initializes our functions on page load
 */
   $(document).ready(function () {
       $("#findUsMarketers, #canadaMarketers").toggle();
       //$("#canadaMarketers").toggle();
       setFindMarketers();
   });

/* begin - for top navigation drop down */

var t ; 
var g_SELECT = "";   // string containing select boxes on a form


  function checkCharLeft(inputTextArea,maxChar,divName) {    
    var v = document.getElementById(inputTextArea).value;
    var t = v.length;
    var x = parseInt(maxChar  -  t);  
     if (x < 0) { 
        x = 0;
       inputTextArea.value = v.substring(0,maxChar);
     }
      document.getElementById(divName).innerHTML = "<strong>" + x + "</strong>";
}

function changeCase(obj,uol) {
  if(uol==1)
    return obj.toLowerCase();
  else
   return obj.toUpperCase();
}

		/*
		  var	symbolOut="[+]";
		  var symbolIn="[-]";
		*/
		
		var symbolOut = "<img src='/images/icons/+.gif' alt='expand icon' />"
		var symbolIn =  "<img src='/images/icons/-.gif' alt='collapse icon' />"
		
		//this switches expand collapse icons
		function filter(symbol,objectsrc){
			document.getElementById(symbol).innerHTML = eval(objectsrc);	
		}
		
		//show OR hide funtion depends on if element is shown or hidden
		function showItem(id) { 
			if (document.getElementById) { // DOM3 = IE5, NS6
				if (document.getElementById(id).style.display == "none"){
					document.getElementById(id).style.display = 'block';
					filter(("symbol"+id),'symbolIn');			
				} else {
					filter(("symbol"+id),'symbolOut');
					document.getElementById(id).style.display = 'none';			
				}	
			} 
		}


function cleanUrl(curURL) {
  var cl_URL = curURL.replace(/\/index.*/,'/');
	cl_URL = cl_URL.replace(/\/default.*/,'/');
	cl_URL = cl_URL.replace(/#.*/,'');
	cl_URL = cl_URL.replace(/\?.*/,'');
  return cl_URL.toLowerCase();
}

function getQueryStr(name)
{  
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
    var regexS = "[\\?&]"+name+"=([^&#]*)";  
    var regex = new RegExp( regexS );  
    var results = regex.exec( document.URL );  
    if( results == null )    
    return null;  
    else    
    return results[1];
}

var docUrl = cleanUrl(document.URL);
var parentPage = getQueryStr('parent');

//onLoad URL
//alert("docURL = " + docUrl);
//alert("parentPage = " + parentPage);


function getCSS_LeftNav_Var(idx) {
  var tmp, tp2
  var foundit = false;
  var tp = document.getElementById(idx).getElementsByTagName(LIX)

  for (var k = 0; k < tp.length; k++) {
      if (tp.item(k).getElementsByTagName(ANX).item(0)) {
        tp2 = tp.item(k).getElementsByTagName(ANX).item(0).href;
        tp2 = tp2.toLowerCase();
          if (
               (docUrl.lastIndexOf(tp2.toLowerCase()) > -1)
               ||
               (tp2.indexOf(parentPage) >= 0)
             )
               {
               tp.item(k).className =  tp.item(k).className + " selected"
               foundit = true;
               break;
            }
        } // if tp.item(k) exists
      
    }  //for loop   
}


function  getCSS_TopNav_Var(idx) {
  // for topnav - search for folder level - called onLoad

  var tmp, tp2
  var foundit = false;
  var tp = document.getElementById(idx).getElementsByTagName(LIX)
  var xpos = -1
  var tDocUrl = ""
  var tTp2 = ""
  var tpx1, tpx2
  for (var k = 0; k < tp.length; k++) {
      tempX = tp.item(k).className
      if (tempX.length > 0) {        
          tp2 = tp.item(k).getElementsByTagName(ANX).item(0).href
          tp2 = tp2.toLowerCase();
          tTp2 = tp2.replace("http://", "")
          tDocUrl = docUrl.replace("http://", "")
          if (tTp2.lastIndexOf("/") > -1) { tpx1 = tTp2.split("/"); }
          if (tDocUrl.lastIndexOf("/") > -1) { tpx2 = tDocUrl.split("/"); }
          tpx1[(tpx1.length - 1)] = ""; tpx2[(tpx2.length - 1)] = ""
          tpx1 = tpx1.join("/"); tpx2 = tpx2.join("/");
                    
          xpos = tpx2.lastIndexOf(tpx1.toLowerCase()) 
          // k=0 is assumed to be HOME
          if (xpos > -1 && k > 0) {
              tp.item(k).className = tp.item(k).className + " tnonstate"
              foundit = true;
              break;
          }
      }
   }
}


/* 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() {
  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('newWin') != -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('word') != -1) {
			  x[i].onkeypress = openNewWindow;
			  x[i].onclick = openNewWindow;
			  x[i].setAttribute("title", "Word Document");
		} else if (x[i].className.indexOf('ppt') != -1) {
			  x[i].onkeypress = openNewWindow;
			  x[i].onclick = openNewWindow;
			  x[i].setAttribute("title", "PowerPoint Document");
		  } else if (x[i].className.indexOf('pdf') != -1) {
			  x[i].onkeypress = openNewWindow;
			  x[i].onclick = openNewWindow;
			  x[i].setAttribute("title", "Adobe PDF Document");
		  } else if (x[i].className.indexOf('printPage') != -1) {
			  x[i].onkeypress = printPage;
			  x[i].onclick = printPage;
			  x[i].setAttribute("title", "Print Page");
		  } else if (x[i].className.indexOf('external') != -1) {
		    x[i].onkeypress = openNewWindow;
			  x[i].onclick = openNewWindow;
			  x[i].setAttribute("title", "External link");
		  } else if (x[i].className.indexOf('internal') != -1) {
			  x[i].setAttribute("title", "Internal link");
		  } else if (x[i].className.indexOf('iframe') != -1) {
         x[i].onkeypress = openNewIframe;
 			   x[i].onclick = openNewIframe;
			   x[i].setAttribute("title", "Fly-Over Window");     
      }
	  }  
  }
}



function openNewWindow(e) {
  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;
}

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");
  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;
  return url.substr(url.lastIndexOf(".")+1);
}


function getFeatures(obj) {
  var features = "";
  if (obj.className.indexOf(" scrollNo") != -1) { features += "scrollbars=no,"; } else { features += "scrollbars=yes,"; }
  if (obj.className.indexOf(" locationNo") != -1) { features += "location=no,"; } else { features += "location=yes,"; }
  if (obj.className.indexOf(" historyNo") != -1) { features += "history=no,"; } else { features += "history=yes,"; }
  if (obj.className.indexOf(" toolbarNo") != -1) { features += "toolbar=no,"; } else { features += "toolbar=yes,"; }
  if (obj.className.indexOf(" menuNo") != -1) { features += "menubar=no,"; } else { features += "menubar=yes,"; }
  if (obj.className.indexOf(" resizeNo") != -1) { features += "resizable=no"; } else { features += "resizable=yes"; }
  return features;
}

function setFindMarketers() {
    $("#showCanadaMarketers, #showUsMarketers").click(function () {
        $("#findCanadaMarketers, #findUsMarketers, #usMarketers, #canadaMarketers").toggle();
        /*$("#findUsMarketers").toggle();
        $("#usMarketers").toggle();
        $("#canadaMarketers").toggle();
        */
    });
}

addLoadEvent(GetAnchors);
