function newCaptcha(imageElementId, captchaUrl) {
    var imageElement = document.getElementById(imageElementId);
    if (imageElement != null) {
        captchaUrl += '?newCaptcha=true&rand=' + Math.random();
        imageElement.src = captchaUrl;
    }
}

function updateContentNoGrayout(componentId, page) {

   jQuery.get(page, function(data) {
	   $('#' + componentId).html(data);
   });

   // webtrends
   dcsMultiTrack('DCS.dcsuri', document.location.pathname.substring(0, document.location.pathname.lastIndexOf('.')) + '/' + page
			     ,'WT.ti',componentId + '_' + page);
}


function updateContent(componentId, page) {
    // Show overlay (not with onLoading callback, onComplete is
    // sometimes faster than onLoading causing permanent grayout.
    showOverlay(componentId);

    // Do update.
    jQuery.get(page, function(data) {
    	$('#' + componentId).html(data);
    	hideOverlay(componentId);
    });

    // webtrends
    dcsMultiTrack('DCS.dcsuri', document.location.pathname.substring(0, document.location.pathname.lastIndexOf('.')) + '/' + page
			      ,'WT.ti',componentId + '_' + page);
}


function updateImage(imageId, imageSrc) {

    document.getElementById(imageId).src=imageSrc;
}


function hideOverlay(componentId) {

    var containerDiv = document.getElementById(componentId);
    var parent = containerDiv.parentNode;
    var childNodes = parent.childNodes;

    for (var i = 0; i < childNodes.length; i++) {
        if (childNodes[i].id == 'generic-overlay') {
            parent.removeChild(childNodes[i]);
        }
    }
}

function showOverlay(componentId) {

    var containerDiv = document.getElementById(componentId);

    // create image.
    var image = document.createElement('img');
    image.setAttribute('src', ROOT + 'images/progress_indicator.gif');
    image.setAttribute('alt', '');
    image.setAttribute('border', '0');

    // create indicator div.
	var indicator = document.createElement('div');
    indicator.style.position = 'absolute';
    indicator.style.top = '50%';
	indicator.style.left = '50%';
	indicator.style.width = '100%';

    // create overlay div.
	var overlay = document.createElement('div');
	overlay.setAttribute('id', 'generic-overlay');
	overlay.style.position = 'absolute';
	overlay.style.backgroundColor = '#fff';
	overlay.style.opacity = '0.7';
	overlay.style.filter = 'alpha(opacity=70)';
	overlay.style.zIndex = '998';
    overlay.style.top = '0';
    overlay.style.left = '0';
	overlay.style.width = containerDiv.offsetWidth + 'px';
	overlay.style.height = containerDiv.offsetHeight + 'px';
	overlay.style.display = 'block';

    // append.
	indicator.appendChild(image);
	overlay.appendChild(indicator);
	containerDiv.parentNode.appendChild(overlay);
}

function specialProductSearchBySelectionOptions() {
	var menuItemId;
	var selectedProductCategoryCode = $('#criteria\\.productCategoryCode').val();
	if (selectedProductCategoryCode == '210') {
		/* Equities */
		menuItemId = 'Equities';
	} else if (selectedProductCategoryCode == '211') {
		/* Interest */
		menuItemId = 'Interest';
	} else if (selectedProductCategoryCode == '212') {
		/* Commodities */
		menuItemId = 'Commodities';
	} else if (selectedProductCategoryCode == '213') {
		/* Credit */
		menuItemId = 'Credit';
	} else if (selectedProductCategoryCode == '214') {
		/* Other */
		menuItemId = 'Other';
	}

	if (menuItemId) {
		$('#specialProductSearchForm').append("<input type=\"hidden\" name=\"menuItemId\" value=\"" + menuItemId + "\"/>");
	}
	$('#specialProductSearchForm').submit();
}

function excelExport() {
	// webtrends
	dcsMultiTrack('DCS.dcsuri', document.location.pathname.substring(0, document.location.pathname.lastIndexOf('.')) + '/save_as_excel'
			  ,'WT.ti','save_as_excel','DCSext.excel','1');
	
	var myOriginalFormAction = $('#specialProductSearchForm').attr('action');
	$('#specialProductSearchForm').attr('action', ROOT + 'products/productSearchExport.do').submit();

	// set the form action back to the original
	$('#specialProductSearchForm').attr('action', myOriginalFormAction);
}

function pdfExport() {
	alert('This functionality is not yet implemented.');
}
