// rofra: 26068: class 'image': Rendre obligatoire ''Alternative image text:"
jQuery(document).ready(function() {

    // On va greffer un javascript sur les templates d'edition qui ont des images avec des champs ALT
    jQuery('#editform').each(function(){

        var relCounter = 0;
        jQuery(this).find('[rel=rofra_alternativeimage_marker]').each(function(){
            relCounter = relCounter + 1;
        });

        // Si on a au moins un text alternatif d'une image, on greffe au Submit une fonction de verification
        if (relCounter != 0)
        {
            // alert("Actionnement de la routine rofra_alternativeimage_marker");
            jQuery(this).submit(function (event) {

                // Merci IE.....
                var orEv = event.originalEvent||event.srcElement||document.activeElement;
                if (orEv.explicitOriginalTarget) {
                    source = orEv.explicitOriginalTarget;
                } else {
                    source = document.activeElement;
                }

                var verify = true;

                // On identifie si c'est un clique a partir d'un bouton ou pas
                if (source.type == "submit")
                {
                    // Et on verifie si c'est le bouton de publication
                    if (source.name != "PublishButton")
                    {
                        verify = false;
                    }
                }

                if (verify)
                {
                    var nombreChampNonRenseignes = 0;
                    jQuery(this).find('[rel=rofra_alternativeimage_marker]').each(function(){

                        // On recupere la valeur des champs pour identifier ce qui est rempli ou pas
                        var thisInputValue =  jQuery(this).val() ;
                        var thisInputValueTrim = jQuery.trim( thisInputValue );
                        if ( (thisInputValue == "") || (thisInputValue == undefined) )
                        {
                            nombreChampNonRenseignes = nombreChampNonRenseignes + 1;
                        }
                    });

                    // Gestion du cas d'erreur...
                    if (nombreChampNonRenseignes != 0)
                    {
                        // On met en rouge le message "required"
                        /*
                        jQuery(this).find('#rofra_alternativeimage_message').each(function(){
                            jQuery(this).css('color', '#FF0000');
                        });
                        */
                        jQuery(this).find('#warning_rofra_alternativeimage').each(function(){
                            jQuery(this).show();
                        });
                        location = "#altanchor";

                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
            });
        }
    });
});












/* 
 * rofra le 08/04/2010: Evolution GEM UPGRADE 
 * 
 */
function gemStep3Upgrade()
{

    var inserted_arrival_date_is_anterior_to_firsthotelnight = false;
    var inserted_departure_date_is_posterior_to_lasthotelnightplusoneday = false;
    
    var check_dates = false;
    
    if (GEMUPGRADE_has_hotel_nights)
    {
        check_dates = true;
    }
    
    // Si on a deja cliqué sur proceed, pas de traitement
    if (GEMUPGRADE_proceedflag == true)
    {
    	check_dates = false;
    }
    
    if (check_dates)
    {
        var day_arrival_outward_day   = jQuery('#day_arrival_outward_day').val(); 
        var day_arrival_outward_month = jQuery('#day_arrival_outward_month').val(); 
        var day_arrival_outward_year  = jQuery('#day_arrival_outward_year').val();  
        
        // On essaie de flagger le que la date de la premiere journee est inferieur a la date de la premiere nuit
        if ( (day_arrival_outward_day != '') && (day_arrival_outward_month != '') && (day_arrival_outward_year != '') )
        {
            var dateString = day_arrival_outward_month + "/" + day_arrival_outward_day + "/" + day_arrival_outward_year;
            var day_arrival_outward_date = new Date(dateString);
            var day_arrival_outward_timestamp = day_arrival_outward_date.getTime();
            var day_arrival_outward_timestamp_seconds = parseInt(day_arrival_outward_timestamp / 1000);
            
            if (day_arrival_outward_timestamp_seconds < GEMUPGRADE_first_hotel_night_date)
            {
                //alert("Date de depart inseree inferieur a la date de la premiere journee... " + day_arrival_outward_timestamp_seconds + " < " + GEMUPGRADE_first_hotel_night_date);
                inserted_arrival_date_is_anterior_to_firsthotelnight = true;
            }
        }
        
        var day_departure_return_day   = jQuery('#day_departure_return_day').val(); 
        var day_departure_return_month = jQuery('#day_departure_return_month').val(); 
        var day_departure_return_year  = jQuery('#day_departure_return_year').val();  
        
        // On essaie de flagger le que la date de la derniere journee est inferieur a la date de la derniere nuit plus un jour
        if ( (day_departure_return_day != '') && (day_departure_return_month != '') && (day_departure_return_year != '') )
        {
            var dateString = day_departure_return_month + "/" + day_departure_return_day + "/" + day_departure_return_year;
            var day_departure_outward_date = new Date(dateString);
            var day_departure_outward_timestamp = day_departure_outward_date.getTime();
            var day_departure_outward_timestamp_seconds = parseInt(day_departure_outward_timestamp / 1000);
            
            if (day_departure_outward_timestamp_seconds > GEMUPGRADE_last_hotel_night_date + 86400 )
            {
                inserted_departure_date_is_posterior_to_lasthotelnightplusoneday = true;
            }
        }
        
        
        
        
        // Il y a eu un problème de concordance de dates
        if (inserted_arrival_date_is_anterior_to_firsthotelnight || inserted_departure_date_is_posterior_to_lasthotelnightplusoneday)
        {
            // On affiche le bloc en simulant un clique
        	jQuery( "#date_concordance_alert" ).val("shown");
            jQuery( "#faceboxlauncherlink" ).click();
            // On casse le POST du formulaire
            return false;
        }
        else
        {
        	jQuery( "#date_concordance_alert" ).val("notshown");
        }
    }
    

}

/*
 * rofra le 09/04/2010: Fonction de validation si ils valident quand même leurs choix
 * 
 */
function gemStep3DoProceed()
{
	// On flag le traitement comme validé par l'utilisateur et on click sur le bouton !
	GEMUPGRADE_proceedflag = true;
	
	jQuery( "#ValidateStep3Button" ).click();
}




