/**
 * 
 * Cas d'utilisation:
 *   - salc_tech.html?nose=ABCDEF123456H70
 *   	[sousVersion=][antiArch=][arch=][lang=][autres=]
 *   - salc_tech.html?Probleme=NoDeSeriePerdu (boutique.php)
 *   - Consulter la fonction Initialisation_Automatique()
 * 
 */

var OS = '';
var OSVersion = '';
var texteur = '';
var texteurVersion = '';
var editionAntidote = '';

var leBoutonQuestionRepondueNonRedigerAEteCliquay = false;
new Event.observe(window, 'load', impl_onLoad);

function cacherElement(element)
{
	// On s'assure de rendre l'élément visible préalablement car sinon IE6 ne cache pas le contenu des
	// TR comme il faut (salc_tech.html)
	element.style.display = '';
	element.style.display = 'none';
}

function montrerElement(element)
{
	element.show();
}

function donneTexteurSansGroupe()
{
	var texteur = $F('Texteur');
	var index = texteur.indexOf('=>');
	if (index >= 0) {
		texteur = texteur.substring(index + 3);
	}
	return texteur;
}

// Gestionnaires d'ŽvŽnements
function OnChange(objet)
{
	if(objet == $('EditionAntidote'))
	{
		editionAntidote = $('EditionAntidote').options[$('EditionAntidote').selectedIndex].id;
	}
	else if(objet == $('SystemeExploitation'))
	{
		OS = $('SystemeExploitation').options[$('SystemeExploitation').selectedIndex].id;
	}
	else if(objet == $('SystemeExploitationVersion'))
	{
		OSVersion = $('SystemeExploitationVersion').options[$('SystemeExploitationVersion').selectedIndex].id;
	}
	else if(objet == $('Texteur'))
	{
		texteur = $('Texteur').options[$('Texteur').selectedIndex].id;
	}
	else if(objet == $('TexteurVersion'))
	{
		texteurVersion = $('TexteurVersion').options[$('TexteurVersion').selectedIndex].id;
	}
	VerifieBesoinEdition();
	VerifieBesoinOS();
	VerifieBesoinTexteur();
	verifierConfirmationLinguistique();
	
	var idTexteur = donneTexteurSansGroupe();
	AideDynamique($F('Probleme'), $F('EditionAntidote'), $F('SystemeExploitation'), $F('SystemeExploitationVersion'), idTexteur, $F('TexteurVersion'));
}

function VerifieBesoinEdition()
{
	switch(editionAntidote)
	{
		case 'AHD':
			$('serie_bloc5').value = 'H70';
			break;
		case 'ARX':
			$('serie_bloc5').value = 'H60';
			break;
		case 'APR':
			$('serie_bloc5').value = 'H50';
			break;
		case 'AMP':
			$('serie_bloc5').value = 'H40';
			break;
		default:
			$('serie_bloc5').value = '';
			break;
	}
	
	switch($F('Probleme'))
	{
		case '':
		case 'CodeActivationNonValide':
		case 'NoDeSeriePerdu':
		case 'MiseANiveau':
		case 'AchatLicence':
		case 'NouvelleAdresse':
		case 'Commentaire':
			cacherElement($('rangeeEditionAntidote'));
			VideSELECT('EditionAntidote');
			AjouteOptionAvecId('EditionAntidote', 'EditionSO');
			SelectionneId('EditionAntidote', 'EditionSO');
			break;
		default:
			VideSELECT('EditionAntidote');
			remplirSelectEdition($('EditionAntidote'), true);
			SelectionneId('EditionAntidote', editionAntidote);
			montrerElement($('rangeeEditionAntidote').show());
			break;
	}
}

function VerifieBesoinOS()
{
	switch($F('Probleme'))
	{
		case '':
		case 'CodeActivationNonValide':
		case 'NoDeSeriePerdu':
		case 'QuestionLinguistique':
		case 'MiseANiveau':
		case 'AchatLicence':
		case 'NouvelleAdresse':
		case 'Commentaire':
			cacherElement($('rangeeSystemeExploitation'));
			break;
		default:
			switch($F('EditionAntidote'))
			{
				case '':
				case 'AHD':
				case 'ARX':
					montrerElement($('rangeeSystemeExploitation'));
					break;
				default:
					cacherElement($('rangeeSystemeExploitation'));
					break;
			}
			break;
	}
	
	$('SystemeExploitation').disabled = $F('EditionAntidote') == '' || $('rangeeSystemeExploitation').style.display == 'none';
	$('SystemeExploitationVersion').disabled = $F('SystemeExploitation') == '' || $('SystemeExploitation').disabled;
	VideSELECT('SystemeExploitation');
	VideSELECT('SystemeExploitationVersion');
	remplirSelectPlateforme($('SystemeExploitation'), false, $F('EditionAntidote'));
	switch($F('EditionAntidote'))
	{
		case 'AHD':
		case 'ARX':
			SelectionneId('SystemeExploitation', OS);
			switch($F('SystemeExploitation'))
			{
				case 'Win':
				case 'Mac':
				case 'Lin':
					remplirSelectPlateformeVersion($('SystemeExploitationVersion'), false, $F('EditionAntidote'), $F('SystemeExploitation'));
					SelectionneId('SystemeExploitationVersion', OSVersion);
					break;
			}
			break;
		default:
			AjouteOptionAvecId('SystemeExploitation', 'SystemeExploitationSO');
			SelectionneId('SystemeExploitation', 'SystemeExploitationSO');
			
			AjouteOptionAvecId('SystemeExploitationVersion', 'SystemeExploitationVersionSO');
			SelectionneId('SystemeExploitationVersion', 'SystemeExploitationVersionSO');
			break;
	}
}

function VerifieBesoinTexteur()
{
	switch($F('Probleme'))
	{
		case 'ProblemeUtilisation':
			switch($F('EditionAntidote'))
			{
				case '':
				case 'AHD':
				case 'ARX':
					montrerElement($('rangeeTexteur'));
					break;
				default:
					cacherElement($('rangeeTexteur'));
					break;
			}
			break;
		default:
			cacherElement($('rangeeTexteur'));
			break;
	}
	
	$('Texteur').disabled = $F('EditionAntidote') == '' || $F('SystemeExploitation') == '' || $('rangeeTexteur').style.display == 'none';
	VideSELECT('Texteur');
	remplirSelectTexteur($('Texteur'), false, $F('EditionAntidote'), $F('SystemeExploitation'));
	SelectionneId('Texteur', texteur);
	
	$('TexteurVersion').disabled = $F('Texteur') == '' || $('Texteur').disabled;
	VideSELECT('TexteurVersion');
	var nbAjoute = remplirSelectVersion($('TexteurVersion'), false, $F('EditionAntidote'), $F('SystemeExploitation'), $F('Texteur'));
	SelectionneId('TexteurVersion', texteurVersion);
	
	if ($('Texteur').disabled)
	{
		EnleveOptionAvecId('Texteur', 'TexteurVide');
	}
	if ($('TexteurVersion').disabled)
	{
		EnleveOptionAvecId('TexteurVersion', 'TexteurVersionVide');
	}
	if ($F('Probleme') == 'ProblemeIntegration')
	{
		if (!$('Texteur').disabled)
		{
			EnleveOptionAvecId('Texteur', 'TexteurSO');
			SelectionneId('Texteur', texteur);
		}
	}
	if (nbAjoute == 0)
	{
		VideSELECT('TexteurVersion');
		AjouteOptionAvecId('TexteurVersion', 'TexteurVersionSO');
	}
}

function verifierConfirmationLinguistique()
{
	$('soumettre').disabled = $F('Probleme') == "QuestionLinguistique" && !$('confirmationLinguistique').checked;
	$('confirmationLinguistique').parentNode.parentNode.style.display = ($F('Probleme') == "QuestionLinguistique" ? '' : 'none');
}

var activeField = null;

function autoJump_onKeyDown(fieldName)
{
	var field = document.formulaire.elements[fieldName];
	
	activeField = field;
	field.lastValue = field.value;
}

function autoJump_onKeyUp(fieldName,nextFieldName,maxLength)
{
	var field = document.formulaire.elements[fieldName];
	var nextField = document.formulaire.elements[nextFieldName];
	
	if (field == activeField &&
	field.value != field.lastValue &&
	field.value.length >= maxLength)
	nextField.focus();
	activeField = null;
}

function impl_onLoad(event)
{
	MM_preloadImages('images/accueil/menu/description_ov.gif',
									'images/accueil/menu/ecrans_ov.gif',
									'images/accueil/menu/compatibilite_ov.gif',
									'images/accueil/menu/assistance_ov.gif',
									'images/accueil/menu/achat_ov.gif',
									'images/accueil/bout_niveau_ov.gif',
									'images/accueil/bout_jour_ov.gif',
									'images/accueil/bout_enregistrer_ov.gif',
									'images/salc/bouton_oui_ov.gif',
									'images/salc/bouton_non_autres_questions_ov.gif',
									'images/salc/bouton_non_rediger_ov.gif');
	
	if(Prototype.OS.Win98) { OS = 'Win'; OSVersion = 'Win98'; }
	else if(Prototype.OS.WinMe) { OS = 'Win'; OSVersion = 'WinMe'; }
	else if(Prototype.OS.WinNT4) { OS = 'Win'; OSVersion = 'WinNT'; }
	else if(Prototype.OS.Win2000) { OS = 'Win'; OSVersion = 'Win2000'; }
	else if(Prototype.OS.WinXP) { OS = 'Win'; OSVersion = 'WinXP'; }
	else if(Prototype.OS.Win2003) { OS = 'Win'; OSVersion = 'Win2003'; }
	else if(Prototype.OS.WinVista) { OS = 'Win'; OSVersion = 'WinVista'; }
	else if(Prototype.OS.Mac) { OS = 'Mac'; }
	else if(Prototype.OS.Linux) { OS = 'Lin'; }
	
	VerifieBesoinEdition();
	VerifieBesoinOS();
	VerifieBesoinTexteur();
	var idTexteur = donneTexteurSansGroupe();
	AideDynamique($F('Probleme'), $F('EditionAntidote'), $F('SystemeExploitation'), $F('SystemeExploitationVersion'), idTexteur, $F('TexteurVersion'));
	Initialisation_Automatique();
}

function rot13(str)
{
	var newStr = [];
	aCode = 'a'.charCodeAt();
	ACode = 'A'.charCodeAt();
	zeroCode = '0'.charCodeAt();
	for(var i = 0, count = str.length; i < count; i++) {
		if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z') newStr[i] = String.fromCharCode(aCode + (((str.charAt(i).charCodeAt() - aCode) + 13) % 26)); 
		else if(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') newStr[i] = String.fromCharCode(ACode + (((str.charAt(i).charCodeAt() - ACode) + 13) % 26));
		else if(str.charAt(i) >= '0' && str.charAt(i) <= '9') newStr[i] = String.fromCharCode(zeroCode + (((str.charAt(i).charCodeAt() - zeroCode) + 5) % 10));
		else newStr[i] = str.charAt(i);
	}
	return newStr.join('');
}

function Initialisation_Automatique()
{
	lireParametresAntidote();
	
	var plateforme = decodeURI(getParameter('plateforme'));
	if(plateforme != null && plateforme != "") {
		if(plateforme == "Win") {
			location.href = "salc_win.html";
		} else if(plateforme == "Mac OS X") {
			location.href = "salc_mac.html";
		} else if(plateforme == "Linux") {
			location.href = "salc_linux.html";
		} else {
			alert(plateforme);
			location.href = "salc_faq.html";
		}
	}
	
	var nose = getNose();
	if (nose != null && nose.length == 15) {
		nose = rot13(nose);
		
		$('serie_bloc1').value = nose.substr(0, 3);
		$('serie_bloc2').value = nose.substr(3, 3);
		$('serie_bloc3').value = nose.substr(6, 3);
		$('serie_bloc4').value = nose.substr(9, 3);
		$('serie_bloc5').value = nose.substr(12, 3);
		$('serie_bloc1').parentNode.parentNode.style.display = 'none';
		$('rangeeNoSeriePerdu').style.display = 'none';
		
		$('sousVersion').value = getSousVersion();
		$('antiArch').value = getAntiArch();
		$('plateforme').value = getPlateforme();
		$('versionPlateforme').value = getVersionPlateforme();
		$('arch').value = getArch();
		$('lang').value = getLang();
		$('autres').value = getAutres();
		
		if (nose.substr(12, 3) == "H70") {
			editionAntidote = "AHD";
			SelectionneId('EditionAntidote', editionAntidote)
		}
		
		var probleme = getParameter("Probleme");
		if (probleme != null) {
			$('Probleme').value = probleme;
			OnChange($('Probleme'));
		}
	}
	OSVersion = $F('versionPlateforme');
}

function CacherIncomplet()
{
	$('serie_bloc1').style.background = 'white';
	$('serie_bloc2').style.background = 'white';
	$('serie_bloc3').style.background = 'white';
	$('serie_bloc4').style.background = 'white';
	$('serie_bloc5').style.background = 'white';
	$('prenom').style.background = 'white';
	$('nom').style.background = 'white';
	$('email').style.background = 'white';
	$('Probleme').style.background = 'white';
	if($('rangeeSystemeExploitation').style.display == '') $('rangeeSystemeExploitation').style.background = 'white';
	if($('rangeeSystemeExploitation').style.display == '') $('rangeeSystemeExploitation').style.background = 'white';
	if($('rangeeTexteur').style.display == '') $('rangeeTexteur').style.background = 'white';
	if($('rangeeTexteur').style.display == '') $('rangeeTexteur').style.background = 'white';
}

///////////////////
function Soumettre()
{
	CacherIncomplet();
	var complet = true;

	if($F('serie_bloc1').length < 3)
	{
		complet = false;
		$('serie_bloc1').style.background = 'yellow';
	}
	if($F('serie_bloc2').length < 3)
	{
		complet = false;
		$('serie_bloc2').style.background = 'yellow';
	}
	if($F('serie_bloc3').length < 3)
	{
		complet = false;
		$('serie_bloc3').style.background = 'yellow';
	}
	if($F('serie_bloc4').length < 3)
	{
		complet = false;
		$('serie_bloc4').style.background = 'yellow';
	}
	if($F('serie_bloc5').length < 3)
	{
		complet = false;
		$('serie_bloc5').style.background = 'yellow';
	}

	if($F('prenom').length < 1)
	{
		complet = false;
		$('prenom').style.background = 'yellow';
	}
	if($F('nom').length < 1)
	{
		complet = false;
		$('nom').style.background = 'yellow';
	}

	if($F('email').length < 4)
	{
		complet = false;
		$('email').style.background = 'yellow';
	}

	if($F('Probleme') == "")
	{
		complet = false;
		$('Probleme').style.background = 'yellow';
	}

	if($F('SystemeExploitation') == "" && $('SystemeExploitation').style.display != 'none' && $('rangeeSystemeExploitation').style.display != 'none')
	{
		complet = false;
		$('SystemeExploitation').style.background = 'yellow';
	}
	if($F('SystemeExploitationVersion') == "" && $('SystemeExploitationVersion').style.display != 'none' && $('rangeeSystemeExploitation').style.display != 'none')
	{
		complet = false;
		$('SystemeExploitationVersion').style.background = 'yellow';
	}

	if($('Texteur') != null && $F('Texteur') == "" && $('Texteur').style.display != 'none')
	{
		complet = false;
		$('Texteur').style.background = 'yellow';
	}
	if($('TexteurVersion') != null && $F('TexteurVersion') == "" && $('TexteurVersion').style.display != 'none')
	{
		complet = false;
		$('TexteurVersion').style.background = 'yellow';
	}

	if(complet)
	{
		var _Edition = $F('EditionAntidote');
		var _Probleme = $F('Probleme');
		var _SystemeExploitation = $F('SystemeExploitation') == 's.o.' ? '' : $F('SystemeExploitation');
		var _SystemeExploitationVersion = $F('SystemeExploitationVersion') == 's.o.' ? '' : $F('SystemeExploitationVersion');
		var _Texteur = ($('Texteur') == null || $F('Texteur') == 's.o.') ? '' : $F('Texteur');
		var _TexteurVersion = ($('Texteur') == null || $F('TexteurVersion') == 's.o.') ? '' : $F('TexteurVersion');

		new Ajax.Request("salc/scripts/statistiques.php", {
			method: 'post',
			asynchronous: false,
			parameters: {
										Edition: _Edition,
										Probleme: _Probleme,
										SystemeExploitation: _SystemeExploitation,
										SystemeExploitationVersion: _SystemeExploitationVersion,
										Texteur: _Texteur,
										TexteurVersion: _TexteurVersion,
										Action: 'SoumettreQuestion'
									}
			});

		document.formulaire.submit();
	}
	else
	{
		alert("Vous n'avez pas complété tous les champs requis.");
	}
}

function goto_faq()
{
	var plateforme = $F('SystemeExploitation').toLowerCase();
	if (plateforme == "lin") plateforme = "linux";
	
	location.href = ('/salc_' + plateforme + '.html');
}

function goto_faq(anchor)
{
	var plateforme = $F('SystemeExploitation').toLowerCase();
	if (plateforme == "lin") plateforme = "linux";
	
	location.href = ('/salc_' + plateforme + '.html#' + anchor);
}

function goto_maj()
{
	var plateforme = $F('SystemeExploitation').toLowerCase();
	if (plateforme == "lin") plateforme = "linux";
	
	location.href = ('/maj_' + plateforme + '.html');
}

