/*
 * Production javascript
 *
 * Copyright (c) 2008 Vidteq India Pvt Ltd
 * and GPL (GPL-LICENSE.txt) licenses.MSI
 *

 * $Date: 2010/07/07 09:33:31 $
 * $Date: 2010/07/07 09:33:31 $

 * $Rev: $
 */

populateMarkers = function (srf,from) {
  if (!srf.showSets[srf.curShowSet]) return from;
  for (var i=srf.showSets[srf.curShowSet].from-1;i<srf.showSets[srf.curShowSet].to;i++) {
    populateMarkIconContent(srf.results[i],from++);
  }
  return from;
}

populateMarkIconContent = function (entity,iconNum) {
  if (typeof(iconNum) == 'undefined') return;
  entity.lpIndex = iconNum;
  entity.markIcon = [];
  entity.markIcon.iconNum = iconNum;
  entity.markIcon.mapUrl=imagePathsObj.locaMarkers[iconNum].map;
  entity.markIcon.fvtUrl=imagePathsObj.locaMarkers[iconNum].div;
  entity.markIcon.mapStyle="border:0px solid black;"    
  entity.markIcon.fvtStyle="border:0px solid black;cursor:pointer;"    
  entity.markIcon.size=32;  
  entity.markIcon.title='Click to get information about this place';
}

populateIconContent = function (entity,iconNum) {
  //if (typeof(entity.icon)=='undefined') entity.icon = {}; 
  if (typeof(entity.icon)=='undefined') return;
  if(typeof(entity.icon.name)!='undefined') {
    //entity.icon.mapUrl=imagePathsObj.logosBase+entity.icon.name;
    //entity.icon.fvtUrl=imagePathsObj.logosBase+entity.icon.name;
    entity.icon.mapUrl=imageLogosLoc+entity.icon.name;
    entity.icon.fvtUrl=imageLogosLoc+entity.icon.name;
    // TBD how to show the number on top of defined icon
    entity.icon.mapStyle="border:0px solid black;"    
    entity.icon.fvtStyle="border:0px solid black;cursor:pointer;"    
    if(entity.icon.size) entity.icon.size=entity.icon.size;
     // TBD wxh
    else entity.icon.size=32;  
  } else if (typeof(iconNum)!='undefined') {   
    entity.icon.mapUrl=imagePathsObj.locaMarkers[iconNum].map;
    entity.icon.fvtUrl=imagePathsObj.locaMarkers[iconNum].div;
    entity.icon.mapStyle="border:0px solid black;"    
    entity.icon.fvtStyle="border:0px solid black;cursor:pointer;"    
    entity.icon.size=32;  
  } else {
    //TBD
    //entity.icon.name='start.png';
    //entity.icon.mapUrl='images/start.gif';
    //entity.icon.size=32;  
  }
  if (entity.type && entity.type == 'center') entity.icon.title=entity.address.name;
  else entity.icon.title='Click to get information about this place';
}

getEntityHtml = function(fvtOrMap,entity) {
  var fvtRow,tmpString;
  //var data = { entity : entity, entityName : 'blah' };
  //var tempFvtRow = TrimPath.processDOMTemplate("fvtRow", data);
  var mapRow,mapRowTemp;
  var popupHeight=50;
  fvtRow=mapRowTemp="<tr style='background-color:inherit;'>";
  var classPNG=(MSIE6==true)?"class='pngfixclass'":"''";	
  // TBD overlay 
  var icon;
  if (entity.icon && fvtOrMap == 'map') { icon = entity.icon; } 
  else { icon = entity.markIcon; }
  if ((!entity.type || entity.type != 'center') && fvtOrMap == 'map') {  // TBD
    if (routeEndsObj.isEqual('start',entity))  icon = mboxObj.startIcon;
    if (routeEndsObj.isEqual('end',entity))  icon = mboxObj.endIcon;
  }
  fvtRow+="<td width=5><img id='fvtIcon"+entity.lpIndex+"' "+classPNG+" style='"+icon.fvtStyle+"' src="+icon.fvtUrl+" href='javascript:void(0);' onclick=";
  var crossPopupText = "'javascript:mboxObj.callPopupLocation.apply(mboxObj,["+entity.lpIndex+"])'";
  if (entity.type && entity.type == 'center') crossPopupText = "'javascript:mboxObj.popoutCenterPlace()'";
  fvtRow += crossPopupText;
  //if (entity.type && entity.type == 'center') fvtRow += "'javascript:mboxObj.popoutCenterPlace()'";
  //else fvtRow += "'javascript:mboxObj.callPopupLocation.apply(mboxObj,["+entity.lpIndex+"])'";
  fvtRow += "/></td><td>";
  mapRowTemp+="<td style='background-color:white;vertical-align:middle;text-align:center;padding-left:10px;' width=5><img id='popupIcon' "+classPNG+" style='"+icon.mapStyle+"' src="+icon.mapUrl+" height=32 width=32 href='javascript:void(0);' /></td><td style='background-color:white'>";
  fvtRow+="<b class='b1f'></b><b class='b2f'></b><b class='b3f'></b><b class='b4f'></b>";
  var name = 'Unknown Name';
  if (typeof(entity.name)!='undefined') name = entity.name;
  if (typeof(entity.address)=='undefined') entity.address = {};
  if (typeof(entity.address.name)!='undefined') name = entity.address.name;
  //var tmpRow="<a class=profileFont onclick='javascript:mboxObj.callPopupLocation.apply(mboxObj,["+entity.lpIndex+"])'>"+ioAreaObj.boldify.apply(ioAreaObj,[entity.parentType,name])+"</a>";
  var tmpRow="<a class=profileFont onclick="+crossPopupText+">"+ioAreaObj.boldify.apply(ioAreaObj,[entity.parentType,name])+"</a>";
  if(entity.category) {
    popupHeight += 15;
    tmpRow+='<br class=\'profileFont\'>'; 
    tmpRow += "<a class=profileFont>Category: "+entity.category+"</a>";
  }
  var tmpRowMap = tmpRow;
  if(typeof(entity.image)!='undefined' && entity.image !=null && entity.image != '') {
  //tmpRow+="<a style='float:right' class=profileFont onclick='javascript:mboxObj.callPopupLocation.apply(mboxObj,["+entity.lpIndex+"])'><img src="+imagePathsObj.camera+" /></a>";
  tmpRow+="<a style='float:right' class=profileFont onclick="+crossPopupText+"><img src="+imagePathsObj.camera+" /></a>";
  }
  //var turnOn=true;
  //if(!isObjectEmpty(entity.address) && entity.address.addr1 && turnOn) {
  //  tmpRow+="<a onfocus='this.blur()' class='click' id='showMoreOrLess"+entity.lpIndex+"' style='text-decoration:underline;display:block' href=javascript:void(0) onclick='fvtObj.showFvtMoreOrLess("+entity.lpIndex+","+0+")'>More >><a>";
  //  //TBD index is not unique
  //  tmpRow+="<div id='showMoreDiv"+entity.lpIndex+"' style='display:none'>";
  //}		
  var propCounter=0;	
  var fieldContent = '';
  var fieldContentMap = '';
  for(var index in addressSubsArray) {
    for(var prop in entity.address) {
      if(prop==addressSubsArray[index].field && entity.address[prop]!=null) {
        if(prop=='addr1') { propCounter++;fieldContentMap+="<br/>"; }
        if(prop!='pin') {
          fieldContent+='<br class=\'profileFont\'>'; 
          fieldContentMap+='<br class=\'profileFont\'>'; 
        } else { 
          fieldContent+='&nbsp';
          fieldContentMap+='&nbsp';
	}	
        if(addressSubsArray[index].text) {
          fieldContent+="<a class='profileFont'>"+addressSubsArray[index].text+"</a>";
          fieldContentMap+="<a class='profileFont'>"+addressSubsArray[index].text+"</a>";
        }
        fieldContent+="<a class='profileFont'>"+entity.address[prop]+"</a>";
        fieldContentMap+="<a class='profileFont'>"+entity.address[prop]+"</a>";
        propCounter++;
      }
    }
  }
  if(entity.id) {
    fieldContent+='<br class=\'profileFont\'>'; 
    fieldContentMap+='<br class=\'profileFont\'>'; 
    fieldContent+="<a class='click' style=text-decoration:underline; target=_blank href=vs/place.php?id="+entity.id+"&name="+encodeURI(name)+">Details</a>";
    fieldContentMap+="<a class='click' style=text-decoration:underline; target=_blank href=vs/place.php?id="+entity.id+"&name="+encodeURI(name)+">Details</a>";
    propCounter++;
  }
  //if(!isObjectEmpty(entity.address) && entity.address.addr1 && turnOn) {
  tmpRowMap += fieldContentMap;
  if(!isObjectEmpty(entity.address) && propCounter > 1) {
    tmpRow+="<br/><a onfocus='this.blur()' class='basic' id='showMoreOrLess"+entity.lpIndex+"' href=javascript:void(0) onclick='fvtObj.showFvtMoreOrLess("+entity.lpIndex+","+0+")'>More >><a>";
    //TBD index is not unique
    tmpRow+="<div id='showMoreDiv"+entity.lpIndex+"' style='display:none'>";
    tmpRow+= fieldContent;
    tmpRow+='</div>';
  } else {
    tmpRow+= fieldContent;
  }
  //if(!isObjectEmpty(entity.address) && entity.address.addr1 && turnOn) {
  //  tmpRow+='</div>';
  //   turnOn=false;
  //}
  popupHeight+=propCounter*15;

  tmpString="<table cellspacing=0; cellpadding=0; width=100% style=background-color:white;><tr><td><div style='background-color:white;padding-left:10px'><a  onfocus='this.blur();'";
  mapRowTemp+=tmpString;
  fvtRow+=tmpString;

  tmpString="class='"+(typeof(ioAreaObj.embed)!='undefined'?'profileFont':'simple')+"'>"+tmpRow+"</a>"
  mapRowTemp+="class='"+(typeof(ioAreaObj.embed)!='undefined'?'profileFont':'simple')+"'>"+tmpRowMap+"</a>"
  fvtRow+=tmpString;  

  if(vidteq.debug && typeof(entity.relevance)!='undefined') {
    fvtRow+="<br class=\'profileFont\' /><a class='profileFont'> Relevance : "+entity.relevance+"</a>";

  //  mapRowTemp+="<br/><a> Relevance : "+entity.relevance+"</a>";
  }
  if(typeof(entity.distance)!='undefined') {
    tmpString="<br class=\'profileFont\' /><a class='profileFont'>At "+entity.distance+"</a>";  
    fvtRow+=tmpString;
    mapRowTemp+=tmpString;
    if(ioAreaObj.embed && ioAreaObj.embed.locateStores && entity.index==0) {
      //tmpString="<a class='profileFont' style='cursor:text;'><b class='highlight'><i>Nearest store to your location</i></b></a>";
      tmpString="<a class='profileFont' style='cursor:text;'><b class='highlight'><i>Nearest store to "+ioAreaObj.embed.locateStores.myLocStr+"</i></b></a>";
      fvtRow+=tmpString;
      mapRowTemp+=tmpString;
    }	
  }		
  fvtRow+="<div>";
  mapRowTemp+="<div>";

  var optionsDecider = function (onclickFunc,text) {
    var s=["<a onfocus='this.blur();' class=basic href='javascript:void(0)' onclick="];
    s.push(onclickFunc+"; >");
    s.push(text+"&nbsp</a>");
    s=s.join('');
    return s;
  }	  
   
  if((ioAreaObj.embed && ioAreaObj.embed.minimap)) {
    var a=optionsDecider("mboxObj.popoutTheMinimap()","Video based Driving Directions");
    fvtRow+=a;
    mapRowTemp+=a;
  } else if(entity.type=='center' || entity.type == 'rcm') { 
  } else if(ioAreaObj.embed && ioAreaObj.embed.locateStores && entity.parentType=='location') {
	var a=optionsDecider("ioAreaObj.locateNearByStoresByIndex("+entity.index+",'start')","Search Stores Around here");
    fvtRow+=a;
    mapRowTemp+=a;
  } else {
    tmpRow='';
    if( (!ioAreaObj.embed && entity.parentType != 'endAddress') ||
        entity.parentType == 'locateStores' || 
        (ioAreaObj.embed && ioAreaObj.embed.fix!='start')) {
	  if(ioAreaObj.embed && entity.parentType!='locateStores') {
          tmpRow="<a onfocus='this.blur();' class=click href='javascript:void(0)' onclick=ioAreaObj.dirFromTo('start',"+entity.index+",'"+entity.parentType+"');ioAreaObj.goVid();>Directions from&nbsp</a>";
	  var msgString="Directions From";
          if(typeof(accountDetails)!='undefined' && accountDetails.q == 'blocate') {
              msgString="Click here to view driving directions in Video";
          }
          var a=optionsDecider("ioAreaObj.dirFromTo('start',"+entity.index+",'"+entity.parentType+"');ioAreaObj.goVid();",msgString);
	  fvtRow+=a;
      mapRowTemp+=a;
	  } else {
	  var a=optionsDecider("ioAreaObj.dirFromTo('start',"+entity.index+",'"+entity.parentType+"');","Directions From");	
	  fvtRow+=a;
      mapRowTemp+=a;
	 }
    }
    if( (!ioAreaObj.embed && entity.parentType == 'location') ||
        entity.parentType == 'locateStores') {
      fvtRow+="&nbsp;<a onfocus='this.blur();' class=simple>|</a>&nbsp";
      mapRowTemp+="&nbsp;<a onfocus='this.blur();' class=simple>|</a>&nbsp";
    }
    if( (!ioAreaObj.embed && entity.parentType != 'startAddress') || 
        entity.parentType == 'locateStores' || 
        (ioAreaObj.embed && ioAreaObj.embed.fix!='end')) {
	if(ioAreaObj.embed && entity.parentType!='locateStores') {
	  tmpRow="<a onfocus='this.blur();' class=click href='javascript:void(0)' onclick=ioAreaObj.dirFromTo('end',"+entity.index+",'"+entity.parentType+"');ioAreaObj.goVid();>Directions to&nbsp</a>";
	  var msgString="Directions To";
          if(typeof(accountDetails)!='undefined' && accountDetails.q == 'blocate') {
              msgString="Click here to view driving directions in Video";
          }
          var a=optionsDecider("ioAreaObj.dirFromTo('end',"+entity.index+",'"+entity.parentType+"');ioAreaObj.goVid();",msgString);
	  fvtRow+=a;
      mapRowTemp+=a;
	  }
	  else {
	  var a=optionsDecider("ioAreaObj.dirFromTo('end',"+entity.index+",'"+entity.parentType+"');","Directions To");	
	  fvtRow+=a;
      mapRowTemp+=a;
	 }			
	} 
  }
  fvtRow+="</div></div></td>";
  mapRowTemp+="</div></div></td>";
  if(vidteq.debug && typeof(entity.relevance)!='undefined') {
    var unRelHeight=((1-entity.relevance)*20)+"px";
    var relHeight=(entity.relevance*20)+"px";
	tmpString="<!--td style='float:right;vertical-align:middle;'><a style='width:10px;' class='simple'>"+entity.relevance*10+"</a></td!-->";
    fvtRow+=tmpString;
    mapRowTemp+=tmpString;
	tmpString="<td style='float:right;border:2px solid black;vertical-align:middle;'><div style=background-color:white;width:10px;height:"+unRelHeight+"></div><div style=background-color:green;width:10px;height:"+relHeight+"></div></td>";
    fvtRow+=tmpString;
    mapRowTemp+=tmpString;
  }
  fvtRow+="<td width=10></td>";
  fvtRow+="</tr></table><b class='b4f'></b><b class='b3f'></b><b class='b2f'></b><b class='b1f'></b>";
  fvtRow+="</td></tr>";
  var imgTT="";
  mapRowTemp+="<td width=10></td>";
  mapRowTemp+="</tr></table>";
  mapRowTemp+="</td></tr>";
  var popupWidth=320;  
  if(typeof(entity.image)!='undefined' && 
     entity.image !=null && entity.image != '') {
    imgTT="<img style='border:2px solid white;height:240px;width:320px;' src="+cloneImageUrl+entity.image+"  />";
    popupHeight+=240;popupWidth=330;    
  }
  mapRow="<div oncontextmenu='return false;' style='text-align:center;vertical-align:center'><div style='position:absolute;right:2px;top:2px;height:20px;width:20px;'><img style='cursor:pointer' src='"+imagePathsObj.close+"' onclick='javascript:mboxObj.removeOpenPopup.apply(mboxObj,[])' href='javascript:void(0);'/></div>";
  if(typeof(entity.image)!='undefined' && 
     entity.image !=null && entity.image != '') { 
    mapRow+="<div>"+imgTT+"</div>"; 
  } else { mapRow+="<div></div>"; }
  mapRow+="<div style='text-align:left;background-color:'transparent';><table width=100% cellspacing=0; cellpadding=0; style='padding-top:5px;padding-left:5px;padding-right:5px;'>"+mapRowTemp+"</table></div></div>";
  //var rg=new RegExp(name,"g");
  // TBD I dont know what is this
  //tt=tt.replace(rg,entity.address);
  //tt=tt.replace(rmReg,locations.address[i]);
  if (fvtOrMap == 'fvt') return fvtRow;
  return {
    popupWidth : popupWidth,
    popupHeight : popupHeight,
    contentHTML : mapRow
  };
}

//populateShowSets = function(srf,perSet) {
//  srf.showSets = [];
//  srf.curShowSet = 0;
//  if (srf.results.length == 0) return; 
//  for (var i=1;i<=srf.results.length;i+=perSet) {
//    srf.showSets.push({'from':i,
//      'to' : (i+perSet-1<srf.results.length?(i+perSet-1):srf.results.length)});
//  } 
//}

reviseIcons = function(srf) {
  if (srf.results.length == 0) return; 
  for (var i in srf.results) { populateIconContent(srf.results[i]); }
}

reviseStartEndInSrfResponse = function(srfResponse) {
  if ( srfResponse.routeFailType == 'sf' ||
       srfResponse.routeFailType == 'sm' ||
       srfResponse.routeFailType == 'ss' ) {
    var entity = srfResponse.srf[0].results[0];
    if (! entity.address.name) {
      entity.address.name = ioAreaObj.startEntity.address.name;
    }  // TBD assignment should be done according to source of startEntity
    // if startEntity is from earlier srf, use that, other wise use incoming
    // TBD center need to be fixed back
    if (ioAreaObj.embed && ioAreaObj.embed.fix=='start') {
      srfResponse.srf[0].results[0] = ioAreaObj.embed.place;
    }
    if (entity.type != 'center') ioAreaObj.dirFromTo('start',entity.index,entity.parentType);
  }
  if ( srfResponse.routeFailType == 'fs' ||
       srfResponse.routeFailType == 'ms' ||
       srfResponse.routeFailType == 'ss' ) {
    var entity = srfResponse.srf[1].results[0];
    if (! entity.address.name) {
      entity.address.name = ioAreaObj.endEntity.address.name;
    }
    // TBD center need to be fixed back
    if (ioAreaObj.embed && ioAreaObj.embed.fix=='end') {
      srfResponse.srf[1].results[0] = ioAreaObj.embed.place;
    }
    if (entity.type != 'center') ioAreaObj.dirFromTo('end',entity.index,entity.parentType);
  }
  if ( srfResponse.routeFailType == 'location' &&
       srfResponse.srf[0].results.length == 1) {
    var entity = srfResponse.srf[0].results[0];
    if (! entity.address.name) {
      entity.address.name = ioAreaObj.startEntity.address.name;
    }
  }
}

//reviseSrfToNewOne = function (srfResponse,type) {
//  if (typeof(srfResponse.vid) != 'undefined') return;
//  var response = [];
//  if (typeof(srfResponse.type) != 'undefined' && srfResponse.type.name) {
//    response.routeFailType = srfResponse.type.name;
//    response.srf = [];
//    response.srf.push(srfResponse);
//    response.srf[0].parentType = response.routeFailType;
//    response.srf[0].reqSeed = ioAreaObj.preReqStart;
//    response.srf[0].srfIndex = 0;
//    if (isObjectEmpty(response.srf[0].results[0])) response.srf[0].results = [];
//    reviseOneSrfArrayWithParentTypeAndIndex(response.srf[0].results,response.routeFailType);
//    reviseIcons(response.srf[0]);
//    populateShowSets(response.srf[0],10);
//    return response;
//  }
//  if (typeof(srfResponse.routeFailType) != 'undefined') {
//    response.routeFailType = srfResponse.routeFailType;
//    response.srf = [];
//    var startAddress = [];
//    startAddress.parentType = 'startAddress';
//    startAddress.type = [];
//    startAddress.type.name = 'startAddress';
//    startAddress.results = srfResponse.startAddress;
//    response.srf.push(startAddress);
//    response.srf[0].srfIndex = 0;
//    response.srf[0].reqSeed = ioAreaObj.preReqStart;
//    if (isObjectEmpty(response.srf[0].results[0])) response.srf[0].results = [];
//    reviseOneSrfArrayWithParentTypeAndIndex(response.srf[0].results,'startAddress');
//    var endAddress = [];
//    endAddress.parentType = 'endAddress';
//    endAddress.type = [];
//    endAddress.type.name = 'endAddress';
//    endAddress.results = srfResponse.endAddress;
//    response.srf.push(endAddress);
//    response.srf[1].srfIndex = 1;
//    response.srf[1].reqSeed = ioAreaObj.preReqEnd;
//    if (isObjectEmpty(response.srf[1].results[0])) response.srf[1].results = [];
//    reviseOneSrfArrayWithParentTypeAndIndex(response.srf[1].results,'endAddress');
//
//    reviseIcons(response.srf[0]);
//    reviseIcons(response.srf[1]);
//    if (10 >= response.srf[0].results.length + response.srf[1].results.length) {
//
//      populateShowSets(response.srf[0],10);
//      populateShowSets(response.srf[1],10);
//    } else {
//      populateShowSets(response.srf[0],5);
//      populateShowSets(response.srf[1],5);
//    }
//    return response;
//  }
//  response.routeFailType = 'location';
//  if(typeof(type)!='undefined') response.routeFailType = type;
//  response.srf = [];
//  var oneLocation = [];
//  oneLocation.parentType = response.routeFailType;
//  oneLocation.type = [];
//  oneLocation.type.name = response.routeFailType;
//  oneLocation.results = srfResponse;
//  response.srf.push(oneLocation);
//  response.srf[0].srfIndex = 0;
//  response.srf[0].reqSeed = ioAreaObj.preReqStart;
//  if (isObjectEmpty(response.srf[0].results[0])) response.srf[0].results = [];
//  reviseOneSrfArrayWithParentTypeAndIndex(response.srf[0].results,response.routeFailType);
//  reviseIcons(response.srf[0]);
//  populateShowSets(response.srf[0],10);
//  return response;
//}

reviseEntityWithAddress = function (entity) {
  entity.place={};
  entity.address={};
  entity.address.name=entity.name;
  entity.lonlat=lonLatObjFrmPoint(entity.geom);
  entity.place.geom=entity.geom;	
  entity.place.address={};
  entity.place.address.name=entity.name;
  entity.place.lonlat=lonLatObjFrmPoint(entity.geom);
}

reviseOneSrfArrayWithParentTypeAndIndex = function (srfArray,parentType) {
  for(i in srfArray) {
    if (srfArray[i].parentType) continue;
    srfArray[i].parentType = parentType;
    srfArray[i].index = i;
    if (!srfArray[i].address) reviseEntityWithAddress(srfArray[i]);
  }
  return;
}

//reviseSrf = function (srfResponse) {
//  if (typeof(srfResponse.vid) != 'undefined') return;
//  for (var i = 0; i < srfResponse.srf.length; i++) {
//    if (isObjectEmpty(srfResponse.srf[i].results[0])) srfResponse.srf[i].results = [];
//    reviseOneSrfArrayWithParentTypeAndIndex(srfResponse.srf[i].results,srfResponse.srf[i].srfType);
//  }
//}

//reviseSrfWithParentTypeAndIndex = function (srfResponse) {
//  if (typeof(srfResponse.vid) != 'undefined') return;
//  if (typeof(srfResponse.type) != 'undefined' && srfResponse.type.name) {
//    if (isObjectEmpty(srfResponse.results[0])) srfResponse.results = [];
//    reviseOneSrfArrayWithParentTypeAndIndex(srfResponse.results,srfResponse.type.name);
//    return;
//  }
//  if (typeof(srfResponse.routeFailType) != 'undefined') {
//    if (isObjectEmpty(srfResponse.startAddress[0])) srfResponse.startAddress = [];
//    reviseOneSrfArrayWithParentTypeAndIndex(srfResponse.startAddress,'startAddress');
//    if (isObjectEmpty(srfResponse.endAddress[0])) srfResponse.endAddress = [];
//    reviseOneSrfArrayWithParentTypeAndIndex(srfResponse.endAddress,'endAddress');
//    return;
//  }
//  if (isObjectEmpty(srfResponse[0])) srfResponse = [];  // does not work
//  reviseOneSrfArrayWithParentTypeAndIndex(srfResponse,'location');
//}

reviseStartEndInResponse = function (response) {
  response.startAddress=ioAreaObj.startAddress;
  response.endAddress=ioAreaObj.endAddress;
  var g=response.marker.split('+');
  if (typeof(response.busStops) != 'undefined') {
    response.startEntity = {index : -1, type: 'rcm', 
                            lonlat : lonLatObjFrmPoint(response.busStops[0].geom),
                            geom : response.busStops[0].geom,
                            address : { name : response.busStops[0].name }};
    var l = response.busStops.length-1;
    response.endEntity = {index : -1, type: 'rcm', 
                            lonlat : lonLatObjFrmPoint(response.busStops[l].geom),
                            geom : response.busStops[l].geom,
                            address : { name : response.busStops[l].name }};
    // TBD center interaction
  } else {
    response.startEntity = ioAreaObj.startEntity;     
    if (!ioAreaObj.startEntity.type || ioAreaObj.startEntity.type != 'center') {
      response.startEntity.lonlat = { lon : g[0], lat : g[1] };
      response.startEntity.geom = "POINT("+g[0]+" "+g[1]+")";
    }
    response.endEntity = ioAreaObj.endEntity;     
    if (!ioAreaObj.endEntity.type || ioAreaObj.endEntity.type != 'center') {
      response.endEntity.lonlat = { lon : g[2], lat : g[3] };
      response.endEntity.geom = "POINT("+g[2]+" "+g[3]+")";
    }
  }
  response.routeSummary={};
  var a=response.summary.split("|");
  response.routeSummary.distance=a[0];
  response.routeSummary.videoDuration=a[1];
}	

reviseImageInResponse = function(response) {
  response.imgData=[];
  for (var i = 0; i < response.img.length; i++) {
    response.imgData[i]={};
    var tempText=response.img[i];
    var splitter=tempText.split("|");
    if(tempText.match("^DP")) {
      response.imgData[i].imgIndex=splitter[1];
      response.imgData[i].imgName=splitter[2];
      response.imgData[i].dpRoadName=splitter[3];
      var ab=splitter[4].split("#");
      response.imgData[i].lonlat = new OpenLayers.LonLat(ab[0],ab[1]);
      var classPNG=(MSIE6==true)?"class='pngfixclass'":"''";		
      response.imgData[i].arrowHtml="<img src="+imagePathsObj.textdirs.straight+" "+classPNG+"  style='opacity:1;filter:alpha(opacity=100)'/>";
    }
    if(tempText.match("^POI")) {
      response.imgData[i].poiName=splitter[1];
      response.imgData[i].imgName=splitter[2];
      response.imgData[i].id=splitter[3];
      var ab=splitter[4].split(",");
      response.imgData[i].lonlat = new OpenLayers.LonLat(ab[0],ab[1]);
      response.imgData[i].arrowHtml='';
    }
  }
}

reviseVideoInResponse = function(response) {
  response.allVideoLinks = [];
  response.mainDpVideoLinks = [];
  response.vVidDuration = 0;
  response.vidDuration = 0;
  if(typeof(accountDetails)!='undefined' && 
     accountDetails.customaudio && 
     routeEndsObj['end'].type == 'center') response.customaudiooverride=1;
     //accountDetails.customaudiooverride && 
  var sumNumVvid = 0;
  for (i in response.vid) {
    response.allVideoLinks.push(response.vid[i].video.src);
    response.mainDpVideoLinks.push(response.vid[i].video.src);
    var duration = (response.vid[i].video.duration);
    if (duration) { response.vVidDuration += parseInt(duration, "10"); }
    for(var j=sumNumVvid;j<(sumNumVvid+response.NumVvid[i]);j++) {
      response.allVideoLinks.push(response.video[j].VvidSource.src);
      var duration0 = response.video[j].VvidSource.duration;
      if (duration0) { response.vidDuration += parseInt(duration0, "10"); }
    }
    sumNumVvid += response.NumVvid[i];
  }
}
  
reviseCaptionInResponse = function(response) {
  for (var i in response.vid) {
    var d = response.vid[i].cap.split(/\|/);
    response.vid[i].arrow = d[0]; 
    response.vid[i].arrowSrc = getArrowSrc(d[0],false); 
    response.vid[i].arrowPrintSrc = getArrowSrc(d[0],true); 
    response.vid[i].direction = d[1];
    response.vid[i].passBy = new Array();
    if (d.length>2) {
      var id = d[2].split(/\#/);
      for(var j=0;j<id.length;j++) {
        var tp=id[j].split(":");
        response.vid[i].passBy[j] = {'name':tp[0],'id':tp[1],'idx':-1};
        var noOfImages=response.imgData.length;
        while(noOfImages) {
          if(response.imgData[--noOfImages].id==tp[1]) {
            response.vid[i].passBy[j].idx = noOfImages;
            break;
          }
        }
      }
    }
    for(var j=0;j<(response.imgData.length);j++) {
      if(response.imgData[j].dpRoadName) {
        if(response.imgData[j].imgIndex == mboxObj.syncMapPoints[i]) { // TBD
          fillArrowHtml(response.imgData[j],response.vid[i].arrow);
        }
      }
    }
  }
}

function getArrowSrc(arrow,toPrint) {
  var whichArrow,whichArrowJPG;
  if(arrow.match(/^A/)) {
    whichArrow=imagePathsObj.textdirs.start;
    if(MSIE6) whichArrowJPG=imagePathsObj.textdirs.start_jpg;
  } else if(arrow.match(/^B/)) {
    whichArrow=imagePathsObj.textdirs.stop;
    if(MSIE6) whichArrowJPG=imagePathsObj.textdirs.stop_jpg;
  } else if(arrow.match(/^S/)) { 
    whichArrow=imagePathsObj.textdirs.straight; 
    if(MSIE6) whichArrowJPG=imagePathsObj.textdirs.straight_jpg;
  } else if(arrow.match(/^R/)) { 
    whichArrow=imagePathsObj.textdirs.right; 
    if(MSIE6) whichArrowJPG=imagePathsObj.textdirs.right_jpg;	
  } else if(arrow.match(/^L/)) { 
    whichArrow=imagePathsObj.textdirs.left; 
    if(MSIE6) whichArrowJPG=imagePathsObj.textdirs.left_jpg;	
  } else if (arrow.match(/^U/)){ 
    whichArrow=imagePathsObj.textdirs.uturn; 
    if(MSIE6) whichArrowJPG=imagePathsObj.textdirs.uturn_jpg;	
  } else { whichArrow=''; if(MSIE6) whichArrowJPG='';}
  if (toPrint) return whichArrowJPG;
  return whichArrow;
}

function fillArrowHtml(imgData,arrow) {
  var classPNG=(MSIE6)?"class='pngfixclass'":"''";	
  if(arrow.match(/^S/)) {
    imgData.arrowHtml="<img "+classPNG+" src="+imageLoc+"/straight.png />";
    imgData.arrow="s";
  } else if(arrow.match(/^L/))  {
    imgData.arrowHtml="<img "+classPNG+" src="+imageLoc+"/left.png />";
    imgData.arrow="l";
  } else if(arrow.match(/^R/))  {
    imgData.arrowHtml="<img "+classPNG+" src="+imageLoc+"/right.png />";
    imgData.arrow="r";
  } else if(arrow.match(/^U/))  {
    imgData.arrowHtml="<img  "+classPNG+" src="+imageLoc+"/uturn.png />";
    imgData.arrow="u";
  } else {
    imgData.arrowHtml="<img  "+classPNG+" src="+imageLoc+"/straight.png />";
    imgData.arrow="s";
  }
}


function fixFatColumns() {
  $('table#MyTable td').each(function() {
    var defined_width = $(this).attr('width');
    if (defined_width) {
      var actual_width = $(this).width();
      var contents = $(this).html();
      if (contents.length) {
        var working_div = $('#ATempDiv');
        if (working_div.is('*')) {
          working_div.html(contents);
        } else {
          $('body').append('<div id="ATempDiv" style="position:absolute;top:-100px;left:-500px;font-size:13px;font-family:Arial">'+contents+'</div>');
          working_div = $('#ATempDiv');
        }

        if (working_div.width() > defined_width) {
          contents = working_div.text();
          working_div.text(contents);
          while (working_div.width() + 8 > defined_width) {
            // shorten the contents of the columns
            var working_text = working_div.text();
            if (working_text.length > 1) working_text = working_text.substr(0,working_text.length-1);
            working_div.text(working_text);
          }
          $(this).html(working_text+'...')
        }

        working_div.empty();
      }

    }
  });

}

