// JavaScript Document
var sAgent = navigator.userAgent.toLowerCase() ;
if (sAgent.indexOf("msie") == -1) {
  var IE = 0;
} else { var IE = 1; }

var mouseX = 0;
var mouseY = 0;
function getMouseXY() {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  /*mouseX = tempX;
  mouseY = tempY;*/
  return {x:tempX, y:tempY};
  //alert(mouseX+'x'+mouseY);
}
var mousePos;
if (!IE) { document.onmousemove = mouseMove; }
function mouseMove(ev){
	ev           = ev || window.event;
	mousePos = mouseCoords(ev);
}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

// surcharge de la fonction getElementById pour simplifer le codage
function _Elem(id) {
  return document.getElementById(id);
}

function Show() {
	for (var i = 0; i < arguments.length; ++i) {
		_Elem(arguments[i]).style.display = 'block';
		_Elem(arguments[i]).style.visibility = 'visible';
	}
}

function Hide() {
	for (var i = 0; i < arguments.length; ++i) {
		_Elem(arguments[i]).style.display = 'none';
		_Elem(arguments[i]).style.visibility = 'hidden';
	}
}

function showHide(id) {
	if (_Elem(id).style.display != 'none')
		Hide(id);
	else
		Show(id);
		
}

function ucFirst(str) {
   return str.substr(0,1).toUpperCase() + str.substr(1,str.length);
}

function checkMail(adresse){

var regEx = /([a-z0-9]+([-_]?[a-z0-9]+)*.)*[a-z0-9]+([-_]?[a-z0-9]+)*@([a-z0-9]+([-_]?[a-z0-9]+)*.)+[a-z]{1,4}/gi;  

if (adresse.match(regEx) != adresse ) {

 return false;

} else {

 return true;

}

}

var cp = '';
var old_cp = '';
var villePrx = '';
function doVilles(prx) {
  //var remoteVilles = new villes(villesCallBack);
  cp = _Elem(prx+'code_postal').value;
  if (cp != old_cp){
    //if (cp) { remoteVilles.getvilles(cp); }
    if (cp) { AkisiJax('ville.php?cp='+cp); }
    old_cp = cp ;
	villePrx = prx;
    AkisiAjaxDst = prx+'ville_id';
  }
}
var naf_cat_id = '';
var nafPrx = '';
var AkisiAjaxDst = '';
function getNaf(prx) {
  naf_cat_id = _Elem(prx+'naf_cat_id').value;
    if (naf_cat_id) { AkisiJax('naf.php?id='+naf_cat_id); }
    old_cp = cp ;
	nafPrx = prx;
    AkisiAjaxDst = prx+'naf_id';
}

function AkisiJax(what) {
    var xjs=document.createElement('script');
    xjs.src=what;
    xjs.id = 'AkisiJax';
    document.getElementsByTagName('head')[0].appendChild(xjs);
}

function removeAkisiJaxScript() {
      document.getElementsByTagName("head").item(0).removeChild(_Elem('AkisiJax'));
}


function checkMaquette() {
  nbErr = 0;
  fieldToFocus = 'none';
  aryReq = new Array('nom','telephone','email','delais');
  for (i=0; i<aryReq.length; i++) {
    if (_Elem(aryReq[i]).value) {
      _Elem(aryReq[i]).style.borderColor = "#666666";
    } else {
      _Elem(aryReq[i]).style.borderColor = "#cc0000";
      nbErr++;
      if (fieldToFocus == 'none') fieldToFocus = aryReq[i];
    }
  }
  
  if (nbErr > 0) _Elem(fieldToFocus).focus();
  else return true;
  return false;
}

function checkContact() {
  nbErr = 0;
  fieldToFocus = 'none';
  aryReq = new Array('nom','telephone','email','message');
  for (i=0; i<aryReq.length; i++) {
    if (_Elem(aryReq[i]).value) {
      _Elem(aryReq[i]).style.borderColor = "#000000";
		_Elem(aryReq[i]).style.backgroundColor = "#ffffff";
    } else {
      _Elem(aryReq[i]).style.borderColor = "#ff0000";
		_Elem(aryReq[i]).style.backgroundColor = "#EACEB5";
      nbErr++;
      if (fieldToFocus == 'none') fieldToFocus = aryReq[i];
    }
  }
  
  if (nbErr > 0) _Elem(fieldToFocus).focus();
  else return true;
  return false;
}

function checkBdc() {
  nbErr = 0;
  fieldToFocus = 'none';
  aryReq = new Array('bdc_nom', 'bdc_prenom', 'bdc_adresse', 'bdc_code_postal', 'bdc_ville',
                     'bdc_telephone', 'bdc_email', 'bdc_destination', 'bdc_date_depart', 'bdc_date_retour', 'bdc_frais_consulaires', 'bdc_frais_retour');
  for (i=0; i<aryReq.length; i++) {
    if (_Elem(aryReq[i])) {
      if (_Elem(aryReq[i]).value) {
        _Elem(aryReq[i]).style.borderColor = "#000000";
      } else {
        _Elem(aryReq[i]).style.borderColor = "#ff0000";
        nbErr++;
        if (fieldToFocus == 'none') fieldToFocus = aryReq[i];        
      }
    }
  }
  
  // frais consulaires..
  if (_Elem('bdc_frais_consulaires').value && (_Elem('bdc_frais_consulaires').value*1 == _Elem('bdc_frais_consulaires').value)) {
    _Elem('bdc_frais_consulaires').style.borderColor = "#000000";
  } else {
    _Elem('bdc_frais_consulaires').style.borderColor = "#ff0000";
    nbErr ++;
    if (fieldToFocus == 'none') fieldToFocus = 'bdc_frais_consulaires';    
  }
  
  if (_Elem('bdc_frais_retour').value && (_Elem('bdc_frais_retour').value*1 == _Elem('bdc_frais_retour').value)) {
    _Elem('bdc_frais_retour').style.borderColor = "#000000";
  } else {
    _Elem('bdc_frais_retour').style.borderColor = "#ff0000";
    nbErr ++;
    if (fieldToFocus == 'none') fieldToFocus = 'bdc_frais_retour';    
  }
  
  if (nbErr > 0) { _Elem(fieldToFocus).focus(); return false; }
  else return true;
}


function imgOpacity(obj,alpha) {
    if (obj.firstChild.className == 'offreLienOff') {
    obj.firstChild.style.opacity = alpha;
    obj.firstChild.style.MozOpacity = alpha;
    obj.firstChild.style.filter = "alpha(opacity="+(alpha*100)+")";
    }
}

function changeFraisRetour(val) {
  tmp = val.split(':');
  if (tmp[1] *1 > 0) {
    _Elem('bdc_mode_expedition').value = tmp[0];
  _Elem('bdc_frais_retour').value = tmp[1];
  
  } else {
    _Elem('bdc_mode_expedition').value = tmp[0];
    _Elem('bdc_frais_retour').value = 'nous consulter';
  }
}

function Move(id, X, Y) {
         _Elem(id).style.left = X;
         _Elem(id).style.top = Y;
}
function createAkisiBulle() {
         tmp = document.createElement('div');
         tmp.id = 'AkisiBulleDiv';
         tmp.style.position = 'absolute';
         tmp.style.zIndex = '1000';
         document.body.appendChild(tmp);
         if (IE) {
         tmp2 = document.createElement('iframe');
         tmp2.id = 'AkisiBulleIframe';
         tmp2.style.position = 'absolute';
         tmp2.style.height = '25px';
         tmp2.style.zIndex = '999';
         document.body.appendChild(tmp2);
         }         
}
function HideAkisiBulle() {_Elem('AkisiBulleDiv').innerHTML = ''; Hide('AkisiBulleDiv'); if (IE) Hide('AkisiBulleIframe'); }
function AkisiBulleFollowMouse() {
 if (IE) mousePos = getMouseXY();
 Move('AkisiBulleDiv',mousePos.x+5, mousePos.y+5);
 if (IE) {
 _Elem('AkisiBulleIframe').style.width = _Elem('AkisiBulleDiv').clientWidth; 
 _Elem('AkisiBulleIframe').style.height = _Elem('AkisiBulleDiv').clientHeight;
 Move('AkisiBulleIframe',mousePos.x+5, mousePos.y+5);
 }
}
function AkisiBulle(obj, texte) {
 if (!_Elem('AkisiBulleDiv')) { createAkisiBulle() ;}
 Show('AkisiBulleDiv') 
 if (IE) Show('AkisiBulleIframe');
 if (IE) { 
    document.onmousemove = AkisiBulleFollowMouse;
    obj.onmouseout = HideAkisiBulle;
 } else {
    document.addEventListener('mousemove', AkisiBulleFollowMouse, true);
    obj.addEventListener('mouseout', HideAkisiBulle, true);
  } 
 _Elem('AkisiBulleDiv').innerHTML = texte;
}

var Texte = new Array();
Texte['date'] = 'Entrez une date au format:<br>jj/mm/aaaa';
Texte['frais_consulaires'] = 'Indiquez ici le montnant des frais consulaires pour votre visa. Vous le trouverez dans la partie <b>Tarifs</b> de notre site.';
Texte['frais_retour'] = 'Indiquez ici le montnant des frais de retour qui varient suivant le mode de livraison de votre visa. Vous le trouverez dans la partie <b>Tarifs</b> de notre site.';

function Status(msg) {
  return void(0);
}


function checkArray(required) {
    var nbErr = 0;
    for (i=0; i < required.length; i++) {
        if (required[i].indexOf('|') > 0 ) {
            var tmp = required[i].split('|');
            var eee = 0;
            for (j=0; j<tmp.length; j++) {
                if (_Elem(tmp[j]).value.length > 0 || _Elem(tmp[j]).value > 0) {
                    eee++;
                }
            }
            for (j=0; j<tmp.length; j++) {
                if (eee) {
                // ok
                _Valid(tmp[j]);
                } else {
                // ko
                _Error(tmp[j]);
                nbErr++;
                }
            }
        } else if (required[i].indexOf('&') > 0 ) {
            var tmp = required[i].split('&');
            var eee = 0;
            var a = _Elem(tmp[0]).value;
            for (j=0; j<tmp.length; j++) {
                if ( _Elem(tmp[j]).value.length == 0 || _Elem(tmp[j]).value != a) eee++;
            }
            
            for (j=0; j<tmp.length; j++) {
                if (eee) {
                // ko
                _Error(tmp[j]);
                nbErr++;
                } else {
                // ok
                _Valid(tmp[j]);
                }
            }
        } else {
            if (_Elem(required[i]).value == 0 || _Elem(required[i]).value == '') {
              nbErr++;
              _Error(required[i]);
            } else {
             _Valid(required[i]);
            }
        }
	}
    return nbErr;
}

function checkMessage() {
    var required = new Array('nom','telephone','email', 'message');
    
    var nbErr = checkArray(required);
    
    return nbErr == 0;
}

function _Error(id) {
    if (id.indexOf('_input') > 0) {
        _Elem(id.replace(/_input/,'_str')).style.borderColor = '#FF0000';   
    } else {
    _Elem(id).style.borderColor = '#FF0000';
    }
}
function _Valid(id) {
    if (id.indexOf('_input') > 0) {
        _Elem(id.replace(/_input/,'_str')).style.borderColor = '#AAAAAA';   
    } else {
    _Elem(id).style.borderColor = '#AAAAAA';
    }    
}

var selectDefVal = new Array();
function doSelect(nom, optObj){
	strNew = '';
  aryOpt = getElementsByClass('opt'+nom, _Elem('opt'+nom));

	for (ii = 0 ; ii < aryOpt.length; ii++) {
			if (aryOpt[ii].checked == true) {
        	HTMLStr = aryOpt[ii].parentNode.innerHTML.split('&nbsp;') ;
					strNew += ', '+HTMLStr[1];
			}
		}
	_Elem('input'+nom).value=strNew.replace(/^, /,'');
  if (!_Elem('input'+nom).value) _Elem('input'+nom).value = selectDefVal[nom];
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function checkSendDevis() {
 nbErr = 0;
 aryReq = new Array('devisNom', 'devisCodePostal','devisTelephone','devisEmail');
 for (i=0; i < aryReq.length; i++){
 	if (_Elem(aryReq[i]).value.length == 0) {
	 	_Elem(aryReq[i]).style.borderColor = "#ff0000";
	 	_Elem(aryReq[i]).style.borderStyle = "solid";
	 	_Elem(aryReq[i]).style.borderWidth = "1px";
		_Elem(aryReq[i]).style.padding = "1px";
	 	nbErr++;
	}
 	else {
  	_Elem(aryReq[i]).style.borderColor = "";
	}
 }

// if (_Elem("devisTelephone").value.length || _Elem("devisEmail").value.length) {
// 		_Elem("devisTelephone").style.borderColor = "";
//  	_Elem("devisEmail").style.borderColor = "";
// } else {
//  	_Elem("devisTelephone").style.borderColor = "#ff0000";
//	 	_Elem("devisTelephone").style.borderStyle = "solid";
//	 	_Elem("devisTelephone").style.borderWidth = "1px";
//	 	_Elem("devisEmail").style.borderColor = "#ff0000";
//	 	_Elem("devisEmail").style.borderStyle = "solid";
//	 	_Elem("devisEmail").style.borderWidth = "1px";
//    nbErr++; 
// }

if (nbErr == 0) return true;
else {
  return false;
}
}

function verifCandidature() {
    var required = new Array('email', 'nom', 'prenom', 'code_postal', 'dnJJ', 'dnMM', 'dnAAAA', 'telephone');
	
    var nbErr = checkArray(required);
    
	 
	fieldTofocus = 'none';
   
    // traitements particuliers
	 if (!(checkMail(_Elem('email').value)==true)) {
        _Elem('email').style.borderColor = '#FF0000';
        nbErr ++;
		if (fieldTofocus == 'none') fieldTofocus = 'email';
    } else { _Elem('email').style.borderColor = '#999999'; }
	
	if (_Elem('ville_id').value > 0) {
        _Elem('ville_id').style.borderColor = '#999999';      
	} else {
		_Elem('ville_id').style.borderColor = '#FF0000';
        nbErr ++;
		if (fieldTofocus == 'none') fieldTofocus = 'ville_id';
	}
    
    if (_Elem('dnJJ').value && _Elem('dnMM').value && _Elem('dnAAAA').value) {
        myDate = new Date(_Elem('dnAAAA').value,_Elem('dnMM').value-1,_Elem('dnJJ').value);
        if ( (myDate.getDate() == _Elem('dnJJ').value) &&
            ((myDate.getMonth()+1) == _Elem('dnMM').value) &&
            (myDate.getFullYear() == _Elem('dnAAAA').value) ) {
            _Elem('dnJJ').style.borderColor = '#999999';
            _Elem('dnMM').style.borderColor = '#999999';
            _Elem('dnAAAA').style.borderColor = '#999999';
        } else {
            _Elem('dnJJ').style.borderColor = '#FF0000';
            _Elem('dnMM').style.borderColor = '#FF0000';
            _Elem('dnAAAA').style.borderColor = '#FF0000';
            nbErr++;
			if (fieldTofocus == 'none') fieldTofocus = 'dnJJ';
        }
    }
	
    // CV
    if (_Elem('cv').value != '') {
        tmp = _Elem('cv').value.split('.');
        if (tmp[tmp.length-1] == 'pdf' || tmp[tmp.length-1] == 'doc' || tmp[tmp.length-1] == 'txt') {
            _Elem('cv').parentNode.style.border = "0px";
        } else {
            _Elem('cv').parentNode.style.border = "1px solid #ff0000";
            nbErr++;
            if (fieldTofocus == 'none') fieldTofocus = 'cv';
        }        
    } else {
        //alert(_Elem('cv').value);
        _Elem('cv').parentNode.style.border = "1px solid #ff0000";
        nbErr++;
        if (fieldTofocus == 'none') fieldTofocus = 'cv';
    }
	
	if (fieldTofocus != 'none') _Elem(fieldTofocus).focus();
    if (nbErr == 0) return true;
    return false;
}

function fillSelect(ary) {
    var sel = _Elem(AkisiAjaxDst);
    sel.length=0;

	if (!ary) {
		var res = document.createElement('option');
        res.value=0;
        res.innerHTML='Pas de resultat';
        sel.appendChild(res);
	}
    else {
      for (var i = 0; i < ary.length; ++i) {
        var res = document.createElement('option');
        res.value=ary[i]['id'];
        res.innerHTML=ary[i]['nom'];
        sel.appendChild(res);
      }
    }
    removeAkisiJaxScript();
    AkisiAjaxDst = '';
  }
