var ID_PAIS_ESP = 71;
var ID_PROV_OTRA = 53;
var REEMBOLSO = 1;
var TARJETA = 2;
var POSTAL = 1;

function check_fields()
{
	nif = new String(document.frm.nif.value);
    nombre = new String(document.frm.nombre.value);
	primer_ape = new String(document.frm.apellido1.value);
	segun_ape = new String(document.frm.apellido2.value);
    provincia = new String(document.frm.id_provincia_envio.value);
	pais = new String(document.frm.id_pais_envio.value);
    direccion = new String(document.frm.direccion.value);
    poblacion = new String(document.frm.poblacion.value);
    cp = new String(document.frm.cp.value);
    tlfno1 = new String(document.frm.tlfno1.value);
    movil1 = new String(document.frm.movil1.value);    

    nif.trim();
    nombre.trim();
    primer_ape.trim();
    segun_ape.trim();
    direccion.trim();
    poblacion.trim();
    cp.trim();
    tlfno1.trim();
    movil1.trim();

	var env = true;
    var msg = "Debe introducir los siguientes campos obligatorios:\n"

    if (nif == "")
	{
		msg += "- NIF/CIF/Pasaporte\n";
		env = false;
	}
	if (nombre == "")
	{
		msg += "- Nombre\n"
	}
    if (primer_ape == "")
	{
		msg += "- Primer apellido\n";
		env = false;
	}
    if (segun_ape == "")
	{
		msg += "- Segundo apellido\n";
		env = false;
	}
	if (pais == "-1")
	{
		msg += "- Pais\n";
		env = false;
	}
    if (provincia == "-1" && pais == ID_PAIS_ESP)
	{
		msg += "- Provincia\n";
        env = false;
	}
    if (direccion == "")
	{
		msg += "- Dirección\n";
		env = false;
	}
    if (poblacion == "")
	{
		msg += "- Población\n";
		env = false;
	}
    if (cp == "")
	{
		msg += "- Código postal\n";
		env = false;
	}
    if (tlfno1 == "" && movil1 == "")
	{
		msg += "- Teléfono fijo o Teléfono móvil\n";
		env = false;
	}
	for (i = 0; i < document.frm.radio_ent.length; i++)
    {
		if (document.frm.radio_ent[i].value == 'Si')
		{
			if (document.frm.radio_ent[i].checked)
			{
				direccion_entrega = new String(document.frm.direccion_entrega.value);
				poblacion_entrega = new String(document.frm.poblacion_entrega.value);		    	
				cp_entrega = new String(document.frm.cp_entrega.value);		    	
				direccion_entrega.trim();
    			poblacion_entrega.trim();    		
				cp_entrega.trim();
				if (direccion_entrega == "" || poblacion_entrega == '' || cp_entrega == '')
				{
					msg += "- Dirección, CP y población de entrega \n"; 
					env = false;
				}
			}
		}
	}

	if (!env)
	{
		alert(msg);
	}
	return env;
}

function check_numbers()
{
	tlfno1 = new String(document.frm.tlfno1.value);
    movil1 = new String(document.frm.movil1.value);
    cp = new String(document.frm.cp.value);

	tlfno1.trim();
	movil1.trim();
	cp.trim();

	var msg = "Los siguientes campos deben ser numéricos:\n";
	var env = true;

	if (tlfno1 != "")
	{
		if (isNaN(tlfno1))
		{
			msg += "- Teléfono fijo\n";
			env = false;
		}
	}
    if (movil1 != "")
	{
		if (isNaN(movil1))
		{
			msg += "- Teléfono móvil\n";
			env = false;
		}
	}
    if (cp != "")
	{
		if (isNaN(cp))
		{
			msg += "- Código postal\n";
			env = false;
		}
	}
	if (!env)
	{
		alert(msg);
	}
	return env;
}

function check_pago()
{	
	var noPago = true;
	
	for (i = 0;i < document.frm.id_forma_pago.length; i++)
    {
		if (document.frm.id_forma_pago[i].checked)
		{
			noPago = false;
			break;
		}
    }
	
	if (noPago)
	{
		alert("Debe seleccionar una forma de pago\n");
		return false;
	}
	else
	{
		return true;
	}
	
}

function checkMail()
{
	email = new String(document.frm.email.value);
	email.trim();
	if (email != "")
	{
		if (!check_email(email))
		{
			alert("Debe introducir una dirección de correo electrónico válida");
			return false;
		}
		return true;
	}
	return true;
}

function check_condiciones()
{  
   if (!document.frm.chk_condiciones.checked)
   {
       alert("Debe marcar la casilla indicando que ha leído y está de acuerdo con las condiciones");
	   return false;
   }
   else
   {
   	   return true;
   }
}

function enviar_pedido()
{
	if ( checkTarjeta() && checkReembolso() && check_fields() && check_pago() && check_condiciones() && checkMail() && check_numbers())
	{
	    /*
		document.frm.provincia.value = document.frm.id_provincia[document.frm.id_provincia.selectedIndex].text;
        document.frm.pais.value = document.frm.id_pais[document.frm.id_pais.selectedIndex].text;
		*/
			document.frm.action = './make_order';
			document.frm.submit();
	}
}

function calcular_envio()
{
	pais = document.getElementById("id_pais_envio").selectedIndex;
	if (pais!=0) {
		document.frm.action = "../shopping/";
		document.frm.submit();
	}
}

function cambiar_pais()
{
	pais = document.getElementById("id_pais_envio2").selectedIndex;
	document.getElementById("id_pais_envio").selectedIndex = pais;
	if (pais!=0) {
		document.frm.action = "../shopping/#datos";
		document.frm.submit();
	}
}

function cambiar_provincia()
{
	provincia = document.getElementById("id_provincia_envio2").selectedIndex;
	document.getElementById("id_provincia_envio").selectedIndex = provincia;
	document.frm.action = "../shopping/#datos";
	document.frm.submit();
}

function checkProv()
{	
	pais = document.frm.id_pais[document.frm.id_pais.selectedIndex].value;
	if (pais != ID_PAIS_ESP )
	{
		document.frm.id_provincia.value = ID_PROV_OTRA;
	}
	else
	{
		document.frm.id_provincia.value = -1;		
	}
}

function checkPais()
{	
	prov = document.frm.id_provincia[document.frm.id_provincia.selectedIndex].value;
	if (prov != ID_PROV_OTRA)
	{
		document.frm.id_pais.value = ID_PAIS_ESP ;
	}	
}

function checkReembolso()
{
	enviar = true;
	pais = document.frm.id_pais_envio[document.frm.id_pais_envio.selectedIndex].value;
	for (i = 0; i < document.frm.id_forma_pago.length; i++)
    {
		if (document.frm.id_forma_pago[i].value == REEMBOLSO)
		{
			if (document.frm.id_forma_pago[i].checked)
			{
				if (pais != ID_PAIS_ESP)
				{
					alert("La opción de CONTRAREEMBOLSO es sólo para ESPAÑA\nPor favor, seleccione otra forma de pago");
					enviar = false;
					break;
				}
			}
		}
    }
	return enviar;
}

function checkTarjeta()
{

	var env = true;
	var msg = "Debe introducir los siguientes campos obligatorios:\n"

	for (i = 0; i < document.frm.id_forma_pago.length; i++)
	{
		if (document.frm.id_forma_pago[i].value == TARJETA)
		{

			//numtarj = document.frm.numtarj.value;
			//codcvv = document.frm.codcvv.value;
			//mescad = document.frm.mescad.value;
			//anocad = document.frm.anocad.value;

			if (document.frm.id_forma_pago[i].checked)
			{
			    numtarj = document.frm.numtarj.value;
			    codcvv = document.frm.codcvv.value;
			    mescad = document.frm.mescad.value;
			    anocad = document.frm.anocad.value;
			    if (numtarj == "")
			    {
					msg += "- Número de tarjeta\n";
					env = false;
			    }
			    if (codcvv == "")
			    {
					msg += "- Código de Seguridad\n";
					env = false;
			    }

			    if (mescad == "")
			    {
					msg += "- Fecha caducidad (mes)\n";
					env = false;
			    }
			    if (anocad == "")
			    {
					msg += "- Fecha caducidad (año)\n";
					env = false;
			    }
			}
		}
	}

	if (!env)
	{
		alert(msg);
	}
	return env;
}


/**
 * Sólo se permite la opción de contrarembolso cuando el pais es ESPAÑA
 */
function change_pago()
{
	if (checkReembolso())
	{
		document.frm.action = "../shopping/#forma";
		document.frm.submit();
	}
}

/**
 * Funcion para guardar y cargar en una cookie los datos del cliente
 */
function save_datos()
{
	set_cookie('nif', document.frm.nif.value);
	set_cookie('nombre', document.frm.nombre.value);
	set_cookie('apellido1', document.frm.apellido1.value);
	set_cookie('apellido2', document.frm.apellido2.value);
	set_cookie('direccion', document.frm.direccion.value);
	set_cookie('poblacion', document.frm.poblacion.value);
	set_cookie('cp', document.frm.cp.value);
	set_cookie('tlfno1', document.frm.tlfno1.value);
	set_cookie('movil1', document.frm.movil1.value);
	set_cookie('email', document.frm.email.value);
}

function load_datos()
{
	if(get_cookie('nif')!=null)
	{
	document.frm.nif.value = get_cookie('nif');
	}
	if(get_cookie('nombre')!=null)
	{
	document.frm.nombre.value = get_cookie('nombre');
	}
	if(get_cookie('apellido1')!=null)
	{
	document.frm.apellido1.value = get_cookie('apellido1');
	}
	if(get_cookie('apellido2')!=null)
	{
	document.frm.apellido2.value = get_cookie('apellido2');
	}
	if(get_cookie('direccion')!=null)
	{
	document.frm.direccion.value = get_cookie('direccion');
	}
	if(get_cookie('poblacion')!=null)
	{
	document.frm.poblacion.value = get_cookie('poblacion');
	}
	if(get_cookie('cp')!=null)
	{
	document.frm.cp.value = get_cookie('cp');
	}
	if(get_cookie('tlfno1')!=null)
	{
	document.frm.tlfno1.value = get_cookie('tlfno1');
	}
	if(get_cookie('movil1')!=null)
	{
	document.frm.movil1.value = get_cookie('movil1');
	}
	if(get_cookie('email')!=null)
	{
	document.frm.email.value = get_cookie('email');
	}
}
