/* BASIC FUNCTIONS */

document.getElementsByClassName = function(clsName){
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function hentpoststed(hvorfra,hvor) {
	
	postnr=document.getElementById(hvorfra).value;
	if((postnr.length)==4) {
		posturl="/res/inc/postnr.php?postnr="+postnr;
		if (window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	
		if (xmlHttp) {
			xmlHttp.open("GET", posturl, false);
			xmlHttp.send(null);
			poststed = xmlHttp.responseText;
			document.getElementById(hvor).value=poststed;
		}
	}
}

/* DAY OF BIRTH */

//Script for å sjekke fødselsdato

var one_day = 1000*60*60*24;
var one_month = 1000*60*60*24*30;
var one_year = 1000*60*60*24*30*12;

function displayage(yr, mon, day, unit, decimal, round) {
	today = new Date();
	var pastdate = new Date(yr, mon-1, day);
	
	var countunit = unit;
	var decimals = decimal;
	var rounding = round;
	
	finalunit=(countunit=="days")? one_day : (countunit=="months")? one_month : one_year
	decimals=(decimals<=0)? 1 : decimals*10
	
	if (unit!="years"){
		if (rounding=="rounddown")
			document.write(Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
		else
			document.write(Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
	}
	else {
		yearspast=today.getFullYear()-yr-1
		tail=(today.getMonth()>mon-1 || today.getMonth()==mon-1 && today.getDate()>=day)? 1 : 0
		pastdate.setFullYear(today.getFullYear())
		pastdate2=new Date(today.getFullYear()-1, mon-1, day)
		tail=(tail==1)? tail+Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals : Math.floor((today.getTime()-pastdate2.getTime())/(finalunit)*decimals)/decimals
		// document.write(yearspast+tail+" "+countunit)
		return yearspast+tail;
	}
}

function check_dob() {

	if (1 < 0) {
	
	if (document.getElementById("dob_dag").value != "0" && document.getElementById("dob_mnd").value != "0" && document.getElementById("dob_aar").value != "0") {

	var checkstr = "0123456789";
	var Datevalue = "";
	var DateTemp = "";
	var seperator = ".";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
	
	err = 0;
	
	DateValue = document.getElementById("dob_dag").value + "." + document.getElementById("dob_mnd").value + "." + document.getElementById("dob_aar").value;
	
	/* Delete all chars except 0..9 */
	for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
		 DateTemp = DateTemp + DateValue.substr(i,1);
	  }
	}
	DateValue = DateTemp;
	/* Always change date to 8 digits - string*/
	/* if year is entered as 2-digit / always assume 19xx */
	if (DateValue.length == 6) {
	  DateValue = DateValue.substr(0,4) + '19' + DateValue.substr(4,2); }
	if (DateValue.length != 8) {
	  err = 19;}
	/* year is wrong if year = 0000 */
	year = DateValue.substr(4,4);
	if (year == 0) {
	  err = 20;
	}
	/* Validation of month*/
	month = DateValue.substr(2,2);
	if ((month < 1) || (month > 12)) {
	  err = 21;
	}
	/* Validation of day*/
	day = DateValue.substr(0,2);
	if (day < 1) {
	 err = 22;
	}
	/* Validation leap-year / february / day */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
	  leap = 1;
	}
	if ((month == 2) && (leap == 1) && (day > 29)) {
	  err = 23;
	}
	if ((month == 2) && (leap != 1) && (day > 28)) {
	  err = 24;
	}
	/* Validation of other months */
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
	  err = 25;
	}
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
	  err = 26;
	}
	/* if 00 ist entered, no error, deleting the entry */
	if ((day == 0) && (month == 0) && (year == 00)) {
	  err = 0; day = ""; month = ""; year = ""; seperator = "";
	}
	/* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
	if (err == 0) {
	  //DateField.value = day + seperator + month + seperator + year;
	dob_tips = document.getElementById("dob_tips");
	alder = displayage(year, month, day, "years", 0, "rounddown");

	dob_tips = document.getElementById("dob_tips");

	if (alder < 18) {
		dob_tips.innerHTML = "M&aring; v&aelig;re over 18 &aring;r - du er " + displayage(year, month, day, "years", 2, "rounddown") + " &aring;r";
		dob_tips.style.color = "red";
	}
	else {
		dob_tips.innerHTML = "OK - " + displayage(year, month, day, "years", 0, "rounddown") + " &aring;r";
		dob_tips.style.color = "black";
	}
	}
	/* Error-message if err != 0 */
	else {
	  // alert("Date is incorrect!");
	dob_tips = document.getElementById("dob_tips");
	dob_tips.innerHTML = "Ugyldig dato!";
	dob_tips.style.color = "red";
	}
	
	} else {
	dob_tips = document.getElementById("dob_tips");
		dob_tips.innerHTML = "*";
	}
	
	}
}
