/********************************************************************
   Validate 2.02                                           2002-07-24

   Collection of validate functions. HANDELSPLATSEN VERSION!

   // Markus Gemstad
   http://www.gemstad.com (references, samples etc)
********************************************************************/


/** Errormessages ***************************************************

  Here's all the error messages used in the validate functions. If 
  you want them in a different language all you have to do is change 
  the strings below. You should not remove the ".\n" or the extra 
  spaces (" ") in front or after the text. To get an english version 
  you comment these swedish ones and decomment the english strings 
  below and vice versa.                                            */

var sErrIsEmpty                 = " är inte ifyllt.\n";
var sErrOnBlur                  = "Fel!";
var sErrNotChoosen              = " är inte valt.\n";

var sErrValidateTextMinLength1  = " måste innehålla minst ";
var sErrValidateTextMinLength2  = " tecken.\n";
var sErrValidateTextMaxLength1  = " får innehålla max ";
var sErrValidateTextMaxLength2  = " tecken.\n";

var sErrValidateText2           = " får endast innehålla siffror och bokstäverna a-z.\n";

var sErrValidateNumber          = " får endast innehålla siffror.\n";
var sErrValidateNumberMin       = " måste var minst ";
var sErrValidateNumberMax       = " får vara max ";

var sErrValidateFileExt         = " tillåter bara filändelsen "
var sErrValidateTime            = " måste innehålla en tid i formatet hh:mm.\n";
var sErrValidateRegNr           = " måste innehålla registreringsnummer i formaten \"ABC123\" eller \"ABC 123\".\n";
var sErrValidateZipcode         = " måste innehålla en postnummer i formaten \"123 45\" eller \"12345\".\n";
var sErrValidateEmail           = " är ingen giltig e-mail adress.\n";
var sErrValidateDate            = " tillåter endast formaten ";

var sErrValidatePersNr          = " är inte ett giltigt personnummer.\n";

var sErrCompareDatesIsNot       = " är inte ";
var sErrCompareDatesSameAs      = "lika med";
var sErrCompareDatesLessOrEqual = "mindre än eller lika med";
var sErrCompareDatesMoreOrEqual = "större än eller lika med";
var sErrCompareDatesLessThan    = "mindre än";
var sErrCompareDatesMoreThan    = "större än";
var sErrCompareDatesDifferent   = "olikt";
/*******************************************************************/


/** Valid date formats **********************************************

  g_arrValidDateFormats:
    This will be used if the arrValidFormats parameter in validateDate()
    function is null. Useful if you want all calls on your site to use
    the same formats without having to send them in every time.
  g_iValidDateReturnFormat:
    Will be used if the iReturnFormat parameter in onblurDate() 
    function is null.
  g_sLastValidDateFormat:
    Used by the validateDate() and onblurDate() functions. Do not 
    thouch this one!                                               */

var g_arrValidDateFormats    = new Array("YYYY-MM-DD", "YYYYMMDD", "YYMMDD", "YY-MM-DD");
var g_iValidDateReturnFormat = 0;
var g_sLastValidDateFormat   = null;
/*******************************************************************/


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-02-14  Markus Gemstad          Created.
********************************************************************/
function validateText(sText, sName, bAllowEmpty, iMinLength, iMaxLength)
{
   var sErrorMsg = "";
   sText = trim(sText);
   
   if(!bAllowEmpty && sText == "") // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sText != "") // else if to short or to long string
   {
      if(iMinLength != null && sText.length < iMinLength)
         sErrorMsg += "- " + sName + sErrValidateTextMinLength1 + iMinLength + sErrValidateTextMinLength2;
      if(iMaxLength != null && sText.length > iMaxLength)
         sErrorMsg += "- " + sName + sErrValidateTextMaxLength1 + iMaxLength + sErrValidateTextMaxLength2;
   }

   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-02-04  Linda Sandström         Created.
********************************************************************/
function validateText2(sText, sName, bAllowEmpty, iMinLength, iMaxLength)
{
   var sErrorMsg = "";

   if(!bAllowEmpty && sText == "") // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sText != "") // else if to short or to long string or nonvalid characters
   {
      sErrorMsg = validateText(sText, sName, bAllowEmpty, iMinLength, iMaxLength);
      
      if(sErrorMsg == "")
      {
		   for(var i=0; i<sText.length; i++)
		   {
		      if(!(sText.charAt(i) <= "9" && sText.charAt(i) >= "0" || 
		           sText.charAt(i) <= "z" && sText.charAt(i) >= "a" || 
		           sText.charAt(i) <= "Z" && sText.charAt(i) >= "A"))
		      {
		         sErrorMsg += "- " + sName + sErrValidateText2;
		         break;
		      }
		   }
      }
   }
   
   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-02-14  Markus Gemstad          Created.
  2001-04-16  Markus Gemstad          Makes the check with regExp instead.
********************************************************************/
function validateNumber(sNumber, sName, bAllowEmpty, iMinValue, iMaxValue, bAllowNegative)
{
   var sErrorMsg = "";
   sNumber = trim(sNumber);

   if(!bAllowEmpty && sNumber == "") // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sNumber != "") // else if number - to low or to high
   {
      if(bAllowNegative && sNumber.charAt(0) == "-")
         sNumber = sNumber.slice(1);
      var objRegExp   = new RegExp("[^0-9]", "g"); // Search for everything except 0-9
      var iInvalidPos = sNumber.search(objRegExp);

      if(iInvalidPos != -1) // Only number characters
      {
         sErrorMsg += "- " + sName + sErrValidateNumber;
      }
		else
		{
		   if(iMinValue != null && eval(sNumber) < iMinValue)
		      sErrorMsg += "- " + sName + sErrValidateNumberMin + iMinValue + ".\n";
		   if(iMaxValue != null && eval(sNumber) > iMaxValue)
		      sErrorMsg += "- " + sName + sErrValidateNumberMax + iMaxValue + ".\n";
		}
   }
   
   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2000-09-14  Markus Gemstad          Created.
  2001-02-14  Markus Gemstad          Added parameter bAllowEmpty.
********************************************************************/
function validateFileExt(sFilePath, arrExtensions, sName, bAllowEmpty)
{
   var sErrorMsg = "";
   sFilePath = trim(sFilePath);
   
   if(!bAllowEmpty && sFilePath == "") // If empty
   {
      sErrorMsg = "- " + sName + sErrNotChoosen;
   }
   else if(sFilePath != "") // Check fileextension
   {
      var sThisExt = sFilePath.slice(sFilePath.lastIndexOf(".") + 1);
      var bFound   = false;
      var sAllExt  = "";

      for(var i = 0; i < arrExtensions.length; i++)
      {
         if(arrExtensions[i].toLowerCase() == sThisExt.toLowerCase())
            bFound = true;

         sAllExt += "\"" + arrExtensions[i] + "\", ";
         if(i == arrExtensions.length-1)
            sAllExt = sAllExt.slice(0,sAllExt.length-2);
      }
      if(!bFound)
         sErrorMsg = "- " + sName + sErrValidateFileExt + sAllExt + ".\n";
   }

   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-04-21  Markus Gemstad          Created.
********************************************************************/
function onblurDate(sDate, arrValidFormats, iReturnFormat)
{
   var sFormated = "";
   sDate = trim(sDate);

   if(validateDate(sDate, "Datum", arrValidFormats, true) == "" && sDate.length > 0)
   {
      var rgExp, sValidDate, sReturnDate;
      
      // Get the format of the sDate - global variable that validateDate saves in
      sValidDate = g_sLastValidDateFormat;

      var iYearLen  = (sValidDate.lastIndexOf("Y") != -1) ? sValidDate.lastIndexOf("Y") - sValidDate.indexOf("Y") + 1 : 0;
      var iMonthLen = (sValidDate.lastIndexOf("M") != -1) ? sValidDate.lastIndexOf("M") - sValidDate.indexOf("M") + 1 : 0;
      var iDateLen  = (sValidDate.lastIndexOf("D") != -1) ? sValidDate.lastIndexOf("D") - sValidDate.indexOf("D") + 1 : 0;

      var iYear  = sDate.slice(sValidDate.indexOf("Y"), sValidDate.indexOf("Y") + iYearLen);
      var iMonth = sDate.slice(sValidDate.indexOf("M"), sValidDate.indexOf("M") + iMonthLen);
      var iDate  = sDate.slice(sValidDate.indexOf("D"), sValidDate.indexOf("D") + iDateLen);

      // If parameters are nulluse the global ones
      if(arrValidFormats == null)
         arrValidFormats = g_arrValidDateFormats;
      if(iReturnFormat == null)
         iReturnFormat = g_iValidDateReturnFormat;

      // Get the format to convert to
      sReturnDate = arrValidFormats[iReturnFormat];

      var iYearLen2  = (sReturnDate.lastIndexOf("Y") != -1) ? sReturnDate.lastIndexOf("Y") - sReturnDate.indexOf("Y") + 1 : 0;
      var iMonthLen2 = (sReturnDate.lastIndexOf("M") != -1) ? sReturnDate.lastIndexOf("M") - sReturnDate.indexOf("M") + 1 : 0;
      var iDateLen2  = (sReturnDate.lastIndexOf("D") != -1) ? sReturnDate.lastIndexOf("D") - sReturnDate.indexOf("D") + 1 : 0;

      if(iYearLen == 2 && iYearLen2 == 4) // Fix year if only two numbers
      {
         if(iYear > 50)
            iYear = "19" + iYear;
         else
            iYear = "20" + iYear;
      }

      // If some of the values don't exist, use todays...
      if(iYear.length == 0) // Fix year if only two numbers
      {
         iYear = new Date().getUTCFullYear() + "";
         iYearLen = iYear.length;
      }
      if(iMonth.length == 0) // Fix year if only two numbers
      {
         iMonth = (new Date().getUTCMonth() + 1) + "";
         if(iMonth.length == 1)
            iMonth = "0" + iMonth;
         iMonthLen = iMonth.length;
      }
      if(iDate.length == 0) // Fix year if only two numbers
      {
         iDate = new Date().getUTCDate() + "";
         if(iDate.length == 1)
            iDate = "0" + iDate;
         iDateLen = iDate.length;
      }

      // Replace Y's, M's and D's with the sent in year, month, date
      if(iYearLen2)
      {
         if((iYearLen - iYearLen2) >= 0)
            iYear = iYear.slice(iYearLen - iYearLen2);
         rgExp = new RegExp("Y{" + iYearLen2 + "}");
         sReturnDate = sReturnDate.replace(rgExp, iYear);
      }
      if(iMonthLen2)
      {
         if((iMonthLen - iMonthLen2) >= 0)
            iMonth = iMonth.slice(iMonthLen - iMonthLen2);
         rgExp = new RegExp("M{" + iMonthLen2 + "}");
         sReturnDate = sReturnDate.replace(rgExp, iMonth.slice(iMonthLen - iMonthLen2));
      }
      if(iDateLen2)
      {
         if((iDateLen - iDateLen2) >= 0)
            iDate = iDate.slice(iDateLen - iDateLen2);
         rgExp = new RegExp("D{" + iDateLen2 + "}");
         sReturnDate = sReturnDate.replace(rgExp, iDate.slice(iDateLen - iDateLen2));
      }
      
      sFormated = sReturnDate;
   }
   else if(sDate.length > 0)
      sFormated = sErrOnBlur;

   return sFormated;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-04-12  Markus Gemstad          Created.
********************************************************************/
function validateDate(sDate, sName, arrValidFormats, bAllowEmpty)
{
   var sErrorMsg   = "";
   var bValidFound = false;
   var bEmpty      = false;

   sDate = trim(sDate);
   
   if(arrValidFormats == null)
      arrValidFormats = g_arrValidDateFormats;

   if(!bAllowEmpty && sDate == "") // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sDate != "") // Check date
   {
      var sAllDates = "";

      for(var i = 0; i < arrValidFormats.length; i++) // Go through all valid formats
      {
         var sValidDate = arrValidFormats[i];
         var bThisValid = true;

         if(sDate.length == sValidDate.length) // Only if correct length.
         {
            var iYear, iMonth, iDate, rgExp;
            var iYearLen   = (sValidDate.lastIndexOf("Y") != -1) ? sValidDate.lastIndexOf("Y") - sValidDate.indexOf("Y") + 1 : 0;
            var iMonthLen  = (sValidDate.lastIndexOf("M") != -1) ? sValidDate.lastIndexOf("M") - sValidDate.indexOf("M") + 1 : 0;
            var iDateLen   = (sValidDate.lastIndexOf("D") != -1) ? sValidDate.lastIndexOf("D") - sValidDate.indexOf("D") + 1 : 0;

            if(iYearLen != 0) // Check year
            {
               iYear = sDate.slice(sValidDate.indexOf("Y"), sValidDate.indexOf("Y") + iYearLen);
               rgExp = new RegExp("[0-9]{" + iYearLen + "}");
               if(iYear.search(rgExp) == -1) // If a valid year number
                  bThisValid = false;
            }

            if(iMonthLen != 0 && bThisValid) // Check month
            {
               iMonth = sDate.slice(sValidDate.indexOf("M"), sValidDate.indexOf("M") + iMonthLen);
               rgExp = new RegExp("[0-9]{" + iMonthLen + "}");
               if(iMonth.search(rgExp) > -1)
               {
                  // Check if from 1 to 12
                  if(iMonth < 1 || iMonth > 12)
                     bThisValid = false;
	            }
	            else
	               bThisValid = false;
            }

            if(iDateLen != 0 && bThisValid) // Check date
            {
               iDate = sDate.slice(sValidDate.indexOf("D"), sValidDate.indexOf("D") + iDateLen);
               rgExp = new RegExp("[0-9]{" + iDateLen + "}");
               if(iDate.search(rgExp) > -1)
               {
                  if(iDate < 1 || iDate > 31)
                     bThisValid = false;

	               // Check if correct nr of days for the month (months with 30 days)
	               if(iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11)
	               {
	               	if(iDate == 31)
	               	   bThisValid = false;
	               }

	               if(iMonth == 2) // Check february
	               {
	                  if(iDate > 29) // If more than 29 days
	                     bThisValid = false;

                     if(iYear) // Check leap year (if a year exist)
                     {
	                     if(iDate == 29 && ((iYear / 4) != parseInt(iYear / 4)))
	                        bThisValid = false;
	                  }
	               }
	            }
	            else
	               bThisValid = false;
            }
            
            if(bThisValid) // Check separators
            {
               rgExp  = new RegExp("[^YMD]", "g"); // Search for everything except YMD
               rgExp2 = new RegExp("[^0-9]", "g"); // Search for everything except 0-9
               
               var arrMatches  = sValidDate.match(rgExp);
               var arrMatches2 = sDate.match(rgExp2);
               
               if(arrMatches != null)
               {
                  for(var i2 = 0; i2 < arrMatches.length; i2++)
                  {
                     if(arrMatches2 == null || 
                        arrMatches2.length < arrMatches.length ||
                        arrMatches[i2] != arrMatches2[i2])
                        bThisValid = false;
                  }
               }
            }
         }
         else
            bThisValid = false;

         if(bThisValid)
         {
            bValidFound = true;
            g_sLastValidDateFormat = sValidDate;
         }

         // Create string that might have to be presented in error message.
         sAllDates += "\"" + arrValidFormats[i] + "\", ";
         if(i == arrValidFormats.length-1)
            sAllDates = sAllDates.slice(0, sAllDates.length-2);
      }

      if(!bValidFound) // No valid at all found
         sErrorMsg = "- " + sName + sErrValidateDate + sAllDates + ".\n";
   }
   
   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-01-10  Markus Gemstad          Created.
********************************************************************/
function compareDates(sDate1, sDate1Name, sMethod, sDate2, sDate2Name)
{
   var sErrorMsg = "";
   var sMethodName = "";
   switch(sMethod)
   {
      case "==" : sMethodName = sErrCompareDatesSameAs; break;
      case "<=" : sMethodName = sErrCompareDatesLessOrEqual; break;
      case ">=" : sMethodName = sErrCompareDatesMoreOrEqual; break;
      case "<"  : sMethodName = sErrCompareDatesLessThan; break;
      case ">"  : sMethodName = sErrCompareDatesMoreThan; break;
      case "!=" : sMethodName = sErrCompareDatesDifferent; break;
   }

   sErrorMsg = "- " + sDate1Name + sErrCompareDatesIsNot + sMethodName + " " + sDate2Name + ".\n";

   if(sDate1.length == 10 && sDate2.length == 10)
   {
      sDate1 = "" + sDate1.substr(0,4) + sDate1.substr(5,2) + sDate1.substr(8,2);
      sDate2 = "" + sDate2.substr(0,4) + sDate2.substr(5,2) + sDate2.substr(8,2);
      
      if(eval(sDate1 + sMethod + sDate2))
         sErrorMsg = "";
   }
   else
      sErrorMsg = "";

   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-04-21  Markus Gemstad          Created.
********************************************************************/
function onblurTime(sTime)
{
   var sFormated = "";
   sTime = trim(sTime);
   
   if(validateTime(sTime, "Tid", true) == "" && sTime.length > 0)
      sFormated = sTime;
   else if(sTime.length > 0)
      sFormated = sErrOnBlur;
   
   return sFormated;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-02-14  Markus Gemstad          Created.
  2001-04-21  Markus Gemstad          Removed bInline parameter. Use onblurTime() instead.
********************************************************************/
function validateTime(sTime, sName, bAllowEmpty)
{
   var sErrorMsg = "";
   var objRegExp;

   sTime = trim(sTime);

   if(!bAllowEmpty && sTime.length == 0) // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sTime.length > 0) // else check regnr
   {
      if(sTime.length == 5)
      {
         iHour    = eval(sTime.substr(0,2));
         iMinutes = eval(sTime.substr(3,2));
         sColon   = sTime.substr(2,1);

         if((iHour    >= 0 && iHour    <= 23) &&
            (iMinutes >= 0 && iMinutes <= 59) &&
            (sColon   == ":")) // Okay, do nothing
         {
            ;
         }
         else
            sErrorMsg = "- " + sName + sErrValidateTime;
      }
      else
         sErrorMsg = "- " + sName + sErrValidateTime;
   }

   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-04-22  Markus Gemstad          Created.
********************************************************************/
function onblurPersNr(sPersNr, iReturnFormat)
{
   // Format          Length  iReturnFormat
   // =====================================
   // YYYYMMDD-NNNN   13      1
   // YYYYMMDDNNNN    12      2
   // YYMMDD-NNNN     11      3
   // YYMMDDNNNN      10      4

   var sFormated  = "";
   sPersNr        = trim(sPersNr);

   if(validatePersNr(sPersNr, "PersNr", true) == "" && sPersNr.length > 0)
   {
      if(sPersNr.length == 13 || sPersNr.length == 12) // Format to YYYYMMDDNNNN
      {
         var sCheckNr = (sPersNr.length == 13) ? sPersNr.slice(9) : sPersNr.slice(8);
         sPersNr      = sPersNr.slice(0,8) + sCheckNr;
      }
      else if(sPersNr.length == 11 || sPersNr.length == 10) // Format to YYYYMMDDNNNN
      {
         var sCheckNr = (sPersNr.length == 11) ? sPersNr.slice(7) : sPersNr.slice(6);
         var sYearNow = new String(new Date().getFullYear()).slice(2,4);
         var sYear    = (sPersNr.slice(0,2) < sYearNow) ? "20" : "19";
         sPersNr      = sYear + sPersNr.slice(0,6) + sCheckNr;
      }

      if(iReturnFormat == 1)
         sFormated = sPersNr.slice(0,8) + "-" + sPersNr.slice(8);
      else if(iReturnFormat == 2)
         sFormated = sPersNr;
      else if(iReturnFormat == 3)
         sFormated = sPersNr.slice(2,8) + "-" + sPersNr.slice(8);
      else if(iReturnFormat == 3)
         sFormated = sPersNr.slice(2,8) + sPersNr.slice(8);
   }
   else if(sPersNr.length > 0)
      sFormated = sErrOnBlur;
   
   return sFormated;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-02-14  Markus Gemstad          Created from base made by Christian Halvarsson.
  2001-04-22  Markus Gemstad          New version, checks date as well as
                                      the controlnumber.
********************************************************************/
function validatePersNr(sPersNr, sName, bAllowEmpty)
{
   // Valid format    Length
   // ======================
   // YYYYMMDD-NNNN   13
   // YYYYMMDDNNNN    12
   // YYMMDD-NNNN     11
   // YYMMDDNNNN      10

   var sErrorMsg  = "";
   var bValidDate = false;
   sPersNr = trim(sPersNr);

   if(!bAllowEmpty && sPersNr == "") // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sPersNr != "") // Check persnr
   {
      if((sPersNr.length == 13 || sPersNr.length == 12) &&
          validateDate(sPersNr.slice(0,8), "PersNr", new Array("YYYYMMDD"), true) == "")
      {
         var sCheckNr = (sPersNr.length == 13) ? sPersNr.slice(9) : sPersNr.slice(8);
         sPersNr      = sPersNr.slice(2,8) + sCheckNr;
         bValidDate   = true;
      }
      else if((sPersNr.length == 11 || sPersNr.length == 10) &&
               validateDate(sPersNr.slice(0,6), "PersNr", new Array("YYMMDD"), true) == "")
      {
         var sCheckNr = (sPersNr.length == 11) ? sPersNr.slice(7) : sPersNr.slice(6);
         sPersNr      = sPersNr.slice(0,6) + sCheckNr;
         bValidDate   = true;
      }
      else // Met none of the requirements above - invalid
      {
         sErrorMsg = "- " + sName + sErrValidatePersNr;
      }

      if(bValidDate) // If datecheck above was successful, this check was made by Christian Halvarsson
      {
	      var sSumma = 0;
	      var sTempNr;
	      var iRest;

	      // Kollar kontrollsiffra (fyra sista), använder YYMMDDNNNN
	      for(i=0;i<=8;i++)
	      {
	      	if(i % 2 == 0)
	      	{
	      		//jämnt index
	      		sTempNr = parseInt(sPersNr.charAt(i)) * 2;

	      		//om tvåsiffrigt addera ihop båda siffrorna
	      		if(sTempNr >= 10)
	      		{
	      			iRest = sTempNr % 10;
	      			sTempNr = 1 + iRest;
	      		}
	      	}
	      	else
	      	{
	      		//Ojämnt index
	      		sTempNr = parseInt(sPersNr.charAt(i)) * 1;
	      	}

	      	sSumma += sTempNr;
	      }
	      sSumma += parseInt(sPersNr.charAt(9));

	      //om int jämt tiotal så stämmer ej kontrollsiffran.
	      if(sSumma %10 != 0)
            sErrorMsg = "- " + sName + sErrValidatePersNr;
      }
   }

	return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-04-21  Markus Gemstad          Created.
********************************************************************/
function onblurRegNr(sRegNr, iReturnFormat)
{
   // Format     Length  iReturnFormat
   // ==================================
   // ABC123     6       1
   // ABC 123    7       2

   var sFormated = "";
   sRegNr = trim(sRegNr);
   
   if(validateRegNr(sRegNr, "RegNr", true) == "" && sRegNr.length > 0)
   {
      // Format to ABC123
      if(sRegNr.length == 7)
         sFormated = sRegNr.slice(0,3).toUpperCase() + sRegNr.slice(4,7);
      else if(sRegNr.length == 6)
         sFormated = sRegNr.slice(0,3).toUpperCase() + sRegNr.slice(3,6);
      
      if(iReturnFormat == 2) // Reformat to ABC 123
         sFormated = sFormated.slice(0,3) + " " + sFormated.slice(3,6);
   }
   else if(sRegNr.length > 0)
      sFormated = sErrOnBlur;
   
   return sFormated;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-02-14  Markus Gemstad          Created.
  2001-04-16  Markus Gemstad          Makes the check with regExp instead.
  2001-04-21  Markus Gemstad          Removed bInline parameter. Use onblurRegNr() instead.
********************************************************************/
function validateRegNr(sRegNr, sName, bAllowEmpty)
{
   // Valid format    Length
   // ======================
   // ABC123          6
   // ABC 123         7

   var sErrorMsg = "";
   var objRegExp;

   sRegNr = trim(sRegNr);

   if(!bAllowEmpty && sRegNr.length == 0) // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sRegNr.length > 0) // else check regnr
   {
      if(sRegNr.length == 6)
      {
         objRegExp = new RegExp("[A-Za-z]{3}[0-9]{3}");
         if(sRegNr.search(objRegExp) == -1) // If invalid
            sErrorMsg = "- " + sName + sErrValidateRegNr;
      }
      else if(sRegNr.length == 7)
      {
         objRegExp = new RegExp("[A-Za-z]{3} [0-9]{3}");
         if(sRegNr.search(objRegExp) == -1) // If invalid
            sErrorMsg = "- " + sName + sErrValidateRegNr;
      }
      else // If another length
         sErrorMsg = "- " + sName + sErrValidateRegNr;
   }

   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-04-21  Markus Gemstad          Created.
********************************************************************/
function onblurZipcode(sZipcode, iReturnFormat)
{
   // Format     Length  iReturnFormat
   // ==================================
   // 12345      5       1
   // 123 45     6       2

   var sFormated = "";
   sZipcode = trim(sZipcode);
   
   if(validateZipcode(sZipcode, "Zipcode", true) == "" && sZipcode.length > 0)
   {
      // Format to 123 45
      if(sZipcode.length == 5 && iReturnFormat == 2)
         sFormated = sZipcode.slice(0,3) + " " + sZipcode.slice(3,5);
      else
         sFormated = sZipcode;
   }
   else if(sZipcode.length > 0)
      sFormated = sErrOnBlur;
   
   return sFormated;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-04-21  Markus Gemstad          Created.
********************************************************************/
function validateZipcode(sZipcode, sName, bAllowEmpty)
{
   // Valid format    Length
   // ======================
   // 12345           5
   // 123 45          6

   var sErrorMsg = "";
   var objRegExp;

   sZipcode = trim(sZipcode);

   if(!bAllowEmpty && sZipcode.length == 0) // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sZipcode.length > 0) // else check zipcode
   {
      if(sZipcode.length == 5) // 12345
      {
         objRegExp = new RegExp("[0-9]{5}");
         if(sZipcode.search(objRegExp) == -1) // If invalid
            sErrorMsg = "- " + sName + sErrValidateZipcode;
      }
      else if(sZipcode.length == 6) // 123 45
      {
         objRegExp = new RegExp("[0-9]{3} [0-9]{2}");
         if(sZipcode.search(objRegExp) == -1) // If invalid
            sErrorMsg = "- " + sName + sErrValidateZipcode;
      }
      else // If another length
         sErrorMsg = "- " + sName + sErrValidateZipcode;
   }

   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  ????-??-??  Paolo Wales             Created.
  2001-02-14  Markus Gemstad          Added sName, bAllowEmpty and string return.
********************************************************************/
function validateEmail(sEmail, sName, bAllowEmpty)
{
   /* Written by Paolo Wales (paolo@taize.fr) starting on a basis by Samrat Sen.

   Notes:
   
   'exclude' checks 5 conditions:
   
   a) characters that should not be in the address
   b) characters that should not be at the start
   c) & d) characters that shouldn't be together
   e) there's not more than one '@'
   
   'check' checks there's at least one '@', later followed by at least one '.'
   'checkend' checks the address ends with a period followed by 2 or 3 alpha characters.
   N.B. Javascript 1.2 only works with version 4 browsers and higher. */
   
   var exclude   =/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
   var check     =/@[\w\-]+\./;
   var checkend  =/\.[a-zA-Z]{2,3}$/;   
   var sErrorMsg = "";
   sEmail = trim(sEmail);
 
   if(!bAllowEmpty && sEmail == "")
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sEmail != "")
   {
      if(((sEmail.search(exclude) != -1) || 
          (sEmail.search(check)) == -1) || 
          (sEmail.search(checkend) == -1))
      {
         sErrorMsg = "- " + sName + sErrValidateEmail;
      }
   }

   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-01-18  Linda Sandström         Created.
********************************************************************/
function validateSelect(oFormObj, sName)
{
   var sErrorMsg = "";

   if(oFormObj.options[oFormObj.selectedIndex].value == "0" ||
      oFormObj.options[oFormObj.selectedIndex].value == "")
   {
      sErrorMsg = "- " + sName + sErrNotChoosen;
   }

   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-01-18  Linda Sandström         Created.
  2002-06-13  Markus Gemstad          Now works with single radiobuttons.
********************************************************************/
function validateRadio(oFormObj, sName)
{
   var sErrorMsg = "- " + sName + sErrNotChoosen;
   if(oFormObj.length == null && oFormObj.checked)
      sErrorMsg = "";
   else
   {
  	   for(i=0;i<oFormObj.length;i++)
	   {
         if(oFormObj[i].checked)
         {
            sErrorMsg = "";
            break;
         }
	   }
   }
   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  2001-01-18  Linda Sandström         Created.
********************************************************************/
function validateCheckbox(oFormObj, sName)
{
   var sErrorMsg = "";
  
	if(!oFormObj.checked)
		sErrorMsg = "- " + sName + sErrNotChoosen;

   return sErrorMsg;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  ????-??-??  Christian Halvarsson    Created.
********************************************************************/
function ltrim(sValue)
{
   while(1)
   {
      if(sValue.substring(0, 1) != " ")
         break;
      sValue = sValue.substring(1, sValue.length);
   }
   return sValue;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  ????-??-??  Christian Halvarsson    Created.
********************************************************************/
function rtrim(sValue)
{
   while(1)
   {
      if(sValue.substring(sValue.length - 1, sValue.length) != " ")
         break;
      sValue = sValue.substring(0, sValue.length - 1);
   }
   return sValue;
}


/** History *********************************************************
  Date:       Name:                   Comment:
  ????-??-??  Christian Halvarsson    Created.
********************************************************************/
function trim(sValue)
{
   var sTemp = ltrim(sValue);
   return rtrim(sTemp);
}