/**
 * AJAX_RETURN_FUNCTIONS
 * Functions that are called using the result of a HTTP call as the parameter
 */


/**
 * create the Product Finder extended textbox
 * @param - pArrayDataString : String representation of an array - '["element1","element2","element3","element4"]'
 */
function CreateProductFinder(pArrayDataString)
{
    var oProductFinder = new AutoSuggestControl( document.getElementById("txtProductFinder"), new ProductSuggestions( eval(pArrayDataString) ) );
}

/**
 * populate criteria range checkboxes panel HTML
 * @param - pRangeCode : string containing range code
 */
function GetProductHTML(pCheckboxArray /*string*/)
{
    // Populate the DIV with HTML
    pCheckboxArray = eval(pCheckboxArray);
    masterCheckboxArray = pCheckboxArray.slice();

    var strHTML = ''; 
    
    for(var s=0;s<masterCheckboxArray.length;s++)
    {
        strHTML += '<div class="FloatLeft"><div class="LabelColumn"><div class="ColumnTitle">' + masterCheckboxArray[s][1] + '</div>';

        // Label col
        for(var c=0;c<masterCheckboxArray[s][2].length;c++)
        {
            strHTML += '<div class="CheckBoxRow"><label ' + masterCheckboxArray[s][2][c][2] + ' id="l' + masterCheckboxArray[s][2][c][0] + '" for="' + masterCheckboxArray[s][2][c][0] + '">' + masterCheckboxArray[s][2][c][1] + '</label></div>';
        }
        strHTML += '</div>';
        
        // Checkbox column : add extra class if needed
        strHTML += '<div class="CheckBoxColumn ';
        if (s == masterCheckboxArray.length-1) strHTML += 'FinalColumn'
        strHTML += '"><div class="ColumnTitle">&nbsp;</div>';
                                    
        for(var c=0;c<masterCheckboxArray[s][2].length;c++)
        {
            strHTML += '<div class="CheckBoxRow">';
            strHTML += '<input type="checkbox" ' + masterCheckboxArray[s][2][c][3] + ' name="' + masterCheckboxArray[s][2][c][0] + '" value="' + s + '" id="' + masterCheckboxArray[s][2][c][0] + '" onclick="repopulate_results(this.name,\'' + s + '\');" /></div>';
        }
        strHTML += '</div></div>'    ;
    }
    
    var MainDivName = 'CheckBoxesColumns';
    var d = document.getElementById(MainDivName);
    d.innerHTML = strHTML;
    

    // Now the HTML is loaded, get the data
    //alert("Fetching 'GetProductDetails'");
    makeHttpRequest(productPaneUrl, 'GetProductDetails', false);
}


/**
 * Check the checkboxes and call the filter
 * @param - pProductCode : string containing product code to display
 */
function GetProductDetails(pProductRangeString)
{
    // Populate the array with the data    
    productarray = eval(pProductRangeString);
    
    // Now we've got the HTML and the Array we can filter
    //filter_results(iResults);
     var d = document.getElementById('ProductsResults');
     d.innerHTML = outputHTML(productarray);             
     //Update the number of products found
     document.getElementById('numberproducts').innerHTML = productarray.length;
}


/**
 * populate page with appropriate content
 * @param - pContent : string containing page html
 */
function GetPageDetails(pContent /*string*/)
{
    var MainDivName = 'MainBodyArea';
    var d = document.getElementById(MainDivName);
    d.innerHTML = pContent;
}



/**
 * Fetch the HTML for the Downloads Centre tabs (4)
 * @param - pTabHTMLString : string containing Tab HTML
 */
function CreateTabs(pTabHTMLString)
{
    /* Render the tabs HTML*/
    var TabDivName = 'DownloadsTabs';
    var d = document.getElementById(TabDivName);
    d.innerHTML = pTabHTMLString;
    
    /* Now render the tab content for the first time*/
    makeHttpRequest('snippet_downloads.asp?createtabs=false&id=1', 'CreateTabContent', false)
}

/**
 * Fetch the HTML for the Downloads Centre Content
 * @param - pContentHTMLString : string containing content HTML
 */
function CreateTabContent(pContentHTMLString)
{
    /* Render the tabs HTML*/
    var ContentDivName = 'DownloadsContent';
    var d = document.getElementById(ContentDivName);
    d.innerHTML = pContentHTMLString;
}

function CreateProductContent(pDownloadProductString)
{
    // Only if Tab 2 is clicked...
    // Populate the productarray with data    
    productarray = eval(pDownloadProductString);
    
    //Get the HTML for checkboxes for Tab 2
    makeHttpRequest('snippet_downloads.asp?createtabs=false&id=2', 'CreateTab2HTML', false);
}    

function CreateTab2HTML(pDownloadProductHTML)
{
    /* Render the tabs HTML*/
    var ContentDivName = 'DownloadsContent';
    var d = document.getElementById(ContentDivName);
    d.innerHTML = pDownloadProductHTML;
    
    // Set the product count element to be visible
    document.getElementById("DownloadsTotalFound").className= 'visible';
    
    // Now we've got the HTML and the Array we can filter
    // set all checkboxes to checked
    /* REMOVED 11/02 AT CLIENT REQUEST
    var allInputs = document.getElementsByTagName("input");
    if(allInputs != null){
       for(var i = 0; i < allInputs.length;i++) {
            if(allInputs[i].type == "checkbox" && allInputs[i].disabled == ""){
                allInputs[i].checked = "checked";
                iResults.push(allInputs[i].name);
            }
       }
    }*/
    // Filter the results (first time through)
    filter_results(iResults);
}




/**
 * Fetch the HTML for the product title (Product Description Page)
 * @param - pTitleString : string containing content HTML
 */
function PopulateProductTitle(pTitleString)
{
    /* Render the product title HTML*/
    var ProductTitleDivName = 'ProductNameDiv';
    var d = document.getElementById(ProductTitleDivName);
    var preHTML = '<h2 class="blue">';
    var postHTML = '</h2>';
    
    d.innerHTML = preHTML + pTitleString + postHTML;
}


/**
 * Fetch the HTML for the product title (Product Description Page)
 * @param - pDetailsString : string containing content HTML
 */
function PopulateProductDetails(pDetailsString)
{
    /* Render the product title HTML*/
    var ProductDetailsDivName = 'ProductDetailsArea';
    var d = document.getElementById(ProductDetailsDivName);
    
    d.innerHTML = pDetailsString;
}




/**
 * Fetch the HTML for the product title (Product Description Page)
 * @param - pQuickLinksString : string containing quick links content HTML
 */
function PopulateQuickLinks(pQuickLinksString)
{
    /* Render the product quick links HTML*/
    var ProductQuickLinksDivName = 'QuickLinks';
    var d = document.getElementById(ProductQuickLinksDivName);
    
    d.innerHTML = pQuickLinksString;
}



/**
 * Fetch the HTML for the product awards image (Product Description Page)
 * @param - pAwardsImgString : string containing quick links content HTML
 */
function PopulateAwardsImg(pAwardsImgString)
{
    /* Render the product quick links HTML*/
    var ProductAwardsImgDiv = 'ProductAwardImgDiv';
    var d = document.getElementById(ProductAwardsImgDiv);
    
    d.innerHTML = pAwardsImgString;
}







/**
 * 
 * @param - pDataString : string containing data
 */
function CreateProductObjects(pDataString)
{
    // * Get 2D product array data * //
	//var AJAXProductArray = eval(pDataString);
    AJAXProductArray = eval(pDataString);

	var AJAXImageArray = new Array();
    
    // * Create products image array * //
	for ( var i=0, len=AJAXProductArray.length; i<len; ++i ){

		// * Create the element and assign ID * //
		var tmp = document.createElement("IMG");
		tmp.id = 'productImage' + i;
		tmp.name = 'productImage' + i;

		// * Get the image details for the code * //
		tmp.src = 'productImages/' + AJAXProductArray[i][1];
		tmp.alt = 'Image for [' + AJAXProductArray[i][0] + ']';
		tmp.title = 'Image for [' + AJAXProductArray[i][0] + ']';

		// * Add default class then determine if we need to add another * //
		tmp.className = 'block';  
		if (i == 2) tmp.className = 'block mainblock';
		if (i == 0 || i > 3) tmp.className = 'block hiddenblock';
			
		// * assign current index to created IMG element      
		AJAXImageArray[i] = tmp;

		// * Set the last item * //
		lastIdx = i;
		lastItem = tmp;
	}

    var productCounter = 1;
    var productTotal = AJAXImageArray.length;


	// * Clear the progress image * //
	document.getElementById('CarouselArea').innerHTML = '';


	// * Populate carousel DIV with image data * //
	for ( var i=0, len=AJAXProductArray.length; i<len; ++i ){
		document.getElementById('CarouselArea').appendChild(AJAXImageArray[i]);
	}


	// * Display numbering information * //
	document.getElementById('ProductPositionNumber').innerHTML = productCounter + '/' + productTotal;


	// * Display link * //
	document.getElementById('ProductBuyLink').innerHTML = '<a href="javascript:void(0);" title="' + AJAXProductArray[2][0] + '" alt="' + AJAXProductArray[2][0] + '" class="blue">Buy this product now</a>';


	// * Populate title * // 
	document.getElementById('ProductNameSpan').innerHTML = AJAXProductArray[2][3];

	// * Populate awards img (if there is one) * // 
	if (AJAXProductArray[2][6] != '')
	{
		document.getElementById('ProductAwardImgDiv').innerHTML = '<img src="/images/' + AJAXProductArray[2][6] + '" title="'+AJAXProductArray[2][6]+'" alt="'+AJAXProductArray[2][6]+'"/>';
	}
	

	// * Populate 'Quick Links' * // 
	document.getElementById('QuickLinks').innerHTML = AJAXProductArray[2][7];
	

	// * Populate product data * // 
	document.getElementById('ProductDetailsArea').innerHTML = AJAXProductArray[2][2];


    $(document).ready(function(){
        $("#linkprevious").bind("click",aPrevClick);
        $("#linknext").bind("click",aNextClick);
	});

    
}