//script d'impression

var plateforme=navigator.appName.substring(0,2);
function printzeframe() {
if (plateforme=='Mi') {window.parent.popMain.focus();window.print();}
if (plateforme=='Ne') {parent.frames.popMain.print();}
}

// script ouverture pop-up

var popup ;
function openPopup(url,largeur,hauteur){
	gauche=(screen.width-largeur)/2;
	haut=(screen.height-hauteur)/2;
	if (popup && !popup.closed) {popup.close();}
	popup=window.open(url,"Apec","scrollbars=yes,status=no,width="+largeur+",height="+hauteur+",top="+haut+",left="+gauche);
}


var listCandidatures ;

function selectAll() {
	listCandidatures = document.form.getElementsByTagName("INPUT") ;
	for (i=0; i<listCandidatures.length; i++) {
		if (listCandidatures[i].type == "checkbox") {
			listCandidatures[i].checked = true;
		}
	}
}
function unselectAll() {
	listCandidatures = document.form.getElementsByTagName("INPUT") ;
	for (i=0; i<listCandidatures.length; i++) {
		if (listCandidatures[i].type == "checkbox") {
			listCandidatures[i].checked = false;
		}
	}
}
function checkIfSelected() {
	listCandidatures = document.form.getElementsByTagName("INPUT") ;
	tmp = false;
	for (i=0; i<listCandidatures.length; i++) {
		if (listCandidatures[i].type == "checkbox") {
			if ( listCandidatures[i].checked ) tmp = true;
		}
	}
	if ( tmp ) unselectAll();
	else selectAll();
}

function doForm (f) {
	if(f.amis[0].checked) location.href = f.amis[0].value;
	if(f.amis[1].checked) location.href = f.amis[1].value;
	if(f.amis[2].checked) location.href = f.amis[2].value;
	if(f.amis[3].checked) location.href = f.amis[3].value;
	return false;
}

function doForm2 (f) {
	if(f.columnCenterCreatIDSaisie[1].checked) location.href = f.columnCenterCreatIDSaisie[1].value;
	return false;
}


function doForm3 (f) {
	if (f.columnCenterCreatAccomptCand.checked) location.href = f.columnCenterCreatAccomptCand.value;
	if (f.columnCenterCreatAccomptRecrut.checked) location.href = f.columnCenterCreatAccomptRecrut.value;
	return false;
}

function enableComponent(id)	{
	document.getElementById(id).disabled = false;
}

function disableComponent(id)	{
	document.getElementById(id).disabled = true;
}

function confirmAndDisplay(msg) {
	if ( confirm(msg) ) {
		return true;
	}
	return false;
}

function confirmAction(msg, button) {
  if (confirm(msg)) {
     clickButton(button);
  }
}

function clickButton(buttonId) {
  var button = document.getElementById(buttonId);
  if ( button ) {
    if ( button.tagName == "A") {
      return clickLink(buttonId);
    } else {
      button.click();
    }
  }
  // return true; Demande 4175 : doit plutôt renvoyer false
  // de façon à ce que le href ne soit pas exécuté
  return false;
}

function clickLink(linkId) {
  var link = document.getElementById(linkId);
  if ( link ) {
    if (plateforme=='Mi') link.click();
    if (plateforme=='Ne') link.onclick();
  }
  // return true; Demande 4175 : doit plutôt renvoyer false
  // de façon à ce que le href ne soit pas exécuté
  return false;
}



function checkAll() {
  var d=document;
  var el=d.getElementsByTagName('INPUT');
  for(i=0;i<el.length;i++) {
    el[i].checked=true;
  }
}

function uncheckAll() {
  var d=document;
  var el=d.getElementsByTagName('INPUT');
  for(i=0;i<el.length;i++) {
    el[i].checked=false;
  }
}

function checkAllByCaller(id) {
  if ( $(id).checked ) {
    checkAll();
  } else {
    uncheckAll();
  }
}

function isThereChecked() {
  var d=document;
  var el=d.getElementsByTagName('INPUT');
  for(i=0;i<el.length;i++) {
    if ( el[i].checked ) {
      return true;
    }
  }
  return false;
}

function hideElement(bool, idToHide)	{
	if ( bool ) {
		document.getElementById(idToHide).style.display='none';
	}
}

function selectAndSubmit(URL, form){
	form.action = URL;
	form.submit();
}

function hideCombobox(elementName) {
  if(navigator.appName=='Microsoft Internet Explorer') {
    var selectTab=$(elementName).getElementsBySelector("select");
    for (var index = 0, len = selectTab.length; index < len; ++index) {
      var sel = selectTab[index];
      sel.style.visibility='hidden';
    }
  }
}

function showCombobox(elementName) {
  if(navigator.appName=='Microsoft Internet Explorer') {
    var selectTab=$(elementName).getElementsBySelector("select");
    for (var index = 0, len = selectTab.length; index < len; ++index) {
      var sel = selectTab[index];
      sel.style.visibility='visible';
    }
  }
}

function plageAjaxCall(target,zone,param,spinnerOn) {
  var req=new Ajax.Request();
  var opt={
    method:'post',
    parameters:param,
    onCreate:function () {
      if(spinnerOn!='false')  {
        new Insertion.Bottom($(zone),"<img src='../../images/spinner.gif'/>");
      }
    },
    onComplete:function (resp) {
      var xmlResponse=plageAjaxExtractXml(resp.responseXML);
      if(xmlResponse==null) {
        $(zone).innerHTML='';
        new Insertion.Bottom($(zone),resp.responseText);
      } else {
        var responseXML=resp.responseXML;
        if(responseXML.childNodes.length>0) { // ie...  
          responseXML=responseXML.childNodes[responseXML.childNodes.length-1];
        }
        plageAjaxTreeBuild(target,zone,responseXML);
      }
    }
  };
  req.initialize(target,opt);
  return false;
}

function plageAjaxExtractXml(xmlContent) {
  if(xmlContent==null) return null;
  if(xmlContent.childNodes.length>0) { // ie...
    for (var index = 0; index < xmlContent.childNodes.length; index++) {
      if(xmlContent.childNodes[index].nodeName=='plageXml') return xmlContent.childNodes[index]; 
    }
  }
  return null;
}

function plageAjaxTreeBuild(target,zone,xmlResponse) {
  if(xmlResponse.childNodes.length!=1||xmlResponse.childNodes.length!=1||xmlResponse.firstChild.childNodes.length!=4) return ;
  var generatedHtml='';
  var xmlTree=xmlResponse.firstChild;
  var controllerId=xmlTree.firstChild.firstChild.nodeValue;
  var controllerParameters='';
  for (var index = 0; index < xmlTree.childNodes[2].childNodes.length; index++) {
    if(controllerParameters!='') controllerParameters+='&';
    controllerParameters+=xmlTree.childNodes[2].childNodes[index].nodeName+'='+xmlTree.childNodes[2].childNodes[index].firstChild.nodeValue;
  }
  var multipleSelection=xmlTree.childNodes[1].firstChild.nodeValue;
  if(xmlTree.childNodes[3].nodeName=='node') {
    var nodeId=xmlTree.childNodes[3].childNodes[1].firstChild.nodeValue;
    generatedHtml+=plageAjaxNodeBuild(target,zone,controllerId,multipleSelection,controllerParameters,xmlTree.childNodes[3],'false');
    var nodeZone='plageAjaxZone'+nodeId;
    $(nodeZone).innerHTML='';
    new Insertion.Bottom($(nodeZone),generatedHtml);
  } else {
    var listNodeNode=xmlTree.childNodes[3];
    for(index = 0; index < listNodeNode.childNodes.length; index++) {
      generatedHtml+=plageAjaxNodeBuild(target,zone,controllerId,multipleSelection,controllerParameters,listNodeNode.childNodes[index],'true');
    }
    $(zone).innerHTML='';
    new Insertion.Bottom($(zone),generatedHtml);
  }
}

function plageAjaxNodeBuild(target,zone,controllerId,multipleSelection,controllerParameters,xmlElement,generateDiv) {
  var result='';
  if (xmlElement.childNodes.length > 8) {
//    var nodeAction=plageAjaxNodeValue(xmlElement.childNodes[0]);
    var nodeId=xmlElement.childNodes[1].firstChild.nodeValue;
    var nodeLabel=xmlElement.childNodes[2].firstChild.nodeValue;
    var nodeIsSelected=xmlElement.childNodes[3].firstChild.nodeValue;
    var nodeisExpanded=xmlElement.childNodes[4].firstChild.nodeValue;
    var nodeisExpandable=xmlElement.childNodes[5].firstChild.nodeValue;
    var nodeParameters='';
    for (var index = 0; index < xmlElement.childNodes[6].childNodes.length; index++) {
      if(nodeParameters!='') nodeParameters+='&';
      nodeParameters+=xmlElement.childNodes[6].childNodes[index].nodeName+'='+xmlElement.childNodes[6].childNodes[index].firstChild.nodeValue;
    }
    var nodeLink1Parameters;
    for (index = 0; index < xmlElement.childNodes[7].childNodes.length; index++) {
      if(nodeLink1Parameters!='') nodeLink1Parameters+='&';
      nodeLink1Parameters+=xmlElement.childNodes[7].childNodes[index].nodeName+'='+xmlElement.childNodes[7].childNodes[index].firstChild.nodeValue;
    }
    var nodeLink2Parameters;
    for (index = 0; index < xmlElement.childNodes[8].childNodes.length; index++) {
      if(nodeLink2Parameters!='') nodeLink2Parameters+='&';
      nodeLink2Parameters+=xmlElement.childNodes[8].childNodes[index].nodeName+'='+xmlElement.childNodes[8].childNodes[index].firstChild.nodeValue;
    }
    if(generateDiv=='true') result+='\n<div class="nodeTreeItem" id="plageAjaxZone'+nodeId+'">';
    result+='<div class="nodeTreeItemLine">';
    result+='<a class="nodeTreeTog" href="#" onclick="plageAjaxCall(';
    result+='\''+target+'\',';
    result+='\''+zone+'\',';
    result+='\''+controllerParameters+'&'+nodeLink1Parameters+'&'+nodeParameters+'\',';
    result+='\'false\'';
    result+=');';
    result+='return false;\">';
    if(nodeisExpandable=='true') {
      if (nodeisExpanded == 'true') { result += '<img class="nodeTreeTogImg" src="../../images/nodeExpanded.gif" align="center"/>'; }
      else { result += '<img class="nodeTreeTogImg" src="../../images/nodeCollapsed.gif" align="center"/>'; }
    } // else { result += '<img src="../../images/nodeNotExpandable.gif" align="center"/>'; }
    result+='</a>';
    if(multipleSelection == 'true') {
      result+='<a class="nodeTreeChk" href="#" onclick="plageAjaxCall(';
      result+='\''+target+'\',';
      result+='\''+zone+'\',';
      result+='\''+controllerParameters+'&'+nodeLink2Parameters+'&'+nodeParameters+'\',';
      result+='\'false\'';
      result+=');';
      result+='return false;\">';
      if (nodeIsSelected == 'true') { result += '<img class="nodeTreeChkImg" src="../../images/nodeChecked.gif" align="center"/>';}
      else { result += '<img class="nodeTreeChkImg" src="../../images/nodeUnchecked.gif" align="center"/>'; }
      result+='</a>';
    }
    result+='<a class="nodeTreeLbl" href="#" onclick="plageAjaxCall(';
    result+='\''+target+'\',';
    result+='\''+zone+'\',';
    result+='\''+controllerParameters+'&'+nodeLink2Parameters+'&'+nodeParameters+'\',';
    result+='\'false\'';
    result+=');';
    if(multipleSelection != 'true') {
      result+='Modalbox.hide();';
    }
    result+='return false;\">';
    result+='<label ';
    if(nodeIsSelected == 'true') result+= 'class=\'selectedNode\'';
    result+='>';
    result+=nodeLabel;
    result+='</label>';
    result+='</a>';
    result+='</div>';
    if(xmlElement.childNodes.length>9) { //&&(nodeAction=='create'||nodeAction=='open')
      var nodeChildren=xmlElement.childNodes[9];
      for(index = 0; index < nodeChildren.childNodes.length; index++) {
        result+=plageAjaxNodeBuild(target,zone,controllerId,multipleSelection,controllerParameters,nodeChildren.childNodes[index],'true');
      }
    }
    if(generateDiv=='true') result+='</div>';
  }
  return result;
}

function plageAjaxShowHide(id) {
  var displTemp=$(id).style.display;
  var tabtr=$(id).up("table").getElementsBySelector("tr");
  tabtr.each (
    function(elem) {
      var tabelem=elem.getElementsBySelector("td.colspan");
      if(tabelem.length>0) {
        elem.hide();
        tabelem.each(
          function(div) {
            var divdiv=div.down("div",1);
            divdiv.innerHTML='';
          }
        );
      }
    }
  );
  if(displTemp=='none') {
    $(id).show();
    return true;
  } else {
    return false;
  }
}

function windowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return {height:myHeight,width:myWidth};
}

function handleReturnKeyPressedWithAction(evt, actionName) {
  // character code for the key which has been pressed
  var characterCode;
  if (evt.which) {
      characterCode = evt.which;
  } else if (evt.keyCode && evt.srcElement) {
      // IE specific
      characterCode = evt.keyCode;
  } else {
    // unhandled browser
    return true;
  }

  // ascii 13 is the character code for the 'return' key
  if (characterCode && characterCode == 13) {
    var actionElement = document.getElementById(actionName);
    if (actionElement) {
      actionElement.click();
    }
    return false;
  }

  return true;
}


function plageChangeTitle(id){
  if($(id).title == "<pegahtml:internationalization resource='GUI' key='PLAGE_SALON_RECRUTEUR_PLANNING_MARK_UNAVAILABLE'/>")
    $(id).title = "<pegahtml:internationalization resource='GUI' key='PLAGE_SALON_RECRUTEUR_PLANNING_MARK_AVAILABLE'/>";
  else
    $(id).title = "<pegahtml:internationalization resource='GUI' key='PLAGE_SALON_RECRUTEUR_PLANNING_MARK_UNAVAILABLE'/>";
}

function placement(x, id) {
  var strChUserAgent = navigator.userAgent;
  var intSplitStart = strChUserAgent.indexOf("(",0);
  var intSplitEnd = strChUserAgent.indexOf(")",0);
  var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
  if(strChMid.indexOf("MSIE") != -1) {
    if (document.getElementById(id)) {
			document.getElementById(id).style.left = document.getElementById('top').offsetLeft + x;
    }
  }
}

function markAsSeen(id) {
  var fullId = 'IMG_'+id;
  if ( document.getElementById(fullId) ) {
    document.getElementById(fullId).style.display = "inline";
  }
  return true;
}
