var contexts = [
  // Index   Site Name              Site Section   Site ID   Color
  /* 0 */ /* Products          */ [ 'products',      0,      'orange' ],
  /* 1 */ /* Help              */ [ 'help',          0,      'muted_blue' ],
  /* 2 */ /* T&C               */ [ 'training_cert', 0,      'red' ],
  /* 3 */ /* Technical Support */ [ 'support',       0,      'red' ],
  /* 4 */ /* Corporate (About) */ [ 'corporate',     0,      'blue' ],
  /* 5 */ /* News & Events     */ [ 'news_events',   0,      'purple' ],
  /* 6 */ /* Partners          */ [ 'partners',      0,      'yellow' ],
  /* 7 */ /* Solutions         */ [ 'solutions',     0,      'teal' ],
  /* 8 */ /* Communities       */ [ 'partners',      0,      'yellow' ],
  /* 9 */ /* Country/Region    */ [ 'global',        0,      'blue' ],
  /* 10 */ /* Services         */ [ 'services',      0,      'green' ],
  /* 11 */ /* All Other        */ [ 'default',       0,      'blue' ]
];

var patterns = [
  // [ Context,  Pattern ]
 
  // Solutions
  [ 7, '/solutions' ],
  [ 7, '/cgi-bin/mynn3/solution' ],

  // Products
  [ 0, '/products/announcements' ],
  [ 0, '/products' ],
  [ 0, '/promotions' ],
  [ 0, '/ecommerce' ],

  // Services
  [ 10, '/cgi-bin/mynn3/service' ],

  // Support and training
  [ 3, '/servsup' ],
		[ 3, '/support' ],

  // Partners
  [ 6, '/prd' ],

  // News and events:
  [ 5, '/corporate/news' ],
  [ 5, '/corporate/events' ],
  [ 5, '/corporate/success' ],
  [ 5, '/corporate/awards' ],
  [ 5, '/corporate/pressroom' ],
  [ 5, '/corporate/nltrs' ],
  [ 5, '/cgi-bin/mynn3/news' ],
  [ 5, '/cgi-bin/mynn3/events' ],
  [ 5, '/cgi-bin/mynn3/awards' ],
  [ 5, '/cgi-bin/mynn3/casestudies' ],
  [ 5, '/cgi-bin/mynn3/topnews' ],

  // Country / Region
  [ 9, '/corporate/global' ],

  // About
  [ 4, '/corporate' ],
  [ 4, '/employment' ],

  // Help
  [ 1, '/help' ],
  [ 1, '/help/contact/erc' ]

];

function pickContext() {
    var url = window.document.location.href;

    for ( var i = 0; i < patterns.length; i++ ) {
        if ( url.match( patterns[i][1] ) ) {
            return patterns[i][0];
        }
    }

    return 11;
}

function getContext( siteSection ) {
    for ( i in contexts ) {
        if ( contexts[i][0] == siteSection ) {
            return Number(i);
        }
    }
    return 11;
}

function getContextualSiteSection() {
    return contexts[ pickContext() ][0];
}

function getContextualSiteId( siteSection ) {
    return contexts[ getContext( siteSection ) ][1];
}

function getContextualColor( siteSection ) {
    return contexts[ getContext( siteSection ) ][2];
}

/* Language functions */

function getLangId( lang ) {
    // Chinese
    if ( lang == 'ch' ) {
        return 1;
    }

    // English
    else if ( lang == 'en' ) {
        return 2;
    }

    // French
    else if ( lang == 'fr' ) {
        return 3;
    }

    // German
    else if ( lang == 'de' ) {
        return 4;
    }

    // Italian
    else if ( lang == 'it' ) {
        return 5;
    }

    // Japanese
    else if ( lang == 'jp' ) {
        return 6;
    }

    // Korean
    else if ( lang == 'kr' ) {
        return 7;
    }

    // Spanish
    else if ( lang == 'sp' ) {
        return 8;
    }

    // Portuguese
    else if ( lang == 'pt' ) {
        return 9;
    }

    // Return English for all languages not supported by search
    return 2;
}


/* header and footer search functions */

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;
}

var samCookie = readCookie('SAMTicket');
var secureSearchURL = 'https://search.nortel.com/go/query.jsp';
var changeSearchURL = false;
var initHeaderSearch = false;
var initFooterSearch = false;
var headerSearchBoxFocus = false;
var footerSearchBoxFocus = false;

// if it's a secure page and the user is logged in, we want to change the search URL

if ((location.href.indexOf('https') != -1) && samCookie) { changeSearchURL = true; } 

// these functions are called by the render_header and render_footer files
// to change the action of the search forms in the header and footer

// these are called onkeypress for each of the search fields
function checkHeaderSearch(k) { if (k == 13) { doHeaderSearch(); return false; } else { initHeaderSearch = true; } }
function checkFooterSearch(k) { if (k == 13) { doFooterSearch(); return false; } else { initFooterSearch = true; } }

// these are called when the focus shifts to the search fields
function doHeaderSearchFocus(e) {
  if (! headerSearchBoxFocus) { 
    e.value = ''; 
    headerSearchBoxFocus = true;
  }
  initHeaderSearch = true;
  return;
}

function doFooterSearchFocus(e) {
  if (! footerSearchBoxFocus) { 
    e.value = ''; 
    footerSearchBoxFocus = true;
  }
  initFooterSearch = true;
  return;
}

// these are called when the user either clicks on the search button,
// or hits enter in the search field
function doHeaderSearch() {

  if (changeSearchURL) { document.HeaderSearch.action = secureSearchURL; }

	var value = document.HeaderSearch.anyWords1.value;

	if (initHeaderSearch && (value != '')) {
		document.HeaderSearch.tx0.value=document.HeaderSearch.anyWords1.value;
		document.HeaderSearch.site.value=siteid;
		document.HeaderSearch.submit();
	} else {
    if (changeSearchURL) {
      location.href = 'https://search.nortel.com/go/index.jsp?locale=' + locale + '&site=' + siteid;
      return false;
    } else {
      location.href = 'http://search.nortel.com/go/index.jsp?locale=' + locale + '&site=' + siteid;
      return false;
    }
	}
}

function doFooterSearch() {

  if (changeSearchURL) { document.FooterSearch.action = secureSearchURL; }

	var value = document.FooterSearch.anyWords1.value;

	if (initFooterSearch && (value != '')) {
		document.FooterSearch.tx0.value=document.FooterSearch.anyWords1.value;
		document.FooterSearch.site.value=siteid;
		document.FooterSearch.submit();
	} else {
    if (changeSearchURL) {
      location.href = 'https://search.nortel.com/go/index.jsp?locale=' + locale + '&site=' + siteid;
    } else {
      location.href = 'http://search.nortel.com/go/index.jsp?locale=' + locale + '&site=' + siteid;
    }
	}
	return false;
}

