﻿$(function () {
    switch (window.spid) {
        case 'confirmPaypalOrder':
            confirmPaypalOrder();
            break;
        case 'showInvoice':
            showInvoice();
            break;
    }

	init();
});

function init() {
/*
    loadCart();
	loadProductCategoryList();
	setShippingSetting();
*/
}

function setShippingSetting() {
	$.post(window.subURL + 'setting-get', {'SETTING': 'SHIPPING'}, function (data) {
		if (data == 'YES') {
			window.shipping = true;
		} else {
			window.shipping = false;
		}
	});
}

function isShipping() {
	return window.shipping;
}

function viewProduct(fProductID) {
	loaderAction('start');
	$.get(window.subURL + 'product-view&linkid='+fProductID, function (data) {
		fancyboxDisplay(data, function () {
			loaderAction('stop');
		});
	});

}

function addProductToCart(fID) {
	fArray = $('#addToCartForm').serializeArray();
	addToCart(fArray);
}

function addToCart(fArray) {
    $.post(window.subURL + 'addtocart', fArray, function (data) {
        if (data == 'OK') {
            loadCart();
            $.fancybox.close();
        } else {
            alert(data);
        }
    });
}

function addToCartWithOptions(fProductID, fSalePriceNum, fOptionID, fQty) {
    $.post(window.subURL + 'addtocart', {
            'productid': fProductID,
            'salepricenum': fSalePriceNum,
            'optionid': fOptionID,
            'qty': fQty
        }, function (data) {
            if (data == 'OK') {
                loadCart();
                $.fancybox.close();
            } else {
                alert(data);
            }
    });
}

function loadCart() {
	loaderAction('start');
	$.get(window.subURL+'loadcart', function(data) {
		data = parseInt(data);
		if (data == 1) {
			fMsg = 'There is 1 item in your cart';	
		} else {
			fMsg = 'There are ' + data + ' items in your cart';	
		}
		$('#cartSummary').html(fMsg);
		
		if (data > 0) {
			$('#checkoutBox').fadeIn(300);
		} else {
			$('#checkoutBox').fadeOut(300);
		}
		loaderAction('stop');
	});
}

function getCartID() {
	$.get(window.subURL+'getcartid', function(data) {
		window.cartid = data;
	});
}

function checkOut(fPageID) {
	loaderAction('start');
	if (fPageID == undefined) {
		fPageID = 0;
	}
	
	if (fPageID == 0) {
		viewCart();
	} else {
		$.get(window.subURL + 'checkout&linkid='+fPageID, function(data) {
			fancyboxDisplay(data, function() {
				loaderAction('stop');
				processFancy();
			});
		});
	}
}

function emptyCart() {
	loaderAction('start');
	$.get(window.subURL+'emptycart', function(data) {
		if (data == 'OK') {
			loadCart();
			loaderAction('stop');
		}
	});
}

function viewCart() {
	loaderAction('start');
	$.get(window.subURL + 'viewcart', function(fData) {
		fancyboxDisplay(fData, function() {
			loaderAction('stop');
		});
	});
}

function processOrder(fPageID, fArray) {
}

function removeFromCart(fID) {
	fAction = 'removefromcart';
	$('#cartViewerTR'+fID).remove();
	$.get(window.subURL + fAction + '&linkid=' + fID, function (data) {
		if (data != 'OK') {
			alert(data);
		} else {
			try {
				removeFromCartCallback(fID);
			} catch (e) {
			}
			viewCart();

		}
	});
}

function processFancy() {
	$('.remote-button').each(function() {
		transformButton(this);
		$(this).removeClass('remote-button');
	});

	$('.remote-input').each(function() {
		$(this).addClass('border');
		$(this).addClass('shadow');
		$(this).css({
			'width': 200,
			'padding': '5px'
		});
		$(this).removeClass('remote-input');
	});

	$('.remote-textarea').each(function() {
		$(this).addClass('border');
		$(this).addClass('shadow');
		$(this).css({
			'width': 400,
			'padding': '5px'
		});
		$(this).removeClass('remote-textarea');
	});

	$('.remote-select').each(function() {
		$(this).addClass('border');
		$(this).addClass('shadow');
		$(this).css({
			'padding': '5px'
		});
		$(this).removeClass('remote-select');
	});
}

/* Checkout Form Functions */
function updateOrder(fNext) {
	loaderAction('start');

	fPageID = $('#PAGEID').val();
	if (fPageID == '' || fPageID == undefined) {
		fPageID = 0;
	}
	fPageID = parseInt(fPageID);
	
	if (fNext == undefined || fNext == '') {
		fNext = 'next';
	}
	
	if (fNext == 'next') {
		fNextPageID = fPageID + 1;
	} else {
		fNextPageID = fPageID - 1;
	}

	if (!isShipping() && fNextPageID == 2) {
		if (fNext == 'next') {
			fNextPageID++;
		} else {
			fNextPageID--;
		}
	}
	
	if (fNext == 'process') {
		fAction = 'processorder';
	} else {
		fAction = 'updateorder';
	}
	
	fArray = $('#checkoutForm').serializeArray();
	$.post(window.subURL + fAction + '&linkid='+fPageID, fArray, function(fData) {
		if (fData == 'OK') {
			checkOut(fNextPageID);
		} else if (fData == 'APPROVED') {
			showInvoice();
		} else if (fData == 'PENDING') {
			showPending();

		} else {
			alert('ERROR:\nACTION: '+fAction+'\nPAGEID: '+fPageID+'\n'+fData);
		}
	});
	
}

function checkForm() {
	fStatus = true;
	$('.input-required').each(function() {
		fVal = $(this).val();
		if (fVal == '') {
			$(this).addClass('input-warning');
			if (fStatus) {
				$(this).focus();
			}
			fStatus = false;
		} else {
			$(this).removeClass('input-warning');
		}
	});
	
	if (!fStatus) {
		alert('Sorry, you missed some important information');
	}
	
	return fStatus;
	
}

function checkCC() {
	if (CheckCardNumber()) {
		updateOrder('process');
	} else {
	}
}

function showInvoice() {
	loaderAction('start');
    $.get(window.subURL + 'invoice', function (fData) {
		fancyboxDisplay(fData, function() {
                processFancy();
                etcResetCart();
                loadCart();
            }, function() {
                location.href = window.browserURL;
            }
        );
    });

}

function showPending() {
	loaderAction('start');
	$.get(window.subURL + 'pending', function (fData) {
		fancyboxDisplay(fData, function () {
			processFancy();
			etcResetCart();
			loadCart();
		});
	});
}

function addDonationToCart() {
	loaderAction('start');
	fAmount = $('#donation-amount').val();
    $.post(window.subURL + 'addDonationToCart', {'AMOUNT': fAmount}, function (data) {
		if (data == 'OK') {
			viewCart();
		} else {
			alert(data);
		}
		loaderAction('stop');

	});
		
}

/* PRODUCT CATEGORIES */
function loadProductCategoryList() {
    $.get(window.subURL + 'loadCategoriesUL', function (data) {
		$('#catsbox').html(data);
	});
}

function openProductCategory(fID) {
	loaderAction('start');
	$('.cat-li').removeClass('cat-selected');
	$('.cat-li-'+fID).addClass('cat-selected');
	$.get(window.subURL + 'loadProductCategory&linkid=' + fID, function (data) {
		$('#category-products-view').html(data);
		ui();
		$.fancybox.close();
	});
}

/* CC */
function checkCC() {
    $('#ccexpy').addClass('input-cc');
    $('#ccexpm').addClass('input-cc');
    $('#cardType').addClass('input-cc');

    fStatus = true;
    $('.input-cc').each(function () {
        fVal = $(this).val();
        if (fVal == 0 || fVal == '') {
            fStatus = false;
            $(this).addClass('input-warning');
        } else {
            $(this).removeClass('input-warning');
        }
    });
    if (fStatus) {
        fMsg = 'Are you sure you wish to apply this payment to your credit card?';
        if (confirm(fMsg)) {
            updateOrder('process');
        }
    }
}
