
/*******************************************************************************
*	Summary of step1.js - Relocated code from XSL templates
* -----------------------------------------------------------------------------
* XSL File						Template
* -----------------------------------------------------------------------------
* common							languages
*	step1scripts				login
*											step1
*											Date
*											route
*                     stations
*                     step1Intranet
*											setOptions
*											popup
*******************************************************************************/

/*******************************************************************************
*	From step1 new switchCheckInManageBookings function released added to v22
*******************************************************************************/

function switchCheckInManageBookings(objShow, objHide)
{
	var objShowElmnt = document.getElementById(objShow);
	var objHideElmnt = document.getElementById(objHide);
	
	var objLeftTab = document.getElementById('loginTabLeft');
	var objRightTab = document.getElementById('loginTabRight');
	
	var objCheckInTop = document.getElementById('checkin_tab_top');
	var objCheckInMiddle = document.getElementById('checkin_tab_middle');
	var objCheckInBottom = document.getElementById('checkin_tab_bottom');

	objShowElmnt.style.display = '';
	objHideElmnt.style.display = 'none';

	if (objShowElmnt.getAttribute('id')=='onlinecheckin')
	{
		objLeftTab.className = 'tabon';
		objRightTab.className = 'taboff';
		objCheckInTop.className = 'loginTabBackground_select_left';
		objCheckInMiddle.className = 'loginTabHead_select_left';
		objCheckInBottom.className = 'loginTabBottom_select_left';
	}
	else if (objShowElmnt.getAttribute('id')=='managebookings')
	{
		objLeftTab.className = 'taboff';
		objRightTab.className = 'tabon';
		objCheckInTop.className = 'loginTabBackground_select_right';
		objCheckInMiddle.className = 'loginTabHead_select_right';
		objCheckInBottom.className = 'loginTabBottom_select_right';
	}
}

/*******************************************************************************
*	From common.xsl template: languages
*******************************************************************************/

function languageChange(box)
{
	destination = box.options[box.selectedIndex].value;
	if (destination) location.href = destination;
}

/*******************************************************************************
*	From step1scripts.xsl template: login
*******************************************************************************/

function signIn(){
	var intErrs = 0;
	var strErrMSG = errorMSG['EnterAValid'];
	var emailVal = document.forms["signin"].strEmail.value;
	if ((emailVal.length < 5)||(emailVal.indexOf('@') < 0)){
		intErrs++;
		strErrMSG += '\n- ' + errorMSG['Email'];
	}
	
	var strPWord = document.forms["signin"].strPassword.value;
	while (strPWord.indexOf(' ') != -1){
		strPWord = strPWord.replace(' ','');
	}
	
	if(strPWord == ''){
		intErrs++;
		strErrMSG += '\n- ' + errorMSG['NoPassword'];
	}
	
	if (intErrs){
		alert(strErrMSG);
		return false;
	} else {
		return true;
	}
}

function clearField(oField){
	oField.value="";
	var strClass = oField.className;
	var iPos = strClass.indexOf('field_init');
	if(iPos>0){
		oField.className = strClass.substr(0,iPos-1);
	}
}

function swapPasswordField()
{
	if(document.getElementById("txtPassword")!=null){
		if(document.getElementById("txtPassword").style.display=="none"){
			document.getElementById("txtPassword").style.display="";
			document.getElementById("pwdPassword").style.display="none";
		} else {
		document.getElementById("txtPassword").style.display="none";
		document.getElementById("pwdPassword").style.display="";
		document.getElementById("pwdPassword").focus();
		}
	}
}


/*******************************************************************************
*	From step1scripts.xsl template: step1
*******************************************************************************/

function submitFAQ(key)
{
	if(key==13)
	{
		document.forms[0].hid_FAQPressed.value = "true";
	}
}


function validateDates(oWith, niMode, bMode) {
/*
	bMode
		1= all
		2= routes and orig date
		3= routes and no dates
 niMode = 1 - Validation
 niMode = 2 - Checks
*/
var bReturn;
bReturn=true;
//document.forms[0]
	with (oWith) {

		// departing date...
		var objMonth = gED.getMonth(oMonYear);
		var objDate = gED.constructDate(oMonYear, oDay, true);
		var dcurrentDate = new Date(serveryear,servermonth-1,serverday);

		if (niMode == 1) { // validation
			if (objDate < dcurrentDate) {
				alert(errorMSG['DatePassed']);
				oDay.selectedIndex = dcurrentDate.getDate()-1;				
				bReturn=false;
				return bReturn;
			}

		
			if (bMode == 1){
				if ( (rDay.selectedIndex != 0) && (rMonYear.selectedIndex != 0) ) {
					var rDate = gED.constructDate(rMonYear, rDay, true);
					with (gED) {
						bReturn = isReturnFirst(getDay(oDay),getMonth(oMonYear),getYear(oMonYear),objDate,getDay(rDay),getMonth(rMonYear),getYear(rMonYear),rDate,dcurrentDate);
					}
				}
			}
		} else { // checks...
			if (objDate - dcurrentDate == 0) {
				if (!confirm(errorMSG['DepartToday'])) {
					
					bReturn=false;
				}
			}
		}		
	}
	return bReturn;

}


function validateBRs(bMode) 
{
	/*
	bMode
		1= all
		2= routes and orig date
		3= routes and no dates
	*/
	var bReturn;
	bReturn=true;
	with (document.forms[0]) 
	{
		if (bMode == 1||bMode == 2)
		{
			var objDD = gED.constructDate(oMonYear, oDay, true);
			if (bMode==1)
			{
				var objRD = gED.constructDate(rMonYear, rDay, true);
			}	
			else 
			{
				var objRD = objDD;
			}
			if (orig.selectedIndex >= 0 && dest.selectedIndex >= 0)
			{
				if (!gERS.verifyRoute(objDD, objRD, orig[orig.selectedIndex].value, dest[dest.selectedIndex].value)) 
				{
					bReturn= false;
				}
			}		
			else
			{
				bReturn = false;
			}
		}	
		if (bReturn)
		{
			// check for flying today...
			if (!validateDates(document.forms[0], 2, bMode)) 
			{
				bReturn= false;
			}
		}
	}
	return bReturn;
}




function isDateValid(oDay, oMonYear, nlcheckID) {
	var bReturn;
	bReturn=true;

	with (gED) {
		if ((getDay(oDay) == 0) && (getMonth(oMonYear) == 0)) {
			// date not selected but left untouched... so exit with a smile...
			bReturn= true;
		} else if ((getDay(oDay) == 0) || (getMonth(oMonYear) == 0)) {
			// the date must be complete, only will be done for returning...
			alert(errorMSG['IncompleteDate']);
			bReturn= false;
		}else{
			
			// js will make 31st Feb 2004 the 2nd March 2004...
			// so check that the date we have, has the same month as the date passed!...
			var objDate = constructDate(oMonYear, oDay, true);
			if ((getMonth(oMonYear)) < objDate.getMonth()+1) {
				alert(errorMSG['InvalidDate']);
				if (nlcheckID == 1) { // out
					oDay.selectedIndex = (getDay(oDay)-objDate.getDate()-1);
				} else {
					oDay.selectedIndex = (getDay(oDay)-objDate.getDate());
				}
				bReturn= false;
			}
		}
	}
	return bReturn;
}

function isReturnFirst(oDay,oMonth,oYear,oDate,rDay,rMonth,rYear,rDate,currentDate) {
	var bReturn;
	bReturn=true;
	with (document.forms[0]) {

		if (oDate >= rDate) {
			if (oDate > rDate) {
				alert(errorMSG['ReturnB4Outbound']);
				rDay.selectedIndex = oDay.selectedIndex + 1;
				rMonYear.selectedIndex = oMonYear.selectedIndex + 1;
				bReturn=false;

			} else {
				if (!confirm(errorMSG['FltSameDay'])){
					bReturn=false;

				}
			}
		}
	}
	return bReturn;
}

function validatePAX(bMode)
{
		// Validate the PAX information
		// Make sure only valid numbers have been entered	
		with (document.forms[0]) 
		{
			/*
			// Validate Adult PAX data	
			if(numOfAdults.value == '') 
			{
				numOfAdults.value = '0';
			}
			
			if(parseInt(numOfAdults.value)!=numOfAdults.value-0)
			{
				alert(errorMSG['AdultPAXNotValid']);
				numOfAdults.select();
				return false;
			}
			
			// Validate Child PAX data	
			if(numOfKids.value == '') 
			{
				numOfKids.value = '0';
			}
			
			if(parseInt(numOfKids.value)!=numOfKids.value-0)
			{
				alert(errorMSG['ChildPAXNotValid']);
				numOfKids.select();
				return false;
			}
			
			// Validate Infant PAX data	
			if(numOfInfants.value == '') 
			{
				numOfInfants.value = '0';
			}
			
			if(parseInt(numOfInfants.value)!=numOfInfants.value-0)
			{
				alert(errorMSG['InfantPAXNotValid']);
				numOfInfants.select();
				return false;
			}
			*/
			
			// Add up the number of adults and children and make sure it is not more than 40
			if(parseInt(numOfAdults.value) + parseInt(numOfKids.value) > 40)
			{
				alert(errorMSG['TooManyPax']);
				return false;
			}
			
		} // End With form[0]
		
		// All Valid
		return true;
}

function initPage(bMode) {
/*
	bMode
		1= all
		2= routes and orig date
		3= routes and no dates
*/	
	var strDestBeforeInit = "";
	with (document.forms[0]) {												
		if ((txtorigID.value != "") && ((txtorigID.value != "  "))) {
			orig.value = txtorigID.value;
			gERS.PopulateDestinationLocations(dest, orig.value);
		}else{
			var strOrig = getCookie("eJ_orig");
			if(strOrig==null) strOrig = getCookie("eJ_orig_perm");
			if(strOrig==null) strOrig="";
			txtorigID.value = strOrig;
			if ((txtorigID.value != "") && ((txtorigID.value != "  "))) {
				orig.value = txtorigID.value;
				gERS.PopulateDestinationLocations(dest, orig.value);
			}
			if(orig.value!=""){
				strDestBeforeInit = dest.options[dest.selectedIndex].value;
				gERS.PopulateDestinationLocations(dest, orig.value);
				for(var i=0;i<dest.options.length;i++){
					if(dest.options[i].value==strDestBeforeInit){
						dest.selectedIndex=i;
						break;
					}
				}
			}
		}
		if ((txtdestID.value != "") && ((txtdestID.value != "  "))) {
			dest.value = txtdestID.value;
		} else {
			var strDest = getCookie("eJ_dest");
			if(strDest!= "" && strDest!=null) selectOption(dest,strDest);	
		}
	
		if (bMode == 1||bMode == 2){
			if ((txtdorig.value != "") && ((txtdorig.value != "  "))) {
				oDay.value = txtdorig.value;
			}
		}
		if (bMode == 1){
			if ((txtddest.value != "") && ((txtddest.value != "  "))) {
				rDay.value = txtddest.value;
			}
		}
			
	}
}

/* Part 2 */

function HighlightName(checkBoxControl, SpanControlName)
{
  var CBElem = document.getElementById(checkBoxControl);
  var SpanElem = document.getElementById(SpanControlName);
  
  if(CBElem != null && SpanElem != null)
  {
    if(CBElem.checked == true)
      SpanElem.className = "SelectedPerson";
    else
      SpanElem.className = "UnSelectedPerson";
  }
  return;
}

function validateRegFlightForm(isDelete, AllowMultiple) {

	var inumOfPax=0;
	var inumOfAdults=0;
	var inumOfKids=0;
	var inumOfInfants=0;
	var bReturn=true;
  
  elems = document.forms[0].elements;
  
  var itemsTicked = 0;
  for (var i = 0; i < elems.length; i++){
    if(elems[i].name.indexOf('RIFlight') >= 0) {
      if(elems[i].checked)
        itemsTicked++;
    }
  }
  
  if(itemsTicked > 1 && AllowMultiple == false) {
    alert(TickOneOnlyRegFlight);
    return(false);
  }
  
  if(itemsTicked == 0) {
    alert(TickOneRegFlight);
    return(false);
  }
  
  if(isDelete == false) {
    with (document.forms[0])  {
	    var n=1;				
	    while(document.getElementById("passenger"+n)!=null){
		    if(document.getElementById("passenger"+n).checked==true){
			    inumOfPax++;
			    switch(document.getElementById("passenger"+n).value){
				    //case "infant":
					  //  inumOfInfants++;
					  //  break;
				    case "child":
					    inumOfKids++;
					    break;
				    case "adult":
					    inumOfAdults++;
					    break;
			    }
		    }
		    n++;
      }
    	
		  inumOfInfants = numOfInfants[numOfInfants.selectedIndex].value;
 
      numOfPax.value = inumOfPax;
      
		  if (inumOfKids>0 && inumOfAdults==0) {
		      if (!confirm(errorMSG['KidsOnly'])){
			      bReturn = false;
			      return bReturn;
		      }
	    }
	    if(parseInt(inumOfInfants) > parseInt(inumOfAdults) || parseInt(inumOfInfants) > 7) {    // Limit the number of infants to 7 parseInt(inumOfAdults)){
		     alert(errorMSG['InfantsOnly']);					
		     bReturn = false;
		     return bReturn;
	    }

		if (numOfPax.value < 1){
			alert(errorMSG['TooFewPax']);
			bReturn = false;
				return bReturn;
		}
      
		document.forms[0].numOfAdults.value = Number(inumOfAdults);
		document.forms[0].numOfKids.value = Number(inumOfKids);
		document.forms[0].numOfInfants.value = Number(inumOfInfants);
		document.forms[0].numOfPax.value = Number(inumOfAdults + inumOfKids + inumOfInfants);
		  
		if(isDelete == false) {     
			var ExceededFlights="";
			var ExceededSeats=false;
			for (var i = 0; i < elems.length; i++){
				if(elems[i].name.indexOf('RIFlight') >= 0 && elems[i].name.indexOf('RIFlightDep') < 0) {
					if(elems[i].checked == true) {
						var FlightID = elems[i].name.replace('RIFlight','');
			            
						var availableSeats = elems['RI_FlightAvailability' + FlightID];
			            
						if(availableSeats != null) {            
						if(Number(inumOfAdults) > Number(availableSeats.value) && Number(availableSeats.value)!=0) {
							ExceededSeats = true;
			                
							if(ExceededFlights.length > 0) ExceededFlights += ",";
							ExceededFlights += elems['RI_FlightDescriptor' + FlightID].value;
						}
						}
					}
				}
			}
		      
			if(ExceededSeats) {
				alert("There are insufficient ST seats for the passengers you require for flight " + ExceededFlights + ".");
				bReturn = false;
			}
		}
    } // with
  }
  
  return(bReturn);
}




function validateRegFlightFormStep3(isDelete, AllowMultiple) {

	var inumOfPax=0;
	var inumOfAdults=0;
	var inumOfKids=0;
	var inumOfInfants=0;
	var bReturn=true;
  
  elems = document.forms[0].elements;
  
  var itemsTicked = 0;
  for (var i = 0; i < elems.length; i++){
    if(elems[i].name.indexOf('RIFlight') >= 0) {
      if(elems[i].checked)
        itemsTicked++;
    }
  }
  
  if(itemsTicked > 1 && AllowMultiple == false) {
    alert(TickOneOnlyRegFlight);
    return(false);
  }
  
  if(itemsTicked == 0) {
    alert(TickOneRegFlight);
    return(false);
  }
  
  return(bReturn);
}
// End of Staff Notification Changes - VGE - May 2006

function validateInput(bMode,bValPax) {
/*
	bMode
		1= all
		2= routes and orig date
		3= routes and no dates
		4= Just Pax for step 3 Deal of the day validation
*/
	var bReturn;
	bReturn=true;
	

	var inumOfPax=0;
	var inumOfAdults=0;
	var inumOfKids=0;
	var inumOfInfants=0;
	// validates that the input field are present and constructed as a date for instance, at this point that is all we are interested in.
	// this then gives us a solid platform for the business rules for route validation and date validation (other functions)
	with (document.forms[0]) 
	{
		if(bMode!=4){
			// Validate origin and destination
			if (orig.value == "") {
				alert(errorMSG['NoOCity']);
				bReturn = false;
				return bReturn;
			} 

			if (dest.value == "") {
				alert(errorMSG['NoDestination']);			
				bReturn = false;
				return bReturn;
			}
			
			// do eastMidlands alert
			bReturn = doEastMidlandsAlert(orig.value, dest.value, oDay, oMonYear, rDay, rMonYear );
			if (bReturn == false) { return false; }
			
			if (bMode == 1 || bMode==2){
				if (!isDateValid(oDay, oMonYear, 1)) { // check the date is valid
					
					bReturn = false
				}
				if (bMode == 1){
					if (!isDateValid(rDay, rMonYear, 2)) { // check the date is valid
						
						bReturn = false
					}
				}	
			}
		}
		
		
		if (bValPax == 1){//only calculate num of pax when coming from stage 1 initial booking
		
			if(!validatePAX())
			{
				// PAX data is not correct
				bReturn = false;
				return bReturn;
			}
		
			if(bStaffTravel){
				var n=1;				
				while(document.getElementById("passenger"+n)!=null){
					if(document.getElementById("passenger"+n).checked==true){
						inumOfPax++;
						switch(document.getElementById("passenger"+n).value){
							//case "infant":
							//	inumOfInfants++;
							//	break;
							case "child":
								inumOfKids++;
								break;
							case "adult":
								inumOfAdults++;
								break;
						}
					}
					n++;
				}
				inumOfInfants = numOfInfants[numOfInfants.selectedIndex].value;
				
				numOfPax.value = inumOfPax;
			}else{
				
				numOfPax.value = (Number(numOfAdults[numOfAdults.selectedIndex].value) + Number(numOfKids[numOfKids.selectedIndex].value));
				
				inumOfAdults = numOfAdults[numOfAdults.selectedIndex].value;
				inumOfKids = numOfKids[numOfKids.selectedIndex].value;
				inumOfInfants = numOfInfants[numOfInfants.selectedIndex].value;				
			}
		}				
		
		if (bValPax != 2) {
			if (bValPax == 1){
				
				if (inumOfKids>0 && inumOfAdults==0) {
					if (!confirm(errorMSG['KidsOnly'])){
						bReturn = false;
						return bReturn;
					}
				}
				if(parseInt(inumOfInfants) > parseInt(inumOfAdults) || parseInt(inumOfInfants) > 7)
				{    // Limit the number of infants to 7 parseInt(inumOfAdults)){
					alert(errorMSG['InfantsOnly']);					
					bReturn = false;
					return bReturn;
				}
			}
			
			if (numOfPax.value < 1){
				alert(errorMSG['TooFewPax']);
				
				bReturn = false
			}

		}

		//New infant message
		if (bReturn && inumOfInfants > 0)
		{
			if (!(confirm(errorMSG['InfantInfo'])))
			{
				bReturn = false;				
			}
		}		
	} // with


	// this far, all is fine!...
	return bReturn;
}

function doEastMidlandsAlert(airportCodeOut, airportCodeRet, dayout, monyearout, dayret, monyearret)
{
  
   if (CheckAirportCodes(airportCodeOut, airportCodeRet) && (CheckEmaDate(dayout, monyearout) || CheckEmaDate(dayret, monyearret)) && CheckLanguageIsEnglish())
   {
	   var alertMessage = "Notice: easyJet is currently in consultation over a proposal to close operations at East Midlands Airport. If the proposal goes ahead the flights you are currently booking may not take place. If this proves to be the case you will be offered your choice of a full refund or re-accommodation on to a flight from another easyJet airport in the UK.";
	   if (!confirm(alertMessage))
	   {
	      return false;
	   }
	   else
	   {
	      return true;
	   }
   }
   else { return true; }
}

function CheckAirportCodes(airportCodeOut, airportCodeRet)
{
   if (airportCodeOut == 'EMA' || airportCodeRet == 'EMA')
   {
      return true;
   }
   else { return false; }
}

function CheckLanguageIsEnglish()
{
    var langddl = document.getElementById('langnav_select');
    if (langddl.value.indexOf('lang=en',0) > -1) 
    {
      return true;
    }
    else { return false; }

}


function CheckEmaDate(day, monyear)
{
   
    // if date not set then return true
    if (gED.getDay(day) == 00 && gED.getMonth(monyear) == 00) { return false; }
    
    var rDate = gED.constructDate(monyear, day, true);
    var checkDate = new Date(2010,00,04); 

    if (rDate > checkDate)
    {
       return true;
    }
    else  {  return false; }
}

function validateForm(bMode,bValPax) {
/*
	bMode
		1= all
		2= routes and orig date
		3= routes and no dates
*/
	var bReturn;
	bReturn=true;
	
	//need to clear late flight choice for staff travel
	if(bStaffTravel) {
		if(document.forms[0].lateFlightRequestFlightID!=null) document.forms[0].lateFlightRequestFlightID.value="";
	}
	
	// basic form validation, after this is a success we can be 100% sure to have good data route/dates and business rule validation...
	// 1 for initial booking (pax are calculated from adults and kids) 2 for changes (pax calculated seperately)
	if (!validateInput(bMode,bValPax)) {
		bReturn = false;
		return bReturn;
	}

	if (bMode == 1 || bMode==2){// validate the dates...
		if (!validateDates(document.forms[0], 1, bMode)) {
			bReturn = false;
			return bReturn;
		}
	}
	// validate the optional confirm rules...
	if (!validateBRs(bMode)) {
		bReturn = false;
	}

	if(document.forms[0].txtPromoCode){
		if(document.forms[0].txtPromoCode.value.length>20){
			alert(errorMSG['promo7']);	
			bReturn = false;	
		}
	}

	//newRoutes(); //???
	
	//Add destination to history cookie
	//if(bReturn) AddToDestHistory(document.forms[0].dest.value);

	return bReturn;
}

// initPage was here

// Staff Travel Changes - VGE - May 2006
function SetLateFlightOrig(){
	var strLFOrig = document.forms[0].lateTravelOriginator.value;
	setCookie("eJ_LForig",strLFOrig,"longtime");
	setCookie("eJ_LForig_perm",gES.FindMarket(strLFOrig),"longtime");
}

function ProcessLateFlightClick(flightAvailability, flightDeparture, flightInfo) {  
  var inumOfPax=0;
	var inumOfAdults=0;
	var inumOfKids=0;
	var inumOfInfants=0;
  var bReturn=true;
  
  with (document.forms[0])  {
    var n=1;				
    while(document.getElementById("passenger"+n)!=null){
	    if(document.getElementById("passenger"+n).checked==true){
		    inumOfPax++;
		    switch(document.getElementById("passenger"+n).value){
			    case "infant":
				    inumOfInfants++;
				    break;
			    case "child":
				    inumOfKids++;
				    break;
			    case "adult":
				    inumOfAdults++;
				    break;
		    }
	    }
	    n++;
    }
  	
    numOfPax.value = inumOfPax;
    
    if (inumOfKids>0 && inumOfAdults==0) {
	      if (!confirm(errorMSG['KidsOnly'])){
		      bReturn = false;
		      return bReturn;
	      }
    }
    if(parseInt(inumOfInfants) > parseInt(inumOfAdults) || parseInt(inumOfInfants) > 7) {    // Limit the number of infants to 7 parseInt(inumOfAdults)){
	     alert(errorMSG['InfantsOnly']);					
	     bReturn = false;
	     return bReturn;
    }

    if (numOfPax.value < 1){
      alert(errorMSG['TooFewPax']);
      bReturn = false;
	    return bReturn;
    }
    
    document.forms[0].numOfAdults.value = Number(inumOfAdults);
	  document.forms[0].numOfKids.value = Number(inumOfKids);
	  document.forms[0].numOfInfants.value = Number(inumOfInfants);
	  document.forms[0].numOfPax.value = Number(inumOfAdults + inumOfKids + inumOfInfants);
  } // with
    
  if(Number(flightAvailability) < Number(inumOfAdults + inumOfKids)) {
    alert("You have more people selected than there are available seats.");
    bReturn = false;
  }
  
  //check the time of departure
  var thisDateTime = new Date();
  thisDateTime.setHours(flightDeparture.substring(0,2),flightDeparture.substring(3,5))
	var currentDateTime = new Date();
	var dateOffset = (thisDateTime - currentDateTime)/1000/60;
	
  //if(dateOffset<=bufferTime){ //restore when using normal sales lead time
  if(dateOffset<=45){
		alert("There is not enough time to book this flight!");
    bReturn = false;
	}
  
  
  if(bReturn) {
    document.forms[0].lateFlightRequestFlightID.value=flightInfo; 
    document.forms[0].submit();
  }
  
  return bReturn;
}
// End of Staff Travel Changes - VGE - May 2006

function SetOrig(){
	var strOrig = document.forms[0].orig.value;
	setCookie("eJ_orig",strOrig,"");
	setCookie("eJ_orig_perm",gES.FindMarket(strOrig),"longtime");
	gERS.PopulateDestinationLocations(document.forms[0].dest, strOrig);
}
function SetDest(){
	var strDest = document.forms[0].dest.value;
	var strCookie = "eJ_dest=" + strDest + ";"
	document.cookie = strCookie;
}
function setCookie(cname,cvalue,cexpiry){
	if(cexpiry=="longtime"){
		var expires = new Date();
		expires.setTime(expires.getTime() + 3E11);   // about 10 years
		cexpiry=expires.toGMTString();
	}
	var strCookie = cname + "=" + cvalue
	if(cexpiry!="") strCookie += "; expires=" + cexpiry;
	document.cookie = strCookie;
}
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name) {
  if (getCookie(name)) {
    document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
function populateDays(oHTML, bOrig, bToday) {

	var sDay='';
	var nlPos=0;
	var dDay = new Date(serveryear,servermonth-1,serverday).getDate();
	var nlNow=0;
	for (var nlCount = 1; nlCount <= 31; nlCount++) {
		sDay = nlCount.toString();
		if (nlCount<10) {
			sDay = ('0'+ sDay)
		}
		if (bOrig) {
			nlPos = nlCount-1
		} else {
			nlPos = nlCount
		}
		if (bToday && (dDay == nlPos)) {
			nlNow = (nlPos-1);
		}
		oHTML.options[nlPos] = new Option(sDay, nlCount);
	}

	oHTML.selectedIndex = nlNow;

}
function validEmailAdd(){
		var Errs = 0;
		var emailVal = document.forms[1].TxtEmailadd.value;
		var strErrmsg = "- " + errorMSG['EnterAValid'] + ' ' + errorMSG['Email'];
		
		var filter  = /^[\w\.-]+(\+[\w-]*)?@([\w-]+\.)+[\w-]+$/;
		if (!filter.test(emailVal)){
			Errs = 1;
		}
	
		if (Errs==1){
			alert(strErrmsg);
			return false;
		} 
		else {
			return true;
		}
		
}
	
function clearEmailAddBox(){
	var emailVal = document.forms[1].TxtEmailadd.value;
	//alert(emailVal);
	if(emailVal =="Email@address"){
	document.forms[1].TxtEmailadd.value ="";
	}		
}

function restrictEnterKey(e){
	try
	{
	var characterCode; 
	if(e && e.which){ 
		e = e;
		characterCode = e.which; 
	}
	else{
		e = event;
		characterCode = e.keyCode; 
	}
	if(characterCode == 13){
		return false;
	}
	else{
		return true;
	}
	}
	catch(exc) {
		return true;
	}
}


/*******************************************************************************
*	From step1scripts.xsl template: date
*******************************************************************************/

function easyDate() {

	easyDate.prototype.formatDate = function(dteDateToFormat) {
		//This will return a date in dd MMM yyyy format
		var strDay = dteDateToFormat.getDate();
		var strMonth = monthArray[dteDateToFormat.getMonth() + 1];
		var strYear = dteDateToFormat.getFullYear();
		var strFormattedDate = strDay + " " + strMonth + " " + strYear;
		return strFormattedDate;
	}

	easyDate.prototype.constructDate = function(oMonYear, oDay, bMinus1) {
		
		var objYear = this.getYear(oMonYear);
		var objMonth = this.getMonth(oMonYear);
		var objDay = this.getDay(oDay);
		var objDate;
		
		if (bMinus1) {
			objDate = new Date(objYear, (objMonth-1), objDay);
		} else {
			objDate = new Date(objYear, objMonth, objDay);
		}
		return objDate;
	}

	easyDate.prototype.getYear = function(oMonYear) {
		return oMonYear.options[oMonYear.selectedIndex].value.substring(2,6);
	}

	easyDate.prototype.getMonth = function(oMonYear){
		return oMonYear.options[oMonYear.selectedIndex].value.substring(0,2)
	}

	easyDate.prototype.getDay = function(oDay) {		
		return oDay.options[oDay.selectedIndex].value;
	}
}


/*******************************************************************************
*	From step1scripts.xsl template: route
*******************************************************************************/

function sortIt(a,b) {
	if(String.prototype.localeCompare) {
		return a._desc.localeCompare(b._desc);
	}else{
		if (a._desc < b._desc) return -1;
    else if (a._desc > b._desc) return 1;
    else return 0;
	}
}

function Routes(pobjStations) {
	
	this._routes = new Array();
	this._routesHash = new Array();
	this._objStations=pobjStations;

	Routes.prototype.sortDate=function (sDate) {
		// niMode 1 = start date
		// niMode 2 = end date
		
		return new Date(sDate.substring(0,4), (sDate.substring(5,7)-1), sDate.substring(8,10));
		
	}
	
	Routes.prototype.AddNewRoute=function (sRoutes) {
		var sStations;
		var dStartDate; 
		var dEndDate;
		var sCP;
		

		while (sRoutes.indexOf('|') > 0) {					
			sStations = sRoutes.substring(0,sRoutes.indexOf('|'));
			sCP = sStations.substring(0,6);
			dStartDate = this.sortDate(sStations.substring(6, sStations.indexOf('?')));
			dEndDate = this.sortDate(sStations.substring((sStations.indexOf('?')+1)));
			
			this._routesHash[this._routesHash.length] = new Route(this, sCP, dStartDate, dEndDate);
			this._routes[this._routes.length] = new Route(this, sCP, dStartDate, dEndDate);
			
			sRoutes = sRoutes.substring((sStations.length+1));
		}
	}

	Routes.prototype.setLUDesc = function(objStation) {
		if(objStation._code.substring(0,1)=="*"||objStation._code=='ALL')
			return objStation._desc;		
		else
			return objStation._desc + " (" + objStation._code + ")";
	}
		
	Routes.prototype.PopulateOriginLocations=function (pHTMLSelectCtrl) {
		// popualtes HTMLSelect control with locations...
		var objStation = this._objStations._stations;
		
		objStation = objStation.sort(sortIt);
		pHTMLSelectCtrl.options.length = 0;
		objHTMLOption=new Option('->   ' + sTo, "");
		pHTMLSelectCtrl.options[0]=objHTMLOption;
		for (var niCount=0; (niCount < objStation.length); niCount++) {
			objHTMLOption = new Option(this.setLUDesc(objStation[niCount]), objStation[niCount]._code);
			pHTMLSelectCtrl.options[niCount+1]=objHTMLOption;
		}
	}
		
	Routes.prototype.PopulateDestinationLocations=function (pCtrl, pSDL) {
		// populates HTMLSelect control with locations.
		var arySortedRoutes;
		var objHTMLOption;
		var numPos=0;
		var objRoute;
		var strCurrentSel;
		var iOffset = 0; // Offset depends upon whether we are painting the '-> To' option or not
		if (pSDL==""){
			this.PopulateOriginLocations(pCtrl);
		}
		else{		
			//strCurrentSel = pCtrl.options[pCtrl.selectedIndex].value;
			strCurrentSel = pSDL;
		
			arySortedRoutes = this.FindTravelRoutes(pSDL);
			pCtrl.options.length=0;

			//First option is just the To ->
			//Not required for B2B - so check below
			if (typeof sTo != 'undefined')
			{
				objHTMLOption = new Option('->   ' + sTo, "");
				pCtrl.options[0] = objHTMLOption;
				iOffset = 1;
			}			
			for (var nlCount=0; (nlCount < arySortedRoutes.length); nlCount++) {
				objRoute = arySortedRoutes[nlCount];
				if (objRoute._Islocal==true) {
					objHTMLOption = new Option(this.setDEDesc(objRoute._desc,objRoute._TSDesc), objRoute._TSDesc);
				} else {
					objHTMLOption = new Option(this.setDEDesc(objRoute._desc,objRoute._FSDesc), objRoute._FSDesc);				
				}
			
				pCtrl.options[nlCount+iOffset] = objHTMLOption;
			}
			
			selectOption(pCtrl,strCurrentSel);
		}
	}
	
	Routes.prototype.setDEDesc = function(desc, code) {
		if(code.substring(0,1)=="*"||code=='ALL')
			return desc;		
		else
			return desc + " (" + code + ")";
	}

	
	Routes.prototype.FindTravelRoutes=function (pSDL) {
		// Finds travel routes and sorts these based on the long description.
		var objSortedLongDescIndexMap=new Array();
		var objRoutesFound=new Array();
		var objRoute;
		var objSortedRoutesFound=new Array();
		var objSortedRoute;
		
		Array.prototype.exists=function(tDest) {
			var bExists=false;
			for (var intPos=0; (intPos < this.length); intPos++) {
				if(this[intPos]._FSDesc==tDest || this[intPos]._TSDesc==tDest) {bExists=true;break;}
			}
			return bExists;
		}
		var pSDLOld=pSDL;
		
		var objMarkets=new Array();
		objMarkets=this._objStations.GetMarkets();
		if(pSDL.substring(0,1)=="*") {
			var oStation;
			oStation=this._objStations.FindLongDesc(pSDL);
			for (var intMPos=0; (intMPos < oStation._marketrange.length); intMPos++) {
				pSDL=	oStation._marketrange[intMPos];
				for (var intPos=0; (intPos < this._routes.length); intPos++) {	
					objRoute =new Object();
					objRoute = this._routes[intPos];
					bIslocal=(objRoute._FSDesc==pSDL)?true:false;
					if (objRoute._FSDesc==pSDL || objRoute._TSDesc==pSDL) {
						if (objRoutesFound.exists(bIslocal?objRoute._TSDesc:objRoute._FSDesc)==false) {
							objRoutesFound[objRoutesFound.length] = objRoute;
							objRoutesFound[objRoutesFound.length-1]._Islocal=bIslocal;
						}
					}
				}
			}
		}else {
			for (var intPos=0; (intPos < this._routes.length); intPos++) {			
				objRoute = this._routes[intPos];
				bIslocal=(objRoute._FSDesc==pSDL)?true:false;
				if (objRoute._FSDesc==pSDL || objRoute._TSDesc==pSDL) {
					objRoutesFound[objRoutesFound.length] = objRoute;
					objRoutesFound[objRoutesFound.length-1]._Islocal=bIslocal;
				}
			}
		}
		var bMarketRouteCount=0;
		for (var intMarketPos=0; (intMarketPos < objMarkets.length); intMarketPos++) {
			var objMarket=objMarkets[intMarketPos];
			for (var intMPos=0; (intMPos < objMarket._marketrange.length); intMPos++) {			
				if(objRoutesFound.exists(objMarket._marketrange[intMPos])==true && objMarket._code!=pSDLOld) {					
					bMarketRouteCount++
				}
			}
			if(bMarketRouteCount>1){objRoutesFound[objRoutesFound.length]=objMarket;}
			bMarketRouteCount=0;
		}
				
		var objStationFound;
		var stationLookingFor;

		// get long descriptions and sort by this to create real order to return objects in.
		for (var intPos=0; (intPos < objRoutesFound.length); intPos++) {			
			objRoute = objRoutesFound[intPos];
			
			if (objRoute._Islocal==true) {
				stationLookingFor=objRoute._TSDesc;
				objStationFound=this._objStations.FindLongDesc(stationLookingFor);
				objRoute._desc=objStationFound._desc;
			} else {
				stationLookingFor=objRoute._FSDesc;
				objStationFound=this._objStations.FindLongDesc(stationLookingFor);
				objRoute._desc=objStationFound._desc;
			}
		}
		
		objRoutesFound.sort(sortIt);
		
		return objRoutesFound;
	}

	Routes.prototype.verifyRoute=function (dDDate, dRDate, sFrom, sTo) {
		var sCityPair = (sFrom + sTo);
		var bValid = true;
		var brouteFound=true;
		for (var intPos=0; (intPos < this._routes.length); intPos++) {			
			objRoute = this._routes[intPos];
			if (((objRoute._FSDesc==sFrom) && (objRoute._TSDesc==sTo)) || ((objRoute._TSDesc==sFrom) && (objRoute._FSDesc==sTo))) {
				if (dDDate < objRoute._SFD) {
					alert(errorMSG['FlightNotStarted'].replace('%DATA%', gED.formatDate(objRoute._SFD)));
					bValid = false;
				} else if (dDDate > objRoute._STD) {
					alert(errorMSG['FlightFinished'].replace('%DATA%', gED.formatDate(objRoute._STD)));
					bValid = false;
				} else if (dRDate > objRoute._STD) { // only need to check the end date, fisrtly, has to be passed the start date
					alert(errorMSG['FlightFinished'].replace('%DATA%', gED.formatDate(objRoute._STD)));
					bValid = false;
				}
			}
		}
		if (brouteFound==false) {
			alert(errorMSG['NoRouteMatch']);
			return false;
		}else{return bValid;}
	}
}

function Route(pobjRoutes, pCP, sSellFrom, sSellTo) {
	this._FSDesc=pCP.substring(0,3);
	this._TSDesc=pCP.substring(3,6);
	this._SFD=sSellFrom;
	this._STD=sSellTo;
	this._parent=pobjRoutes;
	this._Islocal;
}

function selectOption(pCtrl,strVal){
	var nlCount;
	if(pCtrl==null) return;
	if(pCtrl.options){
		for (nlCount=0; (nlCount < pCtrl.options.length); nlCount++) {
			if(pCtrl.options[nlCount].value==strVal){
				pCtrl.selectedIndex = nlCount;
				break;
			}
		}
	}
}


/*******************************************************************************
*	From step1scripts.xsl template: stations
*******************************************************************************/

// temporary static include -- needs to be autogenerated.
function Stations() {
	this._stations=new Array();
	
	Stations.prototype.Add=function(sStations) {
		var sSStation; 
		var oStation;
		while (sStations.indexOf('|') > 0) {
			sSStation = sStations.substring(0,sStations.indexOf('|'))
			oStation=new Station(sSStation.substring(0,3), sSStation.substring(3));
			if(oStation._code.substring(0,1)=="*") {
				this.BuildStationMarket(oStation);
			}
			this._stations[this._stations.length] = oStation;
			sStations = sStations.substring((sSStation.length+1));
		}

	}
	
	Stations.prototype.GetMarkets=function() {
		var objMarkets=new Array();
		with (this) {
			for (var nlCount=0; (nlCount < _stations.length); nlCount++) {
				if (_stations[nlCount]._code.substring(0,1)=="*") {
					objMarkets[objMarkets.length] = _stations[nlCount];
				}
			}
		}
		return objMarkets;
	}
	
	Stations.prototype.BuildStationMarket=function(oStation) {
		var oMarket;
		var sStation;
		var sMarket=oStation._desc;
		
		sStation = sMarket.substring(0,sMarket.indexOf('?'))
		sMarket=sMarket.substring(sMarket.indexOf('?'))
		sMarket=sMarket.substring(1,sMarket.length-1)
		
		oMarket = sMarket.split("?");
		
		oStation._desc=sStation;
		oStation._marketrange=oMarket;
	}
	
	Stations.prototype.FindLongDesc=function(ShortDesc) {
		var objFS;
		with (this) {
			for (var nlCount=0; (nlCount < _stations.length); nlCount++) {
				if (_stations[nlCount]._code == ShortDesc) {
					objFS = _stations[nlCount];
					break;	
				}
			}
		}
		return objFS;
	}
	
	Stations.prototype.FindMarket = function(StationCode){
		var MarketCode = "";
		var oMarket;
		with (this) {
			for (var nlCount=(_stations.length-1); (nlCount >=0 ); nlCount=(nlCount-1)) {
				if (_stations[nlCount]._code.substr(0,1)=='*') {
					oMarket = _stations[nlCount]._marketrange;
					for(var mktcount=0; mktcount < oMarket.length; mktcount++){
						if(oMarket[mktcount]==StationCode){
							MarketCode = _stations[nlCount]._code;
							break;
						}
					}
					if(MarketCode!="") break;
				}
			}
			if(MarketCode=="") MarketCode = StationCode;
		}
		return MarketCode;
	}
}

function Station(pCode, pDesc) {
	this._code=pCode;
	this._desc=pDesc;
	this._FSDesc=pCode;
	this._marketrange=null;
}


/*******************************************************************************
*	From step1scripts.xsl template: step1Intranet
*******************************************************************************/

function limitPax(){
	var strCookieName = 'staffTravel';
	var strCookie = '';
	var intCookieStart = 0;
	var cookieCrumbs = new Object();

	if (document.cookie.indexOf(strCookieName)){
		intCookieStart = document.cookie.indexOf(strCookieName) + strCookieName.length + 1
		strCookie = document.cookie.substring(intCookieStart,document.cookie.indexOf(';',intCookieStart))

		var cookieCrumbs = new Object()
		cookieCrumbs = strCookie.split('*!*')
		document.forms[0].numOfAdults.value = Number(cookieCrumbs[10]);
		document.forms[0].numOfKids.value = Number(cookieCrumbs[11]);
		document.forms[0].numOfInfants.value = Number(cookieCrumbs[12]);
		document.forms[0].numOfPax.value = Number(cookieCrumbs[13]);
	}else{
		alert('cookie is not set. unable to proceed with booking. Please contact IT support or a member of the web team.');
		window.location = 'http://inside.easyjet.com/people/stafftravel/';
	}
}


/*******************************************************************************
*	From step1scripts.xsl template: setOptions
*******************************************************************************/

	function setOptions(strOrig,strDest,strDate,bNotRestricted){
		var pCtrl;
		
		if(bNotRestricted){
			pCtrl = document.forms[0].orig;
			selectOption(pCtrl,strOrig);
			
			pCtrl = document.forms[0].dest;
			gERS.PopulateDestinationLocations(pCtrl, strOrig);		
			selectOption(pCtrl,strDest);
		} else {
			pCtrl = document.forms[0].orig;
			PopulateLocation(pCtrl,strOrig)
					
			pCtrl = document.forms[0].dest;
			PopulateLocation(pCtrl,strDest);			
		}		
		
		var aryDate = strDate.split("-");
		
		if(document.forms[0].oBookingDay!=null){
			pCtrl = document.forms[0].oBookingDay;
			pCtrl.value = aryDate[2];
			
			pCtrl = document.forms[0].oBookingMonYear;
			pCtrl.value = aryDate[1]+""+aryDate[0];
		}
		
		var today = new Date();
		var inputDate = new Date();
		
		inputDate.setFullYear(aryDate[0]);
		inputDate.setMonth(aryDate[1]-1);
		inputDate.setDate(aryDate[2]);
		
		if(today>inputDate){
			var sMonth = (today.getMonth()+1).toString();
			if(today.getMonth()<9) sMonth = "0"+sMonth;
			aryDate = new Array(today.getFullYear(),sMonth,today.getDate());
		}
				
		pCtrl = document.forms[0].oDay;
		selectOption(pCtrl,aryDate[2]);
		
		pCtrl = document.forms[0].oMonYear;
		selectOption(pCtrl,aryDate[1]+""+aryDate[0]);
		
	}
	
	function selectFirstOption(){
		var strCmd="";
		if(document.forms[0].radItinerary){
			if(document.forms[0].radItinerary.length){
				document.forms[0].radItinerary[0].checked=true;
				document.forms[0].radItinerary[0].onclick();
			} else {
				document.forms[0].radItinerary.checked=true;
				document.forms[0].radItinerary.onclick();
			}
		}
	}
		
	function PopulateLocation(pCtrl,strStation){
		var sDesc = "";
		
		if(pCtrl==null) return;
		
		pCtrl.options.length=0;
		
		for(var i=0;i<gES._stations.length;i++){
			if(gES._stations[i]._code==strStation){
				sDesc = gES._stations[i]._desc;
			}			
		}
		
		var objHTMLOption = new Option(sDesc,strStation);				
					
		pCtrl.options[0] = objHTMLOption;
		
		if(strMkts.indexOf("?"+strStation)>0){
			for(i=0;i<arrMkts.length;i++){
				if(arrMkts[i].indexOf("?"+strStation)>0){
					var sMkt = arrMkts[i].substr(0,3);
					sDesc = arrMkts[i].substring(3,arrMkts[i].indexOf("?"));
				}
			}
			objHTMLOption = new Option(sDesc,sMkt);
			pCtrl.options[1] = objHTMLOption;
		}
		
		pCtrl.selectedIndex = 0;		
	}


/*******************************************************************************
*	From step1scripts.xsl template: popup
*******************************************************************************/

	function OpenPopup(strPopupID){
		var objPopup = document.getElementById(strPopupID);
		if(objPopup.getAttribute("closed")==null){
			objPopup.style.display="block";
			if(document.getElementById("langnav")!=null) 
				document.getElementById("langnav").style.visibility="hidden";
		}
	}
	
	function ClosePopup(strPopupID){
		var objPopup = document.getElementById(strPopupID);
		objPopup.setAttribute("closed","Y");
		objPopup.style.display="none";
		if(document.getElementById("langnav")!=null)
			document.getElementById("langnav").style.visibility="visible";
	}
	function TogglePopup(strPopupID){
		var objPopup = document.getElementById(strPopupID);
		if(objPopup.style.display=="none"){
			objPopup.style.display="block";
			if(document.getElementById("langnav")!=null)
				document.getElementById("langnav").style.visibility="hidden";
		} else {
			objPopup.style.display="none";
			if(document.getElementById("langnav")!=null)
				document.getElementById("langnav").style.visibility="visible";
		}
	}

    /* The search faq box should not submit the background text value */
    /* We could display a validation msg instead of submitting a clear value instead -- to be reviewed */
    /* Do a case insensitive comparision of strings */
    function validateSearchBox(searchBox,strBackgroundText){
	    var strSearchValue = searchBox.value;
	    if(strSearchValue == strBackgroundText)
	        searchBox.value = '';
    }