/************************************************
file:	honeywell.js
author:	Richard Schmid
************************************************/

// ----------------------------------------------
// function:  getObjectByID
// ----------------------------------------------
function getObjectByID (oDocument, sID) {
	if (oDocument.all) {
		return oDocument.all[sID];
	}
	else if (oDocument.getElementById) {
		return oDocument.getElementById(sID);
	}
	else if (oDocument.layers) {
		return oDocument.layers[sID];
	}
}

function saveCookie(oDoc, sName, sValue, sSep, iExpires) {
	var dActDate = new Date();
	var dExpDate = new Date(dActDate.getTime()+iExpires);
	var dDelDate = new Date(dActDate.getTime()-1000);
	var sRemainingVal = sValue;
	if (!sValue) return;
	if (sValue.length > 3500) {
	}
	for (var i = 0; i < 20; i++) {
		var sActValue = '';
		if (sRemainingVal.length > 1800) {
			var iSepIdx = sRemainingVal.lastIndexOf(sSep, 1500);
			if (iSepIdx > 0) {
				sActValue = sRemainingVal.substring(0, iSepIdx);
				sRemainingVal = sRemainingVal.substring(iSepIdx);
			}
			else {
				sActValue = sRemainingVal;
				sRemainingVal = '';
			}
		}
		else {
			sActValue = sRemainingVal;
			sRemainingVal = '';
		}
		var sActName = sName+'_'+i;
		if (sActValue != '') {
			oDoc.cookie = sActName+'='+escape(sActValue)+'; expires='+dExpDate.toGMTString()+'; path=' + escape('/') + ';';
		}
		else {
			oDoc.cookie = sActName+'='+escape(sActValue)+'; expires='+dDelDate.toGMTString()+'; path=' + escape('/') + ';';
		}
	}
}

function readCookie(oDoc, sName) {
	try {
		var sResult = '';
		for (var i = 0; i < 20; i++) {
			var sActName = sName + '_' + i;
			var aResults = oDoc.cookie.match ( sActName + '=(.*?)(;|$)' );
			if ( aResults )
				sResult += unescape(aResults[1]);
		}
		return sResult;
	}
	catch(e) {
	}
}

function getDirectoryName(sURL_) {
	var iSlashPos = sURL_.lastIndexOf('/');
	var iBSlashPos = sURL_.lastIndexOf('\\');
	if (iBSlashPos > iSlashPos) iSlashPos = iBSlashPos;
	
	if (iSlashPos == -1) {
		return "/";
	}
	else {
		var sDirectoryName = sURL_.substr(0, iSlashPos+1);
		return sDirectoryName;
	}
}

var HTMLFILEEXTENSION		= ".html";
var HTMLDOCTYPE				= "<html>\n<head>\n<meta http-equiv='Content-Type' content='text/html; charset=Windows-1251'>";
var HTMLSTYLESHEET			= "<link rel='stylesheet' href='styles/content.css' type='text/css'></head>";
var HTMLEND					= "</body></html>";
var BASEPATH				= getDirectoryName(top.location.href);
var TOCPATH					= BASEPATH + "tocs/";
var DOCPATH					= BASEPATH + "ecatdata/";
var PDFPATH					= BASEPATH + "pdf/";

var gToc					= null;
var gDoc					= null;
var gOSNo					= null;
var gBrowser				= null;
var gPdfWinCD				= null;
var goCatWin 				= null;
var goPopupWin				= null;

function _getOpenOptions(sURL, sWidth, sHeight) {
	//var sOptions = 'Toolbar=1,Location=1,Directories=1,Status=1,Menubar=1,scrollbars=1,resizable=1,left=0,top=0,width=';
	var sOptions = 'Toolbar=0,Location=0,Directories=0,Status=0,Menubar=0,scrollbars=1,resizable=1,left=0,top=0,width=';
	if (sWidth) {
		if ((sURL == '') || (sURL.indexOf('gr_') != -1)) {
			sOptions = 'Toolbar=0,Location=0,Directories=0,Status=0,Menubar=0,scrollbars=1,resizable=1,left=0,top=0,width=';
		}
		else {
			sOptions = 'Toolbar=1,Location=1,Directories=0,Status=1,Menubar=0,scrollbars=1,resizable=1,left=0,top=0,width=';
		}
		sOptions += sWidth;
	}
	else {
		if (screen.availWidth)
			sOptions += screen.availWidth;
		else
			sOptions += '800';
	}
	
	sOptions += ',height=';
	if (sHeight) {
		sOptions += sHeight;
	}
	else {
		if (screen.availHeight)
			sOptions += (screen.availHeight-150);
		else
			sOptions += '600';
	}
	return sOptions;
}

function openCatWin(sURL, sWidth, sHeight) {
	if (goCatWin) {
		if (!goCatWin.closed)
			goCatWin.close();
	}	
	var sOptions = _getOpenOptions(sURL, sWidth, sHeight);
	goCatWin = window.open(sURL, 'catWin', sOptions);
	goCatWin.focus();
}

function openPopupWin(sURL, sWidth, sHeight) {
	var bOpen = true;
	if (goPopupWin) {
		if (sURL.indexOf('summary') <= 0) {
			if (!goPopupWin.closed)
				goPopupWin.close();
		}
		else {
			if (!goPopupWin.closed) bOpen = false;
		}
	}
	if (bOpen) {
		var sOptions = _getOpenOptions(sURL, sWidth, sHeight);
		goPopupWin = window.open(sURL, 'popupWin', sOptions);
	}
	if (sURL.indexOf('summary') <= 0) {
		goPopupWin.focus();
	}
}

function VAPS_Summary1(sValveTypeNo, iActuatorDataIdx) {
	var wPSDefWin = parent.frames['HON_DEF'];
	var wASDefWin = parent.frames['HON_AS_DEF'];
	if (!wPSDefWin || !wASDefWin)
		return false;
	var nWidth = 600;
	if (screen.availWidth)
		nWidth = screen.availWidth;
	var nHeight = 200;
	if (goPopupWin) {
		try { 
			if (!goPopupWin.document) parent.openPopupWin('../js/summary.html', nWidth, nHeight);
		}
		catch(e) {
			parent.openPopupWin('../js/summary.html', nWidth, nHeight);
		}
	}
	else {
		parent.openPopupWin('../js/summary.html', nWidth, nHeight);
	}
	if (!goPopupWin)
		return false;
	var oTo = self.setTimeout(function() {
	    self.clearTimeout(oTo);
	    parent.frames['HON_DEF']._VAPS_Summary1(parent.frames['HON_DEF'], parent.frames['HON_AS_DEF'], goPopupWin, sValveTypeNo, iActuatorDataIdx);
	}, 600);
}

function gotoCatURL(sFrame, sCatURL){
	if (sFrame == "_top") {
		top.location.href = BASEPATH + sCatURL;
	}
	else {
		top.frames[sFrame].location.href = BASEPATH + sCatURL;
	}
}

/* 2007 */
function func_onload() {
	focusOSNo(document);
}
//Moz
if (window.addEventListener) {
	window.addEventListener('load', func_onload, false);
}
//IE
else if (window.attachEvent) {
	window.attachEvent('onload', func_onload);
}
/* 2007 */


function focusOSNo(oDocument) {
	var sLocStr = top.location.href;
	var iParamPos = sLocStr.indexOf("?");
	if (iParamPos >= 0) {
		var sParamStr = sLocStr.substring(iParamPos + 1, sLocStr.length);
		var aParams = new Array();
		var iAIdx = 0;
		
		var sNVSep = "=";
		while (sParamStr.indexOf(sNVSep) >= 0) {
			var sParamSep = "&";
			var iEndActParamIdx = 0;
			if (sParamStr.indexOf(sParamSep) >= 0) {
				iEndActParamIdx = sParamStr.indexOf(sParamSep);
			}
			else {
				iEndActParamIdx = sParamStr.length;
			}
			
			var sActParam = sParamStr;
			if (iEndActParamIdx != sParamStr.length) {
				sActParam = sParamStr.substring(0, iEndActParamIdx);
			}
			
			var iNVSep = sActParam.indexOf(sNVSep);
			var sActParamName = sActParam.substring(0, iNVSep);
			var sActParamValue = sActParam.substring(iNVSep + sNVSep.length, sActParam.length);
			if ((sActParamName != "") && (sActParamValue != "")) {
				aParams[iAIdx] = new Array(sActParamName, sActParamValue);
				iAIdx++;
			}
			
			sParamStr = sParamStr.substring(iEndActParamIdx + sParamSep.length, sParamStr.length);
		}
		
		var sOSNr = "";
		for (iAIdx = 0; iAIdx < aParams.length; iAIdx++) {
			if (aParams[iAIdx][0].toUpperCase() == "OSNO") {
				sOSNr = aParams[iAIdx][1].replace("%20","_");
				while (sOSNr.indexOf("%20") >= 0) {
					sOSNr = sOSNr.replace("%20","_");
				}
				while (sOSNr.indexOf(" ") >= 0) {
					sOSNr = sOSNr.replace(" ", "_");
				}
			}
		}
		
		if (sOSNr != "") {
			if (oDocument.all) {
				var oProduct = oDocument.all[sOSNr];
				if (oProduct) {
					oProduct.style.backgroundColor = "#CCCCCC";
					oProduct.scrollIntoView(true);
				}
			}
			else if (oDocument.getElementById) {
				var oProduct = oDocument.getElementById(sOSNr);
				if (oProduct) {
					oProduct.style.backgroundColor = "#CCCCCC";
					oProduct.scrollIntoView(true);
				}
			}
		}
	}
}

function hFireLink(oDocument) {
	var sLocStr = oDocument.location.href;
	var iParamPos = sLocStr.indexOf("?");
	if (iParamPos >= 0) {
		var sParamStr = sLocStr.substring(iParamPos + 1, sLocStr.length);
		var aParams = new Array();
		var iAIdx = 0;
		//alert(sParamStr);
		
		var sNVSep = "=";
		while (sParamStr.indexOf(sNVSep) >= 0) {
			var sParamSep = "&";
			var iEndActParamIdx = 0;
			if (sParamStr.indexOf(sParamSep) >= 0) {
				iEndActParamIdx = sParamStr.indexOf(sParamSep);
			}
			else {
				iEndActParamIdx = sParamStr.length;
			}
			
			var sActParam = sParamStr;
			if (iEndActParamIdx != sParamStr.length) {
				sActParam = sParamStr.substring(0, iEndActParamIdx);
			}
			
			var iNVSep = sActParam.indexOf(sNVSep);
			var sActParamName = sActParam.substring(0, iNVSep);
			var sActParamValue = sActParam.substring(iNVSep + sNVSep.length, sActParam.length);
			if ((sActParamName != "") && (sActParamValue != "")) {
				aParams[iAIdx] = new Array(sActParamName, sActParamValue);
				iAIdx++;
			}
			
			sParamStr = sParamStr.substring(iEndActParamIdx + sParamSep.length, sParamStr.length);
		}
		
		//check for toc, group OR marketing-param
		var sTocID = "";
		var sGroupID = "";
		var sMInfID = "";
		var sShowToc = "";
		var sOSNr = "";
		
		for (iAIdx = 0; iAIdx < aParams.length; iAIdx++) {
			if (aParams[iAIdx][0].toUpperCase() == "TOC") {
				sTocID = aParams[iAIdx][1].toLowerCase();
			}
			else if (aParams[iAIdx][0].toUpperCase() == "GROUP") {
				sGroupID = aParams[iAIdx][1].toLowerCase();
			}
			else if (aParams[iAIdx][0].toUpperCase() == "MARKETING") {
				sMInfID = aParams[iAIdx][1].toLowerCase();
			}
			else if (aParams[iAIdx][0].toUpperCase() == "SHOWTOC") {
				sShowToc = aParams[iAIdx][1];
			}
			else if (aParams[iAIdx][0].toUpperCase() == "OSNO") {
				sOSNr = aParams[iAIdx][1];
			}
		}
		
		//(tocid AND ShowToc) OR (groupid [osno] AND ShowToc)
		if ((sTocID != "") && (sShowToc != "0")) {
			//marketing-info, lookup sMInfID
			top.location.href = BASEPATH + top.gsRelPath + "frames/f_" + sTocID + HTMLFILEEXTENSION;
		}
		else if (sGroupID != "") {
			var sOSNrParam = "";
			var iOSNrLink = 0;
			if (sOSNr != "") {
				iOSNrLink = 1;
				sOSNrParam = "?OSNO=" + sOSNr;
			}
							
			if (sShowToc == "1") {
				top.location.href = BASEPATH + top.gsRelPath + "frames/f_" + sGroupID + HTMLFILEEXTENSION + sOSNrParam;
			}
			else {
				//ecatdata loads the frameset - so: it's wrong
				//docs is Group-Only
				top.location.href = BASEPATH + top.gsRelPath + "docs/" + sGroupID + HTMLFILEEXTENSION + sOSNrParam;
			}
		}
		else if (sOSNr != "") {
			gOSNo = sOSNr;
		}
	}
}