
// Confirmation of deletion                                                            
   function ConfirmDelete() 
   {                                            
	  if (confirm("Er du sikker du vil slette denne rekord. " +        
		  "\nKlik OK for at slet eller Cancel.")) {                                   
			return true                                                    
	  } else {                                                             
			return false                                                   
	  }                                                                    
   } 

// Trim function
	function trim(str)
	{
	   return str.replace(/^\s*|\s*$/g,"");
	}

// Check if string is numeric
	function isNumeric(strString)
	{
	   var strValidChars = "0123456789";
	   var strChar;
	   var blnResult = true;
	
	   if (strString.length == 0) return true;
	
	   //  test strString consists of valid characters listed above
	   for (i = 0; i < strString.length && blnResult == true; i++)
	      {
	      strChar = strString.charAt(i);
	      if (strValidChars.indexOf(strChar) == -1)
	         {
	         blnResult = false;
	         }
	      }
	   return blnResult;
	}

// Check valid characters
	function isValidString(iStr, iValidChars)
	{
	   var strString = iStr;
       var strValidChars = iValidChars;
	   var blnResult = true;
	
	   if (iStr.length == 0) return true;
	
	   //  test strString consists of valid characters listed above
	   for (i = 0; i < strString.length && blnResult == true; i++)
	      {
	      strChar = strString.charAt(i);
	      if (strValidChars.indexOf(strChar) == -1)
	         {
	         blnResult = false;
	         }
	      }
	   return blnResult;
	}

// Check if string is integers
    function isInteger(s){
	    var i;
        for (i = 0; i < s.length; i++){   
            // Check that current character is number.
            var c = s.charAt(i);
            if (((c < "0") || (c > "9"))) return false;
        }
        // All characters are numbers.
        return true;
    }

// Check if a string contains only characters in the bag
    function stripCharsInBag(s, bag){
	    var i;
        var returnString = "";
        // Search through string's characters one by one.
        // If character is not in bag, append to returnString.
        for (i = 0; i < s.length; i++){   
            var c = s.charAt(i);
            if (bag.indexOf(c) == -1) returnString += c;
        }
        return returnString;
    }

// Date validation - Declaring valid date character, minimum year and maximum year
    var dtCh= "/";
    var minYear=1900;
    var maxYear=2100;

// Date Validation
    function isDate(dtStr){
	    var daysInMonth = DaysArray(12)
	    var pos1=dtStr.indexOf(dtCh)
	    var pos2=dtStr.indexOf(dtCh,pos1+1)
	    var strDay=dtStr.substring(0,pos1)
	    var strMonth=dtStr.substring(pos1+1,pos2)
	    var strYear=dtStr.substring(pos2+1)
	    strYr=strYear
	    if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	    if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	    for (var i = 1; i <= 3; i++) {
		    if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	    }
	    month=parseInt(strMonth)
	    day=parseInt(strDay)
	    year=parseInt(strYr)
	    if (pos1==-1 || pos2==-1){
		    alert("The date format should be : dd/mm/yyyy")
		    return false
	    }
	    if (strMonth.length<1 || month<1 || month>12){
		    alert("Please enter a valid month")
		    return false
	    }
	    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		    alert("Please enter a valid day")
		    return false
	    }
	    if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		    alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		    return false
	    }
	    if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		    alert("Please enter a valid date")
		    return false
	    }
        return true
    }

// Date Validation - Calculate the number of days in February given year 
    function daysInFebruary (year){
	    // February has 29 days in any year evenly divisible by four,
        // EXCEPT for centurial years which are not also divisible by 400.
        return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
    }

// Date Validation - returns an array of days in month
    function DaysArray(n) {
	    for (var i = 1; i <= n; i++) {
		    this[i] = 31
		    if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		    if (i==2) {this[i] = 29}
       } 
       return this
    }

// Check if Time is valid 
    function isTime(strTime)
	{
		tTime=strTime.split(":")
		tester1=/[^:0-9]/
		tester2=/[^0-9]/
		if (tester1.test(strTime) | strTime.length!=5 | strTime.indexOf(":")!=2)
 		{
 			alert("Time incorrect format or length")
			return false
 		}
		else if (tTime[0]>23 | tTime[1]>59 | tester2.test(tTime[0]) | tester2.test(tTime[1]))
 		{
 			alert("Time incorrect hour>23 min>59")
			return false
 		}
	}

// Check if string at valid CPR number
	function isCPR(strString)
	{
        //if (strString.length == 0) return true;
        if(!isNumeric(strString))
        {
            alert("CPR number ... only numeric characters are allowed ...");
            return false;
        }
        if(strString.length != 10)
        {
            alert("CPR number ... must be 10 characters long ...");
            return false;
        }
        if(!isDate1(strString.substring(0,6)))
        {
		    alert("An invalid date has been entered in the first 6 characters of the CPR number");
            return false;
        }
        return true;        
	}
	
	// Check date in format ddmmyy with no alerts
	function isDate1(iStr)
	{
	    var daysInMonth = DaysArray(12)
	    var strDay=iStr.substring(0,2)
	    var strMonth=iStr.substring(2,4)
	    var strYear=iStr.substring(4,6)

	    if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	    if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	    if (strYear.charAt(0)=="0" && strYear.length>1) strMonth=strYear.substring(1)

	    day=parseInt(strDay)
	    month=parseInt(strMonth)
	    year=parseInt(strYear)

	    if (strMonth.length<1 || month<1 || month>12){
		    //alert("Please enter a valid month")
		    return false
	    }
	    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		    //alert("Please enter a valid day")
		    return false
	    }
	    if (strYear.length != 2){
		    //alert("Year must be 2 digits long")
		    return false
	    }
        return true
	}

	// Validate the email address
	function CheckEmailAddress(str) 
	{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		 if (str.indexOf(" ")!=-1){
		    return false
		 }
 		 return true					
	}

