/*----------------------------------------------------------
  Global function and constants

  
  
  @package NTP 
  @author Arnd Beyer, arnd.beyer@wmdata.fi
  @copyright WM-data Novo 2006
  @version $Id: functions.php,v 1.0 2005/07/26 12:44:42 arnd Exp $
  ---------------------------------------------------------*/
var userSettings = new UserSettings();
var userLocations = new UserLocations("userLocations");
var messages = new Messages("messages");
var ttLineSelect = new LineSelect("ttLineSelect");
if (!isIE) window.captureEvents(Event.CLICK);



/* Move divs in Windows style */
var wm_mousex = 0;
var wm_mousey = 0;
var wm_grabx = 0;
var wm_graby = 0;
var wm_orix = 0;
var wm_oriy = 0;
var wm_elex = 0;
var wm_eley = 0;
var wm_algor = '';

var wm_dragobj = null;

function falsefunc() { return false; } // used to block cascading events

function init()
{
  document.onmousemove = updateWindowMove; // update(event) implied on NS, update(null) implied on IE
  updateWindowMove();
}

function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      wm_mousex = e.pageX;
      wm_mousey = e.pageY;
      wm_algor = '[e.pageX]';
      if (e.clientX || e.clientY) wm_algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      wm_mousex = e.clientX + document.body.scrollLeft;
      wm_mousey = e.clientY + document.body.scrollTop;
      wm_algor = '[e.clientX]';
      if (e.pageX || e.pageY) wm_algor += ' [e.pageX] '
    }  
  }
}

function updateWindowMove(e)
{
  getMouseXY(e); // NS is passing (event), while IE is passing (null)
}

function grabWindowMove(objectID, e)
{
  updateWindowMove(e);
  document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
  wm_dragobj = attach(objectID);
  wm_dragobj.style.zIndex = 10; // move it to the top
  document.onmousemove = dragWindowMove;
  document.onmouseup = dropWindowMove;
  wm_grabx = wm_mousex;
  wm_graby = wm_mousey;
  wm_elex = wm_orix = wm_dragobj.offsetLeft;
  wm_eley = wm_oriy = wm_dragobj.offsetTop;
 
}

function dragWindowMove(e) // parameter passing is important for NS family 
{
  if (wm_dragobj)
  {
    wm_elex = wm_orix + (wm_mousex - wm_grabx);
    wm_eley = wm_oriy + (wm_mousey - wm_graby);
    wm_dragobj.style.left = (wm_elex).toString(10) + 'px';
    wm_dragobj.style.top  = (wm_eley).toString(10) + 'px';
  }
  updateWindowMove(e);
  return false; // in IE this prevents cascading of events, thus text selection is disabled
}

function dropWindowMove()
{
  if (wm_dragobj)
  {
    wm_dragobj.style.zIndex = 0;
    wm_dragobj = null;
  }
  updateWindowMove();
  document.onmousemove = updateWindowMove;
  document.onmouseup = null;
  document.onmousedown = null;   // re-enables text selection on NS
}


function mapFormSubmit(event) {
	var posx = 0;
	var posy = 0;
	var x = 0;
	var y = 0;
	
	//var names  ="";
	var e = event;
	if (!e) {
	    var e = window.event;
	}

  
    if(isIE){    
	    document.map_print_trip.map_submit_x.value = e.offsetX;
	    document.map_print_trip.map_submit_y.value = e.offsetY;
    }else{
		var search = attach("map_submit");
		if (search.offsetParent){
			while (search.offsetParent)	{
				x += search.offsetLeft;
				y += search.offsetTop;
				search = search.offsetParent;
			}
		} else if (search.x) {
			x += search.x;
			y += search.y;
	    }
		if(document.body.scrollTop > 0 || document.body.scrollLeft >0 ) {
		    posx = e.clientX + document.body.scrollLeft;
		    posy = e.clientY + document.body.scrollTop;
	    }else{
	    	posx = e.clientX + document.documentElement['scrollLeft'];
	    	posy = e.clientY + document.documentElement['scrollTop'];
	    }
	    
	    document.map_print_trip.map_submit_x.value = posx-x;
	    document.map_print_trip.map_submit_y.value = posy-y;
	}
 	document.map_print_trip.submit();
}


/* be sure setfocus_fields array is set by php! */
function setfocus () {
   //first_call =0;
   var focus_set = false;
   var i=0;
   var hasResult = attach("trip_result_detail");
   if(hasResult == null) { // only focus if no results
	   while(!focus_set && i < setfocus_fields.length) {
		   var feld = document.getElementsByName(setfocus_fields[i])[0];
		   if(feld != null) {
		   	 if(feld.type != "hidden" ) { 
		   	   try {
				   locationInput = setfocus_fields[i];
			   	   feld.focus();
			   	   focus_set =true;
			   }catch (e) {
			   }
		     }
		   }
	       i++;
	   }
   }
     
}

/* be sure saved_location_fields array is set by php! */
function useSavedLocation(location) {
        var field;
        var nextfield;
        
		if (locationInput == saved_location_fields[0]) {
		    field = document.getElementsByName(locationInput)[0];
		    nextfield = document.getElementsByName(saved_location_fields[1])[0];
		} else if(locationInput == saved_location_fields[1]) {
		    field = document.getElementsByName(locationInput)[0];
		    nextfield = document.getElementsByName(saved_location_fields[2])[0];
		} else if(locationInput == saved_location_fields[2] ) {
		    field = document.getElementsByName(locationInput)[0];
		    nextfield = document.getElementsByName(saved_location_fields[3])[0];
		} else if(locationInput == saved_location_fields[3] ) {
		    field = document.getElementsByName(locationInput)[0];
		    nextfield = null;
		} else {
			field = null;
			nextfield = null;
		}
 
		if(field != null ){
 	   	   if(field.type != "hidden") { 
			  field.value = location;
			  if(nextfield != null) {
			   	 if(nextfield.type != "hidden") { 
					   	   try{
    					   	   nextfield.focus();
    					  } catch (error) {
    					       //in case the field is in the hidden advanced search
    					  }
   	 }
			  }
		   }
		} else {
		   focus_set = false;
		   i=0;
		   while(!focus_set && i < saved_location_fields.length) {
			   field = document.getElementsByName(saved_location_fields[i])[0];
			   if(field != null) {
			   	 if(field.type != "hidden") { 
				   locationInput = saved_location_fields[i];
				   field.value = location;
			   nextfield = document.getElementsByName(saved_location_fields[i+1])[0];
				   if(nextfield != null) {
				   	 if(nextfield.type != "hidden") { 
    					   	   try{
        					   	   nextfield.focus();
        					  } catch (error) {
        					       //in case the field is in the hidden advanced search
        					  }
   	 }
				   }
			   	   focus_set =true;
			     }
			   }
  		       i++;
		   }
		}
 }


function setlocationInput(fieldname) {
    locationInput=fieldname;
}




function checkFeedbackEmail() {
   var result;
   result = false;
   mail = document.getElementsByName("email")[0].value;
   if (mail == "") {
	  result = true;
   } else {
     if(checkEmailFormat(mail)) {
		result = true;
	 } else {
   		alert(mailErrorMsg);
     }
   }
   return result;
}



function checkEmailFormat(mail) {
    var result;
    result = false;

    regexp = /([0-9a-z\-\.\_\+\^])+@([0-9a-z\-\.\_\+\^])+/i;
    match = regexp.exec(mail);
    if(match) {
	  result = (match[0] == mail);
    }
    return result;
}


  
//Show div layer at mouse click position
  
function show_position(sID, event, offset_x, offset_y) {
    var posx = 0;
    var posy = 0;
    var e = event;
    
    if (!e) {
      var e = window.event;
    }
    if (e.pageX || e.pageY) {
      posx = e.pageX + offset_x;
      posy = e.pageY + offset_y;
    } else if (e.clientX || e.clientY) {
      posx = e.clientX + document.body.scrollLeft + offset_x;
      posy = e.clientY + document.body.scrollTop + offset_y;
    } else {
      posx = e.pageX + offset_x;
      posy = e.pageY + offset_y;
    }
    temp_Obj = attach(sID);
    temp_Obj.style.position   = "absolute";
    temp_Obj.style.left       = posx + "px";
    temp_Obj.style.top        = posy + "px";
    temp_Obj.style.display    = "block";
    temp_Obj.style.visibility = "visible";
    temp_Obj.style.zIndex     = 10;

}

//Toggle div layer visibility
function toggleVisPos(sID, iVis, triggerID, offset_x, offset_y) {
    var obj = attach(sID);
    var search = attach(triggerID);
    
	var x = 0;
	var y = 0;
	if (search.offsetParent)
	{
		while (search.offsetParent)
		{
			x += search.offsetLeft;
			y += search.offsetTop;
			search = search.offsetParent;
		}
	} else if (search.x) {
		x += search.x;
		y += search.y;
    }
    
    obj.style.left  = x +offset_x+ "px";
    obj.style.top   = y +offset_y + "px";
    if(document.layers) { //NN4+
      obj.display    = iVis ? "block" : "none";
      obj.visibility = iVis ? "show" : "hide";
      obj.zIndex     = iVis ? "10" : "0";
    } else { //Others
      obj.style.display    = iVis ? "block" : "none" ;
      obj.style.visibility = iVis ? "visible" : "hidden";
      obj.style.zIndex     = iVis ? "10" : "0";
    }
}

//Toggle div layer visibility
function toggleVisMouse(sID, iVis, event, offset_x, offset_y) {
    var obj = attach(sID);
     
    var posx = 0;
	var posy = 0;
    var styleX = 0;
    var styleY = 0;
    
    var e = event;
	if (!e) {
	    var e = window.event;
	}
    var infoBlock = attach("info_block");
    styleX = infoBlock.offsetLeft;
    styleY = infoBlock.offsetTop;
	if (e.pageX || e.pageY) {
	    posx = e.pageX;
	    posy = e.pageY;
	} else if (e.clientX || e.clientY) {
        var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body
	    posx = e.clientX + iebody.scrollLeft;
	    posy = e.clientY + iebody.scrollTop;
    } else {
        posx = e.pageX;
        posy = e.pageY;
    }
    obj.style.left  = (posx + offset_x - styleX) + "px";
    obj.style.top   = (posy + offset_y - styleY) + "px";

    if(document.layers) { //NN4+
      obj.display    = iVis ? "block" : "none";
      obj.visibility = iVis ? "show" : "hide";
      obj.zIndex     = iVis ? "10" : "0";
    } else { //Others
      obj.style.display    = iVis ? "block" : "none" ;
      obj.style.visibility = iVis ? "visible" : "hidden";
      obj.style.zIndex     = iVis ? "10" : "0";
    }
}

//Toggle div layer visibility
function toggleVis(sID, iVis) {
    var obj = attach(sID);

    if(obj != null){
		      if(document.layers) { //NN4+
		          obj.display    = iVis ? "block" : "none";
		          obj.visibility = iVis ? "show" : "hide";
		          obj.zIndex     = iVis ? "10" : "0";
		      } else { //Others
		          obj.style.display    = iVis ? "block" : "none" ;
		          obj.style.visibility = iVis ? "visible" : "hidden";
		          obj.style.zIndex     = iVis ? "10" : "0";
		      }
    }
}

//Toggle div layer visibility
function toggleVisBlock(elementIdBase, iVis) {
   	var found = true;
    for(var n=0; found; n++){
		var element = attach(elementIdBase + '_' + n);
		if(element == null){
			found = false;
		}else{
		    if(document.layers) { //NN4+
		        element.display    = iVis ? "" : "none";
		        element.visibility = iVis ? "show" : "hide";
		    } else { //Others
		        element.style.display    = iVis ? "" : "none" ;
		        element.style.visibility = iVis ? "visible" : "hidden";
		    }
		}
    }
}

//Hide and Show advanced search
function toggleAdvancedSearchVisibility(cookieName, iVis, secondSubmit) {

    var saveOpt = attach("advanced_search_options_save");
    var linkOn = attach("advanced_search_on");
    var linkOff = attach("advanced_search_off");
    var divAdv = attach("advanced_search_options");
    if(secondSubmit) {
	    var linkOn2 = attach("advanced_search2_on");
	    var linkOff2 = attach("advanced_search2_off");
    }
    
    if(document.layers) { //NN4+
      divAdv.display    = iVis ? "block" : "none";
      divAdv.visibility = iVis ? "show" : "hide";
      linkOff.display    = iVis ? "inline" : "none";
      linkOff.visibility = iVis ? "show" : "hide";
      linkOn.display    = iVis ? "none" : "inline";
      linkOn.visibility = iVis ? "hide" : "show";
      saveOpt.display    = iVis ? "inline" : "none";
      saveOpt.visibility = iVis ? "show" : "hide";
	  if(secondSubmit) {
	      linkOff2.display    = iVis ? "inline" : "none";
	      linkOff2.visibility = iVis ? "show" : "hide";
	      linkOn2.display    = iVis ? "none" : "inline";
	      linkOn2.visibility = iVis ? "hide" : "show";
	  }
    } else { //Others
      divAdv.style.display    = iVis ? "block" : "none" ;
      divAdv.style.visibility = iVis ? "visible" : "hidden";
      linkOff.style.display    = iVis ? "inline" : "none" ;
      linkOff.style.visibility = iVis ? "visible" : "hidden";
      linkOn.style.display    = iVis ? "none" : "inline";
      linkOn.style.visibility = iVis ? "hidden" : "visible";
      saveOpt.style.display    = iVis ? "inline" : "none" ;
      saveOpt.style.visibility = iVis ? "visible" : "hidden";
	  if(secondSubmit) {
	      linkOff2.style.display    = iVis ? "inline" : "none" ;
	      linkOff2.style.visibility = iVis ? "visible" : "hidden";
	      linkOn2.style.display    = iVis ? "none" : "inline";
	      linkOn2.style.visibility = iVis ? "hidden" : "visible";
	  }
    }
    document.cookie=cookieName+"="+iVis+"; path=/";
}



//enable, disable in/exclude lines
function as_ix_disable(disable){
	var select = attach("as_ix_arr0");
	var i =0;
	while (select) {
	    select.disabled = disable;
	    i++;
	    select = attach("as_ix_arr"+i);
	}
	//document.search.as_ix_arr[].disabled = disable;
}


//add new select box for include/exclude lines
function as_ix_newselect(caller) {
  var callingselect = caller.id.substring(9,11);
  if(callingselect == as_ix_count -1 && as_ix_count < as_ix_max) {
	  var td = attach("as_ix_selectboxen");
	  var options = attach("as_ix_arr0");
	  if( td && options) {
	      var selectObj = document.createElement('select');
		  for(var i = 0; i < options.childNodes.length; i++) {
	      	  selectObj.appendChild(options.childNodes[i].cloneNode(true));
	      }
	      selectObj.value='';
	      selectObj.name = "as_ix_arr[]";
	      selectObj.id ="as_ix_arr"+as_ix_count;
	      selectObj.className = "search_exclude_line";
	      selectObj.tabIndex = 210+as_ix_count;
		  selectObj.onchange = function(){
                   as_ix_newselect(this);
                  };
	      td.appendChild(selectObj);
	      td.appendChild(document.createElement('br'));
		  as_ix_count++;
	  }
  }
}

//Hide and Show Tripplanner results
function toggleTripResultVisibility(cookieName, iShow) {
    var list = new Array();
    list[0] = attach("trip_result_summary");
    list[1] = attach("trip_result_detail");
    var tab = new Array();
    tab[0] = attach("trip_result_summary_tab");
    tab[1] = attach("trip_result_detail_tab");
    for( i=0; i <2; i++) {
		show = (iShow == i);
	    if(document.layers) { //NN4+
	      list[i].display    = show ? "block" : "none";
	      list[i].visibility = show ? "show" : "hide";
	      list[i].zIndex     = show ? "10" : "0";
	    } else { //Others
	      list[i].style.display    = show ? "block" : "none";
	      list[i].style.visibility = show ? "visible" : "hidden";
	      list[i].style.zIndex     = show ? "10" : "0";
	    }
		tab[i].className = show ? "active" : "";
	}
    document.cookie=cookieName+"="+iShow+"; path=/";
}


//Hides area and hide link and shows show link
function divAreaHideShow(areaID, iVis) {
    var div = attach(areaID);
    var a_hide = attach(areaID + "_hide");
    var a_show = attach(areaID + "_show");

    div.style.display = iVis ? "block" : "none" ;
 	a_hide.style.display = iVis ? "inline" : "none" ;
 	a_show.style.display = iVis ? "none" : "inline" ;
}

function toggleVisIndex(iShow) {
    var list = new Array();
    list[0] = attach("list_street");
    list[1] = attach("list_stop");
    list[2] = attach("list_poi");
    var tab = new Array();
    tab[0] = attach("list_index_tab_street");
    tab[1] = attach("list_index_tab_stop");
    tab[2] = attach("list_index_tab_poi");
    for( i=0; i <3; i++) {
		show = (iShow == i);
	    if(document.layers) { //NN4+
	      list[i].display    = show ? "block" : "none";
	      list[i].visibility = show ? "show" : "hide";
	      list[i].zIndex     = show ? "10" : "0";
	    } else { //Others
	      list[i].style.display    = show ? "block" : "none";
	      list[i].style.visibility = show ? "visible" : "hidden";
	      list[i].style.zIndex     = show ? "10" : "0";
	    }
		tab[i].className = show ? "active" : "";
	}
}

  
function set_date(yr,mth,dy) {
    var aryDay = document.search.day.options;
    var aryMth = document.search.month.options;
    var aryYr  = document.search.year.options;
    
    for (i=0;i<document.search.day.length;i++) {
      if (aryDay[i].value == dy)
        document.search.day.selectedIndex = i;
    }
    
    for (i=0;i<document.search.month.length;i++) {
      if (aryMth[i].value == mth)
        document.search.month.selectedIndex = i;
    }
    
    for (i=0;i<document.search.year.length;i++) {
      if (aryYr[i].value == yr)
        document.search.year.selectedIndex = i;
    }
}
  

