﻿// Fichier JScript

    function masque() 
    {
        document.getElementById('mesactualites').style.display='none';
        document.getElementById('mesopcvm').style.display='none';
        document.getElementById('mesindices').style.display='none';

        document.getElementById('mesactualitesAFFICH').style.display='none';
        document.getElementById('mesopcvmAFFICH').style.display='none';
        document.getElementById('mesindicesAFFICH').style.display='none';
    }
    
    function getCookie(name)
    {
        var startIndex = document.cookie.indexOf(name);
        if (startIndex != -1) 
        {
            var endIndex = document.cookie.indexOf(";", startIndex);
            if (endIndex == -1) endIndex = document.cookie.length;
                return unescape(document.cookie.substring(startIndex+name.length+1, endIndex));
        }
        else {
          return null;
        }
   }                  
         
   function setCookie(name,value, days) 
   {
        var expire = new Date ();
        expire.setTime (expire.getTime() + (24 * 60 * 60 * 1000) * days);
        document.cookie = name + "=" + escape(value) + "; expires=" +expire.toGMTString()+";path=/";
   }
   
   function deleteCookie(name) 
   {
        var expire = new Date ();
        expire.setTime (expire.getTime() - (24 * 60 * 60 * 1000));
        document.cookie = name + "=; expires=" + expire.toGMTString();
   }
        
   function forceCheck(checkBoxId)
   {
        var checkBox = document.getElementById(checkBoxId);
        checkBox.checked = true;
   }
    
   function manageShowingActuChoice()
   {
       if(hasFundChecked())
       {
            masque(); 
            document.getElementById('mesactualites').style.display='block';
       }
       else
       {
            alert("Vous devez sélectionner un ou plusieurs fonds !!");
       }
   }
    //Returns true if at least a fund is selected
   function hasFundChecked()
   {
        var mesOPCVM = document.getElementById('mesopcvm');
        var inputTab = mesOPCVM.getElementsByTagName("input");
        if(inputTab.length>0)
        {
            for(var i=0;i<inputTab.length;i++)
            {
                if(inputTab[i].checked)
                    return true;
            }
        }
        return false;
   }
    
    
    function uncheckOPCVM(chkFund,fundId)
    {   
        var chkBox = document.getElementById(chkFund);
        chkBox.checked = false;
        var idAbonne = document.getElementById('ctl00_contentPlaceHolder_hfIdAbonne').value;
        var test = SaveOPCVMChangeToBD(fundId,idAbonne);
    }
    
    function uncheckIndex(chk,boxName)
    {
        var chkBox = document.getElementById(chk);
        chkBox.checked = false;
         var idAbonne = document.getElementById('ctl00_contentPlaceHolder_hfIdAbonne').value;
        var test = SaveOtherBoxToBD(boxName,idAbonne);
    }
    
    function treatSort1()
    {
       var sort1Cook = getCookie("sort1");
       if(sort1Cook!= null)
       {
            var tabSort1 = sort1Cook.split("-");
            for(var i=0;i<tabSort1.length;i++)
            {
                var eltSort1 = tabSort1[i].split("|");
                if(eltSort1[1]==1)//Réduit
                {
                    var boxId = "#" + eltSort1[0];
                    var box = $('div.itemContent',$(boxId)).hide();
                }
            }
       }
    }
    
    function treatSort2()
    {
       var sort2Cook = getCookie("sort2");
       if(sort2Cook!= null)
       {
            var tabSort2 = sort2Cook.split("-");
            for(var i=0;i<tabSort2.length;i++)
            {
                var eltSort2 = tabSort2[i].split("|");
                if(eltSort2[1]==1)//Réduit
                {
                    var boxId = "#" + eltSort2[0];
                    var box = $('div.itemContent',$(boxId)).hide();
                }
            }
       }
    }

    function checkOPCVM()
    {
        var sort1Cookie = getCookie("sort1");
        var sort2Cookie = getCookie("sort2");
        $('input','#mesopcvm').each(function(i)
        {
            var idOPCVM = this.id.replace("ctl00_contentPlaceHolder_chk_","");
            idOPCVM = "OPCVM_" + idOPCVM;
            
            if((sort1Cookie != null && sort1Cookie.indexOf(idOPCVM)!= -1) ||
            (sort2Cookie != null && sort2Cookie.indexOf(idOPCVM)!= -1) )
                this.checked = true;      
            else
                this.checked = false;   
        }
        );       
    }
    
    function checkNews()
    {
        var newsChoice = getCookie("newsChoice");
        if(newsChoice != null)
        {
            var choices = newsChoice.split("-");
            if(choices[0]=="1")
            {
                var touteLaGamme = document.getElementById("ctl00_contentPlaceHolder_chkNewGamme");
                touteLaGamme.checked = true;
            }
            if(choices[1]=="1")
            {
                 var carmignacGestion = document.getElementById("ctl00_contentPlaceHolder_chkNewCG");
                 carmignacGestion.checked = true;
            }
        }
    }
    
    function checkReviews()
    {
        var newsChoice = getCookie("reviewsChoice");
        if(newsChoice != null)
        {
            var choices = newsChoice.split("-");
            if(choices[0]=="1")
            {
                var touteLaGamme = document.getElementById("ctl00_contentPlaceHolder_chkReviewGamme");
                touteLaGamme.checked = true;
            }
            if(choices[1]=="1")
            {
                 var carmignacGestion = document.getElementById("ctl00_contentPlaceHolder_chkReviewCG");
                 carmignacGestion.checked = true;
            }
        }
    }
    
    function checkIndex()
    {
        var newsChoice = getCookie("indexChoice");
        if(newsChoice != null)
        {
            var choices = newsChoice.split("-");
            if(choices[0]=="1")
            {
                var touteLaGamme = document.getElementById("ctl00_contentPlaceHolder_chkFundIndex");
                touteLaGamme.checked = true;
            }
            if(choices[1]=="1")
            {
                 var carmignacGestion = document.getElementById("ctl00_contentPlaceHolder_chkMarketIndex");
                 carmignacGestion.checked = true;
            }
        }
    }
 
    function setElementState(radioButton)
    {

       treatSort1();
       treatSort2();  
       checkOPCVM();   
       checkNews();
       checkReviews();
       checkIndex();
       if(radioButton != null  && radioButton != "")
       {
            var rd = document.getElementById(radioButton);
            rd.checked = true;
       }
       else
       {
            var rd = document.getElementById('rdOPCVM');
            rd.checked = true;
       }
       
       
    }
    
   var toggleContent = function(e)
   {
        var sortId = this.parentNode.parentNode.parentNode.id.replace("ctl00_contentPlaceHolder_","");
        var boxId = this.parentNode.parentNode.id
        var cookie = getCookie(sortId);

         var targetContent = $('div.itemContent', this.parentNode.parentNode);
	    if (targetContent.css('display') == 'none') 
	    {//Pas affiché
	        cookie = cookie.replace(boxId + "|1",boxId + "|2");
	        targetContent.slideDown(300);
		    $(this).html('<img src="/img/mon-carmignac/arrow_down.gif" />');
	    } 
	    else 
	    {
	        cookie = cookie.replace(boxId + "|2",boxId + "|1");
		    targetContent.slideUp(300);
		    $(this).html('<img src="/img/mon-carmignac/arrow_right.gif" />');
	    }
	    setCookie(sortId,cookie,30);
	    var idAbonne = document.getElementById('ctl00_contentPlaceHolder_hfIdAbonne').value;
	    if(idAbonne != null && idAbonne != "")
	        SaveAfterToggleInDB(idAbonne);
	    return false;
   };
   
       
   var toggleContent2 = function(e)
   {
        var sortId = this.parentNode.parentNode.parentNode.id.replace("ctl00_contentPlaceHolder_","");
        var boxId = this.parentNode.parentNode.id
        var cookie = getCookie(sortId);
	    var targetContent = $('div.itemContent', this.parentNode.parentNode);
	    if (targetContent.css('display') == 'none') 
	    {
	        cookie = cookie.replace(boxId + "|1",boxId + "|2");
		    targetContent.slideDown(300);
		    $(this).html('<img src="/img/mon-carmignac/arrow_down2.gif" />');
	    } 
	    else 
	    {
	        cookie = cookie.replace(boxId + "|2",boxId + "|1");
		    targetContent.slideUp(300);
		    $(this).html('<img src="/img/mon-carmignac/arrow_right2.gif" />');
	    }
	    setCookie(sortId,cookie,30);
	    var idAbonne = document.getElementById('ctl00_contentPlaceHolder_hfIdAbonne').value;
	    if(idAbonne != null && idAbonne!= "")
	        SaveAfterToggleInDB(idAbonne);
	    return false;
   };
    
    function initJquery()
    {
        $(document).ready(function () 
        {
		    $('a.closeEl').bind('click', toggleContent);
		    $('div.groupWrapper').Sortable(
		    {
				accept: 'groupItem',
				helperclass: 'sortHelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				handle: 'div.itemHeader',
				tolerance: 'pointer',
				onChange : function(ser)
				{
				},
				onStart : function()
				{
					$.iAutoscroller.start(this, document.getElementsByTagName('body'));
				
				},
				onStop : function()
				{
					$.iAutoscroller.stop();
					serialSort1 = $.SortSerialize('ctl00_contentPlaceHolder_sort1').hash;
					serialSort2 = $.SortSerialize('ctl00_contentPlaceHolder_sort2').hash;
					var id = "#" + this.id;
					var targetContent = $('div.itemContent', id);
					var status = targetContent.css('display') == 'none'? "1":"2";
					var idAbonne = document.getElementById('ctl00_contentPlaceHolder_hfIdAbonne').value;
					SaveAfterMoveToBD(serialSort1,serialSort2,status,idAbonne);
				}
		    });
	    } );     
	    $(document).ready(	function () 
        {
		    $('a.closeE2').bind('click', toggleContent2);
		    $('div.groupWrapper').Sortable(
			{
				accept: 'groupItem',
				helperclass: 'sortHelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				handle: 'div.itemHeader',
				tolerance: 'pointer',
				onChange : function(ser)
				{
				},
				onStart : function()
				{
					$.iAutoscroller.start(this, document.getElementsByTagName('body'));
				},
				onStop : function()
				{
					$.iAutoscroller.stop();
					serialSort1 = $.SortSerialize('ctl00_contentPlaceHolder_sort1').hash;
					serialSort2 = $.SortSerialize('ctl00_contentPlaceHolder_sort2').hash;
					var id = "#" + this.id;
					var targetContent = $('div.itemContent', id);
					var status = targetContent.css('display') == 'none'? "1":"2";
					var idAbonne = document.getElementById('ctl00_contentPlaceHolder_hfIdAbonne').value;
					SaveAfterMoveToBD(serialSort1,serialSort2,status,idAbonne);
				}
			});
	    });
    }
    
    function setHiddenFieldValue(hiddenFieldId,chkId, val)
    {
        var hiddenField = document.getElementById(hiddenFieldId);
        var checkBox = document.getElementById(chkId);
        if(checkBox.checked)
            hiddenField.value = val;
        else
            hiddenField.value = "-" + val;
    }
    
    function ClearText(text,txtId)
    {
        var txt = document.getElementById(txtId);
        if(txt.value==text)
            txt.value="";
    }
    
    function ShowText(text,txtId)
    {
        var txt = document.getElementById(txtId);
        if(txt.value=="")
            txt.value=text;
    }
    
    function CleanString(pString)
    {
        if (pString!= "" || pString != null)
        {
            if (pString.indexOf("-")==0 )
                pString = pString.substring(1,pString.length);
            else if (pString.indexOf("-")== pString.length-1)
                pString = pString.substring(0,pString.length-1);
            else
                pString = pString.replace(/--/, "-");
            return pString;
        }
        return pString;
    }
    
    function confirmAbonnement(msg, url)
    {
        if (confirm(msg))
            window.location.href = url;
    }
    
   
