/**
*	javascript.js
*	
*   This is the default javascript functions of CoreTreks Site Base for CorePublish frontends
*	
*	Please DO NOT CHANGE this file. 
*	To add your own functions, put them in another file
*	This makes sure you can update this file (javascript.js)
*	from a newer distribution without worrying about keeping your changes.
*
*	@author Arve Skjørestad
*	
*/

var timervar = null; 

function printArticle( artUrl ){
    behind = window.open(artUrl ,'printwin','height=650,width=810,status=yes,toolbar=yes,directories=no,menubar=no,location=no,resizable=no,scrollbars=yes');
}

function getContentAreaHeight() {
    if (document.all) {
        var ret  =  document.all["menuheighholder"].height;
    } else if (document.getElementById) {
		var ret =  document.getElementById("menuheighholder").height;
    }

    return  ret;

}       

function getAbsolutePos(el){
	for (var lx=0,ly=0;el!=null;
		lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
	return {x:lx,y:ly}
}


/**
 * Sets the active stylesheet for the page.
 * 
 * 
 */
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
       if(a.getAttribute("rel").indexOf("style") != -1
          && a.getAttribute("title")) {
           a.disabled = true;
           if(a.getAttribute("title") == title) {
               a.disabled = false;
           }
       }
   }
}

function getActiveStyleSheet() {
    var i, a;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1
           && a.getAttribute("title")
           && !a.disabled) { 
            return a.getAttribute("title");
        }
    }
    return null;
}

var debugwin;
var debugwinopened = false;
function debug(str) {
    //return;
    // alert(str); return ;
	if (debugwinopened == false || !debugwin) {
        debugwin = window.open('about:blank','debug_window','width=1000,height=700, toolbars=no,menu=no,scrollbars=yes');
        debugwin.document.write("<h3>CorePublish Javascript Debug</h3>");
        debugwinopened = true;
    }
	debugwin.document.write( str );
	debugwin.focus();
    // make the debug win go away after a while..
    setTimeout("self.focus()",2000);
	// alert(str);
}

/**
*   Function displays the metods and properties of an javascript object
*   @param Object inarray - The array to serialize.
*/
function jsdebug(inarray, level) {
    var result = '';
    var sep = '';

    if (level > 5) {
        return;
    }

    if(inarray!=null) {
        for(var key in inarray) {
            
            if (inarray[key] == null) {
                continue;
            }

            result = "<ul>" +  typeof inarray[key];
            result += ' [' + key + '] => ';

            try {
                tmp = inarray[key].toString();
                result += tmp.substring(0,90) + "";
            } catch (e) {
                result += tmp + "";
            } 

            debug(result);
                
            if (typeof inarray[key] == "object" || typeof inarray[key] == "function") {
                jsdebug(inarray[key],level+1);
            }

            debug("</ul>");
            /*
            if(typeof inarray[key] == 'object') {
                result += '[object]';
            } else {
                result += inarray[key].toString();
            }     */



            /*if (result.length > 1000) {
                alert(result + " (continues >>) ");
                result = "";
            } */

        } 
    } else {
        result = "[null]";
    }
    // alert(result);
}

function dateselect(category,url){
	/*var month = document.getElementById('month').value;
	var year = document.getElementById('year').value;
	if(url.indexOf('month')>0){
		url = url.substring(0,url.indexOf('month')-1);
		window.location = url+"&month="+month+"&year="+year;
	} else {
		window.location = url+"&month="+month+"&year="+year;
	}*/


	var month = document.dateselector.month.value;
	var year = document.dateselector.year.value;
	if(url.indexOf('month')>0){
		url = url.substring(0,url.indexOf('month')-1);
		window.location = url+"&month="+month+"&year="+year;
	} else {
		window.location = url+"&month="+month+"&year="+year;
	}
}

// Funksjoner for SMS varsling (Hans Kr. Aag)
function nyBestilling()
{
    window.open('http://seksjon.nhosp.no/tjeneste/smsVarsling/bestilling.php?radnr=67783', '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=yes, width=600, height=300');
}

function setFocus()
{
    document.getElementById("navn").focus();
}

function switchdisplay(stringid, activeid, amounts){
	var i = 0;
	for(i=0;i<=amounts-1;i++){
		if(document.getElementById(stringid+i)){
			if(i == activeid){
				document.getElementById(stringid+i).style.display='block';
			} else {
				document.getElementById(stringid+i).style.display='none';
			}
		}
	}
}

function switchcssclass(stringid, activeid, amounts){
	var i = 0;
	for(i=0;i<=amounts-1;i++){
		if(document.getElementById(stringid+i)){
			if(i == activeid){
			document.getElementById(stringid+i).className=' active';
			} else {
				document.getElementById(stringid+i).className='';
			}
		}
		
		
	}
}


function formatDato(id, value)
{
	if (value.length > 0)
	{
		if (value.length == 6 )
		{
			if (value.indexOf(".") == -1)
			{
				value = value.substr(0,2) + "." + value.substr(2,2) + ".19" + value.substr(4,2);
			}
		}
		else
		{
			if (value.indexOf(".") == -1)
			{
				value = value.substr(0,2) + "." + value.substr(2,2) + "." + value.substr(4,4);
			}
		}
		
		// Foretar validering ...
		if(!validerDato(value.substr(0,2), value.substr(3,2), value.substr(6,4)))
		{
			alert("Ugyldig dato eller feil format.\n\nKorrekt datoformat er dd.mm.yyyy;");
			document.getElementById(id).focus();
		}
		else
		{
			document.getElementById(id).value = value;
		}
	}
}



/** Funksjon for å validere dato. */
function validerDato(dag, maaned, aar)
{
	var dato = new Date();
	dato.setTime(Date.parse(new String(aar + "/" + maaned + "/" + dag)));
	if (dag == dato.getDate() && (maaned - 1) == dato.getMonth() && aar == dato.getFullYear())
		return true;
	else
		return false;
}



/** Funksjon som kopierer valget i øverste nedtrekksliste ned på deltagerne med
    samme verdi som nedtrekkslisten opprinnelig hadde. */
function fellesSelect(id, antall)
{
	var forrigeId = "forrigeV" + id.substring(1, 9) + "0";
	var gmlValg = document.getElementById(forrigeId).value
	verdiEndret(id);
	var nyttValg = document.getElementById(id).value;
	for (var i = 1; i <= antall; i++)
	{
		var newId = id.substring(0, 9) + (i + 1);
		if (document.getElementById(newId).value == gmlValg)
		{
			document.getElementById(newId).value = nyttValg;
			verdiEndret(newId);
		}
	}
	document.getElementById(forrigeId).value = nyttValg;
}


/**	Setter hidden-feltet 'endret_X' = J for å flagge at forekomsten er endret på. */
function verdiEndret(name)
{
	document.getElementById("endret" + name.substr(name.lastIndexOf('_'))).value = "J";
}


/** Validerer epostadresse. */
function sjekkEpost(value)
{
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (value.length == 0 || filter.test(value))
	{
		testresults = true;
	}
	else
	{
		alert("Vennligst oppgi en korrekt epostadresse.");
		testresults = false;
	}
	return (testresults)
}


