var lngDespachoExpress = 0;

function chgLayer(pDiv, pAction) {
	document.getElementById(pDiv).style.display = pAction;
}

function chgBusqueda(pSel1, pSel2) {
	if (Obj_Selec(pSel1) > 0)
		Selec_Obj_Valor(pSel2, '');
}

function BusquedaRapida() {
	if ((Obj_Selec(document.frmBusqRap.selProducto) == 0) && (Obj_Selec(document.frmBusqRap.selPrecio) == 0) && document.frmBusqRap.txtPalabra.value == '') {
		alert('Debe seleccionar al menos un criterio de búsqueda.');
		return;
	}
	
	if (Obj_Selec(document.frmBusqRap.selProducto) != 0) {
		document.frmBusqRap.hidNumero.value = document.frmBusqRap.selProducto.options[Obj_Selec(document.frmBusqRap.selProducto)].value;
		document.frmBusqRap.action = 'producto.asp';
	} else {
		document.frmBusqRap.hidNumero.value = '';
		document.frmBusqRap.action = 'productos_listado.asp';
	}
	document.frmBusqRap.submit();
}

function VerMas(pNro) {
	document.frmInfo.hidNumero.value = pNro;
	document.frmInfo.action = 'producto.asp';
	document.frmInfo.target = '';
	document.frmInfo.submit();
}

function AgregarCarro(pNro, pEstilo) {
	document.frmInfo.hidNumero.value = pNro;
	document.frmInfo.action = 'carro_coord.asp';
	document.frmInfo.target = 'iframecoord';
	document.frmInfo.submit();
	document.getElementById('Agg' + pNro).className = 'agregar' + pEstilo + '_lleno';
}

function EliminarCarro(pNro) {
	document.frmInfo.hidNumero.value = pNro;
	document.frmInfo.hidAccion.value = 'E';
	document.frmInfo.action = 'carro_coord.asp';
	document.frmInfo.target = 'iframecoord';
	document.frmInfo.submit();
}

function AceptarPedido() {
	document.frmInfo.hidNumero.value = '';
	document.frmInfo.hidAccion.value = '';
	document.frmInfo.action = 'carro_coord.asp';
	document.frmInfo.target = 'iframecoord';
	document.frmInfo.submit();
}

function CargaSelect(pObj, pIndex, pArray) {
	var aArreglo = new Array;
	
	for (i=(pObj.options.length - 1); i>=0; i--)
		pObj.options[i] = null;
			
	aArreglo = pArray[pIndex];
	for (i=0; i<aArreglo.length;i++) {
		var op = new Option(aArreglo[i][1], aArreglo[i][0]);
		pObj.options[i] = op;
	}
	pObj.focus();
	return 0;
}

function Ingresar() {
	//Verifica Ingreso de Usuario
	if (document.frmLogin.txtUsuario.value == '') {
		alert('Debe ingresar un correo electrónico.');
		document.frmLogin.txtUsuario.focus();
		return;
	}
	if (!emailCheck(document.frmLogin.txtUsuario)) {
		document.frmLogin.txtUsuario.focus();
		return;
	}
	
	//Verifica Ingreso de Clave
	if (document.frmLogin.pwdClave.value == '') {
		alert('Debe ingresar su contraseña.');
		document.frmLogin.pwdClave.focus();
		return;
	}

	document.frmLogin.action = 'cliente_login.asp';
	document.frmLogin.target = 'iframecoord';
	document.frmLogin.submit();
}

function Registrar() {
	//Verifica Ingreso de Nombre
	if (document.frmLogin.txtNombre.value == '') {
		alert('Debe ingresar su nombre de cliente.');
		document.frmLogin.txtNombre.focus();
		return;
	}
	
	//Verifica Ingreso de Rut
	if (document.frmLogin.txtRut.value == '') {
		alert('Debe ingresar su Rut.');
		document.frmLogin.txtRut.focus();
		return;
	}
	if (document.frmLogin.txtRut.value != '')
		if (!checkRutField(document.frmLogin.txtRut)) {
			document.frmLogin.txtRut.focus();
			return;
		}

	//Verifica Ingreso de Email
	if (document.frmLogin.txtEmail.value == '') {
		alert('Debe ingresar su correo electrónico.');
		document.frmLogin.txtEmail.focus();
		return;
	}
	if (!emailCheck(document.frmLogin.txtEmail)) {
		document.frmLogin.txtEmail.focus();
		return;
	}

	//Verifica Ingreso de Código de Teléfono
	if (document.frmLogin.txtCodFono.value == '') {
		alert('Debe ingresar su Teléfono (código y número).');
		document.frmLogin.txtCodFono.focus();
		return;
	}

	//Verifica Ingreso de Número de Teléfono
	if (document.frmLogin.txtFono.value == '') {
		alert('Debe ingresar su Teléfono (código y número).');
		document.frmLogin.txtFono.focus();
		return;
	}

	document.frmLogin.action = 'cliente_login.asp';
	document.frmLogin.target = 'iframecoord';
	document.frmLogin.submit();
}

function Comprar() {
	//Verifica Cliente registrado
	if (document.frmInfo.hidUsuario.value == '') {
		//No registrado
		//Verifica Ingreso de Nombre
		if (document.frmInfo.txtNombre.value == '') {
			alert('Debe ingresar su nombre de cliente.');
			document.frmInfo.txtNombre.focus();
			return;
		}
	
		//Verifica Ingreso de Rut
		if (document.frmInfo.txtRut.value == '') {
			alert('Debe ingresar su Rut.');
			document.frmInfo.txtRut.focus();
			return;
		}
		if (document.frmInfo.txtRut.value != '')
			if (!checkRutField(document.frmInfo.txtRut)) {
				document.frmInfo.txtRut.focus();
				return;
			}

		//Verifica Ingreso de Email
		if (document.frmInfo.txtEmail.value == '') {
			alert('Debe ingresar su correo electrónico.');
			document.frmInfo.txtEmail.focus();
			return;
		}
		if (!emailCheck(document.frmInfo.txtEmail)) {
			document.frmInfo.txtEmail.focus();
			return;
		}
	} 

	//Verifica Ingreso de Código de Teléfono
	if (document.frmInfo.txtCodFono.value == '') {
		alert('Debe ingresar su Teléfono (código y número).');
		document.frmInfo.txtCodFono.focus();
		return;
	}

	//Verifica Ingreso de Número de Teléfono
	if (document.frmInfo.txtFono.value == '') {
		alert('Debe ingresar su Teléfono (código y número).');
		document.frmInfo.txtFono.focus();
		return;
	}

	//Cliente requiere de factura
	if (Obj_Check(document.frmInfo.rdoFactura) == 0) {
		//Verifica Ingreso de Razón Social de la Empresa
		if (document.frmInfo.txtEmpresaNombre.value == '') {
		    alert('Debe ingresar Razón Social de la Empresa.');
		    document.frmInfo.txtEmpresaNombre.focus();
		    return;
		}

		//Verifica Ingreso de Rut de la Empresa
		if (document.frmInfo.txtEmpresaRut.value == '') {
			alert('Debe ingresar el Rut de la Empresa.');
			document.frmInfo.txtEmpresaRut.focus();
			return;
		}
		if (document.frmInfo.txtEmpresaRut.value != '')
			if (!checkRutField(document.frmInfo.txtEmpresaRut)) {
				document.frmInfo.txtEmpresaRut.focus();
				return;
			}

		//Verifica Ingreso de Dirección de la Empresa
		if (document.frmInfo.txtEmpresaDireccion.value == '') {
		    alert('Debe ingresar Dirección de la Empresa.');
		    document.frmInfo.txtEmpresaDireccion.focus();
		    return;
		}

		//Verifica Elección Región de la Empresa
		if (Obj_Selec(document.frmInfo.selEmpresaRegion) == 0) {
			alert('Seleccione Región de la Empresa.');
			document.frmInfo.selEmpresaRegion.focus();
			return;
		}

		//Verifica Elección Comuna de la Empresa
		if (Obj_Selec(document.frmInfo.selEmpresaComuna) == 0) {
			alert('Seleccione Comuna de la Empresa.');
			document.frmInfo.selEmpresaComuna.focus();
			return;
		}

		//Verifica Ingreso de Giro de la Empresa
		if (document.frmInfo.txtEmpresaGiro.value == '') {
		    alert('Debe ingresar Giro de la Empresa.');
		    document.frmInfo.txtEmpresaGiro.focus();
		    return;
		}

		//Verifica Ingreso de Teléfono de la Empresa
		if (document.frmInfo.txtEmpresaFono.value == '') {
			alert('Debe ingresar el Teléfono de la Empresa.');
			document.frmInfo.txtEmpresaFono.focus();
			return;
		}
	}
	
	//Verifica Ingreso de Nombre del Destinatario
	if (document.frmInfo.txtDestinoNombre.value == '') {
	    alert('Debe ingresar Nombre del Destinatario.');
	    document.frmInfo.txtDestinoNombre.focus();
	    return;
	}

	//Verifica Ingreso de Dirección del Destinatario
	if (document.frmInfo.txtDestinoDireccion.value == '') {
	    alert('Debe ingresar Dirección del Destinatario.');
	    document.frmInfo.txtDestinoDireccion.focus();
	    return;
	}

	//Verifica Elección Región del Destinatario
	if (Obj_Selec(document.frmInfo.selDestinoRegion) == 0) {
		alert('Seleccione Región del Destinatario.');
		document.frmInfo.selDestinoRegion.focus();
		return;
	}

	//Verifica Elección Comuna del Destinatario
	if (Obj_Selec(document.frmInfo.selDestinoComuna) == 0) {
		alert('Seleccione Comuna del Destinatario.');
		document.frmInfo.selDestinoComuna.focus();
		return;
	}

	//Verifica Ingreso de Teléfono del Destinatario
	if (document.frmInfo.txtDestinoFono.value == '') {
		alert('Debe ingresar el Teléfono del Destinatario.');
		document.frmInfo.txtDestinoFono.focus();
		return;
	}

	if (document.frmInfo.hidUsuario.value != '') {
		if (document.frmInfo.chkDestinatario.checked) {
			//Verifica Ingreso de Seudónimo/Nombre Corto del Destinatario
			if (document.frmInfo.txtDestinoNombreCorto.value == '') {
				alert('Debe ingresar el Nombre Corto del Destinatario.');
				document.frmInfo.txtDestinoNombreCorto.focus();
				return;
			}
		}
	}
	
	//Verifica ingreso de datos según tipo de entrega
	switch (Obj_Check(document.frmInfo.rdoEntrega)) {
	case 0:
		//Verifica Ingreso de Código de Autorización
		if (document.frmInfo.txtEntregaCodigo.value == '') {
			alert('Debe ingresar el Código de Autorización.');
			document.frmInfo.txtEntregaCodigo.focus();
			return;
		}
		
		//Verifica Ingreso Valor de Despacho para Código de Autorización Válido
		if (document.frmInfo.hidDespacho.value == '') {
			alert('Debe ingresar el Código de Autorización Válido.');
			document.frmInfo.txtEntregaCodigo.focus();
			return;
		}
		break;
	case 1:
		break;
	case 2:
		//Verifica Ingreso de Programación de Entrega
		if (document.frmInfo.txtEntregaPrograma.value == '') {
			alert('Debe ingresar su Programación para la Entrega.');
			document.frmInfo.txtEntregaPrograma.focus();
			return;
		}
		break;
	}
	
	//Cliente requiere de factura
	if (Obj_Check(document.frmInfo.rdoFormaPago) < 0) {
		alert('Debe seleccionar una Forma de Pago.');
		document.frmInfo.rdoFormaPago[0].focus();
		return;
	}

	document.frmInfo.action = 'pedido_coord.asp';
	document.frmInfo.target = 'iframecoord';
	document.frmInfo.submit();
}

function chgFactura() {
	//Muestra formulario de datos de facturación
	if (Obj_Check(document.frmInfo.rdoFactura) == 0)
		chgLayer('div_factura', 'block');
	else
		chgLayer('div_factura', 'none');
}

function CalculoTotal(pObj) {
	var lngTotal = 0;
	var lngDespacho = 0;
	
	document.frmInfo.hidProductos.value = lngProductos;
	if (Obj_Check(document.frmInfo.rdoEntrega) > 0) {
		if (Obj_Selec(pObj) > 0) {
			lngDespacho = arrDespachos[parseInt(pObj.options[Obj_Selec(pObj)].value)];
			document.getElementById("spanDespacho").innerHTML = strFormatCurrency(lngDespacho, 'SP', false);
			document.frmInfo.hidDespacho.value = lngDespacho;
			if (arrPersonalizados[parseInt(pObj.options[Obj_Selec(pObj)].value)] == 'S') {
				document.getElementById("spanDespachoNombre").innerHTML = '&nbsp;PERSONALIZADO';
			} else {
				document.getElementById("spanDespachoNombre").innerHTML = '';
			}
			lngTotal = lngDespacho + lngProductos;
			document.getElementById("spanTotal").innerHTML = strFormatCurrency(lngTotal, 'SP', false);
			document.frmInfo.hidTotal.value = lngTotal;
		} else {
			document.getElementById("spanDespacho").innerHTML = '';
			document.frmInfo.hidDespacho.value = '';
			document.getElementById("spanDespachoNombre").innerHTML = '';
			lngTotal = lngProductos;
			document.getElementById("spanTotal").innerHTML = '';
			document.frmInfo.hidTotal.value = lngTotal;
		}
	} else {
		if (lngDespachoExpress >= 0) {
			document.getElementById("spanDespacho").innerHTML = strFormatCurrency(lngDespachoExpress, 'SP', false);
			document.frmInfo.hidDespacho.value = lngDespachoExpress;
			document.getElementById("spanDespachoNombre").innerHTML = '&nbsp;PERSONALIZADO';
			lngTotal = lngDespachoExpress + lngProductos;
			document.getElementById("spanTotal").innerHTML = strFormatCurrency(lngTotal, 'SP', false);
			document.frmInfo.hidTotal.value = lngTotal;
		} else {
			document.getElementById("spanDespacho").innerHTML = '';
			document.frmInfo.hidDespacho.value = '';
			document.getElementById("spanDespachoNombre").innerHTML = '';
			lngTotal = lngProductos;
			document.getElementById("spanTotal").innerHTML = '';
			document.frmInfo.hidTotal.value = lngTotal;
		}
	}
}

function chgEntrega() {
	//Habilita ingreso de datos según tipo de entrega
	switch (Obj_Check(document.frmInfo.rdoEntrega)) {
	case 0:
		document.frmInfo.txtEntregaCodigo.disabled = false;
		document.frmInfo.txtEntregaCodigo.focus();
		if (!document.frmInfo.txtEntregaPrograma.disabled)
			document.frmInfo.txtEntregaPrograma.value = '';
		document.frmInfo.txtEntregaPrograma.disabled = true;
		document.getElementById("rdoFormaPago_2").disabled = false;
		document.getElementById("spanDespachoNombre").innerHTML = '&nbsp;PERSONALIZADO';
		document.getElementById("spanDespacho").innerHTML = '';
		document.getElementById("spanTotal").innerHTML = '';
		break;
	case 1:
		if (!document.frmInfo.txtEntregaCodigo.disabled)
			document.frmInfo.txtEntregaCodigo.value = '';
		document.frmInfo.txtEntregaCodigo.disabled = true;
		if (!document.frmInfo.txtEntregaPrograma.disabled)
			document.frmInfo.txtEntregaPrograma.value = '';
		document.frmInfo.txtEntregaPrograma.disabled = true;
		document.getElementById("rdoFormaPago_2").checked = false;
		document.getElementById("rdoFormaPago_2").disabled = true;
		CalculoTotal(document.frmInfo.selDestinoComuna);
		break;
	case 2:
		if (!document.frmInfo.txtEntregaCodigo.disabled)
			document.frmInfo.txtEntregaCodigo.value = '';
		document.frmInfo.txtEntregaCodigo.disabled = true;
		document.frmInfo.txtEntregaPrograma.disabled = false;
		document.frmInfo.txtEntregaPrograma.focus();
		document.getElementById("rdoFormaPago_2").checked = false;
		document.getElementById("rdoFormaPago_2").disabled = true;
		CalculoTotal(document.frmInfo.selDestinoComuna);
		break;
	}
}

function chgDestinatario() {
	var lngIndice;
	
	lngIndice = Obj_Selec(document.frmInfo.selDestino);
	
	document.frmInfo.txtDestinoNombre.value = arrDestinatarios[lngIndice][0];
	document.frmInfo.txtDestinoDireccion.value = arrDestinatarios[lngIndice][1];
	Selec_Obj_Valor(document.frmInfo.selDestinoRegion, arrDestinatarios[lngIndice][2]);
	CargaSelect(document.frmInfo.selDestinoComuna, document.frmInfo.selDestinoRegion.selectedIndex, arrComunas)
	Selec_Obj_Valor(document.frmInfo.selDestinoComuna, arrDestinatarios[lngIndice][3]);
	CalculoTotal(document.frmInfo.selDestinoComuna);
	document.frmInfo.txtDestinoFono.value = arrDestinatarios[lngIndice][4];
	if (arrDestinatarios[lngIndice][5] == 'S')
		document.frmInfo.chkDestinatario.checked = true;
	else
		document.frmInfo.chkDestinatario.checked = false;
	document.frmInfo.txtDestinoNombreCorto.value = arrDestinatarios[lngIndice][6];
	document.frmInfo.txtDestinoNombre.focus();
}

function Contacto() {
	//Verifica Ingreso de Nombre
	if (document.frmInfo.txtNombre.value == '') {
		alert('Debe ingresar su Nombre.');
		document.frmInfo.txtNombre.focus();
		return;
	}
	
	//Verifica Ingreso de Apellido
	if (document.frmInfo.txtApellido.value == '') {
		alert('Debe ingresar su Apellido.');
		document.frmInfo.txtApellido.focus();
		return;
	}

	//Verifica Ingreso de Ciudad
	if (document.frmInfo.txtCiudad.value == '') {
		alert('Debe ingresar su Ciudad.');
		document.frmInfo.txtCiudad.focus();
		return;
	}

	//Verifica Ingreso de Código de Teléfono
	if (document.frmInfo.txtFonoCod.value == '') {
		alert('Debe ingresar su Teléfono (código y número).');
		document.frmInfo.txtFonoCod.focus();
		return;
	}

	//Verifica Ingreso de Número de Teléfono
	if (document.frmInfo.txtFono.value == '') {
		alert('Debe ingresar su Teléfono (código y número).');
		document.frmInfo.txtFono.focus();
		return;
	}

	//Verifica Ingreso de Email
	if (document.frmInfo.txtEmail.value == '') {
		alert('Debe ingresar su correo electrónico.');
		document.frmInfo.txtEmail.focus();
		return;
	}
	if (!emailCheck(document.frmInfo.txtEmail)) {
		document.frmInfo.txtEmail.focus();
		return;
	}

	document.frmInfo.action = 'contacto_coord.asp';
	document.frmInfo.target = 'iframecoord';
	document.frmInfo.submit();
}

/*	Realiza la Validación del Código de Autorización para Entrega Express
	Se ejecuta al haber cambio en el input */
function chgEntregaCodigo() {
	trim(document.frmInfo.txtEntregaCodigo);

	strHTTPVentana = "divExpCodImg";
	strHTTPPagina = "pedido_express_codigo_coord.asp?hidNumero=" + document.frmInfo.txtEntregaCodigo.value;
	makeRequest(strHTTPPagina);
}

/*	Realiza la captura de la solicitud de página
	con parámetro principal como String.	*/
function alertAction() {
	strHTTPVentana = "divExpCodImg";
	
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			Get_http_response = http_request.responseText;
			
			if (Get_http_response != "") {
				lngDespachoExpress = parseInt(Get_http_response);
				if (lngDespachoExpress > 0) {
					document.getElementById(strHTTPVentana).innerHTML = '<img src="images/icono_oc_aceptar.gif" alt="">';
				} else {
					document.getElementById(strHTTPVentana).innerHTML = '<img src="images/icono_oc_rechazar.gif" alt="">';
				}
			} else {
				lngDespachoExpress = -1;
				document.getElementById(strHTTPVentana).innerHTML = '';
			}
		} else {
			lngDespachoExpress = -1;
			alert('Hubo un problema con la solicitud.');
			document.getElementById(strHTTPVentana).innerHTML = '';
		}
		CalculoTotal(document.frmInfo.selDestinoComuna);
	}
}
