
//http://www.biasecurities.com/blogs/jim/archive/2005/04/28/1794.aspx
function AttachEvent(elementObj, eventName, eventHandlerFunctionName)
{
  if (elementObj.addEventListener) 
  { // Non-IE browsers
    elementObj.addEventListener(eventName, eventHandlerFunctionName, false);		
  } 
  else if (elementObj.attachEvent) 
  { // IE 6+
    elementObj.attachEvent('on' + eventName, eventHandlerFunctionName);
  } 
  else 
  { // Older browsers 
    var currentEventHandler = elementObj['on' + eventName];
    if (currentEventHandler == null) 
    {
      elementObj['on' + eventName] = eventHandlerFunctionName;
    } 
    else 
    {
      elementObj['on' + eventName] = function(e) { currentEventHandler(e); eventHandlerFunctionName(e); }
    }
  }
}

 /* UTILITY FUNCTIONS */
  
function getObj(eventArgs){
 	var obj;
	
	if (eventArgs.target) {
		obj = eventArgs.target;
	} else if (eventArgs.srcElement) {
		obj = eventArgs.srcElement;
	}	
	/* For most browsers, obj would now be the object we're after; Safari however
		returns a text node so we need to check the node type to make sure */
	if (obj.nodeType == 3) {
	    obj = obj.parentNode;
	}
  
 	return obj;
}

function getObjID(eventArgs){
  var obj = getObj(eventArgs)
  return obj.id;
}
  
function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );

    while ( idx > -1 ) {
        str = str.replace( from, to );
        idx = str.indexOf( from );
    }
    return str;
}

function getElemRefs(id) {
    var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? getLyrRef(id,document): null;
    if (el) el.css = (el.style)? el.style: el;
    return el;
}


function swapMainImage()
/* This function changes the masthead's image everytime the user refreshes/reloads the main page. */
{
   var mainImage= new Array("MH_alex.jpg", "MH_brian.jpg", "MH_kirk.jpg", "MH_don.jpg", "MH_doug.jpg", "MH_ed.jpg");
   var mastheadLink= new Array("letstalk_masthead_alex_handling&owner=alex_handling", "letstalk_masthead_brian_exceptionalEngineering&owner=brian_Engineering", "letstalk_masthead_kirk_fuelEconomy&owner=kirk_fuelEconomy", "letstalk_masthead_don_reliability_&owner=don_reliability", "letstalk_masthead_doug_intro_&owner=doug_intro", "letstalk_masthead_ed_style&owner=ed_style");
   var pLink= "http://www.letstalktrucks.com/?cmp="
   var imageCount = mainImage.length-1;
   var imageBase = "/images/home/";
   var random = Math.random();
   var index = 1;
   for(i=0; i < imageCount; i++)
      if(random >= (i*(1/imageCount)) && random < ((i+1)*(1/imageCount)))
         index = i+1;
   /* The "masthead" element is the div id, in content/index_content.jsp, that contains
      the masthead image and buttons */
   var elem= document.getElementById("masthead");
   if (elem)
   {
      var clickEvent = "popUpWindow('" + pLink + mastheadLink[index] + "', 'gmc', 'location=no, menubar=yes, scrollbars=yes, status=yes, toolbar=yes, resizable=yes, screenx=20, screeny=150, left=0, top=0')";
      elem.innerHTML = '<img class="phHomeHero" src=' + imageBase + mainImage[index] +' width="840" height="457" border="0" alt="" title="" onclick="' + clickEvent + '"/>' + elem.innerHTML;
      var owners = document.getElementById("owners");
      owners.href = pLink + mastheadLink[index];
   }
}


/* pop-up function. only needs a url and winName parameter -- attributes are defaulted. 
it is recommended that call to function at least include width and height attributes.
any attribute passed in will take precedence over default. -- dtw 8/5/05 */
function popUpWindow(url,winName,attributes) {
	if (attributes.indexOf("directories") == -1) { attributes += ",directories=0"; }
	if (attributes.indexOf("location") == -1) {	attributes += ",location=0"; }
	if (attributes.indexOf("menubar") == -1) { attributes += ",menubar=0"; }
	if (attributes.indexOf("resizable") == -1) { attributes += ",resizable=0"; }
	if (attributes.indexOf("scrollbars") == -1) { attributes += ",scrollbars=1"; }
	if (attributes.indexOf("status") == -1) { attributes += ",status=0"; }
	if (attributes.indexOf("toolbar") == -1) { attributes += ",toolbar=0"; }
	if (attributes.indexOf("top") == -1) { attributes += ",top=80";	}
	if (attributes.indexOf("left") == -1) { attributes += ",left=50"; }
	
	var newWindow = window.open(url,winName,attributes);
	if (window.focus && newWindow) { newWindow.focus() }
}


// We have a lot of popup windows that show up all over the site. 
// We define them commonly here, so that should a request come in to change a param of the window (say the size, or position), 
// we can update it in one place as opposed to accross the site.


function onstarHome() {
	popUpWindow('http://www.onstar.com/us_english/jsp/index.jsp?division=gc','onstar_win','width=785,height=650');	
}
function onstarDemo() {
	popUpWindow('http://www.onstar.com/us_english/jsp/idemo/index.jsp?division=gc','onstar_win','width=785,height=650');	
}
function openkbb() {
	popUpWindow('http://www.kbb.com/kb/ki.dll/ke.kb.sz?gmsg&&&zip;5945&1&GMC','kbb_win','width=795,height=650');	
}
function openNFTop(){
	window.open("http://www.gmfleet.com/email/email_updates.jsp");
	}
	
function superPopUpWindow(url, window_name) {
	popUpWindow(url, window_name, 'width=605, height=580, scrollbars=no');	
}

//-- BEGIN tracking function ------------------------------------------------------------------------------
	
function clickTrack(oArg) {
		
	if (typeof(oArg.s_pageName) == "string") {	s_pageName = oArg.s_pageName;	}
	if (typeof(oArg.s_lnk) == "boolean") {s_lnk = oArg.s_lnk;} else {s_lnk = true;}
	if (typeof(oArg.s_linkName) == "string") { s_linkName = oArg.s_linkName;	}
	if (typeof(oArg.s_linkType) == "string") { s_linkType = oArg.s_linkType;	} else { s_linkType="o"; }					
	if (typeof(oArg.s_prop1) == "string") {	s_prop1 = oArg.s_prop1;		}					
	if (typeof(oArg.s_prop2) == "string") {	s_prop2 = oArg.s_prop2;		} else { s_prop2=""; }		
	if (typeof(oArg.s_prop3) == "string") {	s_prop3 = oArg.s_prop3;}
	if (typeof(oArg.s_prop11) == "string") { s_prop11 = oArg.s_prop11; }
    if (typeof(oArg.s_prop12) == "string") { s_prop12 = oArg.s_prop12; }
	if (typeof(oArg.s_prop13) == "string") { s_prop13 = oArg.s_prop13; }
    if (typeof(oArg.s_prop14) == "string") { s_prop14 = oArg.s_prop14; }
	if (typeof(oArg.s_prop19) == "string") { s_prop19 = oArg.s_prop19;	}					
	if (typeof(oArg.s_prop20) == "string") { s_prop20 = oArg.s_prop20;	}	
	if (typeof(oArg.s_prop23) == "string") { s_prop23 = oArg.s_prop23; }
		
	//s_lnk=true;
	s_gs(s_account);

	s_prop2 = "";
	s_prop19 = "";
	s_prop20 = "";
	s_prop3 = "";
	s_prop12 = "";
	s_prop14 = "";
	s_prop1 = "";
    s_prop23 = "";
}
//-- END tracking function ------------------------------------------------------------------------------

function flashIsReady() {
	if (typeof isFlashReady != "undefined") {
		isFlashReady = true; 
	}
}

function setActiveVehicle(newVehicle) {
	if (typeof vehicle != "undefined") {
		vehicle = newVehicle; 
	}
}


/* as of today, this is only used by gvwr popup. it loads the url passed to it in a popup's parent window */
function gFlashBack(url) {
	if (window.opener && !window.opener.closed) {
		window.parent.opener.top.location.href = url;
		window.parent.opener.focus();
	} else {
		var NewWindow = window.open(url,'new_parent');
	}
}