﻿// JScript File
// Abacalab, Inc. Order Form Events and Validation Code
// Copyright 2007-2009, Abacalab, Inc.

// Globals
var blankSpace = String.fromCharCode( 160 ); //nbsp
var blank = "";
var order_method;
var base_price;
var discount;
var discount_price;
var item_name;
var item_description;
var order_delivery;
var base_item_id;
var full_item_id;
var academic_discount = false;
var promo_codes = blank;
var real_name = blank;
var email = blank;
var phone = blank;
var emailRegEx = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
var telnrRegEx = /^\+?[0-9 ()-]+[0-9]$/  ;
var base_quote_redirect = "http://www.abacalab.com/order_postquote.html";
var quote_redirect;

// Update the order information
function activateScripting()
{
    if( document.getElementById )
    {
        document.getElementById('product_complete222').disabled=false;    
//        document.getElementById('product_wireless212').disabled=false;    
        document.getElementById('product_software222').disabled=false;    
        document.getElementById('product_upgrade12').disabled=false;    
        document.getElementById('method_web').disabled=false;    
        document.getElementById('method_quote').disabled=false;    
        document.getElementById('method_eval').disabled=false;    
        document.getElementById('method_phone').disabled=false;    
//        document.getElementById('method_distrib').disabled=false;    
        document.getElementById('realname').disabled=false;    
        document.getElementById('academicdiscount').disabled=false;    
        document.getElementById('email').disabled=false;    
        document.getElementById('promocode').disabled=false;    
        document.getElementById('phone').disabled=false;    
        clearInputs();
        selectOrderMethod( "web" );
        if( 0 > document.location.search.indexOf( "order_postquote.html" ) )
        {
            selectComplete();
            retrievePromo();
        }
        else
        {
            retrieveQuote();
        }
    }
}

function clearInputs()
{
    document.getElementById('realname').value=blank;    
    document.getElementById('academicdiscount').checked=false;    
    document.getElementById('email').value=blank;    
    document.getElementById('promocode').value=blank;    
    document.getElementById('phone').value=blank;   
}

function selectReferredProduct( referred )
{
    document.getElementById('product_complete222').checked=false;
    if( "complete" == referred )
    {
        document.getElementById( 'product_complete222' ).checked=true;
        selectComplete();
    }
    else if( "software" == referred )
    {
        document.getElementById( 'product_software222' ).checked=true;
        selectSoftwareOnly();
    }
    else if( "upgrade" == referred )
    {
        document.getElementById( 'product_upgrade12' ).checked=true;
        selectUpgrade();
    }
    else
    {
        clearInputs();
        selectComplete();
    }
}


function mouseOverPlaceOrder()
{
    if( checkRequiredFields() )
    {
        document.getElementById('submit').src ="images/b_place_order_r.gif" 
    }
}
    
function mouseOutPlaceOrder() { document.getElementById('submit').src ="images/b_place_order_w.gif" }

function checkRequiredFields()
{
    requiredFieldsOk = false;
    
    if( "web order" != order_method )
    {
        nameOK = checkName();
        
        if( "eval" == order_method || "quote" == order_method )
        {
            requiredFieldsOk = ( checkEmail() && nameOK ) ;
        }
        else if ( "phone" == order_method )
        {
            requiredFieldsOk = ( checkPhone() && nameOK ) ;
        }
    }
    else
    {
        requiredFieldsOk = true;
    }
    
    return requiredFieldsOk;
}

function selectComplete()
{
    item_name="Chemistry Labacus(R) 2.2 Std Wireless";
    base_item_id="510-5105-222";
    item_description = "500+ MHz Windows Mobile handheld computer with Wi-Fi, " +
        "Chemistry Labacus software with one year of free software " +
        "upgrades, standard PDA application software, power supply, " +
        "cables, desktop software installation CDs and " +
        "instruction manual.";
    setUpDelivery( "ship" );
    doPricing();
    showItemDescription();
}

function selectWireless()
{
//    item_name="Chemistry Labacus(R) 2.1 Wireless";
//    base_item_id="510-5104-212";
//    item_description="520 MHz Windows Mobile handheld computer with Wi-Fi, " +
//        "Chemistry Labacus software with one year of software upgrades, " +
//        "standard PDA application software, power supply, cables, desktop " +
//        "software installation CDs and instruction manual.";
//    setUpDelivery( "ship" );
//    doPricing();
//    showItemDescription();
}

function selectSoftwareOnly()
{
    item_name="Chemistry Labacus(R) 2.2 Software-Only Package";
    base_item_id="910-5100-222";
    item_description="Downloadable software with pdf manual and license key; " +
        "includes one year of software upgrades.  You must have a compatible " +
        "mobile device with the Windows Mobile OS.";
    setUpDelivery( "download" );
    doPricing();
    showItemDescription();
}

function selectUpgrade()
{
    item_name="Chemistry Labacus(R) Upgrade Version 1.x to Version 2.x";
    base_item_id="610-1000-200";
    setUpDelivery( "none" );
    item_description="Extend your software upgrades for Chemistry Labacus " +
        "for one additional year.";
    doPricing();
    showItemDescription();
}

function showItemDescription()
{
    document.getElementById( 'product_header' ).firstChild.nodeValue = item_name;
    document.getElementById( 'product_description' ).firstChild.nodeValue = 
        item_description;
}

function setUpDelivery( deliveryMethod )
{
    order_delivery = deliveryMethod;
    if( "eval" == order_method )
    {
        selectOrderMethod( "eval" );
    }
}

function selectOrderMethod( method )
{
    if( "web order" == method )
    {
        setUpOrderMethod( method, "https://www.paypal.com/cgi-bin/webscr", "http://www.abacalab.com/index.html",
            "Web Order (error)", "support@abacalab.com", false, false, false,
            "Order a Labacus online with a credit card or PayPal. When you click " +
            "the 'Place Order' button, you will be transferred to the PayPal site " +
            "for secure online order processing." );
    }
    else if ( "quote" == method )
    {
        setUpOrderMethod( method, "/cgi-sys/formmail.pl", quote_redirect,
            "Request For Quote 2.2", "sales@abacalab.com", true, true, false,
            "Get a quote for a Labacus, including discounts.  An automated " +
            "quote will be generated immediately and our sales department will " +
            "also send you a formal quote by e-mail." );
    }
    else if ( "eval" == method )
    {
        if( "download" == order_delivery )
        {
            // Make this equivalent to demo page results.
            evalRedirect = "http://www.abacalab.com/evalthanks.html";
            evalRecipient = "demoauth2@abacalab.com";
        }
        else if ( "ship" == order_delivery )
        {
            // New page acknowledging hardware eval request
            evalRedirect = "http://www.abacalab.com/evalhwthanks.html";
            evalRecipient = "sales@abacalab.com";
        }
        else
        {
            // Error condition
            evalRedirect = "http://www.abacalab.com/index.html";
            evalRecipient = "support@abacalab.com";
        }
        
        setUpOrderMethod( method, "/cgi-sys/formmail.pl", evalRedirect,
            "Request For Evaluation 2.2", evalRecipient, true, true, false,
            "Request a two-week free evaluation of the Chemistry Labacus. " +
            "The Abacalab sales department will contact you to arrange the " +
            "details and get your shipping information.  If you already have " +
            "a Windows Mobile device, select the 'Software' Labacus option " +
            "for immediate download of an evaluation version on the Web." );
    }
    else if ( "phone" == method )
    {
        setUpOrderMethod( method, "/cgi-sys/formmail.pl", "http://www.abacalab.com/phonethanks.html",
            "Request For Phone Call 2.2", "sales@abacalab.com", true, false, true,
            "Request a phone call from Abacalab sales staff to answer any " +
            "questions you may have or to take your order over the phone. " +
            "You can also call us toll-free in the U.S. at 877-522-2252 or " +
            "internationally at +1-302-494-6507. " );
    }
    else
    {
        selectOrderMethod( "web order" );
    }
}

function setUpOrderMethod( setUpMethod, setUpAction, setUpRedirect, emailSubject, emailRecipient, 
    nameRequired, emailRequired, phoneRequired, methodDetails )
{
    order_method = setUpMethod;
    
    document.forms.process_order.action = setUpAction;
    document.getElementById( 'redirect' ).value = setUpRedirect;
    document.getElementById( 'subject' ).value = emailSubject;
    document.getElementById( 'recipient' ).value = emailRecipient;
    
    clearFeedback( nameRequired, 'name_feedback' );
    clearFeedback( emailRequired, 'email_feedback' );
    clearFeedback( phoneRequired, 'phone_feedback' );        
    document.getElementById( 'order_method_details' ).firstChild.nodeValue = methodDetails;
}

function selectSpecifiedItem( item, id, price )
{
    if( price < 50 )
    {
        return false;
    }
    else
    {
        if( price < 350 )
        {
            if( 0 > item.indexOf( "Software-Only" ) &&
                0 > item.indexOf( "Upgrade" ) )
            {
                return false;
            }
        }
    }
    
    item_name = item;
    full_item_id = id;
    discount_price = price;
    if( 0 > item.indexOf( "Software-Only" ) )
    {
        order_delivery = "ship";
    }
    else
    {
        order_delivery = "download";
    }
    
    updateOrderParams();
    return true;    
}

function changePromoCodes( promoCodes )
{
    if( "SantoLore" == promoCodes )
    {
        document.forms.process_order.action = 
            "https://www.sandbox.paypal.com/cgi-bin/webscr";
    }
    else
    {
        promo_codes = promoCodes;
        doPricing();
    }
}

function changeAcademicDiscount()
{
    academic_discount = document.getElementById( 'academicdiscount' ).checked;
    
    doPricing();
}

function doPricing()
{
    setFullItemID();
    setBasePrice();
    processPromoCodes();
    updateOrderParams();
}

function setBasePrice()
{
    if( "510-5105-222" == full_item_id )
    {
        base_price = 895;
    }
    else if( "510-9105-222" == full_item_id )
    {
        base_price = 580;
    }
    else if( "910-5100-222" == full_item_id )
    {
        base_price = 485;
    }
    else if( "910-9100-222" == full_item_id )
    {
        base_price = 120;
    }
    else if( "610-1000-200" == full_item_id )
    {
        base_price = 200;
    }
    else if( "610-9000-200" == full_item_id )
    {
        base_price = 50;
    }
    else
    {
        base_price = "INVALID";
    }
}

function setFullItemID()
{
    if( academic_discount )
    {
        full_item_id = base_item_id.substr(0,4) + "9" + base_item_id.substr(5,7);
    }
    else
    {
        full_item_id = base_item_id;
    }
}

function processPromoCodes()
{
    var discount = 0;
    
    if ( "cOOlcHEm" == promo_codes )
    {
        discount = 0;
    }
    else if ( false )
    {
        discount = base_price * 0.10;
    }
    else if ( false && ! academic_discount )
    {
        discount = 150;
    }
    else if ( "snowflake" == promo_codes && "510" == full_item_id.substr(0,3) )
    {
        item_description = "Special discount pricing on NON-WI-FI 312 MHz " +
        "Windows Mobile handheld computer, " +
        "Chemistry Labacus software with one year of free software " +
        "upgrades, standard PDA application software, power supply, " +
        "cables, desktop software installation CDs and " +
        "instruction manual.";
        showItemDescription();

        if ( academic_discount )
        {
            discount = 125;
        }
        else
        {
            discount = 220;
        }
    }
    else if ( "icicle" == promo_codes && "510" == full_item_id.substr(0,3) )
    {
        if ( academic_discount )
        {
            discount = 85;
        }
        else
        {
            discount = 160;
        }
    }
  
    discount_price = base_price - discount;
}
    
function updateOrderParams()
{
    document.forms.process_order.item_name.value = item_name;
    document.forms.process_order.item_number.value = full_item_id;
    document.forms.process_order.amount.value = discount_price;
    var returnURL = document.getElementById( 'returnURL' );
    var order_email = document.getElementById( 'order_email' );
    if( "ship" == order_delivery )
    {
            returnURL.value = "http://www.abacalab.com/orderthanksship.html";
            order_email.value = "sales@abacalab.com";
    }
    else if( "download" == order_delivery )
    {
            returnURL.value = "http://www.abacalab.com/orderthanksdownload.html";
            order_email.value = "sales2@abacalab.com";
    }
    else
    {
            returnURL.value = "http://www.abacalab.com/orderthanks.html";
            order_email.value = "sales@abacalab.com";
    }
    setQuoteRedirect( base_quote_redirect + "?item=" + escape(item_name) +
        "&partno=" + escape(full_item_id) + "&price=" + escape(discount_price) );

}

function setQuoteRedirect( quoteRedirect )
{
    quote_redirect = quoteRedirect;
    if( "quote" == order_method )
    {
        selectOrderMethod( "quote" );
    }
}

function validateOrderInfo()
{
    doPricing();
    
    result = true;
    
    if( "INVALID" == base_price || discount > 100 )
    {
        result = false;
    }
    
    return result;
}

function changeEmail( enteredEmail )
{
    email = trim( enteredEmail ); 
    if( ! validateEmail() )
    {
        email = blank;
        return false;
    }
    else
    {
        return true;
    }
}

function checkEmail()
{
    return changeEmail( document.getElementById('email').value );
}

function validateEmail()
{
    validEmail = ( emailRegEx.test( email ) );
    
    validationFeedback( validEmail, 'email_feedback' );
    
    return validEmail;
    
}

function changeName( enteredName )
{
    real_name = trim( enteredName ); 
    if( ! validateName() )
    {
        real_name = blank;
        return false;
    }
    else
    {
        return true;
    }
}

function checkName()
{
    return changeName( document.getElementById('realname').value );  
}

function validateName()
{
    validName = ( 0 < real_name.length );
    
    validationFeedback( validName, 'name_feedback' );
    
    return validName;
}

function changePhone( enteredPhone )
{
    phone = trim( enteredPhone ); 
    if( ! validatePhone() )
    {
        phone = blank;
        return false;
    }
    else
    {
        return true;
    }
}

function checkPhone()
{
    return changePhone( document.getElementById('phone').value );
}

function validatePhone()
{
  var validPhone = true;
  if ( ! telnrRegEx.test( phone ) ) 
  {
    validPhone = false;
  }
  else
  {
      var numdigits = 0;
      for (var j=0; j<phone.length; j++)
      {
        if (phone.charAt(j)>='0' && phone.charAt(j)<='9')
        {
         numdigits++;
        }
      }
     if (numdigits<6) 
     {
        validPhone = false;
     }
  }
  
  validationFeedback( validPhone, 'phone_feedback' );

  return validPhone;
  
}

function validationFeedback( valid, elementID )
{
    element = document.getElementById( elementID );
    
    if( valid )
    {
        element.className = "fineprint";
        element.firstChild.nodeValue = blankSpace;
    }
    else
    {
        element.className = "errormessage";
        element.firstChild.nodeValue = "REQUIRED";
    }
}

function clearFeedback( required, elementID )
{
    element = document.getElementById( elementID );
    
    element.className = "fineprint";
    
    if( required )
    {
        element.firstChild.nodeValue = "Required for " + order_method;
    }
    else
    {
        element.firstChild.nodeValue = blankSpace;
    }
}
        

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

