function addElement(uuid){
	uuidExists = findIndex(jsarray, uuid);
	if(uuidExists>-1){return false}
	
	if(uuid < 1){
		// Show an error message if the field is blank;
		document.getElementById('msg').style.display="block";
		document.getElementById('msg').innerHTML = "Error! Bad user-ID.";
	}else{
		// This is the <ul id="myList"> element that will contain the new elements
		var container = document.getElementById('myList');
		
		var url = '/javascripts/get_username.html?uuid=' + uuid;
		new Ajax.Request(url,
		{
	    	method:'get',
		    onSuccess: function(transport){
				uuid = uuid * 1; // ändra till numeric
				jsarray.push(uuid);
		    	response = transport.responseText || "no response text";
				// Create a new <li> element for to insert inside <ul id="myList">
				var new_element = document.createElement('li');
				new_element.setAttribute("id", uuid);
				new_element.innerHTML = response + ' <a href="#" onclick="removeConferenceParticipator(' + uuid + ')">Ta bort</a>';
				//new_element.innerHTML = get_username(element);
				container.insertBefore(new_element, container.firstChild);
				//document.getElementById('msg').innerHTML = jsarray.toString();
		    },
		    onFailure: function(){ 
				response = 'Error in database communication.';
				alert(response);
			}
		});
	}
}

function addToArray(uuid){
	uuid = uuid * 1; // ändra till numeric
	jsarray.push(uuid);
	// alert(jsarray.toString()); Verifierat. Funkar.
}

function findIndex(theArray, theItem) {
	var myPosition=-1
	for (i=0; i < theArray.length; i++) {
		if(theArray[i]==theItem) {
			myPosition = i;
			break;
		}
	}
	return myPosition;
}

function removeConferenceParticipator(uuid){
	document.getElementById(uuid).remove();
	ind = jsarray.indexOf(uuid);
	jsarray.splice(ind,1);
	//document.getElementById('msg').innerHTML = jsarray.toString();
}

function ValidateConfInfo(theform)
{
	/*var name = document.getElementById('ham_company');
	if(isEmpty(theform.conf_name, "Titel måste fyllas i!")){
		return false;
	}*/
	if (theform.conf_name.value=='') {
	    alert( 'Titel måste fyllas i!' );
	    theform.conf_name.focus();
	    return false ;
	}
	if (theform.conf_site.value=='') {
	    alert( 'Ort måste fyllas i!' );
	    theform.conf_site.focus();
	    return false ;
	}
	if (theform.conf_subj.value=='') {
	    alert( 'Ämne måste fyllas i!' );
	    theform.conf_subj.focus();
	    return false ;
	}
	var theurl = theform.conf_url.value;
	if (theurl > '') {
		if(!isUrl(theurl)){
			alert("Ingen URL!");
		    theform.conf_url.focus();
		    return false ;
		}
	}
	var date_one = theform.start_date.value;
	if(!isValidDate(date_one)){
		alert("Startdatum ej korrekt");
	    theform.start_date.focus();
	    return false ;
	}
	
	var date_two = theform.end_date.value;
	if(!isValidDate(date_two)){
		alert("Slutdatum ej korrekt");
	    theform.end_date.focus();
	    return false ;
	}
	
	if(theform.start_date.value > theform.end_date.value){
		alert("Sludatum kan inte vara före startdatum");
		theform.end_date.value = '';
	    theform.end_date.focus();
	    return false ;
	}
	
	document.getElementById('userarray').value = jsarray.toString();
	//alert(jsarray.toString());
	return true;
}

function isUrl(urlStr) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	if(regexp.test(urlStr.toLowerCase())){
		return true;
	}else{
		return false;
	}
}

function isValidDate(dateStr) {
	var regexp = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
	
	if ( regexp.test(dateStr.toString()) == false) { 
		return false; 
	}
	var parts = dateStr.split(RegExp.$1);
	var yy = parts[0];
	var mm = parts[1];
	var dd = parts[2];

	var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
	if (parseFloat(dd) != dt.getDate()) { return false; }
	if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
	return true;
}

function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url.substring(0,23)+'...</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
    //statusHTML.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
    statusHTML.push('<li><span>'+status+'</span><br /><a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
  }
  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}

function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}

function ValidateRequestSv(theform)
{
	if (theform.title.value=='') {
	    alert( 'Titel måste fyllas i!' );
	    theform.title.focus();
	    return false ;
	}
	if (theform.shelf.value=='') {
	    alert( 'Hyllkod måste fyllas i!' );
	    theform.shelf.focus();
	    return false ;
	}
	if (theform.year.value=='') {
	    alert( 'År måste fyllas i!' );
	    theform.year.focus();
	    return false ;
	}
	if (theform.name.value=='') {
	    alert( 'Du måste fylla i ditt namn!' );
	    theform.name.focus();
	    return false ;
	}
	return true;
}

function ValidateRequestEn(theform)
{
	if (theform.title.value=='') {
	    alert( 'Please fill in the title!' );
	    theform.title.focus();
	    return false ;
	}
	if (theform.shelf.value=='') {
	    alert( 'Please fill in the shelf code!' );
	    theform.shelf.focus();
	    return false ;
	}
	if (theform.year.value=='') {
	    alert( 'Please fill in the year!' );
	    theform.year.focus();
	    return false ;
	}
	if (theform.name.value=='') {
	    alert( 'Please fill in your name!' );
	    theform.name.focus();
	    return false ;
	}
	return true;
}	

function ValidateBookLibrSv(theform)
{
	if (theform.from_name.value=='') {
	    alert( 'Ditt namn måste fyllas i!' );
	    theform.from_name.focus();
	    return false ;
	}
	if (theform.from_email.value=='') {
	    alert( 'Din e-post måste fyllas i!' );
	    theform.from_email.focus();
	    return false ;
	}
	if (theform.subject.value=='') {
	    alert( 'Ämne måste fyllas i!' );
	    theform.subject.focus();
	    return false ;
	}
	if (theform.number.value<'1' || theform.number.value>'5') {
	    alert( 'Ange antal personer i gruppen' );
	    theform.number.focus();
	    return false ;
	}
	return true;
}	

function ValidateBookLibrEn(theform)
{
	if (theform.from_name.value=='') {
	    alert( 'Please fill in your name!' );
	    theform.from_name.focus();
	    return false ;
	}
	if (theform.from_email.value=='') {
	    alert( 'Please fill in your e-mail address!' );
	    theform.from_email.focus();
	    return false ;
	}
	if (theform.subject.value=='') {
	    alert( 'Please fill in a subject!' );
	    theform.subject.focus();
	    return false ;
	}
	if (theform.number.value<'1' || theform.number.value>'5') {
	    alert( 'Please state the number of group members!' );
	    theform.number.focus();
	    return false ;
	}
	return true;
}

/*function toggle(theDiv, theContent) {
	var ele = document.getElementById(theDiv);
	if(ele.style.display == "block") {
		ele.style.display = "none";
		text.innerHTML = "show";
	}else{
		ele.style.display = "block";
		text.innerHTML = "hide";
	}
}*/

function toggle_blind(element, len) {
    var cookieVal = readCookie("box_visible");
    if(cookieVal=='true'){
        document.cookie = 'box_visible=false; path=/'
    }else{
        document.cookie = 'box_visible=true; path=/'
    }
	if(len == null) {
		var len = 1;
	}
	Effect.toggle(element, 'blind', { duration: len });
}

function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

/*
function geo_decode(anchor) {
    var href = anchor.getAttribute('href');
    var address = href.replace(/.*contact\/index.xml\?q\=([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
    if (href != address) {
        anchor.setAttribute('href','mailto:' + address + '?subject=Referring page: ' + document.URL);
    }
}

function getLinks() {
    var links = document.getElementsByTagName('a');
    for (var i = 0 ; i < links.length ; i++) {
        links[i].onclick = function() {
            geo_decode(this);
        }
    }
}

var MyFunction = {
  init: function() {
    var links = document.getElementsByTagName('a');
    for (var i = 0 ; i < links.length ; i++) {
        links[i].onclick = function() {
            geo_decode(this);
        }
    }
  }
}

Event.observe(window, 'load', function() { MyFunction.init() }, false);
*/








/* Event.observe(document, 'load', function() { MyFunction.init() });
Event.observe(window, 'load', function() { MyFunction.init() }); */

//MyFunction.init();

/*document.observe('dom:loaded', function() {
  var links = document.getElementsByTagName('a');
    for (var i = 0 ; i < links.length ; i++) {
        links[i].onclick = function() {
            geo_decode(this);
        }
    }
} );*/

/*window.onload = function () {
    getLinks();
}*/
