// Copyright Sony CRM-VL Europe
// This function provides the XSLT parameters to fill the dropdown
// boxes for the manuals page
// =================================================================

function getHttpRequest() 
{
	try {
		return new XMLHttpRequest(); }
	catch ( tryMicrosoft ) {
		try {
			return new ActiveXObject( "Msxml2.XMLHTTP" ); }	
		catch ( otherMicrosoft ) {
			try {
				return new ActiveXObject( "Microsoft.XMLHTTP" ); }
			catch ( failed ) {
				return false;
			}
		}
	}
}



function showManualsForModel( model, l )
{
	model = ( model == '' ) ? $( 'searchModelName' ).value : model;
	if ( model == '' ) return; // Still empty? -> Then do nothing
	
	var httpRequest = getHttpRequest();
	if ( httpRequest ) {
		httpRequest.open( 'GET', '/shared/code/supportwebservs.asmx/getModelInfo?model=' + model + '&l=' + l, true );
		httpRequest.onreadystatechange = function () 
										 {
											if ( httpRequest.readyState == 4  ) {
												if ( httpRequest.status == 200  ) {
													try {
														var txt = httpRequest.responseXML.getElementsByTagName( 'string' )[0].childNodes[0].nodeValue;
														if (txt != 'not found') {
															var manualInfo = txt.split( '|' );
															// Show the table with the possible downloads
															showManManuals( manualInfo[0], manualInfo[1], manualInfo[2], l )
															// Show the popular section
															showPopularLinks( manualInfo[0], manualInfo[1], l )

															// Show the Feedback section at the bottom
															// document.getElementById( 'divFeedback' ).style.display = 'block';

															// Show the Feedback section on the left
															$( 'feedbackBox' ).style.display = 'block';
														} else {
															updateHash( model );
															refreshSections( '-' );

														}	
													}
													catch ( e ) {}
												}
											}
										 }
		httpRequest.send( null );
	}
}


/* -----------------------------------------------------------------------
   -- Show the table with the manuals available for the specified model --
   ----------------------------------------------------------------------- */

function showManManuals(catId, subCatId, model, l )
{
	if (catId == "") catId = $('selMainCat').value;
	if (subCatId == "") subCatId = $('selSubCat').value;
	// Add the model name to the section title
	with ( $( 'divDownloadsTitle' ) )
	{
		var pos = innerHTML.indexOf( '|' );
		if ( pos == -1 ) {
			innerHTML += '|&#160;&#160;' + model; }
		else {
			innerHTML = innerHTML.substring( 0, pos + 1 ) + '&#160;&#160;' + model;
		}
	}
	
	if ( model != '' ) 
	{
		var httpRequest = getHttpRequest();
		if ( httpRequest ) {
			httpRequest.open( 'GET', '/shared/code/supportwebservs.asmx/getManualsList?cat=' + catId + '&subCat=' + subCatId + '&model=' + model + '&l=' + l, true );
			httpRequest.onreadystatechange = function () 
											 {
												if ( httpRequest.readyState == 4  ) {
													if ( httpRequest.status == 200  ) {
														try {
															try {
																httpRequest.responseXML.normalize(); } 
															catch ( e )  {
																// Do nothing. The above is only needed for firefox which truncates the text nodes that are too big 
																// into small pieces (also text nodes)
															}	
															updateHash( model );
															refreshSections( model );
															$( 'divManuals' ).innerHTML = httpRequest.responseXML.getElementsByTagName( 'string' )[0].childNodes[0].nodeValue;
														}
														catch ( e ) {}
													}
												}
											 }
			httpRequest.send( null );
		}
	} else {
		// This is the case when the option <blank> is selected on the models dropdown box (and a previous model was selected)
		refreshSections( '' );
		showPopularLinks( catId, subCatId, l )  
	}	
}


/* ------------------------------------------------------------------------------------------------
   -- Show the popular are section (if there is something defined for that category/subcategory) --
   ------------------------------------------------------------------------------------------------ */

function showPopularLinks( catId, subCatId, l )   
{
	var httpRequest = getHttpRequest();
	if ( httpRequest ) {
		httpRequest.open( 'GET', '/shared/code/supportwebservs.asmx/getManualsPopularLinks?cat=' + catId + '&subCat=' + subCatId + '&l=' + l, true );
		httpRequest.onreadystatechange = function () 
										 {
											if ( httpRequest.readyState == 4  ) {
												if ( httpRequest.status == 200  ) {
													try {
														txt = httpRequest.responseXML.getElementsByTagName( 'string' )[0].childNodes[0].nodeValue;

														var txtImg	 = '<img src="/dl_img/arrow_dblue_r.gif" alt="" border="" align="absmiddle"/>&#160;';
														var txtHtml  = '';
														var lstLinks = txt.split( '^' );
														for ( i = 0; i < lstLinks.length; i++ ) {
															var aLink = lstLinks[i].split( '~' );
															txtHtml += txtImg;
															/*
															txtHtml += '<a class="popular_area" href="' + aLink[1] + '?l=' + l + aLink[2] + '" ';
															txtHtml += 'target="' + aLink[3] + '" onclick="' + aLink[4] + '" ';
															txtHtml += 'id="' + aLink[5] + '">' + aLink[0] + "</a><br/>";
															*/

															txtHtml += '<a class="popular_area" href="' + aLink[1];
															// Add the language if necessary (Not needed for model CMR-PC4 USB)
															if ( aLink[1].substr( aLink[1].length - 4, 4 ) != '.exe' ) {
																txtHtml += '?site=' + odw;
															}	
															// Add the others parameters
															txtHtml += aLink[2] + '" ';
															// Add target and event handlers
															txtHtml += 'target="' + aLink[3] + '" onclick="' + aLink[4] + '" ';
															// Add the anchor id and text
															txtHtml += 'id="' + aLink[5] + '">' + aLink[0] + "</a><br/>";
														}
														
														var tblPopularLinks = $( 'tblPopularLinks' );
														if ( tblPopularLinks.style.display != 'block') {
															tblPopularLinks.style.display = 'block';
														}	
														$( 'divPopularLinks' ).innerHTML = txtHtml;
													}
													catch ( e ) {
														$( 'tblPopularLinks' ).style.display = 'none';
													}
												}
											}
										 }
		httpRequest.send( null );
	}
}



/* ----------------------------------------------------------------------------------
   -- Populates the dropdown box with the models based on the subcategory selected --
   ---------------------------------------------------------------------------------- */

function showManModels( lang )
{
	var catId 	 	= $( 'selMainCat' ).value;
	var subCatId  	= $( 'selSubCat' ).value;
	var oModelBox 	= $( 'selModel' );
	
	// Reset the other controls/sections
	$( 'selModel' ).options.length = 0;
	refreshSections( '' );
	
	if ( subCatId != '' ) 
	{
		// Populate the dropdown box with the models
		var httpRequest = getHttpRequest();
		if ( httpRequest ) {
			httpRequest.open( 'GET', '/shared/code/supportwebservs.asmx/getManualsModels?cat=' + catId + '&subCat=' + subCatId, true );
			httpRequest.onreadystatechange = function () 
											 {
												if ( httpRequest.readyState == 4  ) {
													if ( httpRequest.status == 200  ) {
														oModelBox.options[0] = new Option( '', '' ); // The 1st option is always empty
														var txt = httpRequest.responseXML.getElementsByTagName( 'string' )[0].childNodes[0].nodeValue;
														var lstModels = txt.split( '|' );
														for ( i = 0; i < lstModels.length; i++ ) {
															oModelBox.options[i + 1] = new Option( lstModels[i], lstModels[i] );
														}
														oModelBox.style.display = 'block';
													}
												}
											 }
			httpRequest.send( null );
		}
		showPopularLinks( catId, subCatId, lang );
	}
	else {
		showPopularLinks( catId, '', lang );
		oModelBox.style.display = 'none';	
	}
}

/* ---------------------------------------------------------------------------------------
   -- Populates the dropdown box with the subacategories based on the category selected --
   --------------------------------------------------------------------------------------- */
   
	function showManSubcats( lang ){
	var catId 	 	= $( 'selMainCat' ).value;
	var oSubCatBox = $('selSubCat');

	$('selModel').style.display = 'none';
	
	// Reset the other controls/sections
	$( 'selSubCat' ).length = 0;

	refreshSections( '' );
	
	switch ( catId ) { 
		case "VAIO" :
			var winVaio = window.open( 'http://www.vaio-link.com/manuals/manuals.asp?l=' + lang, 'winVaio', '' );
			break;
		case "CLIE" :
			var urlLang = lang == 'fr' ? 'fr' : lang == 'de' ? 'de' : 'uk';
			var winClie = window.open( 'http://www.clie-link.com/' + urlLang + '/manuals.asp?submenu=manuals.asp&mainmenu=newuser.asp', 'winClie', '' );
		case "" :
			oSubCatBox.style.display = 'none';	// Just hide the dropdown box
			break;
		default : 
			// Populate the dropdown box with the subcategories
			var httpRequest = getHttpRequest();
			if ( httpRequest ) {
				httpRequest.open( 'GET', '/shared/code/supportwebservs.asmx/getManualsSubCats?cat=' + catId + '&l=' + lang, true );
				httpRequest.onreadystatechange = function () 
												 {
													if ( httpRequest.readyState == 4  ) {
														if ( httpRequest.status == 200  ) {
															oSubCatBox.options[0] = new Option( '', '' ); // The 1st option is always empty
															var txt = httpRequest.responseXML.getElementsByTagName( 'string' )[0].childNodes[0].nodeValue;
															var lstSubCats = txt.split( '^' );
															lstSubCats.sort();
															for ( i = 0; i < lstSubCats.length; i++ ) {
																var subcat = lstSubCats[i].split( '|' );
																oSubCatBox.options[i + 1] = new Option( subcat[0], subcat[1] );
															}
															oSubCatBox.style.display = 'block';
														}
													}
												 }
				httpRequest.send( null );
			}
			showPopularLinks( catId, '', lang );
	}			
}


function updateHash( model ) 
{
	if ( document.location.href.toString().toLowerCase().indexOf( 'searchmodelname' ) == -1 ) {
		document.location.hash = 'm=' + model;
	}
}



function refreshSections( model ) {
	// Show/Hide the sections depending on whether we have a valid model or not
	$( 'tblManSection' ).style.display 	= ( ( model != '-' ) && ( model != '' ) ) ? 'block' : 'none';
	$( 'feedbackBox' ).style.display	 	= ( ( model != '-' ) && ( model != '' ) ) ? 'block' : 'none';
	$( 'tblPopularLinks' ).style.display	= ( ( model != '-' ) && ( model != '' ) ) ? 'block' : 'none';
	$( 'tblError' ).style.display	 	   	= ( model == '-' ) ? 'block' : 'none';}

