/* COMMON JAVASCRIPT */
var Debug = false;

/**
  * Get element by id
  * @param string id, the element id to get
  * @return wanted element or false if not founded
*/
if (typeof 'getE' != 'function') {
	function getE(id) {
		if (el = document.getElementById(id)) { 
			return el;
		} else {
			return false;
		}
	}
}

/* Cross-browser onLoad function */
if (typeof 'addLoadListener' != 'function') {
	function addLoadListener(func) {
		if (window.addEventListener) {
			window.addEventListener("load", func, false);
		} else if (document.addEventListener) {
			document.addEventListener("load", func, false);
		} else if (window.attachEvent) {
			window.attachEvent("onload", func);
		}
	}
}

/**
  * Add an event on a given object
  * @param html object obj, the object to add event on
  * @param string evType, the event type to add (click, mouseover, blur, etc.)
  * @param string fcn, the function name to call on event
  * @return boolean true on success, false on failure
  */
function addEvent(obj, evType, fcn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fcn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fcn);
		return r;
	} else {
		return false;
	}
	return true;
}

/* Switch display */
if (typeof 'switchBox' != 'function') {
	function switchBox(box, displayType){
		if (!displayType) {
			displayType = 'block';
		}
		if(getE(box)){
			box = getE(box);
			if(box.style.display == displayType){
				box.style.display = 'none';
			} else {
				box.style.display = displayType;
			}
		}
	}
}

/* Treat projectFormEdit */
if (typeof 'init' != 'function') {
	function init(){
		getE('projectIdentificationButton').onclick = function(){
			switchBox('projectIdentification');
			return false;
		}
		/*if(getE('openDetailsBox')){
			switchBox('more');
		}*/
		/*getE('projectNeedsButton').onclick = function(){
			switchBox('projectNeeds');
			return false;
		}
		getE('projectSupportButton').onclick = function(){
			switchBox('projectSupport');
			return false;
		}
		getE('projectDescriptionButton').onclick = function(){
			switchBox('projectDescription');
			return false;
		}*/
		getE('projectDetailsButton').onclick = function(){
			switchBox('projectDetails');
			return false;
		}
		getE('projectImageButton').onclick = function(){
			switchBox('projectImage');
			return false;
		}
		getE('moreButton').onclick = function(){
			switchBox('more');
			return false;
		}
		getE('projectDatesButton').onclick = function(){
			switchBox('projectDates');
			return false;
		}
	}
}
addLoadListener(init);

/**********************************************
* Browser definition                          *
**********************************************/
var browserOK = false;
if (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && navigator.userAgent.toLowerCase().indexOf('opera') == -1) {
	var isIE = true;
	var menutimer;
	//get IE version
	if (navigator.userAgent.toLowerCase().indexOf("msie 5.5") != -1) {
		var isIE55 = true;
		browserOK = true;
	} else if (navigator.userAgent.toLowerCase().indexOf("msie 6") != -1)  {
		var isIE6 = true;
		browserOK = true;
	} else if (navigator.userAgent.toLowerCase().indexOf("msie 7") != -1)  {
		var isIE7 = true;
		browserOK = true;
	}
	var timerdiv;
	var timerli;
	var elementdiv;
	var elementli;
} else if (navigator.userAgent.toLowerCase().indexOf('opera') != -1) {
	//not compatible for now (tried with opera 8 & 9)
	var isOp = true;
} else if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1 || 
		  (navigator.userAgent.toLowerCase().indexOf('mozilla') != -1 && (navigator.userAgent.toLowerCase().indexOf('rv:1.7') != -1 || navigator.userAgent.toLowerCase().indexOf('rv:1.8') != -1))) {
	browserOK = true;
	var isMoz = true;
} else if (navigator.userAgent.toLowerCase().indexOf('safari') != -1){
	browserOK = true;
	var isSafari = true;
}

/* add some events on menu for all browsers */
function startMenu() {
	var e = getE('selectProject');
	var box = getE('selectProjectList');
	if(e && box){
		// Need for page onload
		if(box.offsetHeight > 200){
			box.style.height = '200px';
			box.style.width = '220px';
			box.style.overflow = 'auto';
		}
		if(isSafari){
			box.style.height = '200px';
			box.style.width = '220px';
			box.style.overflow = 'auto';
		}
		// Events
		if(!isSafari){
			addEvent(e,'mouseover',function() {
				box.style.display = 'block';
				if(box.offsetHeight > 200){
					box.style.height = '200px';
					box.style.width = '220px';
					box.style.overflow = 'auto';
				}
		    });
		  	addEvent(e,'mouseout',function() {
		  		box.style.display = 'none';
		    });
		}
	}
}
addEvent(window, 'load', function(){
	startMenu();
	return true;
});
function alertOnSubmit(){
	
}

// function sanitizeWindowsChars()
function sanitizeWindowsChars(text){
	var text = text;
	var fTab = new Array('‚','ƒ','„','…','†','‡','ˆ','‰','Š','‹','Œ','‘','’','´','“','”','•','–','—','˜','™','š','›','œ','Ÿ','€','ÿ');
	var rTab = new Array(',','f','"','...','+','!=','^','%%','S','<','OE','\'','\'','\'','"','"','-','-','-','~','TM','s','>','oe','Y','€','Y');
	for(var i=0 ; i<fTab.length ; i++){
		var reg = new RegExp(fTab[i],"g");
		while(reg.test(text)){
			text = text.replace(fTab[i],rTab[i]);
		}
	}
	return text;
}