// ************************************************************************************ //
//
// File name: global.js
// Version: 1.0
// Date: 05/29/2006
// Adapted by: Forrest Frazier
// updated: 03/08/07 by Forrest Frazier to add windowLinks
//
//
//
// ************************************************************************************ //

/* ************************************************************************** */
function goToBrandPage()
{
	document.location.href = document.forms["BrandNav"].getBrand.options[document.BrandNav.getBrand.selectedIndex].value;
}
/* ************************************************************************** */

/* ************************************************************************** */
function goToStylePage()
{
	document.location.href = document.forms["BrandNav"].getStyle.options[document.BrandNav.getStyle.selectedIndex].value;
}
/* ************************************************************************** */
//
/* Simon Willison's addLoadEvent function allows you to stack up 'window.onload' events
without them stepping on each other's toes.
It's explained here - http://www.sitepoint.com/blog-post-view.php?id=171578 */
window.onload = windowLinks;

// rel="external"
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function winPop(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
window.open(strURL, '', strOptions);
}

function windowLinks() {
    if (!document.getElementsByTagName) {
         return;
    }

var anchors = document.getElementsByTagName("a");
	for (var i = 0; i < anchors.length; i++) {
    	var anchor = anchors[i];
        var relIndex = anchor.rel;
		if (relIndex){
		 	var relSplit = relIndex.split("|");
			if (relSplit[0] == "external") {
            	anchor.target = "_blank";
				anchor.title = "Loads in new window: "+ anchor.href;
   			} else if (relSplit[0] == "popup") {      // else if relSplit[0] is 'popup'...
				anchor.title = "Loads in a Popup Window";
				anchor.popupWidth = relSplit[1];
				anchor.popupHeight = relSplit[2];
	        	anchor.onclick = function() 									{winPop(this.href,'console',this.popupWidth,this.popupHeight);return false;};
			}
		}
	}
}

addLoadEvent(function() {
	windowLinks();	// run our new function as soon as the page loads.
	//goHere();
});


function pageview(n) {
	var e, d=document
	for (var x=0; x<10; x++) if (e=d.getElementById('view'+x)) e.style.display='none'
	if (e=d.getElementById('view'+n)) e.style.display='block'
}

function launchSurvey() {
	var surveyurl = "http://online.tns-global.com/wix/p217845304.aspx?trackid=";

	// trackid needs to be set dynamically via ALG backend. simply hardcoded for test. I need multipe things to happen on the click so I broke it out
	// into a js function call.
	var trackid = "123";

	//alert(surveyurl + trackid);
	top.TB_remove();
	window.open(surveyurl + trackid);
}

function showDiv(boxid){
   if(document.getElementById(boxid).style.display == 'none')
   {
   	document.getElementById(boxid).style.display = 'block';
   }else{
   	document.getElementById(boxid).style.display = 'none';
   }
}

// Mullen version of the get media code function. This function looks for a passed in media code, if not then domain and set media code based upon that
// if there is not a media code and a referring domain simply pass the full url as media code

 function getFilteredMediaCode(mktCode) {

		if (mktCode==null) {
			var targetUrl = "https://www.hsbcapply.com/start/gmflex2?mkt=353&";
		} else {
			var targetUrl = "https://www.hsbcapply.com/start/gmflex2?mkt="+mktCode+"&";
		}	
			
		var media_code = "";

		// grab the container - s.prop21 contains referrer domain if there was no campaign code passed if there was it will contain compaign code
		// in which case the value will simply fall through the below conditional statement without triggering an action
		var container = s.getAndPersistValue(s.prop21,'s_cp_persist',0);
		// hardcoded var to use for testing. Simply comment out the line reading from the cookie and set this to the referrer domain your'e looking to test
		//var referrer = "www.gm.com";
		//alert("s.prop21 aka container (" + container + ")");

    var loc = document.location.href;
		//    alert("loc (" + loc+ ")");
    var locLength = loc.length;
		//    alert("locLength (" + locLength+ ")");
    var sliceStart = loc.indexOf("media=");
		//    alert("sliceStart (" + sliceStart+ ")");

    if (sliceStart>=0) {
      media_code = 'media=' + loc.substr(sliceStart+6, locLength);
    } else if (container != '') {

      // if the container contains a designated referrer, let's assign a corresponding media code against this value
			// various domains we need to filter against and assign a media code to - these values are defined per Oliver
			if (container.indexOf("buick.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000611XXHHX';
			else if (container.indexOf("cadillac.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000612XXHHX';
			else if (container.indexOf("chevrolet.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000613XXHHX';
			else if (container.indexOf("gmc.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000614XXHHX';
			else if (container.indexOf("hummer.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000615XXHHX';
			else if (container.indexOf("pontiac.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000616XXHHX';
			else if (container.indexOf("saabusa.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000617XXHHX';
			else if (container.indexOf("saturn.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000618XXHHX';
			else if (container.indexOf("gm.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000620XXHHX';
			else if (container.indexOf("gmcertified.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000621XXHHX';
			else if (container.indexOf("google.com") != -1)
				media_code = 'media=' + 'F5OS099AAX0806000608XXHHX';
			else if (container.indexOf("search.yahoo.com") != -1)
				media_code = 'media=' + 'F5OS099AAX0806000608XXHHX';
			else if (container.indexOf("search.msn.com") != -1)
				media_code = 'media=' + 'F5OS099AAX0806000608XXHHX';
			else if (container.indexOf("search.aol.com") != -1)			
				media_code = 'media=' + 'F5OS099AAX0806000608XXHHX';
								
			// if the container contains a campaign code that we want to assign a media code, let's assign a media co that - these values are defined
			// per Oliver
			if (container.indexOf("dealer") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000619XXHHX';

			//alert("media code (" + media_code + ")");

    } else {
			media_code = 'media=' + 'F5IM099AAX0807000672XXHHX';
			
		//    	alert("nothing");
    	// we'll assign the media code to the full url.  Since at this point we know nothing was passed on the querystring as media= and there
    	// was not a referrer domain or campaign code
     //	media_code = loc.substr(sliceStart+1, locLength);

    }

		var applicationUrl = targetUrl + media_code;
  	document.location.href = applicationUrl;
}

 function getFilteredCoreMediaCode(mktCode) {

		if (mktCode==null) {
			var targetUrl = "https://www.hsbcapply.com/start/gmflex2?mkt=353&";
		} else {
			var targetUrl = "https://www.hsbcapply.com/start/gmflex2?mkt="+mktCode+"&";
		}	
		
		var media_code = "";

		// grab the container - s.prop21 contains referrer domain if there was no campaign code passed if there was it will contain compaign code
		// in which case the value will simply fall through the below conditional statement without triggering an action
		var container = s.getAndPersistValue(s.prop21,'s_cp_persist',0);
		// hardcoded var to use for testing. Simply comment out the line reading from the cookie and set this to the referrer domain your'e looking to test
		//var referrer = "www.gm.com";
		//alert("s.prop21 aka container (" + container + ")");

    var loc = document.location.href;
		//    alert("loc (" + loc+ ")");
    var locLength = loc.length;
		//    alert("locLength (" + locLength+ ")");
    var sliceStart = loc.indexOf("media=");
		//    alert("sliceStart (" + sliceStart+ ")");

    if (sliceStart>=0) {
      media_code = 'media=' + loc.substr(sliceStart+6, locLength);
    } else if (container != '') {

      // if the container contains a designated referrer, let's assign a corresponding media code against this value
			// various domains we need to filter against and assign a media code to - these values are defined per Oliver
			if (container.indexOf("buick.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000611XXHHX';
			else if (container.indexOf("cadillac.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000612XXHHX';
			else if (container.indexOf("chevrolet.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000613XXHHX';
			else if (container.indexOf("gmc.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000614XXHHX';
			else if (container.indexOf("hummer.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000615XXHHX';
			else if (container.indexOf("pontiac.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000616XXHHX';
			else if (container.indexOf("saabusa.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000617XXHHX';
			else if (container.indexOf("saturn.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000618XXHHX';
			else if (container.indexOf("gm.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000620XXHHX';
			else if (container.indexOf("gmcertified.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000621XXHHX';
			else if (container.indexOf("google.com") != -1)
				media_code = 'media=' + 'F5IM095AAX0808000721XXHHX';
			else if (container.indexOf("search.yahoo.com") != -1)
				media_code = 'media=' + 'F5IM095AAX0808000721XXHHX';
			else if (container.indexOf("search.msn.com") != -1)
				media_code = 'media=' + 'F5IM095AAX0808000721XXHHX';
			else if (container.indexOf("search.aol.com") != -1)			
				media_code = 'media=' + 'F5IM095AAX0808000721XXHHX';
				
			// if the container contains a campaign code that we want to assign a media code, let's assign a media co that - these values are defined
			// per Oliver
			if (container.indexOf("dealer") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000619XXHHX';

			//alert("media code (" + media_code + ")");

    } else {
			media_code = 'media=' + 'F5IM099AAX0807000671XXHHX';
			
		//    	alert("nothing");
    	// we'll assign the media code to the full url.  Since at this point we know nothing was passed on the querystring as media= and there
    	// was not a referrer domain or campaign code
     //	media_code = loc.substr(sliceStart+1, locLength);

    }

		var applicationUrl = targetUrl + media_code;
  	document.location.href = applicationUrl;
}