var cmmsTabs = new Array();
var cmmsTabHeaders = new Array();
var allFeatures;
var allWorktools;
function collectTabs() {
	var divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; ++i)
		if (divs[i].className && (divs[i].className == 'activeTab' || divs[i].className == 'inactiveTab'))
			cmmsTabs.push(divs[i]);
	for (var i = 0; i < cmmsTabs.length; ++i)
		cmmsTabHeaders.push(document.getElementById('a' + cmmsTabs[i].id));
	cmmsTabHeaders[0].parentNode.className = 'activeTabHeader';
}
function showTab(tab, feature) {
	for (var i = 0; i < cmmsTabs.length; ++i) {
		if (cmmsTabs[i].id == tab) {
			cmmsTabs[i].className = 'activeTab';
			cmmsTabHeaders[i].parentNode.className = 'activeTabHeader';
		} else {
			cmmsTabs[i].className = 'inactiveTab';
			cmmsTabHeaders[i].parentNode.className = 'inactiveTabHeader';
		}
	}
	if (document.cmmsTabForm)
		document.cmmsTabForm.lastTab.value = tab;
	if (tab == 'features' && !feature && allFeatures)
		document.getElementById('featuresTarget').innerHTML = allFeatures;
	if (tab == 'worktools' && !feature && allWorktools)
		document.getElementById('worktoolsTarget').innerHTML = allWorktools;
	if (tab == 'worktools' && !feature) {
		document.getElementById('changeWorktools').style.display = 'none';
		var subselects = document.getElementsByClassName('worktoolsSubSelect');
		for (var i = 0; i < subselects.length; ++i)
			subselects[i].style.display = 'none';
	}
}
function openPVE(url) {
	window.open(url, "", 'height=500,width=760,left=100,top=75,screenX=100,screenY=75,resize=0,toolbar=no,titlebar=no,scrollbar=no,menubar=no');
}
var inverter = new Array();
inverter['metric'] = 'english';
inverter['english'] = 'metric';
function changeUnits(type) {
	var types = document.getElementsByClassName(type);
	var invertedTypes = document.getElementsByClassName(inverter[type]);
	for (var i = 0; i < types.length; ++i)
		types[i].style.display = 'inline';
	for (var i = 0; i < invertedTypes.length; ++i)
		invertedTypes[i].style.display = 'none';
	createCookie('units', type, 1000);
	return false;
}
document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};
function createCookie(name,value,days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}
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;
}
function initUnits() {
	var cookieVal = readCookie('units');
	if (!cookieVal)
		cookieVal = 'english';
	changeUnits(cookieVal);
}
var def = '';
var arr = new Array();
var subFams = new Array();
var products = new Array();
function addSubFam(val, name) {
	subFams[val] = name;
}
function addProduct(subFam, val, name) {
	products[val] = new Product(subFam,val,name);
}
function Product(subFam,val,name) {
    this.subFam = subFams[subFam];
    this.val = val;
    this.name = name;
}
function getProducts(country) {
	for (var i = 0; i < arr.length; ++i)
		if (arr[i].indexOf(country) > -1)
			return arr[i].split(':')[0];
	return def;
}
function modc(str) {
	var spl = str.split(':');
	if (spl.length == 2)
		arr.push(str);
	else
		def = str;
}
function buildSubFamilies(country) {
	var prod = getProducts(country).split(',');
	var mod = document.cmmsTabForm.model;
	for (var i = 0; i < prod.length; ++i) {
		mod.options[mod.options.length] = new Option(products[prod[i]].name, prod[i]);
	}
}
function buildModel(country) {
	var prod = getProducts(country).split(',');
	var lastSub = '';
	var mod = document.cmmsTabForm.model;
	for (var i = 0; i < prod.length; ++i) {
		var subFam = products[prod[i]].subFam;
		if (subFam != lastSub) {
			mod.options[mod.options.length] = new Option('-----' + subFam + '-----', '');
			lastSub = subFam;
		}
		mod.options[mod.options.length] = new Option(products[prod[i]].name, prod[i]);
	}
}
function changeModel(model, fullPath) {
	var val = model.options[model.selectedIndex].value;
	if (val != '')
		location.href = fullPath ? val : ('/cda/layout' + getOIDValue("f") + 'f=' + val);
}
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));
      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;
      output = output + String.fromCharCode(chr1);
      if (enc3 != 64)
         output = output + String.fromCharCode(chr2);
      if (enc4 != 64)
         output = output + String.fromCharCode(chr3);
   } while (i < input.length);
   return output;
}
function getCountry() {
	var cook;
	if (document.cookie.indexOf('tempCdaPreferences=') > -1)
		cook = 'tempCdaPreferences=';
	else if (document.cookie.indexOf('cdaPreferences2=') > -1)
		cook = 'cdaPreferences2=';
	else if (document.cookie.indexOf('cdaPreferences=') > -1)
		cook = 'cdaPreferences=';
	if (cook)
		return decode64(document.cookie.split(cook)[1].split(';')[0]).split('country=')[1].split('|')[0];
	return 'US';
}
var firstAttachment = true;
var firstFeature = true;
function transferFeature(isAttachment) {
	if (isAttachment) {
		var fi = frames['worktoolsIframe'];
		if (fi) {
			var ft = document.getElementById('worktoolsTarget');
			if (fi && ft) {
				if (!allWorktools)
					allWorktools = ft.innerHTML;
				var filterSplit = fi.location.search.split('filter=');
				document.getElementById('changeWorktools').style.display = (filterSplit.length > 1) ? 'block' : 'none';
				if (filterSplit.length > 1) {
					var subBox = document.getElementById('s' + filterSplit[1]).childNodes[0];
					var visibleElts = '|';
					for (var i = 0; i < subBox.options.length; ++i)
						visibleElts += 'p' + subBox.options[i].value.split('#')[1] + '|';
					var rows = fi.document.getElementById('worktoolList').childNodes[0].getElementsByTagName('tr');
					var eltsToRemove = new Array();
					for (var i = 1; i < rows.length; ++i) {
						var row = rows[i];
						if (visibleElts.indexOf('|' + row.id + '|') == -1)
							eltsToRemove.push(row);
					}
					for (var i = 0; i < eltsToRemove.length; ++i)
						eltsToRemove[i].parentNode.removeChild(eltsToRemove[i]);
				}
				var subselects = document.getElementsByClassName('worktoolsSubSelect');
				for (var i = 0; i < subselects.length; ++i)
					subselects[i].style.display = 'none';
				var subSplit = fi.location.search.split('&subId=');
				if (subSplit.length > 1)
					document.getElementById('s' + subSplit[1]).style.display = 'block';
				ft.innerHTML = '<table id=fullarticle border=0 cellspacing=0 cellpadding=0 width=100%>' + fi.document.getElementById('fullarticle').innerHTML + '</table>';
				showTab('worktools', true);
			}
		}
	} else {
		if (frames['featuresIframe'] && frames['featuresIframe'].location.hash) {
			var fi = frames['featuresIframe'];
			var ft = document.getElementById('featuresTarget');
			if (fi && ft) {
				if (!allFeatures)
					allFeatures = ft.innerHTML;
				var featureId = fi.location.hash.split('#')[1];
				ft.innerHTML = fi.document.getElementById(featureId).innerHTML;
				showTab('features', true);
				document.getElementById('changeWorktools').style.display = 'block';
			}
		} else {
			var ft = document.getElementById('featuresTarget');
			if (ft && allFeatures) {
				ft.innerHTML = allFeatures;
			}
		}
	}
	initUnits();
}
function refreshFeature(featureId) {
	var fi = frames['featuresIframe'];
	if (fi && fi.location.href) {
		if (fi.location.href.indexOf('fullArticleNoNav') > -1) {
			var ft = document.getElementById('featuresTarget');
			if (!allFeatures)
				allFeatures = ft.innerHTML;
			ft.innerHTML = fi.document.getElementById(featureId).innerHTML;
			showTab('features', true);
			document.getElementById('changeWorktools').style.display = 'block';
		} else {
			return true;
		}
	}
	return false;
}
function refreshAttachment(attachmentId) {
	var fi = frames['worktoolsIframe'];
	if (fi && fi.location.href) {
		if (fi.location.href.indexOf('fullArticleNoNav') > -1) {
			var ft = document.getElementById('worktoolsTarget');
			if (!allFeatures)
				allFeatures = ft.innerHTML;
			ft.innerHTML = fi.document.getElementById(attachmentId).innerHTML;
			showTab('worktools', true);
		}
		return true;
	} else {
		location.href = '/cda/layout' + getOIDValue("id") + 'id=' + attachmentId;
		return false;
	}
}
function assocAttachments(product) {
	var productFilter = location.href.split('&productFilter=');
	if (productFilter.length == 2) {
		var atts = ',';
		for (var i = 0; i < arguments.length; ++i) {
			atts += 'p' + arguments[i] + ',';
		}
		var val = productFilter[1].split('&')[0];
		var trs = document.getElementById('worktoolList').getElementsByTagName('tr');
		for (var i = 0; i < trs.length; ++i) {
			if (trs[i].id && atts.indexOf(',' + trs[i].id + ',') == -1) {
				trs[i].style.display = 'none';
			}
		}
	}
}
function showUnits() {
	document.write('<div class=english id=units>Units: <span>US</span> | <a href="" onClick="return changeUnits(\'metric\');">Metric</a></div><div class=metric id=units>Units: <a href="" onClick="return changeUnits(\'english\');">US</a> | <span>Metric</span></div>');
}
var families = new Array();
var lastFamily;
var lastSubFamily;
var lastTechSpec;
function addRange(countries, value, techSpec, subFamily, family) {
	if (family) lastFamily = family; else family = lastFamily;
	if (subFamily) lastSubFamily = subFamily; else subFamily = lastSubFamily;
	if (techSpec) lastTechSpec = techSpec; else techSpec = lastTechSpec;
	var techSpecs = families[family + '-' + subFamily];
	if (!techSpecs) {
		techSpecs = new Array();
		families[family + '-' + subFamily] = techSpecs;
	}
	var countryMap = techSpecs[techSpec];
	if (!countryMap) {
		countryMap = new Array();
		techSpecs[techSpec] = countryMap;
	}
	countryMap[',' + countries + ','] = value;
}
function getRanges(family, subFamily, country) {
	var ranges = new Array();
	var techSpecs = families[family + '-' + subFamily];
	for (var i in techSpecs)
		for (var j in techSpecs[i])
			if (j.indexOf(',' + country + ',') > -1)
				ranges[i] = techSpecs[i][j];
	return ranges;
}
function cmmsRanges(family, subFamily) {
	var arr = getRanges(family, subFamily, getCountry());
	var val = '<div id=contentList><ul class=cmmsRangeSpecs>';
	for (var i in arr)
		val += '<li>' + i + ', ' + arr[i] + '</li>';
	val += '</ul></div>';
	document.write(val);
}
function displaySpec(spec, country) {
	var parts = spec.split(',');
	for (var i in parts) {
		if (parts[i].split('=')[0].indexOf(country) > -1) {
			return parts[i].split('=')[1];
		}
	}
	return parts[0];
}
function addSpecs(specs) {
	var result = '<ul id=contentList>';
	var nb = '&nbsp;';
	var country = getCountry();
	country = country.charAt(0).toUpperCase() + country.charAt(1).toLowerCase();
	for (var i in specs) {
		var spec = specs[i];
		result += '<li>' + spec.desc + ',' + nb + '<div class=english>' + displaySpec(spec.range.eng, country) + nb + spec.unit.eng + '</div><div class=metric>' + displaySpec(spec.range.met, country) + nb + spec.unit.met + '</div>';
		if (spec.range.engEV)
			result += nb + spec.toField + nb + '<div class=english>' + displaySpec(spec.range.engEV, country) + nb + spec.unit.eng + '</div><div class=metric>' + displaySpec(spec.range.metEV, country) + nb + spec.unit.met + '</div>';
		result += '</li>';
	}
	result += '</ul>';
	document.write(result);
}
