/****** eureca.js ******/ /* * Common JavaScript library for Eureca * * * $Id: eureca.js,v 1.9 2008-01-07 01:05:26 cvscore Exp $ */ /* * findPosY : return the y position on the screen of the provided object. * * from http://quirksmode.org */ function findPosY ( obj ) { var curtop = 0; if (obj.offsetParent) { while (obj.offsetParent) { curtop += obj.offsetTop obj = obj.offsetParent; } } else if (obj.y) curtop += obj.y; return curtop; } /** * Change the UTF-8 Html entities into UTF-8-hex html entities * eg. é => 쎩 * This should only be done on javascript interpreted text ! */ function encode_utf_entities( str ) { var a_entities = [ { dec : "'", hex : "\x27" }, //' { dec : "à", hex : "\xE0" }, //à { dec : "â", hex : "\xE2" }, //â { dec : "ä", hex : "\xE4" }, //ä { dec : "ç", hex : "\xE7" }, //ç { dec : "è", hex : "\xE8" }, //è { dec : "é", hex : "\xE9" }, //é { dec : "ê", hex : "\xEA" }, //ê { dec : "ë", hex : "\xEB" }, //ë { dec : "î", hex : "\xEE" }, //î { dec : "ï", hex : "\xEF" }, //ï { dec : "ô", hex : "\xF4" }, //ô { dec : "ö", hex : "\xF6" }, //ö { dec : "ù", hex : "\xF9" }, //ù { dec : "û", hex : "\xFB" }, //û { dec : "ü", hex : "\xFC" } //ü ]; for ( var i=0; i < a_entities.length; i++ ) { while (str.indexOf( a_entities[i].dec )>=0) str = str.replace( a_entities[i].dec, a_entities[i].hex ); } return str; } /** * onchange_browse_panel_offset_path : called in browse panel macro * to set properly br-offset-path param * with user input. * */ function onchange_browse_panel_offset_path ( form_id ) { var oform = document.forms[form_id]; if ( oform ) { var user_offset = oform.elements["br-offset"].value; var max_offset = oform.elements["br-offset-max"].value; if ( (typeof user_offset == "undefined") || (user_offset == "") || !user_offset) user_offset = 1; if ( user_offset - 1 > max_offset ) user_offset = max_offset; /* decrement offset to reflect real value */ if ( user_offset ) user_offset--; /* fetch br-offset-path */ var offset_path = oform.elements["br-offset-path"].value; offset_path = offset_path.replace(/:$/, ''); var a_offset_path = offset_path.split(":"); a_offset_path[a_offset_path.length - 1] = user_offset; var offset_path_n = ''; for ( var i = 0; i < a_offset_path.length; i++ ) offset_path_n += a_offset_path[i]+':'; oform.elements["br-offset-path"].value = offset_path_n; } } /** * onchange_browse_panel_browseidx : called in browse panel macro, for simple * browses to increment index counter. * */ function onchange_browse_panel_browseidx ( form_id ) { var oform = document.forms[ form_id ]; if ( oform ) { var idx = parseInt(oform.elements["editor-browseidx"].value); var max = parseInt(oform.elements["editor-browseidxmax"].value); var next = idx; if ( next + 1 > max ) next = max; oform.elements["editor-browseidx"].value = next; } } /** * EUreca */ var _EUreca = Class.create(); _EUreca.prototype = { _a_slpublisher : $A([ { slpublisher_id : 1, slpublisher_key : 'google' } ,{ slpublisher_id : 2, slpublisher_key : 'overture' } ,{ slpublisher_id : 3, slpublisher_key : 'espotting' } ,{ slpublisher_id : 4, slpublisher_key : 'mirago' } ,{ slpublisher_id : 5, slpublisher_key : 'wanadoo' } ,{ slpublisher_id : 6, slpublisher_key : 'msn' } ,{ slpublisher_id : 7, slpublisher_key : 'daooda' } ,{ slpublisher_id : 8, slpublisher_key : 'himedia' } ]), _a_entities : $A([ { dec : "'", hex : "\x27" }, //' { dec : "à", hex : "\xE0" }, //à { dec : "â", hex : "\xE2" }, //â { dec : "ä", hex : "\xE4" }, //ä { dec : "ç", hex : "\xE7" }, //ç { dec : "è", hex : "\xE8" }, //è { dec : "é", hex : "\xE9" }, //é { dec : "ê", hex : "\xEA" }, //ê { dec : "ë", hex : "\xEB" }, //ë { dec : "î", hex : "\xEE" }, //î { dec : "ï", hex : "\xEF" }, //ï { dec : "ô", hex : "\xF4" }, //ô { dec : "ö", hex : "\xF6" }, //ö { dec : "ù", hex : "\xF9" }, //ù { dec : "û", hex : "\xFB" }, //û { dec : "ü", hex : "\xFC" } //ü ]), _h_media_by_key : $H({ mailing : 2, advertising : 3, affiliation : 4, sponsoredlink : 5, partner : 6, trustedfeed : 7 }), initialize : Prototype.emptyFunction /** * getSLPublisher : return list of slpublishers. */ ,getSLPublisher : function () { return this._a_slpublisher; } /** * getMediaIdByKey : return the media-id for a given media-key */ ,getMediaIdByKey : function ( key ) { var id = this._h_media_by_key.get(key); return ( id ) ? id : 0; } /** * getElementTextNS : implement a cross-browser method for fetching content * of a 'content:encoded' type node * */ ,getElementTextNS : function ( prefix, local, parentElem, index ) { var result = ""; if (prefix && Prototype.Browser.IE) { /* IE/Windows way of handling namespaces */ result = parentElem.getElementsByTagName(prefix + ":" + local)[index]; } else { /** the namespace versions of this method (getElementsByTagNameNS()) operate * differently in Safari and Mozilla, but both return value with just local * name, provided there aren't conflicts with non-namespace element names */ result = parentElem.getElementsByTagName(local)[index]; } if (result) { // get text, accounting for possible // whitespace (carriage return) text nodes if (result.childNodes.length > 1) { return result.childNodes[1].nodeValue; } else { return result.firstChild.nodeValue; } } else { return "n/a"; } } /* unicode_to_utf8 */ ,unicode_to_utf8 : function ( s ) { var utf8 = ""; for (var n = 0; n < s.length; n++) { var c = s.charCodeAt(n); if (c <= 0x7F) { // 0x00 - 0x7F: Emit as single byte, unchanged utf8 += String.fromCharCode(c); } else if ((c >= 0x80) && (c <= 0x7FF)) { // 0x80 - 0x7FF: Output as two byte code, 0xC0 in first byte // 0x80 in second byte utf8 += String.fromCharCode((c >> 6) | 0xC0); utf8 += String.fromCharCode((c & 0x3F) | 0x80); } else { // 0x800 - 0xFFFF: Output as three bytes, 0xE0 in first byte // 0x80 in second byte // 0x80 in third byte utf8 += String.fromCharCode((c >> 12) | 0xE0); utf8 += String.fromCharCode(((c >> 6) & 0x3F) | 0x80); utf8 += String.fromCharCode((c & 0x3F) | 0x80); } } return utf8; } /* UTF8_TO_UNICODE -- Decode UTF-8 argument into Unicode string return value */ ,utf8_to_unicode : function ( utf8 ) { var s = "", i = 0, b1, b2, b2; if (! utf8) return utf8; while (i < utf8.length) { b1 = utf8.charCodeAt(i); if (b1 < 0x80) { // One byte code: 0x00 0x7F s += String.fromCharCode(b1); i++; } else if((b1 >= 0xC0) && (b1 < 0xE0)) { // Two byte code: 0x80 - 0x7FF b2 = utf8.charCodeAt(i + 1); s += String.fromCharCode(((b1 & 0x1F) << 6) | (b2 & 0x3F)); i += 2; } else { // Three byte code: 0x800 - 0xFFFF b2 = utf8.charCodeAt(i + 1); b3 = utf8.charCodeAt(i + 2); s += String.fromCharCode(((b1 & 0xF) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F)); i += 3; } } return s; } /* ENCODE_UTF8 -- Encode string as UTF8 only if it contains a character of 0x9D (Unicode OPERATING SYSTEM COMMAND) or a character greater than 0xFF. This permits all strings consisting exclusively of 8 bit graphic characters to be encoded as themselves. We choose 0x9D as the sentinel character as opposed to one of the more logical PRIVATE USE characters because 0x9D is not overloaded by the regrettable "Windows-1252" character set. Now such characters don't belong in JavaScript strings, but you never know what somebody is going to paste into a text box, so this choice keeps Windows-encoded strings from bloating to UTF-8 encoding. */ ,encode_utf8 : function ( s ) { var i, necessary = false; for (i = 0; i < s.length; i++) { if ((s.charCodeAt(i) == 0x9D) || (s.charCodeAt(i) > 0xFF)) { necessary = true; break; } } if (!necessary) { return s; } return String.fromCharCode(0x9D) + this.unicode_to_utf8(s); } /* DECODE_UTF8 -- Decode a string encoded with encode_utf8 above. If the string begins with the sentinel character 0x9D (OPERATING SYSTEM COMMAND), then we decode the balance as a UTF-8 stream. Otherwise, the string is output unchanged, as it's guaranteed to contain only 8 bit characters excluding 0x9D. */ ,decode_utf8 : function ( s ) { if ((s.length > 0) && (s.charCodeAt(0) == 0x9D)) { return this.utf8_to_unicode(s.substring(1)); } return s; } /** * empty a dom of child */ ,removeChild : function ( o ) { try { if ( o ) while ( o.hasChildNodes() ) o.removeChild(o.childNodes[0]); } catch ( e ) {} } /** * epoch2calendar : from an epoch date return a date string */ ,epoch2calendar : function ( epoch, lang ) { var d = new Date(); if ( typeof(epoch) == 'undefined' ) epoch= (d.getTime()-d.getMilliseconds())/1000; d.setTime( epoch * 1000 ); if ( typeof(lang) == 'undefined' ) lang = 'fr'; var a_d = []; if ( lang == 'en' ) a_d = [ (d.getMonth()+1).toPaddedString(2), d.getDate().toPaddedString(2) ]; else a_d = [ d.getDate().toPaddedString(2), (d.getMonth()+1).toPaddedString(2) ]; a_d.push( d.getFullYear() ); return a_d.join('/'); } /** * formatChunked : given a number, generate chunks. */ ,formatChunked : function ( str ) { var _str = String(str); var _rgxp = /^(.*\s)?(\d+)(\d{3}\b)/; return ( _str == ( _str = _str.replace(_rgxp, "$1$2 $3")) ) ? _str : this.formatChunked(_str); } /** * formatFloat : format a float int. */ ,formatFloat : function ( num, dec ) { if ( typeof num == 'undefined' ) num = 0; if ( typeof dec == 'undefined' ) dec = 2; var onum = new Number(num); return onum.toFixed(dec); } /** * formatPct : format a percentage value. */ ,formatPct : function ( num ) { if ( typeof(num) == 'undefined' ) num = 0; return this.formatFloat(Number(num * 100)); } /** * roundFloat : round a float number. */ ,roundFloat : function ( val ) { var i = parseFloat(val); if (isNaN(i)) { i = 0.00; } var minus = ''; if (i < 0) { minus = '-'; } i = Math.abs(i); i = parseInt((i + .005) * 100); i = i / 100; var s = new String(i); if(s.indexOf('.') < 0) { s += '.00'; } if(s.indexOf('.') == (s.length - 2)) { s += '0'; } s = minus + s; return s; } /** * generateRandomStr : generate a random string, to bypass browser caching. * */ ,generateRandomStr : function () { var e_rn = '001234567890'; var e_rns1 = '1234567890'; var e_rns2 = '0987654321'; e_rn = new String (Math.random()); e_rns1 = e_rn.substring(2, 11); e_rn = new String (Math.random()); e_rns2 = e_rn.substring(2, 11); return e_rns1+e_rns2; } /** * redirect : redirect a url */ ,redirect : function ( url, a_p ) { if ( window && window.location && url ) { url = url.replace(/_rnd=\d+/, '').replace(/\?\&/, ''); var p = ( a_p ) ? '&'+$A(a_p).join('&') : ''; window.location.href = ( p ) ? ( ( /\?/.url ) ? url+p : url+'?'+p ) : url; } } /** * getCurrentUrl : return the current url without hostname. */ ,getCurrentUrl : function () { return window.location.href.replace(/^http(|s):\/\/(.+?)\//, '/'); } /** * getCurrentUrlBase : return the current base url without params. */ ,getCurrentUrlBase : function () { return this.getCurrentUrl().replace(/\?.*?$/, ''); } /** * validateUrl */ ,validateUrl : function ( url ) { return /^(http|https):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)?.*/.test(url); } /** * validateFloat */ ,validateFloat : function ( num ) { var val = Number(parseFloat(num.replace(/,/, '.') || 0)); return ( /^\d+(|\.\d+)$/.test(val) ) ? val : 0; } /** * exceptionDebug : debug output for an exception error. */ ,exceptionDebug : function ( e, str ) { var message = e.name+':\n'+e.message+'\n'+ (e.fileName && e.lineNumber ? e.fileName+' (line '+e.lineNumber+')\n\n':'')+ (e.description ? 'Description: '+e.description+'\n':'')+ (e.stack ? 'Stack trace: '+e.stack+'\n':'')+ (e.Number ? 'Number: '+e.Number+'\n':'')+ (e['opera#sourceloc'] ? 'Location: '+e['opera#sourceloc']+'\n':''); this.log('Exception : '+str+' >> '+message); } /** * log : log messsage to console if possible. */ ,log : function ( message ) {/* if ( !Prototype.Browser.IE && (typeof(console) != 'undefined') ) console.log(message); else alert(message);*/ } /** * whiteSpace */ ,whiteSpace : function ( num ) { var span = document.createElement('span'); span.innerHTML= ' '.times(num); return span; } /** * borderError : set the border of element to error styling. */ ,borderError : function ( element ) { if ( $(element) ) $(element).setStyle({ border : '1px solid red' }); } /** * borderOK : set the border of element to ok styling. */ ,borderOK : function ( element ) { if ( $(element) ) $(element).setStyle({ border : '1px solid #9b9a99' }); } /** * newOption : create a new