/* variables */

var topCenter = 0;
var leftCenter = 0;
var imgFramesPath = "/BQBWeb/img/frames/";
var imgBotonesPath = "/BQBWeb/img/botones/";

var botonesDisabled = new Array();




function muestraMenu(id){
	/*
	if($('tr[name='+id+']').css("display") == "none")
		$('tr[name='+id+']').show();
	else
		$('tr[name='+id+']').hide();
*/
return;
}

function cambiaColorTR(theTr,color,over){
	theTr.style.backgroundColor = color;
	if (over)
		theTr.style.cursor = "hand";
	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";s
	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 generateDate (parDay, parMonth, parYear)
{
	var date = parDay + '/' + parMonth  + '/' + parYear;

	return date;
}









function changeDate(dayI, monthValue, yearValue) {

	if (((yearValue%400) == 0) || (((yearValue%100) !=0) && ((yearValue%4) == 0)))
		dias[1]=29;
	else
		dias[1]=28;


	dayI.length = dias[monthValue - 1];


	for (var cDia=0; cDia < dias[monthValue - 1]; cDia++) { 

		dayI.options[cDia].text = cDia+1;
		dayI.options[cDia].value = cDia+1;

	}

}




function obtieneValorRadio(radioObj){
	var retValue = "";
	for (var i=0;i<radioObj.length;++i){
		if (radioObj[i].checked){
		    retValue = radioObj[i].value;
		}
	}

	return retValue;
}





function corregirFecha(parDate){
	var separatedStr = parDate.split("/");
	if (separatedStr.length < 3) return null;
	today = new Date();
	var year = today.getFullYear().toString();
	for (var i=0; i < separatedStr.length ;i++)
	{
		if (separatedStr[i]==NaN) return null;
		for (var j = 0; j < 2 - separatedStr[i].length;j++)
		{
			separatedStr[i] = "0" + separatedStr[i];
		}
	}
		if ((parseInt(separatedStr[0])<1 || parseInt(separatedStr[1])<1)
		    && separatedStr[1]!="08" && separatedStr[1]!="09") return null;
		if (parseInt(separatedStr[2])<100)
			if (parseInt(separatedStr[2])<parseInt(year.substring(2,4)))
				separatedStr[2]=year.substring(0,2)+separatedStr[2];
			else
				separatedStr[2]=(parseInt(year.substring(0,2))-1).toString()
							+separatedStr[2];
		if (parseInt(separatedStr[2])<1000) return null;
	switch(separatedStr[1]){
		case "01":
		case "03":
		case "05":
		case "07":
		case "08":
		case "10":
		case "12":
			if (parseInt(separatedStr[0]) > 31)
				return null;
			break;
		case "04":
		case "06":
		case "09":
		case "11":
			if (parseInt(separatedStr[0]) > 30)
				return null;
			break;
		case "02":
			if (parseInt(separatedStr[0]) > 29)
				return null;
			if (parseInt(separatedStr[0])>28 
				&& separatedStr[2]%4!=0)
				return null;
			break;
		default:
				return null;
		}
return (separatedStr[0]+"/"+separatedStr[1]+"/"+separatedStr[2]);
}


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 += "<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;
	}
	
	
}

