function openPopup(url,w,h,namn)
{
	if(namn==null || namn=='')
	{
		namn='popup';
	}
	
	var newWin = window.open(url,namn,'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width='+w+',height='+h);
	newWin.focus();
}

function focusParent()
{
	if(window.opener){
		window.opener.focus();
	}
	
	window.close();
}

/* -------------------------------------------------------------------------- */
/* Javascripts */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

function toggleStyleDisplay (id, defaultval)
{
	
	if (document.getElementById(id)) {
		
		myElement = document.getElementById(id);
		
		if (myElement.style.display == defaultval || myElement.style.display == '') {
			
			if (defaultval == 'none') { myElement.style.display = 'block'; }
			else { myElement.style.display = 'none'; }
			
		}
		else {
			
			if (defaultval == 'none') { myElement.style.display = 'none'; }
			else { myElement.style.display = 'block'; }
			
		}
	
	}
}

/* -------------------------------------------------------------------------- */

function toggleMenu (id)
{
	
	if (document.getElementById('sub'+id) && document.getElementById('arw'+id)) {
		subobj = document.getElementById('sub'+id);
		arwobj = document.getElementById('arw'+id);
		
		if (subobj.style.display != 'block') {
			subobj.style.display = 'block';
			arwobj.src = '/images/meny-pil-oppen.gif';
		}
		else {
			subobj.style.display = 'none';
			arwobj.src = '/images/meny-pil-stangd.gif';
		}
		
	}
}

/* -------------------------------------------------------------------------- */


function getSelectedListboxItems(hList)
{
	var items = Array();
	
	for(var i=0; i<hList.options.length; i++)
	{
		var e = hList.options[i];
		
		if(e.selected){
			items[items.length] = e;
		}
	}
	return items;
}

function countSelectedListboxitems(hList){
	return getSelectedListboxItems(hList).length;
}

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Object") == -1)
      return false;
   else
      return true;
}

function getRadioItems(hRadio)
{
	var items = Array();

	if (isArray(hRadio)){
		for(var i=0; i<hRadio.length; i++)
		{
			var e = hRadio[i];

			if(e.checked){
				items[items.length] = e;
			}
		}
	} else {

		if(hRadio.checked)
			items[items.length] = true;
	}
	
	return items;
}

function isRadioChecked(hRadio){
	return ( getRadioItems(hRadio).length > 0 );
}

function countString(str, find){
	return str.split(find).length - 1;
}

function initCap(s)
{
	if(s.length > 0 )
	{
		var first = s.charAt(0);
		var rest = s.substring(1);
		
		return first.toUpperCase() + rest;
	}
	
	return s;
}

function formatPostnr(postnr)
{
	postnr = keepChars(postnr, "0123456789");
	
	if(postnr.length != 5)
		return postnr;
	
	return postnr.substring(0,3) + ' ' + postnr.substring(3);
}

function hideLayer(sLayer)
{
	if(document.layers)
		document.all[sLayer].style.visibility = 'hide';

	else if( document.all)
		document.all[sLayer].style.visibility = 'hidden';
}

function showLayer(sLayer)
{
	if(document.layers)
		document.all[sLayer].style.visibility = 'show';

	else if( document.all)
		document.all[sLayer].style.visibility = 'visible';
}

function compareDates( sDate1, sDate2 )
{
	// compareDates( datum1, datum2 ) lämnar tillbaka
	// 	-1	om datum1 är mindre än datum2
	// 	 0	om datum1 och datum2 är indentiska
	// 	 1	om datum1 är större än datum2
	//
	// Datumformatet måste vara yyyy-mm-dd

// Dela upp datumet vid "-"
	var aDate1 = sDate1.split("-");
	var aDate2 = sDate2.split("-");

// Representerar jämförelse mellan de två datumen
// Värdet för variablen följer samma regelverk som själv compareDates
	var nYearCmp	= 0;
	var nMonthCmp	= 0;
	var nDayCmp		= 0;

	if( aDate1[0] > aDate2[0] )
		nYearCmp = 1;

	else if( aDate1[0] < aDate2[0] )
		nYearCmp = -1;


	if( aDate1[1] > aDate2[1] )
		nMonthCmp = 1;

	else if( aDate1[1] < aDate2[1] )
		nMonthCmp = -1;


	if( aDate1[2] > aDate2[2] )
		nDayCmp = 1;

	else if( aDate1[2] < aDate2[2] )
		nDayCmp = -1;


	if( nYearCmp == 0 )
		return (nMonthCmp != 0 ) ? nMonthCmp : nDayCmp;

	return nYearCmp;
}

function validKorturl( s )
{
   if( s == "" )
      return false;
      
   var sValidChars = "abcdefghijklmnopqrstuvwxyz0123456789-_";
   
   for( var i=0; i<s.length; i++ )
      if( sValidChars.indexOf( s.charAt(i) ) == -1 )
         return false;
   
   return true;
}

function validInteger( sValue, bAllowEmpty )
{
   /*****************************************************
   * Returnerar true om alla tecken i sValue är numeriska.
   * False annars.
   *
   * Funktionen beslutar att tecken som ligger utanför
   * gränserna (enligt nedan) inte är numeriska
   * [0] = ASCII 48
   * [9] = ASCII 57
   ******************************************************/
   if( bAllowEmpty == false && sValue.length == 0 )
      return false;
      
   for( var i=0; i<sValue.length; i++ )
   {
      if( (sValue.charCodeAt(i) < 48) || (sValue.charCodeAt(i) > 57) )
      {
         return false;
      }
   }

   return true;
}

function trim(s){
   return rtrim( ltrim(s) );
}

function ltrim(s)
{
   while(1)
   {
      if( s.charAt(0) != ' ' && s.charAt(0) != '\t' ){
         break;
      }

      s = s.substring(1, s.length);
   }

   return s;
}


function rtrim(s)
{
   while(1)
   {
   	var substr = s.substring(s.length - 1, s.length);
   
      if(substr.charAt(0) != ' ' && substr.charAt(0) != '\t' ){
         break;
      }

      s = s.substring(0, s.length - 1);
   }

   return s;
}

/* Lookuptable for days in month */
var g_arrDaysInMonth = new Array( );
   g_arrDaysInMonth[ 0 ]   = 31;
   g_arrDaysInMonth[ 1 ]   = 28;
   g_arrDaysInMonth[ 2 ]   = 31;
   g_arrDaysInMonth[ 3 ]   = 30;
   g_arrDaysInMonth[ 4 ]   = 31;
   g_arrDaysInMonth[ 5 ]   = 30;
   g_arrDaysInMonth[ 6 ]   = 31;
   g_arrDaysInMonth[ 7 ]   = 31;
   g_arrDaysInMonth[ 8 ]   = 30;
   g_arrDaysInMonth[ 9 ]   = 31;
   g_arrDaysInMonth[ 10 ]  = 30;
   g_arrDaysInMonth[ 11 ] = 31;


function GetDaysOfMonth( nMonth,nYear )
{
/* Returns the number of days in [nMonth] of [nYear]. */

/* Other than February, days are allready calculated */
   if( nMonth != 2 )
   {     
      return g_arrDaysInMonth[ (nMonth - 1) ];
   }

/* Perform a leapyear check */
   else  
   {
      /* Is it leapyear ? */
      if (((nYear % 4 == 0) && (nYear % 100 != 0)) || (nYear % 400 == 0))
      {
         /* If so, febryary has 29 days */
         return 29;
      }

      else
      {
         /* If not, february has 28 */
         return 28;
      }
   }
}

function isValidPnrOrBirthdate( sPnr )
{
 if( isValidPnr(sPnr) )
   return true;
 else if( isValidBirthdate(sPnr) )
   return true;
 else
   return false;
}

function isValidPnr( ip_sPnr )
{
   if(ip_sPnr.length == 12)
   	ip_sPnr = ip_sPnr.substring(2);
   
   var sPnr = ip_sPnr;

   var iControl = sPnr.substring( sPnr.length-1 );   // Angiven kontrollsiffra
   var iCheckSum = 0;
   var iCalculatedControl = 0;   // Kalkylerad kontrollsiffra
   var iMultiplier = 2;

   for( var ix=0; ix<(sPnr.length -1); ix++ )
   {
      iNextVal = iMultiplier * sPnr.charAt( ix );

      if( iNextVal > 9 )
      {
         iNextVal = (((iNextVal - (iNextVal % 10)) / 10) + iNextVal % 10);
      }

      iCheckSum += iNextVal;

      iMultiplier = (iMultiplier == 2) ? 1 : 2;
   }

   iCalculatedControl = (10 - (iCheckSum % 10));
   iCalculatedControl = ( iCalculatedControl == 10 ) ? 0 : iCalculatedControl;

   return ( iCalculatedControl == iControl ) ? true : false;
}

function isValidBirthdate( sDate )
{
/* Validates a date. Returns false if date is invalid.
   Correct bithdate has format:
   yymmdd
*/
   var nYear,nMonth,nDay, nThisYear;

   var newDate = new Date();
   
   //var nThisYear = newDate.getYear();
   //Ny funktion format 'ssss'   browser >= 4.0 
   var nThisYear = newDate.getFullYear();
   
   if( sDate.length == 8 )
   {
      nYear   = sDate.substr( 0,4 );  // Parse out year
      nMonth  = sDate.substr( 4, 2 ); // Parse out month
      nDay    = sDate.substr( 6, 2 ); // Parse out day
   }
   else if( sDate.length == 6 )
   {
      nYear   = sDate.substr( 0,2 );  // Parse out year
      nMonth  = sDate.substr( 2, 2 ); // Parse out month
      nDay    = sDate.substr( 4, 2 ); // Parse out day
   }
   else 
   {
      /* Date must be 10 characters */
      return false;
   }
	
   if( nYear < ( nThisYear - 150 ) ||	nYear  >  nThisYear)
   {
      /* Year must be possitive */
      return false;
   }

   if( nMonth < 1 || nMonth > 12 )
   {
      /* Only 12 months in a year */
      return false;
   }

   if (nDay < 1 )
   {
      /* Days must be possitive */
      return false;
   }

   if( nDay > GetDaysOfMonth( nMonth, nYear ) )
   {
      /* Days cant exceed days in a specific month */
      return false;
   }

   return true;
}


function isValidDate( sDate )
{
/* Validates a date. Returns false if date is invalid.
   Correct date has format:
   yyyy-mm-dd
*/

/* No date is also a valid date :) */
   if( sDate.length == 0 )
   {
      return true;
   }
   
   if( sDate.length != 10 )
   {
      /* Date must be 10 characters */
      return false;
   }

   var nYear   = sDate.substr(0,4);  // Parse out year
   var nMonth  = sDate.substr(5,2); // Parse out month
   var nDay    = sDate.substr(8,2); // Parse out day

   if( nYear < 0 )
   {
      /* Year must be possitive */
      return false;
   }

   if( nMonth < 1 || nMonth > 12 )
   {
      /* Only 12 months in a year */
      return false;
   }

   if (nDay < 1 )
   {
      /* Days must be possitive */
      return false;
   }

   if( nDay > GetDaysOfMonth( nMonth, nYear ) )
   {
      /* Days cant exceed days in a specific month */
      return false;
   }

   return true;
}

function isValidTime( sTime )
{
   sTimeString = keepChars(sTime,"0123456789");

   if (sTimeString.length != 4)
      return false;

   nHour    = sTimeString.substring(0,2);
   nMinute  = sTimeString.substring(2,4);

   if (nHour > 24)
      return false;
      
   if (nMinute > 60)
      return false;
   
   return true;
}

function isValidEmail( str)
{
   if( str == "" )
   {
      return true;
   }

   // are regular expressions supported?
   var supported = 0;
   
   if( window.RegExp )
   {
      var tempStr = "a";
      var tempReg = new RegExp(tempStr);
      
      if( tempReg.test(tempStr) )
      {
         supported = 1;
      }
   }

   if( supported == 0 )
   {
      return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
   }
   
   var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
   var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{1,3}|[0-9]{1,4})(\\]?)$");
   
   return (!r1.test(str) && r2.test(str));
}

function validPostNr( sValue )
{
   /*****************************************************
   * Returnerar true om alla tecken i sValue är numeriska.
   * False annars.
   *
   * Funktionen beslutar att tecken som ligger utanför
   * gränserna (enligt nedan) inte är numeriska
   * [0] = ASCII 48
   * [9] = ASCII 57
   ******************************************************/

   if ( sValue.length > 6 || sValue.length < 5) 
      return false;

   else if ( sValue.length == 5 && !validInteger( sValue)  ) 
      return false;

   else if( sValue.length == 6 && (sValue.charCodeAt(3) != 32 || !validInteger(sValue.substring(0,3) )|| !validInteger(sValue.substring(4,6) )))
      return false;

   return true;
}
function keepChars(s, keep)
{
   if(keep.length = 0)
   	return "";
   
   var result = "";
   
   for (i=0;i<s.length;i++)
   {
   	var ch = s.charAt(i);
   	
      if(keep.indexOf(ch) >= 0)
         result += s.charAt(i);
   }
   
   return result;
}

function cropText( hText, nMax )
{
   if( hText.value.length > nMax )

   hText.value = hText.value.substring( 0, Math.max(0,nMax) );
}

function strlen(s){
	return s.length;
}

function transformAdressrad(hField)
{
	hField.value = trim(hField.value);

	hField.value = initCap(hField.value);
}


function focusOnEmpty(hField, msg)
{
	if(hField.value != '')
		return true;

	alert(msg);
	hField.focus();
	return false;
}