


function submitCancelUrl() {
    $('#mfromtable').html( "<?php lal_patch( $cancel_page ) ?>" ) ;
    top.window.location.href = '<?php print $cancel_page ?>';
    return false;
}


//  DISABLES THE PURCHASE AGAIN OPTION
function submitPurchaseNotAnother(formX, action, target) {
    var elementExists = document.getElementById("purchase_again");
    if ( elementExists != null ) {
        document.getElementById("purchase_again").value = "off";
    }
    submitPurchase2(formX , action, target);
}


//  ENABLES THE PURCHASE AGAIN OPTION
//  22Sept2024 DLB - this is the Purchase and Purchase Again feature that is being disabled
function XsubmitPurchaseAnother(formX , action, target) {
    var elementExists = document.getElementById("purchase_again");
    if ( elementExists != null ) {
        document.getElementById("purchase_again").value = "on";
    }
    $("#" + formX + " #cardsave").prop( "checked", true );
    submitPurchase2(formX , action, target);
}


function submitPurchaseAnotherNoPay(formX, action, target) {

    var elementExists = document.getElementById('purchase_again_nopay');
    if ( elementExists != null ) {
        document.getElementById("purchase_again_nopay").value = "on";
    }
    $("#" + formX + " #cardsave").prop( "checked", true );
    submitPurchase2(formX , action, target);
}







function pictureView(storeitem_id, product, pindex, domain) {

    url = domain + '/rx/display_controller.php?action=picture_view2&storeitem_id=' + storeitem_id + '&product=' + product + '&pindex=' + pindex;
    // alert(">>>" + url );
    window.open(url,'_blank');
    return;

}



function discount_dirty( status ) {
    //alert("JSFnc Hello: discount_dirty");
    discount_dirty_flag = status;
}


function enterDiscountCategory(form, do_empty_code = true) {

    // alert("JSFnc Hello: enterDiscountCategory 1");

    var codexx = $('#discountcode').val();

    //  If no code and high spped just do if there is code....
    if (( codexx == null ) || ( codexx == "" )) {
        //updateFormPrice(form);
        //return;
    }

    if ((( codexx == null )|| ( codexx == "" )) && !do_empty_code ) {
       //  skip discount and do just price
       //  alert("JSFnc Hello: enterDiscountCategory 2");
        updateFormPrice(form);
        return;
    }

    // alert("JSFnc Hello: enterDiscountCategory 3");
    enterDiscount2(form);

    // alert("JSFnc Bye: enterDiscountCategory");
    return;
}

function enterDiscount2(form) {
    //  JAVA SCRIPT CODE TO VALIDATE DISCOUNT CODES

    //alert("JSFnc Hello: enterDiscount2");

    var user_email = $("#" + form + " #Email").val();
    if ( user_email == null ) {
        user_email = $("#" + form + " #blob_Email").val();
    }

    // alert("JSFnc Hello: enterDiscount2");

    //  SEE IF DISCOUNT CODE FIELD EXISTS
    //  If THIS IS A nostoreitem, THEN updateFormPrice IS AN EMPTY FNC
    if ( $('#discountcode').length == 0 ) {
        updateFormPrice(form);
        return;
    }

    var dcode_element = $('#' + form + ' #discountcode');
    var dcode = dcode_element.val();

    //  RETURN ERROR IF DISCOUNT CODE IS ENTERED BUT NOT EMAIL ADDRESS
    if ((( user_email == null ) || ( user_email == "" )) && ( dcode != null ) && ( dcode != "" )) {
        //  NOTE: only display error if discount field is NOT readonly
        if ( !dcode_element.prop('readonly') ) {
            $('#discount_message').html("You must enter an email address above to use a discount code");
            //  !!NO UPDATE PRICING!!
            //  27Nov2023 - DLB - Why no price update... this is bad,  pricing is still 0.
            updateFormPrice(form);
            return;
        }
    }

    //  NO EMAIL ADDRESS (and no Discount Code), CLEAR DISCOUNT MESSAGE AND UPDATE PRICING
    //  If THIS IS A nostoreitem, THEN updateFormPrice IS AN EMPTY FNC
    if (( user_email == null ) || ( user_email == "" ) ) {
        $('#discount_message').html("");
        updateFormPrice(form);
        return;
    }


    //  THIS AJAX IS BEING CALLED TO LOOK Up DISCOUNT CODE FOR THE USER
    //  IN THE CASE WHERE THERE'S nostoreitem WE DON'T WANT TO DO THIS

    //alert("Hello: enterDiscount2");

    //  SUBMIT THE FORM WITH DISCOUNT CODE TO GET DISCOUNT CODE INFO FROM SERVER
    //block_screen('Loading discount');
    block_screen('Loading values');
    $("#" + form + " #action").val("enter_discount");
    $.ajax(
        {
            type:"post",
            url:"/rx/purchase_controller.php" ,
            data:$('#' + form ).serialize(),

            success:function (response) {
                unblock_screen('10S');
                var result = response;
                if ( result.status == 'ok' ) {
                    $('#discount_amount_percent').val(result.discount.percent);
                    $('#discount_amount_fixed').val(result.discount.fixed);
                    $('#discount_amount_flat').val(result.discount.flat);
                    $('#discount_baseonly').val(result.discount.discountcode_baseonly);
                    $('#discount_message').html("<br>" + result.message);

                    if ( (result.code != null) && (result.code != "") ) {
                        $('#discountcode').val(result.code);
                    }
                    $('#' + form + ' #messagefieldtop').html('');
                    $('#' + form + ' #messagefieldmiddle').html('');
                    $('#' + form + ' #messagefieldbottom').html('');

                }  else {
                    $('#discount_amount_percent').val(0);
                    $('#discount_amount_fixed').val(0);
                    $('#discount_amount_flat').val(0);
                    $('#discount_message').html(result.message);
                    $('#' + form + ' #messagefieldtop').html('');
                    $('#' + form + ' #messagefieldmiddle').html('');
                    $('#' + form + ' #messagefieldbottom').html('');
                }

                //  WE GOT THE DISCOUNT CODE INFO -> DO AN UPDATE PRICE
                updateFormPrice(form);
            },
            error:function (x, status, errorThrown) {
                unblock_screen('10E');
                if (status == "error") {
                    var errorMsg = "";
                    if (errorThrown) {
                        errorMsg = errorThrown;
                    }
                    alert("The call to the server side failed (0100). " + errorMsg);
                }
            }
        }
    );
    //alert("JSFnc Bye: enterDiscount2");
    return true;
}

function enterDiscount2_VSR(form)  {
    //  JAVA SCRIPT CODE TO VALIDATE DISCOUNT CODES

    //alert("JSFnc Hello 1: enterDiscount2_VSR");
    var user_email = $('#' + form + ' #Email').val();
    if ( user_email == null ) {
        user_email = $('#' + form + ' #blob_Email').val();
    }

    var dcode_element = $('#' + form + ' #discountcode');
    var dcode = dcode_element.val();

    //alert("JSFnc Hello 2: enterDiscount2_VSR");

    //  RETURN ERROR IF DISCOUNT CODE IS ENTERED BUT NOT EMAIL ADDRESS
    if ((( user_email == null ) || ( user_email == "" ))
        && ( dcode != null ) && ( dcode != "" )) {
        //alert("enterDiscount2_VSR - no email and yes code");
        //  NOTE: only display error if discount field is NOT readonly
        if ( !dcode_element.prop('readonly') ) {
            $('#discount_message').html("You must enter an email address above to use a discount code");
            //  !!NO UPDATE PRICING!!
            return;
        }
    }

    //alert('step 1: enterDiscount2_VSR');

    if (( user_email == null ) || ( user_email == "" )) {
        $('#discount_message').html("");
        //alert("enterDiscount2_VSR - empty email");
        updateFormInvoice_teamrelay_VSR(form, true);
        return;
    }

    //alert('step 2: enterDiscount2_VSR');

    block_screen('Loading information');

    $("#" + form + " #action").val("enter_discount");

    $.ajax(
        {
            type:"post",
            url:"/rx/purchase_controller.php",
            data:$('#' + form).serialize(),

            success:function (response) {
                unblock_screen('10S');
                var result = response;
                if ( result.status == 'ok' ) {
                    $('#discount_amount_percent').val(result.discount.percent);
                    $('#discount_amount_fixed').val(result.discount.fixed);
                    $('#discount_amount_flat').val(result.discount.flat);
                    $('#discount_baseonly').val(result.discount.discountcode_baseonly);
                    $('#discount_message').html("<br>" + result.message);

                    if (( result.code != null ) && ( result.code != "" )) {
                        $('#discountcode').val(result.code);
                    }
                    $('#' + form + ' #messagefieldtop').html('');
                    $('#' + form + ' #messagefieldmiddle').html('');
                    $('#' + form + ' #messagefieldbottom').html('');

                }
                else {
                    $('#discount_amount_percent').val(0);
                    $('#discount_amount_fixed').val(0);
                    $('#discount_message').html(result.message);
                    $('#' + form + ' #messagefieldtop').html('');
                    $('#' + form + ' #messagefieldmiddle').html('');
                    $('#' + form + ' #messagefieldbottom').html('');
                }
                //alert("enterDiscount2_VSR - success");

                updateFormInvoice_teamrelay_VSR(form, true);
            },
            error:function (x, status, errorThrown) {
                unblock_screen('10E');
                if (status == "error") {
                    var errorMsg = "";
                    if (errorThrown) {
                        errorMsg = errorThrown;
                    }
                    alert("The call to the server side failed (0100). " + errorMsg);
                }
            }
        }
    );

    //alert("JSFnc Bye: enterDiscount2_VSR");
    return true;
}

function setPaymentMethod( ) {

    var value = $("#paypal_buy_now #paymethodselect option:selected").val();

    if ( value == "credit" ) {
        showDiv('paymethodcredit');
        hideDiv('paymethodcheck');

    } else if ( value == "check" ) {
        hideDiv('paymethodcredit');
        showDiv('paymethodcheck');
    }

    updateFormPrice('paypal_buy_now');
}

function changePaymentMethod(button_id) {

    $('[id$="-request-buttonx"]').addClass( 'apple-pay-button-white' );
    $('[id$="-request-buttonx"]').removeClass( 'apple-pay-button-black' );

    $('#' + button_id ).removeClass( 'apple-pay-button-white' );
    $('#' + button_id ).addClass( 'apple-pay-button-black' );


    hideDiv('paymethodcheck');
    hideDiv('paymethodvenmo');
    hideDiv('paymethodcredit');
    hideDiv('paybuttondiv');
    hideDiv('paymethodadminpaid');


//    $('#paypalbutton_xpurchase').text('Click To Make Payment');
    $('#paypalbutton_xpurchase').text('Submit');

    if (( button_id == "credit-request-buttonx" ) ||
        ( button_id == "mastercard-request-buttonx" ) ||
        ( button_id == "amex-request-buttonx" )) {
        $('#paymethodselect').val('credit');
        showDiv('paymethodcredit');
        showDivInline('paybuttondiv');
        showDiv('handling_line_item');
        showDiv('total_line_item');
        hideDiv('sub_line_item');


        $('#paypalbutton_xpurchase').text('Click To Make Payment');



    } else if ( button_id == "check-request-buttonx" ) {
        $('#paymethodselect').val('check');
        showDiv('paymethodcheck');

        showDivInline('paybuttondiv');
        hideDiv('handling_line_item');
        hideDiv('total_line_item');
        showDiv('sub_line_item');


        $('#paypalbutton_xpurchase').text('Submit');

        // showDivInline('paybuttondiv');
        // showDiv('handling_line_item');
        // showDiv('total_line_item');
        // hideDiv('sub_line_item');

    } else if ( button_id == "venmo-request-buttonx" ) {
        $('#paymethodselect').val('venmo');
        showDiv('paymethodvenmo');
        // showDivInline('paybuttondiv');
        // hideDiv('handling_line_item');
        // hideDiv('total_line_item');
        // showDiv('sub_line_item');

        showDivInline('paybuttondiv');
        showDiv('handling_line_item');
        showDiv('total_line_item');
        hideDiv('sub_line_item');

        $('#paypalbutton_xpurchase').text('Submit');

    } else if ( button_id == "adminpaid-request-buttonx" ) {
        $('#paymethodselect').val('admin_grant');
        showDiv('paymethodadminpaid');
        // showDivInline('paybuttondiv');
        // hideDiv('handling_line_item');
        // hideDiv('total_line_item');
        // showDiv('sub_line_item');

        showDivInline('paybuttondiv');
        showDiv('handling_line_item');
        showDiv('total_line_item');
        hideDiv('sub_line_item');

        $('#paypalbutton_xpurchase').text('Submit');

    } else if ( button_id == "adminunpaid-request-buttonx"  ) {

        $('#paymethodselect').val('admin_check');
        // showDivInline('paybuttondiv');
        // hideDiv('handling_line_item');
        // hideDiv('total_line_item');
        // showDiv('sub_line_item');

        /*
        // 17Oct2024 DLB - these are wrong

        showDiv('paymethodcredit');
        showDivInline('paybuttondiv');
        showDiv('handling_line_item');
        showDiv('total_line_item');
        hideDiv('sub_line_item');
        */
        showDivInline('paybuttondiv');
        showDiv('handling_line_item');
        showDiv('total_line_item');
        hideDiv('sub_line_item');

        $('#paypalbutton_xpurchase').text('Submit');

    } else {
        //  APPLE AND GOOGLE?
        $('#paymethodselect').val('credit');
        showDiv('handling_line_item');
        showDiv('total_line_item');
        hideDiv('sub_line_item');

        $('#paypalbutton_xpurchase').text('Submit');
    }

    updateFormPrice('paypal_buy_now');
}

function updateAddressField3(id) {

    var test = $("#"+id).length;

    if ( test == 0) {
        return;
    }
    var select  = $("#"+id).val();
    if ((select == "US" ) || (select == "" )) {
        showDiv( "cardstate" );
    } else {
        hideDiv( "cardstate" );
    }
    return;
}





//  ======================================
//  USAT
function getMemberId() {
    var requestType = 'memberid';
    var url = 'https://beta.usatriathlon.org/Demo/ReturnId.ashx';
    $.ajax({
        type: 'GET',
        cache: false,
        url: url,
        async: false,
        contentType: "application/json",
        data: "requestType=" + requestType,
        dataType: 'jsonp',
        success: function (v) {

            $("#blob_usat").val(v.memberid);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("Error");
            alert(xhr.status);
            alert(thrownError);
        }
    });
};

function getLicenseCode() {

    var requestType = 'licensecode';
    var url = 'https://beta.usatriathlon.org/Demo/ReturnId.ashx';
    $.ajax({
        type: 'GET',
        cache: false,
        url: url,
        async: false,
        contentType: "application/json",
        data: "requestType=" + requestType,
        dataType: 'jsonp',
        success: function (v) {
            $("#blob_usat").val(v.licensecode);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("Error returned from USAT");
            alert(xhr.status);
            alert(thrownError);
        }
    });

};


// ==========================================================
// UTIL

function convert_state_full( state ) {
    state = state.toLowerCase();
    if ( state == "--" ) {
        return( "-- select state --" );
    }
    if ( state == "al" ) {
        return( "Alabama" );
    }
    if ( state == "ak" ) {
        return( "Alaska" );
    }
    if ( state == "az" ) {
        return( "Arizona" );
    }
    if ( state == "ar" ) {
        return( "Arkansas" );
    }
    if ( state == "ca" ) {
        return( "California" );
    }
    if ( state == "co" ) {
        return( "Colorado" );
    }
    if ( state == "ct" ) {
        return( "Connecticut" );
    }
    if ( state == "de" ) {
        return( "Delaware" );
    }
    if ( state == "dc" ) {
        return( "District of Col." );
    }
    if ( state == "fl" ) {
        return( "Florida" );
    }
    if ( state == "ga" ) {
        return( "Georgia" );
    }
    if ( state == "hi" ) {
        return( "Hawaii" );
    }
    if ( state == "id" ) {
        return( "Idaho" );
    }
    if ( state == "il" ) {
        return( "Illinois" );
    }
    if ( state == "in" ) {
        return( "Indiana" );
    }
    if ( state == "ia" ) {
        return( "Iowa" );
    }
    if ( state == "ks" ) {
        return( "Kansas" );
    }
    if ( state == "ky" ) {
        return( "Kentucky" );
    }
    if ( state == "la" ) {
        return( "Louisiana" );
    }
    if ( state == "me" ) {
        return( "Maine" );
    }
    if ( state == "md" ) {
        return( "Maryland" );
    }
    if ( state == "ma" ) {
        return( "Massachusetts" );
    }
    if ( state == "mi" ) {
        return( "Michigan" );
    }
    if ( state == "mn" ) {
        return( "Minnesota" );
    }
    if ( state == "ms" ) {
        return( "Mississippi" );
    }
    if ( state == "mo" ) {
        return( "Missouri" );
    }
    if ( state == "mt" ) {
        return( "Montana" );
    }
    if ( state == "ne" ) {
        return( "Nebraska" );
    }
    if ( state == "nv" ) {
        return( "Nevada" );
    }
    if ( state == "nh" ) {
        return( "New Hampshire" );
    }
    if ( state == "nj" ) {
        return( "New Jersey" );
    }
    if ( state == "nc" ) {
        return( "North Carolina" );
    }
    if ( state == "nm" ) {
        return( "New Mexico" );
    }
    if ( state == "ny" ) {
        return( "New York" );
    }
    if ( state == "nd" ) {
        return( "North Dakota" );
    }
    if ( state == "oh" ) {
        return( "Ohio" );
    }
    if ( state == "ok" ) {
        return( "Oklahoma" );
    }
    if ( state == "or" ) {
        return( "Oregon" );
    }
    if ( state == "pa" ) {
        return( "Pennsylvania" );
    }
    if ( state == "ri" ) {
        return( "Rhode Island" );
    }
    if ( state == "sc" ) {
        return( "South Carolina" );
    }
    if ( state == "sd" ) {
        return( "South Dakota" );
    }
    if ( state == "tn" ) {
        return( "Tennessee" );
    }
    if ( state == "tx" ) {
        return( "Texas" );
    }
    if ( state == "ut" ) {
        return( "Utah" );
    }
    if ( state == "vt" ) {
        return( "Vermont" );
    }
    if ( state == "va" ) {
        return( "Virginia" );
    }
    if ( state == "wa" ) {
        return( "Washington" );
    }
    if ( state == "wv" ) {
        return( "West Virginia" );
    }
    if ( state == "wi" ) {
        return( "Wisconsin" );
    }
    if ( state == "wy" ) {
        return( "Wyoming" );
    }

    return("XX");
}


function  racereach_calc_discount( amount, discount_percent, discount_fixed, discount_flat ) {
    var discount = (amount * discount_percent * .01) + discount_fixed;
    if ( discount < 0 )  discount = 0;
    if ( discount_flat > 0 ) {
        discount = amount - discount_flat;
    }
    return(discount);
}





