var browserIE = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
var dateObj = new Date();
var curTime = addZero_func(dateObj.getHours(),2)+""+addZero_func(dateObj.getMinutes(),2);
var curYear = dateObj.getFullYear();
var curDate = dateObj.getDate();
var curMonth = dateObj.getMonth()+1; //Add 1 to get it in format 1-12, javascript uses 0-11

function initPage_func(){
	navObjs = document.getElementById('secmaintransports');
	divObjs = navObjs.getElementsByTagName('div');
	aObjs = navObjs.getElementsByTagName('a');
	divObjsLen = divObjs.length;
	for(i=0;i<divObjsLen;i++){
		aObjs[i].colorOver = divObjs[i].style.backgroundColor;
		aObjs[i].colorOut = aObjs[i].style.backgroundColor;
		aObjs[i].onmouseover = function () {
			this.style.backgroundColor=this.colorOver;
		}
		aObjs[i].onmouseout = function () {
			this.style.backgroundColor=this.colorOut;
		}
	}
	IEPngFix_func();
}

function setOpac_func(){
	/*navObj = document.getElementById('contentnav');
	navObj = document.getElementById('logo');
	opcOK=0;
	if (navigator.appName.toLowerCase().indexOf("netscape") != -1){
		navObj.style.MozOpacity=Number(navObj.style.MozOpacity)+Number(0.1);
		if (navObj.style.MozOpacity >= 1){
			opcOK=1;
		}
	} else {
		navObj.filters.alpha.opacity=Number(navObj.filters.alpha.opacity)+Number(10);
		if (navObj.filters.alpha.opacity >= 100){
			opcOK=1;
		}
	}
	if (opcOK==0){
		setTimeout("setOpac_func()", 40);
	}*/
}
function setStyle_func(obj, stylevar, styleval){
	if (navigator.userAgent.toLowerCase().indexOf("msie") > -1){
		obj.style[stylevar]=styleval;
	} else {
		obj.style[stylevar]=styleval;
	}
}

function init_func(){	
	navObjs = document.getElementsByTagName("a")
	navL = navObjs.length;
	for(i=0;i<navL;i++){
		if (navObjs[i].className == "anav"){
			navObjs[i].onmouseover = navOver_func;
			navObjs[i].onmouseout = navOut_func;
		} else if (navObjs[i].className == "subnav"){
			navObjs[i].onmouseover = subnavOver_func;
			navObjs[i].onmouseout = subnavOut_func;		
		}
	}
	//setDateTime_func(curYear+""+addZero_func(curMonth,2)+""+addZero_func(curDate,2), curTime, document.forms.tfljp);
}

function navOver_func(){
	setStyle_func(this, 'background', '#'+this.id);
}

function navOut_func(){
	setStyle_func(this, 'background', '#B2B2B2');
}

function subnavOver_func(){
	setStyle_func(this, 'color', pgColor);
}

function subnavOut_func(){
	setStyle_func(this, 'color', '#B2B2B2');
}

function showDepDetails_func(selObj){
	if (selObj.value.length > 0){
		loc = location.href.split("?")
		if (loc.length < 1){
			location.href=location.href+"?depart="+selObj.value
		} else {
			location.href=loc[0]+"?depart="+selObj.value
		}
	}
}

function txtfocus_func(txtobj){
	if (txtobj.init == undefined){
	//if (txtobj.value=="location"){
		txtobj.init=1;
		txtobj.value="";
	}
}

function jpTxtfocus_func(txtobj){
	//if (txtobj.init == undefined){
	if (txtobj.value=="location"){
		txtobj.init=1;
		txtobj.value="";
	}
}
function OpenBrWindow(url, wname, topleft, fullscreen, appw, apph, scroll) {
	if (fullscreen == 1){
		fscreen = "fullscreen"
	} else {
		fscreen = ""
	}
	if (topleft == 1){
		screenx = 0
		screeny = 0
	} else {
		sreenw = screen.width
		sreenh = screen.height
		screenx = (sreenw - appw)/2
		screeny = (sreenh - apph)/2
	}	
	newWin = window.open(url, wname, "toolbar=no,status=yes,"+fscreen+",scrollbars="+scroll+",resizable=yes,menubar=no,width="+appw+",height="+apph+",left="+screenx+",top="+screeny+", maximize=yes");
	//return newWin;	
}

function checkform_func(form){
	formlen = form.length;
	submitform = 1;
	for (i=0;i<formlen;i++){
		if (form.elements[i].type == "text" || form.elements[i].type == "textarea" || form.elements[i].type == "file" || form.elements[i].type == "password"){
			if (form.elements[i].value == "" ){
				submitform = 0
			}
		}
	}

	if (submitform == ""){
		alert("Please fill in all fields!!")
		return false;
	} else {
		form.submit();
	}
}

function addZero_func(no, digits){
	if (String(no).length >= Number(digits)){
		return no;
	} else {		
		no_no = Number(digits) - String(no).length
		zeros = ""
		for(p=1;p<=no_no;p++){
			zeros += "0";
		}
		return zeros+no;
	}
}

function chgImg_func(imgobj, imgsrc){
	if (imgobj.src.indexOf("blogger_face.gif") == -1){
		if (imgobj.src.indexOf("_out") != -1){
			newSrc = imgobj.src.split("_over");
			imgobj.src = newSrc.join("_out");
		} else {
			imgobj.src = imgsrc;
		}
	}
}

function preloadImgs_func(arr){	
	for(i=0;i<arr.length;i++){
		var preloadImg = new Image();
		preloadImg.src = arr[i];			
	}
}

function txtarea_func(txtareaObj, strlen){
	txtareal = txtareaObj.value.length;
	if (txtareal > strlen){
		txtareaObj.value = txtareaObj.value.substr(0,strlen);
	}
	if (browserIE){
		parElemt = txtareaObj.parentElement.getElementsByTagName('SPAN');
	} else {
		parElemt = txtareaObj.parentNode.getElementsByTagName('SPAN');
	}
	if (parElemt[0]){
		parElemt[0].firstChild.nodeValue = "(max "+strlen+" characters, "+(strlen-txtareal)+" characters left)"
	}
}

// ***** JOURNEY PLANNER FUNCTIONS ******
function frmSubmit_func(but, frm, act){	
	showalert=0;
	if (frm.txtfrom){
		if (frm.txtfrom.value == "location"){
			showalert=1;
		}
	}
	if (frm.txtto){
		if (frm.txtto.value == "location"){
			showalert=1;
		}
	}
	if (showalert == 1){
		alert("Please enter a valid Origin and Destination location!")
	} else {
		if (act != undefined){
			frm.action = act;
		}
		but.disabled=true;
		frm.submit();
	}
}

function restSubmit_func(frm){
	confvar = confirm("Are you sure you want to start again?")
	if (confvar){
		location.href="journey1.asp?frmreset=1";
		/*frm.reset();
		datObj = new Date(	);
		theDay = datObj.getDate();
		theMonth = datObj.getMonth();
		theYear = datObj.getFullYear();					
		frm.serveract.value="";
		frm.dtyear.value=datObj.getFullYear();
		frm.dtmonth.value=datObj.getMonth()+1;
		frm.dtday.value=datObj.getDate();
		frm.dthour.value=datObj.getHours();
		frm.dtmin.value=datObj.getMinutes();
		frm.frmreset.value=1;
		frmObjs = new Array("fromgetcoordsby", "txtfrom", "txtfromaddr", "fromtype", "fromsearchtype", "fromcoords", "togetcoordsby", "txtto", "txttoaddr", "totype", "tosearchtype", "tocoords");
		ilen=frmObjs.length;
		for (i=0;i<ilen;i++){
			if (frm[frmObjs[i]]){
				frm[frmObjs[i]].value="";	
			}
		}
		//frm.fromsearchtype.value="";
		//frm.tosearchtype.value="";
		//frm.fromsearchtype.value="";
		//frm.tosearchtype.value="";		
		//alert(datObj.getFullYear()+"/"+datObj.getMonth()+"/"+datObj.getDate()+" "+datObj.getHours()+datObj.getMinutes());
		frm.submit()
		//frm.reset();*/
	}
}

function clearFrm_func(){	
	//location.href="default.asp?stover=1";
	location.href="startover.asp";
}

function setDateTime_func(dateVal, TimeVal, frm){ // dateVal must be in format yyyymmdd
	frm.itdDate.value = dateVal;
	frm.itdTime.value = TimeVal;
}

function openMap_func(x,y){
	OpenBrWindow('http://www.journeyon.co.uk/hypergis/client/default.asp?design=../designs/masterFDG3.xml&w=780&h=540&eastern='+x+'&northern='+y+'&zoom=200', 'winmap', 0, 0, 780, 540, 0);
}
function openGMap_func(x,y){
	OpenBrWindow('http://www.journeyon.co.uk/newsite/gmapview.asp?w=780&h=540&eastern='+x+'&northern='+y+'&zoom=17', 'winmap', 0, 0, 780, 540, 0);
}


function openMapSelPoint_func(origin){
	OpenBrWindow('http://www.journeyon.co.uk/hypergis/client/default.asp?design=../designs/masterFDG3_selpoint.xml&w=780&h=540&eastern=531300&northern=104012&zoom=20&origin='+origin, 'winmap', 0, 0, 780, 540, 0);
}
function openGMapSelPoint_func(origin){
	OpenBrWindow('http://www.journeyon.co.uk/gmapselpt.asp?w=780&h=540&eastern=531300&northern=104012&zoom=15&origin='+origin, 'winmap', 0, 0, 780, 540, 0);
}

function openMapShowRoute_func(x,y){
	OpenBrWindow('http://www.journeyon.co.uk/hypergis/client/default.asp?design=../designs/masterFDG3_routedisplay.xml&w=780&h=540&eastern='+x+'&northern='+y+'&zoom=200', 'winmap', 0, 0, 780, 540, 0);
}

function openMapShowRoutePublic_func(x,y){
	OpenBrWindow('http://www.journeyon.co.uk/hypergis/client/default.asp?design=../designs/masterFDG3_routedisplay_public.xml&w=780&h=540&eastern='+x+'&northern='+y+'&zoom=200', 'winmap', 0, 0, 780, 540, 0);
}

function openMapCycleRacks_func(x,y){
	OpenBrWindow('http://www.journeyon.co.uk/hypergis/client/default.asp?design=../designs/masterFDG3_cycleracks.xml&w=780&h=540&eastern='+x+'&northern='+y+'&zoom=30', 'winmap', 0, 0, 780, 540, 0);
}

function radIndex_func(radGroup){
	rL = radGroup.length;
	radChked=0;
	for(a=0;a<rL;a++){
		if (radGroup[a].checked){
			radIndex=a;
		}
	}
	return radIndex;
}

// **** ADMIN FUNCTIONS ****

function adminGoto_func(url){
	if (url != ""){
		location.href=url;
	}
}

function gotourl_func(url){
	if (url != ""){
		location.href=url;
	}
}






function IEPngFix_func(){
    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5  && Number(rslt[1]) <= 6.0);
    if (itsAllGood){
	    for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--){
		if (itsAllGood && img.src.match(/\.png$/i) != null){
		    var src = img.src;
		    img.style.width = img.width + "px";
		    img.style.height = img.height + "px";
		    img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
		    img.src = "images/x.gif";
		}
		if (img.parentNode.attributes[0].value.indexOf("links_") != -1){
			img.style.visibility = "visible";
		}
	    }
    }
}

function enterKeySearch_func(e, urlORfrm){
	var characterCode
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	} else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
	if (characterCode == 13){
		if (urlORfrm.name){
			urlORfrm.submit();
		} else {
			gotourl_func(urlORfrm);
		}
	}
}

function replaceTxt_func(txtStr,replaceSelStr,replaceWithTxt){
	return txtStr.split(replaceSelStr).join(replaceWithTxt);
}

function updateMapControls(){ // this is only a hack
	var images = map.getContainer().getElementsByTagName("img");
	var imgUpdates=0;
	for(var i=0; i<images.length; i++){		
		if (images[i].src.indexOf("mapcontrols3d") != -1){
			imgUpdates=1;
		}
		images[i].src = images[i].src.replace(/mapcontrols2/,"mapcontrols3d");
	}
	if (imgUpdates==0){
		setTimeout(updateMapControls, 500);
	}
	//window.status += imgUpdates+", ";
}

function inputIsPostcode(postcodetxt){
	var isFullPostcode = true;
	if (postcodetxt.length >=5 && postcodetxt.length <= 9 && postcodetxt.substr(0,2).toUpperCase() == "BN"){ // This last part of if statements refers to Brighton postcodes only
		if (postcodetxt.indexOf(" ") == -1){
			postcodetxt = postcodetxt.substr(0,postcodetxt.length-3)+" "+postcodetxt.substr(postcodetxt.length-3,3);
		}
		if (!isNaN(postcodetxt.substr(0,1))){
			isFullPostcode = false;
		}
		if (postcodetxt.indexOf(" ") == -1){
			postcodetxt = postcodetxt.substr(0,postcodetxt.length-3)+" "+postcodetxt.substr(postcodetxt.length-3,3);
		}
		postcode2ndPart = postcodetxt.substr(postcodetxt.length-3,3);
		if (isNaN(postcode2ndPart.substr(0,1))){ // first number of second part of postcode has to be a number
			isFullPostcode = false;
		}
		if (!isNaN(postcode2ndPart.substr(1,1)) || !isNaN(postcode2ndPart.substr(2,1))){  // second and third numbers of second part of postcode has to be a letters
			isFullPostcode = false;
		}
	} else {
		isFullPostcode = false;
	}
	return isFullPostcode;
}

function addPostcodeSpace(postcodetxt){	
	if (postcodetxt.indexOf(" ") == -1){		
		postcodetxt = postcodetxt.substr(0,postcodetxt.length-3)+" "+postcodetxt.substr(postcodetxt.length-3,3);
	}
	return postcodetxt;
}


function rmoveWhite_space(str){
     return (str).replace(/^\s*|\s*$/g,'');
}

function getUrlFileName(){
	var pathArr = location.pathname.split("/");
	return pathArr[pathArr.length-1];
}