﻿function makeStoreItem(refItem) {

    var payPalForm = $("body>form[id=__PayPalForm]")[0];
    
    
    if (payPalForm == undefined) {
        $("body").append(buildPayPalForm());

        payPalForm = $("form[id=__PayPalForm]")[0];
    }

    if ($(refItem).attr("price") != undefined) {
        $(refItem).find(".purchaseButton").append(htmlPayPalButton($(refItem).attr("description"), $(refItem).attr("price")));
    }
    
    //alert(htmlPayPalButton(refItem.description, refItem.price));
}


function buildPayPalForm() {

    var payPalForm =
            '<form target=\'paypal\' action=\'https://www.paypal.com/cgi-bin/webscr\' method=\'post\' ' +
            'id=\'__PayPalForm\'> ' +
            '<input name=\'add\' value=\'1\' type=\'hidden\' id=\'Hidden1\' /> ' +
            '<input name=\'cmd\' value=\'_cart\' type=\'hidden\' id=\'Hidden2\' /> ' +
            '<input name=\'business\' value=\'rondepriest@gmail.com\' type=\'hidden\' id=\'Hidden3\' /> ' +
            '<input name=\'item_name\' value=\'item name\' ' +
            ' type=\'hidden\' id=\'Hidden4\' /> ' +
            '<input name=\'amount\' value=\' $.01\' ' +
            ' type=\'hidden\' id=\'Hidden6\' /> ' +
            '<input name=\'no_note\' value=\'1\' type=\'hidden\' id=\'Hidden7\' /> ' +
            '<input name=\'currency_code\' value=\'USD\' type=\'hidden\' id=\'Hidden8\' /> ' +
            '<input name=\'lc\' value=\'US\' type=\'hidden\' id=\'Hidden9\' /> ' +
            '</form> ';

    return payPalForm;
}

function htmlPayPalButton(itemname, price) {

    var PayPalButton = '<input src="https://www.paypal.com//en_US/i/btn/sc-but-03.gif" class="paypalbutton" alt="Make payments with PayPal - its fast, free and secure!" ' +
         'border="0" type="image" onclick="buyItem(\'' + itemname + '\',\'' + price + '\'); return false;" />'

    return PayPalButton;
}

function buyItem(itemname, price) {

    var payPalForm = $("form[id=__PayPalForm]")[0];
    $(payPalForm).find("input[name=amount]")[0].value = price;
    $(payPalForm).find("input[name=item_name]")[0].value = itemname;
    $(payPalForm).submit();
}

function errhandler(eventobj) {
    // get debug info

    debugger;
}
