﻿// === PARAMO PRODUCT FUNCTIONS === //

// Eonic Ltd 2009 - Webesite Design and Development

// Simple .exists() function
jQuery.fn.exists = function () { return jQuery(this).length > 0; }


$(document).ready(function () {

    //jQuery hover menu
    //call the jQuery Hover Over and Out
    $('.hover').hover(over, out);
    //Tell the browser to change the background when hovered over
    function over(event) {
        $('.hovermenu', this).css("display", "block");
    }
    //tell the browser to change the background to nothing when 
    //going outside the object area
    function out(event) {
        $('.hovermenu', this).css("display", "none");
    }

    if ($("#continueShoppingDummy").exists()) {
        $("#continueShoppingDummy").click(function () {
            // Click Real continue shopping 
            $("#cart .cartButtons input[name='cartBrief']").click();
        });
    }
    if ($("#checkoutDummy").exists()) {
        $("#checkoutDummy").click(function () {
            // Click real proceed button 
            $(this).val('Please wait...');
            $("#discountsForm input.principle").click();
        });
    }

    // if postcodeAnywhere field is used, change text
    
    var getAddress = $('input.[value="Click to find"]');
    if (getAddress.exists()) {
        getAddress.val('Find address');

    }

    if ($("form#contact").exists()) {

        if ($('#cDelContactAddress').val() == $('#cContactAddress').val()) {
            if ($('#cContactAddress').val() == '') {
                //empty so hide delivery
                addDeliveryAddress();
            }
            else {
                //same so hide delivery
                addDeliveryAddress();
            }
        }
        else {
            //tick deliver top this address
            $('input[name="cIsDelivery"]').attr('checked', 'checked')
        }

        //when is delivery clicked
        $('input[name="cIsDelivery"]').click(function () {
            if ($(this).attr('checked')) {
                resetDelAddress();

            }
            else {
                addDeliveryAddress();
            }
        });
        //when form submitted
        $('input[name="cartBillAddress"]').click(function () {
            if ($('#cIsDelivery_1:checked').val() == null) {
                //  alert('do nowt');
                addDeliveryAddress();
            }
        });
    }


    // Auto-tick opt-in for cart reg.
    if ($("#cartFull #cartRegisterBox").exists()) {
        var bAlerts = $("#cartFull #cartRegisterBox .alert").exists();
        if (!bAlerts) {
            var bChecked = $("#cartFull #cartRegisterBox #newsletter_1:checked").exists();
            if (!bChecked) {
                $("#cartFull #cartRegisterBox #newsletter_1").attr('checked', 'checked')
            }
        }
    }

});



function resetDelAddress() {
    $('#cDelContactName').attr('value', '');
    $('#cDelContactName').attr('readonly', '');
    $('#cDelContactName').removeClass('greyed');
    $('#cDelContactCompany').attr('value', '');
    $('#cDelContactCompany').attr('readonly', '');
    $('#cDelContactCompany').removeClass('greyed');
    $('#cDelContactAddress').attr('value', '');
    $('#cDelContactAddress').attr('readonly', '');
    $('#cDelContactAddress').removeClass('greyed');
    $('#cDelContactCity').attr('value', '');
    $('#cDelContactCity').attr('readonly', '');
    $('#cDelContactCity').removeClass('greyed');
    $('#cDelContactState').attr('value', '');
    $('#cDelContactState').attr('readonly', '');
    $('#cDelContactState').removeClass('greyed');
    $('#cDelContactZip').attr('value', '');
    $('#cDelContactZip').attr('readonly', '');
    $('#cDelContactZip').removeClass('greyed');
    $('#cDelContactCountry').attr('value', '');
    $('#cDelContactCountry').attr('readonly', '');
    $('#cDelContactCountry').removeClass('greyed');
    $('#cDelContactTel').attr('value', '');
    $('#cDelContactTel').attr('readonly', '');
    $('#cDelContactTel').removeClass('greyed');
    $('#cDelContactFax').attr('value', '');
    $('#cDelContactFax').attr('readonly', '');
    $('#cDelContactFax').removeClass('greyed');
    $(".column2 .group label").each(function () {
        if ($(this).attr('for') != 'cIsDelivery_1') {
            $(this).removeClass('greyed');
        }
    });
}

function addDeliveryAddress() {
    var a = '';
    blankoutFormFields($('#cDelContactName'), $('#cContactName').val());
    blankoutFormFields($('#cDelContactCompany'), $('#cContactCompany').val());
    blankoutFormFields($('#cDelContactAddress'), $('#cContactAddress').val());
    blankoutFormFields($('#cDelContactCity'), $('#cContactCity').val());
    blankoutFormFields($('#cDelContactState'), $('#cContactState').val());
    blankoutFormFields($('#cDelContactZip'), $('#cContactZip').val());
    blankoutFormFields($('#cDelContactCountry'), $('#cContactCountry').val());
    blankoutFormFields($('#cDelContactTel'), $('#cContactTel').val());
    blankoutFormFields($('#cDelContactFax'), $('#cContactFax').val());

    var foo = [];
    $('#cDelContactCountry option:selected').each(function (i, selected) {
        foo[i] = $(selected).text();
    });

    $(".column2 .group label").each(function () {
        if ($(this).attr('for') != 'cIsDelivery_1') {
            $(this).addClass('greyed');
        }
    });
    /*alert('See all Billing details copied across to Delivery form...');*/
}

function blankoutFormFields(oInput, val) {
    oInput.attr('value', val);
    oInput.attr('readonly', 'readonly');
    oInput.addClass('greyed'); 
}
