// ************************************************************************************ //
//
// File name: global.js
// Version: 1.0
// Date: 05/29/2006
// Adapted by: Forrest Frazier
// Date: 05/14/2010
// Adapted by: Chris Court
//
//
// ************************************************************************************ //

/* ************************************************************************** */
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;
}
/* ************************************************************************** */


/* ************************************************************************** */

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/gmflexcard?mkt=353&";
		} else {
			var targetUrl = "https://www.hsbcapply.com/start/gmflexcard?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&WT.ac=CRS_IM001097';
			else if (container.indexOf("cadillac.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000612XXHHX&WT.ac=CRS_IM001098';
			else if (container.indexOf("chevrolet.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000613XXHHX&WT.ac=CRS_IM001099';
			else if (container.indexOf("gmc.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000614XXHHX&WT.ac=CRS_IM001102';
			else if (container.indexOf("pontiac.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000616XXHHX&WT.ac=CRS_IM001121';
			else if (container.indexOf("gm.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000620XXHHX&WT.ac=CRS_IM001101';
			else if (container.indexOf("gmcertified.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000621XXHHX&WT.ac=CRS_IM001105';
			else if (container.indexOf("google.com") != -1)
				media_code = 'media=' + 'F5OS099AAX0806000608XXHHX&WT.ac=CRS_OS001121';
			else if (container.indexOf("search.yahoo.com") != -1)
				media_code = 'media=' + 'F5OS099AAX0806000608XXHHX&WT.ac=CRS_OS001121';
			else if (container.indexOf("search.msn.com") != -1)
				media_code = 'media=' + 'F5OS099AAX0806000608XXHHX&WT.ac=CRS_OS001121';
			else if (container.indexOf("search.aol.com") != -1)			
				media_code = 'media=' + 'F5OS099AAX0806000608XXHHX&WT.ac=CRS_OS001121';
								
			// 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&WT.ac=CRS_IM001100';

			//alert("media code (" + media_code + ")");

    } else {
			media_code = 'media=' + 'F5IM099AAX0807000671XXHHX&WT.ac=CRS_IM001109';
			
		//    	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/gmcard?mkt=207&";
		} else {
			var targetUrl = "https://www.hsbcapply.com/start/gmcard?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&WT.ac=CRS_IM001097';
			else if (container.indexOf("cadillac.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000612XXHHX&WT.ac=CRS_IM001098';
			else if (container.indexOf("chevrolet.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000613XXHHX&WT.ac=CRS_IM001099';
			else if (container.indexOf("gmc.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000614XXHHX&WT.ac=CRS_IM001102';
			else if (container.indexOf("pontiac.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000616XXHHX&WT.ac=CRS_IM001121';
			else if (container.indexOf("gm.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000620XXHHX&WT.ac=CRS_IM001101';
			else if (container.indexOf("gmcertified.com") != -1)
				media_code = 'media=' + 'F5IM099AAX0806000621XXHHX&WT.ac=CRS_IM001105';
			else if (container.indexOf("google.com") != -1)
				media_code = 'media=' + 'F5OS099AAX0806000608XXHHX&WT.ac=CRS_OS001121';
			else if (container.indexOf("search.yahoo.com") != -1)
				media_code = 'media=' + 'F5OS099AAX0806000608XXHHX&WT.ac=CRS_OS001121';
			else if (container.indexOf("search.msn.com") != -1)
				media_code = 'media=' + 'F5OS099AAX0806000608XXHHX&WT.ac=CRS_OS001121';
			else if (container.indexOf("search.aol.com") != -1)			
				media_code = 'media=' + 'F5OS099AAX0806000608XXHHX&WT.ac=CRS_OS001121';
				
			// 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&WT.ac=CRS_IM001100';

			//alert("media code (" + media_code + ")");

    } else {
			media_code = 'media=' + 'F5IM099AAX0807000671XXHHX&WT.ac=CRS_IM001109';
			
		//    	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;
}

// detect ie6 and throw a roadblock
$(document).ready(function() {
	jQuery.each(jQuery.browser, function(i, val) {
		if (i=="msie" && jQuery.browser.version.substr(0,1)=="6")
		// test alert box
		//alert("this is ie6")

			// if ie6 show this div
			jQuery('#browser-upgrade-box-wrapper').show();
			// if they click button hide div
			jQuery('input#browser-upgrade').click(function() {
				jQuery('#browser-upgrade-box-wrapper').hide('400');
				return false;
			});
	});
	// External links
	$("a[rel=external]").click(function(event)
	{
		event.preventDefault();
		window.open($(this).attr("href"));
	});
	// sized popup Links
	$("a[rel^=popup]").click(function(event)
	{
		var winW = $(this).attr('rel').split('|')[1];
		var winH = $(this).attr('rel').split('|')[2];
		var winOptions = "status=0,toolbar=0,location=0,menubar=0,width="+winW+",height="+winH;
		event.preventDefault();
		window.open($(this).attr("href"),"", winOptions);
	});
	
	//alert(pageVariables.section);
	s.prop8 = "summer event";
});



