function fnsetdefaultdelivery() {
    //If any of the products in the order belongs to Floral Art,
    //the delivery date should be 7 working days.
    //Otherwise, determine the delivery date starting with date today.
    var startdate = new Date();

    if(document.getElementById('hdn_floral_art_product_exists').value == 'yes') {

        /*
        ddate1 = given
        ddate2 = ddate1 + 7days

        loop each day from ddate1 to ddate2
            isholiday = isholiday(day)
            if isholiday or sunday
                get next available day
                compute difference of days from previous day to the new day
                add the difference to ddate2
        so, the last date created from the loop above is the final date
        */

        var enddate = new Date();
        enddate.setDate(startdate.getDate() + 7);
        var one_day=1000*60*60*24
        
        for(ctrdate = startdate, daycount = 0; ctrdate <= enddate; ctrdate.setDate(ctrdate.getDate() + 1)) {
            isctrdateholiday = determineifholiday(ctrdate);
            if(startdate.getDay() == 0 || isctrdateholiday) {
                //get next available day
                strctrdate = (ctrdate.getMonth() + 1) + '/' + (ctrdate.getDate() < 10 ? '0'+ctrdate.getDate() : ctrdate.getDate()) + '/' + ctrdate.getFullYear();
                strpossibledate = fngetnextavailabledate(strctrdate);
                arrpossibledate = strpossibledate.split("/"); //mm/dd/yyyy
                possibledate = new Date();
                possibledate.setFullYear(arrpossibledate[2],parseInt(arrpossibledate[0])-1,arrpossibledate[1])
                //compute difference of days from previous day to the new day
                diffdays = parseInt(Math.ceil(possibledate.getTime() - ctrdate.getTime())/one_day);
                //add the difference to ddate2
                enddate.setDate(enddate.getDate() + diffdays);
            } else {
                if(daycount == 7) break;
                daycount++;
            }
        }
        
        strpossibledate = (ctrdate.getMonth() + 1) + '/' + (ctrdate.getDate() < 10 ? '0'+ctrdate.getDate() : ctrdate.getDate()) + '/' + ctrdate.getFullYear();
        document.getElementById('spn_datetime').innerHTML = strpossibledate + ' at 10AM - 2PM';
        document.getElementById('txt_date').value = strpossibledate;
        document.getElementById('opt_time10am').disabled = '';
        document.getElementById('opt_time10am').checked = 'true;';
        $('#hdn_check').val(1);
        $('#hdn_floral_art_earliestdate').val(strpossibledate);
        
    } else {
        
        var currtime = parseFloat(startdate.getHours() + '.' + startdate.getMinutes());
        var limittime_2pm = 14.0
        var limittime_7pm = 19.01
        var istodayholiday = determineifholiday(startdate);

        thisdate = (startdate.getMonth() + 1) + '/' + (startdate.getDate() < 10 ? '0'+startdate.getDate() : startdate.getDate()) + '/' + startdate.getFullYear();
        if(currtime < limittime_2pm) {
            //thisdate = (today.getMonth() + 1) + '/' + (today.getDate() < 10 ? '0'+today.getDate() : today.getDate()) + '/' + today.getFullYear();
            if(startdate.getDay() != 0 && !istodayholiday) {
                //strpossibledate = formatdate(thisdate);
                strpossibledate = thisdate;
                document.getElementById('spn_datetime').innerHTML = strpossibledate + ' at 2PM - 6PM or 6PM - 8PM';
                document.getElementById('txt_date').value = strpossibledate;
                document.getElementById('opt_time10am').disabled = 'disabled';
                document.getElementById('opt_time2pm').checked = 'true;';
            } else {
                strpossibledate = fngetnextavailabledate(thisdate);
                //strpossibledate = formatdate(strpossibledate);
                document.getElementById('spn_datetime').innerHTML = strpossibledate + ' at 10AM - 2PM';
                document.getElementById('txt_date').value = strpossibledate;
                document.getElementById('opt_time10am').disabled = '';
                document.getElementById('opt_time10am').checked = 'true;';
            }
        } else if(currtime > limittime_7pm) {
            //thisdate = (today.getMonth() + 1) + '/' + today.getDate() + '/' + today.getFullYear();
            strpossibledate = fngetnextavailabledate(thisdate);
            //strpossibledate = formatdate(strpossibledate);
            document.getElementById('spn_datetime').innerHTML = strpossibledate + ' at 2PM - 6PM or 6PM - 8PM';
            document.getElementById('txt_date').value = strpossibledate;
            document.getElementById('opt_time10am').disabled = 'disabled';
            document.getElementById('opt_time2pm').checked = 'true;';
        } else {
            //thisdate = (today.getMonth() + 1) + '/' + today.getDate() + '/' + today.getFullYear();
            strpossibledate = fngetnextavailabledate(thisdate);
            //strpossibledate = formatdate(strpossibledate);
            document.getElementById('spn_datetime').innerHTML = strpossibledate + ' at 10AM - 2PM';
            document.getElementById('txt_date').value = strpossibledate;
            document.getElementById('opt_time10am').disabled = '';
            document.getElementById('opt_time10am').checked = 'true;';
        }
        $('#hdn_check').val(1);
    }
}

function formatdate(datetoformat) {
    arrdatetoformat = datetoformat.split('/')
    newdate = arrdatetoformat[1] + '/' + arrdatetoformat[0] + '/' + arrdatetoformat[2];
    return newdate;
}

function determineday(datetodetermine) {
    dy = new Date(datetodetermine);
    return daysofweek[dy.getDay()];
}

function determineifholiday(thisdate) {
    for(ctr=0; ctr<arrholidays.length; ctr++) {
        holiday = new Date(arrholidays[ctr]);
        if(thisdate.getDate() == holiday.getDate() && thisdate.getMonth() == holiday.getMonth() && thisdate.getFullYear() == holiday.getFullYear()) {
            return true;
        } else if(thisdate.getTime() < holiday.getTime()) {
            return false;
        }
    }
    return false;
}

function fngetnextavailabledate(refdate) { //refdate should be in mm/dd/yyyy format, the same format is returned.
    //set default possible date
    var d = new Date(refdate);
    d.setDate(d.getDate() + 1);
    var strpossibledate = (d.getMonth() + 1) + '/' + (d.getDate() < 10 ? '0'+d.getDate() : d.getDate()) + '/' + d.getFullYear();
    possibledate = new Date(strpossibledate );

    //validate possible date
    if(possibledate.getDay() > 0) { //not a sunday
        for(ctr=0; ctr<arrholidays.length; ctr++) {
            holiday = new Date(arrholidays[ctr]);
            if(possibledate.getTime() == holiday.getTime()) {
                strpossibledate = fngetnextavailabledate(strpossibledate);
                break;
            } else if(possibledate.getTime() < holiday.getTime()) {
                break;
            }
        }
    } else {
        strpossibledate = fngetnextavailabledate(strpossibledate);
    }

    return strpossibledate;
}

function setearliestdelivery(thisdate) { //thisdate is received in the format dd/mm/yyyy
    document.getElementById('opt_time10am').disabled = '';
    document.getElementById('opt_time2pm').disabled = '';
    document.getElementById('opt_time6pm').disabled = '';
    document.getElementById('opt_time10am').checked = 'true;';
    $('#hdn_check').val(1);

    //thisdate = formatdate(thisdate);
    var dthisdate = new Date(thisdate);
    var today = new Date();
    var isthisdateholiday = determineifholiday(dthisdate);
    var currtime = parseFloat(today.getHours() + '.' + today.getMinutes());
    var limittime_2pm = 14.0

    if(document.getElementById('hdn_floral_art_product_exists').value == 'yes') {
        floralartearlydate = new Date($("#hdn_floral_art_earliestdate").val());
        if(dthisdate < floralartearlydate) {
            alert('Delivery on the selected date is not possible.\nA product on your order list needs at least 7 working days to prepare before delivery.\nPlease select another date.')
            document.getElementById('opt_time10am').disabled = 'disabled';
            document.getElementById('opt_time2pm').disabled = 'disabled';
            document.getElementById('opt_time6pm').disabled = 'disabled';
            $('#hdn_check').val(0);
            return false;
        }
    }

    if(dthisdate.getMonth() == today.getMonth() && dthisdate.getDate() == today.getDate() && dthisdate.getFullYear() == today.getFullYear()) {
        if(!isthisdateholiday && dthisdate.getDay() > 0) {
            if(currtime < limittime_2pm) {
                document.getElementById('opt_time10am').disabled = 'disabled';
                document.getElementById('opt_time2pm').checked = 'true;';
            } else {
                alert('Delivery on the selected date is not possible anymore. Please select another date.')

                //suggest possible date

                document.getElementById('opt_time10am').disabled = 'disabled';
                document.getElementById('opt_time2pm').disabled = 'disabled';
                document.getElementById('opt_time6pm').disabled = 'disabled';
                $('#hdn_check').val(0);
            }
        } else {
            alert('The selected date is a sunday or holiday. Please select another date.')

            //suggest possible date

            document.getElementById('opt_time10am').disabled = 'disabled';
            document.getElementById('opt_time2pm').disabled = 'disabled';
            document.getElementById('opt_time6pm').disabled = 'disabled';
            $('#hdn_check').val(0);
        }
    } else if(dthisdate.getTime() < today.getTime()) {
        alert('The selected date is already a past. Please select another date.');

        //suggest possible date

        document.getElementById('opt_time10am').disabled = 'disabled';
        document.getElementById('opt_time2pm').disabled = 'disabled';
        document.getElementById('opt_time6pm').disabled = 'disabled';
        $('#hdn_check').val(0);
    } else {
        if(isthisdateholiday || dthisdate.getDay() == 0) {
            alert('The selected date is a sunday or holiday. Please select another date.')

            //suggest possible date

            document.getElementById('opt_time10am').disabled = 'disabled';
            document.getElementById('opt_time2pm').disabled = 'disabled';
            document.getElementById('opt_time6pm').disabled = 'disabled';
            $('#hdn_check').val(0);
        }
    }
}

function checkdelivery() {
    if($('#hdn_check').val() == '0') {
        alert('Please select the desired delivery date and time.');
        return false;
    }
    return true;
}

function displaydeliverydatetime(todo) { //todo: 1-show, 2-hide
    if(todo == 2) {
        $('#div_deliverydatetime').slideUp('fast');
        $('#delivery').val('nondelivery');
    } else {
        $('#div_deliverydatetime').slideDown('fast');
        $('#delivery').val('delivery');
    }
}

function ToggleSubMenu(withstyle,spn_pm) {
    style = ($('.'+withstyle).css('display') == 'block' ? 'none' : 'block');
    pm = ($('.'+withstyle).css('display') == 'block' ? '+' : '-');
    $('.'+withstyle).css('display',style);
    $('#'+spn_pm).html(pm);
}

/**
 * This function is used on Order Confirmation page's submit button.
 * If PayPal payment, send the email to the administrator and customer first before redirecting the page to PayPal
 */
function fnsendoutemail(actionurl, urlprocessadminemail) {
    if(actionurl.search(/paypal/i) > 0) {
        $.post(urlprocessadminemail,
            function(data) {
            })
    }
    $('#checkout_confirmation').submit();
    return false;
}

//Ajax to get total prices after selecting product attribute options
function getrealtimeprice (opt) {
    var optionprice = $('#'+opt).val();
    var productprice = $('#productprice').val();
    
    

    optionprice = parseFloat(optionprice);
    productprice = parseFloat(productprice.replace("$",""));

    var totalprice = "$"+roundNumber((optionprice + productprice),2);

    $('#realtimeprice').html(totalprice);
}

function roundNumber(num, dec) {
  var result = String(Math.round(num*Math.pow(10,dec))/Math.pow(10,dec));
  if(result.indexOf('.')<0) {result+= '.';}
  while(result.length- result.indexOf('.')<=dec) {result+= '0';}
  return result;
}
