// JavaScript Document

function initNav(){
	var imgObj;	
	for(i=0;i<navImgHoverArr.length;i++){
		imgObj = new Image();
		imgObj.src = navImgHoverArr[i];
	}
}

function navOver(navImgID, srcOver){
	document.getElementById(navImgID).src=srcOver;
}
function navOut(navImgID, srcOut){
	document.getElementById(navImgID).src=srcOut;	
}

function txtboxInit(txtboxObj, initVal){
	if (txtboxObj.value == initVal){
		txtboxObj.value = '';		
	}
	if (txtboxObj.className.substr(txtboxObj.className.length-4,4) == "init"){
		//txtboxObj.className = txtboxObj.className.substr(0,txtboxObj.className.length-4);// Bug in IE
		txtboxObj.style.color='#003366';
	} else if (txtboxObj.className.substr(txtboxObj.className.length-4,4) == "warn"){
		txtboxObj.style.color='#003366';
	}
}

function setStyle_func(obj, stylevar, styleval){
	if (navigator.userAgent.toLowerCase().indexOf("msie") > -1){
		obj.style[stylevar]=styleval;
	} else {
		obj.style[stylevar]=styleval;
	}
}

function search_func(crit){
	location.href="search.asp?s="+crit;
}

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 {
			location.href=urlORfrm;
		}
	}
}

function replaceTxt_func(txtStr,replaceSelStr,replaceWithTxt){
	return txtStr.split(replaceSelStr).join(replaceWithTxt);
}

// ***** JOURNEY PLANNER FUNCTIONS ******

function jpStartAgain(){	
	location.href="jplanner1.asp";
}

function gotoJPlanner(fromtxt,totxt){
	frm = document.getElementById('minijplanner');
	frm.addrfrom.value = fromtxt;
	frm.addrto.value = totxt;
	frm.submit();
}

function jpTimesEarlier(){
	showloader_func();
	location.href="jplanner3.asp?planjourney=1&earlier=1"
}

function jpTimesLater(){
	showloader_func();
	location.href="jplanner3.asp?planjourney=1&later=1"
}

function showOnMap(mode,markerID,fromOSX,fromOSY,toOSX,toOSY,txt){
	var ptFrom = OS2LatLonWGS84_func(fromOSX,fromOSY);
	ptFrom = new GLatLng(ptFrom.lat, ptFrom.lng);
	var ptTo = OS2LatLonWGS84_func(toOSX,toOSY);
	ptTo = new GLatLng(ptTo.lat, ptTo.lng);
	if (mode == "walk"){
		imageUrl = "images/mapicon_walk.png";
		lineColor = "#817bb9";
	} else if (mode == "rail"){
		imageUrl = "images/mapicon_rail.png";
		lineColor = "#67bc77";
	} else if (mode == "bus" || mode == "coach"){
		imageUrl = "images/mapicon_bus.png";
		lineColor = "#f17278";
	}
	
	map.addOverlay(new GPolyline([ptFrom, ptTo], lineColor, 6, 0.6, {geodesic:true}));
	
	// Create our marker icon
	var icon = new GIcon();
	icon.image = imageUrl
	icon.shadow = "images/mapicon_sqshad.png";
	icon.iconSize = new GSize(45, 47);
	icon.shadowSize = new GSize(60, 41);
	icon.iconAnchor = new GPoint(0, 47);
	//icon.infoWindowAnchor = new GPoint(5, 1);	
	
	
	addJMarker2Map(ptFrom,16,icon,markerID,imageUrl,txt);
}


function showloader_func(){
	var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	scroll(0, 0);
	loaderObj = document.getElementById('loader');
	//loaderObj.innerHTML = "<img src='images/aniloader.gif' alt='Loading...' /> Loading..."
	loaderObj.style.visibility='visible';
	loaderObj.style.position='fixed';
	loaderObj.style.width=document.body.clientWidth+'px';
	loaderObj.style.height=windowHeight+'px';

	loaderObj = document.getElementById('loaderbg');
	loaderObj.style.width=document.body.clientWidth+'px';
	loaderObj.style.height=windowHeight+'px';
	loaderObj.style.visibility='visible';
	loaderObj.style.position='fixed';
	//writehtml_func();
}

function inputIsPostcode(postcodetxt){
	var isFullPostcode = true;
	if (postcodetxt.length >=5 && postcodetxt.length <= 9){
		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 gotoMarker_func(pt){
	map.setCenter(pt, 16);
}




function sendmlHttpRequest(url,callback,postdata) {	
	var xmlhttp=false;
	try  {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); /* for IE < 5 */
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}

	/* mozilla & opera */
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest();
	}

	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			callback(xmlhttp.responseText);
			//alert(xmlhttp.responseText);
		}
	}
	xmlhttp.send(postdata); /* are sending null because we dont have any data to post */
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}
