// Updated so that dowcom and dowagro default.js files are identical.
// Do not update this file without updating the other.
var IE4 = (document.all && !document.getElementById) ? true : false;
var NS4 = (document.layers) ? true : false;
var IE5 = (document.all && document.getElementById) ? true : false;
var N6 = (document.getElementById && !document.all) ? true : false;
var currentPageLink;

var domain = document.domain;
var currentEnvironment = '';
if (domain == "usmdlsdoww200" || domain == "usmdlsdoww840") {
	currentEnvironment = 'internal';
} else if (domain.indexOf("-st.") >= 0 || domain.indexOf("preview-") >= 0) {
	currentEnvironment = 'staging';
} else if (domain.indexOf("swdev") >= 0 || domain.indexOf("hodevwsiwoven02") >= 0) {
	currentEnvironment = 'dev';
} else if (domain.indexOf("wcmsandbox") >= 0 || domain.indexOf("brdevwsiwoven01") >= 0) {
	currentEnvironment = 'sandbox';
} else {
	currentEnvironment = 'live';
}
errorLog("Environment: " + currentEnvironment, 'info');

function errorLog(errorMessage, level) {
	if (typeof console == 'object') {
		if (typeof level == 'undefined')
			console.log(errorMessage);
		else if (level == 'log')
			console.log(errorMessage);
		else if (level == 'debug')
			console.debug(errorMessage);
		else if (level == 'error')
			console.error(errorMessage);
		else if (level == 'info')
			console.info(errorMessage);
	}
}

function preloadImages(arrayOfImages) {
	//imageObj = new Image();
	for(i=0; i < arrayOfImages.length; i++) {
		//imageObj.src=arrayOfImages[i];
		$("body").append("<img src='" + arrayOfImages[i] + "' width='1' height='1' />");
	}
}

function RemoveBad(newStr) {
	var index = 0;
	var strLength = 0;
	var left = "";
	var right = "";
	var badChars;
	var InStr;

	badChars = new Array(7);
	badChars[0] = "<";
	badChars[1] = ">";
	badChars[2] = "'";
	badChars[3] = "%";
	badChars[4] = ";";
	badChars[5] = "&";
	badChars[6] = "+";

	InStr = newStr;

	strLength = InStr.length;

	for (var i=0; i<=6; i++) {
		index = 0;
		while (index != -1) {
			index = InStr.indexOf(badChars[i]);
			if (index != -1) {
				if (index != 0) {
					left = InStr.substring(0,index);
				} else {
					left = "";
				}
				if (index != strLength - 1) {
					right = InStr.substring(index + 1, strLength);
				} else {
					right = "";
				}
				InStr = left + right;
			}
		}
	}
	return InStr;
}

function jumpto(sel) {
	var i;
	i = sel.selectedIndex;

	if (sel.options[i].value != "") {
		window.location = sel.options[i].value;
	}
}

function which_Browser() {
	var a,b;
	a = navigator.appVersion;
	b = a.charAt(0);
	if (b > 2) {
		nav_refresh();
	}
}

function nav_refresh() {
	document.navform.reset();
}

var browserOK = false;
var pics;
var NameString;
var objCount = 0;
NameString = navigator.appname + navigator.appCodeName + navigator.appVersion;
if (NameString.indexOf("Explorer") == -1) {
	if (NameString.indexOf("2.0") == -1) {
		browserOK = true;
		pics = new Array();
	}
}

function preload(name, first, second) {
	if (browserOK) {
		var args = (arguments.length);
		pics[objCount] = new Array(args);
		pics[objCount][0] = name;
		for (var i=1; i < args; i++) {
			pics[objCount][i] = new Image();
			pics[objCount][i].src = arguments[i];
		}
		objCount++;
	}
}

function on(name) {
	var imgIndex = 2;
	if (browserOK) {
		if (arguments.length > 1) {
			imgIndex = parseInt(arguments[1]);
		}
		for (i = 0; i < objCount; i++) {
			if (document.images[pics[i][0]] != null) {
				if (name != pics[i][0]) {
					document.images[pics[i][0]].src = pics[i][1].src;
				} else {
					if (imgIndex < pics[i].length) {
						document.images[pics[i][0]].src = pics[i][imgIndex].src;
					} else {
						document.images[pics[i][0]].src = pics[i][1].src;
					}
				}
			}
		}
	}
}

function extra(name) {
	var i=0;
	var imgIndex = 2;
	if (browserOK) {
		if (document.images[name] == null) {
			return 0;
		}
		if (arguments.length > 1) {
			imgIndex = parseInt(arguments[1]);
		}
		for(i=0;i<objCount;i++) {
			if (pics[i][0] == name) {
				break;
			}
		}
		if (pics[i][0] != null && imgIndex < pics[i].length) {
			document.images[name].src = pics[i][imgIndex].src;
		} else {
			document.images[name].src = pics[i][1].src;
		}
		return 1;
	}
	return 0;
}

function off() {
	if (browserOK) {
		for (i = 0; i < objCount; i++) {
			if (document.images[pics[i][0]] != null) {
				document.images[pics[i][0]].src = pics[i][1].src;
			}
		}
	}
}
			
function goToMailForm(formURL, recipient) {
	document.mailRecipientForm.recipient.value = recipient;
	document.mailRecipientForm.action = formURL;
	document.mailRecipientForm.submit();
}

function writeMailRecipientBlock() {
	document.write("<form name='mailRecipientForm' action='http://www.dow.com/' method='POST'>");
	document.write("<input name='recipient' type='hidden' value='' />");
	document.write("</form>");
}

function litOrder(title, formnum, business) {
	changecookie("doctitle", title);
	changecookie("formnum", formnum);
	var oldvar = "http://www.dow.com/webapps/lit/litorder.asp?OPTION="+formnum+"&Business="+business+"&DocTitle="+title;
	var newvar = oldvar;
	while(oldvar.indexOf(" ") != -1) {
		newvar = oldvar.substring(0,oldvar.indexOf(" "))+"%20"+oldvar.substring(oldvar.indexOf(" ")+1,oldvar.length);
		oldvar = newvar;
	}
	window.location = oldvar;
}

/**** Rot13 encoding/decoding ****/
/**** Used to deobfuscate contact form recipients ****/
function JSrot13(text) {
	var rot13text_rotated = ""; /* the function will return this string */;

	for (i = 1 ; i < (text.length + 1); i++) {
		k = text.charCodeAt(i-1);

		if (k >= 97 && k <= 109) {
			k = k + 13;
		} else {
			if (k >= 110 && k <= 122) {
				k = k - 13;
			} else {
				if (k >= 65 && k <= 77) {
					k = k + 13;
				} else {
					if (k >= 78 && k <= 90) {
						k = k - 13;
					}
				}
			}
		}
		rot13text_rotated = rot13text_rotated + String.fromCharCode(k);
	}
	return rot13text_rotated;
}

function iframeFix(CurFrame) {
	if (document.all) {
		CurFrame.width = '100%';
		CurFrame.height = 0;
		CurFrame.height = CurFrame.document.body.scrollHeight;
		NewHeight = CurFrame.height;
		NewHeight = CurFrame.document.body.offsetHeight-CurFrame.height+1*(CurFrame.height);
		CurFrame.height = NewHeight;
	} else {
		CurFrame.width = '100%';
		CurFrame.height = 450;
	}
}

function CreateAnswerCenterLink(Destination) {
	document.write('<a href="' + Destination + '"><img onmouseover="this.src=');
	document.write("'/images/icon-ac2-roll.gif'");
	document.write('" onmouseout="this.src=');
	document.write("'/images/icon-ac2.gif'");
	document.write('" height="46" alt="Answer Center" src="/images/icon-ac2.gif" width="224" border="0" name="icon" /></a>');
}

/**
 * Add a new function to be executed when the page loads.
 */
function addLoadEvent(func) {
	var oldOnload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldOnload();
			func();
		}
	}
}

/**
 * Add a new class to the given DOM element.
 */
function addClass(element, value) {
	if (!element) {
		return;
	}
		if (!element.className) {
		element.className = value;
	} else {
		var oldClassName = element.className;
		element.className = oldClassName + ' ' + value;
	}
}

/**
 * Remove a given class from a DOM element.
 */
function removeClass(element, value) {
	if (!element || !element.className) {
		return;
	} else {
		var oldClasses = element.className.split(' ');
		for (var i = 0; i < oldClasses.length; i++) {
			if (oldClasses[i] == value) {
				oldClasses.splice(i, 1);
			}
		}
		element.className = oldClasses.join(' ');
	}
}

function expandIframeDiv(ExpandButtonDiv) {
	NewIframeHeight = window.screen.height-325;
	NewIframeWidth = window.screen.width-100;
	CurActiveIframeDiv=ExpandButtonDiv.parentNode;
	cids=CurActiveIframeDiv.style;
	cids.position='absolute';
	cids.top='130px';
	cids.left='50px';
	if (NewIframeWidth >= 750) {
		NewIframeWidth = 750;
	}
	cids.height=NewIframeHeight + 'px';
	cids.width=NewIframeWidth + 'px';
	cids.border='black 4px solid';
	ActionButtons = CurActiveIframeDiv.getElementsByTagName('div')
	ActionButtons[0].style.display='';
	ActionButtons[1].style.display='none';
	ActionButtons[2].style.display='';
}

function collapseIframeDiv(CloseButtonDiv) {
	CurActiveIframeDiv=CloseButtonDiv.parentNode;
	cids=CurActiveIframeDiv.style;
	cids.top='0px';
	cids.left='0px';
	cids.width='100%';
	cids.height='25%';
	cids.position='relative';
	cids.border='none';
	ActionButtons = CurActiveIframeDiv.getElementsByTagName('div')
	ActionButtons[0].style.display='none';
	ActionButtons[1].style.display='';
	ActionButtons[2].style.display='none';
}

function buildExpandableIframe(expandLabel,iframeSource) {
	document.write('<div width="100%">');
	document.write('<div align="right" onclick="collapseIframeDiv(this)" class="iframe_action_button_div" style="display: none;">');
	document.write('<table border=0>');
	document.write('<tr>');
	document.write('<td class="iframe_div_action_button"><a href="#">Close</a></td>');
	document.write('<td class="iframe_div_action_button" style="border: black 1px solid"><a href="#">X</a></td>');
	document.write('</tr>');
	document.write('</table>');
	document.write('</div>');
	document.write('<div align="right" onclick="expandIframeDiv(this)" class="iframe_action_button_div">');
	document.write('<table border=0>');
	document.write('<tr>');
	document.write('<td class="iframe_div_action_button"><a href="#">' + expandLabel + '</a></td>');
	document.write('<td class="iframe_div_action_button" style="border: black 1px solid"><a href="#">+</a></td>');
	document.write('</tr>');
	document.write('</table>');
	document.write('</div>');
	document.write('<div style="display: none">');
	document.write('<iframe width="100%" height="100%" frameborder="0" src="' + iframeSource + '"></iframe>');
	document.write('</div>');
	document.write('</div>');
}

function openNewWindow(url) {
	var newwindow;
	newwindow = window.open(url, "mywindow", "location=0,resizable=1,status=1,scrollbars=yes");
	if (window.focus) {
		newwindow.focus();
	}
}

function checkMeta() { 
	var metas = document.getElementsByTagName('META'); 
	var i; 
	for (i = 0; i < metas.length; i++) {
		if (metas[i].getAttribute('NAME') == "Language") {
			break;
		}
	}
	return metas[i].getAttribute('CONTENT');
}  
function getSearch(language) {
	switch (language) {
		case "ENGLISH":
			return "search";
			break;
		case "CHINESE":
			return "&#36755;&#20837;&#20851;&#38190;&#35789;";
			break;
		default: return "search";
	}
}

function getGoButton(language) {
	switch (language) {
		case "ENGLISH":
			return "go";
			break;
		case "CHINESE":
			return "go_chinese";
			break;
		default: return "go";
	}
}

function copyright(language) {
	var dteNow=new Date();
	var intYear = dteNow.getFullYear();
	switch (language) {
		case "ENGLISH":
			return('Copyright &copy; The Dow Chemical Company (1995-' + intYear + '). All Rights Reserved.');
			break;
		case "CHINESE":
			return('Copyright &copy; The Dow Chemical Company (1995-' + intYear + ').&#38518;&#27663;&#21270;&#23398;&#20844;&#21496;&#29256;&#26435;&#25152;&#26377;.');
			break;
		default:
			return('Copyright &copy; The Dow Chemical Company (1995-' + intYear + '). All Rights Reserved.');
	}
}

function trademark(language) {
	switch (language) {
		case "ENGLISH":
			return('&reg;&trade;<sup>*</sup> Trademark of The Dow Chemical Company ("Dow") or an affiliated company of Dow');
			break;
		case "CHINESE":
			return('&reg;&trade;<sup>*</sup> <span lang="ZH-CN">&#38518;&#27663;&#21270;&#23398;&#20844;&#21496;&#25110;&#20854;&#38468;&#23646;&#20844;&#21496;&#30340;&#21830;&#26631;</span>');
			break;
		default:
			return('&reg;&trade;<sup>*</sup> Trademark of The Dow Chemical Company ("Dow") or an affiliated company of Dow');
	}
}

function setSearchHost(ServerName) {
	document.getElementById('host').value = window.location.hostname;
	var site = window.location.pathname.split('/');
	var sitename;
	if (site[1] == 'search.asp') {
		sitename = "";
	} else {
		sitename = site[1];
	}
	document.getElementById('site').value = sitename;
	var sitesearch = window.location.hostname;
	if (typeof ServerName == 'undefined') {
		if (sitesearch.match('www.dow.com') || sitesearch.match('www.dow-st')) {
			if (sitename == 'homepage' || sitename == 'gid'){
				document.getElementById('as_sitesearch').value = "www.dow.com";
			} else {
				document.getElementById('as_sitesearch').value = "www.dow.com/" + sitename;
			}
		} else if (sitesearch.match('www.dowagro.com') || sitesearch.match('www.dowagro-st')) {
			if (sitename == 'homepage') {
				document.getElementById('as_sitesearch').value = "www.dowagro.com";
			} else {
				document.getElementById('as_sitesearch').value = "www.dowagro.com/" + sitename;
			}
		} else {
			document.getElementById('as_sitesearch').value = sitesearch.replace("preview-","");
		}
	} else if (ServerName == 'building.dow.com') {
		document.getElementById('as_sitesearch').value= "building.dow.com/" + sitename;
	} else if (sitesearch.match('news.dow.com') || sitesearch.match('energy.dow.com') || sitesearch.match('reach.dow.com')) {
		document.getElementById('as_sitesearch').value= sitesearch.replace("preview-","");
	} else {
		document.getElementById('as_sitesearch').value = ServerName;
	}
}

function getUrlVar(requestedKey, sanitizeData) {
	if (typeof sanitizeData == 'undefined') {
		sanitizeData = true;
	}
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

	for (var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	if (typeof requestedKey == 'undefined') {
		return vars;
	} else {
		if (typeof vars[requestedKey] == 'undefined') {
			return '';
		} else {
			if (sanitizeData) {
				//TM replace - CM 10/22/10
				var temp = String(vars[requestedKey]);
				temp = temp.replace("%99", "");
				temp = temp.replace("%E2%84%A2", "");
				
				var lsRegExp = /\+/g;
				//Double Quote replace - CM 09/28/10
				clean_data = unescape(temp);
			    clean_data = clean_data.replace("\"", "");
			    clean_data = clean_data.replace("\"", "");
				//clean_data = unescape(String(vars[requestedKey]).replace("\"", " "));
				return clean_data;
			} else {
				return vars[requestedKey];
			}
		}
	}
}

/* 
 * This function draws an iframe which loads search results from our search servers.
 * Example usage:
 *	drawSearchIframe('MSDS Verdiseal', 'dowchem', 'live', '754px', 'building.dow.com');
 */
function drawSearchIframe(term, business, environment, width, host, corporate) {
	errorLog("Search Environment: " + environment, 'info');
	if (typeof width == 'undefined' || width == '') {
		width = '754px';
	}
	if (typeof corporate == 'undefined' || corporate == '') {
		corporate = false;
	}
	
	if (typeof host == 'undefined' || host == '') {
		searchHostString = ''
	} else {
		searchHostString = '&qp=' + host;
	}
	
	if (business == 'dowchem' && corporate) {
		if (environment == 'internal' || environment == 'staging') {
			searchStartURL = "http://extsearchqa.rohmhaas.com/wfSecureSearch.aspx";
		} else if (environment == 'dev') {
			searchStartURL = "http://brdevsearch3.rohmhaas.com/wfsecuresearch.aspx";
		} else if (environment == 'sandbox') {
			searchStartURL = "http://brdevsearch3.rohmhaas.com/sandbox/wfsecuresearch.aspx";
		} else {
			searchStartURL = "http://www.dow.com/searchresults/wfSecureSearch.aspx";
		}
	} else if (business == 'dowchem') {
		if (environment == 'internal' || environment == 'staging') {
			searchStartURL = "http://extsearchqa.rohmhaas.com/wfBasicSearch.aspx";
		} else {
			searchStartURL = "http://www.dow.com/searchresults/wfBasicSearch.aspx";
		}
	} else if (business == 'styron') {
		if (environment == 'internal' || environment == 'staging') {
			searchStartURL = "http://extsearchqa.rohmhaas.com/styron/wfBasicSearch.aspx";
		} else {
			searchStartURL = "http://www.styron.com/searchresults/styron/wfBasicSearch.aspx";
		}
	} else if (business == 'dowagro') {
		if (environment == 'internal' || environment == 'staging') {
			searchStartURL = "http://extsearchqa.rohmhaas.com/dowagro/wfBasicSearch.aspx";
		} else {
			searchStartURL = "http://www.dowagro.com/searchresults/dowagro/wfBasicSearch.aspx";
		}
	} else {
		errorLog("Undefined Business for Search: " + business, 'error');
		return false;
	}
	
	//might want to escape(term) here. Keep an eye on it for possible compatibility issues.
	searchFullURL = searchStartURL + '?qt=' + term + searchHostString;
	errorLog("Search URL: " + searchFullURL);
	document.write('<iframe id="ifSearch" style="WIDTH: ' + width + ';" scrolling="no" frameBorder="0" src="' + searchFullURL  + '"></iframe>');
}
