//##########################################################
//##                                                                       
//##  SENDITNOW AJAX scripts to load banner on index.epl 
//##  (c) 2007 Neopost Loop One
//##  Coding and Documentation by Austin Programming Solutions (APS)       
//##  Core Developers: Kevin Ludlow (ludlow@aps-tx.com) and                
//##                   Travis Huber (thuber@aps-tx.com)                    
//##                                                                       
//##  The following software/script was designed and written for           
//##  Neopost Loop One as a work for hire.                                       
//##                                                                       
//##  No portion of this script may be duplicated, sold, distributed,      
//##  or otherwise used without the owner's explicit written consent.      
//##                                                                       
//##  FILE:      sinjax.js                                          
//##  CREATED:   March 8, 2007                                              
//##  DEVELOPER: Travis Huber                                              
//##                                                                       


// This listen function is an easy way to add event handlers.
function listen(evt,func)
{
    elListen(window,evt,func);
}

function elListen(el, evt, func)
{
    if(document.addEventListener) {
        el.addEventListener(evt, func, true);
    } else if(document.attachEvent) {
        el.attachEvent("on"+evt,func);
    }
}

// xmlLoad handles our ajax requests.
// doc = the name of the file to request server-side
// elem = the element to update with the result.
function xmlLoad(doc,elem) {
    // xmlDoc is our global var for the ajax request.
    var xmlDoc = null ;
    if (typeof window.ActiveXObject != 'undefined' ) {
        xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        xmlDoc = new XMLHttpRequest();
    }
    //alert('doc is ' + doc);
    xmlDoc.open( "GET", doc, false);
    xmlDoc.send( null );

    return(xmlDoc.responseText);
}

function processXML(data,elem) {

    if(typeof elem == 'string') {
        elem = document.getElementById(elem);
    }

    var regexp = new RegExp("\\w");

    if(data.match(regexp))
    {
        try {
            elem.innerHTML = data;
            elem.style.display='block';
        } catch (e) {
            //don't know how to handle this error yet.
            //problems arise when people use MSO html
            //and IE simply fails on setting innerHTML.
        };
    }
}

function processPricing(data) {

    var prices = data.split('|');

    document.getElementById('price_next_day_noon').innerHTML = prices[0] || "14.99";
    document.getElementById('price_next_day').innerHTML      = prices[1] || "11.99";
    document.getElementById('price_two_days').innerHTML      = prices[2] || "10.99";
    if(prices[3])
    {
        var vp_price = prices[3];
        if(vp_price != 0)
        {
            var val_prop_el = document.getElementById('price_val_prop');
            val_prop_el.innerHTML = vp_price;
            val_prop_el.parentNode.style.backgroundImage = "url('../images/send_button_e.jpg')";
            val_prop_el.parentNode.parentNode.parentNode.style.top = "-31px"; 
            document.getElementById('navlist').style.position = 'relative';
            document.getElementById('navlist').style.top = '-31px';

            elListen(val_prop_el.parentNode,'mouseover', function(element) { this.backgroundImage="url('../images/send_button_f.jpg')";});
            elListen(val_prop_el.parentNode,'mouseout' , function(element) { this.backgroundImage="url('../images/send_button_e.jpg')";});
        }  
    }
}

function init_welcome()
{
    //processXML(xmlLoad('senditnow_welcome.epl'),'welcome_text');
    processPricing(xmlLoad('senditnow_prices.epl'));   
}

function init_prices()
{
    processXML(xmlLoad('senditnow_prices_whatwedo.epl'),'whatwedo_prices');   
}

function init_ship()
{
    document.getElementById("agree").checked = false;
    processXML(xmlLoad('senditnow_ship.epl'),'ship_content');
}

function goto(page) 
{
    if (page) {
        document.location.href=page; 
    }   

    return(false);
}

function proceedToShip() 
{

    if(!document.getElementById("agree").checked) {
        alert('You must agree to our terms and conditions before proceeding.');
        return(false);
    }       

    var selection=getQueryVariable('sin_mode');

    return goto('shipping.epl?sin_mode='+selection);

}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
} 

