////////////////////////////////////////////// CONTACT PAGE ///////////////////////////////////////////////////////////////////////////

var bool_Up = false;

function InitAnimeContactFormBlock(){
	if(bool_Up)
	{
		document.getElementById('collapseIco').src = "images/collapseIcoUp.png";
		hideContactForm();
	}else{
		document.getElementById('collapseIco').src = "images/collapseIcoDown.png";	
		document.getElementById('contactForm_box').style.height = "0px"
		document.getElementById('contactForm_box').style.display = "block";
		deployContactForm();
	}
}

function deployContactForm(){
	var elem = document.getElementById('contactForm_box');
	var valHeight = parseInt(elem.style.height.replace('px', ''));
	valHeight += 30;
	elem.style.height = String(valHeight + 'px');
	if(valHeight < 350){
		setTimeout ( "deployContactForm()", 1 );
	}
	else
	{
		bool_Up = true;
	}
}

function hideContactForm(){
	var elem = document.getElementById('contactForm_box');
	var valHeight = parseInt(elem.style.height.replace('px', ''));
	valHeight -= 30;
	elem.style.height = String(valHeight + 'px');
	if(valHeight > 0){
		setTimeout ( "hideContactForm()", 1 );
	}
	else
	{
		bool_Up = false;
		elem.style.display = "none";
	}
	
}


function VerifContactFormName(elem){
	var regexp = "^[a-zA-Z\- ]{2,}$";	
	if(elem.value.match(regexp))
	{
		elem.style.backgroundColor = "#EEE";
	}
	else
	{
		elem.style.backgroundColor = "#F63E62";
	}
}

function VerifContactFormSociete(elem){
	var regexp = "^[a-zA-Z0-9\- ]+$";	
	if(elem.value.match(regexp))
	{
		elem.style.backgroundColor = "#EEE";
	}
	else
	{
		elem.style.backgroundColor = "#F63E62";
	}
}

function VerifContactFormCountry(elem){
	if(elem.value != 'noCountry')
	{
		elem.style.backgroundColor = "#EEE";
	}
	else
	{
		elem.style.backgroundColor = "#F63E62";
	}
}

function VerifContactFormMail(elem){
	var regexp = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{2,}[.][a-z]{2,3}$";
	if(elem.value.match(regexp))
	{
		elem.style.backgroundColor = "#EEE";
	}
	else
	{
		elem.style.backgroundColor = "#F63E62";
	}
}

function VerifContactForm(){
	var varName = document.getElementById('txtName');
	var varCompany = document.getElementById('txtCompany');
	var varCountry = document.getElementById('cbPays');
	var varPhone = document.getElementById('txtPhone');
	var varEMail = document.getElementById('txtEMail');
	var varTypeQuest = document.getElementById('cbTypeQuest');
	var varMessage = document.getElementById('txtMessage');
	var varLang = document.getElementById('page_language').value;

	var errorMsg = "";
	var formIsGood = true;
	
	if(varName.value == ''){
		if(varLang == 'fr')
			errorMsg += "- Votre nom<br/>";
		else
			errorMsg += "- Your name<br/>";
		formIsGood = false;
		varName.style.backgroundColor = "#F63E62";
	}
	if(varCompany.value == ''){
		if(varLang == 'fr')
			errorMsg += "- Votre soci\351t\351<br/>";
		else
			errorMsg += "- Your company<br/>";
		formIsGood = false;
		varCompany.style.backgroundColor = "#F63E62";
	}
	if(varCountry.value == "noCountry"){
		if(varLang == 'fr')
			errorMsg += "- Votre pays<br/>";
		else
			errorMsg += "- Your country<br/>";
		formIsGood = false;
		varCountry.style.backgroundColor = "#F63E62";
	}
	else
	{
		varCountry.style.backgroundColor = "#EEE";
	}
	
	var regexp = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{2,}[.][a-z]{2,3}$";
	if(!varEMail.value.match(regexp)){
		if(varLang == 'fr')
			errorMsg += "- Votre e-mail<br/>";
		else
			errorMsg += "- Your e-mail<br/>";
		formIsGood = false;
		varEMail.style.backgroundColor = "#F63E62";
	}
	if(varMessage.value == ''){
		if(varLang == 'fr')
			errorMsg += "- Votre message<br/>";
		else
			errorMsg += "- Your Message<br/>";
		formIsGood = false;
		varMessage.style.backgroundColor = "#F63E62";
	}
	else
	{
		varMessage.style.backgroundColor = "#EEE";
	}
	
	if(formIsGood){
		SendEmail(varName.value, varCompany.value, varCountry.value, varPhone.value, varEMail.value, varMessage.value);
	}else{
		if(varLang == 'fr')
			Ext.Msg.alert('Warning', "V\351rifiez vos champs car ils ne sont pas tous correctement renseign\351s :<br/>" + errorMsg, function(btn, text){});
		else
			Ext.Msg.alert('Warning', "Verify your fields because they are some errors :<br/>" + errorMsg, function(btn, text){});
	}
}

function ResetContactForm(){
	document.getElementById('formContact').reset();
	document.getElementById('txtName').style.backgroundColor = "#EEE";
	document.getElementById('txtCompany').style.backgroundColor = "#EEE";
	document.getElementById('cbPays').style.backgroundColor = "#EEE";
	document.getElementById('txtPhone').style.backgroundColor = "#EEE";
	document.getElementById('txtEMail').style.backgroundColor = "#EEE";
	document.getElementById('cbTypeQuest').style.backgroundColor = "#EEE";
	document.getElementById('txtMessage').style.backgroundColor = "#EEE";

}

function SendEmail(name, company, country, phone, mail, msgType, Msg){
	
	var xhr;

	try {
		xhr = new ActiveXObject('Msxml2.XMLHTTP');
	}
	catch (e) 
	{
		try {
			xhr = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch (e2) 
		{
			try {
				xhr = new XMLHttpRequest();
			}
			catch (e3) {
				xhr = false;
			}
		}
	}
	if(xhr != null)
	{
		xhr.onreadystatechange  = function()
		{ 
			if(xhr.readyState  == 4)
			{
				if(xhr.status  == 200) 
					document.getElementById("contactForm_box").innerHTML = xhr.responseText; 
				else 
					document.getElementById("contactForm_box").innerHTML = "Error code " + xhr.status;
			}
			else
			{
				document.getElementById("contactForm_box").innerHTML = '<div style="background:url(\'images/ajax-loader.gif\') no-repeat top center; text-align:center; margin-top:100px; margin-bottom:100px; width:220px; heigth:40px; padding-top:25px; margin-left:auto; margin-right:auto; color:#000; font-weigth:bold;">Envoi en cours ...</div>';
			}
		}; 
	
		xhr.open("GET", "scripts/sendmail.php?lang="+document.getElementById("page_language").value+"&name="+name+"&company="+company+"&country="+country+"&phone="+phone+"&mail="+mail+"&msgType="+msgType+"&Msg="+Msg, true);
		xhr.send(null);
	}
	else
	{
		Ext.Msg.alert('Ajax error', 'An Ajax error occurred.',function(btn, text){});
	}
}

////////////////////////////////////////////// LICENCE PAGE ///////////////////////////////////////////////////////////////////////////

function VerifLicenceFormMail1(elem){
	var regexp = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{2,}[.][a-z]{2,3}$";
	if(elem.value.match(regexp))
	{
		elem.style.backgroundColor = "#EEEEEE";
	}
	else
	{
		elem.style.backgroundColor = "#F63E62";
	}
}

function VerifLicenceFormMail2(elem){
	if(elem.value == document.getElementById('email1').value)
	{
		elem.style.backgroundColor = "#EEEEEE";
	}
	else
	{
		elem.style.backgroundColor = "#F63E62";
	}
}

function VerifLicenceFormName(elem){
	var regexp = "^[^0-9]{2,}$";	
	if(elem.value.match(regexp))
	{
		elem.style.backgroundColor = "#EEEEEE";
	}
	else
	{
		elem.style.backgroundColor = "#F63E62";
	}
}

function VerifLicenceFormCompanyAndTown(elem){
	var regexp = "^[^_-.@]+$";	
	if(elem.value.match(regexp))
	{
		elem.style.backgroundColor = "#EEEEEE";
	}
	else
	{
		elem.style.backgroundColor = "#F63E62";
	}
}

function VerifLicenceFormCP(elem){
	if(elem.value != ''){
		var regexp = "^[^a-zA-Z]{2,}$";	
		if(elem.value.match(regexp))
		{
			elem.style.backgroundColor = "#EEEEEE";
		}
		else
		{
			elem.style.backgroundColor = "#F63E62";
		}
	}
}

function VerifLicenceFormPhone(elem){
	var regexp = "^[0-9\- \+.]{5,}$";	
	if(elem.value.match(regexp))
	{
		elem.style.backgroundColor = "#EEEEEE";
	}
	else
	{
		elem.style.backgroundColor = "#F63E62";
	}
}

function VerifLicenceFormCountry(elem){
	if(elem.value != 'noCountry')
	{
		elem.style.backgroundColor = "#EEEEEE";
	}
	else
	{
		elem.style.backgroundColor = "#F63E62";
	}
}


function VerifLicenceForm(){
	var varEMail1 = document.getElementById('email1');
	var varEMail2 = document.getElementById('email2');
	var varTitle = getTitleRadioValue();
	var varName = document.getElementById('name');
	var varCompany = document.getElementById('company');
	var varAddress = document.getElementById('address');
	var varCp = document.getElementById('cp');
	var varTown = document.getElementById('town');
	var varCountry = document.getElementById('country');
	var varPhone = document.getElementById('phone');
	var varFax = document.getElementById('fax');
	var varKey = document.getElementById('key');
	var varMessage = document.getElementById('msg');
	var varLang = document.getElementById('page_language').value;

	var errorMsg = "";
	var formIsGood = true;	
	
	if(varEMail1.value == ''){
		if(varLang == 'fr')
			errorMsg += "- Votre e-mail<br/>";
		else
			errorMsg += "- Your e-mail<br/>";
		formIsGood = false;
		varEMail1.style.backgroundColor = "#F63E62";
	}
	if(varEMail2.value == '' ){
		if(varLang == 'fr')
			errorMsg += "- Retapez votre e-mail<br/>";
		else
			errorMsg += "- Retype your e-mail<br/>";
		formIsGood = false;
		varEMail2.style.backgroundColor = "#F63E62";
	}
	if(varName.value == ''){
		if(varLang == 'fr')
			errorMsg += "- Votre nom<br/>";
		else
			errorMsg += "- Your name<br/>";
		formIsGood = false;
		varName.style.backgroundColor = "#F63E62";
	}
	if(varCompany.value == ''){
		if(varLang == 'fr')
			errorMsg += "- Votre soci\351t\351<br/>";
		else
			errorMsg += "- Your company<br/>";
		formIsGood = false;
		varCompany.style.backgroundColor = "#F63E62";
	}
	if(varTown.value == ''){
		if(varLang == 'fr')
			errorMsg += "- Votre soci\351t\351<br/>";
		else
			errorMsg += "- Your company<br/>";
		formIsGood = false;
		varTown.style.backgroundColor = "#F63E62";
	}
	if(varCountry.value == "noCountry"){
		if(varLang == 'fr')
			errorMsg += "- Votre pays<br/>";
		else
			errorMsg += "- Your country<br/>";
		formIsGood = false;
		varCountry.style.backgroundColor = "#F63E62";
	}
	else
	{
		varCountry.style.backgroundColor = "#EEEEEE";
	}
	
	if(varPhone.value == ''){
		if(varLang == 'fr')
			errorMsg += "- Votre soci\351t\351<br/>";
		else
			errorMsg += "- Your company<br/>";
		formIsGood = false;
		varPhone.style.backgroundColor = "#F63E62";
	}
	
	if(formIsGood){
		if(document.getElementById('controle').value == ""){ // si un robot n'a pas remplis le champs de controle, alors c'est GOOD !!
			SendLicenceRequest(
				varEMail1.value, 
				varTitle, 
				varName.value, 
				varCompany.value, 
				varAddress.value, 
				varCp.value, 
				varTown.value, 
				varCountry.value, 
				varPhone.value, 
				varFax.value, 
				varKey.value, 
				varMessage.value);
		}
		else
		{
			if(varLang == 'fr')
				Ext.Msg.alert('Warning', "La r&eacute;ponse de contr&ocirc;le est incorrect.", function(btn, text){});
			else
				Ext.Msg.alert('Warning', "The control response isn't correct.", function(btn, text){});
			document.getElementById('controle').style.backgroundColor = "#F63E62";
		}
	}else{
		if(varLang == 'fr')
			Ext.Msg.alert('Warning', "V\351rifiez vos champs car ils ne sont pas tous correctement renseign\351s :<br/><br/>" + errorMsg, function(btn, text){});
		else
			Ext.Msg.alert('Warning', "Verify your fields because they are some errors :<br/>" + errorMsg, function(btn, text){});
	}
}

function SendLicenceRequest(email, title, name, company, address, cp, town, country, phone, fax, key, msg){

	var xhr;

	try {
		xhr = new ActiveXObject('Msxml2.XMLHTTP');
	}
	catch (e) 
	{
		try {
			xhr = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch (e2) 
		{
			try {
				xhr = new XMLHttpRequest();
			}
			catch (e3) {
				xhr = false;
			}
		}
	}
	if(xhr != null)
	{
		xhr.onreadystatechange  = function()
		{ 
			if(xhr.readyState  == 4)
			{
				if(xhr.status  == 200) 
					document.getElementById("licenceFormDiv").innerHTML = xhr.responseText; 
				else 
					document.getElementById("licenceFormDiv").innerHTML = "Error code " + xhr.status;
			}
			else
			{
				document.getElementById("licenceFormDiv").innerHTML = '<div class="licenceForm"><div><div class="licenceFormHeaderTitle"></div><div style="line-height:26px;" class="licenceFormTitle">Envoi de votre demande</div></div><div class="licenceFormContentPart"><div style="background:url(\'/images/ajax-loader.gif\') no-repeat top center; text-align:center; margin:100px auto;  width:220px; heigth:40px; padding-top:25px;"><b>Envoi en cours ...</b></div></div></div>';
			}
		}; 

		xhr.open("GET", "/scripts/sendLicenceRequest.php?lang="+document.getElementById("page_language").value + "&email=" + email + "&title=" + title + "&name=" + name + "&company=" + company + "&address=" + address + "&cp=" + cp + "&town=" + town + "&country=" + country + "&phone=" + phone + "&fax=" + fax + "&key=" + key + "&msg=" + msg, true);
		xhr.send(null);
	}
	else
	{
		Ext.Msg.alert('Ajax error', 'An Ajax error occurred.',function(btn, text){});
	}
}

function getTitleRadioValue(){
	var res = '';
	var formu = document.getElementById('licenceForm');
	var n = formu.titleRadio.length;
	// Je parcours mes radio afin de voir si une d'elle est à l'etat "checked"
	for (i=0;i<n;i++){
		if (formu.titleRadio[i].checked){
			res = formu.titleRadio[i].value;
		}
	}
	return res;
}
