var v_currentOutboundSelectedID = 0;
var v_currentReturnSelectedID 	= 0;

/*Rollover states*/
/*The dontRoll fearture is to stop the row rollovers when a cell rollover has already run*/
function farePickOver(cell)	{genOver(cell,'#999999');}
function farePickOut(cell)	{genOut (cell,'');}
/*Roll-out function*/
function genOut(cell,col)
{
	cell.style.textDecoration='none';
	cell.style.backgroundColor=col;
}
/*Roll-over function*/
function genOver(cell,col)
{
	cell.style.textDecoration='underline';
	cell.style.backgroundColor=col;
}

// RailEuropWebService relate JS's
function lockReturnOptions(thisId,l_compatible_return_fares) {
	//Loop through all the radio options for the "returnPriceIdentifier" radio.
	for (i=0;i<document.getElementsByName("returnPriceIdentifier").length;i++) {
		str_l_compatible_return_fares 	= l_compatible_return_fares;
		strThisFareId 					= document.getElementsByName("returnPriceIdentifier")[i].value;
		v_name 					= document.getElementsByName("returnPriceIdentifier")[i].id;
		v_name 					= 'div_'+v_name.substring(6,v_name.length);
		if (str_l_compatible_return_fares.indexOf(strThisFareId)>=0 || l_compatible_return_fares == '') {
			document.getElementsByName("returnPriceIdentifier")[i].disabled = false;
			document.getElementById(v_name).style.color='#000000';
		} else {
			//Un-check it before you disable it.
			document.getElementsByName("returnPriceIdentifier")[i].checked = false;
			//Now disable it.
			document.getElementsByName("returnPriceIdentifier")[i].disabled = true;
			document.getElementById(v_name).style.color='#CCCCCC';
		}
	}
}
function selectedOutboundPrice(clickedItem,l_compatible_return_fares) {
	v_clickedItemId = clickedItem.id;
	v_clickedItemId = v_clickedItemId.replace('div_','');
	if (document.getElementById('radio_'+v_clickedItemId).disabled != true) {
		lockReturnOptions(v_clickedItemId,l_compatible_return_fares);
		if (v_currentOutboundSelectedID > 0) {
			//Set DIV to default colours
			document.getElementById(v_currentOutboundSelectedID).style.backgroundColor='';
			document.getElementById(v_currentOutboundSelectedID).style.color='#000000';
		}
		//Set the checkbox to checked and the highlight the DIV
		document.getElementById('radio_'+v_clickedItemId).checked = true;
		document.getElementById('div_'+v_clickedItemId).style.backgroundColor='#FF0000';
		document.getElementById('div_'+v_clickedItemId).style.color='#FFFFFF';
		v_currentOutboundSelectedID = 'div_'+v_clickedItemId;		
	}
}
function selectedReturnPrice(clickedItem) {
	v_clickedItemId = clickedItem.id;
	v_clickedItemId = v_clickedItemId.replace('div_','');
	if (document.getElementById('radio_'+v_clickedItemId).disabled != true) {		
		if (v_currentReturnSelectedID > 0) {			
			document.getElementById(v_currentReturnSelectedID).style.backgroundColor='';
			document.getElementById(v_currentReturnSelectedID).style.color='#000000';
		}
		document.getElementById('radio_'+v_clickedItemId).checked = true;
		document.getElementById('div_'+v_clickedItemId).style.backgroundColor='#FF0000';
		document.getElementById('div_'+v_clickedItemId).style.color='#FFFFFF';
		v_currentReturnSelectedID = 'div_'+v_clickedItemId;
	} else {
		alert('Sorry, that fare is unavailable:\nThe option is disabled because it is not compatible with the outbound fare you\'ve selected. Either chose a different return option or change your outbound fare to find one which is compatible with this option.');
	}
}

function checkText(txtStr)
		{
		var TxtCount=0                       
            for(i=0;i<txtStr.length; i++)                        
            if(!((txtStr.charAt(i)>="0" && txtStr.charAt(i)<="9")))
			{
				TxtCount++
			}
			if(TxtCount>0)
                  	{
					return false
				}
			else				  
			 	{
					return true
				}
       	}

function numericonly(formname,obj1name,p)
		{
		//obj1 = eval("document."+formname+"."+obj1name);
		obj1 = document.getElementById(obj1name);
		if(!checkText(obj1.value))
		{
						obj1_numeric = obj1.value;
						obj1_numericlen = obj1_numeric.length;
						obj1_numericlen = obj1_numericlen - 1;
						//alert(SupplierPriceLen);
						obj1_numeric = obj1_numeric.substring(0,parseInt(obj1_numericlen));
						//alert(SupplierPrice);
						obj1.value = "";
					{
						alert("Please enter a correct age for Traveller " + p);
					}
						
		}
		}
		
	function windowOpener(url,width,height,target,isfullscreen,top,left) 
      {
       browserName = navigator.appName;
       browserVer = navigator.appVersion.substring(0,1);
       if (width==null) {width=600;}
       if (height==null) {height=400;}
       if (top==null) {top=40;}
       if (left==null) {left=50;}
       if (browserName != "Netscape" || browserVer != 2)
       {
controlWindow=window.open(url,target,"toolbar=no,height="+height+",width="+width+",top="+top+",left="+left+",location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,fullscreen="+isfullscreen);
       }
      }
function ShowMenu(n) {
	var menu, arrowImg
	menu = document.getElementById("d" + n);
	
	// Determine if the menu is currently showing.
	if (menu.style.display == 'block') {
		// If it is showing, hide the menu and update the twisty image.
		menu.style.display = 'none'
	} else {
		// Hide all layers first.		
		var divs = document.getElementsByTagName("div")		
		// Show the menus and update their twisty images.
		i = 2
		while (n.length >= i) {
			menu = document.getElementById("d" + n.substring(0, i));
			menu.style.display = "block";
			i += 2;
		}
	}
}
	

function wopen(url, w, h)
{
// Fudge factors for window decoration space.
 // In my tests these work well on all platforms & browsers.
w += 32;
h += 96;
 var win = window.open(url,
  'popup', 
  'width=' + w + ', height=' + h + ', ' +
  'location=no, menubar=no, ' +
  'status=no, toolbar=no, scrollbars=yes, resizable=yes');
 win.resizeTo(w, h);
 win.focus();
}

function makeFieldAppearDisabled(fieldName) {
	document.getElementById(fieldName).disabled 				= true;
	document.getElementById(fieldName).style.color 				= '#999999';
	document.getElementById(fieldName).style.backgroundColor 	= '#eeeeee';
}
function makeFieldAppearEnabled(fieldName) {
	document.getElementById(fieldName).disabled 				= false;
	document.getElementById(fieldName).style.color 				= '#000000';
	document.getElementById(fieldName).style.backgroundColor 	= '#FFFFFF';
}

function addBookmark(title, url) {
	var title, url;
	title = 'Rail Plus - '+document.title;
	url = location.href;
	if (document.all) { 			//MSIE
		window.external.AddFavorite(url, title);
	} else if (window.sidebar) { 	// FireFox
		window.sidebar.addPanel(title, url,"");
	} else {
		alert("Sorry, your browser doesn't support this action.");
	}
}

function SelectBoxRedirect(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function OpenBrWindow(theURL,winName,features, myWidth, myHeight, isCenter) { //v3.0
  if(window.screen)if(isCenter)if(isCenter=="true"){
    var myLeft = (screen.width-myWidth)/2;
    var myTop = (screen.height-myHeight)/2;
    features+=(features!='')?',':'';
    features+=',left='+myLeft+',top='+myTop;
  }
  window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
}

function toggleLayer(objLayer, action){
	if (action == 'hide'){
		objLayer.style.display = 'none';
	}else{
		objLayer.style.display = '';
	}
}

// JavaScript Rollover Functions
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
// This code block somehow magically gets the indexOf() on an array to work!
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

function openLuxuryEbook() {
	var url = 'http://editions.magsbyme.com/ebook/ebook.php?id=10008619#/0';
	var windowName = '_Lite_eMagazine';
	var windowFeatures = 'type=fullWindow,fullscreen=yes,width=99999,height=99999,status=0,status=no,statusbar=no,resizable=yes,scrollbars=yes';
	window.open(url, windowName, windowFeatures);
}


function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/datevalidation.asp)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr,elementId){
	var daysInMonth = DaysArray(12);
	var pos1 = dtStr.indexOf(dtCh);
	var pos2 = dtStr.indexOf(dtCh,pos1+1);
	var strDay = dtStr.substring(0,pos1);
	var strMonth = dtStr.substring(pos1+1,pos2);
	var strYear = dtStr.substring(pos2+1);
	var day = "";
	var month = "";
	var year = "";
	var currentDate = new Date();
	var currentYear = currentDate.getFullYear();
	var currentTwoDigitYear = currentYear.toString().slice(2);
	
	if (strDay.charAt(0)=="0" && strDay.length>1) {
		day = parseInt(strDay.substring(1));
	}else{
		day = parseInt(strDay);
	}
	if (strMonth.charAt(0)=="0" && strMonth.length>1){
		month = parseInt(strMonth.substring(1));
	}else{
		month = parseInt(strMonth);
	}
	for (var i = 1; i <= 3; i++) {
		if (strYear.charAt(0)=="0" && strYear.length>1) strYear=strYear.substring(1)
	}
	
	year=parseInt(strYear);
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy");
		return false;
	}
	//pad out single digit months
	if (strMonth.length < 2){
		strMonth = "0" + strMonth;
		document.getElementById(elementId).value = strDay+"/"+strMonth+"/"+strYear;
	}
	if (strMonth.length<2 || month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	//pad out single digit months
	if (strDay.length < 2){
		strDay = "0" + strDay;
		document.getElementById(elementId).value = strDay+"/"+strMonth+"/"+strYear;
	}
	if (strDay.length<2 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert(strDay.length);.
		alert("Please enter a valid day");
		return false;
	}
	//Check if a two digit year has been entered by the user
	if (strYear.length <= 2) {
		//If the year is in the 2000's
		if (Number(strYear) <= Number(currentTwoDigitYear)) {
			if (strYear.length == 2) {	
				//prefix the century
				strYear = "20" + strYear;
			}else{
				//prefix the century and pad out sub 2010 years with a extra 0
				strYear = "200" + strYear;
			}
		}else{
			//Must be pre 21st century date, prefix with 19
			strYear = "19" + strYear;
		}
		//correct the year variable for the following checks
		year = parseInt(strYear);
		//write the new date value to the corresponding text field 
		document.getElementById(elementId).value = strDay+"/"+strMonth+"/"+strYear;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>currentYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+currentYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date in the format dd/mm/yyyy.\neg. 24/12/1990");
		return false;
	}
	return true;
}

function checkValidDate(frmDateField){
	var dateField=document.getElementById(frmDateField);
	if (isDate(dateField.value,frmDateField)==false){
		dateField.focus();
		return false;
	}
	return true;
}

