var L1=null;
var L2=null;
var L3=null;
var L4=null;
var L5=null;
var L6=null;
var L7=null;
var L8=null;
var L9=null;
var L10=null;

var Layer1 = "Header";
var Layer2 = "Content";
var Layer3 = "CountrySelection";
var Layer4 = "SubHeader";
var Layer5 = "ComSol";
var Layer6 = "Footer";
var Layer7 = "SolutionType";
var Layer8 = "SubdivisionLabel";
var Layer9 = "Specialization";
var Layer10 = "AssistedSubmit"

var formTab1;
var formTab2;
var formBusinessSize;
var formSolDeployment;
var formSpecialization;
var formSubmit;
var formSubdivision;
var layerSubdivisionLabel;
var layerSubdivision;

var layerUser = null;
var layerUserName = "";
var layerSpecialization = null;
var layerSpecializationName = "";

var NS4 = false;
var NS6 = false;
var IE = false;

if (document.all)
  IE = true;
else
  NS6 = true;  

function displayLayer(layerVar){
    layerVar.style.visibility = 'visible';
    layerVar.style.display    = 'block';
}

function hideLayer(layerVar){
    layerVar.style.visibility = 'hidden';
    layerVar.style.display    = 'none';
}

//alert("Layers.js NS4="+NS4+" NS6="+NS6+" IE="+IE);

//----------------------------------------------------------
// Displays the number of users radio buttons
//----------------------------------------------------------
function  displayUserNumberLayer (layerName) {
  if (layerUser != null) {
    //We remove the options selected
    //We unchek all options
    for(i=0; i < formBusinessSize.FldBusinessSize.length; i++){
      if (formBusinessSize.FldBusinessSize[i].checked == true)
        formBusinessSize.FldBusinessSize[i].checked = false;
    }
    //If we hide the user number layer, we must also hide the Spezialization layer
    displaySpecializationLayer('')
    hideLayer(layerUser);
    layerUser = null;
  }
  
  layerUserName = layerName;
  if (layerName != '') {  
    if (NS6) {
      eval ("layerUser = document.getElementById('" + layerName + "')");
    }
    else {
      eval ("layerUser = document.all." + layerName); 
    }
    displayLayer(layerUser);
  }
}

//----------------------------------------------------------
// Displays the specialization Layer
//----------------------------------------------------------
function  displaySpecializationLayer (layerName) {
  if (layerSpecialization != null) {
    //We unchek all options
    for(i=0; i < formSpecialization.FldSpecialization.length; i++){
      if (formSpecialization.FldSpecialization[i].checked == true)
        formSpecialization.FldSpecialization[i].checked = false;
    }
    hideLayer(layerSpecialization);
    layerSpecialization = null;
  }
  
  layerSpecializationName = layerName;
  if (layerName != '') {  
    if (NS6) {
      eval ("layerSpecialization = document.getElementById('" + layerName + "')");
    }
    else {
      eval ("layerSpecialization = document.all." + layerName);
    }
    displayLayer(layerSpecialization);           
  }
}

//----------------------------------------------------------
// Initialize Layers position
//----------------------------------------------------------
function start(origin) {		
  if (origin == "form") {       	
  	initFormLayers(); 
    //To prevent an error message when user presses the "Return" key
    document.action = "";
  }
	//alert("Start");
  //Layers initialization
  
  if (IE) {
    //Initialize all layers objects
    eval("L1=document.all."+Layer1); //Banner
    eval("L2=document.all."+Layer2); //Content
    eval("L3=document.all."+Layer3); //CountrySelection
    eval("L4=document.all."+Layer4); //SubHeader
    eval("L5=document.all."+Layer5); //ComSol
    eval("L6=document.all."+Layer6); //Footer
    eval("L7=document.all."+Layer7); //SolutionType layer
    eval("L8=document.all."+Layer8); //SubdivisionLabel
    eval("L9=document.all."+Layer9); //SubdivisionLabel
    eval("L10=document.all."+Layer10); //SubdivisionLabel
    
    H1=L1.offsetHeight;
    //H2=L2.offsetHeight;
    //position layer Content
    L2.style.top = H1;
    //displayLayer(L2);
  }
  
  if (NS6) {
    L1=document.getElementById(Layer1);
    L2=document.getElementById(Layer2);
    L3=document.getElementById(Layer3);
    L4=document.getElementById(Layer4);
    L5=document.getElementById(Layer5);
    L6=document.getElementById(Layer6);
    L7=document.getElementById(Layer7);
    L8=document.getElementById(Layer8);
    L9=document.getElementById(Layer9);
    L10=document.getElementById(Layer10);
    
    H1=L1.offsetHeight;
    //H2=L2.offsetHeight;
    //position layer Content
    L2.style.top = H1;
//    displayLayer(L2);     
  }  
  
  
  //if the user makes a back, we can't use the query string that is either not filled
  // or not valid.
  var comingFromBack;
  comingFromBack = false;
  if (origin == "form"){
    //We check if the form contains alread a value for the Solution type field
    //if so, we are sure the user made a back jsut after an assisted search
    //In case of an fulltext search, we have no problem because there is no hidden layer
    if (formTab1){
      for(i=0; i < formTab1.FldSolutionType.length; i++){
        if (formTab1.FldSolutionType[i].checked){
          comingFromBack = true;
          break;
        } 
      }
    }
    //We must also test if the user comes back after an fulltext search.
    if (formTab2){
      if (formTab2.FldCompanyName.value != '')
        comingFromBack = true;
    }
  }
  
  if ((origin == "form") && (comingFromBack == false)){
    //initFormValues uses CGI parameter so we don't call if there is no param
    initFormValues();
  }
  else if (origin == "form"){
   //Check if a user value is selected
    for(i=0; i < formBusinessSize.FldBusinessSize.length; i++){
      if (formBusinessSize.FldBusinessSize[i].checked == true){
        displayUserNumberLayer('ComSol');
        //if the user has selected > than 200 users, we must display the specialization
        if (i == 1){
          displaySpecializationLayer('Specialization');
        } 
        break;
      }
    }
    //The country is not a hidden field but, if country has departments, we must 
    // display the departments layer
    if (countryHasDepartments( getSelectedCountry() )){
      //we must rebuild the departments list
      initValueinListe(formSubdivision.FldDepartments, getSubdivisionArray(getSelectedCountry()));
      //and reselect the value by using the hidden field
      formSubdivision.FldDepartments.selectedIndex = formSubdivision.selectedDept.value;
      showDepartments( true );
    }
  }
}

//----------------------------------------------------------
// DRG 15/12/2003 PartnersLocator V1.8
// Retourne la liste des départements sélectionnés au format Code|Label
//----------------------------------------------------------
function initFormValues(){
    //Selecting current tab regarding search method
    //Full Text method
    if (findCGIParam("&method=").toLowerCase() == "fulltext") {    	      
      
      //Filling company field 	
      formTab2.FldCompanyName.value = findCGIParam("&company=");      
      
    }
    //Assisted search method
    else {
      	
      //Checking Solution Type checkboxes      
      solutionTypeIndexes = findCGIParam("&ST=");      
      if (solutionTypeIndexes != "") {      	
      	currentIndex = 0;      	
      	do {
      	  foundAtPosition = solutionTypeIndexes.indexOf(";");
      	  if (foundAtPosition == -1){
      	    solutionTypeIndex = solutionTypeIndexes;
      	    solutionTypeIndexes = "";      	   
      	  } else {
      	    solutionTypeIndex = solutionTypeIndexes.substring(0, foundAtPosition);
      	    solutionTypeIndexes = solutionTypeIndexes.substring(foundAtPosition + 1);      	   
      	  }
      	 
      	  if (solutionTypeIndex != "") {
      	    for (i=currentIndex; i<solutionTypeIndex; i++) { 
      	      formTab1.FldSolutionType[i].checked = false;      	    
      	    }
      	    formTab1.FldSolutionType[solutionTypeIndex].checked = true;        	    	  
      	    currentIndex = ++solutionTypeIndex;      	        	    
      	  }
      	  else 
      	    solutionTypeIndexes = "";      	  
      	} while (solutionTypeIndexes != "")        
      }    	
    	
      //Displaying Users Number Layer
      usersNumberLayer = findCGIParam("&UP=");
      if (usersNumberLayer != "") {
        displayUserNumberLayer (usersNumberLayer);
      
        //Checking Users Number radio buttons
        usersNumberIndex = findCGIParam("&STU=");      
        if ((usersNumberIndex != "") && (usersNumberIndex != -1)) {
          if (NS6)
            radioButton = formBusinessSize.FldBusinessSize;
          else 
            eval("radioButton = layerUser.all.FldBusinessSize");
            
          if (radioButton != null) radioButton[usersNumberIndex].checked = true;
          
          //if user has validated choce 1 (> 200 users) we must display specialization layer
          if (usersNumberIndex == '1'){
            displaySpecializationLayer('Specialization');
            specializationTypeIndexes = findCGIParam("&APP=");  
            if (specializationTypeIndexes != ""){
              do {
      	       foundAtPosition = specializationTypeIndexes.indexOf(";");
      	       if (foundAtPosition == -1){
      	         specializationTypeIndex = specializationTypeIndexes;
      	         specializationTypeIndexes = "";      	   
      	       } else {
      	         specializationTypeIndex = specializationTypeIndexes.substring(0, foundAtPosition);
      	         specializationTypeIndexes = specializationTypeIndexes.substring(foundAtPosition + 1);      	   
      	       }
      	 
      	       if (specializationTypeIndex != "") {
      	         formSpecialization.FldSpecialization[specializationTypeIndex].checked = true;        	    	        	              	        	    
      	       }
      	       else 
      	         specializationTypeIndexes = "";      	  
      	     } while (specializationTypeIndexes != "")  
            }
          }
        }
        //setTimeout("for (i=0;i<formTab1.FldSolutionType.length;i++) if (formTab1.FldSolutionType[i].checked == true) {  formTab1.FldSolutionType[i].checked = false; formTab1.FldSolutionType[i].click(); break;}", 50);
      }
     
      //Selecting current country
      setSelectedCountry( findCGIParam("&SC=") );
      
      // Initialize Departments combobox and show/hide them
	  countryChange();
	  var selectedSubdivisions = formatSubdivision( findCGIParam("&Dept=") );
	  
	  if (selectedSubdivisions)
      	initComboSubdivision(formSubdivision.FldDepartments , selectedSubdivisions );
    }
}	
  	
//----------------------------------------------------------
// DRG 15/12/2003 PartnersLocator V1.8
// Initialise les variables Form utilisés dans les layers
//----------------------------------------------------------
function initFormLayers(){
    //Setting tabs form variables
    formTab1 = document.frmTab1;
    formTab2 = document.frmTab2;
    formSolDeployment = document.frmSolDeployment;
    formSpecialization = document.frmSpecialization; 
    formSubdivision = document.frmSubdivision;
    formBusinessSize = document.frmBusinessSize; 
            
    if (IE) {
       layerSubdivisionLabel = document.all.SubdivisionLabel;
       layerSubdivision = document.all.Subdivision;
    } 
    else { // NS6
       layerSubdivisionLabel = document.getElementById('SubdivisionLabel');
       layerSubdivision = document.getElementById('Subdivision');
    }	 
}

//----------------------------------------------------------
// DRG 01/12/2003 PartnersLocator V1.8
// Retourne la liste des départements sélectionnés au format Code|Label
//----------------------------------------------------------
function formatSubdivision( strSubdivision ){
	if ((strSubdivision==null) || (strSubdivision=="")) return null;
	
	var lstCode = strSubdivision.split(",");
	
	for (var iCode=0; iCode<lstCode.length; iCode++)
		lstCode[iCode] = getSubdivisionRecord( lstCode[iCode] );
	return lstCode;
}

//----------------------------------------------------------
// DRG 01/12/2003 PartnersLocator V1.8
// Retourne un enregistrement de subdivision au format Code|Label en fonction du code
//----------------------------------------------------------
function getSubdivisionRecord( strCode ){
	var lstRecord = getSubdivisionArray();
	if (lstRecord){
		var nbRecords = lstRecord.length;
		var recCode;
		for (var iRec=0; iRec<nbRecords; iRec++){
			recCode = lstRecord[iRec].split("|")[1];
			if ( recCode == strCode )
				return lstRecord[iRec];
		}
    }
	return strCode;	
}

//----------------------------------------------------------
// DRG 01/12/2003 PartnersLocator V1.8
// Retourne la liste des départements du pays sélectionné
//----------------------------------------------------------
function getSubdivisionArray(){
   var currentCountry = getSelectedCountry();
   if ( countryHasDepartments(currentCountry) )
	   return eval("dept" + currentCountry);
   else
   	   return null;	
}

//----------------------------------------------------------
// DRG 28/11/2003 PartnersLocator V1.8
// Retourne la liste des départements sélectionnés
//----------------------------------------------------------
function getSelectedSubdivision(){
	var combo = formSubdivision.FldDepartments;
	var result = "";
	
	for (ic = 0;  ic <combo.options.length; ic++)
		if (combo.options[ic].value != ""){
		  if (combo.options[ic].selected){
		    if (result=="")
			   result = combo.options[ic].value;
		    else
			   result += "," + combo.options[ic].value;
      }	
		}
	return result;
}

//----------------------------------------------------------
// DRG 28/11/2003 PartnersLocator V1.8
// Initialise la liste des subdivisions sélectionnées
//----------------------------------------------------------
function initComboSubdivision( combo, deptList){

  //We set the departments list
  initValueinListe(combo, getSubdivisionArray(getSelectedCountry()));
  
	//then we must select the value choosen by the user
	//We look for a match based on the left part of deptList
	var strKey = new String(deptList);
	idx = strKey.indexOf("|");
	if (idx>0){
		strKey =strKey.substring(idx+1, strKey.length);
	} 
	for (ic = 0;  ic <combo.options.length; ic++){
		if (combo.options[ic].value == strKey) {
      combo.selectedIndex = ic;
      return(true);
    }
	}
}

//----------------------------------------------------------
// DRG 25/11/2003 PartnersLocator V1.8
//
// Release 15.5.1
// The departments are now displayed in a list so we must feed
//the list when country has departments
//----------------------------------------------------------
function countryChange(){
	var currentCountry = getSelectedCountry();
	var bShow = countryHasDepartments( currentCountry );
	if (!bShow) {
		formSubdivision.FldDepartments.options.length = 0;
	}
	else {
    initValueinListe(formSubdivision.FldDepartments, getSubdivisionArray(getSelectedCountry()));
  }
  showDepartments( bShow );
}

//--------------------------------------------------------------------------
// Function initValueinListe
//
// Feeds a list with the option possible and reset the selected index
// We add an empty value at the top of the list.
//--------------------------------------------------------------------------
// Initialisation de la liste des valeurs
function initValueinListe(listeobj, tabValue) 
{
	var nbRef = tabValue.length;
	listeobj.options.length = nbRef + 1;  
  listeobj.options[0].value = "";
	listeobj.options[0].text = listsEmptyChoice;        
	listeobj.selectedIndex = 0;
	for ( var iRef = 0 ; iRef < nbRef ; iRef++){ 
		strRecord = tabValue[iRef];
		idx = strRecord.indexOf("|");
		if (idx>0){
			strText = strRecord.substring(0,idx);
			strValue =strRecord.substring(idx+1, strRecord.length);
		} else {
			strValue = strRecord;
			strText =strRecord;
		}
		listeobj.options[iRef+1].value = strValue;
		listeobj.options[iRef+1].text = strText;
   	}
}

//----------------------------------------------------------
// DRG 12/12/2003 PartnersLocator V1.8
//----------------------------------------------------------
function getSelectedCountry(){
		if (formSolDeployment.FldCountry.options.selectedIndex>=0)
			return formSolDeployment.FldCountry.options[formSolDeployment.FldCountry.options.selectedIndex].value;
		else
			return null;
}

//----------------------------------------------------------
// DRG 12/12/2003 PartnersLocator V1.8
// Compatible NS4 NS6 IE
//----------------------------------------------------------
function setSelectedCountry(country){
	for( var k=0; k<formSolDeployment.FldCountry.options.length; k++)
		if (formSolDeployment.FldCountry.options[k].value==country ){
			formSolDeployment.FldCountry.options.selectedIndex = k;
			break;
		}
}

//----------------------------------------------------------
// DRG 28/11/2003 PartnersLocator V1.8
// Ouvre le popup de sélection des départements
//----------------------------------------------------------
function openSubdivisionPopup(strButtonLabels, strImgNames){
   if ((SL_window==null)||(SL_window.closed)){ 
	   SL_labelButtons = strButtonLabels.split(",");
	   SL_ImgNames = (typeof(strImgNames)=='undefined') ? new Array("","","","") : strImgNames.split(",");	
	   SL_field = formSubdivision.FldDepartments;
	   SL_listValues = getSubdivisionArray();
	   SL_window = getSubdivisionPopup();
	   SL_window.moveTo(300,300);
   }
   SL_window.focus();
}

//----------------------------------------------------------
// DRG 25/11/2003 PartnersLocator V1.8
// Return true if country has Territory subdivisions
//----------------------------------------------------------
function countryHasDepartments(country){
	if (country!=""){
		for (ic = 0;  ic <deptCountries.length; ic++)
			if ( deptCountries[ic] == country ) return true;
	}
	return false;
}

//----------------------------------------------------------
// PHG 17/11/2005 PartnersLocator V15.5.1
// Return true if country's Territory subdivisions have been loaded
//
//country: the country from which we want to retrieve departments
//----------------------------------------------------------
function initDepartmentsList(country) {
  var nbRef = tabValue.length;
	listeobj.options.length = nbRef;          
	for ( var iRef = 0 ; iRef < nbRef ; iRef++){ 
		strRecord = tabValue[iRef];
		idx = strRecord.indexOf("|");
		if (idx>0){
			strText = strRecord.substring(0,idx);
			strValue =strRecord.substring(idx+1, strRecord.length);
		} else {
			strValue = strRecord;
			strText =strRecord;
		}
		listeobj.options[iRef].value = strValue;
		listeobj.options[iRef].text = strText;
   	}
}

//----------------------------------------------------------
// DRG 25/11/2003 PartnersLocator V1.8
// Show/Hide Departments
// History :
// Release 15.5.1
//  Now the departments are displayed as list and we must feed the list when 
// departemnts are shown
//----------------------------------------------------------
function showDepartments(bShow){
		  if (bShow){
		    displayLayer(layerSubdivisionLabel);
		    displayLayer(layerSubdivision);
		  }else{
		    hideLayer(layerSubdivisionLabel);
		    hideLayer(layerSubdivision);
      }
}
//Building Layers on the form onload event (Netscape 4.7 Bug)
window.onload=function(){start("form");}
