/**
 * @author ray.zhang
 */

/* Cart Object */

var Cart = {
	version: '1.0.0',
	
  	doRequest: function(_url , _callback , _data) {
		new jQuery.ajax({
			url: _url,
			type: 'post',
			data: _data,
			success: _callback
		});
		return false;
	},
	
	refreshWin: function(){
		window.location.reload();
	},
	
	updateCartCount : function(result){
		var data = eval('(' + result + ')');
		if (data.result == "ok"){
			jQuery('#cart_count').html(data.count);
			jQuery('#message').html("Item added to Cart. Please click <strong>Proceed to Checkout</strong> to complete your order!");
		}
		else {
			jQuery('#message').html(data.msg);
		}
	},
	
	checkoutRedirect: function(result){
		var data = eval('(' + result + ')');
		if (data['referred']){
			window.location = "../store/checkout.php?step=2&code=" + data['referred'];
		}
		else {
			window.location = "../store/checkout.php?step=2";
		}
	},
	
	confirmAddCheckoutItem: function(msg, type, id){
		if (confirm(msg, 3 , 4)){
			Cart.addAndCheckoutItem(type, id);
			return true;
		}
		return false;
	},
	
	addAndCheckoutItem: function(type, id){
		if (id == '') {
			return false;
		}
		params = "task=add_" +  type + "&id=" + id;
		return Cart.doRequest("../store/actCart.php", Cart.checkoutRedirect, params);
	},
	
	adwordsCheckoutItem: function(type, id, referred){
		if (id == '') {
			return false;
		}
		params = "task=add_" +  type + "&id=" + id + "&referred=" + referred;
		return Cart.doRequest("../store/actCart.php", Cart.checkoutRedirect, params);
	},
	
	confirmAddItem: function(msg, type, id){
		if (confirm(msg, 3 , 4)){
			Cart.addItem(type, id);
			return true;
		}
		return false;
	},
	
	addItem: function(type , id){
		params = "task=add_" +  type + "&id=" + id;
		/*if (type == 'bundle'){
			return Cart.doRequest("actCart.php", Cart.bundleRedirect, params);
		}*/
		return Cart.doRequest("actCart.php" , Cart.updateCartCount , params);	
	},
	
	deleteItem: function(type, id){
		params = "task=delete_" +  type + "&id=" + id;
		return Cart.doRequest("actCart.php" , Cart.refreshWin , params);	
	},
	
	ownGame: function(name){
		var div_msg = jQuery('#message');
		div_msg.html("You already own <strong>" + name + "</strong>. Please select another game!");
		return false;	
	}
}

var Store = {
	version: "1.0.0",
	win_legal: null,
	
	redirect: function(_url){
		window.location = _url;
	},
	
	doTask: function(_task){
		jQuery('#task').val(_task);
		return true;
	},
	
	checkEmpty: function(tag_name , message){
		tag = jQuery('#' + tag_name);
		if (jQuery.trim(tag.val()) == ""){
			alert(message);
			tag.focus();
			return false;
		}
		return true;
	},
	
	checkZero: function (tag_name , message){
		tag = jQuery('#' + tag_name);
		if (tag.val() == 0){
			alert(message);
			tag.focus();
			return false;
		}
		return true;
	},
	
	checkValidEmail: function (tag_name , message){
		tag = jQuery('#' + tag_name);
		str = tag.val();
		if (!str.match(/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i)){
			alert(message);
			tag.focus();
			return false;
		}
		return true;
	},
	
	openLegalDialog: function(){
		Store.win_legal = window.open("legal.html","legal","toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,height=400, resizable=yes,width=450,top=50,left=300");
	},
	
	openLegal2Dialog: function(){
		Store.win_legal = window.open("legal2.html","legal","toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,height=280, resizable=yes,width=450,top=50,left=300");
	},
	
	checkPaymentOption: function(){
		var options = document.getElementsByName("payment");
		var opt_delivered = document.getElementsByName("delivered");
		var is_empty = true;
	
		if (Store.win_legal && !Store.win_legal.closed) {
			Store.win_legal.close();	
		}
	
		if(!document.formPayment.accept.checked)
		{
			alert("Please tick the checkbox to confirm that you have read and accept our 'terms and conditions' of sale.");
			return false;
		}
		for (next = 0 , is_empty = true; next < opt_delivered.length ; next ++){
			option = opt_delivered[next];
			if (option.checked == true && option.type == "radio") {
				is_empty = false;
			}
		}
		if (is_empty && opt_delivered.length > 0) {
			alert("You should select one hand delivered option.");
			return false;
		}
	
		for (next = 0 , is_empty = true; next < options.length ; next ++ ) {
			option = options[next];
			if (option.checked == true && option.type == "radio") {
				is_empty = false;
			}
		}
		if (is_empty) {
			alert("You should select one payment option.");
			return false;
		}
		return true;
	},
	
	checkBillingAddress: function(){
		if (!this.checkEmpty("FIRSTNAME" , "Please enter your first name.")){
			return false;
		}
		if (!this.checkEmpty("LASTNAME" , "Please enter your last name.")){
			return false;
		}
		if (!this.checkEmpty("EMAIL" , "Please enter your email address.")){
			return false;
		}
		if (!this.checkValidEmail("EMAIL" , "Email address is invalid.")){
			return false;
		}
		if (!this.checkEmpty("PHONE" , "Please enter a telephone number.")){
			return false;
		}
		if (!this.checkEmpty("ADDRESS1" , "Please enter your billing address.")){
			return false;
		}
		if (!this.checkEmpty("CITY" , "City is required.")){
			return false;
		}
		if (!this.checkEmpty("ZIP" , "Please enter your Zip or postal code.")){
			return false;
		}
		if (!this.checkEmpty("COUNTRY" , "Country is required.")){
			return false;
		}
		if ($('#COUNTRY').val() == "US" || $('#COUNTRY').val() == "CA"){
			$('#STATE').val($('#sel_state').val());
		}
		else {
			$('#STATE').val($('#txt_state').val());
		}
		if (!this.checkEmpty("STATE" , "State is required.")){
			return false;
		}
		return true;
	},
	
	checkShippingAddress: function(){
		if (!this.checkEmpty("SHIPPING_FIRSTNAME" , "Please enter the shipping first name.")){
			return false;
		}
		if (!this.checkEmpty("SHIPPING_LASTNAME" , "Please enter the shipping last name.")){
			return false;
		}
		if (!this.checkEmpty("SHIPPING_ADDRESS1" , "Please enter the shipping address.")){
			return false;
		}
		if (!this.checkEmpty("SHIPPING_CITY" , "The shipping city is required.")){
			return false;
		}
		if (!this.checkEmpty("SHIPPING_COUNTRY" , "The shipping country is required.")){
			return false;
		}
		if ($('#SHIPPING_COUNTRY').val() == "US" || $('#SHIPPING_COUNTRY').val() == "CA"){
			$('#SHIPPING_STATE').val($('#shipping_sel_state').val());
		}
		else {
			$('#SHIPPING_STATE').val($('#shipping_txt_state').val());
		}
		if (!this.checkEmpty("SHIPPING_STATE" , "Please enter the shipping state.")){
			return false;
		}
		if (!this.checkEmpty("SHIPPING_ZIP" , "Please enter the shipping zip or postal code.")){
			return false;
		}
		return true;
	},
	
	checkCreditCard:function(){
		if ($('#cc_type') == null){
			return true;
		}
		
		if (!this.checkZero("cc_type" , "Please select your card type.")){
			return false;
		}
		if (!this.checkEmpty("cc_number" , "Please enter your credit card number.")){
			return false;
		}
		if (!this.checkEmpty("cvv2_number" , "Please enter your card verification number.  Your card verification number is the last 3 digits (or 4 digits for American Express) on the back of your credit card.")){	
			return false;
		}
		return true;
	},
	
	checkFalconAddress: function(){
		if (!this.checkBillingAddress()){
			return false;
		}
		if(document.formBuyer.ship_choice[1].checked){
			if (!this.checkShippingAddress()){
				return false;
			}
		}
		/*if (jQuery('#COUNTRY').val() != "US" && document.formBuyer.ship_choice[0].checked)
		{
			alert("Please enter a Shipping Address - must be in the USA.");
			document.formBuyer.ship_choice[1].checked = "checked";
			jQuery('div#shipping').show();
			return false;
		}*/
		if (!this.checkCreditCard()){
			return false;
		}
		return true;
	},
	
	checkGameAddress: function(){
		if (!this.checkBillingAddress()){
			return false;
		}
		if (!this.checkCreditCard()){
			return false;
		}
		return true;
	},
	
	checkAddress: function(_type){
		var result = false;
		if (_type == "falcon"){
			result = this.checkFalconAddress();
		}
		else if (_type == "game"){
			result = this.checkGameAddress();
		}
		if (result){
			$('form#formBuyer').submit();
		}
		return result;
	},
	
	makePayment: function (_type){
		if (_type == "1"){
			if (Store.win_legal && !Store.win_legal.closed) {
				Store.win_legal.close();	
			}
		
			if(!document.formOrder.accept.checked){
				alert("Please tick the checkbox to confirm that you have read and accept our 'terms and conditions' of sale.");
				return false;
			}
		}
		
		jQuery('#make_payment').width(0);
		jQuery("#payment_tip").text("Payment processing.  Please wait....");
		Store.doTask('make_payment');
		return true;
	},
	
	showShippingInfo: function(_show){
		var shipping = jQuery("#shipping");
		if (_show) {
			$('#shipping_amount1').html("");
			shipping.show();
		}
		else {
			$('#shipping_amount2').html("");
			shipping.hide();
		}
	},
	
	getShippingPrice : function (){
		var _country = "";
		var _state = "";
		var choice = "shipping";
		if ($('#ship_choice_1').is(':checked')){
			choice = "billing";
		}
		
		if (choice == "shipping" && this.name == "COUNTRY"){
			return;
		}
		if (choice == "billing" && this.name == "SHIPPING_COUNTRY"){
			return;
		}
				
		if (choice == "billing"){
			_country = $('#COUNTRY').val();
			_state   = $('#sel_state').val();
		}
		else {
			_country = $('#SHIPPING_COUNTRY').val();
			_state   = $('#shipping_sel_state').val();
		}
		if (_country){
			$.post("../store/actStore.php", {country: _country, state: _state, task:'get_shipping_fee'}, Store.updateShippingPrice, "json");
		}
	},
	
	updateShippingPrice: function (data){
		$('#shipping_amount1').html("");
		if (data.result == "success" ){
			if ($('#ship_choice_1').is(':checked')){
				$('#shipping_amount1').html(data.message);
			}
			else {
				$('#shipping_amount2').html(data.message);
			}
		}
	},
	
	checkReferralCode: function(){
		var code = $('input#referral_code').val();
		var amount = $('input#total_amount').val();
		if ($.trim(code) != ""){
			$.post("../store/actStore.php", {code:code, amount:amount, task:'check_referrral'}, Store.updateReferralResult, "json");
		}
		return false;
	},
	
	updateReferralResult: function (data){
		if (data.result == "success"){
			$('#referral_msg').html(data.message);
		}
		else {
			$('#referral_msg').html(data.message);
			$('input#referral_code').val('');
		}
	},
	
	toggleStateArea: function (){
		var billing_country = $('#COUNTRY').val();
		var shipping_country = $('#SHIPPING_COUNTRY').val();
		if (billing_country == "US" || billing_country == "CA"){
			$(".billing_us_state_area").show();
			$(".billing_other_state_area").hide();
		}
		else {
			$("#txt_state").val("");
			$(".billing_us_state_area").hide();
			$(".billing_other_state_area").show();
		}
		if (shipping_country == "US" || shipping_country == "CA"){
			$(".shipping_us_state_area").show();
			$(".shipping_other_state_area").hide();
		}
		else {
			$('#shipping_txt_state').val("");
			$(".shipping_us_state_area").hide();
			$(".shipping_other_state_area").show();
		}
	},
	
	countryChange: function (){
		Store.toggleStateArea();
		Store.getShippingPrice();
	}
	
}

var PreviewWindow = {
	id: 0,
	show: function(id){
		$("#" + id).show();	
	},
	move: function(){
	},
	hide: function(id){
		$("#" + id).hide();
	},
	setEvent: function() {
		var div_lay = document.getElementsByTagName("div");
		for (next = 0 ; next < div_lay.length ; next ++){
			if (div_lay[next].className == "flay"){
				alert(div_lay[next].innerHTML);
			}
		}
	}
}

$(function(){
	$('#referral_apply a').click(Store.checkReferralCode);
	$('#formBuyer #COUNTRY').change(Store.countryChange);
	$('#formBuyer #sel_state').change(Store.getShippingPrice);
	$('#formBuyer #SHIPPING_COUNTRY').change(Store.countryChange);
	$('#formBuyer #shipping_sel_state').change(Store.getShippingPrice);
});

/*function toCheckout(){
	var store = new Store();
	store.redirect("checkout.php");
}

function toBuyGames(){
	var store = new Store();
	store.redirect("game.php");
}

function toBuyHardware(){
	var store = new Store();
	store.redirect("falcon.php");
}

function doTask(_task){
	var store = new Store();
	store.doTask(_task);
	return true;
}*/




