/******************************************************************************************************/
// usado no footer
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,resizable=0,width=650,height=450,left = 262,top = 159');");
}

/******************************************************************************************************/
// utilizar sempre essa função
function showCenterPopUp(url, wid, hei, params)
{
	w = screen.width;
	h = screen.height;
	meio_w = w/2;
	meio_h = h/2;

	largura = wid/2;
	altura = hei/2;
	meio1 = meio_h-altura;
	meio2 = meio_w-largura;
		
	window.open(url,'','width=' + wid + ',height=' + hei + ',top=' + meio1 + ',left=' + meio2 + ',' + params);
}

/******************************************************************************************************/
// nao utilizar ainda. 
function showCenterModalPopUp(url, wid, hei, params)
{
	w = screen.width;
	h = screen.height;
	meio_w = w/2;
	meio_h = h/2;

	largura = wid/2;
	altura = hei/2;
	meio1 = meio_h-altura;
	meio2 = meio_w-largura;
		
	window.showModalDialog(url,'','dialogWidth:' + wid + 'px;dialogHeight:' + hei + 'px;top=' + meio1 + 'px;dialogLeft:' + meio2 + 'px;' + params);
}

/******************************************************************************************************/
// utilizar sempre essa função
function showPopUp(url, params)
{
	showCenterPopUp(url, 800, 500, params);
}

/******************************************************************************************************/
function showMenu(obj, iframe, id, value)
{
	setTimeout("makeVisible('" + iframe + "','" + id + "','" + value + "')", 300);
	/*
	document.getElementById(id).style.visibility=value
	
	if (value == 'visible')
	{
		document.getElementById(iframe).style.width=document.getElementById(id).offsetWidth;
		document.getElementById(iframe).style.height=document.getElementById(id).offsetHeight;
	}
	else
	{
		document.getElementById(iframe).style.width=0;
		document.getElementById(iframe).style.height=0;
	}
	*/
}

/******************************************************************************************************/
function makeVisible(iframe, id, value)
{
    document.getElementById(id).style.visibility=value;
	
	if (value == 'visible')
	{
		document.getElementById(iframe).style.width=document.getElementById(id).offsetWidth;
		document.getElementById(iframe).style.height=document.getElementById(id).offsetHeight;
	}
	else
	{
		document.getElementById(iframe).style.width=0;
		document.getElementById(iframe).style.height=0;
	}
}

/******************************************************************************************************/
function showDropDownContent(obj,content)
{
	if (document.getElementById(content).style.display == 'none')
	{
		document.getElementById(content).style.display = 'block';
		obj.src = '../images/minus.gif';
	}
	else
	{
		document.getElementById(content).style.display = 'none';
		obj.src = '../images/plus.gif';
	}
}

/******************************************************************************************************/
function setColor(obj, color)
{
	obj.style.backgroundColor=color;
}

/******************************************************************************************************/
function setCursor(obj, style)
{
	obj.style.cursor = style;
}

/******************************************************************************************************/
function Enter(e, button)
{
	if (e && e.keyCode == 13)
	{
		__doPostBack(button,'');
		e.returnValue = false;
		e.cancel = true;
	}
	else
		return true;
}

/******************************************************************************************************/
function doSomething(action)
{
    __doPostBack('lnkDoSomething', action);
}

/******************************************************************************************************/
function saveHandler()
{
    if (document.getElementById('TripEditService1_lnkSaveHandler') != null)
        __doPostBack('TripEditService1$lnkSaveHandler', '');
}

/******************************************************************************************************/
function saveHotel()
{
    if (document.getElementById('TripEditService1_lnkSaveHotel') != null)
        __doPostBack('TripEditService1$lnkSaveHotel', '');
}

/******************************************************************************************************/
function refreshOpener()
{
	window.opener.location.href = window.opener.location.href;
}

/******************************************************************************************************/
function refresh()
{
	window.location.href = window.location.href;
}

/******************************************************************************************************/
function setValue(control, value)
{
	if (document.getElementById(control) != null)
		document.getElementById(control).value = value;
}

/******************************************************************************************************/
/* atribui valores nas telas que chamam os pop-ups*/
function setOpener(control, value)
{
	if (window.opener.document.getElementById(control) != null)
	{
		if (window.opener.document.getElementById(control).value != "")
		{
			ctr = window.opener.document.getElementById(control).value;
			window.opener.document.getElementById(ctr).value = value;
		}
	}
}

/******************************************************************************************************/
function setOpenerControl(control, value)
{
	if (window.opener.document.getElementById(control) != null)
		window.opener.document.getElementById(control).value = value;
}

/******************************************************************************************************/
function getOpenerControl(control, value)
{
	if (window.opener.document.getElementById(control) != null)
		value = window.opener.document.getElementById(control).value;
}

/******************************************************************************************************/
function getControlValue(control)
{
	return document.getElementById(control).value;
}

/******************************************************************************************************/
/* seleciona os crews e respectivos positions*/
function checkCrew(check, label, code, hidden, counter)
{
	crew = document.getElementById('ddlPosition').value.split('|');
	
	if (document.getElementById(counter) != null)
	    t = parseInt(document.getElementById(counter).innerHTML);
	
	if (document.getElementById('ddlPosition').value == "")
	{
		document.getElementById(code).value = 0;
		document.getElementById(label).innerHTML = "";
		document.getElementById(hidden).value = "";
		
		if (! check.checked)
		    if (document.getElementById(counter) != null)
			    document.getElementById(counter).innerHTML = t - 1;
		
		nome = code.substring(0, 14);
		newCheck = document.getElementById(nome + 'chkAllLegs')
		newCheck.checked = false;
		
		check.checked = false;
	}
	else
	{
		document.getElementById(code).value = crew[0];
		document.getElementById(label).innerHTML = crew[1];
		document.getElementById(hidden).value = crew[1];
		
		if (check.checked)
		    if (document.getElementById(counter) != null)
			    document.getElementById(counter).innerHTML = t + 1;
		
		check.checked = true;
	}
}

/******************************************************************************************************/
/* soma o total de pax selecionados por coluna */
function sumPax(check, label)
{
	t = parseInt(document.getElementById(label).innerHTML);
	
	if (check.checked)
		document.getElementById(label).innerHTML = t + 1;
	else
		document.getElementById(label).innerHTML = t - 1;
}

/******************************************************************************************************/
/* remove a seleção do all legs da linha do pax */
function checkPax(check, index, allCheck)
{
	if (! check.checked)
		document.getElementById(allCheck).checked = false;

	updatePaxColCounters(index);
}

/******************************************************************************************************/
/* seleciona todas as legs do respectivo pax */
function checkRow(check, row)
{
	if (check.checked)
	{
		table = document.getElementById("paxTable").childNodes[0]; // tabela
		rows = table.childNodes[0].childNodes;
		cells = rows[row].childNodes;
		
		// conta só as legs sem o AllLegs
		for (i = 4; i < cells.length - 1; i++)
		{   
			check = cells[i].childNodes[0].childNodes[0];
			if (check.getAttribute("type") == "checkbox")
				check.setAttribute("checked", "true");			
		}
	}

	updatePaxCounters();
}

/******************************************************************************************************/
/* seleciona os crews de uma linha inteira e respectivos positions*/
function checkCrewRow(check, row, label, code, hidden)
{
	crew = document.getElementById('ddlPosition').value.split('|');
	
	if (document.getElementById('ddlPosition').value != "")
	{
	    table = document.getElementById("crewTable").childNodes[0]; // tabela
		rows = table.childNodes[0].childNodes;
		cells = rows[row].childNodes;
		
		for (i = 4; i < cells.length - 1; i++)
		{
		    j = i - 4;   
		
			check = cells[i].childNodes[0].childNodes[0];
			if (check.getAttribute("type") == "checkbox")
				check.setAttribute("checked", "true");
			
			document.getElementById('rptCrew__ctl' + row + '_rptUserLegs__ctl' + j + '_lblPosition').innerText = crew[1];
			document.getElementById('rptCrew__ctl' + row + '_rptUserLegs__ctl' + j + '_hidCode').innerText = crew[0];
			document.getElementById('rptCrew__ctl' + row + '_rptUserLegs__ctl' + j + '_hidPosition').innerText = crew[1];
		}
		
		check.checked = true;
	}
	else
	    check.checked = false;
	
	
	updateCrewCounters();
}

/******************************************************************************************************/
function updatePaxColCounters(index)
{
	table = document.getElementById("paxTable").childNodes[0]; // tabela
	rows = table.childNodes[0].childNodes;

	rows[rows.length - 1].childNodes[index + 3].childNodes[0].innerHTML = "0";

	// para cada row
	for (i = 1; i < rows.length - 1; i++)
	{
		cells = rows[i].childNodes;
		// para cada cell
		check = cells[index + 3].childNodes[0].childNodes[0];
		if (check.getAttribute("type") == "checkbox")
			if (check.getAttribute("checked") == true)
			{
				ct = parseInt(rows[rows.length - 1].childNodes[index + 3].childNodes[0].innerHTML);
				rows[rows.length - 1].childNodes[index + 3].childNodes[0].innerHTML = ct + 1;
			}
	}
	
	updatePaxAllCounter();
}

/******************************************************************************************************/
function updatePaxAllCounter()
{
	table = document.getElementById("paxTable").childNodes[0]; // tabela
	rows = table.childNodes[0].childNodes;

	rows[rows.length - 1].childNodes[3].childNodes[0].innerHTML = "0";

	// para cada row
	for (i = 1; i < rows.length - 1; i++)
	{
		cells = rows[i].childNodes;
		// para cada cell
		check = cells[3].childNodes[0].childNodes[0];
		if (check.getAttribute("type") == "checkbox")
			if (check.getAttribute("checked") == true)
			{
				ct = parseInt(rows[rows.length - 1].childNodes[3].childNodes[0].innerHTML);
				rows[rows.length - 1].childNodes[3].childNodes[0].innerHTML = ct + 1;
			}
	}
}

/******************************************************************************************************/
/* atualiza todos os totais de todos os pax por legs */
function updatePaxCounters()
{
	table = document.getElementById("paxTable").childNodes[0]; // tabela
	rows = table.childNodes[0].childNodes;

	if (rows[rows.length - 1] != null)
	{
		cells = rows[rows.length - 1].childNodes;
	
		for (j = 3; j < cells.length - 1; j++)
			rows[rows.length - 1].childNodes[j].childNodes[0].innerHTML = "0";

		// para cada row
		for (i = 1; i < rows.length - 1; i++)
		{
			cells = rows[i].childNodes;
			
			// para cada cell
			for (j = 3; j < cells.length - 1; j++)
			{
				check = cells[j].childNodes[0].childNodes[0];
				if (check.getAttribute("type") == "checkbox")
					if (check.getAttribute("checked") == true)
					{
						ct = parseInt(rows[rows.length - 1].childNodes[j].childNodes[0].innerHTML);
						rows[rows.length - 1].childNodes[j].childNodes[0].innerHTML = ct + 1;
					}
			}
		}
	}
}

/******************************************************************************************************/
/* atualiza todos os totais de todos os crews por legs */
function updateCrewCounters()
{
	table = document.getElementById("crewTable").childNodes[0]; // tabela
	rows = table.childNodes[0].childNodes;
	
	for (i = 1; i < rows.length - 1; i++)
	{
		cells = rows[i].childNodes;
		
		for (j = 4; j < cells.length - 1; j++)
		{
			rows[rows.length - 1].childNodes[j].childNodes[0].innerHTML = "0";
		}
	}
	
	// para cada row
	for (i = 1; i < rows.length - 1; i++)
	{
		cells = rows[i].childNodes;
		// para cada cell
		for (j = 4; j < cells.length - 1; j++)
		{
			check = cells[j].childNodes[0].childNodes[0];
			if (check.getAttribute("type") == "checkbox")
				if (check.getAttribute("checked") == true)
				{
					ct = parseInt(rows[rows.length - 1].childNodes[j].childNodes[0].innerHTML);
					rows[rows.length - 1].childNodes[j].childNodes[0].innerHTML = ct + 1;
				}
		}
	}
}

/******************************************************************************************************/
/* salva os checkboxes selecionados dureante a paginação */
function saveSelected(check, control, code)
{
	sel = document.getElementById(control).value;
	arr = sel.split(',');
	insert = true;
	value = "";

	if (check.checked)
	{
		for (i = 0; i < arr.length; i++)
		{
			if (arr[i] != "" && arr[i] == code)
			{
				insert = false;
				break;
			}
		}
		
		if (insert)
		{
			if (sel == "")
				document.getElementById(control).value += code;
			else
				document.getElementById(control).value += "," + code;
		}
	}
	else
	{
		for (i = 0; i < arr.length; i++)
		{
			if (arr[i] != "" && arr[i] != code && arr[i] != "0")
			{
				if (insert)
				{
					value += arr[i];
					insert = false;
				}
				else
					value += "," + arr[i];
			}
		}
		
		//value += "0";
		document.getElementById(control).value = value;
	}
}

/******************************************************************************************************/
/* limpa um campo texto de acordo com o valor do checkbox */
function clearField(check, field)
{
	if (! check.checked)
		document.getElementById(field).value = "";
}

/******************************************************************************************************/
// seleciona um checkbox
function checkBox(check)
{
	document.getElementById(check).checked = true;
}

/******************************************************************************************************/
function noEnter() 
{
	return !(window.event && window.event.keyCode == 13);
}


/******************************************************************************************************/
function clearTimeZone()
{
	var list = document.getElementsByTagName("input");
	
	for (var i = 0; i < list.length; i++)
	{
		var e = list[i];
		
		// limpa os texts com as horas
		if ((e != null) && (e.type == "text")
			&& ( (e.id.indexOf("txtOpeningInput") > 0) || (e.id.indexOf("txtClosingInput") > 0) ))
		{
			e.value = '';
		}
		
		// "uncheck" todos os timezones (UTC/LCL)
		if ((e != null) && (e.type == "radio") && (e.id.indexOf("radioTimezone") > 0) )
		{
			e.checked = false;
		}
	}
}

function clearOfficeTimeZone()
{
	var list = document.getElementsByTagName("input");
	
	for (var i = 0; i < list.length; i++)
	{
		var e = list[i];
		
		// limpa os texts com as horas
		if ((e != null) && (e.type == "text")
			&& ( (e.id.indexOf("txtOfficeOpeningInput") > 0) || (e.id.indexOf("txtOfficeClosingInput") > 0) ))
		{
			e.value = '';
		}
		
		// "uncheck" todos os timezones (UTC/LCL)
		if ((e != null) && (e.type == "radio") && (e.id.indexOf("radioOfficeTimezone") > 0) )
		{
			e.checked = false;
		}
	}
}

/******************************************************************************************************/
function setCustom()
{
	//document.getElementById('rdoCustom').checked = true;
}

/******************************************************************************************************/
function copyRadioTimezone(obj)
{
	var timezone, nome;
	
	if (obj != null)
	{
		timezone = obj.value;
		
		var list = document.getElementsByTagName("input");
		
		for (var i = 0; i < list.length; i++)
		{
			var e = list[i];
			if ((e != null) && (e.type == "radio") && (e.id.indexOf(timezone) > 0) && (e.id.indexOf("radioTimezone") > 0))
			{
				e.checked = true;
			}
		}
		
		document.getElementById('radioSunriseSunset').checked = false;
		document.getElementById('radio24Hours').checked = false;
	}	
}

function copyOfficeRadioTimezone(obj)
{
	var timezone, nome;
	
	if (obj != null)
	{
		timezone = obj.value;
		
		var list = document.getElementsByTagName("input");
		
		for (var i = 0; i < list.length; i++)
		{
			var e = list[i];
			if ((e != null) && (e.type == "radio") && (e.id.indexOf(timezone) > 0) && (e.id.indexOf("radioOfficeTimezone") > 0))
			{
				e.checked = true;
			}
		}
		
		document.getElementById('radioOfficeSunriseSunset').checked = false;
		document.getElementById('radioOffice24Hours').checked = false;
	}	
}

/******************************************************************************************************/
function copyAllOperatingHours()
{
	var openingElem, closingElem;
	var openingHour, closingHour;
	
	openingElem = document.getElementById('rptOperatingHours__ctl0_txtOpeningInput');
	closingElem = document.getElementById('rptOperatingHours__ctl0_txtClosingInput');
	
	if ((openingElem != null) && (closingElem != null))
	{
		openingHour = openingElem.value;
		closingHour = closingElem.value;
		
		var list = document.getElementsByTagName("input");
		
		for (var i = 0; i < list.length; i++)
		{
			var e = list[i];
			
			// copia os texts com as horas
			if ((e != null) && (e.type == "text") && (e.id.indexOf("txtOpeningInput") > 0) )
			{
				e.value = openingHour;
			}
			
			if ((e != null) && (e.type == "text") && (e.id.indexOf("txtClosingInput") > 0) )
			{
				e.value = closingHour;
			}
			
		}
	}
}

function copyAllOfficeOperatingHours()
{
	var openingElem, closingElem;
	var openingHour, closingHour;
	
	openingElem = document.getElementById('rptOfficeOperatingHours__ctl0_txtOfficeOpeningInput');
	closingElem = document.getElementById('rptOfficeOperatingHours__ctl0_txtOfficeClosingInput');
	
	if ((openingElem != null) && (closingElem != null))
	{
		openingHour = openingElem.value;
		closingHour = closingElem.value;
		
		var list = document.getElementsByTagName("input");
		
		for (var i = 0; i < list.length; i++)
		{
			var e = list[i];
			
			// copia os texts com as horas
			if ((e != null) && (e.type == "text") && (e.id.indexOf("txtOfficeOpeningInput") > 0) )
			{
				e.value = openingHour;
			}
			
			if ((e != null) && (e.type == "text") && (e.id.indexOf("txtOfficeClosingInput") > 0) )
			{
				e.value = closingHour;
			}
			
		}
	}
}



/******************************************************************************************************/
// TRIM functions for javascript (see http://www.jibbering.com/faq/#FAQ4_16)

// LEFT trim
function LTrim(str)
{
	for (var k=0; k<str.length && str.charAt(k)<=" " ; k++);
	return str.substring(k,str.length);
}

// RIGHT trim
function RTrim(str)
{
	for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--);
	return str.substring(0,j+1);
}

function Trim(str)
{
	return LTrim(RTrim(str));
}

/******************************************************************************************************/
// unchecks radio buttons
// to use this on a radio button, add "check(this)" to the OnClick event
			
var radioStatus = new Object();

function check(radioButton)
{
	if (radioStatus[radioButton.name] == null)
	{
		radioStatus[radioButton.name] = radioButton.value;
	}
	else
	{
		if (radioStatus[radioButton.name] == radioButton.value)
		{
			radioButton.checked = false;
			radioStatus[radioButton.name] = null;
		}
		else
		{
			radioStatus[radioButton.name] = radioButton.value;
		}
	}
}

/******************************************************************************************************/
/*	Conversion from pounds (lb) to metric tons, with two decimal places.
	1 metric ton = 2204 pounds
	1 pound = 0.0004536 metric tons
 */
 function ConvertPoundsToMetricTons(weightInPounds)
 {
	if ( (weightInPounds != null) && (!isNaN(weightInPounds)) )
	{
		var weightInTons = weightInPounds * 0.0004536;
		return Math.round(weightInTons * 100)/100;
	}
 }
 
 /******************************************************************************************************/

 function setLineOnMouseOut(trThis, trToBeHidden)
{
	if(document.getElementById(trToBeHidden).style.display == 'none')
	{
		document.getElementById(trThis).style.backgroundColor = ''
	}
}

function setLineOnMouseOver(trThis, trToBeHidden, color)
{
	if(document.getElementById(trToBeHidden).style.display == 'none')
		{
			document.getElementById(trThis).style.backgroundColor = ''
			document.getElementById(trThis).style.backgroundColor=color;
			document.getElementById(trThis).style.cursor='';
		}
}
/******************************************************************************************************/
function showDropDownContentForDataGrid(obj,content)
{
	if (document.getElementById(content).style.display == 'none')
	{
		document.getElementById(content).style.display = 'block';
		obj.src = '../images/minus_black.gif';
	}
	else
	{
		document.getElementById(content).style.display = 'none';
		obj.src = '../images/plus_black.gif';
	}
}