
/**
* These Javascript functions drive many of the dbt functionality.
* Page updated by C. West 6/8/09
* Page optimized by C. West 10/4/10
*/
 
/* **********************************************************************************
	Function that calculates how many articles are selected.
*********************************************************************************** */

function hideTOCItem(action)
{
    if (action=="hide") { 
		$("#showhidearts, .articlesectionlisting, .noselectrow").hide(); 
		$("#showhideallarts").show();
	    $(".selectedrow input:checked").each(function() {
	    	var sectname = $(this).attr("name");
	    	var sectionid = "#section_" + sectname.substring(sectname.indexOf("_")+1,sectname.length);
	    	//alert(sectionid);
	    	$(sectionid).show();
	    }); 
    }
    if (action=="show") { 
		$("#showhidearts, .articlesectionlisting, .noselectrow").show(); $("#showhideallarts").hide(); 
    }
}

/* **********************************************************************************
	Function that adds a collection of articles to the cart
	Executes the post to cart through an AJAX call.
*********************************************************************************** */
function addAbsToCart(cartserver) {
	var allDialogs = [];
	options = $.extend({title: "Add Selected Articles to Your Cart"},{modal:true}, {});
	var domBody = "";
	var artcount = countSelected();
	if (artcount == 0)
	{
		domBody = "<p>Please Select at least one article</p>";
		var dialog = new Boxy(domBody, options);
		allDialogs.push(dialog);
	}
	else if (artcount > 20)
	{
		domBody = "<p>You cannot select more then 20 articles.</p>";
		var dialog = new Boxy(domBody, options);
		allDialogs.push(dialog);
	}
	else 
	{
		var ticktock = 0;
		var appendform = "<input name=\"productVariationCount\" value=\""+artcount+"\" type=\"hidden\" />";
        $(".selectedrow input:checked").each(function() {
            appendform+="<input name=\"cartItem["+ticktock+"].remoteSite\" value=\"QAB\" type=\"hidden\" />";
            appendform+="<input name=\"cartItem["+ticktock+"].quantity\" value=\"1\" size=\"2\" maxlength=\"3\" type=\"hidden\" />";
            appendform+="<input name=\"cartItem["+ticktock+"].skuID\" value=\""+$(this).val()+"\" type=\"hidden\" />";
            ticktock++;
        }); 
                    
		appendform+="<input name=\"refURL\" value=\""+document.location.href+"\" type=\"hidden\" />";
		$("#addToCartFormelement").append(appendform);
		$("#addToCartFormelement").submit();
		//document.addToCartForm.submit();
	}
	return false;
}

/* **********************************************************************************
	Function that manages forwarding the request to send an email out
	based on how many articles were clicked.
	C.West 6.9.08
*********************************************************************************** */
function emailAbstracts() {
	
	var allDialogs = [];
	options = $.extend({title: "Email Articles"},{modal:true}, {});

	if (countSelected() == 0)
	{
		domBody = "<p>Please Select at least one article</p>";
	}
	else if (countSelected() > 20)
	{
		domBody = "<p>You cannot select more then 20 articles.</p>";
	}
	else 
	{
		domBody = "<form name=\"emailform\" onSubmit=\"return colleague_formcheck(this);\">";
        $(".selectedrow input:checked").each(function() {
            domBody += "<input type=\"hidden\" name=\"cvips \" value=\"" + $(this).val() +"\" />";
        }); 	
		domBody += 	"<table width=\"500\">" +
			"<tr><td width=\"40%\" align=right>Colleague's Email: </td><td width=\"60%\"><input name=\"colleagueemail\" type=\"text\" size=\"35\" /></td></tr>"+
			"<tr><td width=\"40%\" align=right>Colleague's Name: </td><td width=\"60%\"><input name=\"colleaguename\" type=\"text\" size=\"35\" /></td></tr>"+
			"<tr><td width=\"40%\" align=right>Your Name: </td><td width=\"60%\"><input name=\"sendername\" type=\"text\" size=\"35\" /></td></tr>"+
			"<tr><td width=\"40%\" align=right>Your Email: </td><td width=\"60%\"><input name=\"senderemail\" type=\"text\" size=\"35\" /></td></tr>"+
			"<tr><td width=\"40%\" align=right>Message:<br><i>Max: 200 characters</i>&nbsp;</td><td width=\"60%\"><textarea name=\"sendermessage\" cols=\"27\" rows=\"2\" maxlength=\"200\"></textarea></td></tr>"+
			"<tr><td></td><td><input type=\"submit\" name=\"Submit\" value=\"Send Abstract\" /></td></tr>"+
			"</table>"+
			"</form>";
	}
	/* creates the boxy		*/
	var dialog = new Boxy(domBody, options);
	allDialogs.push(dialog);
	return false;
}


function hightlightsecaction(checkid, sectioncheckboxid) {
	var ischecked = false;
	if($("#"+sectioncheckboxid+":checked").size() > 0)
		ischecked = true;
	$("#articleactionform input[name='"+checkid+"']").each(function() {
		if (ischecked)
		{
			$("#art"+$(this).val()+checkid.substring(6)).addClass("selectedrow");
			$("#art"+$(this).val()+checkid.substring(6)).removeClass("noselectrow");
			$(this).attr('checked', 'checked');
		}
		else
		{
			$("#art"+$(this).val()+checkid.substring(6)).removeClass("selectedrow");
			$("#art"+$(this).val()+checkid.substring(6)).addClass("noselectrow");
			$(this).removeAttr("checked");
		}
	});
	updateShowCounts();
}

function PrintPage(){
if (window.print) {  window.focus(); window.print(); }
else {  alert('This script does not work in your browser');  }
}

function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

function keyclosewindow(e) 
{
	if (e.keyCode==27) 
		close_focus_window();
}

/* Functions for the Volume Browsing Pane ************************* */
/* **********************************************************************************
    Functions that manage the calling and rendering of the volume-issue navigator
    Executes the render through a JQuery AJAX call.
*********************************************************************************** */

        function loadCurrentVolume(volume, currentvol, highvol, lowvol, loaddirection)
        {
            currentvol = Math.round(parseInt(currentvol));
            if ((currentvol <= highvol) && (currentvol >= lowvol))
            {
                $(".vol-loading").html("Loading...");
                var volumeloaction = urllocation+"/"+volume+"?view=isslist";
                //alert("volumeloaction: " + volumeloaction);
                $.get(volumeloaction, { volumestr : volume},
                  function(data){
                    // Removes the next or previous buttons if there is no next or previous volume
                    // ***************************************************************************
                    var nextvol = currentvol+1;
                    var prevvol = currentvol-1;
                    
                    $("#nextbutton").css("display","block");
                    $("#prevbutton").css("display","block");
                    
                    if (nextvol > parseInt(highvol))
                        $("#nextbutton").css("display","none");
                    if (currentvol <= 0)
                        $("#prevbutton").css("display","none");
                        
                    $('#slider').slider('option', 'value', currentvol);
                    $("a.ui-slider-handle").html(volume.replace("v",""));

                    if (loaddirection!=null && loaddirection!="")
                        $("#current").show("slide", { direction: loaddirection }, 1);

                    $("#current").html(data);
                    $(".vol-loading").html("");
                  });
            }
        }
        
/* Renders an Issue list on a pop-up box */

function loadIssue(urllocation, volnumber, idpaint, selectedissue, issuedateformat)
{
    //var volumeloaction = urllocation+"/"+volnumber+"?view=isslist";
    var volumeloaction = urllocation+"?volume="+volnumber + "&issueDateFormat=" + escape(issuedateformat);
    var idcontents = $(idpaint).html().trim();
    selectedissue = selectedissue.substring(10,selectedissue.indexOf(")"));
    //alert(volumeloaction + "&siss=" + selectedissue);
    if (idcontents == "")
    {
        $(idpaint).html("<p style=\"padding:20px; text-align:center;\">Loading...</p>")
        $(idpaint).load(volumeloaction + "&siss=" + selectedissue);
    }
    
    $(idpaint).toggle('fast');
    return false;
}

function MLloadIssue(urllocation, params)
{

	// Retrieve id from params map
	var idpaint = params["idpaint"]; 
    var idcontents = $(idpaint).html().trim();

    if (idcontents == "")
    {
        $(idpaint).html("<p style=\"padding:20px; text-align:center;\">Loading...</p>")
	    $.post(urllocation, params , function(data){
	 		if(data!=null) $(idpaint).html(data);
	 	} );
    }
    $(idpaint).toggle('fast');
    return false;
}

function showVolumeSection()
{
    var startvolrange = $("#volyears").attr("value");
    //alert( startvolrange);
    if($('#allvolumelist').is(':hidden')) {
        $('#allvolumelist').toggle('fast');
    }
    var divselector = "#show" + startvolrange;
    $(".volumeshowsections").hide();
    $(divselector).show();

    return false;
}


/* When the page first loads, it fires this function to highlight pre-selected articles */
function starthighlightSelections() { 
    $(".article_checkbox").each(function() {
        if ($(this).attr("checked") == true) {
            var namestrip = $(this).attr("name");
            namestrip = namestrip.substring(7,namestrip.length);
            var idtomanipulate = "#art" + $(this).attr("value") + "_" + namestrip;
            $(idtomanipulate).removeClass("noselectrow");
            $(idtomanipulate).addClass("selectedrow");
            $("#showhidearts").show(); // shows the show/hide options when something is selected
            
        }
    });
}

function ToggleInfoTip(showtext)
{
    $("#Action_tooltip").html(showtext);
    $("#Action_tooltip").toggle('drop');
}
function hideToggleInfoTip()
{
    $("#Action_tooltip").html("");
    $("#Action_tooltip").toggle('drop');
}
        
function swapUpDown(testid, repaintid)
{
    //alert(testid + " | hidden=" + $(testid).is(':hidden') + " | visible=" + $(testid).is(':visible'));
    if($(testid).is(':hidden')) {
        $(repaintid).css("background-image", "url('/img/dbt/uparrow.gif')");
        $(testid).show('fast');
    } else {
        $(repaintid).css("background-image", "url('/img/dbt/downarrow.gif')");
        $(testid).hide('fast');
    }
    
}

function openIssue(idpaint)
{
    $(idpaint).toggle('drop');
    return false;
}

/* For Issue Search */
function showIssueSearch()
{
    $("#issuesearchbox").toggle('fast');
    return false;
}
function filledout(idcheck)
{
      if($(idcheck).val() != "")
      {
          $("#volissq").val($(idcheck).val());
          return true;
      } else {
          return false;
      }
}

/* **********************************************************************************
Functions that executes the Jump to Page select box
*********************************************************************************** */
function jumpToPage(position,url)
{ 
	//alert(position);
	if(position == 'top'){
	  var pagenum = $('select.pagejump:first option:selected').val();
	}else if(position == 'bottom'){
	  var pagenum = $('select.pagejump:last option:selected').val();
	}
	window.location.href= url + "&page=" + pagenum;
}

