var order_des;
var order_cost;
var firstname;
var lastname;
var address;
var city;
var state;
var country;
var zip;
var email;
var phone;
var order_amount;


$(document).ready(function(){

	order_des	= $("#UD-purchase-description");
	order_cost	= $("#UD-purchase-total-cost");
	firstname 	= $("#UD-purchase-firstname");
	lastname 	= $("#UD-purchase-lastname");
	address 	= $("#UD-purchase-address");
	city 		= $("#UD-purchase-city");
	state 		= $("#UD-purchase-state");
	country 	= $("#UD-purchase-country");
	zip 		= $("#UD-purchase-zip");
	email 		= $("#UD-purchase-email");
	phone 		= $("#UD-purchase-phone");

	params={};window.location.search.replace(/[?&;#]+([^=&]+)=([^&]*)/gi,function(str,key,value){params[key] = value;});

	if(params.redir){
        redir_addr = params.redir;
    }

    /*fetch billing and order info from database
	make call to the api and populate order details and billing info*/
    //// console.log("ord: "+params.ord+";pr: "+params.p);
    $.post("API.php", 
        {purchase: "GET_PURCHASE", p: params.p, o: params.ord}) 
        .done(function (data){
            // console.log("data: "+data);
            if(data !=null && !$.isEmptyObject(data)){
            	// // console.log("call for get purchase successful");
                populate_fields(data);
            }
        },"json")
        .fail(function(xhr, status, error){
            alert("error: "+error);
            window.location.replace($(location).attr('href').split('purchase')[0]+"projectDashboard-copy.php");
        });

    /*send credit card details to charge credit card for authorize and capture*/
    $("#UD-purchase-submit").click(function(){
        var cardnum = $("#UD-purchase-credit-card").val();
        var expdate = $("#UD-purchase-credit-expdate").val();
        var sec_code = $("#UD-purchase-credit-code").val();
        var pay = new Object;
        pay['card'] = cardnum;
        pay['expdate'] = expdate;
        pay['code'] = sec_code;
        // $.post("API.php",{payments: "GET_BILLING", id: params.p}, function(data){
        //     console.log(data[0]);
        //     data = data[0];
        //     console.log("data: "+data.project_certification_cost);
        //     pay['amount'] = data.project_certification_cost;
        // }, "json");



        /*validate credit card fields before sending data*/
        if(validatepay(pay)){
            // console.log("data before sending: "+pay['amount']);
            $.post("API.php", 
            {purchase: "CHARGE_CREDIT", details: JSON.stringify(pay)}) 
            .done(function (data){
                console.log("data: "+data);

                if(data !=null && !$.isEmptyObject(data)){
                    
                    var obj = JSON.parse(data);
                    if(obj.response_code!=null && obj.response_code==1){
                        alert(obj.description);
                         var url = $(location).attr('href').split('purchase')[0]+"payment_success.php?p="+encodeURIComponent(params.p)+"&ord="+encodeURIComponent(params.ord);
                         $('#UD_purchase_form').attr('action', url);
                         $('#UD_purchase_form').submit();
                       // console.log("obj values: "+obj.response_code+";"+obj.auth_code+";"+obj.description);
                    }else{
                        var err_message = '';
                        if(obj.err_message!=null){
                            err_message = obj.err_message;
                        }
                        console.log("obj values: "+obj.response_code+";"+obj.description+";"+err_message);
                        alert("Error: "+obj.description+"\n"+err_message);
                    }
                }
            },"json")
            .fail(function(xhr, status, error){
                alert("error: "+error);
                window.location.replace("www.udeworld.com/uds/V2/index.php");
            }); 
        } 
    });
});

function validatepay(pay){
    if(pay){
        if(pay['card'] && pay['expdate'] && pay['code']){
            return true;
        }else{
            return false;
        }
    }
    return false;
}

function populate_fields(data){
    if(data==null){
        return;
    }

    var obj = JSON.parse(data);
    obj = obj[0];
    // console.log("obj: "+obj[0].order_info);
    $(order_des).append("<p>"+obj.order_info+"</p>");
    $(order_cost).append("<p>"+obj.order_amount+"</p>");
    $(firstname).append("<p>"+obj.firstname+"</p>");
    $(lastname).append("<p>"+obj.lastname+"</p>");
    $(address).append("<p>"+obj.address+"</p>");
    $(city).append("<p>"+obj.city+"</p>");
    $(state).append("<p>"+obj.state+"</p>");
    $(country).append("<p>"+obj.country+"</p>");
    $(zip).append("<p>"+obj.zipcode+"</p>");
    $(email).append("<p>"+obj.email+"</p>");
    $(phone).append("<p>"+obj.phone+"</p>");
}
;