// JavaScript Document
check_form = function(language, form_type)
{
  // The same testing will be used for the different forms
  
  if(form_type == "register")
  {
     var f = document.reg_form; 
  }
  else if(form_type == "subscription")
  {
     var f = document.sub_form;
  }
  else if(form_type == "consult")
  {
     var f = document.consult_form;
  }
  
  var email1  = f.textEmail.value;
  var email2  = f.textEmail2.value;
  var name    = f.textName.value;
  var surname = f.textSurname1.value;
  var phone   = f.textPhone.value;
  
  if(language == "esp")
  {
     email_not_equal = "Los emails no son iguales";
	 email_empty     = "Escribe tu email";
	 name_empty      = "Escribe tu nombre";
	 surname_empty   = "Escribe tu apellido";
	 phone_empty     = "Escribe tu número de teléfono";
	 phone_not_num   = "Teléfono debe ser un número";
	 email_not_valid = "Tu email no es valido";
	 address_empty   = "Escribe tu dirección";
	 city_empty      = "Escribe tu ciudad";
	 province_empty  = "Escribe ty provincia";
	 post_empty      = "Escribe tu código postal";
	 
	 credit_card_empty   = "Escribe el número de tu tarjeta de crédito";
	 credit_card_not_num = "El número de la tarjeta de crédito debe ser un número";
	 credit_card_bad_len = "El número de la tarjeta de crédito debe contener 16 numeros";
	 
	 bank_entity_empty    = "Escribe la entidad del banco";
	 bank_entity_not_num  = "La entidad del banco debe ser un número";
	 bank_entity_bad_len  = "La entidad del banco debe contener 4 numeros";
	 bank_office_empty    = "Escribe la oficina del banco";
	 bank_office_not_num  = "La oficina del banco debe ser un numero";
	 bank_office_bad_len  = "La oficina del banco debe contener 4 numeros";
	 bank_control_empty   = "Escribe el control del banco";
	 bank_control_not_num = "El control del banco debe ser un número";
	 bank_control_bad_len = "El control del banco debe contener 2 numeros";
	 bank_account_empty   = "Escribe la cuenta del banco";
	 bank_account_not_num = "La cuenta del banco debe ser un número";
	 bank_account_bad_len = "La cuenta del banco debe contener 10 numeros";
  }
  else if(language == "eng")
  {
     email_not_equal = "The emails are not the same";
	 email_empty     = "Please write your email";
	 name_empty      = "Please write your name";
	 surname_empty   = "Please write your surname";
	 phone_empty     = "Please write your phone number";
	 phone_not_num   = "Phone must be a number";
	 email_not_valid = "Your email is not valid";
	 address_empty   = "Please write your address";
	 city_empty      = "Please write your city";
	 province_empty  = "Please write your province/county/state";
	 post_empty      = "Please write your post code";
	 
	 credit_card_empty   = "Please write your credit card number";
	 credit_card_not_num = "The credit card must be a number";
	 credit_card_bad_len = "The credit card number must contain 16 digits";
	 
	 bank_entity_empty    = "Please write the bank entity number";
	 bank_entity_not_num  = "The bank entity must be a number";
	 bank_entity_bad_len  = "The bank entity number must contain 4 digits";
	 bank_office_empty    = "Please write the bank office number";
	 bank_office_not_num  = "The bank office must be a number";
	 bank_office_bad_len  = "The bank office number must contain 4 digits";
	 bank_control_empty   = "Please write the bank control number";
	 bank_control_not_num = "The bank control must be a number";
	 bank_control_bad_len = "The bank control must contain 2 digits";
	 bank_account_empty   = "Please write the bank account number";
	 bank_account_not_num = "The bank account must be a number";
	 bank_account_bad_len = "The bank account must contain 10 digits";
  }
  else if(language == "fr")
  {
     email_not_equal = "";
	 email_empty     = "";
	 name_empty      = "";
	 surname_empty   = "";
	 phone_empty     = "";
	 phone_not_num   = "";
	 email_not_valid = "";
	 address_empty   = "";
	 city_empty      = "";
	 province_empty  = "";
	 post_empty      = "";
	 
	 credit_card_empty   = "";
	 credit_card_not_num = "";
	 credit_card_bad_len = "";
	 
	 bank_entity_empty    = "";
	 bank_entity_not_num  = "";
	 bank_entity_bad_len  = "";
	 bank_office_empty    = "";
	 bank_office_not_num  = "";
	 bank_office_bad_len  = "";
	 bank_control_empty   = "";
	 bank_control_not_num = "";
	 bank_control_bad_len = "";
	 bank_account_empty   = "";
	 bank_account_not_num = "";
	 bank_account_bad_len = "";
  }
  else if(language == "ger")
  {
     email_not_equal = "";  
	 email_empty     = "";
	 name_empty      = "";
	 surname_empty   = "";
	 phone_empty     = "";
	 phone_not_num   = "";
	 email_not_valid = "";
	 address_empty   = "";
	 city_empty      = "";
	 province_empty  = "";
	 post_empty      = "";
	 
	 credit_card_empty   = "";
	 credit_card_not_num = "";
	 credit_card_bad_len = "";
	 
	 bank_entity_empty    = "";
	 bank_entity_not_num  = "";
	 bank_entity_bad_len  = "";
	 bank_office_empty    = "";
	 bank_office_not_num  = "";
	 bank_office_bad_len  = "";
	 bank_control_empty   = "";
	 bank_control_not_num = "";
	 bank_control_bad_len = "";
	 bank_account_empty   = "";
	 bank_account_not_num = "";
	 bank_account_bad_len = "";
  }
  else if(language == "it")
  {
     email_not_equal = "";
	 email_empty     = "";
	 name_empty      = "";
	 surname_empty   = "";
	 phone_empty     = "";
	 phone_not_num   = "";
	 email_not_valid = "";
	 address_empty   = "";
	 city_empty      = "";
	 province_empty  = "";
	 post_empty      = "";
	 
	 credit_card_empty   = "";
	 credit_card_not_num = "";
	 credit_card_bad_len = "";
	 
	 bank_entity_empty    = "";
	 bank_entity_not_num  = "";
	 bank_entity_bad_len  = "";
	 bank_office_empty    = "";
	 bank_office_not_num  = "";
	 bank_office_bad_len  = "";
	 bank_control_empty   = "";
	 bank_control_not_num = "";
	 bank_control_bad_len = "";
	 bank_account_empty   = "";
	 bank_account_not_num = "";
	 bank_account_bad_len = "";
  }
  
  // Check the contents of the form
  if(email1 != "")
  {
    if(!checkEmail(form_type))
	{
	  alert(email_not_valid);
	  return;
	}
	
    if(email1 != email2)
    {
      alert(email_not_equal);
	  return;
	}
  }
  else
  {
    alert(email_empty);
	return;
  }
  
  if(name == "")
  {
    alert(name_empty);
	return;
  }
  
  if(surname == "")
  {
    alert(surname_empty);
	return;
  }
  
  if(phone != "")
  {
     if(phone/1 != phone)
	 {
	    alert(phone_not_num);
		return;
	 }
  }
  
  if(form_type == "register")
  {
     // If all is well, the page is sent onto the reg_database page
     // where the data is registered in the database;
     f.action = "databases/reg_db.php";
  }
  else if(form_type == "subscription")
  {
     // Other fields that must be filled in for the subscription
	 var address  = f.textAddress1.value;
	 var city     = f.textCity.value;
	 var province = f.textProvince.value;
     var post     = f.textPost.value;
	 
	 if(address == "")
	 {
	   alert(address_empty);
	   return;
	 }
	 
	 if(city == "")
	 {
	   alert(city_empty);
	   return;
	 }
	 
	 if(province == "")
	 {
	   alert(province_empty);
	   return;
	 }
	 
	 if(post == "")
	 {
	   alert(post_empty);
	   return;
	 }
	 
	 if(f.radiocc.checked == true) // credit card
	 {
	    var card_no = f.textCardNumber.value;
		
		/* --- Test amount chosen ---------------
		for(var i = 0; i < 5; i ++)
	    {
	      if(f.radiobuttoncc[i].checked == true)
		  {
		    alert(f.radiobuttoncc[i].value);
		  }
	    }
		
		----------------------------------------*/
		
		if(card_no != "")
		{
		   if(card_no/1 != card_no)
		   {
		      alert(credit_card_not_num);
		      return;
		   }
		   
		   if(card_no.length != 16)
		   {
		      alert(credit_card_bad_len);
			  return;
		   }
		}
		else
		{
		   alert(credit_card_empty);
		   return;
		}
	 }
	 else if(f.radiobt.checked == true) // transfer
	 {
	    var bank_entity  = f.textEntidad.value;
		var bank_office  = f.textOficina.value;
		var bank_control = f.textControl.value;
		var bank_account = f.textAccount.value;
		
		/* ------- Test amount chosen ------------
		for(var i = 0; i < 5; i ++)
	    {
	      if(f.radiobuttonbt[i].checked == true)
		  {
		    alert(f.radiobuttonbt[i].value);
		  }
	    }
		-------------------------------------------*/
		
		if(bank_entity != "")
		{
		   if(bank_entity/1 != bank_entity)
		   {
		      alert(bank_entity_not_num);
			  return;
		   }
		   
		   if(bank_entity.length != 4)
		   {
		      alert(bank_entity_bad_len);
			  return;
		   }
		}
		else
		{
		  alert(bank_entity_empty);
		  return;
		}
		
		if(bank_office != "")
		{
		   if(bank_office/1 != bank_office)
		   {
		      alert(bank_office_not_num);
			  return;
		   }
		   
		   if(bank_office.length != 4)
		   {
		     alert(bank_office_bad_len);
			 return;
		   }
		}
		else
		{
		  alert(bank_office_empty);
		  return;
		}
		
		if(bank_control != "")
		{
		   if(bank_control/1 != bank_control)
		   {
		      alert(bank_control_not_num);
			  return;
		   }
		   
		   if(bank_control.length != 2)
		   {
		      alert(bank_control_bad_len);
			  return;
		   }
		}
		else
		{
		   alert(bank_control_empty);
		   return;
		}
		
		if(bank_account != "")
		{
		   if(bank_account/1 != bank_account)
		   {
		      alert(bank_account_not_num);
			  return;
		   }
		   
		   if(bank_account.length != 10)
		   {
		     alert(bank_account_bad_len);
			 return;
		   }
		}
		else
		{
		   alert(bank_account_empty);
		   return;
		}
	 }
  }
  else if(form_type == "consult")
  {
     f.action = "send_email.php";
  }
  f.submit();
}

function checkEmail(form_type) 
{
  if(form_type == "register")
  {
     var f = document.reg_form; 
  }
  else if(form_type == "subscription")
  {
     var f = document.sub_form;
  }
  else if(form_type == "consult")
  {
     var f = document.consult_form;
  }
  
  text = f.textEmail.value;
  
  if (text.indexOf("@") < 3)
  {
    return false;
  }
  if ((text.indexOf(".com") <5) && (text.indexOf(".org") <5)
    &&(text.indexOf(".gov") <5) && (text.indexOf(".net") <5)
    &&(text.indexOf(".co.uk") <7) &&(text.indexOf(".nl") <4) &&(text.indexOf(".es") <4)
    &&(text.indexOf(".mil") <5) &&(text.indexOf(".edu") <5))
  {
     return false;
  }
  return true;
}

enable_submit_register = function()
{
  document.reg_form.submit_register.disabled = false;
}

disable_submit_register = function()
{
  document.reg_form.submit_register.disabled = true;
}

register_term_agreement = function()
{
  var f = document.reg_form;
  
  if(f.checkAgree.checked == true)
  {
     enable_submit_register();
  }
  else
  {
     disable_submit_register();
  }
}

show_payment_form = function(language, payment_method)
{
 
  var url      = "subscribe.php?language=" + language + "&pay=yes&pay_method=" + payment_method;
  var f        = document.sub_form;
  var name     = f.textName.value;
  var surname1 = f.textSurname1.value;

  if(language == "esp")
  {
     var surname2 = f.textSurname2.value;
  }
  var address1 = f.textAddress1.value;
  var address2 = f.textAddress2.value;
  var city     = f.textCity.value;
  var province = f.textProvince.value;
  var post     = f.textPost.value;
  var phone    = f.textPhone.value;
  var email    = f.textEmail.value;
  var email2   = f.textEmail2.value;
  
  if(name != "")
  {
     url += "&name=" + name;
  }
  
  if(surname1 != "")
  {
     url += "&surname1=" + surname1;
  }
  
  if(language == "esp")
  {
     if(surname2 != "")
     {
        url += "&surname2=" + surname2;
     }
  }
  
  if(address1 != "")
  {
     url += "&address1=" + address1;
  }
  
  if(address2 != "")
  {
     url += "&address2=" + address2;
  }
  
  if(city != "")
  {
     url += "&city=" + city;
  }
  
  if(province != "")
  {
     url += "&province=" + province;
  }
  
  if(post != "")
  {
     url += "&post=" + post;
  }
  
  if(phone != "")
  {
     url += "&phone=" + phone;
  }
  
  if(email != "")
  {
     url += "&email=" + email;
  }
  
  if(email2 != "")
  {
     url += "&email2=" + email2;
  }
  
  document.location = url;
  
}

highlight_payment_method = function(method, name, surname1, surname2, address1, address2, city, province, post, phone, email, email2)
{
  var f = document.sub_form;
  if(method == "cc") // credit card
  {
    document.sub_form.radiocc.checked = true;
	document.sub_form.radiobt.checked = false;
  }
  else if(method == "bt")
  {
    document.sub_form.radiobt.checked = true;
	document.sub_form.radiocc.checked = false;
  }
  
  f.textName.value     = name;
  f.textSurname1.value = surname1;
  f.textSurname2.value = surname2;
  f.textAddress1.value = address1;
  f.textAddress2.value = address2;
  f.textCity.value     = city;
  f.textProvince.value = province;
  f.textPost.value     = post;
  f.textPhone.value    = phone;
  f.textEmail.value    = email;
  f.textEmail2.value   = email2;
}

// ***** Police.php ****************
show = function(option_chosen, lang)
{
  var f             = document.formPolice;
  var params        = "?option_chosen=" + option_chosen + "&lang=" + lang;
  document.location = "buscador-policia.php" + params;
}

getTown = function(option_chosen, lang)
{
  var f        = document.formPolice;
  var province = f.selectProvince.value;
  var params;
  
  if(province != '')
  {
    params  = "?option_chosen=" + option_chosen + "&lang=" + lang;
    params += "&province_db=" + province;
  }
  document.location = "buscador-policia.php" + params;
}

searchByPost = function(option_chosen, lang)
{
  var f      = document.formPolice;
  var cp_db = f.selectPost.value;
  var params;
  
  if(cp_db != '')
  {
     params  = "?option_chosen=" + option_chosen + "&lang=" + lang;
     params += "&cp_db=" + cp_db;
  }
  document.location = "buscador-policia.php" + params;
}

searchByProv = function(option_chosen, lang, province_db)
{
  var f       = document.formPolice;
  var town_db = f.selectTown.value;
  var params;
  
  if(town_db != '')
  {
     params  = "?option_chosen=" + option_chosen + "&lang=" + lang;
     params += "&province_db=" + province_db + "&town_db=" + town_db;
  }
  document.location = "buscador-policia.php" + params;
}

// Code for the consulates php page
showConsulates = function(language)
{
  var f             = document.formConsulates;
  var country       = f.selectCountry.value;
  document.location = "buscador-de-consulados.php?lang=" + language + "&country_chosen=" + country + "&show_details=yes";
}

showValues = function(country)
{
  if(country != 'na')
  {
    document.formConsulates.selectCountry.value = country;
  }
}
