/* All */

var regex = /^([a-zA-Z0-9_\.\-\+'])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var isMSIE6 = /MSIE 6/i.test(navigator.userAgent);
var eu_country_codes = ["AUT", "BEL", "BGR", "CYP", "CZE", "DEU", "DNK", "ESP", "EST", "FIN", "FRA", "GBR", "GRC", "HUN", "IRL", "ITA", "LTU", "LUX", "LVA", "MLT", "NLD", "POL", "PRT", "ROU", "SWE", "SVK", "SVN"];

function registrationFormCheckEB() {
  fields = new Array();
  fields[0] = 'contact_first_name';
  fields[1] = 'contact_last_name';
  fields[2] = 'contact_email';
  fields[3] = 'contact_email_confirm';
  fields[4] = 'contact_telephone';
/*
  fields[5] = 'contact_address_building';
  fields[6] = 'contact_address_town';
  fields[7] = 'contact_address_postal_code';
  fields[8] = 'billing_first_name';
  fields[9] = 'billing_last_name';
  fields[10] = 'billing_email';
  fields[11] = 'billing_email_confirm';
  fields[12] = 'billing_telephone';
*/
  fields[5] = 'billing_address_building';
  fields[6] = 'billing_address_town';
  fields[7] = 'billing_address_postal_code';
  fields[8] = 'payment_type';
  fields[9] = 'how_heard';
  fields[10] = 'terms_and_conditions';

  var errors = new Array();
  errors[0] = ($('#contact_first_name').val() == '' ? 'Please enter your first name' : '');
  errors[1] = ($('#contact_last_name').val() == '' ? 'Please enter your last name' : '');
  errors[2] = (!regex.test($('#contact_email').val()) ? 'Email address does not appear to be valid' : '');
  errors[3] = ($('#contact_email_confirm').val() != $('#contact_email').val() ? 'Email addresses do not match' : '');
  errors[4] = (processPhoneNum('contact_telephone') ? 'Please enter a valid telephone number' : '');
/*
  errors[5] = ($('#contact_address_building').val() == '' ? 'Please enter address 1' : '');
  errors[6] = ($('#contact_address_town').val() == '' ? 'Please enter address 3' : '');
  errors[7] = ($('#contact_address_postal_code').val() == '' ? 'Please enter your post code' : '');
  errors[8] = ($('#billing_first_name').val() == '' ? 'Please enter the first name of your billing contact' : '');
  errors[9] = ($('#billing_last_name').val() == '' ? 'Please enter the last name of your billing contact' : '');
  errors[10] = (!regex.test($('#billing_email').val()) ? 'Email address for billing contact does not appear to be valid' : '');
  errors[11] = ($('#billing_email_confirm').val() != $('#billing_email').val() ? 'Billing email addresses do not match' : '');
  errors[12] = (processPhoneNum('billing_telephone') ? 'Please enter a valid telephone number for billing contact' : '');
*/
  errors[5] = ($('#billing_address_building').val() == '' ? 'Please enter address 1 for billing contact' : '');
  errors[6] = ($('#billing_address_town').val() == '' ? 'Please enter address 3 for billing contact' : '');
  errors[7] = ($('#billing_address_postal_code').val() == '' ? '%%16' : '');
  errors[8] = ($('#payment_type').val() == '' ? 'Please select payment type' : '');
  errors[9] = ($('#how_heard').val() == '' ? 'Please specify how you heard of us' : '');
  errors[10] = ((document.getElementById('terms_and_conditions').type == 'checkbox' && document.getElementById('terms_and_conditions').checked === false) ? 'Please read our terms and conditions and mark the checkbox as you agree' : '');

  return scanPopErrorBox(fields, errors);
}

function telesalesRegistrationFormCheckEB() {
  fields = new Array();
	fields[0] = 'contact_first_name';
  fields[1] = 'contact_last_name';
  fields[2] = 'contact_email';
  fields[3] = 'contact_email_confirm';
  fields[4] = 'contact_telephone';
  fields[5] = 'contact_address_building';
  fields[6] = 'contact_address_town';
  fields[7] = 'contact_address_postal_code';
  fields[8] = 'how_heard';
  fields[9] = 'terms_and_conditions';

  var errors = new Array();
	errors[0] = ($('#contact_first_name').val() == '' ? 'Please enter your first name' : '');
  errors[1] = ($('#contact_last_name').val() == '' ? 'Please enter your last name' : '');
  errors[2] = (!regex.test($('#contact_email').val()) ? 'Email address does not appear to be valid' : '');
  errors[3] = ($('#contact_email_confirm').val() != $('#contact_email').val() ? 'Email addresses do not match' : '');
  errors[4] = (processPhoneNum('contact_telephone') ? 'Please enter a valid telephone number' : '');
  errors[5] = ($('#contact_address_building').val() == '' ? 'Please enter address 1' : '');
  errors[6] = ($('#contact_address_town').val() == '' ? 'Please enter address 3' : '');
  errors[7] = ($('#contact_address_postal_code').val() == '' ? 'Please enter your post code' : '');
  errors[8] = ($('#how_heard').val() == '' ? 'Please specify how you heard of us' : '');
  errors[9] = ((document.getElementById('terms_and_conditions').type == 'checkbox' && document.getElementById('terms_and_conditions').checked === false) ? 'Please read our terms and conditions and mark the checkbox as you agree' : '');

  return scanPopErrorBox(fields, errors);
}

function accountDetailsFormCheckEB() {
  fields = new Array();
	fields[0] = 'contact_first_name';
  fields[1] = 'contact_last_name';
  fields[2] = 'contact_email';
  fields[3] = 'contact_telephone';
  fields[4] = 'contact_address_building';
  fields[5] = 'contact_address_town';
  fields[6] = 'contact_address_postal_code';
  fields[7] = 'billing_first_name';
  fields[8] = 'billing_last_name';
  fields[9] = 'billing_email';
  fields[10] = 'billing_telephone';
  fields[11] = 'billing_address_building';
  fields[12] = 'billing_address_town';
  fields[13] = 'billing_address_postal_code';

  var errors = new Array();
  errors[0] = ($('#contact_first_name').val() == '' ? 'Please enter your first name' : '');
  errors[1] = ($('#contact_last_name').val() == '' ? 'Please enter your last name' : '');
  errors[2] = (!regex.test($('#contact_email').val()) ? 'Email address does not appear to be valid' : '');
  errors[3] = (processPhoneNum('contact_telephone') ? 'Please enter a valid telephone number' : '');
  errors[4] = ($('#contact_address_building').val() == '' ? 'Please enter address 1' : '');
  errors[5] = ($('#contact_address_town').val() == '' ? 'Please enter address 3' : '');
  errors[6] = ($('#contact_address_postal_code').val() == '' ? 'Please enter your post code' : '');
  errors[7] = ($('#billing_first_name').val() == '' ? 'Please enter the first name of your billing contact' : '');
  errors[8] = ($('#billing_last_name').val() == '' ? 'Please enter the last name of your billing contact' : '');
  errors[9] = (!regex.test($('#billing_email').val()) ? 'Email address for billing contact does not appear to be valid' : '');
  errors[10] = (processPhoneNum('billing_telephone') ? 'Please enter a valid telephone number for billing contact' : '');
  errors[11] = ($('#billing_address_building').val() == '' ? 'Please enter address 1 for billing contact' : '');
  errors[12] = ($('#billing_address_town').val() == '' ? 'Please enter address 3 for billing contact' : '');
  errors[13] = ($('#billing_address_postal_code').val() == '' ? 'Please enter the post code for billing contact' : '');

  return scanPopErrorBox(fields, errors);
}

function updatePaymentDetailsFormCheckEB() {
  fields = new Array();
  fields[0] = 'contact_first_name';
  fields[1] = 'contact_last_name';
  fields[2] = 'contact_email';
  fields[3] = 'contact_telephone';
  fields[4] = 'contact_address_building';
  fields[5] = 'contact_address_town';
  fields[6] = 'contact_address_postal_code';
  fields[7] = 'payment_type';

  var errors = new Array();
  errors[0] = ($('#contact_first_name').val() == '' ? 'Please enter the first name of your billing contact' : '');
  errors[1] = ($('#contact_last_name').val() == '' ? 'Please enter the last name of your billing contact' : '');
  errors[2] = (!regex.test($('#contact_email').val()) ? 'Email address does not appear to be valid' : '');
  errors[3] = (processPhoneNum('contact_telephone') ? 'Please enter a valid telephone number' : '');
  errors[4] = ($('#contact_address_building').val() == '' ? 'Please enter address 1' : '');
  errors[5] = ($('#contact_address_town').val() == '' ? 'Please enter address 3' : '');
  errors[6] = ($('#contact_address_postal_code').val() == '' ? 'Please enter your post code' : '');
  errors[7] = ($('#payment_type').val() == '' ? 'Please select payment type' : '');

  return scanPopErrorBox(fields, errors);
}

function addNewPinFormCheckEB() {
  fields = new Array();
  fields[0] = 'first_name';
  fields[1] = 'last_name';
  fields[2] = 'telephone';
  fields[3] = 'email';
  //fields[4] = 'password';
  //fields[5] = 'password_repeat';

  var errors = new Array();
  errors[0] = ($('#first_name').val() == '' ? 'Please enter first name for your new pin contact' : '');
  errors[1] = ($('#last_name').val() == '' ? 'Please enter last name for your new pin contact' : '');
  errors[2] = (processPhoneNum('telephone') ? 'Please enter a valid telephone number' : '');
  errors[3] = (!regex.test($('#email').val()) ? 'Email address does not appear to be valid' : '');
  //errors[4] = ($('#password').val() == '' ? '%22%%' : '');
  //errors[5] = ($('#password').val() != $('#password_repeat').val() ? 'Passwords do not match' : '');

  return scanPopErrorBox(fields, errors);
}

function contactUsFormCheckEB() {
  fields = new Array();
  fields[0] = 'first_name';
  fields[1] = 'last_name';
  fields[2] = 'company';
  fields[3] = 'email';
  fields[4] = 'email_confirm';
  fields[5] = 'telephone';
  fields[6] = 'comments';

  var errors = new Array();
  errors[0] = ($('#first_name').val() == '' ? 'Please enter your first name' : '');
  errors[1] = ($('#last_name').val() == '' ? 'Please enter your last name' : '');
  errors[2] = ($('#company').val() == '' ? 'Please enter your company name' : '');
  errors[3] = (!regex.test($('#email').val()) ? 'Email address does not appear to be valid' : '');
  errors[4] = ($('#email').val() != $('#email_confirm').val() ? 'Email addresses do not match' : '');
  errors[5] = (processPhoneNum('telephone') ? 'Please enter a valid telephone number' : '');
	errors[6] = ($('#comments').val() == '' ? 'Please enter your comments' : '');

  return scanPopErrorBox(fields, errors);
}

function tellafriendFormCheckEB() {
  fields = new Array();
  fields[0] = 'sender_name';
  fields[1] = 'sender_email';
  fields[2] = 'recipient_name';
  fields[3] = 'recipient_email';

  var errors = new Array();
  errors[0] = ($('#sender_name').val() == '' ? 'Please enter your name' : '');
  errors[1] = (!regex.test($('#sender_email').val()) ? 'Sorry, email does not appear to be valid' : '');
  errors[2] = ($('#recipient_name').val() == '' ? 'Please enter the name of your friend' : '');
  errors[3] = (!regex.test($('#recipient_email').val()) ? 'Sorry, email does not appear to be valid' : '');

  return scanPopErrorBox(fields, errors);
}

function requestWalletcardFormCheckEB() {
  fields = new Array();
	fields[0] = 'address_building';
	fields[1] = 'address_town';
  fields[2] = 'address_postal_code';

  var errors = new Array();
	errors[0] = ($('#address_building').val() == '' ? 'Please enter address 1' : '');
	errors[1] = ($('#address_town').val() == '' ? 'Please enter address 3' : '');
	errors[2] = ($('#address_postal_code').val() == '' ? 'Please enter your post code' : '');

  return scanPopErrorBox(fields, errors);
}

function resetPasswordFormCheckEB() {
  fields = new Array();
	fields[0] = 'email';

  var errors = new Array();
	errors[0] = (!regex.test($('#email').val()) ? 'Old password is not correct' : '');

  return scanPopErrorBox(fields, errors);
}

function loginFormCheckEB() {
  fields = new Array();
	fields[0] = 'email';

  var errors = new Array();
	errors[0] = (!regex.test($('#email').val()) ? 'Email address does not appear to be valid' : '');

  return scanPopErrorBox(fields, errors);
}

function processPhoneNum(id) {
	if (e = document.getElementById(id)) {
		e.value = e.value.replace(/[^0-9\.\(\)\+ ]+/g,'');
		return ( e.value.replace(/[^0-9]+/g,'').length < 6 ? true : false );
	} else {
		return false;
	}
}

function scanPopErrorBox(fields, errors) {
	for (i = 0; i < fields.length; i++) {
		if(errors[i] != '') {
      popErrorBox(fields[i], errors[i]);
      return false;
    }
	}
  $('#errbox').fadeOut();
  return true;
}

function popErrorBox(id, error) {
	if (id.length) {
		if (obj = document.getElementById(id)) {
      if(obj.focus)
        obj.focus();
      unfadeMainContent();
      $('#errbox').html(error);
      $('#errbox').fadeIn();
    }
  }
}

function registrationFormReceiveEB(responseText, statusText) {
  if (responseText.length > 50) {
    showPopupDialog(responseText, 450, 300);
  } else {
    var err = '';
    var FocusId, FocusObj;
		resp = responseText.split(':');
		switch(resp[0]) {
			case '0': //TODO
				err = 'System error';
        break;
			case '-1': //TODO
				err = 'Session expired. Please try again. Thank you';
        break;
			case '-2':
				err = 'Cannot use this email to register. Please contact <a href="/conference-calling/contact/" target="_blank">our support</a>.';
        break;
			case '-3':
				err = 'Sorry, email does not appear to be valid';
        break;
			case '-4': //TODO
				err = 'System error';
        break;
			case '-5': //TODO
				err = 'System error';
        break;
			case '-6':
				err = 'Email address does not appear to be valid';
        FocusId = 'contact_email';
        break;
			case '-7':
				err = 'Email addresses do not match';
        break;
			case '-8':
				err = 'Email address for billing contact does not appear to be valid';
        break;
			case '-9':
        err = 'Billing email addresses do not match';
        break;
			case '-10': //TODO
				err = 'System error';
        break;
			case '-11': //TODO
				err = 'System error';
        break;
      case '-12':
        err = 'Please enter your first name';
        break;
      case '-13':
        err = 'Please enter your last name';
        break;
      case '-14':
        err = 'Please enter a valid telephone number';
        break;
      case '-15':
        err = 'Please enter address 1';
        break;
      case '-16':
        err = 'Please enter address 3';
        break;
      case '-17':
        err = 'Please enter your post code';
        break;
      case '-18':
        err = 'contact_address_country_code empty/missing';
        break;
      case '-19':
        err = 'Please enter the first name of your billing contact';
        break;
      case '-20':
        err = 'Please enter the last name of your billing contact';
        break;
      case '-21':
        err = 'Please enter a valid telephone number for billing contact';
        break;
      case '-22':
        err = 'Please enter address 1 for billing contact';
        break;
      case '-23':
        err = 'Please enter address 3 for billing contact';
        break;
      case '-24':
        err = 'Please enter the post code for billing contact';
        break;
      case '-25':
        err = 'billing_address_country_code empty/missing';
        break;
      case '-26':
        err = 'VAT-number is not a valid EU VAT-number';
        break;
      case '-27':
        err = 'Please select payment type';
        break;
      case '-28':
        err = 'Please specify how you heard of us';
        break;
      case '-29':
        err = 'There was an error generating a session to be used with the payment service provider. Please try again. If problem persists, contact administration';
        FocusId = 'vat_number';
        break;
      case '-30':
        err = 'System error. Saving some of your details failed, if problem persists, please <a href="/conference-calling/contact/" target="_blank">contact us.</a>';
        break;
			default:
				err = responseText;
        break;
		}

    unfadeMainContent();
    $('#errbox').html(err);
    $('#errbox').fadeIn();

    FocusObj = document.getElementById(FocusId);
    if(FocusObj) {
      if(FocusObj.focus)  FocusObj.focus();
      if(FocusObj.select) FocusObj.select();
    }
  }
}

function telesalesRegistrationFormReceiveEB(responseText, statusText) {
  if (responseText.length > 50) {
    showPopupDialog(responseText, 450, 300);
  } else {
    var err = '';
    var FocusId, FocusObj;
		resp = responseText.split(':');
		switch(resp[0]) {
			case '0': //TODO
				err = 'System error';
        break;
			case '-1': //TODO
				err = 'Session expired. Please try again. Thank you';
        break;
			case '-2': //TODO
				err = '';
        break;
			case '-3':
				err = 'Sorry, email does not appear to be valid';
        break;
			case '-4': //TODO
				err = 'System error';
        break;
			case '-5': //TODO
				err = 'System error';
        break;
			case '-6':
				err = 'Email address does not appear to be valid';
        FocusId = 'contact_email';
        break;
			case '-7':
				err = 'Email addresses do not match';
        break;
			case '-10': //TODO
				err = 'System error';
        break;
			case '-11': //TODO
				err = '';
        break;
      case '-12':
        err = 'Please enter your first name';
        break;
      case '-13':
        err = 'Please enter your last name';
        break;
      case '-14':
        err = 'Please enter a valid telephone number';
        break;
      case '-15':
        err = 'Please enter address 1';
        break;
      case '-16':
        err = 'Please enter address 3';
        break;
      case '-17':
        err = 'Please enter your post code';
        break;
      case '-18':
        err = 'contact_address_country_code empty/missing';
        break;
      case '-19':
        err = 'Please specify how you heard of us';
        break;
			default:
				err = responseText;
        break;
		}

    unfadeMainContent();
    $('#errbox').html(err);
    $('#errbox').fadeIn();

    FocusObj = document.getElementById(FocusId);
    if(FocusObj) {
      if(FocusObj.focus)  FocusObj.focus();
      if(FocusObj.select) FocusObj.select();
    }
  }
}

function accountDetailsFormReceiveEB(responseText, statusText) {
  if (responseText.length > 50) {
    showPopupDialog(responseText, 450, 300);
  } else {
    var err = '';
    var FocusId, FocusObj;
		resp = responseText.split(':');
		switch(resp[0]) {
			case '0':
				err = 'Sorry, the email address you are trying to change to is already being used';
        break;
			case '-1': //TODO
				err = 'Session expired. Please try again. Thank you';
        break;
			case '-2': //TODO
				err = '';
        break;
			case '-3':
				err = 'Sorry, email does not appear to be valid';
        break;
			case '-4': //TODO
				err = 'System error';
        break;
			case '-5': //TODO
				err = 'System error';
        break;
			case '-6':
				err = 'Email address does not appear to be valid';
        FocusId = 'contact_email';
        break;
			case '-7':
				err = 'You have no usage for the selected date range';
        break;
			case '-8':
				err = 'Email address for billing contact does not appear to be valid';
        break;
			case '-9':
        err = 'Billing email addresses do not match';
        break;
			case '-10': //TODO
				err = 'System error';
        break;
			case '-11': //TODO
				err = '';
        break;
      case '-12':
        err = 'Please enter your first name';
        break;
      case '-13':
        err = 'Please enter your last name';
        break;
      case '-14':
        err = 'Please enter a valid telephone number';
        break;
      case '-15':
        err = 'Please enter address 1';
        break;
      case '-16':
        err = 'Please enter address 3';
        break;
      case '-17':
        err = 'Please enter your post code';
        break;
      case '-18':
        err = 'contact_address_country_code empty/missing';
        break;
      case '-19':
        err = 'Please enter the first name of your billing contact';
        break;
      case '-20':
        err = 'Please enter the last name of your billing contact';
        break;
      case '-21':
        err = 'Please enter a valid telephone number for billing contact';
        break;
      case '-22':
        err = 'Please enter address 1 for billing contact';
        break;
      case '-23':
        err = 'Please enter address 3 for billing contact';
        break;
      case '-24':
        err = 'Please enter the post code for billing contact';
        break;
      case '-25':
        err = 'billing_address_country_code empty/missing';
        break;
      case '-26':
        err = 'VAT-number is not a valid EU VAT-number';
        break;
      case '-27':
        err = 'Please select payment type';
        break;
      case '-28':
        err = 'Please specify how you heard of us';
        break;
      case '-29':
        err = 'There was an error generating a session to be used with the payment service provider. Please try again. If problem persists, contact administration';
        FocusId = 'vat_number';
        break;
      case '-30':
        err = 'System error. Saving some of your details failed, if problem persists, please <a href="/conference-calling/contact/" target="_blank">contact us.</a>';
        break;
			default:
				err = responseText;
        break;
		}

    unfadeMainContent();
    $('#errbox').html(err);
    $('#errbox').fadeIn();

    FocusObj = document.getElementById(FocusId);
    if(FocusObj) {
      if(FocusObj.focus)  FocusObj.focus();
      if(FocusObj.select) FocusObj.select();
    }
  }
}

function updatePaymentDetailsFormReceiveEB(responseText, statusText) {
  if (responseText.length > 50) {
    showPopupDialog(responseText, 450, 300);
  } else {
    var err = '';
    var FocusId, FocusObj;
		resp = responseText.split(':');
		switch(resp[0]) {
			case '0':
				err = 'Sorry, the email address you are trying to change to is already being used';
        break;
			case '-1': //TODO
				err = 'Session expired. Please try again. Thank you';
        break;
			case '-2': //TODO
				err = '';
        break;
			case '-3':
				err = 'Sorry, email does not appear to be valid';
        break;
			case '-4': //TODO
				err = 'System error';
        break;
			case '-5': //TODO
				err = 'System error';
        break;
			case '-6':
				err = 'Email address does not appear to be valid';
        FocusId = 'contact_email';
        break;
			case '-7':
				err = 'You have no usage for the selected date range';
        break;
			case '-8':
				err = 'Email address for billing contact does not appear to be valid';
        break;
			case '-9':
        err = 'Billing email addresses do not match';
        break;
			case '-10': //TODO
				err = 'System error';
        break;
			case '-11': //TODO
				err = '';
        break;
      case '-12':
        err = 'Please enter your first name';
        break;
      case '-13':
        err = 'Please enter your last name';
        break;
      case '-14':
        err = 'Please enter a valid telephone number';
        break;
      case '-15':
        err = 'Please enter address 1';
        break;
      case '-16':
        err = 'Please enter address 3';
        break;
      case '-17':
        err = 'Please enter your post code';
        break;
      case '-18':
        err = 'contact_address_country_code empty/missing';
        break;
      case '-27':
        err = 'Please select payment type';
        break;
      case '-29':
        err = 'There was an error generating a session to be used with the payment service provider. Please try again. If problem persists, contact administration';
        FocusId = 'vat_number';
        break;
      case '-30':
        err = 'System error. Saving some of your details failed, if problem persists, please <a href="/conference-calling/contact/" target="_blank">contact us.</a>';
        break;
			default:
				err = responseText;
        break;
		}

    unfadeMainContent();
    $('#errbox').html(err);
    $('#errbox').fadeIn();

    FocusObj = document.getElementById(FocusId);
    if(FocusObj) {
      if(FocusObj.focus)  FocusObj.focus();
      if(FocusObj.select) FocusObj.select();
    }
  }
}

function contactUsFormReceiveEB(responseText, statusText) {
  if (responseText.length > 50) {
    showPopupDialog(responseText, 450, 300);
  } else {
    var err = '';
    var FocusId, FocusObj;
		resp = responseText.split(':');
		switch(resp[0]) {
			case '0':
				err = 'Sorry, the email address you are trying to change to is already being used';
        break;
			case '-1': //TODO
				err = 'Session expired. Please try again. Thank you';
        break;
			case '-6':
				err = 'Email address does not appear to be valid';
        FocusId = 'email';
        break;
			case '-7':
				err = 'Email addresses do not match';
				FocusId = 'email_confirm';
        break;
			case '-8':
				err = 'Please enter your company name';
				FocusId = 'company';
        break;
      case '-12':
        err = 'Please enter your first name';
        break;
      case '-13':
        err = 'Please enter your last name';
        break;
      case '-14':
        err = 'Please enter a valid telephone number';
        break;
			default:
				err = responseText;
        break;
		}

    unfadeMainContent();
    $('#errbox').html(err);
    $('#errbox').fadeIn();

    FocusObj = document.getElementById(FocusId);
    if(FocusObj) {
      if(FocusObj.focus)  FocusObj.focus();
      if(FocusObj.select) FocusObj.select();
    }
  }
}

function addNewPinFormReceive(responseText, statusText) {
  if (responseText.length > 50) {
    showPopupDialog(responseText, 450, 300);
  } else {
    var err = '';
    var FocusId, FocusObj;
		resp = responseText.split(':');
		switch(resp[0]) {
			case '0':
				err = 'System error';
        break;
      case '-6':
				err = 'Email address does not appear to be valid';
        FocusId = 'contact_email';
        break;
      case '-12':
        err = 'Please enter first name for your new pin contact';
        break;
      case '-13':
        err = 'Please enter last name for your new pin contact';
        break;
      case '-14':
        err = 'Please enter a valid telephone number';
        break;
      case '-31':
        err = 'Password cannot be empty';
        break;
      case '-32':
        err = 'Passwords do not match';
        break;
			default:
				err = responseText;
        break;
    }

    unfadeMainContent();
    $('#errbox').html(err);
    $('#errbox').fadeIn();

    FocusObj = document.getElementById(FocusId);
    if(FocusObj) {
      if(FocusObj.focus)  FocusObj.focus();
      if(FocusObj.select) FocusObj.select();
    }
  }
}

function askDeactivatePinFormReceive(responseText, statusText) {
  showPopupDialog(responseText);
}

function deactivatePinFormReceive(responseText, statusText) {
  showPopupDialog(responseText);
}

function usageTableFormReceive(responseText, statusText) {
  if (responseText.length > 50) {
    $('#UsageTableContainer').html(responseText);
  } else {
    $('#UsageTableContainer').html('You have no usage for the selected date range');
  }

  $('#UsageTableAjaxLoader').hide();
}

function recordingsTableFormReceive(responseText, statusText) {
  if (responseText.length > 50) {
    $('#RecordingsTableContainer').html(responseText);
  } else {
    $('#RecordingsTableContainer').html('You have no recordings for the selected date range');
  }

  $('#RecordingsTableAjaxLoader').hide();
}

function emailRecordingButtonFormReceive(responseText, statusText) {
  showPopupDialog(responseText);
}

function emailRecordingFormReceive(responseText, statusText) {
 showPopupDialog(responseText);
}

function invoicesTableFormReceive(responseText, statusText) {
  if (responseText.length > 50) {
    $('#InvoicesTableContainer').html(responseText);
  } else {
    $('#InvoicesTableContainer').html('You have no Invoices for the selected date range');
  }

  $('#InvoicesTableAjaxLoader').hide();
}

function statementTableFormReceive(responseText, statusText) {
  $('#StatementTableContainer').html(responseText);
  $('#StatementTableAjaxLoader').hide();
}

function pinSettingsFormReceive(responseText, statusText) {
  showPopupDialog(responseText);
}

function closePopupDialog() {
  $('div#PopupDialog').hide();
  unfadeMainContent();
}

function showPopupDialog(content, width, height) {
  if(!width)
    width = 420;

  if(!height)
    height = 270;

  $('div#PopupDialog').css('width', width + 'px').css('height', height + 'px').css('margin-left', -(width / 2) + 'px').css('margin-top', -(height / 2) + 'px').html(content).show();
}

function getBodyHeight() {
  var height = 0;
  if (document.body && document.body.clientHeight) {
    height = document.body.clientHeight;
  }

  if(document.body && document.body.scrollHeight) {
    height = document.body.scrollHeight;
  }
  return height;
}

function getBodyWidth() {
  var width = 0;

  if (document.body && document.body.clientWidth) {
    width = document.body.clientWidth;
  }

  return width;
}

function fadeMainContent() {
  if(isMSIE6)
    return;
  var FadeBox = document.getElementById('FadeBox');

  FadeBox.style.height = getBodyHeight() + "px";
  FadeBox.style.width = getBodyWidth() + "px";
  FadeBox.style.display = '';
}

function unfadeMainContent() {
  if(isMSIE6)
    return;

  var FadeBox = document.getElementById('FadeBox');
  FadeBox.style.display = 'none';
}

function showVATInfo() {
  fadeMainContent();

  $.ajax({
    type: "POST",
    url: "/default/faq/vatinfo/format/html/",
    data: '',
    success: function(ResponseText) {
      showPopupDialog(ResponseText);
    }
  });
}

function showPaymentCardTypeInfo() {
  fadeMainContent();

  $.ajax({
    type: "POST",
    url: "/default/faq/paymentcardtypeinfo/format/html/",
    data: '',
    success: function(ResponseText) {
      showPopupDialog(ResponseText);
    }
  });
}

function changePasswordFormCheckEB() {
	fields = new Array();
	fields[0] = 'new_password';
	fields[1] = 'new_password_repeat';

	var errors = new Array();
	errors[0] = ($('#new_password').val() == '' ? 'New passwords do not match' : '');
	errors[1] = ($('#new_password').val() != $('#new_password_repeat').val() ? 'Old password is not correct' : '');

	return scanPopErrorBox(fields, errors);
}

function changePasswordFormReceiveEB(responseText, statusText) {
	if (responseText.length > 50) {
		showPopupDialog(responseText, 450, 300);
	} else {
		var err = '';
		var FocusId, FocusObj;
		resp = responseText.split(':');
		switch(resp[0]) {
			case '0': //TODO
				err = 'System error';
				break;
			case '-30':
				err = 'Please enter your name';
				FocusId = 'password';
				break;
			case '-31':
				err = 'New passwords do not match';
				break;
			case '-32':
				err = 'Old password is not correct';
				break;
		}

		unfadeMainContent();
		$('#errbox').html(err);
		$('#errbox').fadeIn();

		FocusObj = document.getElementById(FocusId);
		if(FocusObj) {
			if(FocusObj.focus)  FocusObj.focus();
			if(FocusObj.select) FocusObj.select();
		}
	}
}

function tellafriendFormReceiveEB(responseText, statusText) {
	if (responseText.length > 50) {
		showPopupDialog(responseText, 450, 300);
	} else {
		var err = '';
		var FocusId, FocusObj;
		resp = responseText.split(':');
		switch(resp[0]) {
			case '0': //TODO
				err = 'System error';
				break;
			case '-10':
				err = 'Please enter your name';
				break;
			case '-11':
				err = 'Please enter the name of your friend';
				break;
			case '-12':
				err = 'Sorry, your email is unknown to us, please <a href="/allconferencecalls/register/">register</a>';
				break;
		}

		unfadeMainContent();
		$('#errbox').html(err);
		$('#errbox').fadeIn();

		FocusObj = document.getElementById(FocusId);
		if(FocusObj) {
			if(FocusObj.focus)  FocusObj.focus();
			if(FocusObj.select) FocusObj.select();
		}
	}
}

function requestWalletcardFormReceiveEB(responseText, statusText) {
	if (responseText.length > 50) {
		showPopupDialog(responseText, 450, 300);
	} else {
		var err = '';
		var FocusId, FocusObj;
		resp = responseText.split(':');
		switch(resp[0]) {
			case '0': //TODO
				err = 'System error';
				break;
			case '-10':
				err = 'Please enter address 1';
				break;
			case '-11':
				err = 'Please enter address 3';
				break;
			case '-12':
				err = 'Please enter your post code';
				break;
		}

		unfadeMainContent();
		$('#errbox').html(err);
		$('#errbox').fadeIn();

		FocusObj = document.getElementById(FocusId);
		if(FocusObj) {
			if(FocusObj.focus)  FocusObj.focus();
			if(FocusObj.select) FocusObj.select();
		}
	}
}

function resetPasswordFormReceiveEB(responseText, statusText) {
	if (responseText.length > 50) {
		showPopupDialog(responseText, 450, 300);
	} else {
		var err = '';
		var FocusId, FocusObj;
		resp = responseText.split(':');
		switch(resp[0]) {
			case '0': //TODO
				err = 'System error';
				break;
			case '-10':
				err = 'Old password is not correct';
				break;
			case '-11':
				err = 'Login incorrect';
				break;
		}

		unfadeMainContent();
		$('#errbox').html(err);
		$('#errbox').fadeIn();

		FocusObj = document.getElementById(FocusId);
		if(FocusObj) {
			if(FocusObj.focus)  FocusObj.focus();
			if(FocusObj.select) FocusObj.select();
		}
	}
}

function loginFormReceiveEB(responseText, statusText) {
	if (responseText.length > 50) {
		showPopupDialog(responseText, 450, 300);
	} else {
		closePopupDialog();
		var err = '';
		var FocusId, FocusObj;
		resp = responseText.split(':');
		switch(resp[0]) {
			case '0': //TODO
				err = 'Please enter your comments';
				break;
			case '-11':
				err = 'Login incorrect';
				break;
		}

		unfadeMainContent();
		$('#errbox').html(err);
		$('#errbox').fadeIn();

		FocusObj = document.getElementById(FocusId);
		if(FocusObj) {
			if(FocusObj.focus)  FocusObj.focus();
			if(FocusObj.select) FocusObj.select();
		}
	}
}

$(document).ready(function() {
  window.onresize = function() {
    if(isMSIE6)
      return;
    var FadeBox = document.getElementById('FadeBox');

    FadeBox.style.height = getBodyHeight() + "px";
    FadeBox.style.width = getBodyWidth() + "px";
  };

  $('#RegistrationForm').submit(function() {
		$(this).ajaxSubmit({
        beforeSubmit:function()	{ $('#errbox').fadeOut('fast'); fadeMainContent(); return registrationFormCheckEB(); },
        success:registrationFormReceiveEB
      });
		return false;
	});

  $('#TelesalesRegistrationForm').submit(function() {
		$(this).ajaxSubmit({
        beforeSubmit:function()	{ $('#errbox').fadeOut('fast'); fadeMainContent(); return telesalesRegistrationFormCheckEB(); },
        success:telesalesRegistrationFormReceiveEB
      });
		return false;
	});

  $('#AccountDetailsForm').submit(function() {
		$(this).ajaxSubmit({
        beforeSubmit:function()	{ $('#errbox').fadeOut('fast'); fadeMainContent(); return accountDetailsFormCheckEB(); },
        success:accountDetailsFormReceiveEB
      });
		return false;
	});

  $('#UpdatePaymentDetailsForm').submit(function() {
		$(this).ajaxSubmit({
        beforeSubmit:function()	{ $('#errbox').fadeOut('fast'); fadeMainContent(); return updatePaymentDetailsFormCheckEB(); },
        success:updatePaymentDetailsFormReceiveEB
      });
		return false;
	});

  $('#ContactUsForm').submit(function() {
		$(this).ajaxSubmit({
        beforeSubmit:function()	{ $('#errbox').fadeOut('fast'); fadeMainContent(); return contactUsFormCheckEB(); },
        success:contactUsFormReceiveEB
      });
		return false;
	});

  $('#UsageTableForm').submit(function() {
		$(this).ajaxSubmit({
        beforeSubmit:function()	{ $('#UsageTableAjaxLoader').show(); },
        success:usageTableFormReceive
      });
		return false;
	});

  $('#RecordingsTableForm').submit(function() {
		$(this).ajaxSubmit({
        beforeSubmit:function()	{ $('#RecordingsTableAjaxLoader').show(); },
        success:recordingsTableFormReceive
      });
		return false;
	});

  $('#InvoicesTableForm').submit(function() {
		$(this).ajaxSubmit({
        beforeSubmit:function()	{ $('#InvoicesTableAjaxLoader').show(); },
        success:invoicesTableFormReceive
      });
		return false;
	});

  $('#StatementTableForm').submit(function() {
		$(this).ajaxSubmit({
        beforeSubmit:function()	{ $('#StatementTableAjaxLoader').show(); },
        success:statementTableFormReceive
      });
		return false;
	});

  $('#AddNewPinForm').submit(function() {
		$(this).ajaxSubmit({
        beforeSubmit:function()	{ $('#errbox').fadeOut('fast'); fadeMainContent(); return addNewPinFormCheckEB(); },
        success:addNewPinFormReceive
      });
		return false;
	});

  $('#PinSettingsForm').submit(function() {
    $(this).ajaxSubmit({
      beforeSubmit:function()	{ fadeMainContent();  },
      success:pinSettingsFormReceive
    });
    return false;
  });

	$('#PasswordChangeForm').submit(function() {
		$(this).ajaxSubmit({
			beforeSubmit:function()	{ $('#errbox').fadeOut('fast'); fadeMainContent(); return changePasswordFormCheckEB(); },
			success:changePasswordFormReceiveEB
		});
		return false;
	});

	$('#TellafriendForm').submit(function() {
		$(this).ajaxSubmit({
			beforeSubmit:function()	{ $('#errbox').fadeOut('fast'); fadeMainContent(); return tellafriendFormCheckEB(); },
			success:tellafriendFormReceiveEB
		});
		return false;
	});

	$('#ResetPasswordForm').submit(function() {
		$(this).ajaxSubmit({
			beforeSubmit:function()	{ $('#errbox').fadeOut('fast'); fadeMainContent(); return resetPasswordFormCheckEB(); },
			success:resetPasswordFormReceiveEB
		});
		return false;
	});

	$('#LoginForm').submit(function() {
		$(this).ajaxSubmit({
			beforeSubmit:function()	{
				$('#errbox').fadeOut('fast');
				fadeMainContent();
				if(loginFormCheckEB()) {
					showPopupDialog('<p style="text-align: center;"><br /><br /><img src="/images/ajax-loader-bar.gif" /></p>', 450, 100);
					return true;
				} else return false;
			},
			success:loginFormReceiveEB
		});
		return false;
	});

	$('#RequestWalletcardForm').submit(function() {
		$(this).ajaxSubmit({
			beforeSubmit:function()	{ $('#errbox').fadeOut('fast'); fadeMainContent(); return requestWalletcardFormCheckEB(); },
			success:requestWalletcardFormReceiveEB
		});
		return false;
	});

});

function fadenews() {
	i = 0;
	while (l = document.getElementById('news-'+i)) {
		if (l.style.display!='none') {
			$('#news-'+i).fadeOut(500);
			j = i + 1;
			setTimeout('$("#news-'+ (document.getElementById('news-'+j)?j:0) +'").fadeIn(500)',600);
			break;
		}
		i++;
	}
	setTimeout('fadenews()',5000);
}