/*----------------------------------------------------------------------------\
|							Askia Script Objects							  |
|-----------------------------------------------------------------------------|
|                          Created by Askia Company							  |
|							(http://www.askia.com)							  |
|-----------------------------------------------------------------------------|
| The AskiaScript is the main javascript library of Askia					  |
| It will manage integrate all needed javascript files of Askia				  |
|-----------------------------------------------------------------------------|
|							Copyright Askia © 1994-2006						  |
|-----------------------------------------------------------------------------|
| {No Dependencies}															  |
|-----------------------------------------------------------------------------|
| 2006-05-23 |V 1.0.0														  |
|			 |+ AUTO-LOAD THE RESOURCES										  |
|			 |	Load the Javascript files into the html page				  |
|			 |	Load the resources files of the Javascripts	(pictures...)	  |			
|-----------------------------------------------------------------------------|
| 2007-06-29 |V 1.0.1														  |
|			 |+ Add the cursor constants									  |
|-----------------------------------------------------------------------------|
| Created 2006-05-23 | All changes are in the log above. | Updated 2007-06-29 |
|-----------------------------------------------------------------------------|
|														All rights reserved	  |
|Askia - AskiaScript - API Version 1.0.1		Copyright Askia © 1994-2007   |
\---------------------------------------------------------------------------- */

/* -----
	Working on:
		Internet Explorer 6			   [Tested on 6]
		NetScape (since the version 6) [Tested on 6 / 7 / 8]
		Opera (since the version 7)	   [Tested on 7 / 8] (no calendar appear on version 7)
		FireFox 1					   [Tested on 1]

	Not working on: 
		Opera 3 / 5 / 6
		NetScape 4 
		
----- */


/* ====
	Set-up browser checks and add emulator
==== */
var isOpera   = /opera|opera/i.test(navigator.userAgent);
var isIE	  = !isOpera && /msie/i.test(navigator.userAgent);			// preventing opera to be identified as ie
var isMozilla = !isOpera && /mozilla\/5/i.test(navigator.userAgent);	// preventing opera to be identified as mz
var isNS6	  = isMozilla && /Netscape6/i.test(navigator.userAgent);	

//Constants to have the version of script
var VERSION_OF_SCRIPTS="1.0.3";

//Other constants
var UNDEFINED='undefined';

//Special keys of keyboard
var KEY_ENTER=13;
var KEY_PAGEUP=33;
var KEY_PAGEDOWN=34;
var KEY_END=35;
var KEY_HOME=36;
var KEY_LEFT=37;
var KEY_UP=38;
var KEY_RIGHT=39;
var KEY_DOWN=40;

//Extension
var EXTENSION_CSS='css';

//Cursors
var CURSOR_WAIT="wait";

//Add method getElementById() in the document 
//(for IE4 and other browser which don't support the getElementById method)
if (isIE && document.getElementById == null) {	
	document.getElementById = function(sId) {
			return document.all[sId];
		};
	}

//Add new method in the Date object 
//(for NS6 and other browser which don't suppport the toDateString method)
if (typeof (Date.prototype.toDateString)==UNDEFINED){
		Date.__arrDays=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
		Date.__arrMonths=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
		Date.prototype.toDateString=function(){
				var strDate=Date.__arrDays[this.getDay()] + ' ' + Date.__arrMonths[this.getMonth()] + ' ' + formatNumber(this.getDate()) + ' ' + this.getFullYear();
				
				//Format number with 2 digits
				function formatNumber(number){
						var sReturn=number;
						if (parseInt(number)<10)sReturn = "0" + parseInt(number);
						return sReturn;
					}
				return strDate;
			}
	}

/* ====
	AskiaLibrary
	Objects which content the name of different javascript files
==== */
var AskiaLibrary={
		//Mozilla emulator to retrieve some IE features into Mozilla
		MOZILLA_EMULATOR	:   'IEEmulatorForMoz.js',
		//Manage the Questions/Responses (Semi-open, ranking...)
		COMMON				:	'Common.js',
		//Manage the navigation (Previous/Next/Pause)
		NAVIGATION			:   'Nav.js',
		//Manage the validation of form
		VALIDATOR			:   'FieldValidator.js',
		//Manage the error messages
		ERROR_MESSAGES		:   'ErrorMessages.js',
		//Manage the live calculation
		CALCULATOR			:	'Calculator.js',
		//Manage the calendar
		CALENDAR			:   'Calendar.js',		
		//Manage the sliders
		SLIDER				:   'Slider.js',
		//Manage the tool tip text
		TOOLTIPTEXT			:   'ToolTipText.js',
		//Manage the string
		STRINGBUILDER		:   'StringBuilder.js',
		//Manage the translation of script
		TRANSLATION			:	'Translation.js'
	};
var AskiaCSS={
		//CSS of ToolTip text
		TOOLTIPTEXT			:   'ToolTipText.css',
		//Css of calendar
		CALENDAR			:	'Calendar.css'
	};
/* ====
	AskiaScript object
====*/
var AskiaScript={
		//GLOBAL PROPERTIES OF SURVEY
		intNameOfSurvey				: '',
		extNameOfSurvey				: '',
		languageId					: null,
		
		//ARCHITECTURE OF SERVER
		pathOfResources				: '../Resources/',
		pathOfScripts				: '../Scripts/',
		_CSSFolderName				: 'CSS',
		
		//
		_addedStyleSheet			: {},
		
		//LOAD THE DIFFERENT LIBRARIES
		load						: function(){
				//Add scripts
				for (var lib in AskiaLibrary){
						//Don't write the Mozilla Emulator when we are in IE
						if (!isMozilla && AskiaLibrary[lib]==AskiaLibrary.MOZILLA_EMULATOR)continue;
						if (AskiaLibrary[lib]==AskiaLibrary.TRANSLATION)continue;
						document.write('<script language="javascript" src="' + this.pathOfScripts + 'v' + VERSION_OF_SCRIPTS + '/' + AskiaLibrary[lib] + '"></script>');
					}
				//At the end, add the scripts overwrite the translations
				//in all scripts
				if (this.languageId && this.languageId!=''){
						document.write('<script language="javascript" src="' + this.pathOfScripts + 'v' + VERSION_OF_SCRIPTS + '/' + this.languageId + '/' + AskiaLibrary.TRANSLATION + '"></script>');
					}
					
				//Add css
				for (var lib in AskiaCSS){
						this.addStyleSheet(AskiaCSS[lib]);
					}
			},
		
		//Add style sheet into the document
		addStyleSheet					: function(fileName){
				//Don't add a duplicate stylesheet
				if (this._addedStyleSheet[fileName])return;
				document.write('<link rel="stylesheet" href="' + this.pathOfScripts + 'v' + VERSION_OF_SCRIPTS + '/' + this._CSSFolderName + '/' + fileName + '">');
				this._addedStyleSheet[fileName]=true;
			},
		//State of navigation
		isNavigationInit			: false,
		
		//Init NavigatorHandler object
		initNavigation				: function(isFirstInit){
				if (this.isNavigationInit)return;
				if (this.initNavigation.__attemptsCount && !isFirstInit)return;
				
				//10 attempts (represent 5 seconds) to initialize the navigator
				if (!this.initNavigation.__attemptsCount){
						this.initNavigation.__attemptsCount=0;
					}
				if (this.initNavigation.__attemptsCount>=10)return;
				try{
						NavigatorHandler.init();
						this.isNavigationInit=true;
					}
				catch(ex){
						this.initNavigation.__attemptsCount++;
						setTimeout("AskiaScript.initNavigation(true);",500);					
					}
			}
	};
	
//Function to replace all characters of string
//Example of use : 
//replace("Text With Three Spaces"," ","_");
//Will return: "Text_With_Three_Spaces"
function replace(text,match,replacement){
		var sReturn=text;
		if (match=='')return sReturn;
		if (!text)return '';
		if (match==replacement)return sReturn;
		while(sReturn.indexOf(match)!=-1){
				sReturn=sReturn.replace(match,replacement);
			}
		return sReturn;
	}

//Position of elements
var ePosition={
		left	: 'left',
		top		: 'top'
	};
//Return the position of HTMLElement
//Example of use :
//var left=getPosition(document.getElementById("MyElement"),ePosition.left);
//var top=getPosition(document.getElementById("MyElement"),ePosition.top);
//alert("Left=>" + left + "px,Top=>" + top + "px");
function getPosition(htmlElt,pos){
		switch (String(pos).toLowerCase()) { 
			case ePosition.left : 
				var l=htmlElt.offsetLeft;
				var parent=htmlElt.parentNode;
				while (parent.offsetLeft){
						l +=parent.offsetLeft;
						parent=parent.parentNode;
					}
				return l;
				break; 

			case ePosition.top : 
				var t=htmlElt.offsetTop;
				if (isNS6)return t;
				var parent=htmlElt.parentNode;
				var previous=htmlElt;
				while (parent.offsetTop){
						//Don't calculate the position twice
						//For example if my element is a TD, 
						//the offsetTop of TD is the same of the offsetTop of TR
						if (previous.offsetTop!=parent.offsetTop){
								t +=parent.offsetTop;
								previous=parent;
							}
						parent=parent.parentNode;
					}
				return t;
				break; 
		} 
	}

