
// Script for calendar FINAL
jQuery(function () {
    jQuery("#datepicker").datepicker({
        minDate: "-20Y", maxDate: "+0D",
        dateFormat: 'yy-mm-dd',

        // Start submit form on date select
        onSelect: function (date) {
            var qstring = "http://books.usatoday.com/list/index?date=" + date;
            document.searchform.action = qstring;
            if (date != "") {
                jQuery("#searchform").submit();
            }
        }
        // End submit on date select

    });
});

//script to pass sort filter and date together
var holdQuery = window.location.search.substring(1);


function submitList(newQ) {
    //alert(holdQuery);
    var f2Position = newQ.indexOf("f=");
    var s2Position = newQ.indexOf("s=");

    var qPosition = holdQuery.indexOf("=");
    var s1Position = holdQuery.indexOf("s=");
    var f1Position = holdQuery.indexOf("f=");
    var d1Position = holdQuery.indexOf("date=");
    if (qPosition != -1) {
        if (d1Position != -1 && f1Position != -1) {
            var vars = holdQuery.split("=");
            var newVar = vars[1] + '=' + vars[2];
            var trimmed = newVar.replace(/&s/, '');
            var trimmed = newVar.replace(/&r/, '');
            //var trimmed = trimmed.replace(/&f/, '');
            if (f2Position != -1) {
                var varsF1 = trimmed.split("=");
                var trimmed = varsF1[0];
                var trimmed = trimmed.replace(/&f/, ''); 
                postURL = 'http://books.usatoday.com/list/index?date=' + trimmed;
            } else {
                postURL = 'http://books.usatoday.com/list/index?date=' + trimmed + '&';
            }

            postURL = 'http://books.usatoday.com/list/index?date=' + trimmed + '&';
        } else if (d1Position != -1 && f1Position == -1) {
            var vars = holdQuery.split("=");
            var trimmed = vars[1].replace(/&s/, '');
            var trimmed = vars[1].replace(/&r/, '');
            postURL = 'http://books.usatoday.com/list/index?date=' + trimmed + '&';
        } else if (d1Position == -1 && f1Position != -1) {
            var vars = holdQuery.split("=");
            var newVar = vars[0] + '=' + vars[1];
            var trimmed = newVar.replace(/&s/, '');
            var trimmed = newVar.replace(/&r/, '');
            if (f2Position != -1) {
                //alert('a');
                var trimmed = '';
                postURL = 'http://books.usatoday.com/list/index?' + trimmed;
            } else {
                //alert('b');
                postURL = 'http://books.usatoday.com/list/index?' + trimmed + '&';
            }
        } else if (s1Position != -1) {
            var vars = holdQuery.split("=");
            var newVar = vars[0] + '=' + vars[1];
            var trimmed = newVar.replace(/&s/, '');
            var trimmed = newVar.replace(/&r/, '');
            //alert('have s value' + holdQuery);
            if (s2Position != -1) {
                var trimmed = '';
                postURL = 'http://books.usatoday.com/list/index?' + trimmed;
            } else {
                postURL = 'http://books.usatoday.com/list/index?' + trimmed + '&';
            }    
        } else {
            //alert('no s');
            postURL = 'http://books.usatoday.com/list/index?';
        }
        //postURL = 'http://books.usatoday.com/list/index?' + holdQuery + '&';

    } else {
        postURL = 'http://books.usatoday.com/list/index?';
    }

    var finalURL = postURL + newQ;
    //alert(finalURL);
    document.genre.action = finalURL;
    document.forms["genre"].submit();
}

      // script for buy button
      /*function showRetail(rank) {
        if (document.forms.buyButton.on.value != "1") {
          jQuery("div.books_buy" + rank).css("display", "block");
          document.forms.buyButton.on.value = '1'
        } else {
          jQuery("div.books_buy" + rank).css("display", "none");
          document.forms.buyButton.on.value = ''
        }
      }*/
   
