//
//  Copyright Manta Technologies Inc., 2008.  All rights reserved.  
//
//
function init() {
    //document.domain = "mantatech.com";
    var headHTML = "";
        headHTML += "<TABLE cellSpacing=0 cellPadding=0 width='100%' border=0><TBODY>";
        headHTML += "  <TR vAlign=top>";
        headHTML += "    <TD height=80>";
        headHTML += "      <TABLE height=130 cellSpacing=0 cellPadding=0 width='100%' border=0><TBODY>";
        headHTML += "        <TR>";
        headHTML += "          <TD style='VERTICAL-ALIGN: middle'><A name=top></A><A";
        headHTML += "            title='Manta Home' href='http://www.mantatech.net/'><IMG";
        headHTML += "            style='MARGIN-LEFT: 32px' alt='Manta alt'";
        headHTML += "            src='../manta/images/mantalgo.png' border=0></IMG></A></TD>";
        headHTML += "          <TD vAlign=top align=right>";
        headHTML += "            <DIV class=toplinks>";
        headHTML += "            <div id='googlecart-widget'>Checkout</div>";
        headHTML += "            <BR/><BR/>";
        headHTML += "            <H1>YOUR IBM i TRAINING SOURCE";
        headHTML += "      </H1></DIV></TD></TR></TBODY></TABLE></TD></TR>";
        headHTML += "  <TR vAlign=center>";
        headHTML += "    <TD class=divider>";
        headHTML += "      <UL class=menulist id=listMenuRoot>";
        headHTML += "       <LI><A href='offerings.html'>What We Offer</A>";
        headHTML += "        <UL>";
        headHTML += "          <LI><A href='products.html'>Product Types</A></LI>";
        headHTML += "          <LI><A href='intro.html'>The Manta Advantage</A></LI>";
        headHTML += "          <LI><A href='feature.html'>Product Feature</A></LI>";
        headHTML += "          <LI><A href='newprod.html'>New Products</A></LI>";
        headHTML += "        </UL></LI>";
        headHTML += "        <LI><A href='course_menu.html'>Courses</A>";
        headHTML += "        <UL>";
        headHTML += "        <LI><A href='course_menu.html'>Course Descriptions</A>";
        headHTML += "             <UL>";
        headHTML += "	        <LI><A href='fast.html'>All Courses By Series</A></LI>";
        headHTML += "	        <LI><A href='subject.html'>Courses By Subject Area</A></LI>";
        headHTML += "	        <LI><A href='audience.html'>Courses By Audience</A></LI>";
        headHTML += "	        <LI><A href='series.html'>Available Series</A></LI>";
        headHTML += "	        <LI><A href='combos.html'>Combination Packs</A> </LI>";
        headHTML += "	        <LI><A href='certexams.html'>IBM Certification Exams</A> </LI>";
        headHTML += "             </UL>";
        headHTML += "        </LI>";
        headHTML += "        <LI><A href='liborg.html'>Library Organization</A> </LI>";
        headHTML += "        <LI><A href='libopts.html'>Options</A>";
        headHTML += "              <UL>";
        headHTML += "              <LI><A href='libopts.html'>Overview of Options</A></LI>";
        headHTML += "              <LI><A href='delivery.html'>Delivery Options</A> </LI>";
        headHTML += "              <LI><A href='users.html'>Multiple Users</A> </LI>";
        headHTML += "              <LI><A href='srgs.html'>Student Reference Guides</A></LI>";
        headHTML += "              <LI><A href='mantasa.html'>System Administration</A></LI>";
        headHTML += "              <LI><A href='maintplan.html'>Maintenance Plan</A></LI>";
        headHTML += "              </UL>";
        headHTML += "          <LI><A href='sample.html'>Take a Sample Session</A></LI>";
        headHTML += "          <LI><A href='catalog.html'>Download Course Catalog</A></LI>";
        headHTML += "        </UL></LI>";
        headHTML += "        <LI><A href='seminar_menu.html'>Seminars</A>";
        headHTML += "        <UL>";
        headHTML += "          <LI><A href='seminar_desc.html'>General Description</A></LI>";
        headHTML += "          <LI><A href='seminars.html'>Seminars By Topic</A></LI>";
        headHTML += "          <LI><A href='speakers.html'>Seminars By Instructor</A></LI>";
        headHTML += "          <LI><A href='seminar_pricing.html'>Seminar Pricing</A></LI>";
        headHTML += "          <LI><A href='seminar_sample.html'>Sample Seminar</A></LI>";
        headHTML += "          <LI><A href='catalog.html'>Download Seminar Catalog</A></LI>";
        headHTML += "        </UL></LI>";
        headHTML += "        <LI><A href='livetraining.html'>Classes</A>";       
        headHTML += "        <LI><A href='books.html'>Books</A></LI>";
        headHTML += "        <LI><A href='technical.html'>Technical</A>";
        headHTML += "        <UL>";
        headHTML += "          <LI><A href='sysreq.html'>System Requirements</A></LI>";
        headHTML += "          <LI><A href='techsup.html'>Technical Support</A> </LI>";
        headHTML += "        </UL></LI>";
        headHTML += "        <LI><A href='ordering.html'>Ordering</A>";
        headHTML += "        <UL>";
        headHTML += "          <LI><A href='orderopts.html'>Ordering Options</A></LI>";
        headHTML += "          <LI><A href='returns.html'>Money Back Guarantee</A></LI>";
        headHTML += "          <LI><A href='shipping.html'>Shipping and Taxes</A></LI>";
        headHTML += "          <LI><A href='faq.html'>Questions About Online Store</A></LI>";
        headHTML += "          <LI><A href='partners.html'>Orders Outside the U.S.</A></LI>";
        headHTML += "          <LI><A href='paying.html'>Paying an Invoice</A></LI>";         
        headHTML += "          <LI><A href='associate.html'>Become a Manta Sales Associate</A></LI>";
        headHTML += "        </UL></LI>";
        headHTML += "        <LI><A href='legal.html'>Legal</A>";
        headHTML += "        <UL>";
        headHTML += "          <LI><A href='agreement.html'>Terms and Conditions</A></LI>";
        headHTML += "          <LI><A href='license.html'>License For Local Delivery</A> </LI>";
        headHTML += "          <LI><A href='license2.html'>License For Internet Delivery</A></LI>";
        headHTML += "          <LI><A href='seminar_license.html'>Seminar License</A></LI>";
        headHTML += "          <LI><A href='download_licenses.html'>Download PDFs</A></LI>";
        headHTML += "          <LI><A href='ack.html'>Acknowledgements</A>";
        headHTML += "          </LI>";
        headHTML += "        </UL></LI>";
        headHTML += "        <LI><A target='_blank' href='http://www.mantatech.com/forums'>Forums</A></LI>";
        headHTML += "        <LI><A href='contacts.html'>Contacts</A>";
        headHTML += "        <UL>";
        headHTML += "          <LI><A href='comments.html'>Mail/Telephone/Fax</A></LI>";
        headHTML += "          <LI><A href='sales.html'>Sales</A></LI>";
        headHTML += "          <LI><A href='support.html'>Technical Support</A></LI>";
        headHTML += "          <LI><A href='customercare.html'>Customer Care</A></LI>";
        headHTML += "          <LI><A href='development.html'>Product Development</A></LI>";
        headHTML += "          <LI><A href='partners.html'>Partners</A></LI>";
        headHTML += "        </UL></LI>";
        headHTML += "        <LI><A href='search/search.php'>Search</A></LI>";
        headHTML += "      </UL>";
        headHTML += "		         </TD></TR>";
        headHTML += "		       <TR vAlign=top>";
        headHTML += "		         <TD class=nav2div><IMG height=23 alt='spacer gif'";
        headHTML += "		           src='../manta/images/blankgif.gif' width=32 border=0></IMG>";
        headHTML += "		       </TD></TR></TBODY></TABLE>";
        headHTML += addTaxTable();

   var headDiv = document.getElementById('header');
   headDiv.innerHTML = headHTML;

   var footHTML = "<FONT SIZE=1><p align=center>";
       footHTML += "&nbsp;&nbsp;";
       footHTML += "<A href='offerings.html'>What We Offer</A>";
       footHTML += "&nbsp;|&nbsp;";
       footHTML += "<A href='course_menu.html'>CD/Web Courses</A>";
       footHTML += "&nbsp;|&nbsp;";
       footHTML += "<A href='seminar_menu.html'>Seminars</A>";
       footHTML += "&nbsp;|&nbsp;";
       footHTML += "<A href='livetraining.html'>Classes</A>";
       footHTML += "&nbsp;|&nbsp;";
       footHTML += "<A href='books.html'>Books</A>";
       footHTML += "&nbsp;|&nbsp;";
       footHTML += "<A href='technical.html'>Technical</A>";
       footHTML += "&nbsp;|&nbsp;";
       footHTML += "<A href='ordering.html'>Ordering</A>";
       footHTML += "&nbsp;|&nbsp;";
       footHTML += "<A href='legal.html'>Legal</A>";
       footHTML += "&nbsp;|&nbsp;";
       footHTML += "<A href='http://www.mantatech.com/forums'>Forums</A>";
       footHTML += "&nbsp;|&nbsp;";
       footHTML += "<A href='contacts.html'>Contacts</A>";
       footHTML += "&nbsp;&nbsp;";       
       footHTML += "<br/><br/>&#169;  Copyright 2010 Manta Technologies. All Rights Reserved.</p>";
       //footHTML += addTaxTable();
   var footDiv = document.getElementById('footer');
   footDiv.innerHTML = footHTML;
}

//
// Google checkout exit
//

var alreadyPitchedSA = false;
   
function googlecartOnCheckoutClick(items,form) {
   //Called when customer clicks the Checkout button
   //Offers SA for MantaNow and sets price
   //Also offers overnight shipping
   //Verifies each SRG or handout has a product
   var soldLibrary = false;
   var soldMantaNow = false;
   var combosSold = 0;
   var seriesSold = 0;
   var coursesSold = 0;
   var maxUsers = 1;
   var saPrice = 0;
   var titles = new Array();
   var userField;

   var booksShipped = 0;
 
   //The follow flag prevents infinite recursion as checking-out re-calls this exit.
   if (alreadyPitchedSA) {
      //alert(document.cookie);
      return true;
   }
   alreadyPitchedSA = true;
         
   //Extract titles from shopping cart
   //Along the way, determine maximum number of users selected.
   for (var i=0; i < items.length; i++) {
      titles[i] = items[i].getTitle().toUpperCase();
      userField = "" + items[i].getCustomAttribute('users');
      userField = userField.toUpperCase();
      
      if (contains(userField,"MANTANOW")) {
         soldMantaNow = true;
      }

      if ((contains(userField,"5 USERS")) && (maxUsers < 5)) {
         maxUsers = 5;
      }
      if ((contains(userField,"10 USERS")) && (maxUsers < 10)) {
         maxUsers = 10;
      }
      
      if ((contains(titles[i],"LIBRARY")) && (!contains(titles[i],"SRGSET"))) {
         soldLibrary = true;
      } else {
         if ((contains(titles[i],"PACK")) && (!contains(titles[i],"H)"))) {
            combosSold += 1;
         } else {
            if (contains(titles[i],"00)")) {
               seriesSold += 1;
            } else {
               if ((!contains(titles[i],"H)")) && (!contains(titles[i],"SRGSET"))) {
                  coursesSold += 1;
               }
            }
         }
      }      
   }
   
   updateShippingPrices();
   
   //alert("maxUsers=" + maxUsers
   //     + "\nLibrary?" + soldLibrary  
   //     + "\n" + combosSold + " combos"
   //     + "\n" + seriesSold + " series"
   //     + "\n" + coursesSold + " courses or seminars");   

   if (soldMantaNow) {
      saPrice = setSAPrice(maxUsers, soldLibrary, combosSold, seriesSold, coursesSold);
      var item = makeItemNode("sanode","Student Administration",saPrice, "MantaNow " + maxUsers +"-user level");
      displaySA(saPrice);
   } else {
      nobuySA();
   }
}

function makeItemNode(nodename,iname,iprice,users) {
   //This function makes a Student Administration <div> tag for Google
      
      var item      = document.createElement("div");
      var itemName  = document.createElement("div");
      var unitPrice = document.createElement("div");
      var userAttr  = document.createElement("div");
      item.setAttribute("class","product");
      item.setAttribute("className","product");
      item.setAttribute("id",nodename);
      
      itemName.setAttribute("class","product-title");
      itemName.setAttribute("className","product-title");
      itemName.setAttribute("style", "visibility:hidden");
      itemName.appendChild(document.createTextNode(iname));
      itemName.style.cssText = "visibility:hidden";
      
      unitPrice.setAttribute("currency", "USD");
      unitPrice.setAttribute("class","product-price");
      unitPrice.setAttribute("className","product-price");
      unitPrice.setAttribute("style", "visibility:hidden");
      unitPrice.appendChild(document.createTextNode(iprice));
      unitPrice.style.cssText = "visibility:hidden";

      item.appendChild(itemName);
      item.appendChild(unitPrice);
 
      if (!isNull(users)) {  
         userAttr.setAttribute("class","product-attr-users");
         userAttr.setAttribute("className","product-attr-users");
         userAttr.setAttribute("style", "visibility:hidden");
         userAttr.appendChild(document.createTextNode(users));
         userAttr.style.cssText = "visibility:hidden";
         item.appendChild(userAttr);
      }
      
      var mydiv = document.getElementById("headline");
      mydiv.appendChild(item);
}

function displaySA(price) {
   var popDiv = document.getElementById("popup");
   var pageHTML = '<div class="mypop">';
       pageHTML += '<p id="TOP" align="center">';
       pageHTML +=  '<img hspace="10" src="images/3d_stuadmin.gif" width="300"/></p>';
       pageHTML += '<table style="width: 90%" align="center"><tr><td>';
       pageHTML += '<p>Manta&#39;s Student Administration software allows a training administrator ';
       pageHTML += 'to monitor and control the use of the Manta <i>IBM i Training Library</i> ';
       pageHTML += 'courseware. This software lets you: </p><ul>';
       pageHTML += '<li>Grant course access only to registered students</li>';
       pageHTML += '<li>Create custom student menus</li>';
       pageHTML += '<li>Print reports detailing student course usage </li>';
       pageHTML += '<li>Print reports detailing students Competency Exam results</li>';
       pageHTML += '<li>Check your license status</li></ul>';
       pageHTML += '<p>For the products and user levels you have selected, Student Administration ';
       pageHTML += 'will cost an additional ' + price + '.';
       pageHTML += '</p><p>Do you wish to add it to your cart?</p><br />';
       pageHTML += '<div id="SA" class="product product-digital">';
       pageHTML += '<span class="product-title"><i>Student Administration</i></span> ';
       pageHTML += '<span class="product-price">: ' + price + '</span><br/><br/>';
       pageHTML += '<br/><img alt="No, thanks" src="images/nothanks.png" onClick="nobuySA()"/>&nbsp;&nbsp;';
       pageHTML += '<img alt="Add to cart" src="images/AddToCart.png" onClick="buySA()"/>';
       pageHTML += '</div></td></tr></table><br/></div>'
   var mynode = document.getElementById("headline");
   popDiv.style.top  = 200;
   popDiv.style.left = mynode.style.left;      
   popDiv.innerHTML = pageHTML;
}

function buySA() {
     //Called if the customer bought Student Administration
     closePopup();
     googlecart.addItem(googlecart.makeItemFromNode(document.getElementById("sanode")));
     //displayLastScreen();
     fixTaxes();
     googlecart.checkout();
}

function nobuySA() {
     //Called if the customer chose not to buy Student Administration
     closePopup();
     //displayLastScreen();
     fixTaxes();
     googlecart.checkout();
}

function fixTaxes() {
   googlecart.saveCartAndRefreshWidget();
   var items = googlecart.getItems(); 
   var userField;
   for (var i=0; i < items.length; i++) {
      userField = "" + items[i].getCustomAttribute('users');
      userField = userField.toUpperCase();
      if (contains(userField,"MANTANOW")) {
         addTaxExempt(i);
      }
   }
}

function updateShippingPrices() {  
   var standardShipNode = document.getElementById("stdShip");
   //alert("standard was " +  standardShipNode.value);
   standardShipNode.value = standardShipping() + ".00";
   //alert("standard changed to " +  standardShipNode.value);
   var overnightShipNode = document.getElementById("ovrShip");
   //alert("overnight was " +  overnightShipNode.value);
   overnightShipNode.value = overnightPrice() + ".00";
   //alert("overnight changed to " +  overnightShipNode.value);
}

function standardShipping() {
   //This function computed the shipping rate from the cart
   var items = googlecart.getItems();
   var shippingFirst = 0;
   var shippingRest = 0;
   var itemFirst;
   var itemUnit;
   var itemQuan;
   
   for (var i=0; i < items.length; i++) {
      itemFirst = items[i].getShippingFirst();
      if (isNull(itemFirst)) itemFirst = 0;
      itemUnit  = items[i].getShippingPerUnit();      
      if (isNull(itemUnit)) itemUnit = 0;
      itemQuan = items[i].getQuantity();
     if (isNull(itemQuan)) itemQuan = 0;
     
      //alert("next " + itemFirst + "  " + itemUnit)     
      if (shippingFirst < itemFirst) {
         shippingFirst = itemFirst;
         if (items[i].getQuantity() > 1) {
         shippingRest = shippingRest + ( itemUnit * (itemQuan-1));
         }
      } else {
         shippingRest = shippingRest + (itemUnit * itemQuan);
      }
   }
   return shippingFirst + shippingRest;
}

function overnightPrice() {
    var s = standardShipping();
    if (s > 0) {
       return 25 + ((s-10)*2);
    } else {
       return 0;
    }
}

function setSAPrice(maxUsers, soldLibrary, combosSold, seriesSold, coursesSold) {
   //Returns an integer representing the Student Administration price
   //  based on the cart contents.
   var basePrice = 0;
   if(soldLibrary) {
      basePrice = 200;
   } else {
      if ((combosSold > 1) || ((combosSold == 1) && ((seriesSold + coursesSold) >0))) {
          basePrice = 200;
      } else {  
            if (combosSold == 1) {
                basePrice = 150;
            } else { 
                if ((seriesSold > 1) || ((seriesSold == 1) && (coursesSold > 0))) {
	             basePrice = 150;
                } else { 
                    if (seriesSold == 1) {
			basePrice = 100;
                    } else { 
                       if (coursesSold > 1) {
	                  basePrice = 100;
                       } else { 
                          if (seriesSold == 1) {
		             basePrice = 50;
                          }
                       }
                    }
                }
            }
        }
    }
       
    if (maxUsers == 1) {
         return dollarize(basePrice);
    } else {
       if (maxUsers == 5) {
         return dollarize(1.5 * basePrice);
       } else {
         return dollarize(2 * basePrice);
       }
   } 
}
                       
function contains(x,y)  {
    //returns true if string x contains string y
    var index1 = x.indexOf(y);
    if (index1 > -1) {
       return true;
    } else {
       return false;
    }
}

function isNull(x) {
//This utility function returns true if string x is null, undefined, or empty.
   if ((x == 'undefined') || (x == "") || (x == null) || (x == "null")) {
       return true;
   } else {
       return false;   
   }
}
 function dollarize(n) {
 //This function converts an integer to the form $xx,xxx.00.
    var str = "" + n;
    var len = str.length;
    if (len > 3) {
       var commapsn = len - 3;
       return "$" + str.substring(0,commapsn) + "," + str.substring(commapsn) + ".00";
    } else {
       return "$" + str + ".00";
    }
 }
 function closePopup() {
    //Receives control from the pop-up Exit button to close the window.
    var popDiv = document.getElementById("popup");
    popDiv.innerHTML = "";
}

function addTaxTable() {
   var outHTML =  '<form id="googlecart-checkout-config">';
       outHTML += '<input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.shipping-methods.flat-rate-shipping-1.shipping-restrictions.allowed-areas.world-area-1" value="true">'
       outHTML += '<input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.tax-tables.default-tax-table.tax-rules.default-tax-rule-1.shipping-taxed" value="false"/>'
       outHTML += '<input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.tax-tables.default-tax-table.tax-rules.default-tax-rule-1.rate" value="0.0620"/>'
       outHTML += '<input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.tax-tables.default-tax-table.tax-rules.default-tax-rule-1.tax-areas.us-state-area-1.state" value="IL"/>'
       outHTML += '<input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.tax-tables.default-tax-table.tax-rules.default-tax-rule-2.shipping-taxed" value="false"/>'
       outHTML += '<input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.tax-tables.default-tax-table.tax-rules.default-tax-rule-2.rate" value="0.0290"/>'
       outHTML += '<input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.tax-tables.default-tax-table.tax-rules.default-tax-rule-2.tax-areas.us-state-area-2.state" value="CO"/>'
       outHTML += '<input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.tax-tables.alternate-tax-tables.alternate-tax-table-1.name" value="tax_exempt"/>'
       outHTML += '<input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.tax-tables.alternate-tax-tables.alternate-tax-table-1.standalone" value="true"/>'
       outHTML += '<input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.tax-tables.alternate-tax-tables.alternate-tax-table-1.alternate-tax-rules.alternate-tax-rule.rate" value="0.00"/>'
       outHTML += '<input type="hidden" name="ship_method_name_1" value="Priority Mail (2-day)"/>';
       outHTML += '<input id="stdShip" type="hidden" name="ship_method_price_1" value="10.00"/>';
       outHTML += '<input type="hidden" name="ship_method_currency_1" value="USD"/>';
       outHTML += '<input type="hidden" name="ship_method_name_2" value="Overnight"/>';
       outHTML += '<input id="ovrShip" type="hidden" name="ship_method_price_2" value="25.00"/>';
       outHTML += '<input type="hidden" name="ship_method_currency_2" value="USD"/>';
       outHTML += '</form>';
   return outHTML;
}

function addTaxExempt(n) {
   n ++;
   var ctlForm = document.getElementById("googlecart-checkout-config");
    if (document.uniqueID) {
        var input = document.createElement('<input type=hidden name=shopping-cart.items.item-' + n + '.tax-table-selector value=tax-exempt/>');
    }
    else {   
      var input = document.createElement("input");
      input.setAttribute("type","hidden");
      input.setAttribute("zname","shopping-cart.items.item-" + n + ".tax-table-selector");
      input.setAttribute("value","tax_exempt");
    }
    ctlForm.appendChild(input);
}


