var popUpCalendar=null; 
var popUpFastEmailForm=null;

var ApplicationPath='';
//var ApplicationPath='http://www2.clubservice.fr/';//mode serveur

function CloseAllPopUpWithOut(popupActif)
{
	if ((popUpCalendar) && (popUpCalendar!=popupActif))
		popUpCalendar.close();
	if ((popUpFastEmailForm) && (popUpFastEmailForm!=popupActif))
		popUpFastEmailForm.close();
}

function OpenFastEmailForm(email)
{
    if (email.indexOf("@")>0 ) {
        popUpFastEmailForm = window.open(ApplicationPath+'FastEmailForm.aspx?email=' + email, 
			    'popupcal', 
			    'width=400,height=550,left=200,top=150');
	    popUpFastEmailForm.focus();
	    CloseAllPopUpWithOut(popUpFastEmailForm);
	}
	else
	    alert("Fonction pour les emails uniquement");
		 			
}

function OpenCalendar(idname, postBack, sy)
{
//alert("La fonction Calendrier n'est pas encore fonctionnelle. Veuillez saisir la date au format JJ/MM/AAAA. Merci");

	CloseAllPopUpWithOut(null);
	var i =FindGridEditID(idname);
	if (i>0) {
		popUpCalendar = window.open(ApplicationPath+'TheCalendar.aspx?formname=' + document.forms[0].name + 
			'&id=' + document.forms[0].elements[i].name + '&selected=' + document.forms[0].elements[document.forms[0].elements[i].name].value + '&postBack=' + postBack + '&startyear='+sy, 
			'popupcal', 
			'width=170,height=300,left=200,top=250');
		popUpCalendar.focus();
		CloseAllPopUpWithOut(popUpCalendar);				
	}

}


function SetDate(formName, id, newDate, postBack)
{
	eval('var theform = document.' + formName + ';');
	popUpCalendar.close();
	theform.elements[id].value = newDate;
	if (postBack)
		__doPostBack(id,'');
}


function FindGridEditID(id)
{
    i = 0;
    trouve = false;
    while ((i<document.forms[0].elements.length) && (trouve == false))
    {
            chaine = document.forms[0].elements[i].name;
            if (chaine.indexOf(id) > -1) 
            {
                trouve = true;
            }
            else i++;
    }
    if (!trouve)
		i=0;
    return i;
}

function FindControlClientId(controlName, valeur)
{
       i = 0;
       trouve = false;
       while ((i<document.forms[0].elements.length) && (trouve == false))
       {
             chaine = document.forms[0].elements[i].name;
             if (chaine.indexOf(controlName) > -1) 
             {
                    trouve = true;
             }
             else i++;
       }
       if (trouve)
       {
             var ifield = document.forms[0].elements[i];
             document.forms[0].elements[i].value = valeur;
       }
}

function ValidateTime1(oSrc, args) {

	heure="";
	minute="";
	bOk=true;
	count=0; i=0;
	for (i=0; i<args.Value.length; i++) 
		if ( (args.Value.substr(i,1)>="0") && (args.Value.substr(i,1)<="9") ) count++;
	if ((count==args.Value.length)&&(count==3))	 
		args.Value=args.Value.substr(0,1)+":"+args.Value.substr(1,args.Value.Length);
	if ((count==args.Value.length)&&(count==4))	
		args.Value=args.Value.substr(0,2)+":"+args.Value.substr(2,args.Value.Length);	
	count=0; i=0;			    		    
	while (i<args.Value.length) {				
		if ( (args.Value.substr(i,1)<"0") || (args.Value.substr(i,1)>"9") ) {
			if (count==0) {
				args.Value=args.Value.substr(0,i)+":"+args.Value.substr(i+1,args.Value.Length);
				i++;
			}
			else 
				args.Value=args.Value.substr(0,i)+args.Value.substr(i+1,args.Value.Length);
			count++;
		}
		else
			i++;
	}								
	count=0;	
	for (i=0; i<args.Value.length; i++) {				
		if (args.Value.substr(i,1)==":") count++;
		else if (count==0) heure=heure+args.Value.substr(i,1);	
		else minute=minute+args.Value.substr(i,1);	
	}
	if (heure.length>2) heure=heure.substr(0,2);				
	if (minute.length>2) minute=minute.substr(0,2);
	if (heure=="") heure="00";
	if (minute=="") minute="00";					
	args.Value=heure+":"+minute;				 
	if (!((heure>=0) && (heure<=23))) bOk=false;
	if (!((minute>=0) && (minute<=59))) bOk=false;
	
	eval('var theform = document.aspnetForm;');
	var id =FindGridEditID('TextBoxRDVHeure');
	theform.elements[id].value = args.Value;	
	args.IsValid = bOk;
}


function getById(id){
	return document.getElementById ? document.getElementById(id) : (document.all ? document.all(id) : null);
}


function showC(myId){
	var d;
	d=getById(myId).style.display;
	if(d!=''){ getById(myId).style.display='';}
	else{ getById(myId).style.display='none';}
}




// Test si une Date est Valide
function dateExist(j,m,a)	
{

    var Ok = false;
    if (j >= 1 && j <= 31 && m >= 1 && m <= 12 && a >= 0) 
        Ok = true; 
    else 
        Ok = false;
    if (m >= 1 && m <= 6 && Ok == true) 
    {
        if (m % 2 == 0) 
        {
        
            if (m == 2) 
            {
                if (j > 28)
                    if ( (j == 29) && (a % 4 == 0) ) 
                        Ok = true;
                    else 
                        Ok = false;
                else 
                  Ok = true;
            }
            else 
            {
                if (j == 31) 
                    Ok = false;
                else 
                    Ok = true;
            }
        }
    }
    else if (Ok)
    {
        if ((m == 9 || m == 11) && j == 31) 
            Ok = false;
        else 
            Ok = true;
    }
    if ((a<1800) || (a>2100))
        Ok = false;
    return Ok;
}


function dateChecker(id)
{
    dateCheckerFree(id, false);
}

function dateCheckerFree(id, free)
{
    today = new Date();
    
    v = getById("_ctl0:ContentPlaceHolder1:" + id);
    if (v==null)
        v = getById(id);
    if (v==null)
    {
        var i = FindGridEditID(id);
        v = document.forms[0].elements[i];
    }
        
    if (v != null) {
        var jour = '';
        var mois = '';
        var annee = '';
        var p = 0;
        var countvalid = 0;
        var serie = 0;
        
        if (v.value.length != 0) {
            for (i = 0; i < v.value.length; i++) 
            {
                if ( v.value.charAt(i)<'0' || v.value.charAt(i)>'9' )
                   if (countvalid == 1 && serie==0) {
                      serie = 1;
                      p = p + 1;
                   }
                   
                if ( v.value.charAt(i)>='0' && v.value.charAt(i)<='9' ) 
                {
                    countvalid = 1;
                    serie = 0;
                    if (p==0)
                       jour = jour + v.value.charAt(i);
                    else if (p==1)
                       mois = mois + v.value.charAt(i);
                    else if (p==2)
                       annee = annee + v.value.charAt(i);
                }
            }
            if (p==0 && jour.length>=4) 
            {
                if (jour.length==5)
                    annee = jour.substr(4,1);
                if (jour.length==6)
                    annee = jour.substr(4,2);
                 if (jour.length==8)
                    annee = jour.substr(4,4);
                mois = jour.substr(2,2);
                jour = jour.substr(0,2);
            } 
            
            if (countvalid) { 
                if (jour == '')
                   jour = today.getDate();
                if (mois == '')
                   mois = today.getMonth() + 1;
                   
                var todayYear = today.getYear();
                if (todayYear<999)
                   todayYear = today.getYear()+ 1900;
                if (annee == '')
                   annee = todayYear;
                if (parseInt(annee,10)<100)
                    if (2000 + parseInt(annee,10)>todayYear && free==false)
                        annee = 1900 + parseInt(annee,10);
                    else
                        annee = 2000 + parseInt(annee,10);
                
                if (dateExist( jour, mois, annee)) 
                {
                    if (parseInt(jour,10)<10)
                      jour = '0' + parseInt(jour,10);
                    if (parseInt(mois,10)<10)
                      mois = '0' + parseInt(mois,10);
                    v.value = jour + '/' + mois + '/' + annee;
                }
                else {
                    alert('Date incorrecte : ' + jour + '/' + mois + '/' + annee);
                    v.value = '';
                }
            }
            else
               v.value = '';
        }
    }
}

function timeChecker(id) 
{
    v = getById('_ctl0:ContentPlaceHolder1:' + id);
    if (v==null)
        v = getById(id);
    if (v==null)
    {
        i = FindGridEditID(id);
        v = document.forms[0].elements[i];
    }
    if (v != null) {

	    heure="";
	    minute="";
	    bOk=true;
	    count=0; 
	    i=0;
	    var newvaleur="";
	    while (i<v.value.length) {				
		    if ( (v.value.substr(i,1)<"0") || (v.value.substr(i,1)>"9") ) {
			    if (count==0) {
				    newvaleur+=":";
			    }
			    count++;
		    }
		    else 
		    {
		        if (count==0 && newvaleur.length==2) 
		        {
		            newvaleur+=":";
		            count++;
		        }
                newvaleur+=v.value.substr(i,1);
            }
			i++;
	    }	
	    v.value=newvaleur;							
	    count=0;
	    for (i=0; i<v.value.length; i++) {				
		    if (v.value.substr(i,1)==":") count++;
		    else if (count==0) heure=heure+v.value.substr(i,1);	
		    else minute=minute+v.value.substr(i,1);	
	    }
	    if (minute.length==1) minute=minute+"0";
	    if (heure.length>2) heure=heure.substr(0,2);				
	    if (minute.length>2) minute=minute.substr(0,2);
	    if (heure=="") heure="00";
	    if (minute=="") minute="00";					
	    v.value=heure+":"+minute;				 
	    if (!((heure>=0) && (heure<=23))) {
	        alert('Heure incorrecte');
	        v.value = '';
	    }
	    if (!((minute>=0) && (minute<=59))) 
	    {
	    	alert('Heure incorrecte');
	        v.value = '';
	    }
	}
}
