/* Warmteservice Javascript file */
/* Copyright quince 2007 */

function lib_bwcheck(){
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera=(navigator.userAgent.indexOf("Opera")>-1 && document.getElementById)?1:0
	this.opera6=(navigator.userAgent.indexOf("Opera 6")>-1 && this.opera)
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera)?1:0;
	this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera)?1:0;
	this.ie=(this.ie4||this.ie5||this.ie6||this.ie7)
	this.mac=(this.agent.indexOf("Mac")>-1)
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns7=(this.dom && parseInt(this.ver) >= 7) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.firefox=(navigator.userAgent.indexOf("Firefox")>-1 && document.getElementById)?1:0
	this.firefox1_0=(this.dom && !this.opera && !this.ie && navigator.userAgent.indexOf("Firefox/1.0")>-1) ?1:0;
	this.firefox1_5=(this.dom && !this.opera && !this.ie && navigator.userAgent.indexOf("Firefox/1.5")>-1) ?1:0;
	this.firefox2_0=(this.dom && !this.opera && !this.ie && navigator.userAgent.indexOf("Firefox/2.0")>-1) ?1:0;
	return this
}

browser = new lib_bwcheck();

function changecolor_to(e,highlightcolor,tagName) {
	if (!browser) return;

	source=browser.ie? event.srcElement : e.target
	if (source.tagName=="TABLE") return
	while(source.tagName!=tagName&&source.tagName!="HTML") {
		source=browser.ns6? source.parentNode : source.parentElement
	}
	if (source.style.backgroundColor!=highlightcolor&&source.id.substr(0,6)!="ignore") {
		source.style.backgroundColor=highlightcolor
		if (source.id.substr(0,4)=='swap') {
			//Additional swap image required, name is: source.id.substr(4,imagename.length-4)
			additionalimageswaps(source.id.substr(4,source.id.length-4));
		}
	}
}

function changecolor_back(e,originalcolor,tagName){
	if (!browser) return;

	if (browser.ie&&(event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id.substr(0,6)=="ignore")||source.tagName=="TABLE"){
		return
	}
	if (browser.ie&&event.toElement!=source||browser.ns6&&e.relatedTarget!=source){
		if (source.style.backgroundColor!=originalcolor&&source.id.substr(0,6)!="ignore") {
			source.style.backgroundColor=originalcolor;
			if (source.id.substr(0,4)=='swap') {
				//Reset swap image
				additionalimageswapsrestore(source.id.substr(4,source.id.length-4));
			}
		}
	}
}

function changeCssClass(obj, className) {
	if (obj && className) {
		obj.className = className;
	}
}

function changeBackgroundColor(obj, color) {
	if (obj && color) {
		obj.style.backgroundColor = color;
	}
}

function changeCursor(obj, cursor) {
	if (obj && cursor) {
		obj.style.cursor = cursor;
	}
}

function gotoPage(url) {
	if (url) {
		document.location.href = url;
	}
}

function copyFields(formName, srcFieldNames, destFieldNames) {
	var f = eval('document.forms.' + formName);
	if (null != f) {
		for (i = 0; i < srcFieldNames.length; i++) {
			srcField = eval('f.' + srcFieldNames[i]);
			destField = eval('f.' + destFieldNames[i]);
			if (null != srcField && srcField.value != null && null != destField) {
				destField.value = srcField.value;
			}
		}
	}
}

function delAttribute(avList, selList) {
	var availableList = document.getElementById(avList);
    var selectedList = document.getElementById(selList);
    if (selectedList.options.length > 1) {
	    var selIndex = selectedList.selectedIndex;
	    if (selIndex < 0) return;

	    availableList.appendChild(selectedList.options.item(selIndex));
	}
}

function delAllAttributes(avList, selList) {
	var availableList = document.getElementById(avList);
    var selectedList = document.getElementById(selList);

    var selIndex = selectedList.selectedIndex;
	if (selIndex < 0) return;

	for (var i = 0; i < selectedList.options.length; i++) {
	 if (!selectedList.options[i].selected) {
	  availableList.appendChild(selectedList.options[i]);
	  i--;
	 }
	}
}


function addAttribute(avList, selList) {
	var availableList = document.getElementById(avList);
    var selectedList = document.getElementById(selList);
    var addIndex = availableList.selectedIndex;
    if (addIndex < 0) return;

    selectedList.appendChild(availableList.options.item(addIndex));
}

function selectAll(list) {
	if (list) {
	    for (var i = 0; i < list.options.length; i++) {
			list.options[i].selected = true;
		}
	}
}

function showElement(id, displayMode) {
	var result = false;
	var obj = document.getElementById(id);
	if (obj) {
		obj.style.display = displayMode;
		result = true;
	}
	return result;
}

function hideElement(id, displayMode) {
	var result = false;
	var obj = document.getElementById(id);
	if (obj) {
		obj.style.display = displayMode;
		result = true;
	}
	return result;
}

function checkEnter(event, form) {
	var keycode = 0;
	keycode = event.keyCode;
	if (keycode == 13) {
		form.submit();
	}
}


function isEnterKey(event) {
	return event.keyCode == 13;
}

function setPage(page, formName) {
	var form = document.forms[formName];
	form.page.value = page;
	form.submit();
}

/*********************************
	Javasript for specific pages
**********************************/

// product.jsp
var chosenId = 'items';
function changeButton(obj) {
	if (obj) {
		var cssName = 'specialButton';
		var cursor = 'default';

		// set the proper values.
		if (obj.className && (obj.className == 'specialButton')) {
			cssName = 'specialButtonHover';
		}

		if (obj.style.cursor || (obj.style.cursor == 'default' || obj.style.cursor == '')) {
			cursor = 'pointer';
		}

		var id = obj.getAttribute('id');
		if (id && (chosenId == id.substr(0, id.indexOf('_tab')))) {
			cssName = 'chosenSpecialButton';
		}

		changeCssClass(obj, cssName);
		changeCursor(obj, cursor);
	}
}

function showTab(id, registerClick) {
	var item = document.getElementById('items');
	var attributes = document.getElementById('attributes');
	var brochures = document.getElementById('brochures');
	var links = document.getElementById('links');

	var itemTab = document.getElementById('items_tab');
	var attributesTab = document.getElementById('attributes_tab');
	var brochuresTab = document.getElementById('brochures_tab');
	var linksTab = document.getElementById('links_tab');

	item.style.display = 'none';
	attributes.style.display = 'none';
	brochures.style.display = 'none';
	links.style.display = 'none';
	
	changeCssClass(itemTab, itemTab.className);
	changeCssClass(attributesTab, attributesTab.className);
	changeCssClass(brochuresTab, brochuresTab.className);
	changeCssClass(linksTab, linksTab.className);

	var prevObj = document.getElementById((chosenId + '_tab'));
	if (prevObj) {
		changeCssClass(prevObj, 'specialButton');
	}

	var tabObj = document.getElementById((id + '_tab'));
	changeCssClass(tabObj, 'chosenSpecialButton');

	document.getElementById(id).style.display = 'block';
	chosenId = id;
	
	if (registerClick) {
		pageTracker._trackPageview(location.pathname + location.search + '/' + id);
	}
}

function filteronly() {
	var list = $('excludeDealers');
	selectAll(list);
	list = $('includeDealers');
	selectAll(list);
	$('myDataForm').filtermode.value = 'search';
	postForm();
}

function compareProducts(productSize) {
	var oneProductIsSelected = false;
	for (var i = 0; i < productSize; i++) {
		var id = 'c' + i;
		var obj = document.getElementById(id);
		if(obj){
		if (obj.checked == true) {
			oneProductIsSelected = true;
			break;
		}
	}
	}

	if (oneProductIsSelected) {
		document.getElementById('myDataForm').getAttributeNode('action').value = "productcompare.do";
		document.forms.myDataForm.submit();
	}
}

var counter = 1;
function checkCheckBox(number, message) {
	var id = ("c" + number);
	var obj = document.getElementById(id);
	if (obj) {
		if (obj.checked == true) {
			if (counter > 3) {
				obj.checked = false;
				var msgObj = document.getElementById(message);
				alert("U kunt maximaal 3 producten vergelijken");

			} else {
				counter++;
			}
		} else if (obj.checked == false) {
			if (counter > 0) {
				counter--;
			}
		}
	}
}

//subgroup.jsp
function changeText(id, msgIndex) {
	var label = $(id);
	if (label) {
		var fullText = texts[msgIndex];
		label.update(fullText);
	}
}

function clearText(id, text) {
	var label = document.getElementById(id);
	if (label) {
		//var myText = document.createTextNode(text);
		//label.replaceChild(myText, label.firstChild);
		label.innerHTML = text;
	}
}

// myData.jsp
function display(elementNames) {
	for (i = 0; i < elementNames.length; i++) {
		field = document.getElementById(elementNames[i]);
		if (null != field) {
			field.style.display = '';
		}
	}
}

function hide(elementNames) {
	for (i = 0; i < elementNames.length; i++) {
		field = document.getElementById(elementNames[i]);
		if (null != field) {
			field.style.display = 'none';
		}
	}
}

// order-login.jsp
var isHidden = false;
function register() {
	if (isHidden) {
		showRegistrationElement('registration');
	} else {
		hideRegistrationElement('registration');
	}
}

function showRegistrationElement(id) {
	var shown = showElement(id, 'block');
	if (shown) {
		isHidden = false;
	}
}

function hideRegistrationElement(id) {
	var hidden = hideElement(id, 'none');
	if (hidden) {
		isHidden = true;
	}
}

function postForm() {
	if (isHidden) {
		document.forms.loginForm.action.value='login';
		document.forms.loginForm.submit();
	} else {
		document.forms.myDataForm.action.value='register';
		document.forms.myDataForm.submit();
	}
}

function checkEnterOrderLogin(event) {
	var keycode = 0;
	keycode = event.keyCode;
	if (keycode == 13) {
		postForm();
	}
}
//titlebar.jsp
var chosenTab = '<bean:write name="chosen" ignore="true"/>';
var http_request = null;
/**
 * @return a new XMLHttp object... gets it in an IE/Mozilla friendly way.
 */
function getXMLHTTP(){

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType("text/xml");
                // See note below about this line
        }
    } else {
        if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {
                }
            }
        }
    }
    return http_request;
}
/**
 * Make a call to the server, without refreshing the current page
 *
 * @param url 		Url to make the http call to.
 * @param callback 	Function to delegate controll to when the callback
 *					yields it's result (optionally, required if sync=false)
 * @param sync 		Indicator for synchronously. <pre>true</pre> for sync call,
 *					<pre>false</pre> for async call. Defaults to false.
 * @return			Response XML if sync call was made, void otherwise.
 */
function makeRequest(url, callback, sync) {
	try {
		var http_request = getXMLHTTP();
	    if (!http_request) {
	        window.status = "Giving up :( Cannot create an XMLHTTP instance";
	        return false;
	    }
	    /**
	     * Prevent IE or WebCACHE caching, append dummy variable.
	     */
	    if( url.indexOf('?') == -1 ){
	    	url += "?cache=" + new Date().getTime();
	    } else {
		    url += "&cache=" + new Date().getTime();
		}
	    /**
	     * Test if sync parameter is set, and if it's value equals true
	     */
	    if (sync && sync === true) {
	        http_request.open("GET", url, false);
	        http_request.send(null);
	        if( callback ){
		        callback(http_request);
		    }
	        return http_request.responseXML;
	    } else {
	        http_request.onreadystatechange = function () {
	            reportContents(http_request, callback);
	        };
	        http_request.open("GET", url, true);
	        http_request.send(null);
	    }
    } catch (e) {}
}

function showHeaderTab(id) {
	if(chosenTab == null){
		chosenTab = 'webshop';
	}
	var tabObj = document.getElementById((chosenTab + '_tab'));
	chosenTab = id;
	makeRequest('servlet/TabHandler?chosenTab=' + chosenTab, function () {}, true);
}

function showOnly(className, idPrefix, idToShow) {
	var allElements = document.getElementsByClassName(className);
	allElements.each( function(e) {
		e.id == idPrefix + idToShow ? e.show() : e.hide();
	});
}

function toggleProductGroupText() {
	var count = 0;
	var e = $('productText')
	if (e = e.down()) {
		if (e = e.next('p')) {
			if (e = e.next('p')) {
				if (e = e.next()) {
					while (e != undefined && e.id != 'product-text-gradient') {
							count++;
							if (e.style.display == 'none') {
								e.style.display = 'block';	
							} else {
								e.style.display = 'none';
							}
						e = e.next();
					}
				}
			}
		}
	}
	e = $('more-info-button');
	if (e.style.display == 'none' && count > 0) {
		e.style.display = 'block';	
	} else {
		e.style.display = 'none';
	}
	
}