///////////////////////////////////////////////////////////////////////////////////////////
///                                                                                     ///
///                           JWT (Javascript Widget Toolkit)                           ///
///                          JWT is a part of Ellipse Framework                         ///
///                                                                                     ///
///   Authors : Dominique LIARD                     dominique.liard@infini-software.com ///
///             Alexia RAMAIOLI                     alexia.ramaioli@infini-software.com ///
///                                                                                     ///
///   Copyright (c) 2007..2011 Infini Software.     http://www.infini-software.com      ///
///   All rights reserved.                                                              ///
///                                                                                     ///
///////////////////////////////////////////////////////////////////////////////////////////
try {
var lastChar = __APPLICATION_PATH__.charAt( __APPLICATION_PATH__.length - 1 );
if ( lastChar != "/" ) __APPLICATION_PATH__ += "/";
} catch( exception ) {
var __APPLICATION_PATH__ = "./";
}
navigator.isChrome     = navigator.appVersion.indexOf( "Chrome" ) > -1;
navigator.isFirefox    = navigator.appName.indexOf( "Netscape" ) > -1 && navigator.appVersion.indexOf( "Chrome" ) == -1; 
navigator.isInternetExplorer = navigator.appVersion.indexOf( "MSIE" ) > -1;
navigator.version = "Not actually supported for this browser";
if ( navigator.isInternetExplorer ) {
navigator.version = parseFloat( navigator.appVersion.split( "MSIE" )[1] );
} else if ( navigator.isFirefox ) {
navigator.version = parseFloat( navigator.userAgent.toLowerCase().split( "firefox/" )[1] );
} else if ( navigator.isChrome ) {
navigator.version = parseFloat( navigator.userAgent.toLowerCase().split( "chrome/" )[1] );
}
Function.prototype.extendPrototype = function ( newMembers, replace ) {
for( member in newMembers ) {
if ( this.prototype.member && ! replace ) continue;
this.prototype[ member ] = newMembers[ member ];
}
};
if ( navigator.isInternetExplorer && navigator.version < 9 ) {
if ( ! window.Element ) window.Element = function() {}
}
if ( ! Element.extendPrototype ) Element.extendPrototype = Function.prototype.extendPrototype;
Function.extendPrototype( {
extendClass : function extendClass( newMembers ) {    
var newType = function() {
try {
var callerFunctionName = /\W*function\s+([\w\$]*)\(/.exec( arguments.callee.caller.toString() )[1];
if ( callerFunctionName != null && callerFunctionName == "extendClass" ) {
return;
}
} catch( error ) {
}
if ( this.initialize ) {
this.initialize.apply( this, arguments );
}
};
newType.prototype = new this();
for( member in newMembers ) newType.prototype[ member ] = newMembers[ member ]; 
return newType;
}
});
Object.extendPrototype( {
extendObject : function( childObject ) {
var newObject = this.clone();
for( var member in childObject ) newObject[member] = childObject[member];  
return newObject;
},
clone : function (){
if ( typeof(this) != 'object' ) return this;
var temporaryObject = new this.constructor();
for( var key in this ) temporaryObject[key] = (typeof(this[key]) == 'object' ? this[key].clone() : this[key]);
return temporaryObject;
}
});
Array.extendPrototype( {
contains : function( value ) {
for( var i=0; i<this.length; i++ ) {
if ( value === this[i] ) return true; 
}
return false;
},
remove : function( element ){
for( var index=0;index<this.length; index++ ) {
if ( element == this[index]) this.splice( index, 1 );
}
}
} );
if ( ! window.XMLHttpRequest ) {
window.XMLHttpRequest = function () {
if ( window.ActiveXObject ) {
try {
return new ActiveXObject("Msxml2.XMLHTTP");        
} catch (e) {
return new ActiveXObject("Microsoft.XMLHTTP");       
}
} else {
throw new Error( "This web browser can't support Ajax components" );  
}
};
}
var AjaxGetCall = Object.extendClass( {
initialize: function( url, handleAs, async, timeout ) {
var xhrGet = this.xhrGet = new XMLHttpRequest();
var handleAs = handleAs || "xml";
var _this = this;
timeout = timeout || 0;
var clockId = null;
xhrGet.onreadystatechange = function() {
if ( xhrGet.readyState == 4 ) {
if ( clockId != null ) window.clearTimeout( clockId );
if ( xhrGet.status == 200 ) {
if ( handleAs == "text" ) {
_this.load( xhrGet.responseText );
} else if ( handleAs == "xml" ) {
_this.load( xhrGet.responseXML );      
} else if ( handleAs == "json" ) {
eval( "var jsonObject = " + xhrGet.responseText + ";" );
_this.load( jsonObject );
} else {
throw new Exception( "handleAs: " + handleAs + " not supported" );
} 
} else {
if ( _this.error ) {
_this.error();
}
}
}
};
xhrGet.open( "GET", url, async );
if ( timeout > 0 ) {
clockId = setTimeout( function() { xhrGet.abort(); }, timeout );
}
},
send: function( params) {
this.xhrGet.send( params );  
},
load: function() {},
error: function() { throw new Exception( "Cannot execute AJAX call" ); }
});
var __importedPackages__ = new Array();
__importedPackages__.push( "corelib/services/web/javascript/Core.js" );
if ( typeof( importPackage ) == "undefined"  ) {    
function importPackage( packagedTypeName ) {
if ( __importedPackages__.contains( packagedTypeName ) == false ) {
__importedPackages__.push( packagedTypeName );
try {              
var xhrGet = new XMLHttpRequest();
xhrGet.open( "GET", __APPLICATION_PATH__ + packagedTypeName, false );
xhrGet.send( null );
window.document.write( "<script type='text/javascript'><!--\n" + xhrGet.responseText + "\n//--></script>\n" );
xhrGet.abort();
} catch( exception ) {         
alert( exception );
window.document.write( "<script language='javascript' src='" + __APPLICATION_PATH__ + packagedTypeName + "'></script>\n" );
}
}
}
}
importPackage( "corelib/services/web/javascript/Exception.js" );
importPackage( "corelib/services/web/javascript/Form.js" );
importPackage( "corelib/services/web/javascript/Debug.js" );
importPackage( "corelib/services/web/javascript/L10N.js" );
importPackage( "corelib/services/web/javascript/jwt/JwtUtility.js" );   
function addEventHandler( node, eventName, handler ) {
if ( node == null ) throw new Exception( "addEventHandler: Bad node" );
if ( typeof( eventName ) != "string" ) throw new Exception( "addEventHandler: Bad eventName" ); 
if ( typeof( handler ) != "function" ) throw new Exception( "addEventHandler: Bad event handler" ); 
var handlerArray = node[ eventName + "_handlers" ];
if ( handlerArray == null ) {
handlerArray = node[ eventName + "_handlers" ] = new Array(); 
var __temp = null;
eval( 
"__temp = function( event ) {" +
"    var result = true;" +
"    var handlerArray = this['" + eventName + "_handlers'];" +
"    event = ( event ? event : (window.event ? window.event : null ) );" +
"    try { if ( event.target ) event.srcElement = event.target; } catch( exception ) {} " +  
"    for( var i=0; i<handlerArray.length; i++ ) {" +
"        this.__temp__handler__ = handlerArray[i];" +           
"        var returnCode = this.__temp__handler__( event );" +
"        result = result && returnCode;" +            
"    }" +
"    return result;" +
"}"
);
node[ eventName ] = __temp;                 
}
handlerArray[ handlerArray.length ] = handler;
}
function removeEventHandler( node, eventName, handler ) {
throw new Exception( "Not implemented" );
}
addEventHandler( window, "onload", function( event ) {
if ( document.body.className != null && document.body.className != "" ) document.body.className += " ";
var browserClass = "CSS_UN";
if ( navigator.isInternetExplorer ) browserClass = "CSS_IE";
else if ( navigator.isFirefox ) browserClass = "CSS_FF";
else if ( navigator.isChrome ) browserClass = "CSS_CH";
document.body.className += browserClass + " " + browserClass + "_V" + Math.floor(navigator.version);
});
function attachClickMethod( node ) {
if ( node.click ) return;
node.click = function() {
var clickEvent = document.createEvent( "MouseEvents" );
clickEvent.initMouseEvent( 'click', true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null );
this.dispatchEvent( clickEvent );
};
}
function updateAllAreasForOnContextMenuEvent() {
if ( window.document.all ) {
var areaNodes = window.document.getElementsByTagName( "area" );
for( var i=0; i<areaNodes.length; i++ ) {
areaNodes[i].onmousedown = function() {
if ( window.event.button != 2 ) return;
this.fireEvent( 'oncontextmenu' );
window.event.cancelBubble = true;
};
areaNodes[i].imageNode = null;
var mapName = "#" + areaNodes[i].parentNode.name;
var imageNodes = document.getElementsByTagName( "img" );
for (var imgIndex=0; imgIndex<imageNodes.length; imgIndex++ )
if (imageNodes[imgIndex].usemap == mapName) areaNodes[i].imageNode = imageNodes[imgIndex];     
}
}
return true;
}
addEventHandler( window, "onload", updateAllAreasForOnContextMenuEvent );
function CookieManager() {
this.cookies = new Array();
var tempCookies = document.cookie.split(";");
for(var i=0; i<tempCookies.length; i++) {
var currentCookie = tempCookies[i].split("=");
this.cookies[i] = currentCookie;
}
this.getValue = function CookiesManager_getValue(name) {
for(var i=0;i<this.cookies.length; i++) {
if (this.cookies[i][0].indexOf(name) > -1) {
return this.cookies[i][1];
}
}
return null; 
};
this.setValue = function CookieManager_setValue(key, value, expirationDate) {
if (arguments.length != 3) {
expirationDate = new Date();
expirationDate.setTime( expirationDate.getTime()*1 + (365*24*60*60*1000) );
}
document.cookie = key + "=" + value + ";expires=" + expirationDate.toGMTString();
};
}
String.extendPrototype( {
endsWith : function( endString ) {
if ( endString == null ) throw new NullPointerException();
if ( typeof endString != "string" ) throw new BadTypeException( "parameter must be a string" );
if ( endString.length > this.length ) throw new Exception( "parameter length is greater than this length" );
return this.right( endString.length ) == endString;
},
left : function ( length ) {
var strLength = this.length;
if ( length > strLength ) throw new Exception( "Bad length for left function" );
return this.substring( 0, length );    
},
right : function ( length ) {
var strLength = this.length;
if ( length > strLength ) throw new Exception( "Bad length for left function" );
return this.substring( strLength - length );    
},
startsWith : function( startString ) {
if ( startString == null ) throw new NullPointerException();
if ( typeof startString != "string" ) throw new BadTypeException( "parameter must be a string" );
if ( startString.length > this.length ) throw new Exception( "parameter length is greater than this length" );
return this.substring( 0, startString.lenght ) == startString;
},
trim : function() {
if ( this == "" ) return this;
var begin = 0;
var theChar = this.charAt( begin );
while( theChar == ' ' || theChar == '\t' || theChar == '\r' || theChar == '\n' ) {
begin ++;
if ( begin == this.length ) return "";
theChar = this.charAt( begin );
}
var end = this.length-1;
theChar = this.charAt( end );
while( theChar == ' ' || theChar == '\t' || theChar == '\r' || theChar == '\n' ) {
end --;
theChar = this.charAt( end );
}
return this.substring( begin, end+1 );
},
format: function() {
var isprototype = (this == String.prototype);
var index = (!isprototype ? 0 : 1);
var value = (!isprototype ? this : arguments[0]);
for(var i = index; i < arguments.length; i++)
value = value.replace("\{" + (i - index) + "}", arguments[i].toString());
return value;
}
} );
Date.extendPrototype( {
addDays: function( n)
{
this.setTime( this.getTime() + (n * 86400000));
return this.getTime();
},
getWeek : function() {
var firstDay = new Date( this.getFullYear(), 0, 1 );
return Math.ceil( ( ( (this - firstDay ) / 86400000) + firstDay.getDay() ) / 7 );
},
toLocaleFormat : function(format) {
switch(format) {
case "%a":
return this.toString().left(3); 
default:
throw new Exception("the specified formatting is not currently supported");
}
},
toURIString: function() {
var index = 0;
if((new Date(1970, 12, 1)).getMonth() == 0) 
index++;
return "" + this.getUTCFullYear() + (this.getUTCMonth() + index).toFixedString(2) + this.getUTCDate().toFixedString(2) + "T" + this.getUTCHours().toFixedString(2) + this.getUTCMinutes().toFixedString(2) + this.getUTCSeconds().toFixedString(2) + "Z";
}
} );
Number.extendPrototype( {
toFixedString: function(digits)
{
var value = this.toString();
if(!isNaN(digits) && digits > value.length)
for(var i = value.length; i < digits; i++)
value = "0" + value;
return value;
}
});
function isInteger( value ) {
if ( isNaN( value ) ) return false;
if ( Math.floor( value ) != value ) return false;
return true;
}
var Locale = Object.extendClass( {
initialize : function( language, country ) {
this._language = language;
this._country = country;
},
getLanguage : function () { return this._language; },
getCountry : function () { return this._country; } 
});
Element.extendPrototype( {
createAndAppendChild : function( elementName, attributes, styleDefinitions ) {
var newElement = document.createElement( elementName );
if ( attributes ) {
for( var key in attributes ) {
newElement[ key ] = attributes[ key ];
}
}
if ( styleDefinitions ) {
for( var key in styleDefinitions ) {
newElement.style[ key ] = styleDefinitions[ key ];
}
}
this.appendChild( newElement );
return newElement;
},
getElementsByClassName : function( className ) {
var classNameRegExp = new RegExp( "(^|\\s)" + className + "(\\s|$)" );
var subElements = this.getElementsByTagName("*");
var selectedElements = [];
for ( var i=0; i<subElements.length; i++ ){
var currentNode = subElements[i];
if ( classNameRegExp.test( currentNode.className ) ){
selectedElements.push( currentNode );
}
}
return selectedElements;
},
addCssClass : function( cssClassName ) {
var classArray = this.className.split( " " );
if ( classArray.contains( cssClassName ) == false ) {
this.className += " " + cssClassName;
}
},
removeCssClass : function( cssClassName ) {
var rules = this.className.split(" ");
var names = new Array();
for( var i = 0; i < rules.length; i++ ) {
if( rules[i] != cssClassName ) {
names.push(rules[i]);
}
}
this.className = names.join(" ");
}
});
if ( ! document.getElementsByClassName ) {
document.getElementsByClassName = Element.prototype.getElementsByClassName;
}
if ( navigator.isInternetExplorer && navigator.version <= 7 ) {
var addCssClass = function( node, cssClassName ) {
var classArray = node.className.split( " " );
if ( classArray.contains( cssClassName ) == false ) {
node.className += " " + cssClassName;
}
};
var removeCssClass = function( node, cssClassName ) {
var rules = node.className.split(" ");
var names = new Array();
for( var i = 0; i < rules.length; i++ ) {
if( rules[i] != cssClassName ) {
names.push(rules[i]);
}
}
this.className = names.join(" ");
};
}

