﻿var infoList = ['carnerosbag', 'carnerosbasket', 'celebrationbasket', 'champagnebasket', 'cheesebasket',
                         'classic', 'couples', 'dinnerfor2', 'individual', 'redflower', 'romance', 'romanticbasket', 
                         'teabasket', 'winebasket', 'winecountrybasket', 'yoga'];
var prearrival = ['info', 'amenities', 'dining', 'spa', 'activities', 'checkOut'];
var currSection = 'info';
var isBusy = false;

window.addEvent('domready', function()
{
    //hiding sections that are not in use, if it's noticeable then hide it
    //right after the section is created
    prearrival.each(function(item, i){
       
        var img = (item + 'Img');
            
        //init onclick on the section menu
        $(item + 'Img').addEvents(
        {
            click: function(){
                if(!isBusy)
                {
                    if($('disableScreen'))
                        enableScreen();
                        
                    toggleSection(item);
                }
            },
            mouseenter: function(){
                toggleImg(item, true);
            },
            mouseleave: function()
            {
                toggleImg(item, false);
            }
        });
        
        //add cursor pointer
        $(item + 'Img').setStyle('cursor', 'pointer');
    });
    
    
    //initialize more info list
    infoList.each(function(item, i){
        if($(item))
        {
            $(item).setStyle('cursor', 'pointer');
            new MooInfoTip({
                id: item
            });
        }
    });
    
    createDisableScreen();
    
    
    var dropdownList = ['spa1Treatment1', 'spa1Treatment2', 'spa1Treatment3', 'spa1Yoga', 'spa1Tharapist', 
                        'spa2Treatment1', 'spa2Treatment2', 'spa2Treatment3', 'spa2Yoga', 'spa2Tharapist', 
                        'spa3Treatment1', 'spa3Treatment2', 'spa3Treatment3', 'spa3Yoga', 'spa3Tharapist'];
    
    //overwriting the drop down menu
    dropdownList.each(function(item){
        new MooDropDown({
            id: item
        });
    });
    
    
});

window.addEvent('load', function()
{
    //overwrite the submit event and stop it from refreshing the page
    $('aspnetForm').addEvent('submit', function(e){
        toggleSection(prearrival[prearrival.length - 1]);
    
        new Event(e).stop();
        processForm();
    });
    
});

function toggleSection(ele)
{
    if($(currSection))
    {
        $(currSection).setStyle('display', 'none');
        $(currSection + 'Img').setProperty('src', 'gfx/prearrival_' + currSection + '.gif');
    }
    
    $(ele + 'Img').setProperty('src', 'gfx/prearrival_' + ele + '_roll.gif');
    
    //problem here: google chrome and safari won't retrieve the width and height
    //unless it's inline-table
    $(ele).setStyle('display', (Browser.Engine.webkit ? 'inline-table' : 'inline'));
    currSection = ele;
    
    
}

function toggleImg( ele, isRoll)
{
    if(currSection == ele)
        return;
        
    if(isRoll)
        $(ele + 'Img').setProperty('src', 'gfx/prearrival_'+ele+'_roll.gif');
    else
        $(ele + 'Img').setProperty('src', 'gfx/prearrival_'+ele+'.gif');
}

function createDisableScreen()
{
    var div = new Element('div', {
        
        id: 'disableScreen',
        styles:
        {
            position: 'absolute',
            left: 0,
            top: 0,
            zIndex: 999,
            backgroundColor: '#f0f0f0',
            opacity: 0,
            color: 'red'
            
        }
    }).inject($(document.body));
    var table = new Element('table', {
        width: '100%',
        height: '100%'
    }).inject(div);
    var tbody = new Element('tbody').inject(table);
    var tr = new Element('tr').inject(tbody);
    var td = new Element('td').inject(tr);
    var divText = new Element('div', {
        id: 'disableText', 
        styles:
        {
            padding: 20
        }
    }).inject(td);
    
}

function enableScreen()
{
    $('disableScreen').setStyle('opacity', 0);
    $('disableText').set('text', '');
}

function disableScreen(id)
{
    var container = $(id);
    
    $('disableScreen').setStyles({
        opacity: 0.9,
        left: container.getPosition().x,
        top: container.getPosition().y,
        width: container.getWidth(),
        height: container.getHeight(),
        lineHeight: "100%"
    });
}

function validate(form)
{
    disableScreen('checkOut');
    
    //not worth it to build out using element
    var html = '<strong>Please fill in the following required fields:</strong> <ul>';
    var msg = '';
    
    var fName = stringCheck(form,"infoFirstName",true,50,"First Name");
    var lName = stringCheck(form,"infoLastName",true,50,"Last Name");
    var email = checkEmail(form,"infoEmail",true,150,"Email");
    var arrivalMonth = stringCheck(form,"infoArrivalMonth",true,50,"Arrival Month");
    var arrivalDay = stringCheck(form,"infoArrivalDay",true,50,"Arrival Day");
    var arrivalYear = stringCheck(form,"infoArrivalYear",true,50,"Arrival Year");
    var phone = checkPhone(form,"checkPhone",true,10,"Phone");
    
    if (fName != '')
        msg += '<li>' + fName + '</li>';
    if (lName != '')
        msg += '<li>' + lName + '</li>';
    if (email != '')
         msg += '<li>' + email + '</li>';
    if (arrivalMonth != '')
        msg += '<li>' + arrivalMonth + '</li>';
    if (arrivalDay != '')
        msg += '<li>' + arrivalDay + '</li>';
    if (arrivalYear != '')
        msg += '<li>' + arrivalYear + '</li>';
    if (phone != '')
        msg += '<li>' + phone + '</li>';         
        
    if(!$('checkCancellation').checked)
        msg += '<li>You need to agree with the cancellation policy to continue.</li>';
    
    //check arrivalDate if it's today they need to call, instead of booking online
    var now = new Date();
    var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    today.setDate(today.getDate() + 2);
    var arrivalDate = new Date($('infoArrivalYear').value, getMonth($('infoArrivalMonth').value), $('infoArrivalDay').value);
    
    if(today > arrivalDate)
        msg += '<li>You need to book 48 hours prior to your arrival.  If you plan to come today, please call us at 707.299.4900 so we can assist you with your stay.</li>';
    
    
    html += msg + '</ul>';
    
    if(msg == '')
        return true;
    else
    {
        $('disableText').set('html', html);
        return false;
    }
}

function checkArrivalDate()
{
    var now = new Date();
    var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    today.setDate(today.getDate() + 2);
    var arrivalDate = new Date($('infoArrivalYear').value, getMonth($('infoArrivalMonth').value), $('infoArrivalDay').value);
    
    if(today > arrivalDate)
        alert('You need to book 48 hours prior to your arrival.  If you plan to come today, please call us at 707.299.4900 so we can assist you with your stay.');
}

function getMonth(month)
{
    switch(month)
    {
        case 'Jan': return 0;
        case 'Feb': return 1;
        case 'Mar': return 2;
        case 'Apr': return 3;
        case 'May': return 4;
        case 'Jun': return 5;
        case 'Jul': return 6;
        case 'Aug': return 7;
        case 'Sep': return 8;
        case 'Oct': return 9;
        case 'Nov': return 10;
        case 'Dec': return 11;
        default: return 0;
    }
}

function processForm(e)
{
    if(!validate($('aspnetForm')))
        return false;
    
    var processHtml = '<div style="text-align:center">Processing your submission...<br/><img src="gfx/loader.gif"></div>' ;
    $('disableText').set('html', processHtml);
    
    
    
    //the request is starting to put a busy marker here
    isBusy = true;
    
    var request = new Request.JSON({
        url: 'prearrival_submit.aspx',
        onComplete: function(response)
        {
            //if there is no error then print out the thank you text
            if(response && response.error == '')
            {
                $('disableText').set('html', $('thankyou').get('html'));
            }
            //else if the error is there then print it out
            else if(response && response.error != '')
            {
                printError('disableText', response.error);
            }
            //if response is not there that means the submit url is incorrect
            else
            {
                printError('disableText', 'There was an error with your request.  Please try again, if the problem persists please contact the administrator.');
            }
            isBusy = false;
        },
        onFailure: function(error)
        {
            //report or bury the error
            printError('disableText', 'There was an error with your request.  Please try again, if the problem persists please contact the administrator.');
            isBusy = false;
        }
    });
    
    //there is a problem with this form, since we don't want it to be runat="server" but the form tag is
    //in the master page, so I have no control over it.  Therefore, I have to get the data manually
    //and post it using ajax form.
    
    
    (function(){
        request.post(formData('prearrivalForm'));
    }).delay(1000);
    
}

function printError(id, msg)
{
    $(id).set('html', '<div style="text-align:center">' + msg + '</div>');
}

//retrieve the actual form data
function formData(id)
{
    var inputs = $$('#'+id+' input', '#'+id+' textarea', '#'+id+' select');
    var formStr = '{';
    var currRadio = '';
    var isComma = false;
    inputs.each(function(item, i){
        
        if(item.type == 'checkbox')
        {
            formStr += '"' + item.name + '": "' + item.checked + '"';
            isComma = true;
        }
        //for radio it will only return the last
        else if(item.type == 'radio' && item.checked)
        {
            formStr += '"' + item.name + '": "' + item.value + '"';
            isComma = true;
        }
        else if(item.type != 'checkbox' && item.type != 'radio')
        {
            formStr += '"' + item.name + '": "' + item.value + '"';
            isComma = true;
        }
        
        if(i + 1 < inputs.length && isComma)
        {
            formStr += ', ';
            isComma = false;
        }
    });
    formStr += '}';
    return JSON.decode(formStr);
}


//calendar functions
function initDate(month, day, year) 
{
    var dt = new Date();
    var advancedDay = 2;
    var yr = dt.getFullYear();
    var curYr = dt.getFullYear();
    var mo = dt.getMonth();
    mo++;
    var da = dt.getDate();

    var daysInCurrent = getDaysInMonth(da, yr);

    if (da + advancedDay > daysInCurrent)
    {	da = ((da+advancedDay) % daysInCurrent);
        if (mo == 12)
        {	mo = 1;
            yr++;	}
        else
        {	mo++;	}	}
    else
    {	da = da + advancedDay;	}
    da--;
    mo--;
    yr-=curYr;
    $(month).selectedIndex = mo;
    $(day).selectedIndex = da;
    $(year).selectedIndex = yr;
}

function createYear()
{
    var tdate = new Date()
    var curyr = tdate.getFullYear();
    for (var jj=0; jj<3; jj++) 
        document.write('<option value=' + (curyr+jj) + '>' + (curyr+jj) + '</option>' );
}

var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

function createMonth()
{
    for(var i = 0; i < months.length; i++)
        document.write('<option value=' + months[i] + '>' + months[i] + '</option>' );
}

function createDay()
{
    for (var i=1; i <= 31; i++) 
        document.write('<option value=' + i + '>' + i + '</option>' );
}

var spaList = ["Therapeutic Massage 30 minutes $75", 
"Therapeutic Massage 60 minutes $150", 
"Therapeutic Massage 90 minutes $215", 
"Flower Power Pulse Point Massage 60 minutes $150", 
"Carneros Healing Gem and Stone Massage 90 minutes $225", 
"Royal Thai Herbal Massage 90 minutes $225", 
"Red Flower Japan Body Ritual Massage 90 minutes $225", 
"Red Flower Hammam Body Ritual Massage 90 minutes $225", 
"Red Flower Carneros Wine Blossom Tribute 120 minutes $300", 
"Wild Magnolia Hair and Scalp Therapy 15 minutes $35", 
"Wild Magnolia Hair and Scalp Therapy 30 minutes $70", 
"Grape Seed and Guava Body Scrub 45 minutes $125", 
"Orchard Olive Stone and Honeydew Exfoliation 45 minutes $125", 
"Sweet Ripened Apricot and Chardonnay and Body Polish 45 minutes $125", 
"Sweet Ripened Apricot and Chardonnay and Body Polish 90 minutes $225", 
"Vitamin-Rich Goat Milk Body Scrub 45 minutes $125", 
"Vitamin-Rich Goat Milk Body Scrub and Massage 90 minutes $225", 
"Anti-Oxidant Body Wrap 75 minutes $195", 
"Jasmine Rose Rhassoul Clay Body Wrap 90 minutes $225", 
"The Huichica Creek Bath 30 minutes $75", 
"The Huichica Creek Bath for Couples 30 minutes $150", 
"Mellow Mama Pre-Natal Massage 60 minutes $150", 
"Mellow Mama Pre-natal Massage 90 minutes $215", 
"The Yummy Tummy 30 minutes $75", 
"Mama's Skin Smoothie 60 minutes $150", 
"4th Trimester Healing Hour 60 minutes $150", 
"4th Trimester Healing Hour 90 minutes $225", 
"Carneros Classic Facial 75 minutes $185", 
"Carneros Classic Facial Plus 90 minutes $225", 
"Gentle Man's Facial 75 minutes $185", 
"Fruitful Vitamin C Facial 75 minutes $185", 
"Chardonnay Anti-oxidant Wine Therapy Facial 30 minutes $75", 
"Chardonnay Anti-oxidant Wine Therapy Facial 75 minutes $185", 
"Goat Milk and Honey Extreme Moisture Facial 75 minutes $185", 
"Head to Toe Face and Body Anti-oxidant Booster 135 minutes $325", 
"Make-Up Application 45 minutes $65", 
"In Cottage Make Up Application 45 minutes $85", 
"Eye Brow Tinting 15 minutes $20", 
"Eye Lash Tinting 15 minutes $20", 
"Eye Brow Wax 15 minutes $30", 
"Lip Wax 15 minutes $20", 
"Chin Wax 15 minutes $20", 
"Full Face Fax 45 minutes $65", 
"Lower Leg Wax 30 minutes $65", 
"Full Leg Wax 60 minutes $120", 
"Bikini Wax 30 minutes $65", 
"Half Leg and Bikini 45 minutes $90", 
"Back Wax 30 minutes $65", 
"Chest 30 minutes $45", 
"Eye Therapy 15 minutes $20", 
"Wild Magnolia Hair and Scalp Therapy 15 minutes $35", 
"Wild Magnolia Hair and Scalp Therapy 30 minutes $70", 
"Holistic Hair and Scalp Therapy 60 minutes $135", 
"Stimulating Therapy for Thinning Hair 60 minutes $135", 
"Men's Hair Cut 30 minutes $45", 
"Women's Hair Cut 45 minutes $75", 
"Hair Styling and Blow Dry 45 minutes $65", 
"Hair Styling with Deep Conditioning and Blow Dry 60 minutes $90", 
"Up Do 60 minutes $85", 
"Sweet Ripened Apricot and Chardonnay Manicure 45 minutes $60", 
"Sweet Ripened Apricot and Chardonnay Pedicure 60 minutes $80", 
"Warm Goat Milk Manicure 45 minutes $60", 
"Warm Goat Milk Pedicure 60 minutes $80", 
"Grape Seed and Guava Manicure 45 minutes $60", 
"Grape Seed and Guava Pedicure 45 minutes $80", 
"Sugar Daddy Manicure 45 minutes $60", 
"Sugar Daddy Pedicure 60 minutes $80", 
"Red Flower Carneros Wine Blossom Manicure 60 minutes $80", 
"Red Flower Carneros Wine Blossom Pedicure 90 minutes $120", 
"Carneros Classic Facial 75 minutes $185", 
"Carneros Classic Facial Plus 90 minutes $225", 
"Gentle Man's Facial 75 minutes $185", 
"Fruitful Vitamin C Facial 75 minutes $185", 
"Chardonnay Anti-oxidant Wine Therapy Facial 30 minutes $75", 
"Chardonnay Anti-oxidant Wine Therapy Facial 75 minutes $185", 
"Goat Milk and Honey Extreme Moisture Facial 75 minutes $185", 
"Head to Toe Face and Body Anti-oxidant Booster 135 minutes $325", 
"Make-Up Application 45 minutes $65", 
"In Cottage Make Up Application 45 minutes $85", 
"Eye Brow Tinting 15 minutes $20", 
"Eye Lash Tinting 15 minutes $20", 
"Eye Brow Wax 15 minutes $30", 
"Lip Wax 15 minutes $20", 
"Chin Wax 15 minutes $20", 
"Full Face Fax 45 minutes $65", 
"Lower Leg Wax 30 minutes $65", 
"Full Leg Wax 60 minutes $120", 
"Bikini Wax 30 minutes $65", 
"Half Leg and Bikini 45 minutes $90", 
"Back Wax 30 minutes $65", 
"Chest 30 minutes $45", 
"Eye Therapy 15 minutes $20", 
"Private Training 60 minutes $100", 
"Private Active or Restorative Yoga 75 minutes $125", 
"Yoga for Couples 75 minutes $125", 
"Yoga for Mothers-To-Be 75 minutes $125", 
"Yoga for Kids 60 minutes $125", 
"Carneros Boot Camp 60 minutes $100", 
"Personal Writing Workshop 120 minutes $200", 
"Country Escape Spa Package 3 hours 15 minutes $400", 
"Holistic Retreat Spa Package3 hours 45 minutes $425", 
"Quality Time for Couples Package 2 hours 30 minutes $575", 
"Mayacama Mama Spa Package 3 hours 45 minutes $410", 
"Orchard Indulgence 4 hours 45 minutes $625"];

function createSpa()
{
    document.write('<option selected="selected" value="">(Choose)</option>');
    spaList.each(function(item){
        document.write('<option value="'+item+'">'+item+'</option>');
    });
}