var months = new Array(12, 2);

months[0, 0] = "";
months[0, 1] = "";
months[1, 0] = "januari";
months[1, 1] = "31";
months[2, 0] = "februari";
months[2, 1] = "28";
months[3, 0] = "mars";
months[3, 1] = "31";
months[4, 0] = "april";
months[4, 1] = "30";
months[5, 0] = "maj";
months[5, 1] = "31";
months[6, 0] = "juni";
months[6, 1] = "31";
months[7, 0] = "juli";
months[7, 1] = "31";
months[8, 0] = "augusti";
months[8, 1] = "31";
months[9, 0] = "september";
months[9, 1] = "30";
months[10, 0] = "oktober";
months[10, 1] = "31";
months[11, 0] = "november";
months[11, 1] = "30";
months[12, 0] = "december";
months[12, 1] = "31";

function getfieldvalue(obj) {

  //alert("name=" + obj.name + ", type=" + obj.type);
  if (obj != null) {
    if (obj.type == "text" || obj.type == "hidden" || obj.type == "radio") {
      return obj.value;
    }
    else {
    	if (obj.selectedIndex == -1) {
    	  return "";
    	}
        else {
          return obj.options[obj.selectedIndex].value;
        }
    }
  }
}

function setfieldvalue(obj, value) {

  alert("name=" + obj.name + ", type=" + obj.type);
  if (obj != null) {
    if (obj.type == "text" || obj.type == "hidden" || obj.type == "radio") {
      obj.value = value;
    }
    else {
      //return obj.options[obj.selectedIndex].value;
      obj.value = value;
    }
  }
}

function fixnum(num) {
  var strNum = new String(num);
  var astr;
  var newstr;

  newstr = "";
  for (i=0; i<strNum.length; i++) {
    astr = strNum.charAt(i);
    if (astr==",") {
      newstr = newstr + ".";
    }
    else {
      newstr = newstr + astr;
    }
  }

  return newstr;
}

function computedate(adate, year, month, date) {
	var str = new String(adate.getYear());
	
	//alert('computedate: ' + adate.getYear() + "-" + adate.getMonth() + "-" + adate.getDate());
	newyear = adate.getYear() + year;
	
	newmonth = adate.getMonth() + month;
	
	if (newmonth > 11) {
		newyear = newyear + 1;
	}
	newmonth = newmonth % 12;
	//alert('newmonth=' + newmonth);
	
	newdate = adate.getDate() + date;
	//tempdate = newdate - months[newmonth, 1];
	tempdate = newdate - getnumberofdaysinmonth(newmonth + parseFloat(1));
	//alert('tempdate=' + tempdate);
	if (tempdate > 0) {
		newmonth = newmonth + 1;
		newdate = tempdate;
	}
	
	return new Date(newyear, newmonth, newdate);

}

function formatdate(adate) {
	var str = new String(adate.getYear());
	if (str.length == 3) {
		newyear = parseFloat(str) + 1900;
	}
	else {
		newyear = str;
	}
	
	newmonth = adate.getMonth() + parseFloat(1);
	if (newmonth<10)  { newmonth = "0" + newmonth; }
	newday = adate.getDate();
	if (newday<10)  { newday = "0" + newday; }
	
	return newyear + "-" + newmonth + "-" + newday;

}


function formatdate_text(adate) {
	var month_text;
	var newyear;

	
	var str = new String(adate.getYear());
	if (str.length == 3) {
		newyear = parseFloat(str) + 1900;
	}
	else {
		newyear = str;
	}

	//month_text = months[adate.getMonth(), 0];
	month_text = getmonthtext(adate.getMonth() + parseFloat(1));
	
	return "den " + adate.getDate() + " " + month_text + " " + newyear;		
}

function getmonthtext(month) {
	if (month == 1) {
		return "januari";
	}
	else if (month == 2) {
		return "februari";
	}
	else if (month == 3) {
		return "mars";
	}
	else if (month == 4) {
		return "april";
	}
	else if (month == 5) {
		return "maj";
	}
	else if (month == 6) {
		return "juni";
	}
	else if (month == 7) {
		return "juli";
	}
	else if (month == 8) {
		return "augusti";
	}
	else if (month == 9) {
		return "september";
	}
	else if (month == 10) {
		return "oktober";
	}
	else if (month == 11) {
		return "november";
	}
	else if (month == 12) {
		return "december";
	}
}

function getnumberofdaysinmonth(month) {
	if (month == 1) {
		return 31;
	}
	else if (month == 2) {
		return 28;
	}
	else if (month == 3) {
		return 31;
	}
	else if (month == 4) {
		return 30;
	}
	else if (month == 5) {
		return 31;
	}
	else if (month == 6) {
		return 30;
	}
	else if (month == 7) {
		return 31;
	}
	else if (month == 8) {
		return 31;
	}
	else if (month == 9) {
		return 30;
	}
	else if (month == 10) {
		return 31;
	}
	else if (month == 11) {
		return 30;
	}
	else if (month == 12) {
		return 31;
	}
}


function checkdate(adate) {
	var thedate = new String(adate);
	var year;
	var month;
	var day;
	var result = false;
	var okdate = null;

	if ((thedate.length == 8) || (thedate.length == 10)) {
		if (thedate.length == 8) {
			year = adate.substring(0,4);
			month = adate.substring(4,6);
			day = adate.substring(6,8);
		}
		else {
			year = adate.substring(0,4);
			month = adate.substring(5,7);
			day = adate.substring(8,10);
		}
		okdate = new Date(year, month - 1, day);
	}
	
	//alert(year + "-" + month + "-" + day);
	
	return okdate;
}

function setlastdayinmonth(adate) {
	var setdate = new Date(adate.getYear(), adate.getMonth(), getnumberofdaysinmonth(adate.getMonth() + parseFloat(1)));
	return setdate;
}

function checkpersonid(id) {
  var idstr = new String(id);
  var accsum = 0;
  var sumchar = 2;
  var i;

  if (idstr.length == 10) {
    for (i=0; i<(idstr.length-1); i++) {
      astr = idstr.charAt(i);
      accsum = parseInt(accsum) + partsum(astr * sumchar);

      if (sumchar==1) { sumchar = 2; } else { sumchar = 1; }
    }
  }
  else {
    return false;
  }

  var closest = 10 * Math.ceil(accsum/10);

  if ((closest - accsum) == idstr.charAt(9)) {
    return true;
  }
  else {
    return false;
  }
}

function partsum(num) {
  var strNum = new String(num);
  var astr;
  var newsum;
  var i;

  newsum = 0;
  for (i=0; i<strNum.length; i++) {
    astr = strNum.charAt(i);
    newsum = parseInt(newsum) + parseInt(astr);
  }

  return newsum;
}

function checkemail(email) {
  var strEmail = new String(email);
  var astr;

  newsum = 0;
  for (i=0; i<strEmail.length; i++) {
    astr = strEmail.charAt(i);
    if ((astr == "@") && (i>0) && (i!=(strEmail.length-1))) {
      return true;
    }
  }

  return false;
}
