isSubmit = false;
function disableButton(butt, f, val) {
	butt.disabled = true;
	if (val.length > 0){
		butt.value=val;
	}
	if (f.length > 0 && !isSubmit){
		f.submit();
		isSubmit=true;
	}
}

function checkMode(formname){
	selVal = document.formname.PersonnummerMode.options[document.formname.PersonnummerMode.selectedIndex].value;
	if (selVal=='<' || selVal=='>'){
		if (document.formname.Personnummer.value.length > 0 && !checkFodelsenummer(document.formname.Personnummer)){
			alert('Fel format på födelsenummer.\nFormat ska vara ååååmmdd, t.ex. 19780414.');
			document.formname.Personnummer.value='';		
			document.formname.Personnummer.focus();
		}
	}
}

function checkFodelsenummer(s){
   var re = new RegExp('^(19[0-9][0-9]|20[0-9][0-9])([0][1-9]|[1][012])([0][1-9]|[12][0-9]|[3][01])$');
  
  if (!s.value.match(re)) {
	return false;
  }
  return true;
}

function checkAllCheckboxes(field){
	for (i = 0; i < field.length; i++){
		var p = field[i];
		p.checked = true;
	}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function hide(id) { 
 obj = document.getElementsByTagName("div"); 
 obj[id].style.visibility = "hidden"; 
 obj[id].style.position = "absolute"; 
} 

function show(id) { 
 obj = document.getElementsByTagName("div"); 
 obj[id].style.visibility = "visible"; 
 obj[id].style.position = "relative"; 
} 


function updatekursselect(selectedgroup){
kurslist.options.length=0;
	if (selectedgroup>0){
		for (i=0; i<kurser[selectedgroup].length; i++){
			kurslist.options[kurslist.options.length]=new Option(kurser[selectedgroup][i].split("|")[0], kurser[selectedgroup][i].split("|")[1]);
		}
	}	
}

function updateunderkatergoriselect(selectedgroup){
underkategorilist.options.length=0;
	if (underkategori[selectedgroup] == null){
		return;
	}
	if (selectedgroup>0){
		for (i=0; i<underkategori[selectedgroup].length; i++){
			underkategorilist.options[underkategorilist.options.length]=new Option(underkategori[selectedgroup][i].split("|")[0], underkategori[selectedgroup][i].split("|")[1]);
		}
	}
}


function grayOut(vis, options) {  // Pass true to gray out screen, false to ungray  // options are optional.  This is a JSON object with the following (optional) properties  // opacity:0-100         // Lower number = less grayout higher = more of a blackout   // zindex: #             // HTML elements with a higher zindex appear on top of the gray out  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear  // in any order.  Pass only the properties you need to set.  var options = options || {};   var zindex = options.zindex || 50;  var opacity = options.opacity || 70;  var opaque = (opacity / 100);  var bgcolor = options.bgcolor || '#000000';  var dark=document.getElementById('darkenScreenObject');  if (!dark) {    // The dark layer doesn't exist, it's never been created.  So we'll    // create it here and apply some basic styles.    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917    var tbody = document.getElementsByTagName("body")[0];    var tnode = document.createElement('div');           // Create the layer.        tnode.style.position='absolute';                 // Position absolutely        tnode.style.top='0px';                           // In the top        tnode.style.left='0px';                          // Left corner of the page        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars                    tnode.style.display='none';                      // Start out Hidden        tnode.id='darkenScreenObject';                   // Name it so we can find it later    tbody.appendChild(tnode);                            // Add it to the web page    dark=document.getElementById('darkenScreenObject');  // Get the object.  }  if (vis) {    // Calculate the page width and height     if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {        var pageWidth = document.body.scrollWidth+'px';        var pageHeight = document.body.scrollHeight+'px';    } else if( document.body.offsetWidth ) {      var pageWidth = document.body.offsetWidth+'px';      var pageHeight = document.body.offsetHeight+'px';    } else {       var pageWidth='100%';       var pageHeight='100%';    }       //set the shader to cover the entire page and make it visible.    dark.style.opacity=opaque;                          dark.style.MozOpacity=opaque;                       dark.style.filter='alpha(opacity='+opacity+')';     dark.style.zIndex=zindex;            dark.style.backgroundColor=bgcolor;      dark.style.width= pageWidth;    dark.style.height= pageHeight;    dark.style.display='block';                            } else {     dark.style.display='none';  }}function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';				 
  } else {
     dark.style.display='none';
  }
}


// ajax request functions
//


   var http_request = false;
   function makePOSTRequest(url, parameters, inContainer) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange =  function() { alertContents(inContainer) };
      http_request.open('POST', url, true);

      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function sendCheck(id, target,inContainer) {
      var poststr = id +"=" + encodeURI( document.getElementById(id).value);
      makePOSTRequest(target, poststr,inContainer);
   }

   function sendCommand(id, query, target,inContainer) {
      var poststr = id + "=" + encodeURI( document.getElementById(id).value) + "&" +query;
      makePOSTRequest(target, poststr,inContainer);
   }


   function alertContents(inContainer) {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById(inContainer).innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }

//
// end ajax request functions
//



// http://www.harrymaugans.com/2007/03/06/how-to-create-an-animated-sliding-collapsible-div-with-javascript-and-css/

var timerlen = 5;
var slideAniLen = 250;

var timerID = new Array();
var startTime = new Array();
var obj = new Array();
var endHeight = new Array();
var moving = new Array();
var dir = new Array();

function slidedown(objname){
        if(document.getElementById(objname) !=null){
        if(moving[objname])
                return;

         if(document.getElementById(objname).style.display != "none")
                return; // cannot slide down something that is already visible

        moving[objname] = true;
        dir[objname] = "down";
        startslide(objname);
        }
}

function slideup(objname){
        if(document.getElementById(objname) !=null){
        if(moving[objname])
                return;

        if(document.getElementById(objname).style.display == "none")
                return; // cannot slide up something that is already hidden

        moving[objname] = true;
        dir[objname] = "up";
        startslide(objname);
}
}

function startslide(objname){
        obj[objname] = document.getElementById(objname);

        endHeight[objname] = parseInt(obj[objname].style.height);
        startTime[objname] = (new Date()).getTime();

        if(dir[objname] == "down"){
                obj[objname].style.height = "1px";
        }

        obj[objname].style.display = "block";

        timerID[objname] = setInterval('slidetick(\'' + objname + '\');',timerlen);
}

function slidetick(objname){
        var elapsed = (new Date()).getTime() - startTime[objname];

        if (elapsed > slideAniLen)
                endSlide(objname)
        else {
                var d =Math.round(elapsed / slideAniLen * endHeight[objname]);
                if(dir[objname] == "up")
                        d = endHeight[objname] - d;

                obj[objname].style.height = d + "px";
        }

        return;
}

function endSlide(objname){
        clearInterval(timerID[objname]);

        if(dir[objname] == "up")
                obj[objname].style.display = "none";

        obj[objname].style.height = endHeight[objname] + "px";

        delete(moving[objname]);
        delete(timerID[objname]);
        delete(startTime[objname]);
        delete(endHeight[objname]);
        delete(obj[objname]);
        delete(dir[objname]);

        return;
}

var DHTML = (document.getElementById || document.all || document.layers);

function swapDiv(cssid)
{
	if (!DHTML) return;
	var x = new getObj(cssid);

	if (x.style.visibility=='visible'){
		grayOut(false);
		x.style.visibility='hidden';
	} else {
		x.style.visibility='visible';
		x.style.zIndex=60;     
		grayOut(true,{'zindex':'40', 'bgcolor':'#000000', 'opacity':'20'});
	}
}
function foldMenu(cssid)
{
	if (!DHTML) return;
	var x = new getObj(cssid);

	if (x.style.visibility=='visible'){
		x.style.visibility='hidden';
		x.style.position = 'absolute';
	} else {
		x.style.visibility='visible';
		x.style.position = 'relative';
	}
}

function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

function generate_address( username, hostname, text ) {
    var atsign = "&#64;";
    var addr = username + atsign + hostname;
    document.write( 
      "<" + "a" + " " + "href=" + "mai" + "lto:" + addr + " class=\"internlink\">" +
      text +
      "<\/a>");
  }

function popup(file,height,width,name)
{	
	window.open(file, name, "status=0, resizable=1, scrollbars=1, height=" + height + ", width=" + width + "" );
}

function refreshWindow(page){
	window.opener.location.href=page;
	self.close();
}

function refreshParentWindow(page){
  window.opener.location.href = page;

  if (window.opener.progressWindow)
		
 {
    window.opener.progressWindow.close()
  }
  window.close();
}


function confirmIt(msg)
{
    var is_confirmed = confirm(msg);
    if (is_confirmed) {
     return true;
    }
return false;
}

  function askDelete(pic){
  	if (confirm('Vill du verkligen ta bort denna bild?')){
  		var form = $('deleteform');
		var input = form['delete'];

  		input.value=pic;
  		form.submit();
  	}
  }
