﻿//Override.js (v1.0.0.1)
function getCookie(c_name)
{
  if (document.cookie.length>0)
  {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
    {
      c_start=c_start + c_name.length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

function checkCookieAndRedirectToMap()
{
	if (document.location.href.toLowerCase().indexOf('/oem/en/pages/index.aspx') >= 0)
	{
		var cookieVal = new String();
		cookieVal = getCookie('OPC');
		
		if (cookieVal == null && cookieVal == '')
		{
			document.location.href = '/oem/pages/preferences.aspx';
			return;
		}
	}
}


function overrideCountry()
{		
	var override_country = document.location.href;
	var override_country_arr = override_country.split('?');
	var overriden_country = '';

	if (override_country_arr.length > 1)
	{
		var country_arr_val = override_country_arr[1].split('=');
		if (country_arr_val.length > 1 && country_arr_val[0].toLowerCase() == 'country')
		{
			overriden_country = country_arr_val[1];
		}
	}

	if (overriden_country == '')
	{
		checkCookieAndRedirectToMap();
	}
}

function checkCookieAndRedirectVariation()
{

	if (document.location.href.toLowerCase().indexOf('landing.aspx') > 0)
	{
		var	cookieVal = getCookie('OPC');

		if (cookieVal != null && cookieVal != '')
		{
			
		}
		else
		{
			document.location.href="/oem/pages/preferences.aspx";
			return;
		}

		var cookie = getCookie('RedirectURL');

		var url = document.location.href.toLowerCase();

		// If cookie missing or missing the language
		if (cookie == '' || cookie == '/oem')
		{
			document.location.href="/oem/en/pages/index.aspx";
			return;
		}

		if (cookie.toLowerCase().indexOf("http://") >= 0)
		{
			document.location.href = cookie;
			return;
		}

		if (url.indexOf(cookie + "/") >= 0)
		{
			//stay -- home site of preference			
			document.location.href= cookie + "/pages/index.aspx";
		}
		else if (cookie.indexOf("/oem/") >= 0)
		{
		    // Changed to use /pages/index.aspx to prevent looping
            document.location.href = cookie + "/pages/index.aspx";
		}
		else
		{
			document.location.href= cookie + "/oem/en/pages/index.aspx";
		}
	}
}
