/*
	COPYRIGHT 1996, 2009 JAMES H. ZISCH. ALL RIGHTS RESERVED.
	CONTACT:  James H. Zisch - Computer Services (JHZ-CS) http://www.jhz-cs.com/
*/

function init() {
	try {
		if (msie && !msieOK) return false;
		sector=getSector();
		pageComponentsInit();
		document.getElementById(main).focus();
	}
	catch (err) { errHandler(err,'init'); }
}

function getSector() {
	try {
		var x=top.location.href;
		for(var y in sectors) {
			if (sectors[y].search(/search|map/) != -1) continue;
			var t='/'+sectors[y]+'/';
			if (x.search(t) != -1) return sectors[y];
		}
		if (x.search('/search/') != -1) return 'search';
		if (x.search('/contact/') != -1) return about;
		if (x.search('/assists/') != -1) return support;
		if (x.search('/demos/') != -1) return solutions;
		if (x.search('/solutions/order/') != -1) return solutions;
		if (x.search('/guides/') != -1) return support;
		return none;
	}
	catch (err) { errHandler(err,'getSector'); }
	return false;
}

function pageComponentsInit() {
	try {
		if (solutionsNav) solutionsInit();
		if (guidesNav) guidesInit();
		buttonsInit();
		bannerInit();
		mapInit();
		orderInit();
	}
	catch (err) { errHandler(err,'pageComponentsInit'); }
}

function orderInit() {
	try {
		var f=(self.document.getElementById(orderframe))?self.document.getElementById(orderframe):parent.document.getElementById(orderframe);
		var d=(f.contentDocument)?f.contentDocument:f.contentWindow.document;
		var r=d.getElementById(orderPane).innerHTML;
		document.getElementById(orderPane).innerHTML=r;
		var c=cookieArray(orderPane);
		if (c.length > 0) document.getElementById(orderPane).style.display=c[0];
		with (document.getElementById(orderPane).style) {
			if (document.getElementById(tnL_order) )
				document.getElementById(tnL_order).innerHTML=(display == orderPaneDispAttr)?orderHide:orderShow;
			if (!navigator.cookieEnabled) alert(enableCookiesMsg);
			document.cookie=orderPane+'='+display+'; path=/';
		}
		hasCurrentOrder();
	}
	catch (err) { errHandler(err,'orderInit'); }
}

function hasCurrentOrder() {
	try {
		hasOrder=(cookieJar('cartid') > 0)?true:false;
		if (document.getElementById(order)) with (document.getElementById(order).style) display=(hasOrder)?tblCellDisplay:none;
		with (document.getElementById(orderPane).style) {
			display=(hasOrder)?display:none;
			document.cookie=orderPane+'='+display+'; path=/';
		}
	}
	catch (err) { errHandler(err,'hasCurrentOrder'); }
	return hasOrder;
}

function colorsInit() {
// not used: keep as example
	with (getCSSBySelector('#PgC').style) {
		var s=document.getElementById('search_string').style;
		s.color=color;
		s.backgroundColor=backgroundColor;
	}
	with (getCSSBySelector('#page_header').style) {
		var s=document.getElementById('ft').style;
		s.color=color;
		s.backgroundColor=backgroundColor;
	}
}

function cookieArray(x) {
//	x - cookie to find; returns array of values of x if found; return new array if not found
	var y=cookieJar(x);
	if (y) var z=y.split(',');
	if (z) return (z);
	return (new Array());
}

function cookieJar(x) {
//	x - cookie to find, returns value if found, false if not found
	var c=document.cookie.split('; ');
	for (var i in c) if (c[i].split('=')[0] == x) return (c[i].split('=')[1]);
	return false;
}

function cookieString(x) {
//	x - an array of cookie values ^ delimited; returns formatted string
	var y=x.join(',');
	return (y.replace(/^,/,''));
}

function cookieExpiration(days) {
//	document.cookie = "name=value; path=/; expires=" + exp.toGMTString();
	var e=new Date();
	var msecs=24*60*60*1000;
	var h=e.getTime()+(days*msecs);
	e.setTime(h);
	return (e.toGMTString());
}

function orderRefresh() {
	try {
		parent.document.getElementById(orderframe).src='/solutions/order/orderSM.cgi'; //	loads order into frame
		var f=parent.document.getElementById(orderframe);
		var d=(f.contentWindow)?f.contentWindow.document:f.contentDocument;
		var s=d.getElementById(orderPane).innerHTML;
		document.getElementById(orderPane).innerHTML=s;
		if (document.getElementById(orderPane).style.display != orderPaneDispAttr) buttonsOrderToggle();
		hasCurrentOrder();
	}
	catch (err) { errHandler(err,'orderRefresh'); }
}

function getCssRuleByID(s) {
//	s - CSS defined #id w/o '#'
	s='^#'+s.toUpperCase()+'$';	// exclusive starting position 0
	return getCSSSelector(s);
}

function getCSSBySelector(s) {
	//	s - a CSS defined #id w/ #
	s='^'+s.toUpperCase();	// '^' inclusive starting position 0
	return getCSSSelector(s);
}

function getCSSSelector(s) {
	try {
		var sheets = top.document.styleSheets;
		for(var si=0; si<sheets.length; si++) {
			var rules=(sheets[si].rules)?sheets[si].rules:sheets[si].cssRules;
			for(var ri=0; ri<rules.length; ri++) {
				if (!rules[ri].selectorText) continue;
				if (rules[ri].selectorText.toUpperCase().search(s) != -1)
					return rules[ri]; // returns first occurence
			}
		}
	}
	catch (err) { errHandler(err,'getCSSSelector'); }
	return false;
}

function hexToRGB(n) {
//	returns rgb string; n - hexdecimal RGB color
	try {
		n = n.replace(/#/, '');
		if (n.length == 3) n=n.substring(0,1)+n.substring(0,1)+n.substring(1,2)+n.substring(1,2)+n.substring(2,3)+n.substring(2,3);
		return 'rgb('+hexToDec(n.substring(0,2))+','+hexToDec(n.substring(2,4))+','+hexToDec(n.substring(4,6))+')';
	}
	catch(err) { errHandler(err,'hexToRGB'); }
}

function hexToDec(n) {
//	n - hexadecimal number; returns decimal value
	try {
		hex={ '0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'A':10,'B':11,'C':12,'D':13,'E':14,'F':15};
		var n1=hex[n.toUpperCase().substring(0,1)];
		if (n1 > 0) n1=(n1*15)+15;
		var n2=hex[n.toUpperCase().substring(1,2)];
		return (n1+n2);
	}
	catch (err) { errHandler(err,'hexToDec'); }
}

function validateEmail(x) {
	try {
		var ok=true;
		if (x=='') ok=false;	// none
		if (x.indexOf('@') == -1 ) ok=false;	// no @ sign
		if (x.split('@')[0].length < 3) ok=false; // invalid userid
		if (x.split('@')[1].indexOf('.') == -1) ok=false;	// no '.' in domain
		if (x.split('@')[1].split('.')[0] == '') ok=false; // no domain
		if (x.split('@')[1].split('.')[1] == '' || x.split('@')[1].split('.')[1].length < 3) ok=false;	// invalid domain qualifier
		return ok;
	}
	catch (err) { errHandler(err,'validate_email'); }
	return false;
}

function errHandler(err,func) {
	if (!debug) return;
	var emsg='ERROR: '+func+'() : '+err.message;
	alert(emsg);
}

var	debug=(top.location.href.search(/jhz-cs.com/)==-1)?true:false;

var	wsSuspend=new Object();
	wsSuspend.enabled=false,
	wsSuspend.Test='/jhz-cs.com/',
	wsSuspend.RedirFreq=1,
	wsSuspend.RedirURL='/errors/suspend.shtml';
	wsSuspend.RedirCode='<meta http-equiv="refresh" content="'+wsSuspend.RedirFreq+';URL='+wsSuspend.RedirURL+'">';
if (wsSuspend.enabled && top.location.href.search(wsSuspend.Test) == -1) 
	document.write(wsSuspend.RedirCode);

var hasOrder=(cookieJar('cartid') > 0)?true:false;
var ancID='anc';

var	msie=(navigator.appVersion.search(/MSIE/i) > -1)?true:false;
var	msieOK=(navigator.appVersion.search(/MSIE 5/i) > -1)?false:true;
var browserCompliant=(!msie || (msie && msieOK))?true:false;

var post_content='';
var sector, sectors=['about','services','solutions','support','order','search'];
var	guidesNav=false,solutionsNav=false;
var tblCellDisplay=(msie)?'block':'table-cell';
var enableCookiesMsg='Browser cookies must be enabled to support this feature. Please enable browser cookies, then try again.';

var orderPaneDispAttr='inline';

var services='services', main='main', about='about', support='support', solutions='solutions', none='none', srch='srch';

var orderframe='orderframe', orderPane='orderPane', tnL_order='tnL_order', order='order', orderShow='SHOW ORDER', orderHide='HIDE ORDER';
