// JavaScript Document

function check_login(form) {
   ajax = new class_ajax();
   ajax.set_response_action(function() {
		if (ajax.get_response_text() == '0') {
			var yes_function = null;
			if(window.location.href.indexOf('checkout_s1') != -1)
				yes_function = function() { toggle_account_table(); document.getElementById('civility1').focus(); }
			else
				yes_function = function() { document.location.href = INCPATH +'users/register.php?email='+ form.email.value; }
			new question_box(confirm_new_account, '', button_yes, yes_function, button_no, null);
		} else if (ajax.get_response_text() == '-1') {
			yes_function = function() { 
				form.password.value = '';
				form.password.className = 'text_invalid';
				form.password.focus();
         }
			new question_box(wrong_password, '', button_yes, yes_function, null, null);
		} else if (ajax.get_response_text() == '2') {
			/* BPA Esta comprobación es por el BPA */
			window.location.href = INCPATH + 'basket/index.php?item=BP01';
			/* BPA */
		} else if ((ajax.get_response_text() == 1) && (form.referer)) {
			window.location.href = form.referer.value;
		} else if ((ajax.get_response_text() == 1) && (window.location.href.indexOf('checkout') == -1)) {
			window.location.href = INCPATH +'users/login.php';
		} else if ((ajax.get_response_text() == 1) && (window.location.href.indexOf('checkout') != -1)) {
			window.location.href = INCPATH +'basket/checkout_s1_user.php';
		}
	});
   ajax.query_form(form, INCPATH +'users/admin_users.php');
}

// Checklogin
function check_login_top_panel(form) {
	fc = new FormChecker2;
	p = new Array(
		new Array('email', fcTxtEmail(), FC_EMAIL),
		new Array('password', fcTxtPassword())
	);
	// Hacemos un chequeo previo
	if(form.email.value == fcTxtEmail()) form.email.value = "";
	if(form.password.value == fcTxtPassword()) form.password.value = "";
	
	if (fc.checkForm(p, form))
	{
	   ajax = new class_ajax();
	   ajax.set_response_action(function() {
			if (ajax.get_response_text() == '0') {	// Usuario no encontrado
				yes_function = function() { document.location.href = INCPATH +'users/register.php?email='+ form.email.value; }
				new question_box(fcTxtConfirmNewAccount(), '', fcTxtBttnYes(), yes_function, fcTxtBttnNo(), null);
			} else if (ajax.get_response_text() == '-1') {	// Contraseña incorrecta
				yes_function = function() { 
					form.password.value = '';
					form.password.className = 'text_invalid2';
					form.password.focus();
			 }
				new question_box(fcTxtWrongPassword(), '', fcTxtBttnYes(), yes_function, null, null);
			} else if (ajax.get_response_text() == '2') {	// Controlamos la compra de un BPA si ya tenía otro
				/* BPA Esta comprobación es por el BPA */
				window.location.href = INCPATH + 'basket/index.php?item=BP01';
				/* BPA */
			} else if ((ajax.get_response_text() == 1) && (form.referer)) {	// Login OK y se indica la página de destino
				window.location.href = form.referer.value;
			} else if ((ajax.get_response_text() == 1) && (window.location.href.indexOf('checkout') != -1)) {	// Login OK y se envía a la página de selección de direcciones para completar el proceso de compra
				window.location.href = INCPATH +'basket/checkout_s1_user.php';
			} else if ((ajax.get_response_text() == 1) && (window.location.href.indexOf('checkout') == -1)) {	// Login OK desde cualquier otra página que no sea del proceso de compra
				jQuery("#account_logged_email").text(form.email.value);
				jQuery("#panel_account").hide();
				jQuery("#tab_account").hide();
				jQuery("#account_logged").show();
				
				form.email.value = "";
				form.password.value = "";
			}
		});
	   ajax.query_form(form, INCPATH +'users/admin_users.php');
	}
}

// Checklogout
function check_logout_top_panel() {
   ajax = new class_ajax();
   ajax.set_vars_string('action=logout');
   ajax.set_response_action(function() {
	   if(window.location.href.indexOf('users/index.php') != -1 || window.location.href.indexOf('users/account.php') != -1|| window.location.href.indexOf('users/address.php') != -1 || window.location.href.indexOf('users/billing.php') != -1 || window.location.href.indexOf('users/orders_history.php') != -1)
	      window.location.href = INCPATH + 'users/login.php';
       else
	   {
		   jQuery("#account_logged").hide();
		   jQuery("#tab_account").show();
	   }
   });
   ajax.query(INCPATH +'users/admin_users.php');
}


function check_account_data(form) {
   ajax = new class_ajax();
   isBIO = false;
   if(arguments.length > 1) isBIO = true;
   ajax.set_response_action(function() {
		if (ajax.get_response_text() == '-1') {
			yes_function = function() { window.location.href = INCPATH +'users/fgtnpwd.php?email=' + form.email.value; }
			if (window.location.href.indexOf('checkout_s3.php') != -1) {
				no_function = function() { if(isBIO) basket_process_orderBIO(document.forms['order_data']); else basket_process_order(document.forms['order_data']); }
				new question_box(user_exists, '', button_yes, yes_function, button_no, no_function);
			} else new question_box(user_exists, '', button_yes, yes_function, button_no, null);
		} else if(window.location.href.indexOf('checkout_s1.php') != -1) {
			yes_function = function () { window.location.href = INCPATH + 'basket/checkout_s1_user.php?reg_basket='; }
			question_box(account_created, "", fcMsgOkAccountRegBasket(), yes_function, null, null);
		} else if (window.location.href.indexOf('account.php') != -1) {
			if (ajax.get_response_text() == '1') {
				window.location.href = INCPATH +'users/index.php';
			}
		} else if (window.location.href.indexOf('register.php') != -1)  {
			if (ajax.get_response_text() == '1') {
				yes_function = function () { window.location.href = INCPATH + 'users/index.php?email=' + form.email.value; }
				question_box(account_created, "", 'OK', yes_function, null, null);
			}
		} else if (window.location.href.indexOf('checkout_s3.php') != -1)  {
			if (ajax.get_response_text() == '1') { if(isBIO) { basket_process_orderBIO(document.forms['order_data']); } else { alert("2" + arguments.length); basket_process_order(document.forms['order_data']); } }
		} else if (ajax.get_response_text() != '1') new info_box('Error', '');
	});
	ajax.query_form(form, INCPATH +'users/admin_users.php');
}


function user_fgtnpwd(form) {
   ajax = new class_ajax();
   ajax.set_response_action(function() {
		if (ajax.get_response_text() != 'true') info_box(nouser_message, '');
		else {
			yes_function = function () { window.location.href = INCPATH + 'users/login.php'; }
			question_box(mail_sent_message, '', 'OK', yes_function, null, null);
		}
	});
   ajax.query_form(form, INCPATH +'users/fgtnpwd.php');
}


function delete_user() {
	yes_function = function() {
		ajax = new class_ajax();
		ajax.set_vars_string('action=delete_user');
		ajax.set_response_action(function() {
			window.location.href = INCPATH +'users/login.php?logout';
		});
		ajax.query(INCPATH +'users/admin_users.php');
	}
	new question_box(msg_delete_user, '', button_yes, yes_function, button_no, null);
}


function save_address_data(form) {
   ajax = new class_ajax();
   ajax.set_response_action(function() {
		if (ajax.get_response_text() != 'true') {
			info_box(no_save_address, '');
		} else if (window.location.href.indexOf('address.php') != -1) {
			window.location.href = INCPATH +'users/index.php';
		}
	});
   ajax.query_form(form, INCPATH +'users/admin_users.php');
}


function delete_address_data(address) {
	yes_function = function() { window.location.href = INCPATH +'users/address.php?delete='+ address; }
	new question_box(msg_delete_address, '', button_yes, yes_function, button_no, null);
}


function save_billing_data(form) {
   ajax = new class_ajax();
   ajax.set_response_action(function() {
		if (ajax.get_response_text() != 'true') {
			info_box(no_save_address, '');
		} else if (window.location.href.indexOf('billing.php') != -1) {
			window.location.href = INCPATH +'users/index.php';
		}
	});
   ajax.query_form(form, INCPATH +'users/admin_users.php');
}


function delete_billing_data(billing) {
	yes_function = function() { window.location.href = INCPATH +'users/billing.php?delete='+ billing; }
	new question_box(msg_delete_billing, '', button_yes, yes_function, button_no, null);
}


function change_status_subscription(checkbox) {
		ajax = new class_ajax();
		if (checkbox.checked == true) ajax.set_vars_string('action=subscribe&email=' + checkbox.value);
		else ajax.set_vars_string('action=unsubscribe&email=' + checkbox.value);
		ajax.query(INCPATH +'newsletter/admin_subscriptions.php');
}

