﻿/* Common functions */
String.prototype.trim = function() {
	/* Use a regular expression to replace leading and trailing spaces with the empty string */
	return this.replace(/(^\s*)|(\s*$)/g, '');
}
	
function getpagelang() {
	/* Returns the page's language */
	var ln = getmeta('content-language');
	if (ln.length!=2) ln='en';	//default to english;
	return ln;
}

function getsection() {
	/* Returns the page's section */
	return getmeta('section');
}

function getsubsection() {
	/* Returns the page's section */
	return getmeta('subsection');
}

 function getmeta(sName) {
 	/* Returns the value of a meta-tag (from the content attribute). This function will examine .name and .httpEquiv attributes for a match */
	var metas = document.getElementsByTagName('meta');
	for (var i = 0; i < metas.length; i++)
		if ((metas[i].httpEquiv == sName) || (metas[i].name == sName))
		return metas[i].getAttribute('content');
	return '';
} 

function getbaseurl() {
	/* Returns the base url with the language included ( /<lang> ) */
	return '/' + getpagelang();
}

function clearsearch(oTxt) {
	/* Clears the searchboxes on focus */
	if (oTxt.value.substring(oTxt.value.length-1)=='#') oTxt.value='';
}

function showhelp(sSection, sKey, iWidth) {
	/* Shows an (ajax) help form */
	if (!iWidth) iWidth = 400;
	return showpopupform(getPostParams('common', 'gethelp', 'helpsection=' + sSection + '&key=' + sKey),iWidth);
}

function showforgotpass() {
	/* Shows an (ajax) help form */
	return showpopupform(getPostParams('common', 'forgotpass'),400);
}

function showpopupform(url, width, popuphandler) {
	/* Shows an (ajax) 'pop-up' form */
	var iForm = 0, sPrefix = 'pf';
	while (document.getElementById(sPrefix + iForm)) iForm++;
	oDiv = createDiv(sPrefix + iForm,'popupform');
	url+= '&formid=' + oDiv.id;
	if (!width) width = 400; oDiv.style.width = width + 'px';
	oDiv.style.left = (((document.body.clientWidth - width) / 2) + (iForm * 20)) + 'px';
	oDiv.style.top = (getScrollY() + 120 + (iForm * 20)) + 'px';
	document.body.appendChild(oDiv);
	retrieveXml(url, doshowpopupform, popuphandler);
	return false;
}
  
function closepopupform(sFormID) {
	/* closes an (ajax) 'pop-up' form */
	var oDiv = document.getElementById(sFormID);	
	if (oDiv) document.body.removeChild(oDiv);
	return false;
}

function doshowpopupform(oResponseXML, handler) {
	/* handles incoming XML containing the HTML for an (ajax) 'pop-up' form */
	if (isValidResponse(oResponseXML)) {
		var oExec = oResponseXML.getAttribute('execute');
		if(oExec) {	//Do not display a pop-up, rather execute a function
			var arrParams = new Array(), i=1;
			while (oResponseXML.getAttribute('param'+i)) {
				arrParams.push(oResponseXML.getAttribute('param'+i));
				i++;
			}
			this[oExec].apply(this,arrParams);
		} else {
			var oDiv = document.getElementById(oResponseXML.getAttribute('formid'));	
			if (oDiv) {
				oDiv.innerHTML = oResponseXML.firstChild.data;
				oDiv.style.display = 'block';
				//wmaxz++; oDiv.style.zIndex = wmaxz; // ensure on top (currently disabled because the pop-up calendar doesn't support z-index which makes it effectively a pop-under calendar ;-)
			}
			if (handler) handler();
		}
	} else displayError(oResponseXML);
}

function getScrollY() {
	/* returns the document's y offset (scroll-position) */
	if (document.all) {
		return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	} else {
		return window.pageYOffset;
	}
}

function toggleElement(sID) {
	var oE = document.getElementById(sID);
	if (oE) oE.style.display = (oE.style.display != 'block') ? 'block' : 'none';
	return false;
}

function endisable_button(sID, bEnabled) {
	var oB = document.getElementById('ic' + sID);
	if (oB) {
		var oS = oB.src;
		var sClass='';
		if (bEnabled) {
			if (oS.indexOf('_dis.gif')>=0) oS = oS.replace('_dis.gif','.gif');
			sClass='';
		} else {
			if (oS.indexOf('_dis.gif')<0) oS = oS.replace('.gif','_dis.gif');
			sClass='disabled';
		}
		oB.src = oS;
		oB.parentNode.className=sClass;
	}
}

function getformstring(oForm, bGetEmpty) {
	var s = '';
	for (i=0; i<oForm.elements.length; i++) {
		var oElem = oForm.elements[i];
		switch (oElem.tagName.toLowerCase()) {
			case 'input':
				switch (oElem.type) {
					case 'text':
					case 'hidden':
						if ((oElem.value!='') || bGetEmpty) s += getpostvalue(oElem, oElem.value, s.length>0);
						break;
					case 'password':
						if ((oElem.value!='') || bGetEmpty) s += getpostvalue(oElem, hex_md5(oElem.value), s.length>0);
						break;
					case 'checkbox':
					case 'radio':
						if (oElem.checked) s += getpostvalue(oElem, oElem.value, s.length>0);
						break;
				}
				break;
			case 'textarea':
				if ((oElem.value!='') || bGetEmpty) s += getpostvalue(oElem, oElem.value, s.length>0);
				break;
			case 'select':
				s += getpostvalue(oElem, getddvalue(oElem), s.length>0);
				break;
		}
	}
	return s;
}

function getddvalue(oDD) {
	if (oDD && (oDD.selectedIndex>=0)) { return oDD.options[oDD.selectedIndex].value } else { return '' };
}

function cleardd(oDD, bLeaveFirst) {
	var i = bLeaveFirst ? 1 : 0;
	while (oDD.options.length > i) {
		oDD.remove(i);
	}
}

function getpostvalue(oElem, sValue, bAddSep) {
	return (bAddSep ? '&' : '') + oElem.name + '=' + encodeURIComponent(sValue)
}

Array.prototype.inArray = function (value) {
	// Returns true if the passed value is found in the array.  Returns false if it is not.
    var i;
    for (i=0; i < this.length; i++) {
		// Matches identical (===), not just similar (==). This means that for example '005' != '5'
        if (this[i] === value) return true;
    }
    return false;
};

function submitform(sID) {
	var oFrm = document.getElementById(sID);
	if (oFrm) oFrm.submit();
	return false;
}

/* Account functions */
function acc_logoff() {
	document.location.href = getbaseurl() + '/logoff/logoff.html';
}

function acc_logon() {
	document.location.href = getbaseurl() + '/logon/logon.html';
}

function acc_request() {
	document.location.href = getbaseurl() + '/content/resellerrequest.html';
}

function acc_doforgotpass(sFormID) {
	showpopupform(getPostParams('common','doforgotpass', getformstring(document.getElementById('frmForgotPass_' + sFormID))),400);
	closepopupform(sFormID);
}

function enc_pass() {
	/* encrypts all passed passwordboxes */
	var oTxt;
	for (var i=0;i<arguments.length;i++) {
		oTxt = document.getElementById(arguments[i]);
		oTxt.value = hex_md5(oTxt.value);
	}
	return true;
}

function positionContextMenu() {
	var cm = document.getElementById('contextmenu');
	if (document.documentElement.scrollTop>=10) {
		cm.style.marginTop = document.documentElement.scrollTop + 'px';
	} else {
		cm.style.marginTop = 10 + 'px';
	}
}

/* Event handler functions */

// written by Dean Edwards, 2005
// with input from Tino Zijdel - crisp@xs4all.nl
// http://dean.edwards.name/weblog/2005/10/add-event/
function addEvent(element, type, handler) {
	if (element.addEventListener)
		element.addEventListener(type, handler, false);
	else {
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		if (!element.events) element.events = {};
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			if (element['on' + type]) handlers[0] = element['on' + type];
			element['on' + type] = handleEvent;
		}
		handlers[handler.$$guid] = handler;
	}
}
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener)
		element.removeEventListener(type, handler, false);
	else if (element.events && element.events[type] && handler.$$guid)
		delete element.events[type][handler.$$guid];
}

function handleEvent(event) {
	event = event || fixEvent(window.event);
	var returnValue = true;
	var handlers = this.events[event.type];
	for (var i in handlers) {
		if (!Object.prototype[i]) {
			this.$$handler = handlers[i];
			if (this.$$handler(event) === false) returnValue = false;
		}
	}
	if (this.$$handler) this.$$handler = null;
	return returnValue;
}

function fixEvent(event) {
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
}
fixEvent.preventDefault = function() {
	this.returnValue = false;
}
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
}

// This little snippet fixes the problem that the onload attribute on the body-element will overwrite
// previous attached events on the window object for the onload event
if (!window.addEventListener) {
	document.onreadystatechange = function() {
		if (window.onload && window.onload != handleEvent) {
			addEvent(window, 'load', window.onload);
			window.onload = handleEvent;
		}
	}
}

/* Calendar helper functions */
function setupsinglecalendar(sID) {
	Calendar.setup({
		inputField		:	sID,                // id of the input field
		ifFormat		:   getcalformat(),
		button			:   'icdttrig_' + sID,  // trigger for the calendar (button ID)
		align			:   'BR',               // alignment (defaults to ""Bl"")
		singleClick		:   true
	});
}

function getcalformat() {
	switch (getpagelang()) {
		case 'nl':
			return '%d-%m-%Y';
		default:
			return '%m/%d/%Y';
	}
}