// Conjunto de elementos que definen la interfaz de Printmax
var gPrintmaxVersion = '1.1';

// Sistema de acceso
var gPrintmaxSistemaId = 1;

// Comprobación de Usuario
var gPrintmaxRegistrarUsuario = gPrintmaxWebInterfaceURL + "RegistrarUsuario";
var gPrintmaxComprobarLogin   = gPrintmaxWebInterfaceURL + "ComprobarLogin";
// Comprobación login de comercial
var gPrintmaxComprobarLoginCmr = gPrintmaxWebInterfaceURL + "ComprobarLoginCmr";

// Busqueda de Clientes desde cmr
var gPrintmaxBuscarClientesCmr = gPrintmaxWebInterfaceURL + "BuscarClientesCmr";

// Seleccionar un cliente desde cmr
var gPrintmaxSelectClienteCmr = gPrintmaxWebInterfaceURL + "SelectClienteCmr";

// Obtener datos de un cliente desde cmr
var gPrintmaxObtenerDatosClienteCmr = gPrintmaxWebInterfaceURL + "ObtenerDatosClienteCmr";

// Guardar datos de un cliente desde cmr
var gPrintmaxGuardarDatosClienteCmr = gPrintmaxWebInterfaceURL + "GuardarDatosClienteCmr";

// Notificar Correo
var gPrintmaxNotificacionCorreo = gPrintmaxWebInterfaceURL + "NotificacionCorreo";

// Alta de cliente
var gPrintmaxDatosRefCliente = gPrintmaxWebInterfaceURL + "DatosReferenciaCliente";
var gPrintmaxAltaCliente     = gPrintmaxWebInterfaceURL + "ClienteAltaWeb";
var gPrintmaxVerificarClienteDuplicado = gPrintmaxWebInterfaceURL + "VerificarClienteDuplicado";

// Obtener datos de cliente
var gPrintmaxDatosCliente = gPrintmaxWebInterfaceURL + "DatosCliente";

// Obtener filtros de seleccion de tarifas
var gPrintmaxTarifaFiltro     = gPrintmaxWebInterfaceURL + "TarifaFiltro";
var gPrintmaxTarifaFiltroComp = gPrintmaxWebInterfaceURL + "TarifaFiltroComp";

// Obtener detalles y ejemplares de tarifa
var gPrintmaxTarifaDetalle = gPrintmaxWebInterfaceURL + "TarifaDetalle";

// Hacer pedido desde tarifa o desde formulario
var gPrintmaxTarifaPedido = gPrintmaxWebInterfaceURL + "TarifaPedido";
var gPrintmaxPresupuestoPedido = gPrintmaxWebInterfaceURL + "PresupuestoPedido";

// Generar y enviar la carta del presupuesto
var gPrintmaxGenerarCarta = gPrintmaxWebInterfaceURL + "GenerarCarta";
var gPrintmaxEnviarCarta = gPrintmaxWebInterfaceURL + "EnviarCarta";

// Obtener listado de presupuestos
var gPrintmaxPresupuestos = gPrintmaxWebInterfaceURL + "Presupuestos";

// Cambiar el estado de un presupuesto
var gPrintmaxActualizarPresup = gPrintmaxWebInterfaceURL + "ActualizarPresupuesto";

// Llamada antes de aceptar un presupuesto
var gPrintmaxDatosPresup = gPrintmaxWebInterfaceURL + "DatosPresupuesto";

// Obtener listado de Órdenes
var gPrintmaxOrdenes      = gPrintmaxWebInterfaceURL + "Ordenes";
var gPrintmaxPruebasOrden = gPrintmaxWebInterfaceURL + "DetalleOrdenTaller";

// Obtener listado de Albaranes
var gPrintmaxAlbaranes = gPrintmaxWebInterfaceURL + "Albaranes";
var gPrintmaxDetalleAlbaran = gPrintmaxWebInterfaceURL + "DetalleAlbaran";

// Obtener listado de Facturas
var gPrintmaxFacturas       = gPrintmaxWebInterfaceURL + "Facturas";
var gPrintmaxDetalleFactura = gPrintmaxWebInterfaceURL + "DetalleFactura";

// Listado y nuevos registros CMR
var gPrintmaxSeguimientoCmr = gPrintmaxWebInterfaceURL + "SeguimientoCmr";
var gPrintmaxGuardarCmr = gPrintmaxWebInterfaceURL + "GuardarCmr";

// Adjuntar archivo al presupuesto
var gPrintmaxAdjuntar = gPrintmaxWebInterfaceURL + "AdjuntarArchivo";


/*
 *  Funciones comunes
 */

function printmaxLogout()
{
	delCookie('printmax_loginid');
	delCookie('printmax_loginuser');
  delCookie("printmax_session");

	window.location = gPrintMaxEndPage;
}

function printmaxCmrLogout()
{
	delCookie('printmax_cmrid');
	window.location='logincmr.php';
}

function printmaxRefreshLogin()
{
	// Refrescamos la sesión del usuario
	lId = getCookie("printmax_loginid");
	if (parseInt(lId)>0) {
		// Renovamos la caducidad de la cookie si se estan haciendo operaciones
		lUser = getCookie("printmax_loginuser");
		lSession = getCookie("printmax_session");

		setCookieLT("printmax_loginid", lId, gPrintmaxDuracionSesion*60);
		setCookieLT("printmax_loginuser", lUser, gPrintmaxDuracionSesion*60);
		setCookieLT("printmax_session", lSession, gPrintmaxDuracionSesion*60);
	}

	// Y también la del comercial
	lId = getCookie("printmax_cmrid");
	if (parseInt(lId)>0) {
		// Renovamos la caducidad de la cookie si se estan haciendo operaciones
		lUser = getCookie("printmax_cmrname");
		lSession = getCookie("printmax_cmrsession");

		setCookieLT("printmax_cmrid", lId, gPrintmaxDuracionSesion*60);
		setCookieLT("printmax_cmrname", lUser, gPrintmaxDuracionSesion*60);
		setCookieLT("printmax_cmrsession", lSession, gPrintmaxDuracionSesion*60);
	}

}


/*
 * Llamada cuando se ha caducado la sesión al realizar una petición al CGI
 * Actualmente solo hace LogOut
 */
function sessionExpired()
{
  debugLog('Sesión caducada');
  printmaxLogout();
}


function ResaltarLinea(aLinea) {
	aLinea.style.backgroundColor = "#FFC";//"#FFD942";
	aLinea.style.cursor = "pointer";
}

function DesresaltarLinea(aLinea) {
	// Comprobamos si la linea está abierta o no, para dejarla
	// con el color gris o quitarselo por completo.
	aLinea.style.backgroundColor = "";
	aLinea.style.cursor = "default";
	if ((aLinea.style.borderTop != "") && (aLinea.style.borderTop.indexOf("none")<0))
		aLinea.style.backgroundColor = "#FFC";//"#EEE";
}


function ResetearFiltroTarifa(aFiltro, aDefaultOption)
{
	var lSelect = document.getElementById(aFiltro);
	while (lSelect.length>0) lSelect.remove(0);
	if (aDefaultOption != null) AddOpcionFiltroTarifa(aFiltro,aDefaultOption,aDefaultOption,aDefaultOption);
}


function AddOpcionFiltroTarifa(aFiltro, aTexto, aValor, aSelected)
{
	if (aValor != null) {
		var lSelect = document.getElementById(aFiltro);
		var lOption = document.createElement("option");
		lOption.text = aTexto;
		lOption.value = aValor;
		if (aValor == aSelected) lOption.selected = true;
		try {
			lSelect.add(lOption,null); // standards compliant
		} catch(ex) {
			lSelect.add(lOption); // IE only
		}
	}
}


function myescape(aText)
{
	aText = escape(aText);
	return aText.replace(/\+/gi,'%2B');
}


function myencodeURI(aText)
{
	var lText = encodeURIComponent(aText);
	lText = lText.replace(/%C2%AA/gi, '%AA');
	lText = lText.replace(/%C2%BA/gi, '%BA');
	//alert(aText + '\n\n' + lText);
	return lText;
}
function BlankXml(aText)
{
	if (aText == "-") return ""
	else return aText;
}


function uploadDialog(idPresup)
{
	var div = $('<div/>', {'id': 'upload_dialog', 'title': 'Adjuntar un documento'});
	$('body').append(div);

	div.append($('<iframe/>', {
    id: 'upload',
    frameborder: 0,
    border: 0,
    src: gPrintMaxUploadDir + 'upload.php?id=' + idPresup + '&session=' +
         getCookie("printmax_session") + '&loginid=' + getCookie("printmax_loginid"),
    scrolling: 'no',
    scrollbar: 'no'
  }));

  //div.load('upload.php?id=' + idPresup);
	div.dialog({
		modal:     true,
		resizable: false,
		width:     485,
		height:    375,
		close:     function() {$('#upload_dialog').remove()},
		buttons:   {'Cerrar': function() {$('#upload_dialog').remove()}}
	});
}


/*function waitScreen(show)
{
  var div;
  if (show)
    this.divWait.dialog('open');
  else
    this.divWait.dialog('close');
  if (show) {
    $('#ContentCenter').busyBox({
      spinner: '<img src="img/printing.gif" />'
    });
  }
  else {
    $('#ContentCenter').busyBox('close');
  }
}*/


// Para la búsqueda por fecha en los jqGrid
setDatePicker = function(elem) {
  $(elem).datepicker();
}


function datosAsistente()
{
  $('#imgAsistente').attr('src', gPrintmaxWebAssistantImg + getCookie('printmax_asist_id') + '.png');
  $('.nombreAsistente').html(getCookie('printmax_asist_name'));
  $('a.emailAsistente').attr('href', 'mailto:' + getCookie('printmax_asist_email'));
  $('span.emailAsistente').html(getCookie('printmax_asist_email'));

  $('.emailAsistente').removeClass('displaynone');
}

