function addpayment() {
    $('#error').html('');
    $('#error').hide();
    var data = ($('#newpaymentform').formSerialize());
    $.post('paymentaction.php', data, function(res) {
        if(res.Errors) {
            var txt = '';
            $('#error').show();
            $.each(res.Errors, function(index, item){
                txt = txt + item + "<br/>";
            });
            $('#error').html(txt);
        } else {
            alert('Payment data successfully saved');
            parent.tb_remove();
        }
    }, 'json');
}
function saveprofile() {
    $('#error').html('');
    $('#error').hide();
    var data = ($('#editprofileform').formSerialize());
    $.post('sponsoraction.php', data, function(res) {
        if(res.Errors) {
            var txt = '';
            $('#error').show();
            $.each(res.Errors, function(index, item){
                txt = txt + item + "<br/>";
            });
            $('#error').html(txt);
        } else {
            alert('Your profile successfully saved');
            document.location.reload();
        }
    }, 'json');
}
function savepricing() {
    if($('#cost').val() == '') {
        document.getElementById('error').style.display = 'block';
        document.getElementById('error').innerHTML = 'Please enter value';
    } else {
        var data = {
            'data[Pricing][action]' : 'save',
            'data[Pricing][cost]' : $('#cost').val()
        };
        $.post('pricingaction.php', data, function(res) {
            if(res.Errors) {
                alert('Error saving pricing');
            } else {
                alert('Pricing successfully saved');
                $('#cost').val(res.Pricing.cost);
            }
        }, 'json');
    }
}
function validate_sponsor_email(field) {
    var result = $.ajax({
        type: 'POST',
        url: 'checkemail.php',
        async: false,
        data: ({
            'data[Sponsor][email]': field.value
        }),
        dataType: 'html'
    }).responseText;
    return result;
}
function addorphan() {
    if($('#paymentoption option:selected').val() == '') {
        document.getElementById('error').style.display = 'block';
        document.getElementById('error').innerHTML = 'Please select payment option';
    } else {
        var data = {
            'data[Sponsorship][action]' : 'add',
            'data[Sponsorship][isactive]' : '1',
            'data[Sponsorship][kind]' : 'sponsorfor',
            'data[Sponsorship][orphan]' : $('#orphan').val(),
            'data[Sponsorship][sponsor]' : $('#sponsor').val(),
            'data[Sponsorship][paymentoption]' : $('#paymentoption option:selected').val(),
            'data[Sponsorship][cost]' : $('#cost').val()
        };
        $.post('sponsorshipaction.php', data, function(res) {
            $('#orphans').html(res);
            parent.tb_remove();
        });
    }
}
function orphansearch() {
    var data = {
        'data[Search][firstname]' : $('#firstname').val(),
        'data[Search][lastname]' : $('#lastname').val(),
        'data[Search][city]' : $('#city').val(),
        'data[Search][region]' : $('#region option:selected').val(),
        'data[Search][age]' : $('#age option:selected').val(),
        'data[Search][sponsored]' : $('#sponsored option:selected').val()
    };
    $.get('orphanselect.php', data, function(res) {
        $('#orphanlist').html(res);
    });
}
function addsponsor() {
    if($('#paymentoption option:selected').val() == '') {
        document.getElementById('error').style.display = 'block';
        document.getElementById('error').innerHTML = 'Please select payment option';
    } else {
        var data = {
            'data[Sponsorship][action]' : 'add',
            'data[Sponsorship][isactive]' : '1',
            'data[Sponsorship][kind]' : 'sponsoredby',
            'data[Sponsorship][orphan]' : $('#orphan').val(),
            'data[Sponsorship][sponsor]' : $('#sponsor').val(),
            'data[Sponsorship][paymentoption]' : $('#paymentoption option:selected').val(),
            'data[Sponsorship][cost]' : $('#cost').val()
        };
        $.post('sponsorshipaction.php', data, function(res) {
            document.location.reload();
        //$('#sponsors').html(res);
        //parent.tb_remove();
        });
    }
}
function detachsponsor(orphan, sponsor) {
    if(confirm('You are about to delete this sponsorship record from the database. Do you want to continue?')) {
        var data = {
            'data[Sponsorship][action]' : 'detach',
            'data[Sponsorship][orphan]' : orphan,
            'data[Sponsorship][sponsor]' : sponsor
        };
        $.post('sponsorshipaction.php', data, function(res) {
            document.location.reload();
        });
    }
}
function sponsorsearch() {
    var data = {
        'data[Search][firstname]' : $('#firstname').val(),
        'data[Search][lastname]' : $('#lastname').val(),
        'data[Search][city]' : $('#city').val(),
        'data[Search][state]' : $('#state option:selected').val(),
        'data[Search][sponsorship]' : $('#sponsorship option:selected').val()
    };
    $.get('sponsorselect.php', data, function(res) {
        $('#sponsorlist').html(res);
    });
}
function makenavigation(id) {
    $('a.search' + id).bind('click', function(event) {
        event.preventDefault();
        $.get(this.href, {}, function(response) {
            $('#' + id).html(response)
        })
    });
    $('p.nav').show();
}
function deletesponsor(id) {
    if(confirm('You are about to delete this sponsor from the database. Do you want to continue?')) {
        var data = {
            'data[Sponsor][action]': 'delete',
            'data[Sponsor][id]': id
        };
        $.post('adm_sponsoraction.php', data, function(res) {
            if(res.Errors) {
                alert(res.Errors[0]);
            } else {
                document.location = 'adm_sponsors.php';
            }
        }, 'json');
    }
}
function deleteorphan(id) {
    if(confirm('You are about to delete this orphan from the database. Do you want to continue?')) {
        var data = {
            'data[Orphan][action]': 'delete',
            'data[Orphan][id]': id
        };
        $.post('adm_orphanaction.php', data, function(res) {
            if(res.Errors) {
                alert(res.Errors[0]);
            } else {
                document.location = 'adm_orphans.php';
            }
        }, 'json');
    }
}
function getRandomNum(lbound, ubound) {
    return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}
function getRandomChar(number, lower, upper, other, extra) {
    var numberChars = '0123456789';
    var lowerChars = 'abcdefghijklmnopqrstuvwxyz';
    var upperChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
    var charSet = extra;
    if (number == true)
        charSet += numberChars;
    if (lower == true)
        charSet += lowerChars;
    if (upper == true)
        charSet += upperChars;
    if (other == true)
        charSet += otherChars;
    return charSet.charAt(getRandomNum(0, charSet.length));
}
function getPassword(length, extraChars, firstNumber, firstLower, firstUpper, firstOther,
    latterNumber, latterLower, latterUpper, latterOther) {
    var rc = "";
    if (length > 0)
        rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars);
    for (var idx = 1; idx < length; ++idx) {
        rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars);
    }
    return rc;
}
function generatepassword() {
    return getPassword(8, false, true, true, true, false, true, true, true, false);
}
