//variables
var methodepaiement='';
var produitchoisi=0;
function setmode(mode)
{
	if(mode=='moyensdepaiement')
	{
		$('moyens').style.display='block';
		$('formulaireproduit').style.display='none';
	} else {
		$('moyens').style.display='none';
		$('formulaireproduit').style.display='block';
	}
}
function setmethode(methode)
{
	if(methode=='cb')
	{
		$('chapeau').src=image_chapeau_cb;
		methodepaiement='cb';
		$('methode').value='cb';
	} else {
		$('chapeau').src=image_chapeau_wha;
		methodepaiement='wha';
		$('methode').value='wha';
	}
}

function setproduit(produit)
{
	if(methodepaiement=='cb')
	{
		$('num_achat').value = produit;
		$('target').value='_self';
	} else {
		$('num_achat').value='';
		$('target').value='';
	}

	if(produit==0)
	{
		produitchoisi=0;
		$('produit0').checked='checked';
		//on cache les donnees abonnement
		$('emaildata').style.display='block';
		$('passworddata').style.display='none';
		$('texteabo').style.display='none';
		//on affiche le texte d'un acces
		$('texteacte').style.display='block';
		$('etoile0').style.display='inline';
		$('etoile2').style.display='none';
		wha_url = arrWhaUrl[0];
	} else {
		produitchoisi=2;
		$('produit2').checked='checked';
		//on cache le texte d'un acces
		$('texteacte').style.display='none';
		//on affiche les donnees abonnement
		$('emaildata').style.display='block';
		$('texteabo').style.display='inline';
		$('passworddata').style.display='block';
		$('etoile0').style.display='none';
		$('etoile2').style.display='inline';
		wha_url = arrWhaUrl[2];
	}
}

var okToGo = false;
var formStatus = false;
var formOk = false;
var msg = '';
var email1Empty = true;
var email2Empty = true;

function startChecks(theForm)
{
	var checkpass=0;
	var email_obligatoire;
	//email pas forcement obligatoire pour le simple acces
	if(produitchoisi==0){
	    email_obligatoire = conf_email_obligatoire;
	} else {
	    email_obligatoire=1;
	}
	// mot de passe obligatoire pour l'abonnement
	if(produitchoisi==2){
	    checkpass=1;
	}
	// email obligatoire pour la cb
	// if(methodepaiement=='cb') email_obligatoire=1;
    formStatus = checkform(theForm,checkpass);
    if(formStatus != true){
        formOk = false;
        msg = formStatus;
    } else {
        formOk = true;
    }

    email1Empty = ($('email_achat').value.length == 0) ? true: false;
    email2Empty = ($('confirm_email_achat').value.length == 0) ? true: false;

	if((formOk || !email_obligatoire && email1Empty && email2Empty)){
        // Pour l'abonnement, on demande verification
	    if(produitchoisi==2) {
	        // On ne peut pas utiliser une fonction asynchrone pour ouvrir une fenetre:
	        //checkUserData($('email_achat').value, $('password_achat').value);
            startPayment();
        } else {
            startPayment();
        }

        if(methodepaiement == 'wha'){
            return false;
        }
        return true;
	} else {
	    alert(msg);
	    return false;
	}
}

// Inutilise (voir commentaire ci-dessus)
//function checkUserData(email, password)
//{
//    new Ajax.Request('/paiement/check/', {method:'post', encoding:'windows-1252', onSuccess:afterCheckUserData, parameters:{email:$('email_achat').value, password:$('password_achat').value}});
//}
//
//function afterCheckUserData(transport,json)
//{
//    var message = json.msg;
//    var errors = json.errors;
//    if(message == 'ok'){
//        startPayment();
//    } else {
//        alert(errors);
//    }
//}

function startPayment()
{
	if(methodepaiement=='cb'){
		document.formulaire.submit();
	} else {
//		document.getElementById('desc_paiement').style.display = 'none';
//		document.getElementById('moyens').style.display = 'none';
//
//		if(produitchoisi == 0){
//			document.getElementById('whaIframe').src = baseUrl+'/wha/?produit=0';
//		} else {
//			document.getElementById('whaIframe').src = baseUrl+'/wha/?produit=2';
//		}
//		document.getElementById('whaIframe').style.display = 'block';

		document.formWha.submit();
		
			
			/*var email = $('email_achat').value;
			var password = $('password_achat').value;
			var wha=wha_url+'&email='+email+'&password='+password+'&achat='+produitchoisi;
			window.open(wha,'_blank', 'width=500,height=500,top='+((screen.height-500)/2)+',left='+((screen.width-500)/2)+',scrollbars=0,location=0,status=0,resizable=0');*/
		}
}

