/* variables */

var topCenter = 0;
var leftCenter = 0;
var imgFramesPath = "/BQBWeb/img/frames/";
var imgBotonesPath = "/BQBWeb/img/botones/";

var botonesDisabled = new Array();


function cambiarIdioma(idioma) {
		
	if (idioma == "ES") {
		window.location.href='/BQBWeb/Home';		  
	} 
		else if (idioma == "EN") {
				window.location.href = '/BQBWeb/HomeENG';
	} 
		else if (idioma == "PO") {
				window.location.href = '/BQBWeb/HomePOR';
	}
}
	


function muestraMenu(id){
	/*
	$(tr).unbind("click");
	$(tr).toggle(function(){$('tr[name='+id+']').show;},function(){$('tr[name='+id+']').hide;});
	$(tr).click();
	*/
	/*if($('tr[name='+id+']').css("display") == "none")
		$('tr[name='+id+']').show();
	else
		$('tr[name='+id+']').hide();*/
}

function cambiaColorTR(theTr,color,over){
	theTr.style.backgroundColor = color;
	if (over)
		theTr.style.cursor = "pointer";
	else
		theTr.style.cursor = "default";	
}

function MouseOverBoton(theID){

	if(!botonHabilitado(theID))
		return;
		
	var izq = document.getElementById("TDBI_"+theID);
	var med = document.getElementById("TDBM_"+theID);
	var der = document.getElementById("TDBD_"+theID);

	izq.style.backgroundImage = "url("+imgBotonesPath+"on_izq.gif)";
	med.style.backgroundImage = "url("+imgBotonesPath+"on_med.gif)";
	der.style.backgroundImage = "url("+imgBotonesPath+"on_der.gif)";
	med.style.color = "#58595B";
	izq.style.cursor = "pointer";
	med.style.cursor = "pointer";
	der.style.cursor = "pointer";

	
}

function MouseOutBoton(theID){
	if(!botonHabilitado(theID))
		return;
	
	var izq = document.getElementById("TDBI_"+theID);
	var med = document.getElementById("TDBM_"+theID);
	var der = document.getElementById("TDBD_"+theID);

	izq.style.backgroundImage = "url("+imgBotonesPath+"off_izq.gif)";
	med.style.backgroundImage = "url("+imgBotonesPath+"off_med.gif)";
	der.style.backgroundImage = "url("+imgBotonesPath+"off_der.gif)";
	
	med.style.color = "#FFFFFF";	
}

function EnableBoton(theID){
	var izq = document.getElementById("TDBI_"+theID);
	var med = document.getElementById("TDBM_"+theID);
	var der = document.getElementById("TDBD_"+theID);

	izq.style.backgroundImage = "url("+imgBotonesPath+"off_izq.gif)";
	med.style.backgroundImage = "url("+imgBotonesPath+"off_med.gif)";
	der.style.backgroundImage = "url("+imgBotonesPath+"off_der.gif)";
	
	med.style.color = "#FFFFFF";	
	izq.style.cursor = "pointer";
	med.style.cursor = "pointer";
	der.style.cursor = "pointer";
	setValorEstadoBoton(theID, true,null);
}

function DisableBoton(theID){

	var izq = document.getElementById("TDBI_"+theID);
	var med = document.getElementById("TDBM_"+theID);
	var der = document.getElementById("TDBD_"+theID);
	var tr = document.getElementById("TRB_"+theID);

	izq.style.backgroundImage = "url("+imgBotonesPath+"dis_izq.gif)";
	med.style.backgroundImage = "url("+imgBotonesPath+"dis_med.gif)";
	der.style.backgroundImage = "url("+imgBotonesPath+"dis_der.gif)";
	med.style.color = "#FFFFFF";	
	izq.style.cursor = "default";
	med.style.cursor = "default";
	der.style.cursor = "default";	
	setValorEstadoBoton(theID, false,tr.onclick);
	tr.onclick = "";
}

function botonHabilitado(theId){
	
	var ret = true;
	for (var i=0;i<botonesDisabled.length;++i){
		if (botonesDisabled[i][0] == theId){
			ret = botonesDisabled[i][1];
			break;
		}
	}
	return ret;
}

function setValorEstadoBoton(theId, valor, onClickAction){
	var encontrado = false;
	for (var i=0;i<botonesDisabled.length;++i){
		if (botonesDisabled[i][0] == theId){
			botonesDisabled[i][1] = valor;
			encontrado = true;
			break;
		}
	}
	if (!encontrado)
		botonesDisabled[botonesDisabled.length] = new Array(theId,valor,onClickAction);
}

function desabilitaBotonesDeForm(formulario){
	for (var i=0;i<document.all.length;++i){
		if(document.all[i].id.indexOf("TRB_")==0){
			DisableBoton(document.all[i].id.substring(4,document.all[i].id.length));
		}
	}
}

function habilitaBotonesDeForm(formulario){
	for (var i=0;i<document.all.length;++i){
		if(document.all[i].id.indexOf("TRB_")==0){
			EnableBoton(document.all[i].id.substring(4,document.all[i].id.length));
		}
	}
}


function cambiaColorFondo(theTr,color){
	theTr.style.backgroundColor = color;
}


function irAURLSiConfirma(confirmaMensaje, url){
	if (confirm(confirmaMensaje))
		location.href = url;
}

function verUsuarioLogueado(){


	var esta = document.cookie.indexOf("BQB_LOGGED_USER=");
	var htmlEscribe = "";
	var linkLogin = "";
	var textoLogin = "";	
	
	if (esta == -1){
		//alert("No esta logueado");
		//htmlEscribe = "<A CLASS=\"frame_texto_blanco\" HREF=\"com.buquebus.web.login.LoginUsuario\">Log in</A>";
		linkLogin = "com.buquebus.web.login.LoginUsuario";
		textoLogin = "LOG IN";
		htmlEscribe = "";
	
	}
	else{
		var fin = document.cookie.indexOf(";",esta+16);
		
		if (fin == -1)
			fin = document.cookie.length;
			
		var valorLogueado = unescape(document.cookie.substring(esta+16,fin));
		
		htmlEscribe = valorLogueado+"&nbsp;|&nbsp;";
		linkLogin = "com.buquebus.web.login.LogoutUsuario";
		textoLogin = "LOG OUT";
		
	}
	
	loginPropertiesName.innerHTML = htmlEscribe;
	loginPropertiesLink.href = linkLogin
	loginPropertiesLink.innerHTML = textoLogin;
	

	cargarImagenes();
	
}


var imgPreload1 = new Image();
var imgPreload2 = new Image();
var imgPreload3 = new Image();	
var imgPreload4 = new Image();	
var imgPreload5 = new Image();	
var imgPreload6 = new Image();	
var imgPreload7 = new Image();	
var imgPreload8 = new Image();	
var imgPreload9 = new Image();	
var imgPreload10 = new Image();	
function cargarImagenes(){
	imgPreload1.src = imgFramesPath+"band_arg_down.gif";
	imgPreload2.src = imgFramesPath+"band_uy_down.gif";
	imgPreload3.src = imgBotonesPath+"on_der.gif";
	imgPreload4.src = imgBotonesPath+"on_izq.gif";
	imgPreload5.src = imgBotonesPath+"on_med.gif";
	imgPreload6.src = imgBotonesPath+"off_der.gif";
	imgPreload7.src = imgBotonesPath+"off_izq.gif";
	imgPreload8.src = imgBotonesPath+"off_med.gif";	
	imgPreload9.src = imgBotonesPath+"band_arg_on.gif";
	imgPreload10.src = imgFramesPath+"band_uy_on.gif";		
}



var dias = new Array();
var meses = new Array();


meses[0] = 'Enero';
meses[1] = 'Febrero';
meses[2] = 'Marzo';
meses[3] = 'Abril';
meses[4] = 'Mayo';
meses[5] = 'Junio';
meses[6] = 'Julio';
meses[7] = 'Agosto';
meses[8] = 'Septiembre';
meses[9] = 'Octubre';
meses[10] = 'Noviembre';
meses[11] = 'Diciembre';

dias[0] = 31;
dias[2] = 31;
dias[3] = 30;
dias[4] = 31;
dias[5] = 30;
dias[6] = 31;
dias[7] = 31;
dias[8] = 30;
dias[9] = 31;
dias[10] = 30;
dias[11] = 31;












// define where it's MSIE compatible or it's not.
var isMSIE = (navigator.appVersion.indexOf("MSIE")==-1) ? false : true;






/* source */

function setPosition (nwWidth, nwHeight)
{
clWidth = screen.width;
clHeight = screen.height;

topCenter = (clHeight/2)-(nwHeight/2);
leftCenter = (clWidth/2)-(nwWidth/2);
}


function mDe(){
	
	$("body").append('<div id="divBlock" class="div_block"></div>');
	$("#divBlock").fadeTo(1, 0, function(){
		$("#divBlock").css({"height": $(document).height(), "width": $(document).width()});
		$("#divBlock").fadeTo("slow", 0.55)});
	
}

function mDa(mensaje){
	
mDe();

$("body").append('<div id="divAviso" class="div_datos1 TRAvisosGrande">'+mensaje+'</div>');
$("#divAviso").css({"left":(($(document).width()/2)-($("#divAviso").width()/2)),"top":"200px"});
$("#divAviso").slideDown("slow");

}


function muestraDivEspera(){
	  
	  
	  var divBlock = document.getElementById("divBlocId");
	  var divAviso = document.getElementById("divAviso");
	  var anchoVentana = $(document).width();		 
	
	  if(divAviso != null){
		  if(divAviso.style.display == "none"){
			  divBlock.style.height = $(document).height();
			  divBlock.style.width = $(document).width();
			  var anchoDiv = $("#divAviso").width();
			  var xVentana = (anchoVentana/2)-(anchoDiv/2);
			  divAviso.style.left = xVentana;
			  divAviso.style.display = "block";
			  divBlock.style.display = "block";
			  
			  $("#divBlocId").fadeTo("slow", 0.55);
		  }
	  }
}


function obtieneValorRadio(radioObj){
	var retValue = "";
	for (var i=0;i<radioObj.length;++i){
		if (radioObj[i].checked){
		    retValue = radioObj[i].value;
		}
	}

	return retValue;
}


var FLASH_CLASSID = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
//var FLASH_CODEBASE = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0";
var FLASH_CODEBASE = "/BuquebusLocal/cab/swflash.cab";
var FLASH_PLUGINPAGE = "http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash";

function objectFlash(src,width,height){

var html = "<object classid=\""+FLASH_CLASSID+"\" codebase=\""+FLASH_CODEBASE+"\" width=\""+width+"\" height=\""+height+"\">";
html += "<param name=movie value=\""+src+"\">";
html += "<param name=quality value=high>";
html += "<param name=\"wmode\" value=\"transparent\">";
html += "<embed src=\""+src+"\" quality=high pluginspage=\""+FLASH_PLUGINPAGE+"\"  width=\""+width+"\" height=\""+height+"\"  type='application/x-shockwave-flash'> ";
html += "</embed>";
html += "</object>";

document.write(html);

}



var wChild = null;
var wChildOpen = false;

/*******************************************************************************************
* Esta funcion se ejecuta en cada focus de la pagina.
* Chequea si se encuentra abierta un popup y si se encuentra abierta 
* la ventana de LOV.
********************************************************************************************/
function onFocusEvent(){
	
	try{
		checkWChildOpen();
	}
	catch(ee){}

}

//Seteo aca el evento que se ejecuta en el onFocus.
window.onfocus = onFocusEvent;

/*******************************************************************************************
* Esta funcion chequea si hay una popup abierto y lo cierra.
********************************************************************************************/
function checkWChildOpen(){

	if (wChildOpen){
		wChild.close();
		wChildOpen = false;
	}
}

function openPopUp(parUrl,parAttributes){
	openPopUp(parUrl,parAttributes,false);
}

/*******************************************************************************************
* Esta funcion se abre un popup.
* Revisa los atributos que vienen, si encuentra ancho y alto, entonces la popup 
* va a ser centrada
********************************************************************************************/
function openPopUp(parUrl,parAttributes,paginaIndependiente){

	var attrsOpen = null;

	try{
		//Separo los atributos para poder usarlos
		separaAtributos(parAttributes);

	 	var theLeft=(screen.width-getWidth())/2;
		var theTop=(screen.height-getHeight())/2;


		agregaPropiedad("TOP",theTop);
		agregaPropiedad("LEFT",theLeft);
		
		attrsOpen = obtieneAtributosListo();
		
		//Elimino los atributos
		wChildAttrs = null;
		
	}
	//Si hay algun error, toma los atributos que entro,
	catch(ee){
		attrsOpen = parAttributes;
	}

	if (paginaIndependiente){
		window.open(parUrl,"wChildInd",attrsOpen)
	}
	else{
		checkWChildOpen();
		wChildOpen = true;
		wChild = window.open(parUrl,"wChild",attrsOpen);
		wChild.focus();
	}
	
}


var wChildAttrs = null;

/*******************************************************************************************
* Obtiene la lista de atributos para abrir un popup.
* Los valores estan separados por ";" y el par nombre-valor por "="
********************************************************************************************/
function obtieneAtributosListo(){
	var out = null;
	
	for (var i=0;i<wChildAttrs.length;++i){
		out = (out == null ? "" : out+", ");
		out += wChildAttrs[i][0].toUpperCase();
		out += "=";
		out += wChildAttrs[i][1];
	}
	
	return out;
}

/*******************************************************************************************
* Esta funcion agrega una propiedad al vector de propiedades.
********************************************************************************************/
function agregaPropiedad(nombre,valor){

	if (!updateProperty(nombre,valor)){
		var prop = new Array();
		prop[0] = nombre;
		prop[1] = valor;	
		wChildAttrs[wChildAttrs.length] = prop;
	}


}

/*******************************************************************************************
* Esta funcion separa los atributos de un popup para poder analizarlos y usarlos en 
* caso de ser necesarios (se van a utilizar para obtener el ancho y el alto y luego 
* determinar la posicion de la ventana).
********************************************************************************************/
function separaAtributos(parAttributes){
	wChildAttrs = new Array();
	
	//Si no hay atributos, entonces no retorno nada.
	if (parAttributes == "") return;
	
	//Determino el token que separa todas las propiedades, por defecto es ";", pero
	//si no hay ";" en el string y encuentro "," entonces tomo la "," como el token
	var token = ";";
	if (parAttributes.indexOf(";")==-1 && parAttributes.indexOf(",")>=0) token = ",";
	
	//Obtengo las propiedades
	var attrsAux = parAttributes.split(token);
	
	//Recorro todas las propiedades obtenidas y obtengo los valores de las
	//propiedades y las voy colocando el el vector.
	for (var i=0;i<attrsAux.length;++i){
	
		var valores = obtieneValores(attrsAux[i]);
	
		if (valores != null){
			wChildAttrs[wChildAttrs.length] = valores;
		}
	
	}
		
}

/*******************************************************************************************
* Funcion que obtiene el ancho de la ventana que se va a abrir.
* Obtiene la propiedad de ancho, si corresponde, si la pudo obtener, entonces retorno
* una representacion en entero de la misma, sino, retorno 0
********************************************************************************************/
function getWidth(){
	var prop = getProperty("width");
	if (prop == null || prop == "" || isNaN(prop)){
		return 0;
	}
	else{
		return parseInt(prop);
	}
}

/*******************************************************************************************
* Funcion que obtiene el alto de la ventana que se va a abrir.
* Obtiene la propiedad de ancho, si corresponde, si la pudo obtener, entonces retorno
* una representacion en entero de la misma, sino, retorno 0
********************************************************************************************/
function getHeight(){
	var prop = getProperty("height");
	if (prop == null || prop == "" || isNaN(prop)){
		return 0;
	}
	else{
		return parseInt(prop);
	}
}

/*******************************************************************************************
* Obtiene una propiedad por su nombre.
* Transformo las cadenas a upper case, con el objetivo de poder hacer una mejor busqueda
* y reducir los errores
********************************************************************************************/
function getProperty(nombrePropiedad){
	for (var i=0;i<wChildAttrs.length;++i){
		
		if (wChildAttrs[i][0].toUpperCase() == nombrePropiedad.toUpperCase()){
			return wChildAttrs[i][1];
			break;
		}	
	}
	
	return null;
}

/*******************************************************************************************
* Esta funcion actualiza el valor de una propiedad.
* Si se actualizo, entonces retorna true, si no se puedo actualizar por que no se
* encontro la propiedad, entonces retorno false.
********************************************************************************************/
function updateProperty(nombrePropiedad,valorPropiedad){
	for (var i=0;i<wChildAttrs.length;++i){
		
		if (wChildAttrs[i][0].toUpperCase() == nombrePropiedad.toUpperCase()){
			wChildAttrs[i][1] = valorPropiedad;
			return true;
			break;
		}	
	}
	
	return false;
}

/*******************************************************************************************
* Funcion que saca los espacios en blanco al comienzo y al fin de una cadena.
********************************************************************************************/
function trim(str){
	
	var out = str;

	//Saco los espacios al comienzo
	while(out.indexOf(" ") == 0){
		if (out.length>1) out = out.substring(1);
		else out = "";
	}
	
	//Saco los espacios al final
	while(out.length > 0 && out.lastIndexOf(" ") == out.length-1){
		if (out.length>1) out = out.substring(0,out.length-1);
		else out = "";
	}
	
	return out;
}

/*******************************************************************************************
* Esta funcion obtiene un array con 2 strings, un nombre y un valor de una cadena que puede
* estar separado por "=" o por ":"
********************************************************************************************/
function obtieneValores(parAtributo){

	//Si el atributo es nulo, entonces retorno null
	if (parAtributo == null && parAtributo == "") return null;

	//Determino el token que separa el nombre del valor, por defecto es "=", pero
	//si no hay "=" en el string y encuentro ":" entonces tomo la ":" como el token
	var token = "=";
	if (parAtributo.indexOf("=")==-1 && parAtributo.indexOf(":")>=0) token = ":";
	
	//Creo un array con el par nombre valor
	var out = parAtributo.split(token);
	
	
	//Si la longitud no es la correcta, entonces no hago nada
	if (out.length != 2) return null;
	else {
		out[0] = trim(out[0]);
		out[1] = trim(out[1]);		
		return out;
	}
	
	
}


