function handle_div(){
	var myWidth = 0, myHeight = 0;
	var container_width = 900, container_height = 650; // hardcoded values, must be the same in css for 'container' 
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
  
	d = document.getElementById('container');
	pos_left = Math.max((myWidth - container_width) / 2, 0); 
	pos_top = Math.max((myHeight - container_height) / 2, 0);
	d.style.left = "" + pos_left + "px";
	d.style.top = "" + pos_top + "px";
	d.style.display = 'block';
}

/**
 * make sure we have an integer, otherwise put 0 there
 */
function validate_qty(object) {
	the_value = parseInt(object.value);
	if (isNaN(the_value)) the_value = 0;
	object.value = the_value;
}

/**
 * Calculate the order total based on the id's found on the page
 * The purpose is to present the information to the client, it's 
 * not used in the backend ( there it's recalculated )
 */
function calculate_order_total(){
	var ds = get_decimal_separator();
	var current_item = 0;
	var order_total = 0;
	var field_ot = document.getElementById('order_total');
	if (!field_ot) return null; // we don't have the element in our page
	var field_item, field_qty, hidden_item_price;
	var item_price_found, item_qty_found;
	var price;
	var item_price;
	do {
		item_price_found = 0;
		item_qty_found = 0;
		current_item += 1; // look for fields with price and quantity
		field_item = document.getElementById('order_item_price' + current_item);
		if (field_item) item_price_found = 1;
		field_qty = document.getElementById('order_item_qty' + current_item);
		if (field_qty) item_qty_found = 1;
		if ((item_price_found == 1) && (item_qty_found == 1)) {
			item_price = get_value(field_item, 1, ds) * get_value(field_qty, 0, ds);
			hidden_item_price = document.getElementsByName('order_item_price' + current_item);
			if (hidden_item_price.length > 0) hidden_item_price[0].value = ('' + format_price(item_price)).replace('.', ds);
			order_total += item_price;
		}
	} while ((item_price_found == 1) && (item_qty_found == 1));
	set_total_price(order_total); // order_total is with . as separator (because of the values returned by get_value)
}

/** 
  * get the value from the element object given
  * element is presumed to exist
  * if element_type: 0 = we have an input field, 1 = element with innerHTML
  * decimal separator is used for reading the value in case of type 1 (where we expect floats)
  * returns 0 if it's empty or not a valid number, or the number (with "." as decimal separator)
  */
function get_value(element, element_type, decimal_separator) {
	var the_value;
	switch (element_type) {
		case 0: the_value = parseInt(element.value); break;
		case 1: 
			the_value = element.innerHTML.toString().replace(decimal_separator, '.');
			the_value = parseFloat(the_value);
			break;
		default: the_value = 0;
	}
	if (isNaN(the_value)) 
		return 0
	else
		return the_value;
}

/**
 * set the total price of the order
 */
function set_total_price(price) {
	total_price = '' + format_price(price);
	document.getElementById('order_total').innerHTML = total_price;
	hidden_item_total_price = document.getElementsByName('order_total');
	if (hidden_item_total_price.length > 0) hidden_item_total_price[0].value = total_price;
}

/**
 * transform the price to have two decimals. 1 -> 1.00, 1.4 -> 1.40, etc...
 * it is used only on 2 decimal numbers (we format them before)
 */
function format_price(price) {
	var ds = get_decimal_separator();
	var to_append = '';
	var formated_price = FormatFloat(price,2);
	formated_price = String(formated_price).replace('.', ds);
	if (formated_price.indexOf(ds) == -1)
		to_append = ds + '00'
	else if ((formated_price).length - formated_price.indexOf(ds) - 1 == 1)
		to_append = '0';
	return formated_price + to_append;
}

/**
 * format the number giving the number of decimal points. the pFload should be with with "." as decimal separator
 */
function FormatFloat(pFloat, pDp){
    var m = Math.pow(10, pDp);
    return parseInt(pFloat * m, 10) / m;
}

/**
 * get the order number based on date. it will be YYMMDDHHMMSS
 */
function get_order_number(){
	var order_number = '';
	var temp = '';
	var d = new Date();
	order_number += ('' + d.getFullYear()).substr(2,2);
	temp = '' + (d.getMonth() + 1);	if (temp.length < 2) temp = '0' + temp; order_number += temp;
	temp = '' + d.getDate(); 		if (temp.length < 2) temp = '0' + temp; order_number += temp;
	temp = '' + d.getHours(); 		if (temp.length < 2) temp = '0' + temp; order_number += temp;
	temp = '' + d.getMinutes();		if (temp.length < 2) temp = '0' + temp; order_number += temp;
	temp = '' + d.getSeconds();		if (temp.length < 2) temp = '0' + temp; order_number += temp;
	temp = '' + d.getMilliseconds();
	if (temp.length == 1) temp = '00' + temp; 
	if (temp.length == 2) temp = '0' + temp; 
	order_number += temp;
	return order_number;
}

/**
 * get the date of the order, in the correct format. if field not found, return date in format DD-MM-YYYY
 */
function get_order_date() {
	var order_date = '';
	var temp = '';
	var df_element = document.getElementsByName('date_format');
	var df = "{2}-{1}-{0}";
	if (df_element.length > 0) df = document.order.date_format.value;
	var d = new Date();
	order_year = '' + d.getFullYear();
	order_month = '' + (d.getMonth() + 1);	if (order_month.length < 2)	order_month = '0' + order_month;
	order_day = '' + d.getDate(); 			if (order_day.length < 2) 	order_day = '0' + order_day;
	return df.format( order_year, order_month, order_day );
}

/**
 * get the time of the order, in format HH:MM (HH: 00-23, MM: 00-59)
 */
function get_order_time() {
	var order_date = '';
	var temp = '';
	var d = new Date();
	var tf = "{0}:{1}";
	order_hour = '' + d.getHours();      if (order_hour.length < 2)	order_hour = '0' + order_hour;
	order_minutes = '' + d.getMinutes(); if (order_minutes.length < 2) order_minutes = '0' + order_minutes;
	return tf.format(order_hour, order_minutes);
}

/**
 * check if something was ordered, and set the other hidden field variables
 */
function submit_form() {
	var current_item = 0;
	var field_qty;
	var total_qty = 0;
	do {
		current_item += 1; // look for fields with quantity
		field_qty = document.getElementById('order_item_qty' + current_item);
		if (field_qty) total_qty += get_value(field_qty, 0);
	} while (field_qty);
	
	if (total_qty > 0) {
		document.order.order_number.value = get_order_number();
		document.order.order_date.value = get_order_date();
		document.order.order_time.value = get_order_time();
		document.order.submit();
	} else {
		alert('You didn\'t order anything !');
		return false;
	}
} 

/**
 * return the decimal separator (it looks in the form, for the value of the hidden field "decimal_separator". if element not found, return "."
 */
function get_decimal_separator() {
	var ds_element = document.getElementsByName('decimal_separator');
	if (ds_element.length == 0) return ".";
	var ds = document.order.decimal_separator.value;
	return ds;
}

/**
 * Format a string
 * Eg.
 * 		var firstName = 'Ray';
 * 		var lastName = 'Houston';
 * 		var hello = 'Hello. My name is {0} {1}.'.format( firstName, lastName );
 */ 
String.prototype.format = function()
{
    var str = this;
    for(var i=0;i<arguments.length;i++)
    {
        var re = new RegExp('\\{' + (i) + '\\}','gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
}

/**
 * functions for taking care of the language parameter for the other languages other then default
 *
 * get url parameters
 * 'name' is the name of the parameter
 * returns empty string or the value of the parameter
 */
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function check_language_params() {
	var param_l = gup('L');
	var L; // language link object
	var current_link = 0;
	if (param_l != '') {
		param_l = parseInt(param_l);
		do {
			current_link += 1;
			L = document.getElementById('lang_link' + current_link);
			if (L) L.href = L.href + '&L=' + param_l;
		} while (L);
	}
	return;
}