/*****************************************************************************/
/*****  Asphodel Press Functions					******/
/*****************************************************************************/

    var booklist = new Array();
    var numFormats = 4;
    noEconomy = false;



function formatInfo (nType, nPrice, nProductID) {
    this.type = nType;
    this.price = nPrice;
    this.productID = nProductID;
    this.fPrice = function () { return price(this.price)};
}
 
function category(n) {
	switch(n) {
	case 1: return "Devotionals";
	case 5: return "Poetry Devotionals";
	case 3: return "Prayer Books";
	case 2: return "Specialty Books";
	case 4: return "NT Shamanism";
	default: return "Other Books";
	}
}

function initFormats(book, n) {
    book.format = new Array();
    for (i=0;i<=n+1;i++){ book.format[i] = new formatInfo("UNAVAILABLE",0,"XXXXX"+i);}
}

function book() {  //(nTitle, nSubtitle, nAuthor, nBookID, nLuluProjectID, nPages, nSize, nCategory) {
/*    this.title = nTitle;
    this.subtitle = nSubtitle;
    this.author = nAuthor;
    this.bookID = nBookID;
    this.luluProjectID = nLuluProjectID;
    this.pages = nPages;
    this.size = nSize;
    this.category = nCategory;
    this.format = new Array();*/
    
    // ONLY init Formats
    initFormats(this, numFormats)
    this.fTitle = function() { if (this.shortTitle) { return this.shortTitle;} else {return this.title;}}
}


function price(n) {
   if (!isNaN(n)) {return "$" + n.toFixed(2);} else {return n;}
}


/*****************************************************************************/
/*****  Inventory Display						******/
/*****************************************************************************/



function displayThisBook (book) {

	document.write("<div class='book'><a name=" + book.bookID + "></a>");
	
	if (book.newRelease) {	document.write("<h1 class='newtitle'>" + book.title) }
	else {	document.write("<h1 class='title'>" + book.title) }
	
	if ((book.title.length+book.subtitle.length)>57) {
		document.write("<br><span class='subtitle'>" + book.subtitle + " </span></h1>");
	} else {
		document.write(" <span class='subtitle'>" + book.subtitle + " </span></h1>");
	}
	document.write("<p class='byline'>by " + book.author + " </p>");
	if (book.luluProjectID != "") {document.write("<a href=http://www.lulu.com/content/" + book.luluProjectID + " >");}
	else {document.write("<a>");}
	document.write("<img class='cover' src='covers/" + book.bookID + ".jpg' alt='" + book.title + " cover'></a>");
	document.write("<p class='description'>" + book.description + " ");
	if (book.pages>0) {document.write(" (" + book.pages + " pages, " + book.size + ") ");}

	if (book.luluProjectID == "") {	document.write("</p>");}
	else {

 		document.write("<!--a href='http://www.lulu.com/browse/preview.php?fCID=" + book.luluProjectID + " '>See a Preview</a></p-->");

		document.write("<form action='https://www.lulu.com/commerce/index.php' method='POST' target='_self'>");

		    	document.write("<input name='fCID' value='" + book.luluProjectID + "' type='hidden'>");
    			document.write("<input name='fError' value='' type='hidden'>");
		    	//document.write("<input name='fBuyProduct' value='' type='hidden'>");
    			//document.write("<input name='fLocale' value='' type='hidden'>");
    			//document.write("<input name='fCOUID' value='' type='hidden'>");
    			//document.write("<input name='fCurrencyCode' value='' type='hidden'>");
			document.write("<input type='hidden' name='fReturnUrl' value='http://www.asphodelpress.com'>");

    		document.write("<ul class='bookoptions'>");

		typeName = "fBuyProduct";
		
    		for (x=1; x<=numFormats; x++) {
  		  if (book.format[x].price != 0) {
    			document.write("<li>  ");
    			if (!book.oneFormat) {
    	  			document.write("<input type=radio name='fBuyProduct[]' value='" + book.format[x].productID + "' ");
    	  			if (x==1) {document.write("checked='checked'");}
    	  			document.write(">");
    			} else { document.write("<input type=hidden name='fBuyProduct[]' value='" + book.format[x].productID + "'>");}
    			document.write(" <b>" + book.format[x].fPrice() + "</b> " + book.format[x].type + "</li>");
  	  	  }
  		}

		document.write("</ul>");
		document.write("<div class='buybook'><input type='image' class='button' src='template/LuluButton.gif'></div>");
		
		document.write("</form>");

    		document.write("<div class='clear'></div>");
   	}


    	if (book.format[0].price>0){ //Available direct

	    if (book.luluProjectID == "") { // Only direct, not Lulu
	      document.write("<div class='apdirecttext'><h3><b>Direct Shipment: " + book.format[0].fPrice() + "</b> " + book.format[0].type + "</h3></div>");
	      document.write("<div class='apdirectbutton'><a href='direct.html'><img class='button' src='template/APButton.gif' alt='Buy Direct'></a></div>");
	    }
	    else {
	      document.write("<div class='apdirect'><div class='apdirecttext'><h3>Direct Shipment Discount Available!</h3>");
	      document.write("<h2>Our price: " + book.format[0].fPrice() + "</h2></div>");
	      document.write("<div class='apdirectbutton'><a href='direct.html'><img class='button' src='template/APButton.gif' alt='Buy Direct'></a></div></div><!--apdirect-->");
	    }

    }

     if (book.note) {
     	document.write("<div class='clear'></div>");
        document.write("<p class=note>" + book.note + "</p>");
     }

    document.write("<div class='clear'></div>");
    document.write("</div><!--book-->");
}

function listBooks(c) {
    document.write("<li><h2>" + category(c)+ "</h2><ul>");
    for (i=1; i<=numBooks; i++)  { 
      if (booklist[i].available && booklist[i].category==c)
        { document.write("<li><a href=#" + booklist[i].bookID + ">"+ booklist[i].fTitle() + "</a></li>");}
    }
    document.write("</ul></li>");
}


function displayBooks(category) {
    for (i=1; i<=numBooks; i++)  { 
      if (booklist[i].available && booklist[i].category==category)
        { displayThisBook(booklist[i]);}
    }
}

function displayNewReleases() {
    for (i=1; i<=numBooks; i++)  { 
      if (booklist[i].available && booklist[i].newRelease)
        { displayThisBook(booklist[i]);}
    }
}


/*****************************************************************************/
/*****  DIRECT ORDERS							******/
/*****************************************************************************/

function displayDirectOrder() {

	n=0;	
    	for (i=1; i<=numBooks; i++)  { 
      	    if (booklist[i].available && booklist[i].availableDirect){ 
      	        n++;
        	document.write("<tr class=d"+ n%2 + "><td>" + booklist[i].fTitle() + "</td>");
	   	document.write("<td>" + booklist[i].format[0].fPrice() + "</td>");
	   	document.write("<input type='hidden' name='book" + n + "index' value=" + i + ">");
	   	
	   	if (!isNaN(booklist[i].qty)) {document.write("<input type='hidden' name='book" + n + "items' value=" + booklist[i].qty + ">");
	   	} else { document.write("<input type='hidden' name='book" + n + "items' value=1>");   	}
	   	
	   	document.write("<input type='hidden' name='book" + n + "price' value=" + booklist[i].format[0].price + ">");
	    	document.write("<td><input type='text' name='book" + n + "qty' size=3 value=0 onChange='updateTotal(this.form, " + n +");'></td></tr>");
            }
    	}
	return n;
}

function updateTotal(f,n){
   totPrice = 0;
   qty = 0;
   for (x=1;x<=numDirect;x++){
     totPrice += eval("f.book" + x + "price").value * eval("f.book" + x + "qty").value;
     if (eval("f.book" + x + "items").value > 1) {
     	if (eval("f.book" + x + "qty").value >0) {noEconomy=true; } 	else {noEconomy=false;} }
     qty+=eval("f.book" + x + "qty").value * eval("f.book" + x + "items").value;
   };
   f.subtot.value = price(totPrice);
   f.qty.value = qty;
   f.shipCost.value = price(shippingCost(f.qty.value, f.shipping.value, noEconomy));
   f.total.value = price(f.subtot.value.substring(1,9)*1 + f.shipCost.value.substring(1,9)*1);
   if (qty>10) {resetQty(f,n,qty, 10)};
};

function resetQty(f,n,q, max) {
  eval("f.book" + n + "qty").value -= q-max;
  updateTotal(f,n);
  alert("For orders over " + max + " books, please contact us for a wholesale pricing.");
}

function shippingCost(n,method, noEconomy) {
  if (noEconomy && method=="mm") {alert ("This order does not qualify for Economy Shipping."); method = "pm";}
  switch (method) {
     case "mm": {
        n=n*1;
        switch (n) { //1.5-2lbs each?
         case 1:return 3;
         case 2:return 3.5;
         case 3:return 4.5;
         case 4:return 5;
         case 5:return 6;
         case 6:return 6.5;
         case 7:return 7.5;
         case 8:return 8.5;
         case 9:return 9.5;
         case 10:return 10;
         default: return 0;
       }}
    case "pm": {
            n=n*1;
            switch (n) { 
             case 1:return 5;
             case 2:return 5;
             case 3:return 11;
             case 4:return 11;
             case 5:return 11;
             case 6:return 11;
             case 7:return 14;
             case 8:return 14;
             case 9:return 14;
             case 10:return 14;
             default: return 0;
	}}
    case "pic": {
            n=n*1;
            switch (n) {
             case 1:return 11;
             case 2:return 11;
             case 3:return 26;
             case 4:return 26;
             case 5:return 26;
             case 6:return 26;
             case 7:return 33;
             case 8:return 33;
             case 9:return 33;
             case 10:return 33;
             default: return 0;
	}}

    case "pix": {
            n=n*1;
            switch (n) { 
             case 1:return 13;
             case 2:return 13;
             case 3:return 42;
             case 4:return 42;
             case 5:return 42;
             case 6:return 42;
             case 7:return 54;
             case 8:return 54;
             case 9:return 54;
             case 10:return 54;
             default: return 0;
	}}
;
    default: return 0;
}}

function shippingMethod(m) {
   switch (m) {
     case "mm": return "Economy Shipping (USA)"
    case "pm": return "Standard Shippping (USA)";
    case "pic": return "Standard Shipping (Canada/Mexico)";
    case "pix": return "Standard Shipping (International)";
    default: return "Shipping Error";
}}


function confirmOrder(f,loc)
{ datastring = f.shipping.value + "_"+ 1*f.shipCost.value.substring(1,9);
   for (x=1;x<=numDirect;x++){
	if (eval("f.book" + x + "qty").value>0){ 
		datastring+= "_" + eval("f.book" + x + "index").value + "_" + eval("f.book" + x + "qty").value;
	}
   }  window.location = loc + "?" + datastring;
}

function parseData(){
  var query = window.location.search;
  // Skip the leading ?, which should always be there.
  if (query.substring(0, 1) == '?') { query = query.substring(1);}
  var data = query.split('_');
  return data;
}



function displayShippingTable(max){

   document.write("<table><tr class=heading><td># Books</td><td>USA Economy</td><td>USA Standard</td><td>Canada/Mexico</td><td>Other Countries</td></tr>");
   for (i=1; i<=max; i++) {
      document.write("<tr><td  align=center>" + i + "</td><td align=center>");
      document.write(price(shippingCost(i, "mm")) + "</td><td align=center>");
      document.write(price(shippingCost(i, "pm")) + "</td><td align=center>");
      document.write(price(shippingCost(i, "pic")) + "</td><td align=center>");;
      document.write(price(shippingCost(i, "pix")) + "</td></tr>");
   }  
   document.write("<tr><td colspan=5>For orders over " + max);
   document.write(" books, please contact us for a wholesale discount.</td></tr></table>");
}

function displayPrintOrder() {
    	for (i=1; i<=numBooks; i++)  { 
      	    if (booklist[i].available && booklist[i].availableDirect){ 
        	document.write("<tr><td>" + booklist[i].title + "</td>");
	   	document.write("<td>" + booklist[i].format[0].fPrice() + "</td>");
	   	document.write("<td>&times;</td>");
	    	document.write("<td>&nbsp;</td>");
	    	document.write("<td>=</td>");
	    	document.write("<td>&nbsp;</td></tr>");
            }
    	}
}


/*****************************************************************************/
/*****  Copyright Footer						******/
/*****************************************************************************/

function DisplayCopyright(y,a) {
 if (a != undefined) {	rightsowner = a; } else {rightsowner = "Asphodel Press";}
 var modiDate = new Date(document.lastModified);
 var monthName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
 if (y != undefined) {
   document.write("&copy;" + y + " " + rightsowner + ". ");
   document.write("Last modified " + monthName[modiDate.getMonth()] + " ");
   document.write(modiDate.getDate() + ", " + modiDate.getFullYear() + ". ");
 } else {
   if (Date.parse(document.lastModified) != 0) {
	 document.write("&copy;" + modiDate.getFullYear() + " " + rightsowner + ". ");
   	 document.write("Last modified " + monthName[modiDate.getMonth()] + " ");
     document.write(modiDate.getDate() + ", " + modiDate.getFullYear() + ". ");
   }
 }
}
