var BrowserType;
document.all ? BrowserType="IE" : BrowserType="NS";
var isopen = "no";

function handleResize() {
	location.reload();
	return false;
}

if (BrowserType=="NS") {
	window.captureEvents(Event.RESIZE);
	window.onresize=handleResize;
}

function closePopup() {
	if (typeof(popup) != "undefined") {popup.close();}
}
	
function errorMessage(asMsgText, asMsgField) {
	gsErrMsg += asMsgText;
	if (gsErrField == '') {gsErrField = asMsgField;}
}

function displayError() {
	document.getElementById('errMsgArea').style.display = 'inline';
	document.getElementById('errMsgText').innerHTML = gsErrMsg;
	correctFooter();
	window.scrollTo(document.getElementById('errorTextMarker').offsetX,document.getElementById('errorTextMarker').offsetY)
}

function fareRulesPopup(asFareIndex, asFareType) {
	xmlStr = '<CRSREQUEST>' + xmlcontroldets() + '<APPLICDETS><FUNCTION>FR</FUNCTION><REQKEY><SCHTP>TRA</SCHTP><SCHKEY>0</SCHKEY><SCHSCR>HS1</SCHSCR><CART>Y</CART><MKSL_CD>FCRS</MKSL_CD>';
	xmlStr += '<WSFSSEQ>' + gsWsfsSeq + '</WSFSSEQ></REQKEY></APPLICDETS>';
	xmlStr += '<SCREEN><CRS><Criteria><Method>ShowFareRules</Method><CacheType>Sell</CacheType><TemplateType>Popup</TemplateType><Fare><FareIndex>' + asFareIndex + '</FareIndex><FareType>' + asFareType + '</FareType></Fare>';
	if (document.getElementById('TravellerDetails')) xmlStr += buildTravellers();
	xmlStr += '<ReturnFromCache>N</ReturnFromCache></Criteria></CRS></SCREEN></CRSREQUEST>';
	var options = "toolbar=no,location=no,directories=no,scrollbars=yes,resizable=yes,copyhistory=no,width=850px,height=380px";
	var posY = (screen.height - 500) / 2;
	var posX = (screen.width - 850) / 2;
	options += ", top=" + posY + ",left=" + posX;
	popup = open('', 'POP' + agDbSes, options);
	if (navigator.appName == "Netscape") {popup.focus();}
	if (popup.opener == null) {popup.opener = self;}
	document.frmXmlPopup.XML.value = xmlStr;
	document.frmXmlPopup.target = 'POP' + agDbSes;
	document.frmXmlPopup.submit();
}

function hotelProntoCanxCond() {
	xmlStr = "<RESCANXTERMSREQUEST>";	
	xmlStr += xmlcontroldets();
	xmlStr += "<APPLICDETS>";
	xmlStr += "<REQKEY>";
	xmlStr += "<PREBKG>Y</PREBKG>";
	xmlStr += "</REQKEY>";
	xmlStr += "</APPLICDETS>";
	xmlStr += "</RESCANXTERMSREQUEST>";
	
	var options = "toolbar=no,location=no,directories=no,scrollbars=yes,resizable=yes,copyhistory=no,width=850px,height=380px";
	var posY = (screen.height - 500) / 2;
	var posX = (screen.width - 850) / 2;
	options += ", top=" + posY + ",left=" + posX;
	popup = open('', 'POP' + agDbSes, options);
	if (navigator.appName == "Netscape") {popup.focus();}
	if (popup.opener == null) {popup.opener = self;}
	
	document.frmXmlPopup.XML.value = xmlStr;
	document.frmXmlPopup.target = 'POP' + agDbSes;
	document.frmXmlPopup.submit();
}

function submitRequest(abDisplayHolding, asHoldingPage) {
	if (typeof(abDisplayHolding) == "undefined") {abDisplayHolding = false;}
	if (document.getElementById('savePageDetails')) {saveFormDetails();}
	document.frmXmlSubmit.XML.value = xmlStr;
	if (abDisplayHolding) {holdingEnable(asHoldingPage, gaHoldingStr);}
	document.frmXmlSubmit.submit();
}

function submitSecureRequest(abDisplayHolding, asHoldingPage) {
	if (typeof(abDisplayHolding) == "undefined") {abDisplayHolding = false;}
	if (document.getElementById('savePageDetails')) {saveFormDetails();}
	document.frmXmlSecureSubmit.XML.value = xmlStr;
	if (abDisplayHolding) {holdingEnable(asHoldingPage, gaHoldingStr);}
	document.frmXmlSecureSubmit.submit();
}

function pad(astr, desiredStrLen, padChar, padOnRight) {
	var args = pad.arguments;
	if (args[3] == null) padOnRight = false;
	var lstr = astr.toString();
 	var strLen = lstr.length;
 	while(strLen < desiredStrLen) {
		if (padOnRight) {lstr = lstr + padChar;} else {lstr = padChar + lstr;}
		strLen = lstr.length;
	}
	return lstr;
}

function strltrim() {return this.replace(/^\s+/,'');}
function strrtrim() {return this.replace(/\s+$/,'');}
function strtrim() {return this.replace(/^\s+/,'').replace(/\s+$/,'');}
String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;

// ------------------------------------------------------------------
// Get element function
// ------------------------------------------------------------------
function $(element) {
	if (arguments.length > 1) {
		for (var i = 0, elements = [], length = arguments.length; i < length; i++)
			elements.push($(arguments[i]));
		return elements;
	}
	if (typeof element == 'string' || typeof element == 'number')
		element = document.getElementById(element);
	return element;
}

// ------------------------------------------------------------------
// Get first DOM child (Firefox fix)
// ------------------------------------------------------------------
function getFirstChild(theParent) {
	var theChild = theParent.firstChild;
	while (theChild.nodeType != 1) {
		theChild = theChild.nextSibling;
	}

	return theChild;
}

function getSelectedRadio(buttonGroup) {
	if (buttonGroup[0]) {
		for (var i=0; i<buttonGroup.length; i++) {if (buttonGroup[i].checked) {return i}}
	} else {
		if (buttonGroup.checked) {return 0;}
	}
	return -1;
}

function getSelectedRadioValue(buttonGroup) {
	var i = getSelectedRadio(buttonGroup);
	if (i == -1) {
		return "";
	} else {
		if (buttonGroup[i]) {return buttonGroup[i].value;} else {return buttonGroup.value;}
	}
}

function getRadioValue(asName) {
	var value = null;
	var radioButtonGroup = eval("document.frmSubmit." + asName);
				
	if (radioButtonGroup.length) {
		for (var idx = 0; idx < radioButtonGroup.length; idx++)
			if (radioButtonGroup[idx].checked) value = radioButtonGroup[idx].value;
	}
	else if (radioButtonGroup.checked)
		value = radioButtonGroup.value;
	return value;
}

function getDescription(asCode, asArray) {
	for (var i=0; i < asArray.length; i++) {
		if (asArray[i][0] == asCode) {
			asCode = asArray[i][1];
			break;
		}
	}
	return asCode;
}

function parseTag(asText, abDisplay) {
	if (abDisplay) {
		while (asText.indexOf("'") != -1) {asText = asText.replace("'", "&apos;");}
	} else {
		while (asText.indexOf("&apos;") != -1) {asText = asText.replace("&apos;", "'");}
	}
	return asText;
}

function gaSplitDate(asDate) {
	var splitMonth = 0;
	var sSplitDate = asDate.split("-");
	for (iSplit=0; iSplit<=11; iSplit++) {
		if (MONTH_NAMES_SHORT[iSplit].toUpperCase() == sSplitDate[1].toUpperCase()) {
			splitMonth=iSplit;
			break;
		}
	}
	return new Date(sSplitDate[2], pad(splitMonth, 2, '0', false), sSplitDate[0]);
}

function gaSplitDateStr(asDate, aiOffset) {
	if (asDate != '') {
		var dSplitDate = gaSplitDate(asDate);
	} else {
		var dSplitDate = new Date();
		var alDays = dSplitDate.getDate();
		alDays += aiOffset;
		dSplitDate.setDate(alDays);
	}
	return new Array(formatDate(dSplitDate,"dd"), formatDate(dSplitDate,"NNN"), formatDate(dSplitDate,"yyyy"));
}

function hideOverrideFields(asGroups, bInclude) {
	if (typeof(asGroups) == "undefined") asGroups = "";
	if (typeof(bInclude) == "undefined") bInclude = true;
	if (asGroups != '') {
		var tempArr = asGroups.split(",");
		if (bInclude) {
			for (var gIdx=0; gIdx < tempArr.length; gIdx++) {document.getElementById('overrideField' + tempArr[gIdx]).style.display = 'none';}
		} else {
			var laExclude = new Array(100);
			for (var xIdx=0; xIdx<100; xIdx++) laExclude[xIdx] = false;
			for (gIdx=0; gIdx < tempArr.length; gIdx++) laExclude[Number(tempArr[gIdx])] = true;
			for (var fIdx=1; fIdx<99; fIdx++) {
				if (document.getElementById('overrideField' + fIdx)) {
					if (laExclude[fIdx] == false) document.getElementById('overrideField' + fIdx).style.display = 'none';
				} else {
					break;
				}
			}
		}
	} else {
		for (var fIdx=1; fIdx<99; fIdx++) {
			if (document.getElementById('overrideField' + fIdx)) {
				document.getElementById('overrideField' + fIdx).style.display = 'none';
			} else {
				break;
			}
		}
	}
}

function showOverrideFields() {
	for (var fIdx=1; fIdx<99; fIdx++) {
		if (document.getElementById('overrideField' + fIdx)) {
			document.getElementById('overrideField' + fIdx).style.display = 'inline';
		} else {
			break;
		}
	}
}

function resetAirport(sField) {
	document.getElementById(sField).value = "";
}

function alterReturns(which) {
	if (which == 'out_dep_airport') {
		document.getElementById('ret_arr_airport').value = document.getElementById('out_dep_airport').value;
		document.getElementById('ret_arr_airport_desc').value = document.getElementById('out_dep_airport_desc').value;
	}
	if (which == 'out_arr_airport') {
		document.getElementById('ret_dep_airport').value = document.getElementById('out_arr_airport').value;
		document.getElementById('ret_dep_airport_desc').value = document.getElementById('out_arr_airport_desc').value;
	}
}

function getStyle(oElm, asStyle) {
	var strValue = "";
	if (document.defaultView && document.defaultView.getComputedStyle) {
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(asStyle);
	} else if (oElm.currentStyle) {
		strValue = oElm.currentStyle[asStyle];
	}
	return strValue;
}

function correctFooter() {
	if (document.getElementById('footer')) {
	    document.getElementById('footer').style.bottom = "1px";
        setTimeout("document.getElementById('footer').style.bottom = '0px'", 300);
    }
}

function removeClassName (elem, className) {
    elem.className = elem.className.replace(className, "").trim(); 
}

function addClassName (elem, className) {
	removeClassName (elem, className);
	elem.className = (elem.className + " " + className).trim();
}

function setCursorPoint(aField, sStatus) {
	if (sStatus == 'Wait') {
		aField.style.cursor = 'Wait';
	} else if (sStatus == 'On') {
		if (window.showModalDialog) {
			aField.style.cursor = 'Hand';
		} else {
			aField.style.cursor = 'Pointer';
		}
	} else {
		aField.style.cursor = 'default';
	}
}

function returnDateFromString(asDateField) {
	var lsDateStr = document.getElementById(asDateField + '_DAY').value + "-" + document.getElementById(asDateField + '_MONTH').value + "-" + document.getElementById(asDateField + '_YEAR').value;
	return getDateFromFormat(lsDateStr,"dd-NNN-yyyy",true);
}
