/* COMMON JAVASCRIPT */

/**
  * 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;
		}
	}
}

/**
  * 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;
}


/* PAGE EVENTS */
// Style for submit button
/*
addEvent(window, 'load', function(){
	var e = getE('newssearchSubmit');
	var c = getE('docsSubmitContinue');
	if(e){
		addEvent(e, 'mouseover', function(){
			e.src = '/img/modules/pnews/validerON.gif';
		});
		addEvent(e, 'mouseout', function(){
			e.src = '/img/modules/pnews/valider.gif';
		});
	}
	if(c){
		addEvent(c, 'mouseover', function(){
			c.src = '/img/modules/pdocs/poursuivreON.gif';
		});
		addEvent(c, 'mouseout', function(){
			c.src = '/img/modules/pdocs/poursuivre.gif';
		});
	}
});
*/
// Event for search form
addEvent(window, 'load', function(){
	var e = getE('q');
	if(e){
		addEvent(e, 'click', function(){
			if(e.value == 'Mots clés'){
				e.value = '';
			}
		});
		addEvent(e, 'blur', function(){
			if(e.value == ''){
				e.value = 'Mots clés';
			}
		});
	}
});
// Event for edition form
addEvent(window, 'load', function(){
	var e = getE('pcarnetsChildren');
	var p = getE('pcarnetsChildrenField');
	if(p){
		p.className = 'childON';
	}
	if(e){
		for(var m=2 ; m<=5 ; m++){
			var box = getE('child'+m);
			box.className = 'child';
		}
		if(e.selectedIndex){
			for(var k=1 ; k<=e.selectedIndex+1 ; k++){
				var box = getE('child'+k);
				box.className = 'childON';
			}
		}
		addEvent(e, 'change', function(){
			if(e.value){
				for(var i=1 ; i<=e.value ; i++){
					var box = getE('child'+i);
					box.className = 'childON';
				}
			}
			for(var j=(parseInt(e.value))+1 ; j<=5 ; j++){
				var box = getE('child'+j);
				if(box.className == 'childON'){
					inputs = box.getElementsByTagName('input');
					inputs[0].value = '';
					cats = box.getElementsByTagName('select');
					cats[0].selectedIndex = '';
					box.className = 'child';
				}
			}
		});
	}
});
// Event for all pages
addEvent(window, 'load', function(){
	// Authentication form
	var form = getE('cms_forms_3');
	if(form){
		tab = form.getElementsByTagName("input");
		if(tab.length){
			for(var i=0 ; i<tab.length ; i++){
				if(tab[i].type == 'text'){
					e = tab[i];
					break;
				}
			}
			if(e){
				addEvent(e, 'click', function(){
					if(e.value == 'Identifiant'){
						e.value = '';
					}
				});
				addEvent(e, 'blur', function(){
					if(e.value == ''){
						e.value = 'Identifiant';
					}
				});
			}
		}
	}
	// Print link
	var print = getE('print');
	if(print){
		print.style.display = 'inline';
		printLinks = print.getElementsByTagName('a');
		if(printLinks[0]){
			addEvent(printLinks[0], 'click', function(){
				window.print();
			});
		}
	}
});


// jQuery
if (typeof jQuery != 'undefined') {
	$(document).ready(function(){
		// colorBox events
		var compareEntreprises = $('.compareEntreprises');
		if(compareEntreprises.length > 0){
			compareEntreprises.colorbox({
				iframe: true,
				width:650,
				height:600,
				title:'Comparatif entreprise'
			});
		}

		//gestion des indicatifs pays (entreprise et ingenieur)
		/*$("#c,#cPoste,#copays_fk").change(function(){

		   
		   parentFieldset = $(this).parents('fieldset , #divFormAnnuaire');

		   
		   index = $('option:selected',this).val();
		   
		   //on met a jour les indicatifs
		   if (indPays[index]['code'] != 0) {
					   $('.indic',parentFieldset).val(indPays[index]['code']);
		   }
		   
		   contInt = $('.indInt',parentFieldset);
		   contFr 	= $('.indFR',parentFieldset);
		   if(index == 'FR'){
				 contInt.css({display:'none'});
				 contFr.css({display:'block'});	
		   }else{
				 contInt.css({display:'block'});
				 contFr.css({display:'none'});
		   }
		   
		   //pour chaque num on met a jour l'affichage
		   $(".reference",parentFieldset).each(function(){
			   sInputId = this.id;
			   sNumRef = $(this).val();
			   
			   //mise a jour num international
			   $(".indInt .a_"+sInputId,parentFieldset).val(sNumRef);
			   
			   //mise a jour num fr
			   var aNum = sNumRef.match(/^\+?(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)\d*$/);
			   var aNumInt = sNumRef.match(/^([\d]{1,12})$/);
			  
			   var iNumFrIndex = 0;

			   if(aNum){
				   aNum.shift();
				   $(".indFR .a_"+sInputId,parentFieldset).each(function(){
					   $(this).val( aNum[iNumFrIndex] );
					   iNumFrIndex++; 
				   });
			   }

			   if (
				   (
					(!aNum && index == 'FR')
					||
					(index != 'FR' &&  !aNumInt )
				   )
				   &&
				   sNumRef
				) {
				//le numéro n'est pas valide on passe dans le cas d'un num invalide
					$('.unmatched' , $(this).closest('.col1') ).css('display','block');
			   }
		   });
		});
		
		//mise a jour des numeros
		$('.indInt input, .indFR input').blur(function(){
			aNum = '';
			numContainer = $(this).parents('.col1');
			parentType = $(this).parents('.indFR , .indInt');
			
			if (parentType.hasClass('indFR') ) {
				$('input',parentType).each(function(){
					aNum += $(this).val();	
				});
			}else{
				aNum = $('input:not(".indic")',parentType).val();
			}
			
			$('input',parentType).each(function(){
					aNum += $(this).val();	
			});
			$('.reference',numContainer).val(aNum);
		});*/
		if ($.mask) {
			$.mask.rules['s'] = /[0-9\s]/;
			$.mask.masks.num = {mask: 'ssssssssssss'};
			$.mask.masks.num2 = {mask: '99'};
			$(function(){
				$('.indInt input , .indFR input').setMask();
			});
		}
	});
}
