var NOUVEAU_CLIENT = 1;
var STRUCTURE = 2;

function OuvrirFenetre(url) {
    window.open(url);
    return false;
}

function disableBothSubmits(texte) {
	$('input#submitPanier')
		.attr('disabled', 'disabled')
		.attr('value', texte)
	;
	
	$('input#submitFinaliser')
		.attr('disabled', 'disabled')
		.attr('value', texte)
	;
}

function DoSubmit(inputSubmit,texte) {
    inputSubmit.disabled=true;
    inputSubmit.value= texte;
    disableBothSubmits(texte);
    inputSubmit.form.submit();
    return false;
}

function DoSubmitFinal(inputSubmit,texte) {
    inputSubmit.disabled=true;
    inputSubmit.value= texte;
	disableBothSubmits(texte);
    inputSubmit.form.action += "&final=true";
    inputSubmit.form.submit();
    return false;
}

function Rediriger(url) {
    window.location.replace(url);
}

function EnvoyerMail() {
	var strMailTo =  'mailto:' + 'info' + '@' + 'digitick' + '.' + 'com';
    	location.href = strMailTo;
}

function EnvoyerMailMiseEnVente() {
var strMailTo =  'mailto:' + 'mev' + '@' + 'digitick' + '.' + 'com';
	location.href = strMailTo;
}

function numberFormatFrancais(value) {
	if ((value * 100) % 100 == 0) {
		return value;
	}
	var nb = value.toFixed(2) + '';
	return nb.replace('.',',');
}
    
function OuvrirPopup(url,nom,height,width){
	var topPosition = (screen.height - height) / 2;
	var leftPosition = (screen.width - width) / 2;
	
	window.open(url, nom, "width="+width+", height="+height+", top="+topPosition+", left="+leftPosition+", toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no");
}

function DescendreFenetreRep() {
	
	var totalNbLignes = $('#contentAccueil > .invisible').length + $('#contentAccueil > .visible').length;
	var nbIterations = Math.floor(totalNbLignes/10);
	// Récupération du dernier id affiché
	var idDernierRepAffiche = $('#contentAccueil > .visible:last').attr('id');
	// Suppression du "rep" avant
	idDernierRepAffiche = idDernierRepAffiche.substr(3, idDernierRepAffiche.length);
	// Pareil pour le premier id
	var idPremierRepAffiche = $('#contentAccueil > .visible:first').attr('id');
	idPremierRepAffiche = idPremierRepAffiche.substr(3, idPremierRepAffiche.length);

    for (i = 0; i < nbIterations; i++) {
        if (idDernierRepAffiche < totalNbLignes - 1) { 
        	// Masquage du premier
            document.getElementById('rep'+idPremierRepAffiche).className = "invisible";
            idPremierRepAffiche++;

            // Affichage du suivant
            idDernierRepAffiche++;
            document.getElementById('rep'+idDernierRepAffiche).className = "visible";
        }
    
        if (idDernierRepAffiche == totalNbLignes - 1) {
            document.getElementById('accueilevtssuivants').className = "invisible";
        }
        
        if (idPremierRepAffiche > 0) {
            document.getElementById('accueilevtsprecedents').className = "visible";
        }
    }
}

function MonterFenetreRep() {
	
	var totalNbLignes = $('#contentAccueil > .invisible').length + $('#contentAccueil > .visible').length;
	var nbIterations = Math.floor(totalNbLignes/10);
	// Récupération du dernier id affiché
	var idDernierRepAffiche = $('#contentAccueil > .visible:last').attr('id');
	// Suppression du "rep" avant
	idDernierRepAffiche = idDernierRepAffiche.substr(3, idDernierRepAffiche.length);
	// Pareil pour le premier id
	var idPremierRepAffiche = $('#contentAccueil > .visible:first').attr('id');
	idPremierRepAffiche = idPremierRepAffiche.substr(3, idPremierRepAffiche.length);

    for (i = 0; i < nbIterations; i++) {

        if (idPremierRepAffiche > 0) {        
            // Masquage du dernier
            document.getElementById('rep'+idDernierRepAffiche).className = "invisible";
            idDernierRepAffiche--;
    
            // Affichage du précédent
            idPremierRepAffiche--;
            document.getElementById('rep'+idPremierRepAffiche).className = "visible";

            if (idPremierRepAffiche == 0) {
                document.getElementById('accueilevtsprecedents').className = "invisible";
            }
            
            if (idDernierRepAffiche < totalNbLignes - 1) {
                document.getElementById('accueilevtssuivants').className = "visible";
            }
        }
    }
}

// ADDTHIS
addthis_pub = 'lumbroso';
addthis_logo = 'http://www.digitick.com/ext/billetterie4/site/digitick/image/ico_digitick.png';
addthis_brand = 'digitick';
addthis_options = 'delicious, digg, email, favorites, facebook, fark, furl, google, live, myweb, myspace, newsvine, reddit, slashdot, stumbleupon, technorati, twitter, more';

function addthis_start_bookmark( obj , url , title, top , left ){
	addthis_offset_left = left;
	addthis_offset_top = top;
    addthis_open( obj , '', url , title);
}

function addthis_start_share( obj , url , title, top , left ){
    addthis_offset_left = left;
    addthis_offset_top = top;
    addthis_open( obj , '1', url , title);              
}

function addthis_stop(){
	if (typeof addthis_close != "undefined") {
		addthis_close();
    }
}

function interdireFrappeGuillemet(event) {    
    if (event.keyCode) { // IE
        if (event.keyCode == 34 || event.keyCode == 39)  event.returnValue = false;
    }
    else if (event.which) { // Firefox, Chrome, Safari, etc.
        if (event.which == 34 || event.which == 39) return false;
    }
}


// GESTION DE L AFFICHAGE DU SELECT ETAT SELON LE PAYS
function initChangePays(){
    $('select[name="nouveaupays"]').unbind("change").change( function (){
        getListStateData($(this).val(), NOUVEAU_CLIENT);
    });
}
                    
function initChangePaysStructure(){
    $('.compStructureForm #formStructureForm .lineStructureForm select#formatpays').unbind("change").change( function (){
        console.log('test');
        getListStateData($(this).val(), STRUCTURE);
    });
}

function getListStateData( paysId, type ){
    
    var $selectName = "";
    if(type == NOUVEAU_CLIENT){
        $selectName = 'select[name="nouveauetat"]';
    } else if(type == STRUCTURE){
        $selectName = 'select[name="formatpaysetat"]';
    }
    
    $.ajax({
        url: globalsVars['urlAbsoluBilletterie'] + '/index.php', 
        data: {
            p:                  11,
            px: 	            121,
	        ajaxAction:    		"getListState",
	        paysId:             paysId
		},
        dataType: "json",
        success: function(datas){
            //si une liste existe alors on affiche le select 
            if(typeof(datas.listStateData) == "object"){
                    for(var i in datas.listStateData){
                        var etat = datas.listStateData[i];
                        if($($selectName + ' option[value="' + etat.id +'"]').length == 0){
                            $($selectName).append('<option value="' + etat.id +'"> ' + etat.nom +'</option>');
                        }
                    }
                    
                    if(type == NOUVEAU_CLIENT){
                        $($selectName).removeClass("invisible");
                        $('.tableColonne2.etat').removeClass("invisible");
                        $(".tableColonne1.etat").removeClass("invisible");
                    } else{
                        $($selectName).parents("div.lineStructureForm:first").removeClass("invisible");
                    }
            }    
            else{
                $($selectName).html("")
                                               .addClass("invisible");
                $('.tableColonne2.etat').addClass("invisible");
                $(".tableColonne1.etat").addClass("invisible");
            }
        }, 
        error: function (XMLHttpRequest, textStatus, errorThrown){
            //console.log(errorThrown);
        }
        
    });
                                
}

