function $ (obj) {
	return (typeof obj == 'string')? document.getElementById(obj): obj;
}
function hide (obj) {
	if (el = $(obj)) el.style.display = 'none';
}
function show (obj) {
	if (el = $(obj)) {
		if (CurrObj && el != CurrObj) CurrObj.style.display = 'none';
		if (CurrTimeout) CurrTimeout = window.clearTimeout(CurrTimeout);
		(CurrObj = el).style.display = '';
	}
}
function down () {
	CurrTimeout = window.setTimeout("hide(CurrObj); CurrObj = null;", 500);
}
var CurrObj = null;
var CurrTimeout = null;

function checkBrowser () {
	if (window.XMLHttpRequest) return true;
	if (window.ActiveXObject) return true;
	return false;
}
function getId (str) {
	if (typeof str == 'object') return str; // Already an object
	if (document.getElementById) return document.getElementById(str);
	if (document.all) return document.all[str];
	return null; // Not supporting older methods of getting ID
}
function getQuery () {
	var query = document.location.search.substring(1);
	var vars = query.split("&");
  for (var i=0; i<vars.length; i++) {
		var pair = vars[i].split("=");
		this[pair[0]] = pair[1];
  }
}
function setQuery (data) {
	var tmp = [];
	for (var key in data) tmp.push(key + '=' + escape(data[key]));
	return tmp.join('&');
}
function getHTML (url, obj) {
	var el = getId (obj);
	if (el) getAsync (url, function (req) {el.innerHTML = req.responseText});
}
function getXML (url, obj) {
	var el = getId (obj);
	if (el) getAsync (url, function (req) {if (req.responseXML) el = req.responseXML.documentElement});
}
function getAsync (url, cb) {doAsync ('GET', url, null, cb)}
function postAsync (url, data, cb) {doAsync ('POST', url, data, cb)}
function doAsync (mode, url, data, cb) {
	var req = (window.XMLHttpRequest)? new XMLHttpRequest(): (window.ActiveXObject)?
		new ActiveXObject("Microsoft.XMLHTTP"): null;
	if (req) {
		req.onreadystatechange = function () {
			if (req.readyState == 4) {
				switch (req.status) {
				case 200:	if (cb) cb (req); break;
				case 205:	if (window.onload) window.onload; break;
				case 401: if (window.onload) window.onload; else window.location.reload(true); break;
				default:	window.status = "Error getting page: " + req.status;
				}
			} 
		}
		req.open (mode, url, true);
		if (mode == 'POST') req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		req.send (data);
	} 
}
function submitFormHTML (form, obj) {
	var el = getId (obj);
	if (el) submitForm (form, function (req) {el.innerHTML = req.responseText});
	return false;
}
function submitForm (form, cb) {
	if (form) {
		var method = form.method.toUpperCase(), action = form.action, data = "";
		var els = form.elements
		for (var i=0; i < els.length; i++) {
			if (els[i].type != 'radio' || els[i].checked) {
				data += ((i>0)? '&': '') + els[i].name + '=' + escape(els[i].value);
			}
		}
		if (method == 'GET') {
			action += ((action.indexOf('?') > 0)? '&': '?') + data; data = null;
		}
		doAsync (method, action, data, cb)
	}
	return false;
}
function setProperty (obj, prop, value) {
	var el = getId (obj);
	if (el) el[prop] = value;
}
function open_smartlink (url) {
	window.open (url, "Smartlink");
}
function popupImage (img) {
	window.open ('/app/popup_image.asp?img='+img, 'image', 'width=100,height=100,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no');
}
function open_pricing () {
	window.open('/brochure_pricing.asp?dealer=<%=Dealer.Code%>&brochure=<%=Brochure.ID%>','pricing',
  'height=330,width=400,resizeable=no,toolbar=no,location=no,status=no,scrollbars=yes');
}
function quote_request_form (obj, product) {
	if (!product) product = '';
	var data = ['mode=form', 'element=' + obj, 'product=' + encodeURIComponent(product)];
	getHTML('/ajax/request_quote.asp?' + data.join('&'), obj);
}
function quote_request_popup (product) {
	if (!product) product = '';
	var data = ['mode=popup', 'element=lightbox_content', 'product=' + encodeURIComponent(product)];
	open_lightbox('/ajax/request_quote.asp?' + data.join('&'));
}
function open_lightbox (url) {
	if (!document.getElementById('lightbox_overlay')) {
		var div = document.createElement('div');
		div.id = 'lightbox_overlay'; div.className = 'lightbox_overlay';
		document.body.appendChild(div);
	}
	if (!document.getElementById('lightbox_content')) {
		var div = document.createElement('div');
		div.id = 'lightbox_content'; div.className = 'lightbox_content';
		document.body.appendChild(div);
	}
	$('lightbox_overlay').style.display = 'block'; $('lightbox_content').style.display = 'block';
	if (url) getHTML(url,'lightbox_content');
}
function close_lightbox () {
	$('lightbox_overlay').style.display = ''; $('lightbox_content').style.display = '';
}
function log_traffic (data) {
	postAsync('/ajax/log_traffic.asp', setQuery(data));
}
