function GroupClicked(chkbox) {
    var bCheck = chkbox.checked;
    for (var i=0; i<document.forms[0].elements.length; i++) {
        var elem = document.forms[0].elements[i];
        if (elem.tagName == 'INPUT' && elem.type == 'checkbox') {
            if (elem.name.substr(0, chkbox.name.length).indexOf(chkbox.name) == 0) {
                if (chkbox != elem && !elem.disabled) {
                    if (elem.checked != bCheck) {elem.click(); }
                }
            }
        }
    }
    return false;
}

function DeselectRestOfGroup(id, chkbox) {
    for (var i=0; i<document.forms[0].elements.length; i++) {
        var elem = document.forms[0].elements[i];
        if (elem.tagName == 'INPUT' && elem.type == 'checkbox') {
            if (elem.id.indexOf(id) == 0) {
                if (chkbox != elem && !elem.disabled) {
                    if (elem.checked == true) {
                        elem.checked = false;
                        SelectRow(elem, elem.parentNode.parentNode.id); 
                    }
                }
            }
        }
    }
    return false;
}

function SelectRow(chkbox, rowID) {
    var bCheck = chkbox.checked;
    var oRow = document.getElementById(rowID);
    if (oRow == null) return false;
    if (bCheck) {oRow.style.backgroundColor = '#DDDDDD';} 
    else {oRow.style.backgroundColor = '#F3F3F3';}
}

function SelectRowGroup(chkbox, rowID, colSpan) {
    var bCheck = chkbox.checked;
    for (var i=0; i<colSpan; i++) {
        var oRow = document.getElementById(rowID + '_' + i);
        if (oRow == null) return false;
        if (bCheck) {oRow.style.backgroundColor = '#DDDDDD';} 
        else {oRow.style.backgroundColor = '#F3F3F3';}
    }
}

function ExtDataGridCmd(chkboxname, url) {
    url += '&elements=';
    for (var i=0; i<document.forms[0].elements.length; i++) {
        var elem = document.forms[0].elements[i];
        if (elem.tagName == 'INPUT' && elem.type == 'checkbox' && elem.name.substr(0, chkboxname.length).match(chkboxname)) {
            if (elem.checked) {  
                if (elem.value != 'all') {              
                    url += elem.value;
                    url += '+';
                    //elem.click();
                } else {
                    //elem.checked = false;
                }
            }
        }
    }     
    window.open(url.substring(0, url.length - 1),'Action','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=200'); 
    
    document.forms[0].submit();
}

function ExtFilterTableUnselect(filtername) {
    for (var i=0; i<document.forms[0].elements.length; i++) {
        var elem = document.forms[0].elements[i];
        if (elem.tagName == 'INPUT' && elem.type == 'checkbox' && elem.id.substr(0, filtername.length).match(filtername)) {
            if (elem.checked) {
                elem.checked = false;
            } else {
            }
        }        
    }
}

function ExtFilterCheck(filtername) {
  if (document.getElementById(filtername + "_any").checked == true) {
    document.getElementById(filtername + "_set").checked = true;
  }
}

function CombTableDeactivateOpposite(chkelem) {
    if (chkelem.checked) {
        if (chkelem.id.indexOf('_include') > 0) {
            var opp1 = document.getElementById(chkelem.id.replace('_include','_exclude'));
            if (opp1 != null) {
                opp1.checked = false;
            }
        } else {
            var opp2 = document.getElementById(chkelem.id.replace('_exclude','_include'));
            if (opp2 != null) {
                opp2.checked = false;
            }
        }                
    }    
}

var AmazonImagesToLoad = new Array();

function loadAmazonImages(){
   var noImages = AmazonImagesToLoad.length;
   var groupSize = 20;
   var groupDelay = 2000;
   var delay = -groupDelay;
   for(var i=0; i<noImages; i++){
      if(i%groupSize == 0){
         delay += groupDelay;
      }
      var noticeid = AmazonImagesToLoad[i];
      setTimeout('getAmazonImgUrl('+noticeid+', true)', delay);
   }
}


function getImageURL(noticeId, shortView) {
	var imageFound = false;

	// Get Image from Amazon
    var command;
    if (shortView) {
        command ="id="+noticeId+"&small=yes"
    } else {
        command = "id="+noticeId
    }
	    
    $.ajax({
		type: "GET",
		url: "AmazonImage.ashx",
		data: command,
		success: function(response){
			if (response.length > 0){
				var exception = 'Exception: ';
				var ex = false;
				        
				if (response.substr(0, exception.length) === exception)  {
					ex = true;
				}
        
				if (shortView) {
					if (!ex) {
						var img = $('#amazonimg'+noticeId);
					}
				} else {
					if (ex) {
						var err = $('#amazonerr');
					} else {
						var img = $('#amazonimg');
					}
				}
				if (ex) {
					if(!shortView){
						err.show();
						err.text(response);
					}
				} else {
					img.attr('src', response);
					img.css('width', '');
					img.css('height', '');
					imageFound = true;
				}
			}  
		}
	});
	
	//If no image found, get it from Buchzentrumservice
	if (!imageFound) {
		$.ajax({
			type: "GET",
			url: "BuchzentrumService.ashx",
			data: "id="+noticeId+"&image=True",
			success: function(response){
				if (response.length > 0) {
					var img = $('#amazonimg');
					img.attr('src', response);
					img.css('width', '');
					img.css('height', '');
				}
			}
		});
	}
}

function getBuchzentrumSummary(noticeId) {
	$.ajax({
		type: "GET",
		url: "BuchzentrumService.ashx",
		data: "id="+noticeId+"&image=False",
		success: function(response){
			var summary = $('#bzsummary');
			if (response.length > 0) {
				summary.html(response);
			} else {
				summary.html("-");
			}
		}
	});
}

function getMusicNotesImage(noticeId) {
	// Get Music Notes Image
    var command = "id="+noticeId;
        
    $.ajax({
		type: "GET",
		url: "MusicNotes.ashx",
		data: command,
		success: function(response){
			if (response.length > 0){
					var img = $('#musicnotesimg');
					img.attr('src', response);
					img.css('width', '');
					img.css('height', '');
					imageFound = true;
			}  
	   }
   });
}

function monitorSessionExpiry(time, parms){
    setTimeout("redirectAfterExpiry('" + parms + "')", time);
}

function redirectAfterExpiry(parms){
    window.location = "SessionEnd.aspx" + parms;
}

function keepAliveSession(time){
    setTimeout('redirectAfterKeepAlive()', time);
}

function redirectAfterKeepAlive(){
    window.location = window.location;
}

function showHolidays(){
    win1 = window.open("holidayPeriods.aspx", "Information", "width=600,height=400,status=yes,scrollbars=yes,resizable=yes");
    win1.focus();
}

function fnTrapKD(event, btnId){
   if (event.keyCode == 13)
   { 
     var btn = document.getElementById(btnId);
     event.returnValue=false;
     event.cancel = true;
     btn.click();
   } 
}


// PopupWindow
function popup_display(id){
    id = '#' + id;
    var pu = $(id);
    pu.show('slow');
    
    
    // hide button
    $(id + ' .popup_close').click(function(e){
        pu.hide();
        return false;
    });
    
    // automatically hide after 5 secs
    setTimeout(function(){
        pu.hide();
    }, 5000);
}

// Folded MARC Datafields
function prepareFoldDiv(id){
    id = '#' + id;
    
    $(id + ' .btnShow').click(function(){
        $(id + ' .content').show();
        $(id + ' .btnShow').hide();
        return false;
    });
}


// Scrollbar on top of the short view
$(function(){
    $("#TopHorizontalScrollbar").scroll(function(){
		adjustWidth();
		var scrollAmount = $("#TopHorizontalScrollbar").scrollLeft()
        $("#ScrollbarWrapper")
            .scrollLeft(scrollAmount);
		//$('#TopHorizontalScrollbar').css('margin-left',scrollAmount+'px');
    });
    $("#ScrollbarWrapper").scroll(function(){
		adjustWidth();
		var scrollAmount = $("#ScrollbarWrapper").scrollLeft();
        $("#TopHorizontalScrollbar")
            .scrollLeft(scrollAmount);
		//$('#TopHorizontalScrollbar').css('margin-left',scrollAmount+'px');
    });
});

$(window).resize(function() {
	adjustWidth();
});

// Initialize and adjust the width of the wrapper div
$(function() {
   scrollbar = $('#TopHorizontalScrollbar');
   if (scrollbar.length > 0) {
      scrollbar.hide();
   }
   adjustWidth()
});

function adjustWidth(){
   var dataGrid = $('#ExtDataGrid1');
   var scrollbar = null;
   scrollbar = $('#TopHorizontalScrollbar');
   
   var newWidth;
   if ((dataGrid.length > 0) && (scrollbar.length > 0)) {
      
      newWidth = dataGrid.outerWidth(true);
	   $('#TopHorizontalScrollbarDiv').width(newWidth);

	   scrollbar.show();
	   if (dataGrid.width() < scrollbar.width()) {
		   scrollbar.hide();
	   } else {
		   scrollbar.show();
	   }
   }
}

