// Generic libs for INFOMAS WSX
// expects base_url variable to be set with the base-url.
//
// version 2.0, Vincent van Beveren

var messageDiv = null;

var greetings = {
	'nl' : [ 'Goedenacht', 'Goedemorgen', 'Goedemiddag', 'Goedenavond' ],
	'en' : [ 'Good night', 'Good morning', 'Good afternoon', 'Good evening' ]	
}

function displayGreeting(lang) {
	if (!lang) {
		lang = 'nl';
	}
	dayPart = Math.floor((new Date()).getHours() / 6);
	
	document.writeln(greetings[lang][dayPart]);
}

/*
 Opens a window in which the assortment can be selected
 */
function addToAssortmentWindow(prodId) {
	var left = (screen.width - 300) / 2;
	var top = (screen.height - 100) / 3;

	var w = window.open(appRootUrl + '/shop/selectAssortment.html?objId=' + prodId, 'addToAssortment', 
		'width=300, height=120, left=' + left + ', top=' + top);
	w.focus();
}

function addToAssortmentByCodeWindow(code) {
	var left = (screen.width - 300) / 2;
	var top = (screen.height - 100) / 3;
	
    // first check if there is only one 'order' component.
    var components = document.getElementsByName('bestel');
    if (components.length == 0) {
        // else check if there are multiple, and select the right one.
        components = document.getElementsByName('bestel' + code)
    }
    if (components.length == 0) {
        alert('Bestel kolom niet gevonden, actie kan niet worden voltooid');
        return;
    }
    v = components[0].value;
    
	var w = window.open(appRootUrl + '/shop/selectAssortment.html?productCode=' + escape(code) + '&productQuantity=' + v, 'addToAssortment', 
		'width=300, height=120, left=' + left + ', top=' + top);
	w.focus();
}


/*
	General helper functions
 */
 
function doAction(name, parameters) {
	url="shop/actions/" + name + "?url=" + escape(location.href);

	if (parameters != null) {	
		for (key in parameters) {
			url += "&" + key + "=" + escape(parameters[key]);
		}
	}
	location.href = url;
}

function setCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	setCookie(name,"",-1);
}

/*
 * Creates a new XmlHttpRequest object.
 */
function createXmlHttpRequest() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
		}
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		}
	}
	throw new Error(null, 'Browser ondersteund geen XML aanroep');
}

/**
 * Simple method to check whether or no the request is completed.
 */
function isHttpRequestDone(req) {
	return req.readyState == 4;
}

function showMessage(message, width, height) {
	var windowWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;
	if (!windowWidth) {
		return;
	}

	var windowHeight = window.innerHeight ? window.innerHeight : document.body.clientHeight;
	var windowScrollX = window.scrollX ? window.scrollX : document.body.offsetTop;

	if (messageDiv == null) {
		var body = document.getElementsByTagName('body')[0];
		messageDiv = document.createElement('div');
		messageDiv.style.position = 'absolute';
		messageDiv.className = 'text';
		messageDiv.style.border = 'solid 1px black';
		messageDiv.style.backgroundColor = 'white';
		messageDiv.style.textAlignment = 'center';
		messageDiv.style.padding = '5px';
		body.appendChild(messageDiv);
	}
	messageDiv.innerHTML = message;
	messageDiv.style.width = width + 'px';
	messageDiv.style.height = height + 'px';
	
	
	messageDiv.style.left = ((windowWidth - width) / 2) + 'px';
	messageDiv.style.top = (windowScrollX + (windowHeight - height) / 2) + 'px';
}

function clearMessage() {
	if (messageDiv == null) {
		return;
	}
	var body = document.getElementsByTagName('body')[0]
	body.removeChild(messageDiv);
	messageDiv = null;
}

function isMessageShowing() {
	return messageDiv != null;
}

function zoomImage(src) {
	imgWin = window.open(src, "_imgWin", "width=800,height=600,resizable=1");	
	imgWin.focus();
}
	

