/*****************************************************************************************
* COPYRIGHT NOTICE                                                                       *
*                                                                                        *
* This code is (c) 2004 Dupoint AB. All rights reserved. Any unauthorized usage, copying *
* or reverse engineering is strictly prohibited.                                         *
*****************************************************************************************/


function isInteger(check) {
  var reg = /^\d{1,20}$/;
  if (reg.test(check)) {
    return true;
  }
  return false;
}

/**
* Checks if the given string is a numeric value. Accepts positive, negative
* values as well as floating point values. Floating point values must be 
* separated with . (not ,) to be accepted. String with only white space will
* return false.
* @param str  string
* @return boolean   True on success, false otherwise.
*/
function isNumeric(str) {
  return str.length ? !isNaN(str.replace(/\s/,"z")/1) : true
}

/**
* Checks if the given string is a valid Date.
* Understands dates in the formats:
* YYYY-MM-DD hh:mm:ss
* YYYY-MM-DD hh:mm
* YYYY-MM-DD
* @param str   string
* @return boolean   True on valid date, False otherwise.
*/
function checkDate(str) {
  var dateOK = true;
  
  var regFullDateTime = /^\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}$/;
  var regDateTime = /^\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}$/;
  var regDate = /^\d{4}\-\d{2}\-\d{2}$/;
  
  // Check for format YYYY-MM-DD hh:mm:ss
  if (regFullDateTime.test(str)) {
    var dArr = str.split("-");
    var DD = parseInt(dArr[2], 10);
    var MM = parseInt(dArr[1], 10)-1;
    var YYYY = parseInt(dArr[0], 10);
    
    //alert(parseInt(dArr[0]) + '-' + parseInt(dArr[1]) + '-' + parseInt(dArr[2]));
    //return false;
    var tempArr = str.split(" ");
    
    var tArr = tempArr[1].split(":");
    var hh = parseInt(tArr[0]);
    var mm = parseInt(tArr[1]);
    var ss = parseInt(tArr[2]);

    var d = new Date(YYYY, MM, DD, hh, mm, ss);
    if(d.getDate() != DD || d.getMonth() != MM || d.getFullYear() != YYYY || d.getHours() != hh || d.getMinutes() != mm || d.getSeconds() != ss) {
      var dateOK = false;
    }
  }
  // Check for format YYYY-MM-DD hh:mm
  else if (regDateTime.test(str)) {
    var dArr = str.split("-");
    var DD = parseInt(dArr[2]);
    var MM = parseInt(dArr[1])-1;
    var YYYY = parseInt(dArr[0]);
    
    var tempArr = str.split(" ");
    
    var tArr = tempArr[1].split(":");
    var hh = parseInt(tArr[0]);
    var mm = parseInt(tArr[1]);

    var d = new Date(YYYY, MM, DD, hh, mm);
    if(d.getDate() != DD || d.getMonth() != MM || d.getFullYear() != YYYY || d.getHours() != hh || d.getMinutes() != mm) {
      var dateOK = false;
    }
  }
  // Check for format YYYY-MM-DD
  else if (regDate.test(str)) {
    var dArr = str.split("-");
    
    var DD = dArr[2];
    var MM = (dArr[1] * 1) -1;
    var YYYY = dArr[0];

    var d = new Date(YYYY, MM, DD);
    if(d.getDate() != DD || d.getMonth() != MM || d.getFullYear() != YYYY) {
      var dateOK = false;
    }
  }
  else {
      var dateOK = false;
  }
  return dateOK;
}

/**
* Uses the same reg. exp as checkDate (and also performs a call to checkDate() 
* to validate the passed in string. Converts the given string into at javascript
* Date object and returns it. Will return null if string was not parsed to a Date
* properly. Accepts date string in the formats: YYYY-MM-DD hh:mm:ss, YYYY-MM-DD hh:mm
* and YYYY-MM-DD  
* @param  str string with date information.
* @return mixed Date object or null on failure.
* @see checkDate(str)
*/
function getDate(str) {
  var dateObj = null;
  
  if(!checkDate(str)){
    return null;
  }
  
  var regFullDateTime = /^\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}$/;
  var regDateTime = /^\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}$/;
  var regDate = /^\d{4}\-\d{2}\-\d{2}$/;
  
  // Check for format YYYY-MM-DD hh:mm:ss
  if (regFullDateTime.test(str)) {
    var dArr = str.split("-");
    if (dArr[1][0] == 0) { dArr[1] = dArr[1][1]; }
    if (dArr[2][0] == 0) { dArr[2] = dArr[2][1]; }
    var DD = parseInt(dArr[2], 10);
    var MM = parseInt(dArr[1], 10)-1;
    var YYYY = parseInt(dArr[0], 10);
    
    //alert(parseInt(dArr[0]) + '-' + parseInt(dArr[1]) + '-' + parseInt(dArr[2]));
    //return false;
    var tempArr = str.split(" ");
    
    var tArr = tempArr[1].split(":");
    var hh = parseInt(tArr[0], 10);
    var mm = parseInt(tArr[1], 10);
    var ss = parseInt(tArr[2], 10);

    dateObj = new Date(YYYY, MM, DD, hh, mm, ss);
  }
  // Check for format YYYY-MM-DD hh:mm
  else if (regDateTime.test(str)) {
    var dArr = str.split("-");
    if (dArr[1][0] == 0) { dArr[1] = dArr[1][1]; }
    if (dArr[2][0] == 0) { dArr[2] = dArr[2][1]; }
    var DD = parseInt(dArr[2], 10);
    var MM = parseInt(dArr[1], 10)-1;
    var YYYY = parseInt(dArr[0], 10);
    
    var tempArr = str.split(" ");
    
    var tArr = tempArr[1].split(":");
    var hh = parseInt(tArr[0], 10);
    var mm = parseInt(tArr[1], 10);

    dateObj = new Date(YYYY, MM, DD, hh, mm);
  }
  // Check for format YYYY-MM-DD
  else if (regDate.test(str)) {
    var dArr = str.split("-");
    if (dArr[1][0] == 0) { dArr[1] = dArr[1][1]; }
    if (dArr[2][0] == 0) { dArr[2] = dArr[2][1]; }
    var DD = parseInt(dArr[2], 10);
    var MM = parseInt(dArr[1], 10)-1;
    var YYYY = parseInt(dArr[0], 10);

    dateObj = new Date(YYYY, MM, DD);
    
  }

  return dateObj;
}

/**
* personalId class.
* Creates a personalId object from the passed in personal id string.
* Check the valid attribute to see if the personal id was a valid one.
* @param no string Personal id string
* @see checkPersonalId(check)
*/
function personalId(no){
	this.valid=false;
	if(!no.match(/^(\d{2})(\d{2})(\d{2})\-(\d{4})$/)){ return false; }
	this.now=new Date(); this.nowFullYear=this.now.getFullYear()+""; this.nowCentury=this.nowFullYear.substring(0,2); this.nowShortYear=this.nowFullYear.substring(2,4);
	this.year=RegExp.$1; this.month=RegExp.$2; this.day=RegExp.$3; this.controldigits=RegExp.$4;
	this.fullYear=(this.year*1<=this.nowShortYear*1)?(this.nowCentury+this.year)*1:((this.nowCentury*1-1)+this.year)*1;
	var months = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if(this.fullYear%400==0||this.fullYear%4==0&&this.fullYear%100!=0){ months[1]=29; }
	if(this.month*1>12||this.day*1>months[this.month*1-1]){ return false; }
	this.alldigits=this.year+this.month+this.day+this.controldigits;
	var nn="";
	for(var n=0;n<this.alldigits.length;n++){ nn+=((((n+1)%2)+1)*this.alldigits.substring(n,n+1)); }
	this.checksum=0;
	for(var n=0;n<nn.length;n++){ this.checksum+=nn.substring(n,n+1)*1; }
	this.valid=(this.checksum%10==0)?true:false;
	this.sex=parseInt(this.controldigits.substring(2,3))%2;
}

/**
* Controls if the passed in str is a valid personal id.
* @param check  string with personal id 
* @return boolean true on success, otherwise false
*/
function checkPersonalId(check){
  if(new personalId(check).valid){
    return true;
  }
  return false;
}