﻿//v26
var bn_response = 0;
var country='', language='', description, locale='', wtSeg = '';
//BMS Specific Meta Data Variables
var productLine='', productCategory='', industries='', pageType='', businessUnit='', documentType='', solutionCategory='', accessory='';
baynote_getMetaInfo();
//console.log('language' + language);
//var languages = ['da', 'de', 'en', 'es', 'fi', 'fr', 'it', 'ja', 'ko', 'nl', 'no', 'pt', 'sv', 'zh_cn', 'zh_tw'];
//var corp_emea = ['cz', 'dk', 'hu', 'ie', 'il', 'no', 'at', 'pl', 'pt', 'ro', 'sk', 'es', 'se', 'tr', 'ua'];
//var corp_latam = ['ar', 'cl', 'co', 'cr', 'pe', 'pr', 've'];

var b2b_apac = ['XP', 'XA', 'HK', 'ID', 'JP', 'PH', 'KR', 'TW', 'TH', 'VN'];
var b2b_emea = ['AF', 'XF', 'AT', 'W1', 'Y1', 'XC', 'W2', 'XE', 'IL', 'IT', 'XM', 'XN', 'W4', 'PK', 'PL', 'Y4', 'ZA', 'ES', 'W5', 'Y5'];
var b2b_latam = ['XL'];

var b2c_apac = ['Indonesia', 'ID', 'New Zealand', 'NZ', 'Philippines', 'PH', 'Thailand', 'TH', 'Vietnam', 'VN', 'AP', 'HK', 'TW', 'XP', 'XA', 'BD', 'BN', 'Bhutan', 'BT', 'KH', 'JP', 'LA', 'MV', 'NP', 'PW', 'PG',  'KR', 'LK', 'AP' ];
var b2c_emea = ['Africa', 'XF', 'Alternative Middle East', 'AR', 'Austria', 'AT', 'Belgium [Nederlands]', 'Belgium [Français]',  'Belgium [FranÃ§ais]', 'BE', 'Bulgaria', 'BG', 'Czech Republic', 'CZ', 'Danmark', 'DK', 'Finland', 'EU', 'FI', 'Greece', 'GR', 'Hungary', 'HU', 'IE', 'Eire', 'IL', 'Jamaica', 'JM', 'Kenya', 'KE', 'Lithuania', 'LT', 'Arab Countries [French]', 'Middle East and Africa', 'XZ', 'Nederlands', 'NL', 'Nigeria', 'NG', 'NN', 'Norwegian', 'Norweigan', 'NO','Pakistan', 'PK', 'Poland', 'PL', 'Portugal', 'PT', 'Romania', 'RO', 'Slovakia', 'SK', 'Slovenia', 'SI', 'SL', 'South Africa', 'ZA', 'España', 'EspaÃ±a','ES', 'Sweden', 'SE','Switzerland [Deutsch]', 'Switzerland [Français]', 'Switzerland [FranÃ§ais]', 'CH', 'Turkey', 'TR', 'UA', 'Ukraine', 'UR', 'XE', 'XW'];
var b2c_latam = ['Argentina', 'AR', 'América Central', 'AmÃ©rica Central', 'Cabire', 'CB', 'CE', 'CR', 'Chile', 'CL', 'Colombia', 'CO', 'Ecuador', 'EC', 'EP', 'Paraguay', 'PY', 'Peru', 'PE', 'PR', 'Caribe', 'Venezuela', 'VE','XA'];

var codes = new Array();
var b2bcodes = new Array();
/*******************************
codes['corp_emea'] = corp_emea;
codes['corp_latam'] = corp_latam;
codes['corp_na'] = ['ca',''];
codes['corp_au'] = ['au',''];
codes['corp_sg'] = ['sg',''];
codes['corp_br'] = ['br',''];
codes['corp_fr'] = ['fr',''];
codes['corp_de'] = ['de',''];
codes['corp_in'] = ['in',''];
codes['corp_it'] = ['it',''];
codes['corp_kr'] = ['kr',''];
codes['corp_my'] = ['my',''];
codes['corp_mx'] = ['mx',''];
codes['corp_ru'] = ['ru',''];
codes['corp_uk'] = ['uk',''];
codes['corp_usa'] = ['ww', 'us'];
*******************************/
b2bcodes['b2b_apac'] = b2b_apac;
b2bcodes['b2b_emea'] = b2b_emea;
b2bcodes['b2b_latam'] = b2b_latam;
b2bcodes['b2b_na'] = ['CA',''];
b2bcodes['b2b_cn'] = ['CN',''];
b2bcodes['b2b_au'] = ['AU',''];
b2bcodes['b2b_sg'] = ['SG',''];
b2bcodes['b2b_br'] = ['BR',''];
b2bcodes['b2b_fr'] = ['FR',''];
b2bcodes['b2b_de'] = ['DE',''];
b2bcodes['b2b_in'] = ['IN',''];
b2bcodes['b2b_kr'] = ['KR',''];
b2bcodes['b2b_my'] = ['MY',''];
b2bcodes['b2b_mx'] = ['MX',''];
b2bcodes['b2b_ru'] = ['RU',''];
b2bcodes['b2b_uk'] = ['XU',''];
b2bcodes['b2b_usa'] = ['US','WW'];
/********************************/
codes['b2c_apac'] = b2c_apac;
codes['b2c_emea'] = b2c_emea;
codes['b2c_latam'] = b2c_latam;
codes['b2c_ru'] = ['Россия','RU'];
codes['b2c_br'] = ['Brasil','BR'];
codes['b2c_cn'] = ['CN',''];
codes['b2c_de'] = ['Germany','DE'];
codes['b2c_in'] = ['India','IN'];
codes['b2c_it'] = ['Italia','IT']; 
codes['b2c_kr'] = ['Korea', 'KR'];
codes['b2c_mx'] = ['México', 'MÃ©xico', 'MX'];
codes['b2c_uk'] = ['United Kingdom','GB'];
codes['b2c_my'] = ['Malaysia','MY'];
codes['b2c_sg'] = ['Singapore','SG'];
codes['b2c_au'] = ['Australia','AU'];
codes['b2c_fr'] = ['France','FR'];
codes['b2c_na'] = ['Canada','CA'];
codes['b2c_sg'] = ['Singapore', 'SG'];
codes['b2c_usa'] = ['USA', 'US','WW'];

/*
 * This function will check if an array "a" has a certain value
 */
function oc(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}

/*
 * This function will go through every sub-array in codes[]. 
 * The for loop will get every key in codes[], that is 'corp_apac', 'b2c_apac', etc.
 * Then it will access the content of the sub-array with that key, that is codes['corp_apac'], 
 * and examine whether or not the string in var language is in that subarray.  
 * USA B2B and B2C has a special logic.
 */
function get_cc() {
	var code = '';	
	
		if (window.location.href.search(/business/i) != -1)  {
			for (i in b2bcodes) {
				if (country in oc(b2bcodes[i])) {
					code = i;
				}
			}
			if (code == '')
			{
				for (i in codes) {
					if (country in oc(codes[i])) {
						code = i;
					}
				}
			}
		}else {
		for (i in codes) {
			if (country in oc(codes[i])) {
				code = i;
				if(language=='AR' && country=='AR'){
					code='b2c_emea';
				}
			}
		}
	}
	if(language == 'Russian'){
		code = "b2c_ru";
	}

	if(code == ''){
		code = "www_en";
	}
	return code;
}

function baynote_getMetaInfo() {
	var metas = document.getElementsByTagName("meta");
	if (metas) {   
		for (var i = 0; i < metas.length; i++) {			
			if (metas[i] && metas[i].name =="language") {
				language =  metas[i].content;
			} else if (metas[i] && (metas[i].name =="country")) {
				country =  metas[i].content;
			} else if (metas[i] && metas[i].name =="description") {
				description =  metas[i].content;						
			} else if(metas[i] && (metas[i].name =="DCSext.locale")) {
				if(metas[i].content) {
					result = metas[i].content.split("-");
					if(result.length>1) {
						//country = result[0].toLowerCase();
						//language = result[1].toLowerCase();
						country = result[0];
						language = result[1];						
												
					}
				}
			} else if (metas[i] && metas[i].httpEquiv =="Content-Language") {			
				language =  metas[i].content;
			}else if (metas[i] && metas[i].name == "WT.seg_1" ){
				wtSeg = metas[i].content;
			} else if (metas[i] && metas[i].name == "ProductLine" ){
				productLine = metas[i].content;
			} else if (metas[i] && metas[i].name == "ProductCategory" ){
				productCategory = metas[i].content;
			} else if (metas[i] && metas[i].name == "Industries" ){
				industries = metas[i].content;
			} else if (metas[i] && metas[i].name == "PageType" ){
				pageType = metas[i].content;
			} else if (metas[i] && metas[i].name == "BusinessUnit" ){
				businessUnit = metas[i].content;
			} else if (metas[i] && metas[i].name == "DocumentType" ){
				documentType = metas[i].content;
			} else if (metas[i] && metas[i].name == "SolutionCategory" ){
				solutionCategory = metas[i].content;
			} else if (metas[i] && metas[i].name == "Accessory" ){
				accessory = metas[i].content;
			}
		}
	}
	if ( language == ''|| typeof(language) == undefined || country=='') {
		baynote_getSearchFormInfo();		
	}
	
	if(country.length == 2){
		country = country.toUpperCase();
	}
	
	if(language.length == 2){
		language = language.toUpperCase();
	}
	
	if(locale == '' && language != '' && country != ''){
		locale = country + '-' + language;	
	}	
	
}
function baynote_getSearchFormInfo() {
	if ( document.forms['frmSearch'] && document.forms['frmSearch'].elements['view']) {		
		result = document.forms['frmSearch'].elements['view'].value.split("-");
		if(result.length > 1) {
			/*country = result[0].toLowerCase();
			language = result[1].toLowerCase();*/
			country = result[0];
			language = result[1];			
		}
	} else if (document.forms['searchform'] && document.forms['searchform'].elements['qp']) {
		result = document.forms['searchform'].elements['qp'].value.split(":");
		if(result.length > 1) {
			country_language = result[1].split("-");
			if(country_language.length > 1) {
				/*country = result[0].toLowerCase();
				language = result[1].toLowerCase();*/
				country = country_language[0];
				language = country_language[1];
			}
		}
	}
}

function baynote_handleSubmit(frm){	
	var code = get_cc();
	document.getElementById("bn_cc").value = code;
	document.getElementById("bn_af").value = get_bn_af();
	document.getElementById("bn_if").value = get_bn_if(frm);
	
	var insertedEl = document.createElement("input");		
		insertedEl.setAttribute("name", "bn_lang");
		insertedEl.setAttribute("type", "hidden");
		if (typeof(language) != undefined && language=='') {
			language="en";
		}
		insertedEl.setAttribute("value", language);
		frm.appendChild(insertedEl);
		
	var insertedEl2 = document.createElement("input");		
		insertedEl2.setAttribute("name", "bn_code");
		insertedEl2.setAttribute("type", "hidden");		
		insertedEl2.setAttribute("value", code );
		frm.appendChild(insertedEl2);
		
	var insertedEl3 = document.createElement("input");		
		insertedEl3.setAttribute("name", "WTseg_1");
		insertedEl3.setAttribute("type", "hidden");		
		insertedEl3.setAttribute("value", wtSeg );
		frm.appendChild(insertedEl3);
	
	if (!bn_response || code == '') {
		var mode = document.getElementById("bn_mode").value ;
	
		frm.action = "http://search.motorola.com/query.html";
		
		document.getElementsByName("q")[0].name = "qt";		
	} 
}

function get_bn_af() {
	var code = get_cc();
	var filterGroup = "";
	
	
	if(locale.length > 2){
		if(code == 'b2c_apac' || code=='b2c_emea' || code=='b2c_latam' || code=='b2b_latam' || code=='b2b_emea' || code=='b2c_na' || code=='b2b_na' ){
			filterGroup="bnGroup:" + locale;
		}
	}
	
	return filterGroup;
}
function get_bn_if(frm) {
	var if_list = '';
	for (i=0; i<frm.elements.length; i++) {
		if(frm.elements[i].name == "col") {
			if (if_list == '') {
				if_list = frm.elements[i].value;
			} else {
				if_list = frm.elements[i].value + "," + if_list;
			}
		}
	}	
	return if_list;
}

/*	baynote_validateSearchBox() - Determine if the service is available and show the search box if it is.  */
function baynote_validateSearchBox(server, customerId, code) {
	var failsafeId = baynote_tag.getFailsafeResourceId();

	baynote_tag.server = server;
	baynote_tag.customerId = customerId;
	baynote_tag.code = code;
	
	if ((bnResourceManager != "undefined")
		 && (bnResourceManager != null)
		 && (typeof(failsafeId) != "undefined")
		 && (failsafeId != null)) {
			 bnResourceManager.waitForResource(failsafeId,
			 "baynote_modifySearchBox()",
			 baynote_tag.getFailsafeResourceAddress(),
			 "img");			 
		 }		
	}
/*	baynote_modifySearchBox() - Modify the search box to point directly to Ultraseek */
function baynote_modifySearchBox() {	
	bn_response=1;	
}