// This file contains some routines for showing some dialog boxes
// These dialog boxes are NOT meant to be general, but is more customized to this site.
// Assumes jQuery, and login.js has been loaded.
// Assuming the messages are defined

function set_to_screen_center(e) {
  // set the element to screen center
  e.css('top',$(window).height()/2);
  e.css('left',$(window).width()/2);
  e.css('margin-top', -(e.height()/2));
  e.css('margin-left', -(e.width()/2));
}
///////////////
//// Alert Box
// the click handler for the alert box
$(function() {
    $('#gainbid_alert_ok').click(function(event){
      event.preventDefault();
      $('#gainbid_alert_box').hide(0);
      $('#gainbid_alert_overlay').hide(0);
    });
  });
function gainbid_alert(title,msg) {
  // Displays the (global) alert box, setting the title and message
  $('#gainbid_alert_title').html(title);
  $('#gainbid_alert_msg').html(msg);
  // re-adjust the position to make it center
  var box = $('#gainbid_alert_box');
  set_to_screen_center(box);
  $('#gainbid_alert_overlay').show(0);
  box.show(0);
}
///////////////

///////////////
//// Login Box
// the click handler for the login box
$(function() {
    $('#gainbid_login_cancel').click(function(event){
      event.preventDefault();
      $('#login_box_popup').hide(0);
      $('#gainbid_login_overlay').hide(0);
    });
    var pop_login_handler = login_handler('_popup');
    $('#login_button_popup').click(pop_login_handler);
    $('#login_form_popup').submit(pop_login_handler);
  });
function gainbid_login() {
  // Displays the (global) login box
  // re-adjust the position to make it center
  var box = $('#login_box_popup');
  set_to_screen_center(box);
  $('#gainbid_login_overlay').show(0);
  box.show(0);
}
///////////////

///////////////
//// Buy Bid Box
// the click handler for the buy bid box
///////////////
function gainbid_buybid() {
  // Displays the (global) buybid box
  // re-adjust the position to make it center
  var box = $('#buybid_box_popup');
  set_to_screen_center(box);
  $('#gainbid_buybid_overlay').show(0);
  box.show(0);
}
function show_buybid_msg(msg, is_error) {
  $('#buybid_popup_msg').attr('class',is_error ? 'error-msg' : 'okay-msg');
  $('#buybid_popup_msg').html(msg);
  $('#buybid_popup_msg').show(0);
}
function gainbid_buy_handler(event) {
  //
  var buya = $('input:radio[name=gainbid_package_radio]:checked').val();
  event.preventDefault();
  if(isNaN(buya)) {
    show_buybid_msg(g_dialog_msgs['pick_package'], true);
    return false;
  }
  $.ajax({
      type: 'GET', 
      url: 'buybids.php',
      data: {a: buya, b:$(location).attr('href')},
      dataType: 'json',
      timeout: 5000,
      success: function(ret) {
	if(ret['status'] == 'OK') {
	 show_buybid_msg(g_dialog_msgs['connecting_to_paypal'], false);
	 // call paypal, now in sandbox
	 // window.location.href = 'https://www.paypal.com/cgi-bin/webscr?' + jQuery.param({
	  window.location.href = 'https://www.sandbox.paypal.com/cgi-bin/webscr?' + jQuery.param({
		      cmd: '_xclick',
		      item_number: ret['inu'],
		      no_note: 1,
		      //business: '4DJ28QJ7WDX46',
		      business: 'MVU86CTUZPCWQ', // for testing in sandbox
		      currency_code: 'HKD',
		      item_name: ret['inm'],
		      amount: ret['amt']
			  });
	} else if(ret['status'] == 'login') {
	  gainbid_login();
	} else {
	  gainbid_alert(g_suggestions_msgs['network_err']);
        }
      },
      error: function() {gainbid_alert(g_suggestions_msgs['network_err']);}
	});
  return false;
}
$(function() {
    $('#gainbid_buybid_cancel').click(function(event){
      event.preventDefault();
      $('#buybid_box_popup').hide(0);
      $('#gainbid_buybid_overlay').hide(0);
    });
    $('#buybid_popup_link').click(function(event) {
	event.preventDefault();
	gainbid_buybid();
      });
    $('#buybid_header_link').click(function(event) {
	event.preventDefault();
	gainbid_buybid();
      });
    $('input:radio[name=gainbid_package_radio]').change(function(){show_buybid_msg('',false);});
    $('#gainbid_buybid_buy').click(gainbid_buy_handler);
  });

///////////////
//// Register Box
// the click handler for the register box
function gainbid_register() {
  // Displays the (global) login box
  // re-adjust the position to make it center
  var box = $('#reg_box_popup');
  set_to_screen_center(box);
  $('#gainbid_register_overlay').show(0);
  box.show(0);
}
function register_show_err(msg) {
  $('#reg_box_msg').html(msg);
}
//
function reg_check_user_id_OK() {
  // returns false if not OK
  var user_id = $('#tbUserID_reg').val();
  if(user_id.length <= 0) {
    register_show_err(g_dialog_msgs['user_id_required']);
    $('#reg_box_user_id_err').show(0);
    return false;
  }
  if(! user_id.match(/^\w+$/)) {
    register_show_err(g_dialog_msgs['user_id_wrong']);
    $('#reg_box_user_id_err').show(0);
    return false;
  }
  $('#reg_box_user_id_err').hide(0);
  register_show_err('');
  return true;
}
function reg_check_first_name_OK() {
  // returns false if not OK
  var first_name = $('#tbFirstName_reg').val();
  if(first_name.length <= 0) {register_show_err(g_dialog_msgs['first_name_required']); return false;}
  register_show_err('');
  return true;
}
function reg_check_last_name_OK() {
  // returns false if not OK
  var last_name = $('#tbLastName_reg').val();
  if(last_name.length <= 0) {register_show_err(g_dialog_msgs['last_name_required']); return false;}
  register_show_err('');
  return true;
}
function reg_check_email_OK() {
  // returns false if not OK
  var email_addr = $('#tbEmailAddress_reg').val();
  if(email_addr.length <= 0) {register_show_err(g_dialog_msgs['email_required']); return false;}
  if(! email_addr.match(/^[\w\.\_\-]+@[\w\.\_\-]+$/)) {register_show_err(g_dialog_msgs['email_wrong']);return false;}
  register_show_err('');
  return true;
}
function reg_check_postal_OK() {
  // returns false if not OK
  var postal_addr = $('#tbPostalAddress_reg').val();
  if(postal_addr.length <= 0) {register_show_err(g_dialog_msgs['postal_required']); return false;}
  register_show_err('');
  return true;
}
function reg_check_password_OK() {
  // returns false if not OK
  var password = $('#tbPassword_reg').val();
  if(password.length <= 0) {
    register_show_err(g_dialog_msgs['password_required']);
    $('#reg_box_pw_ok').hide(0);
    return false;
  }
  $('#reg_box_pw_ok').show(0);
  register_show_err('');
  return true;
}
function reg_check_re_password_OK() {
  // returns false if not OK
  var re_password = $('#tbRePassword_reg').val();
  if(re_password.length <= 0) {
    register_show_err(g_dialog_msgs['confirm_password']);
    $('#reg_box_repw_ok').hide(0);
    return false;
  }
  if($('#tbPassword_reg').val() != re_password) {
    register_show_err(g_dialog_msgs['password_wrong']);
    $('#reg_box_repw_ok').hide(0);
    return false;
  }
  $('#reg_box_repw_ok').show(0);
  register_show_err('');
  return true;
}
function reg_check_mobile_OK() {
  // returns false if not OK
  var mobile = $('#tbMobile_reg').val();
  if(mobile.length <= 0) {register_show_err(g_dialog_msgs['mobile_required']); return false;}
  if(! mobile.match(/^(\([\d\-]+\))?[\d\-]+$/)) {register_show_err(g_dialog_msgs['mobile_wrong']); return false;}
  register_show_err('');
  return true;
}
function register_handler(event) {
  // do sanity check of the values, then submit to the server
  var user_id = $('#tbUserID_reg').val();
  var first_name = $('#tbFirstName_reg').val();
  var last_name = $('#tbLastName_reg').val();
  var email_addr = $('#tbEmailAddress_reg').val();
  var postal_addr = $('#tbPostalAddress_reg').val();
  var password = $('#tbPassword_reg').val();
  var re_password = $('#tbRePassword_reg').val();
  var mobile = $('#tbMobile_reg').val();
  //
  event.preventDefault();
  // simple checking
  if(!(reg_check_user_id_OK() && reg_check_first_name_OK() && reg_check_last_name_OK()
       && reg_check_email_OK() && reg_check_postal_OK() && reg_check_password_OK()
       && reg_check_re_password_OK() && reg_check_mobile_OK())) {
      return false;
  }
  // seems OK, make the request
  $.ajax({
      type: 'POST', 
      url: 'register.php',
      data: {
	user: user_id,
        fname: first_name,
        lname: last_name,
        email: email_addr,
        addr: postal_addr,
        pw: password,
        repw: re_password,
        mo: mobile
      },
      dataType: 'text',
      timeout: 5000,
      success: function(msg) {
	if(msg == 'OK') {
	  $('#reg_box_popup').hide(0);
	  $('#gainbid_register_overlay').hide(0);
	  gainbid_alert(g_dialog_msgs['success'],g_dialog_msgs['account_created']);
	} else {
	  register_show_err(g_dialog_msgs['err_create_account']);
        }
      },
      error: function() {register_show_err(g_dialog_msgs['err_send_form']);}
	});
  return false;
}
//
$(function() {
    $('#reg_box_cancel').click(function(event){
      event.preventDefault();
      $('#reg_box_popup').hide(0);
      $('#gainbid_register_overlay').hide(0);
    });
    $('#reg_box_submit').click(register_handler);
    $('#reg_box_form').submit(register_handler);
    // the change handler for the register form
    $('#tbUserID_reg').change(reg_check_user_id_OK);
    $('#tbFirstName_reg').change(reg_check_first_name_OK);
    $('#tbLastName_reg').change(reg_check_last_name_OK);
    $('#tbEmailAddress_reg').change(reg_check_email_OK);
    $('#tbPostalAddress_reg').change(reg_check_postal_OK);
    $('#tbPassword_reg').change(reg_check_password_OK);
    $('#tbRePassword_reg').change(reg_check_re_password_OK);
    $('#tbMobile_reg').change(reg_check_mobile_OK);
    //
    $('.reg_box_link').click(function(event){
      event.preventDefault();
      gainbid_register();
    });
  });
///////////////

///////////////
//// Forgot Password Box
// the click handler
function gainbid_forgot_password() {
  // Displays the (global) forgot password box
  // re-adjust the position to make it center
  var box = $('#forgot_password_box');
  set_to_screen_center(box);
  $('#gainbid_forgot_password_overlay').show(0);
  box.show(0);
}
function forgot_password_show_err(msg) {
  $('#forgot_password_box_msg').html(msg);
}
function forgot_password_check_email_OK() {
  // returns false if not OK
  var email_addr = $('#email_forgot_password').val();
  if(email_addr.length <= 0) {forgot_password_show_err(g_dialog_msgs['email_required']); return false;}
  if(! email_addr.match(/^[\w\.\_\-]+@[\w\.\_\-]+$/)) {forgot_password_show_err(g_dialog_msgs['email_wrong']);return false;}
  forgot_password_show_err('');
  return true;
}
function forgot_password_handler(event) {
  // do sanity check of the values, then submit to the server
  var email_addr = $('#email_forgot_password').val();
  //
  event.preventDefault();
  // simple checking
  if(!forgot_password_check_email_OK()) {return false;}
  // seems OK, make the request
  $.ajax({
      type: 'POST', 
      url: 'forgot_password.php',
      data: {
        email: email_addr
      },
      dataType: 'text',
      timeout: 3000,
      success: function(msg) {
	if(msg == 'OK') {
	  $('#forgot_password_box').hide(0);
	  $('#gainbid_forgot_password_overlay').hide(0);
	  gainbid_alert(g_dialog_msgs['validation_sent'],g_dialog_msgs['validation_pw_sent']);
	} else {
	  forgot_password_show_err(g_dialog_msgs['err_reset_pw'] + msg);
        }
      },
      error: function() {forgot_password_show_err(g_dialog_msgs['err_send_form']);}
	});
  return false;
}

//
$(function() {
    $('#forgot_password_cancel').click(function(event){
      event.preventDefault();
      $('#forgot_password_box').hide(0);
      $('#gainbid_forgot_password_overlay').hide(0);
    });
    $('#forgot_password_submit').click(forgot_password_handler);
    $('#forgot_password_form').submit(forgot_password_handler);
    // the change handler for the register form
    $('#email_forgot_password').change(forgot_password_check_email_OK);
    //
    $('.forgot_password_link').click(function(event){
      event.preventDefault();
      gainbid_forgot_password();
    });
  });
///////////////

