/***** Created by Terje Rudi, HVL, 2017 *****/ function domReady () { // JQUERY ready-alternative - det blir ingen feil dersom denne funksjonen gjentas document.body.className += " javascript"; //... } function isNotVal(val){ // brukes til sjekking om verdi har innhold og/eller er gyldig return (val==null || val===false); } // Mozilla, Opera, Webkit if ( document.addEventListener ) { document.addEventListener( "DOMContentLoaded", function(){ document.removeEventListener( "DOMContentLoaded", arguments.callee, false); domReady(); // <- kan brukes ute i html-doc }, false ); // If IE event model is used (Explorer 8...) } else if ( document.attachEvent ) { // ensure firing before onload document.attachEvent("onreadystatechange", function(){ if ( document.readyState === "complete" ) { document.detachEvent( "onreadystatechange", arguments.callee ); domReady(); // <- kan brukes ute i html-doc } }); } // AJAX var AJAXerrors = { "0":"Unknown Error", "100":"Continue", "101":"Switching Protocol", "103":"Processing (WebDAV)", "200":"OK", "201":"Created", "202":"Accepted", "203":"Non-Authoritative Information", "204":"No Content", "205":"Reset Content", "206":"Partial Content", "207":"Multi-Status (WebDAV)", "208":"Multi-Status (WebDAV)", "226":"IM Used (HTTP Delta encoding)", "300":"Multiple Choice", "301":"Moved Permanently", "302":"Found", "303":"See Other", "304":"Not Modified", "305":"Use Proxy ", "306":"unused", "307":"Temporary Redirect", "308":"Permanent Redirect", "400":"Bad Request", "401":"Unauthorized", "402":"Payment Required", "403":"Forbidden", "404":"Not Found", "405":"Method Not Allowed", "406":"Not Acceptable", "407":"Proxy Authentication Required", "408":"Request Timeout", "409":"Conflict", "410":"Gone", "411":"Length Required", "412":"Precondition Failed", "413":"Payload Too Large", "414":"URI Too Long", "415":"Unsupported Media Type", "416":"Requested Range Not Satisfiable", "417":"Expectation Failed", "418":"I'm a teapot", "421":"Misdirected Request", "422":"Unprocessable Entity (WebDAV)", "423":"Locked (WebDAV)", "424":"Failed Dependency (WebDAV)", "426":"Upgrade Required", "428":"Precondition Required", "429":"Too Many Requests", "431":"Request Header Fields Too Large", "451":"Unavailable For Legal Reasons", "500":"Internal Server Error", "501":"Not Implemented", "502":"Bad Gateway", "503":"Service Unavailable", "504":"Gateway Timeout", "505":"HTTP Version Not Supported", "506":"Variant Also Negotiates", "507":"Insufficient Storage", "508":"Loop Detected (WebDAV)", "510":"Not Extended", "511":"Network Authentication Required" } function makeRequest(url,callback,ref,callbackExstraArgsObj) { // AJAX-kall if (typeof callback != "undefined"){ var req = new XMLHttpRequest(); if (!req) { alert('Gir opp :( XMLHTTP instance not created -1'); return false; } req.onreadystatechange = function(){ try { if (req.readyState === XMLHttpRequest.DONE) { if (req.status !== 200) { try { console.log('En feil oppstod: ' + AJAXerrors[req.status] + ' (' + req.status + ')'); }catch (e){ console.log('Det var et problem med å hente data. Feilkode: ' + req.status); } }else{ try{ if (callbackExstraArgsObj && typeof callbackExstraArgsObj == "object"){ callback(req.responseText,callbackExstraArgsObj); }else{ callback(req.responseText); } }catch(e){ try { console.log('En feil oppstod [-1]: ' + e + ' (Melding: ' + AJAXerrors[req.status] + ' (sjekk callback))'); }catch (f){ console.log('Kunne ikke aktivere callback: ' + e + ' (Status: ' + req.status + ')'); } } } } }catch( e ) { console.log('Opplevde et unntak: ' + e.description); } }; req.open('GET', url); req.send(); }else{ console.log("Callback undefinert i 'makeRequest', referanse: " + ref); } } function postJson(url,json,callback,ref,callbackExstraArgsObj) { // Sendes som btoa (base64) // AJAX-kall if (typeof callback != "undefined"){ var req = new XMLHttpRequest(); if (!req) { alert('Gir opp :( XMLHTTP instance not created -2'); return false; } req.onreadystatechange = function(){ try { if (req.readyState === XMLHttpRequest.DONE) { if (req.status !== 200) { try { console.log('En feil oppstod: ' + AJAXerrors[req.status]); }catch (e){ console.log('Det var et problem med å hente data. Feilkode: ' + req.status); } }else{ try{ if (callbackExstraArgsObj && typeof callbackExstraArgsObj == "object"){ callback(req.responseText,callbackExstraArgsObj); }else{ callback(req.responseText); } }catch(e){ try { console.log('En feil oppstod [-2]: ' + e + ' (Melding: ' + AJAXerrors[req.status] + ', Callback = ' + JSON.stringify(callback) + ')'); }catch (f){ console.log('Kunne ikke aktivere callback: ' + e + ' (Status: ' + req.status + ')'); } } } } }catch( e ) { console.log('Opplevde et unntak: ' + e.description); } }; req.open("POST", url); var params = "btoajson=" + btoa(JSON.stringify(json)); //Send the proper header information along with the request req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //req.setRequestHeader("Content-length", params.length); //req.setRequestHeader("Connection", "close"); req.send(params); }else{ console.log("Callback undefinert i 'makeJSONpostRequest', referanse: " + ref); } } function addCSSRule(sheet, selector, rules, index) { return true; /* FASET UT try{ if("insertRule" in sheet) { sheet.insertRule(selector + "{" + rules + "}", index); }else if("addRule" in sheet) { sheet.addRule(selector, rules, index); } }catch(e){ console.log("FEIL ved innsetting av i stilark: " + e + ". Sheet = " + sheet + " Selector = " + selector + " Index = " + index); } */ } // SlideUp SlideDown SlideToggle /* Element to slide gets the following CSS: max-height: 0;opacity: 0;overflow: hidden;transition: max-height 0.4s ease 0s; */ function slideElemDown(elem) { elem.style.maxHeight = '1000px'; // We're using a timer to set opacity = 0 because setting max-height = 0 doesn't (completely) hide the element. elem.style.opacity = '1'; } function slideElemUp(elem) { elem.style.maxHeight = '0'; once( 1, function () { elem.style.opacity = '0'; }); } function slideElemToggle(elem){ if (elem.style.opacity == 1){ slideElemUp(elem); }else{ slideElemDown(elem); } } function once (seconds, callback) { var counter = 0; var time = window.setInterval( function () { counter++; if ( counter >= seconds ) { callback(); window.clearInterval( time ); } }, 400 ); } // Sliding ferdig /* UTFASET // direkte manipulering av stilark for HVL-Bouvet-stil try{ // Fjerne dobbel ned-pil i Explorer i rullegardinmeny: addCSSRule(document.styleSheets[0],'select::-ms-expand','display: none',0); // Sette inn spinner addCSSRule(document.styleSheets[0],'@-webkit-keyframes rwSpinnerRotate','{-webkit-transform:rotate(0deg);}to{-webkit-transform:rotate(360deg);}',document.styleSheets[0].cssRules.length); addCSSRule(document.styleSheets[0],'@-moz-keyframes rwSpinnerRotate','{-moz-transform:rotate(0deg);}to{-moz-transform:rotate(360deg);}',document.styleSheets[0].cssRules.length); addCSSRule(document.styleSheets[0],'@-ms-keyframes rwSpinnerRotate','{-ms-transform:rotate(0deg);}to{-ms-transform:rotate(360deg);}',document.styleSheets[0].cssRules.length); addCSSRule(document.styleSheets[0],'@keyframes rwSpinnerRotate','{transform:rotate(0deg);}to{transform:rotate(360deg);}',document.styleSheets[0].cssRules.length); addCSSRule(document.styleSheets[0],'.rwSpin','position:relative;top:0.3rem;left:0.3rem;padding: 0.2rem;color: #fff;width: 0.6rem;height: 0.6rem;background: #acb7b2;border-radius: 0.6rem;-webkit-animation-name: rwSpinnerRotate;-webkit-animation-duration: 0.8s;-webkit-animation-iteration-count: infinite;-webkit-animation-timing-function: linear;-moz-animation-name: rwSpinnerRotate;-moz-animation-duration: 0.8s;-moz-animation-iteration-count: infinite;-moz-animation-timing-function: linear;-ms-animation-name: rwSpinnerRotate;-ms-animation-duration: 0.8s;-ms-animation-iteration-count: infinite;-ms-animation-timing-function: linear;'); }catch(e){ console.log("addRule or insertRule not supported"); } */ /*** FASTE HJELPERE ***/ function swapLang(){ // Bytter ut all elementers tekst med verdi fra 'data-english' - oversetter rett ot slett, // men bare derson verdi i data-english eksisterer: // Eks: try{ var otherLangVals = document.querySelectorAll("[data-english]"); for (key in otherLangVals){ try{ var otherLangString = otherLangVals[key].getAttribute("data-english"); if (otherLangString != "undefined"){ var originalString = otherLangVals[key].innerHTML; otherLangVals[key].innerHTML = otherLangString; otherLangVals[key].setAttribute("data-english",originalString); } }catch(f){ console.log(f); } } }catch(e){ alert("Sorry, translation unavailable :-("); } } /*** FASTE HJELPERE SLUTT ***/ // EXTENSION extractHVLCalendarInfo