/** common.js This is a collection of commonly used JavaScript functions that is loaded on every page. Part of KMS, (C) 2014 Frederik Keim **/ // // Global variables // var imgPath; var resized = false; var showing = ''; var clientWidth = 0; var clientHeight = 0; var cX; var cY; var activeId = ''; var activeElement; document.onclick = regId; document.onload = initjs; // // Runs some initializing functions when the document body is loaded // function initjs() { hide_dt(); if ( self.updateReport ) updateReport(); if ( window.innerWidth ) { clientWidth = window.innerWidth; clientHeight = window.innerHeight; } else if ( document.documentElement ) { clientWidth = document.documentElement.clientWidth; clientHeight = document.documentElement.clientHeight; } else { clientWidth = document.body.clientWidth; clientHeight = document.body.clientHeight; if ( clientHeight > screen.availHeight ) clientHeight = screen.availHeight; } } // // If the mobile virsion of the website is being displayed, // add the CSS declarations for webfonts to the header section. // function mobileFonts() { if ( !self.location.href.match( /:\/\/mobile\./ ) ) return; var header = document.getElementsByTagName('head')[0]; var domain = self.location.host.match( /^.*?\.(.*)/ )[1]; var URL = self.location.protocol+'//styles.'+domain+'/style-fonts.css'; remoteFileGet( URL, function(responseText) { var hostPattern = new RegExp( '://.*?\.'+domain+'/fonts', 'g' ); var header = document.getElementsByTagName('head')[0]; var fontCSS = document.createElement('style'); fontCSS.type = 'text/css'; fontCSS.innerHTML = responseText.replace( hostPattern, '://styles.'+domain ); header.appendChild( fontCSS ); } ); } // // Changes an image when the mouse cursor moves over it and back // to its original state when the cursor leaves it // function cp( object_id, state, img_name ) { if ( document.getElementById ) { if ( !img_name ) img_name = object_id; if ( !imgPath ) { var searchstr = '(.*)/' + img_name; if ( document.getElementById(object_id).src.search(searchstr) != -1 ) { imgPath = document.getElementById(object_id).src.match(searchstr)[1] + '/'; } } searchstr = img_name + '.*\\.(.*)'; if ( document.getElementById(object_id).src.search(searchstr) != -1 ) { var ending = '.' + document.getElementById(object_id).src.match(searchstr)[1]; } if ( typeof ending != 'undefined' && typeof imgPath != 'undefined' ) { if ( state != 'normal' ) { document.getElementById(object_id).src = imgPath + img_name + "_" + state + ending; } else { document.getElementById(object_id).src = imgPath + img_name + ending; } } } } // // Toggles the display of HTML element (paragraphs) with ID "dyntxt". // function switch_dt ( id ) { hide_dt(); if ( showing != id ) { document.getElementById("dyntxt"+id).style.display = "inline"; if ( document.getElementById("dyntxt"+id+"_r") ) { document.getElementById("dyntxt"+id+"_r").style.display = "none" } showing = id; } else { showing = ''; } } // // Hides *ALL* elements with ID "dyntxt<##>" // function hide_dt () { var i = 1; while ( document.getElementById("dyntxt"+i) ) { document.getElementById("dyntxt"+i).style.display = "none"; if ( document.getElementById("dyntxt"+i+"_r") ) { document.getElementById("dyntxt"+i+"_r").style.display = "inline" } ++i; } } // // Opens a new browser window with the info page "pid" // function info( pid, basePath, pwidth, pheight ) { var size = 'width=' + pwidth + ',height=' + pheight + ','; var path = basePath + 'info/' + pid; window.open(path,'_blank',size+'dependent=yes,status=no,location=no,scrollbars=yes,toolbar=yes'); } // // Checks, if all mandatory fields of a formular contain input // function validate() { if ( !document.getElementsByName ) return true; if ( window.mandatoryFields == undefined ) return true; var ok = true; for ( var i = 0; i < mandatoryFields.length; ++i ) { var field = document.getElementsByName( mandatoryFields[i] ); if ( field[0] == null ) var field = document.getElementsByName( mandatoryFields[i]+'[]' ); if ( field[0] == null ) var field = document.getElementsByName( mandatoryFields[i]+'[0]' ); if ( field[0] == null ) alert( mandatoryFields[i] ); var fieldType = field[0].type.toLowerCase(); for ( var j = 0; j < field.length; ++j ) { var normalClass = field[j].className.replace( /_warning/, '' ); if ( fieldType == 'text' || fieldType == 'password' || fieldType == 'textarea' ) { var repeatName = field[0].name.replace( /((?:\[\]|))$/, '_repeat$1' ); var repeatField = document.getElementsByName(repeatName)[j]; if ( field[j].value == '' ) { field[j].className = normalClass + '_warning'; ok = false; } else if ( repeatField != null && field[j].value != repeatField.value ) { field[j].className = normalClass + '_warning'; repeatField.className = normalClass + '_warning'; ok = false; } else field[j].className = normalClass; } else if ( fieldType == 'select-one' ) { if ( field[j][field[j].selectedIndex].value == '' ) { field[j].className = normalClass+'_warning'; ok = false; } else field[j].className = normalClass; } else if ( fieldType == 'hidden' ) { if ( field[j].value == '' ) ok = false; } } if ( fieldType == 'radio' || ( fieldType == 'checkbox' && field[0].name.substr( field[0].name.length - 3 ) != '[0]' ) ) { var fieldOk = false; var normalClass = field[0].parentNode.className.replace( /_warning/, '' ); for ( var j = 0; j < field.length; j++ ) { if ( field[j].value != '' && field[j].checked == true ) { fieldOk = true; break; } } for ( var j = 0; j < field.length; j++ ) { if ( fieldOk ) field[j].parentNode.className = normalClass; else field[j].parentNode.className = normalClass + '_warning'; } if ( !fieldOk ) ok = false; } else if ( fieldType == 'checkbox' ) { var normalClass = field[0].parentNode.className.replace( /_warning/, '' ); var fieldName = field[0].name.substr( 0, field[0].name.length - 3 ); var j = 0; var fieldOk = false; while ( sibling = document.getElementsByName( fieldName+'['+j+']' )[0] ) { if ( sibling.value != '' && sibling.checked == true ) { fieldOk = true; break; } j++; } j = 0; while( sibling = document.getElementsByName( fieldName+'['+j+']' )[0] ) { if ( fieldOk ) sibling.parentNode.className = normalClass; else sibling.parentNode.className = normalClass + '_warning'; j++; } if ( !fieldOk ) ok = false; } } return ok; } // // Allows for the adding of a custom value to a select field // function selectOther ( selectName, formId ) { if ( formId == undefined ) var formId = 0; var select = document.forms[formId].elements[selectName]; if ( select == null ) return; var otherContainer = document.getElementById( selectName+'_other' ); if ( select.options[select.selectedIndex].value == "selectOther" ) { otherContainer.style.display = ''; if ( window.mandatoryFields != undefined ) { arrayRemove( selectName, mandatoryFields ); mandatoryFields[mandatoryFields.length] = selectName+'_other'; } } else { otherContainer.style.display = 'none'; document.forms[formId].elements[selectName+'_other'].value = document.forms[formId].elements[selectName+'_other'].defaultValue; if ( window.mandatoryFields != undefined ) { arrayRemove( selectName+'_other', mandatoryFields ); mandatoryFields[mandatoryFields.length] = selectName; } } } // // Toggles the "mandatoryness" of a formular element // function toggleMandatory( name ) { if ( window.mandatoryFields == undefined ) window.mandatoryFields = new Array(); if ( inArray( name, window.mandatoryFields ) ) arrayRemove( name, window.mandatoryFields ); else window.mandatoryFields[window.mandatoryFields.length] = name; } // // Toggles the display of element 'elem' (without place holder, // page structure may change) // function toggleDisplay( elem, dispType ) { if ( !document.getElementById ) return; if ( typeof( elem ) == 'string' ) elem = document.getElementById(elem); if ( !elem ) return; if ( dispType === undefined ) var dispType = 'block'; if ( elem.lockDisplay != undefined && elem.lockDisplay == true ) return; elem.style.display = ( elem.offsetWidth == 0 ) ? dispType : 'none'; } // // Toggles th evisibility of element 'elem' (WITH place holder, // there will be blank space in the elements place) // function toggleVisibility( elem, vis ) { if ( !document.getElementById ) return; if ( typeof( elem ) == 'string' ) elem = document.getElementById(elem); if ( !elem ) return; var cVis = elem.style.visibility; var newVis = ( vis != undefined ) ? vis : ( ( cVis == 'visible' ) ? 'hidden' : 'visible' ); elem.style.visibility = newVis; } // // Toggles the display lock status of an element. If the element display // is locked, it cannot be changed by the toggleDisplay() function. // function toggleLockDisp( elementId ) { if ( !document.getElementById ) return; if ( !document.getElementById(elementId) ) return; var element = document.getElementById( elementId ); var lock = ( element.lockDisplay != undefined ) ? element.lockDisplay : false; element.lockDisplay = !lock; } // // Deletes the DOM element with the specified id // function deleteDOMObject( elementId ) { if ( !document.getElementById ) return false; if ( !document.getElementById(elementId) ) return true; var element = document.getElementById( elementId ); return element.parentNode.removeChild( element ); } // // Shortcut for document.getElementById() // function $( id ) { return document.getElementById( id ); } // // PHP inspired functions for manipulating array objects // function inArray( needle, haystack ) { if ( typeof haystack != 'object' || haystack.length == null ) { alert( 'inArray() argument #2 is not an Array!' ); return false; } for ( var i = 0; i < haystack.length; i++ ) { if ( haystack[i] == needle ) return true; } return false; } function arraySearch( needle, haystack ) { for ( var i = 0; i < haystack.length; i++ ) { if ( haystack[i] == needle ) return i; } return false; } function arrayRemove( needle, haystack ) { var count = 0; for ( var i = 0; i < haystack.length; i++ ) { if ( haystack[i] == needle ) { haystack.splice( i, 1 ); count++; } } return count; } // // Registers the coordinates of a mouse click event. // If an element with a HTML id tag was clicked, this ID is // stored in the global "activeId" variable. // function regId ( event ) { //modified from script at: http://www.quirksmode.org/js/events_properties.html#target if ( event == undefined ) var event = window.event; if ( event == undefined ) return; if ( event.target ) targ = event.target; else if ( event.srcElement ) targ = event.srcElement; if ( targ.nodeType == 3 ) // defeat Safari bug targ = targ.parentNode; if ( targ.id != '' ) activeId = targ.id; cX = event.clientX; cY = event.clientY; } // // Get the page scroll offsets // function getPageYOffset() { if ( window.pageYOffset ) { return window.pageYOffset; } else if ( document.body && document.body.scrollTop ) { return document.body.scrollTop; } else return 0; } function getPageXOffset() { if ( window.pageXOffset ) { return window.pageXOffset; } else if ( document.body && document.body.scrollLeft ) { return document.body.scrollLeft; } else return 0; } // // Round a floating point number to the given umber // of places // function fround( num, places ) { return ( Math.round( num * Math.pow( 10, places ) ) / Math.pow( 10, places ) ); } // // Format floating point numbers according to the // language profile and decode formatted strings to // floating point number again // function numberEncode( num, places ) { var numStr = fround( num, places ).toString(); return numStr.replace( /\./, _numDecimalSep ); } function numberDecode( num ) { // alert( num ); var decSep = new RegExp( _numDecimalSep ); num = num.replace( _numThousandsSep, '' ); // alert( num ); num = num.replace( _numDecimalSep, '.' ); // alert( num ) return parseFloat( num ); } // // Scale a video i-frame to a relative width // function scaleVideo( frameId, aspectRatio ) { var vidFrame = document.getElementById( frameId ); vidFrame.height = ( vidFrame.offsetWidth / aspectRatio ) + 'px'; vidFrame.style.height = ( vidFrame.offsetWidth / aspectRatio ) + 'px'; } // // Switch out a custom poster image for a YouTube (R) vidoe with // the actual Video i-frame // function showVideo( vidId ) { document.getElementById(vidId+'_ph').style.display = 'none'; document.getElementById(vidId).style.display = 'block'; scaleVideo( vidId, 1.7778 ); }