if (typeof logError == "undefined") {
  logError = function(){};
}
if (typeof logLoaded == "undefined") {
	logLoaded = function(){};
}
if (typeof logDebug == "undefined") {
	logDebug = function(){};
}
var jsName = "acoNavButtons_new.js";
logDebug(jsName + "@start");

// Validation routines for Non Tariff Products on flight search pages.

function validatePromotionCode() {
	// Seven first chars must not (^) be any of "0Oo1Ii5sS" and last must be one of "1234".
	var validation = "[^0Oo1Ii5sS]{7}[1234]";
	var f = document.flights;
	return isHidden('Promotion Code') ||
			checkEmptyField(f.promotionCode) ||
			f.promotionCode.value.match(validation) != null;
}

function checkEmptyField(field){
	return field.value == null || field.value == "";
}

// pop up window to get agent details.
function checkAgentDetails() {
	var ff = document.flights;
	var fp = document.flightsPlanitgo;

	var ch = "";
	var objErr;
	var agent_details_url = "";
	var windowName = "_name_pop_agent_details";
   var windowFeatures2 = "width=550,height=430,top=50,left=50,scrollbars=no";
   var windowFeatures3 = "width=550,height=240,top=50,left=50,scrollbars=no";
   var windowFeatures4 = "width=550,height=500,top=50,left=50,scrollbars=no";
	try {

		if (fp.NTP_AUTHORIZATION.value=="true")
			return false;
		if (getCookie("new_lang_pref") == "french")
			agent_details_url = "/shared/fr/aco/flights/pop_agent_signin.html";   
		else if (getCookie("new_lang_pref") == "de")
			agent_details_url = "/de/shared/de/aco/flights/pop_agent_signin.html";
		else if (getCookie("new_lang_pref") == "it")
			agent_details_url = "/it/shared/it/aco/flights/pop_agent_signin.html";
		else
			agent_details_url = "/shared/en/aco/flights/pop_agent_signin.html";
		if (ff.promotionCode.value != "" && ff.promotionCode.value != null) {

			if (ff.validToSubmitPromotion.value=="YES")
				return true;
			var ch = ff.promotionCode.value.charAt(7);
	    if (ch == '1') {
				document.FlightSearchNTPRequestForm.agentId.value="";
				ff.validToSubmitPromotion.value = "YES";
				return true;
			}
			if (ff.IATA_AGENCY_PNR.value != "")
				return true;
			if (ch == '3') {
				window.open(agent_details_url,ch + windowName,windowFeatures3);
				return ff.validToSubmitPromotion.value == "YES";
			}
			if (ff.AGENCY_ID.value != "")
   			return true;
			if (ch == '2') {
				window.open(agent_details_url,ch + windowName,windowFeatures2);
				return ff.validToSubmitPromotion.value == "YES";
			}

			if (ff.IATA_AGENCY_PNR.value == "" || ff.AGENCY_ID.value == "") {
				if (ch == '4') {
					window.open(agent_details_url,ch + windowName,windowFeatures4);
					return ff.validToSubmitPromotion.value == "YES";
				}
   		}

		} else {
			fp.NTP_AUTHORIZATION.value="true";
			return false;
		}
    } catch(objErr) {
		alert(objErr.message);
	}
}

// set all the required fields by NTP service to validate the authorization ID(Promotion code).
function setNTPFormFields(sitePage) {
	var fs = document.FlightSearchNTPRequestForm;
	var ff = document.flights;
	var objErr;

	try {

		show("bodyPage");
		WDSWaitingImage.pleaseWait("anim_waiting-bar");
		// default Request type is Booking from flight search and magnets.
		fs.requestType.value = "B";

		if (document.flightsPlanitgo.PRIVATE_LABEL.value == "AGENT") {
			fs.callerApp.value = "ADO";
			fs.action = "/secure/ado/flightSearch.do"
		} else {
			fs.callerApp.value = "ACO";
			fs.action = "/aco/flightSearch.do"
		}

		if (getCookie("new_lang_pref") == "english")
			fs.language.value = "EN";

		if (getCookie("new_lang_pref") == "french")
			fs.language.value = "FR";

		fs.authorizationId.value = ff.promotionCode.value;
		fs.certificateField.value = ff.certificateField.value;

		fs.agentId.value = ff.AGENCY_ID.value;
		//setCookie("IATA_AGENCY_PNR",ff.IATA_AGENCY_PNR.value,"1","/");

		// Number of passengers
		var numOfPax = 0;
		numOfPax = Math.round(ff.numberOfAdults.value) +Math.round(ff.numberOfYouth.value)+ Math.round(ff.numberOfChildren.value);
		fs.numOfPax.value = numOfPax;

		fs.travelStartDate.value = ff.departure1.value;

		if(typeof(ff.tripType.value)!="string"){
			fs.travelEndDate.value = isChecked(ff.tripType[0]) ? ff.departure2.value : "";
		}else{
			fs.travelEndDate.value = fl.tripType.value=="R" ? ff.departure2.value : "";
		}

		fs.origin.value = getAirportCode(ff.org1.value);
		fs.destination.value = getAirportCode(ff.dest1.value);
		fs.site.value = sitePage;

		if(typeof(ff.tripType.value)!="string"){
			fs.tripTypeNTP.value = isChecked(ff.tripType[0]) ? "R" : "O";
		}else{
			fs.tripTypeNTP.value = fl.tripType.value=="R" ?  "R" : "O";
		}

		fs.departTime1NTP.value = ff.departTime1.value;
		fs.departTime2NTP.value = ff.departTime2.value;
		fs.numberOfChildrenNTP.value = ff.numberOfChildren.value;
		fs.numberOfAdultsNTP.value = ff.numberOfAdults.value;
		fs.numberOfYouthNTP.value = ff.numberOfYouth.value;
		fs.numberOfInfantsNTP.value = ff.numberOfInfants.value;
		fs.countryOfResidenceNTP.value = ff.countryOfResidence.value;

		fs.flexibleNTP.value = isChecked(ff.Flexible) ? "Flexible" : "";
		fs.classTypeNTP.value = ff.classType.value;
		fs.searchTypeNTP.value = isChecked(ff.searchType[0]) ? "F" : "S";

		fs.agentFirstName.value = ff.AGENT_FIRSTNAME.value;
		fs.agentLastName.value = ff.AGENT_LASTNAME.value;
		fs.agencyIATAPNR.value = ff.IATA_AGENCY_PNR.value;

	} catch(objErr) { alert(objErr.message); }
}

function getComboboxHTML(values, selValue) {
    var optValues = values.split(",");
    var retValue = "";
    var selValueinList = false;

    for (var i=0;i<optValues.length;i++) {
        var optValue = getCityName(optValues[i]);
        if (optValue == selValue) {
            selValueinList = true;
        }
    }

    for (var i=0;i<optValues.length;i++) {
        var optValue = getCityName(optValues[i]);
        if ((i==0) && (selValueinList == false)) {
            retValue = retValue + "<option value='" + optValue + "' selected>" + optValue + "</option>";
        } else {
           if (optValue == selValue) {
             retValue = retValue + "<option value='" + optValue + "' selected>" + optValue + "</option>";
           } else {
             retValue = retValue + "<option value='" + optValue + "'>" + optValue + "</option>";
           }
        }
    }

    return retValue;
}

function getComboboxFilled(select, values, selValue, fieldtoSet) {

	/* Added for the SR2381 for adding cities in the list
	 * if the city selected by user is not in the list of cities
	 * coming as response from NTP
	 */
	if (values.search(selValue) < 0)
		values = selValue + "," + values;
	// End of Change

    var optValues = values.split(",");
    var objErr;
    try {
     if (select.options.length>0){
		for(var i=select.options.length-1;i>=0;i--)
			select.remove(i);
     }

     for (var i=0;i<optValues.length;i++) {
        var optValue = getCityName(optValues[i]);
            if(optValue != ""){
				var opt = new Option(optValue, optValue);
				select.options[select.options.length] = opt;

				if ((optValues[i] == selValue) ) {
					opt.selected = true;
					fieldtoSet.value = optValue;
				}
            }
    	}
    } catch (objErr) { alert(objErr.message); }

}


function setOrigin() {
    var f = document.flights;
    f.org1.value = getSelected(f.ntpOrigins);
	if (f.dest1.value == "")
		f.dest1.value = document.FlightSearchNTPRequestForm.destination.value;
}

function setDestination() {
    var f = document.flights;
	f.dest1.value = getSelected(f.ntpDestinations);
	if (f.org1.value == "")
		f.org1.value = document.FlightSearchNTPRequestForm.origin.value;
}

function checkPromotionCode(){
	var f = document.flightsPlanitgo;
	f.NTP_AUTHORIZATION.value="false";
	f.TRAVEL_AGENT_PNR.value ="";
	f.IATA_AGENT_ID_NUMBER.value ="";
	f.IATA_AGENT_LAST_NAME.value ="";
	f.IATA_AGENT_FIRST_NAME.value="";

	f = document.flights;
	f.AGENCY_ID.value = "";
	f.IATA_AGENCY_PNR.value = "";
	f.validToSubmitPromotion.value = "NO";
	f.AGENT_LASTNAME.value="";
	f.AGENT_FIRSTNAME.value ="";
	//expiryOldCookie("IATA_AGENCY_PNR","");
}

function validateInfantPromotion() {
	return isHidden('Promotion Code') ||
			checkEmptyField(document.flights.promotionCode) ||
			Math.round(document.flights.numberOfInfants.value) == 0;
}

function validateInfantHomePromotion() {
	return isHidden('Promotion Code') ||
			checkEmptyField(document.flights.promotionCode) ||
			(isHidden("Infant") || Number(getSelected(document.flights.numberOfInfants)) == 0);
}

/*
The showHidePromotionCode function removed for SR2799
*/

// Script loader indicator.
logLoaded(jsName);
