//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='/scripts/comparator/ + 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() {
    if (brand == "canyon"){
	   var browser=navigator.appName;
	   if (browser=="Microsoft Internet Explorer"){
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=canyon&deepLink=overview','onstar_win','width=693,height=490', 'scrollbars=no');
	   } 
	   else{
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=canyon&deepLink=overview','onstar_win','width=675,height=465', 'scrollbars=no');
	   }
    }	

    if (brand == "acadia"){
	   var browser=navigator.appName;
	   if (browser=="Microsoft Internet Explorer"){
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=acadia&deepLink=overview','onstar_win','width=693,height=490', 'scrollbars=no');
	   } 
	   else{
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=acadia&deepLink=overview','onstar_win','width=675,height=465', 'scrollbars=no');
	   }
    }	

    if (brand == "envoy"){
	   var browser=navigator.appName;
	   if (browser=="Microsoft Internet Explorer"){
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=envoy&deepLink=overview','onstar_win','width=693,height=490', 'scrollbars=no');
	   } 
	   else{
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=envoy&deepLink=overview','onstar_win','width=675,height=465', 'scrollbars=no');
	   }
    }	

    if (brand == "yukon"){
	   var browser=navigator.appName;
	   if (browser=="Microsoft Internet Explorer"){
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=yukon&deepLink=overview','onstar_win','width=693,height=490', 'scrollbars=no');
	   } 
	   else{
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=yukon&deepLink=overview','onstar_win','width=675,height=465', 'scrollbars=no');
	   }
    }	
	
    if (brand == "savana"){
	   var browser=navigator.appName;
	   if (browser=="Microsoft Internet Explorer"){
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=savana&deepLink=overview','onstar_win','width=693,height=490', 'scrollbars=no');
	   } 
	   else{
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=savana&deepLink=overview','onstar_win','width=675,height=465', 'scrollbars=no');
	   }
    }	
	
    if (brand == "sierra900"){
	   var browser=navigator.appName;
	   if (browser=="Microsoft Internet Explorer"){
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=sierraNew&deepLink=overview','onstar_win', 'width=693,height=490', 'scrollbars=no');
	   } 
	   else{
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=sierraNew&deepLink=overview','onstar_win','width=675,height=465', 'scrollbars=no');
	   }
    }	

    if (brand == "sierra"){
	   var browser=navigator.appName;
	   if (browser=="Microsoft Internet Explorer"){
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=sierraClassic&deepLink=overview','onstar_win', 'width=693,height=490', 'scrollbars=no');
	   } 
	   else{
	      popUpWindow('http://www.onstar.com/us_english/jsp/services/index.jsp?make=gmc&model=sierraClassic&deepLink=overview','onstar_win','width=675,height=465', 'scrollbars=no');
	   }
    }	
 }

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 lowesPromo() {
  window.location.href = "/promo/lowes/sweeps.jsp?source=lowes_2006_news";
}

function superPopUpWindow(url, window_name) {
	popUpWindow(url, window_name, 'width=605, height=580, scrollbars=no');
}
function openNFTop(){
	window.open("http://www.gmfleet.com/email/email_updates.jsp");
}


//-- 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; }                                                           
   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; }

   //alert(s_pageName+"~"+s_linkName+"~"+s_linkType+"~"+s_prop1+"~"+s_prop2+"~"+s_prop3+"~"+s_prop19+"~"+s_prop20+"~");
   //alert(s_account);
  // s_lnk=true;
   s_gs(s_account);
   s_prop19 = "";
   s_prop20 = "";
   s_prop3 = "";
   s_prop12 = "";
   s_prop14 = "";
   s_prop23 = "";
   s_prop2 = "";
   s_prop1 = "";
   s_prop23 = "";
}
//-- END tracking function ------------------------------------------------------------------------------

function flashIsReady() {
	if (typeof isFlashReady != "undefined") {
		isFlashReady = true; 
	}
}

// called by flash whenever the active vehicle trim changes
function setActiveVehicle(newVehicle) {
	if (typeof vehicle != "undefined") {
		vehicle = newVehicle; 
	}
}

function validateZIP(ZipField)
{
     var valid = "0123456789";
     var length = ZipField.length
     if (length !=5)
     {

       alert("Please enter your 5 digit zip code.");
       return false;
     }
     for (var i=0; i < length; i++) {
     	temp = "" + ZipField.substring(i, i+1);
     
     	if (valid.indexOf(temp) == "-1")
       	{
        	alert("Invalid characters in your zip code.  Please try again.");
        	return false;
     	}
     }
}

// --------------------------------------------------------------
// generic name/value pair function
// return a hash (of sorts) of name value pairs from the querystring
// uasge: var qsParams = getQsParams();
// then: qsParams['url'] whould equal whatever url=XXX in the querystring

function getQsParams() {
	var qs = location.search;
	qs = qs.substring(1);
	// create an 'array' called newArray with the name value pairs from the querystring
	var qsArray = new Array;
	qsArray = qs.split('&'); //creating an array in which the values are separated by ampersands in the code//
	var keyValueArray = new Array; //this one loads the names and values into a hash (of sorts)//
	for(i=0; i<qsArray.length; i++) {
		var nameValue = qsArray[i].split('='); //splitting what we find between each ampersand into key value pairs //
		keyValueArray[nameValue[0]] = unescape(nameValue[1]); //we are then turning all the escaped characters back into the 'real thing' ie. %3F turns into a '?' //
	}
	return keyValueArray;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/*usage: url = setURLParameter(, "year"., value)*/
function setURLParameter(url, parameter, value){
	regexS = "([\\?&])" + parameter + "=[^&#]*";
	regex = new RegExp(regexS, "g");
	var results = regex.exec(url);
	if (results == null)
		return url + ((url.indexOf("?") == -1)?"?":"&") + parameter + "=" + value;
	else
		return url.replace(regex, results[1] + parameter + "=" + value);
}


