﻿function createXMLHttpRequest()
{
    var xhr = null; 
	 
	if(window.ActiveXObject){ // Internet Explorer 
	   try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
	}else if(window.XMLHttpRequest){ // Firefox et autres
	   xhr = new XMLHttpRequest(); 
	}
	else { // XMLHttpRequest non supporté par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	   xhr = false; 
	}
	
	return xhr
}

function SetNewsLetters(){
    var xhr = createXMLHttpRequest();
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4){
            if(xhr.status == 200) {
                alert(xhr.responseText);
                return false;
            }
        }
    }
    xhr.open("GET", "AjaxServer.aspx?NewsLetters=" + document.getElementById("Raccourcis1_txtEmail").value + '|' + document.getElementById("Raccourcis1_rblNewsLetters_0").checked, true);
    xhr.send(null);
    return false;
}

function SetEnableOk(){
    if (document.getElementById("Raccourcis1_txtEmail").value == ''){
        document.getElementById("Raccourcis1_btnOK").disabled = true;
    }
    else{
        document.getElementById("Raccourcis1_btnOK").disabled = false;
    }
}

function IsSameAdr(strId){
    return document.getElementById(strId + "chkSameAdr").checked;
}

function SetProvinceTxtLiv(strId){
    if (IsSameAdr(strId) == true){
        document.getElementById(strId + "txtProvinceLiv").value= document.getElementById(strId + "txtProvince").value;
    }
}

function SetProvinceLiv(strId){
    if (IsSameAdr(strId) == true){
        document.getElementById(strId + "cboProvinceLiv").selectedIndex = document.getElementById(strId + "cboProvince").selectedIndex;
    }
}

function SetAdrLiv(strId){
    if (IsSameAdr(strId) == true){
        document.getElementById(strId + "txtAdrLiv").value= document.getElementById(strId + "txtAdresse").value;
    }
}

function SetVilleLiv(strId){
    if (IsSameAdr(strId) == true){
        document.getElementById(strId + "txtVilleLiv").value= document.getElementById(strId + "txtVille").value;
    }
}

function SetCPLiv(strId){
    if (IsSameAdr(strId) == true){
        document.getElementById(strId + "txtCodePostalLiv").value= document.getElementById(strId + "txtCodePostal").value;
    }
}

function SameAdr(strId)
{
    var chk =document.getElementById(strId + 'chkSameAdr');           
    
    if (chk.checked == true)
    {
        EnableCtrlSameAdr(true,strId);
        SetSameAdr(strId);       
    }
    else
    {
        EnableCtrlSameAdr(false,strId);
    }
    
}

function SetSameAdr(strId){
    if (document.getElementById(strId + "cboProvince").style.visibility == 'visible')
    {
        //Remplir cboProvinceLiv
        RemplirProvince(strId);
        document.getElementById(strId + "cboProvinceLiv").style.visibility = 'visible';
        document.getElementById(strId + "txtProvinceLiv").style.visibility = 'hidden';
        document.getElementById(strId + "cboProvinceLiv").selectedIndex = document.getElementById(strId + "cboProvince").selectedIndex;
    }
    else{
        document.getElementById(strId + "cboProvinceLiv").style.visibility = 'hidden';
        document.getElementById(strId + "txtProvinceLiv").style.visibility = 'visible';
    }
    
    document.getElementById(strId + "cboPaysLiv").selectedIndex = document.getElementById(strId + "drpPays").selectedIndex;
    document.getElementById(strId + "txtProvinceLiv").value = document.getElementById(strId + "txtProvince").value;
    document.getElementById(strId + "txtVilleLiv").value = document.getElementById(strId + "txtVille").value;
    document.getElementById(strId + "txtAdrLiv").value = document.getElementById(strId + "txtAdresse").value;
    document.getElementById(strId + "txtCodePostalLiv").value= document.getElementById(strId + "txtCodePostal").value; 
}

function RemplirProvince(strId)
{     
    var cboProv = document.getElementById(strId + 'cboProvince');
    var cboProvLiv = document.getElementById(strId + 'cboProvinceLiv');
    
   
    //Effacer tous les éléments de ProvLiv
    for (var count = cboProvLiv.options.length-1; count >-1; count--)
    {
	    cboProvLiv.options[count] = null;
    }

    //Mettre les données de Prov dans ProvLiv
    for (var count = 0; count < cboProv.length-1; count++)
    {
		optionItem = new Option(cboProv.options[count].text, cboProv.options[count].value,  false, false);
		cboProvLiv.options[cboProvLiv.length] = optionItem;
    }
}

function EnableCtrlSameAdr(enable,strId)
{
    document.getElementById(strId + "cboPaysLiv").disabled= enable;
    document.getElementById(strId + "cboProvinceLiv").disabled= enable;
    document.getElementById(strId + "txtProvinceLiv").disabled= enable;
    document.getElementById(strId + "txtVilleLiv").disabled= enable;
    document.getElementById(strId + "txtAdrLiv").disabled= enable;
    document.getElementById(strId + "txtCodePostalLiv").disabled= enable;
}

function ChangeCalendar(info,strId){
    var strDate = document.getElementById(strId + 'DIVInfo').innerHTML;
    var x = createXMLHttpRequest();
    
    x.onreadystatechange = function(){
        if(x.readyState == 4){ 
            if(x.status == 200){
                var tab = x.responseText.split("|");
                document.getElementById(strId + 'DIVDate').innerHTML = tab[0];
                document.getElementById(strId + 'TDTitle').innerHTML = tab[1];
                document.getElementById(strId + 'DIVInfo').innerHTML = tab[2];
            }
        }
    }
    //myHandleStateChange(x);
    x.open("GET", "AjaxServer.aspx?Calendrier=" + info + '|' + strDate, true);
    x.send(null);
}

function AddEmail(strId){
    strInfo = document.getElementById(strId + 'txtCourrielToAdd').value + '|';
    if (document.getElementById(strId + 'chkIsAdmin').checked){
        strInfo = strInfo + '1';
    }
    else{
        strInfo = strInfo + '0';
    }
    
    var x = createXMLHttpRequest();
    x.onreadystatechange = function(){
        if(x.readyState == 4) 
        {
            if(x.status == 200) 
            {
                var tab = x.responseText.split("|");
                if (tab[0]=='0') {
                    document.getElementById(strId + 'lblErr').style.visibility = 'visible';
                    document.getElementById(strId + 'lblErr').innerHTML = tab[1];
                }
                else{
                    document.getElementById(strId + 'lblErr').style.visibility = 'hidden';
                    
                    var count = document.getElementById(strId + 'lstCourriel').options.length;
                    
                    document.getElementById(strId + 'lstCourriel').options[count] = new Option(tab[1], tab[2], false, false);
                    document.getElementById(strId + 'txtEmailAdd').value = document.getElementById(strId + 'txtEmailAdd').value + '|' + tab[1];
                }
                document.getElementById(strId + 'txtCourrielToAdd').value = "";
                document.getElementById(strId + 'chkIsAdmin').checked = false;
            }
        }
    }
    //myHandleStateChange(x);
    x.open("GET", "AjaxServer.aspx?EmailCie=" + strInfo, true);
    x.send(null);
}

//Vérification du courriel 
function CountryListOnChange(cboPays,cboProvince,txtProvince)
{
    //AccountInfo1_CompanyInfo1_drpPays
    var countryList = document.getElementById(cboPays);
    var selectedCountry = countryList.options[countryList.selectedIndex].value;

    var x = createXMLHttpRequest();
    x.onreadystatechange = function(){
        if(x.readyState == 4) 
        {
            if(x.status == 200) 
            {
                ClearAndSetStateListItems(x.responseText,cboProvince,txtProvince);
            }
        }
    }
    //myHandleStateChange(x);
    x.open("GET", "AjaxServer.aspx?Country=" + encodeURIComponent(selectedCountry), true);
    x.send(null);
}

//Clears the contents of state combo box and adds the states of currently selected country
function ClearAndSetStateListItems(countryNode,cbo,txt)
{
    if (countryNode == "")
    {
        document.getElementById(cbo).style.visibility = 'hidden';
        document.getElementById(txt).style.visibility = 'visible';
        document.getElementById(txt).value = "";
    }
    else
    {
        
        var stateList = document.getElementById(cbo);
        
        document.getElementById(cbo).style.visibility = 'visible';
        document.getElementById(txt).style.visibility = 'hidden';
        document.getElementById(txt).value = " ";
        
	    //Clears the state combo box contents.
	    for (var count = stateList.options.length-1; count >-1; count--)
	    {
		    stateList.options[count] = null;
	    }

	    var tab = countryNode.split('░');
	    var textValue; 
	    var optionItem;
	    //Add new states list to the state combo box.
	    for (var count = 0; count < tab.length-1; count++)
	    {
   		    textValue = tab[count].split('|')[1];
		    optionItem = new Option( textValue, tab[count].split('|')[0],  false, false);
		    stateList.options[stateList.length] = optionItem;
	    }
	}
}

//Vérification du courriel 
function VerififUserName()
{
    var strEmail =document.getElementById("AccountInfo1_txtCourriel");  
    var strEmailCie =document.getElementById("AccountInfo1_CompanyInfo1_txtCourriel")
    if (strEmailCie.value == "")
    {
        strEmailCie.value = strEmail.value;
    }
    var x = createXMLHttpRequest();
    x.onreadystatechange = function(){
    if(x.readyState == 4) 
    {
        if(x.status == 200) 
        {
                var tab1 = x.responseText.split('█');
                var tab = tab1[0].split('░');
                var lbl = document.getElementById("AccountInfo1_lblCourrielExist");
                lbl.innerHTML=tab[0];
                if (tab[1] =="-1")
                {
                    EnableAllCtrl(false);
                    document.getElementById("AccountInfo1_CompanyInfo1_txtNomCie").value = "";

                    document.getElementById("AccountInfo1_CompanyInfo1_txtVille").value = "";
                    document.getElementById("AccountInfo1_CompanyInfo1_txtAdresse").value = "";
                    document.getElementById("AccountInfo1_CompanyInfo1_txtCodePostal").value = "";

                    document.getElementById("AccountInfo1_CompanyInfo1_txtVilleLiv").value = "";
                    document.getElementById("AccountInfo1_CompanyInfo1_txtAdrLiv").value = "";
                    document.getElementById("AccountInfo1_CompanyInfo1_txtCodePostalLiv").value = "";
                    document.getElementById("AccountInfo1_CompanyInfo1_chkSameAdr").value = false;
                    document.getElementById("AccountInfo1_CompanyInfo1_txtTelephone").value = "";
                    document.getElementById("AccountInfo1_CompanyInfo1_txtExtension").value = "";
                    document.getElementById("AccountInfo1_CompanyInfo1_txtTelecopieur").value = "";
                    document.getElementById("AccountInfo1_CompanyInfo1_txtTelSansFrais").value = "";
                    document.getElementById("AccountInfo1_CompanyInfo1_txtCourriel").value = "";
                }
                else
                {
                    EnableAllCtrl(true);
                    
                    document.getElementById("AccountInfo1_CompanyInfo1_txtNomCie").value = tab[2];
                    document.getElementById("AccountInfo1_CompanyInfo1_drpPays").value = tab[3];
                    ClearAndSetStateListItems(tab1[1],"AccountInfo1_CompanyInfo1_cboProvince","AccountInfo1_CompanyInfo1_txtProvince");

                    document.getElementById("AccountInfo1_CompanyInfo1_cboProvince").value = tab[5];
                    document.getElementById("AccountInfo1_CompanyInfo1_txtProvince").value = tab[4];
                    document.getElementById("AccountInfo1_CompanyInfo1_txtVille").value = tab[6];
                    document.getElementById("AccountInfo1_CompanyInfo1_txtAdresse").value = tab[7];
                    document.getElementById("AccountInfo1_CompanyInfo1_txtCodePostal").value = tab[8];
                    document.getElementById("AccountInfo1_CompanyInfo1_cboPaysLiv").value = tab[10];
                    ClearAndSetStateListItems(tab1[2],"AccountInfo1_CompanyInfo1_cboProvinceLiv","AccountInfo1_CompanyInfo1_txtProvinceLiv");

                    document.getElementById("AccountInfo1_CompanyInfo1_cboProvinceLiv").value = tab[12];
                    document.getElementById("AccountInfo1_CompanyInfo1_txtProvince").value = tab[11];
                    document.getElementById("AccountInfo1_CompanyInfo1_txtVilleLiv").value = tab[13];
                    document.getElementById("AccountInfo1_CompanyInfo1_txtAdrLiv").value = tab[14];
                    document.getElementById("AccountInfo1_CompanyInfo1_txtCodePostalLiv").value = tab[15];
                    document.getElementById("AccountInfo1_CompanyInfo1_chkSameAdr").checked = new Boolean(tab[9]);
                    if (tab[9].toUpperCase()=='TRUE'){
                        SetSameAdr('AccountInfo1_CompanyInfo1_'); 
                    }
                    else{
                    document.getElementById("AccountInfo1_CompanyInfo1_txtTelephone").value = tab[16];
                    document.getElementById("AccountInfo1_CompanyInfo1_txtExtension").value = tab[17];
                    document.getElementById("AccountInfo1_CompanyInfo1_txtTelecopieur").value = tab[18];
                    document.getElementById("AccountInfo1_CompanyInfo1_txtTelSansFrais").value = tab[19];
                    document.getElementById("AccountInfo1_CompanyInfo1_txtCourriel").value = tab[20];
                    }
                }
        }
    }
    }
    //myHandleStateChange(x);
    x.open("GET", "AjaxServer.aspx?Email="+strEmail.value, true);
    x.send(null);
}

function EnableAllCtrl(enable)
{
    document.getElementById("AccountInfo1_CompanyInfo1_txtNomCie").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_drpPays").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_cboProvince").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_txtProvince").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_txtVille").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_txtAdresse").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_txtCodePostal").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_cboPaysLiv").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_cboProvinceLiv").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_txtProvince").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_txtVilleLiv").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_txtAdrLiv").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_txtCodePostalLiv").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_chkSameAdr").disabled = enable; 
    document.getElementById("AccountInfo1_CompanyInfo1_txtTelephone").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_txtExtension").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_txtTelecopieur").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_txtTelSansFrais").disabled = enable;
    document.getElementById("AccountInfo1_CompanyInfo1_txtCourriel").disabled = enable;
}

/*function SetErrProvinceVide(strId){
    var bOk = true;
    
    if (document.getElementById(strId + "txtProvince").style.visibility == 'visible'){
        if (document.getElementById(strId + 'txtProvince').value == ''){
            document.getElementById(strId + 'lblErrPro').style.visibility = 'visible';
            bOk= false;
        }
        else{
            document.getElementById(strId + 'lblErrPro').style.visibility = 'hidden';
        }
    }
    
    if (document.getElementById(strId + "txtProvinceLiv").style.visibility == 'visible'){
        if (document.getElementById(strId + 'txtProvinceLiv').value == ''){
            document.getElementById(strId + 'lblErrProLiv').style.visibility = 'visible';
            bOk= false;
        }
        else{
            document.getElementById(strId + 'lblErrProLiv').style.visibility = 'hidden';
        }
    }
    
    return bOk;
}*/
