var request;
var gFunctionHandler;

function initAjax() {       
    var request;
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = false;
			}
		}
	}
	
	if (!request) {
		alert("Error initializing XMLHttpRequest!");
	} else {
		return request;
	}
    
}

function sendRequest( url, functionHandler ) {
    gFunctionHandler = functionHandler;
    //alert (url);
    request = initAjax();
    request.open('GET', url, true);
    
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    request.onreadystatechange = handleAjaxResponse;
    request.send(null);
    
}

function handleAjaxResponse() {
    if ( request.readyState != 4 ) return;
        if ( request.status == 200 ) {
            //alert("url existe");
            var rep = request.responseText;
            //alert (rep);
            switch ( gFunctionHandler ) {
                case 'makeCatalogue' :
                    makeCatalogue( rep );
                break; 
            }

        } else {
            if ( request.status == 404 ) {
                alert("url not existe");
            } else {
                alert("request status = " + request.status );
            }
        }

}


function makeCatalogue( rep ) {
    
    //alert( rep );
    var t = rep.split("|");
    genreId = 0;
    var genre = "";
    var catalogue = "";
    

    switch( t[0] ) {
        
        case 'noselect':
            genreId = parseInt(t[1]) + 1;

            if ( genreId == 2 ) {
                document.getElementById("genre02").style.visibility = "hidden";
                document.getElementById("genre03").style.visibility = "hidden";
            } else if ( genreId == 3 ) {
                document.getElementById("genre03").style.visibility = "hidden";
            }
            catalogue += "<br/><br/>Veuillez selectionner \340 l\'aide de la recherche ci-dessus le genre de musique dont vous voulez obtenir les d\351tails<br/><br/>";
            document.getElementById('tableCatalogue').innerHTML = catalogue;
            break;
        
        case 'selectok':
            genreId = parseInt(t[1]) + 1;
            
            if ( genreId == 2 ) {
                document.getElementById("genre02").style.visibility = "visible";
                document.getElementById("genre03").style.visibility = "hidden";
                selectList = document.getElementById("genre02");
            } else if ( genreId == 3 ) {
                document.getElementById("genre03").style.visibility = "visible";
                selectList = document.getElementById("genre03");
            }
            // -- On efface la selectlist
            for (var i=1; i < selectList.length; i++) {
                selectList.options[i] = null;
            }
            // -- On rempli la nouvelle selectlist
            var pos=1;
            for (var i=2; i < t.length; i+=2) {
                selectList.options[pos] = new Option (t[i], t[i+1]);
                pos++;
            }
            catalogue += "<br/><br/>Veuillez selectionner \340 l\'aide de la recherche ci-dessus le genre de musique dont vous voulez obtenir les d\351tails<br/><br/>";
            document.getElementById('tableCatalogue').innerHTML = catalogue; 
            break;

        case 'selectempty':
            
            genreId = parseInt(t[1]) + 1;
            if ( genreId == 2 ) {
                document.getElementById("genre02").style.visibility = "hidden";
                document.getElementById("genre03").style.visibility = "hidden";
            } else if ( genreId == 3 ) {
                document.getElementById("genre03").style.visibility = "hidden";
            }

            for (var i=2; i < t.length; i+=19) {

                catalogue += '<table cellspacing="0" cellpadding="0" style="font-family: Verdana; font-size: 12px; color: #000000; border-right: 0px solid #333333; background-color: #FFA07A; width: 690px">'
                catalogue += '  <tr id="td_menu_normal" onMouseOver="this.id=\'td_menu_hover\';" onMouseOut="this.id=\'td_menu_normal\';">'
                catalogue += '    <td>'

                catalogue += '<table cellspacing="0" cellpadding="0">'
                catalogue += '  <tr>'
                catalogue += '    <td width="300px" style="text-align: left; font-family: Verdana; font-size: 12px; font-weight: bold;">' + t[i+1] + '&nbsp;' + t[i+18] + '</td>'
                if ( t[i+2] != '' ) {
                    catalogue += '    <td width="390px" align="right">(' + t[i+2] + ')</td>'
                } else {
                    catalogue += '    <td width="390px" align="right">&nbsp;</td>'
                }
                catalogue += '  </tr>'
                catalogue += '  <tr>'
                catalogue += '    <td>&nbsp;</td>'
                catalogue += '    <td align="right">' + t[i+14] + '</td>'
                catalogue += '  </tr>'
                catalogue += '</table>'

                catalogue += '<table cellspacing="0" cellpadding="0">'
                if ( t[i+3] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Duree&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+3] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+4] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Effectif&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+4] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+5] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Roles&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+5] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+6] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Creation&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+6] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+7] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Reprise&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+7] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+8] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Emission&nbsp;radiophonique&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+8] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+9] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Publication&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+9] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+10] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Sc\350nes additionnelles&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+10] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+11] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Representation&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+11] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+12] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Television&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+12] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+13] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Recitant&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+13] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+15] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Dedicace&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+15] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+16] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Premiere diffusion&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+16] + '</td>'
                    catalogue += '  </tr>'
                }
                if ( t[i+17] != '' ) {
                    catalogue += '  <tr valign="top"> '
                    catalogue += '    <td align="right" width="180">Enregistrement&nbsp;:&nbsp;</td>'
                    catalogue += '    <td align="left">' + t[i+17] + '</td>'
                    catalogue += '  </tr>'
                }

                    catalogue += '</table>'

                catalogue += '</td>'
                catalogue += '</tr>'
                catalogue += '</table>'

                catalogue += '<br/><br/>'


                pos++;
            }
            document.getElementById('tableCatalogue').innerHTML = catalogue;

            break;
    }
    
}

