// the version of javascript supported
var jsVersion = 1.0;
// -----------------------------------------------------------------------------

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
jsVersion = 1.1;
// JavaScript helper required to detect Flash Player PlugIn version information
function JSGetSwfVer(i){
    // NS/Opera version >= 3 check for Flash plugin in plugin array
    if (navigator.plugins != null && navigator.plugins.length > 0) {
        if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
            var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
            var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
            descArray = flashDescription.split(" ");
            tempArrayMajor = descArray[2].split(".");
            versionMajor = tempArrayMajor[0];
            versionMinor = tempArrayMajor[1];
            if ( descArray[3] != "" ) {
                tempArrayMinor = descArray[3].split("r");
            } else {
                tempArrayMinor = descArray[4].split("r");
            }
            versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
            flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
        } else {
            flashVer = -1;
        }
    }
    // MSN/WebTV 2.6 supports Flash 4
    else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
    // WebTV 2.5 supports Flash 3
    else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
    // older WebTV supports Flash 2
    else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
    // Can't detect in all other cases
    else {
        
        flashVer = -1;
    }
    return flashVer;
} 

function DetectFlashVer(){
    // Major version of Flash required
    var reqMajorVer = 8;
    // Minor version of Flash required
    var reqMinorVer = 0;
    // Revision of Flash required
    var reqRevision = 0;


    reqVer = parseFloat(reqMajorVer + "." + reqRevision);

    // loop backwards through the versions until we find the newest version 
    for (i=25;i>0;i--) {    
        if (isIE && !isOpera) {
            versionStr = VBGetSwfVer(i);
        } else {
            versionStr = JSGetSwfVer(i);        
        }
        if (versionStr == -1 ) { 
            return false;
        } else if (versionStr != 0) {
            if(isIE && !isOpera) {
                tempArray         = versionStr.split(" ");
                tempString        = tempArray[1];
                versionArray      = tempString .split(",");             
            } else {
                versionArray      = versionStr.split(".");
            }
            versionMajor      = versionArray[0];
            versionMinor      = versionArray[1];
            versionRevision   = versionArray[2];
            
            versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
            versionNum        = parseFloat(versionString);
            // is the major.revision >= requested major.revision AND the minor version >= requested minor
            if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
                return true;
            } else {
                return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false ); 
            }
        }
    }   

    return (reqVer ? false : 0.0);
}


//for input tips
var Utility = {
	version:'1.0.beta',
	//params: inputId, tipMessage, defaultTextColor(optional), tipTextColor(optional)
	bindHint: function(inputId, tipMessage, hasNotice, defaultTextColor, tipTextColor){
		var inputBox = document.getElementById(inputId);
		if(inputBox){
			
			//check parameters
			if(!defaultTextColor)defaultTextColor = '#000000';//default black
			if(!tipTextColor)tipTextColor='#818181';//default grey
			if(!tipMessage)tipMessage='';
			inputBox.defaultTextColor = defaultTextColor;
			inputBox.tipTextColor = tipTextColor;
			inputBox.tipMessage = tipMessage;
			inputBox.isTipEnable = 'true';//just a flag for extension.
			
			inputBox.orignalOnfocus=inputBox.onfocus ;
			inputBox.orignalOnblur=inputBox.onblur ;
			inputBox.onfocus = function(){
				if(this.orignalOnfocus)this.orignalOnfocus();
				if(this.value==this.tipMessage){
					this.value='';
					this.style.color=defaultTextColor;
				}
			}
			inputBox.onblur = function (){
				if(this.orignalOnblur)this.orignalOnblur();
				if(this.value==''){
					this.value=this.tipMessage;
					this.style.color=tipTextColor;
				}
				if (hasNotice){
					var noticeId = (inputId+'Notice');
					$(inputId+'Notice').hide();
				}
			}
			inputBox.initialize = function(){
				if(inputBox.value==''){
					inputBox.value = tipMessage;
					inputBox.style.color = tipTextColor;
				}else if(inputBox.value==tipMessage){	
					inputBox.style.color = tipTextColor;
				}else{
					inputBox.style.color = defaultTextColor;
				}
			}
			inputBox.getValue = function(){
				if(this.value == this.tipMessage)
					return '';
				else
					return this.value;
			}
			inputBox.reset = function(){
				inputBox.value = tipMessage;
				inputBox.style.color = tipTextColor;
			}
			inputBox.info = function(){
				return 'input box with tip[' + this.tipMessage + '] value['+this.getValue()
					+'] defaultTextColor['+this.defaultTextColor + '] tipTextColor[' +this.tipTextColor +']';
			}
			inputBox.changeColor = function(){
				inputBox.style.color = defaultTextColor;	
			}
			inputBox.initialize();
		}
	}
}



function checkResult(response) {
	$('showPopup').innerHTML = response.responseText;
    var showWin = $('showPopup').innerHTML;
    ScreenConvert();DialogShow(showWin,500,300,0,0);
}




//from map-main.jsp
/*
 * Display price for gas station or phone for common poi.
 */
function showPhonePrice(phone, price){
    var result = '';
    if(!isNullOrEmpty(phone)){
        if(phone.length == 10){
            var f3 = phone.substring(0,3);
            var m3 = phone.substring(3,6);
            var l4 = phone.substring(6);
            result += "(" + f3 + ")" + m3 + "-" + l4;
        }
        else{
            result += phone;
        }
    }
    if(!isNullOrEmpty(price)){
        result += " price: $" + price;
    }
    return result;
}


/**
 * Show error message for Javascript error.
 */
function showError(msg){
    if(msg!=null && msg!='' && $('JSerror') ){
        $('JSerror').innerHTML = msg;
        $('JSerror').style.display = '';
        if ($('error') != null)
            $('error').style.display = 'none';
        if ($('message') != null)
            $('message').style.display = 'none';
    }else{
        $('JSerror').style.display = 'none';
    }
}

function switchContent(showContent, hideContent){
	$(hideContent).hide();
	$(showContent).show();
}


 
/**
 * parse a server string into several separated server strings
 */
function parseImgServerUrl(serverStr){
	var urls = serverStr.split(',');
    var len = urls.length;
    var imgUrl = '';
    if (len>0){
    	imgUrl +='MSN='+len;
    	for(var i =0;i<len;i++){
    		imgUrl += '&mapUrl' + i + '=' + urls[i];
    	}
    }
    return imgUrl;
}

function quickAdd(url) {
	callMyTN(url+"&dlg_id=popupQuickAdd", 650, 620);
}

function addSpotFromBubble(url){
	callMyTN(url+"&dlg_id=popupQuickAdd", 650, 620);
}

function quickShare(url) {
	callMyTN(url+"&dlg_id=popupQuickShare", 650, 620);
}

function callMyTN(url,width,height) {
	if (MyTN)
		MyTN.doModal(url,width,height);
}

function addSpotFromMap(lat, lon){
	var ll = lat+", "+lon;
	quickAdd(const_mytn_path+"/api/goCreateSpot.do?src=pop&locationType=0&latLon="+ll);
}

/**
 * Display loading image.
 */
function loadingStatus(divId){
	if(divId && $(divId)){
		//$(divId).show();
        $(divId).innerHTML = "<img src='" + const_img_path + "/images/ajax_loader.gif' alt='' />";
	}
}

//not be used
function selectAddrFromList(id, addr, llHid, ll){
	$(id).value = addr;
	$(llHid).value = ll;
}



function popWin(urlstring){
    window.open(urlstring,'', 'width=620,height=400,scrollbars=yes');
    return false;
}

/*******************************************
* Util
*/
// some util functions
String.prototype.trim= function(){   
    return this.replace(/(^\s*)|(\s*$)/g, "");  
}

function isNullOrEmpty(s){
	var result = false;
	if (s==null){
		result = true;
	} else {
		result = /^\s*$/.test(s);
	}
	return result;
}

function toJs(str){
    return str.replace(/'/g, "\\'");
}

/**
 * get attribute from xml doc
 */
function getXmlAttrValue(doc,nodeName,idx,attrName) {
	if (doc==null)
		return "";
	var node = doc.getElementsByTagName(nodeName);
	if (node==null)
		return "";
	if (node.length>idx)
		return node[idx].getAttribute(attrName);
	return "";
}

/**
 * get text from xml doc
 */
function getXmlTextValue(doc,nodeName,idx,attrName) {
	if (doc==null)
		return "";
	var node = doc.getElementsByTagName(nodeName);
	if (node==null)
		return "";
	if (node.length>idx)
		return node[idx].getAttribute(attrName);
	return "";
}


/*
 * Set value to specific inputbox.
 */
function copyValue(id, value){
    if ($(id)){
        $(id).value = value;
        $(id).style.color = '#000';//set text black 
    }
}

function toJs(str){
	str = str.replace(/"/g, '\\"');
	return str.replace(/'/g, "\\'");
}

function switchValue(id1,id2){
	if ($(id1) != null && id2 != null){
		var tmp = $F(id1);
		$(id1).value = $F(id2);
		$(id2).value = tmp;
	}
}

function setCookie(name, value, validTime){
    var expires = new Date();
    if (validTime != null)
    	expires.setTime(expires.getTime() + validTime);
    else
    	expires.setTime(expires.getTime() + 12 * 30 * 24 * 60 * 60 * 1000);
   // document.cookie = name + "=" + escape(value) + ";path="+const_ctx_path+";expires=" + expires.toGMTString();
   document.cookie = name + "=" + escape(value) + ";expires=" + expires.toGMTString();
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
} 

/*************************
* from mapTele.js
**/

// JavaScript Document


//switch big and small map
function ss(){
	var mb=document.getElementById('leftColumn');
	 if(mb.style.display=='none'){
	 	map.shrinkMap();
	 }else{
	 	map.expandMap();
	 }
}
//Search Nav for map(map dir biz)
function showSearch(tagName){ 
 	 var secondNav= null;
	 var searchMap = null;
	 if($("secondNav")){
	   secondNav=$("secondNav").getElementsByTagName("li");
	 }
	 if($("searchMap")){
	   searchMap=$("searchMap").getElementsByTagName("ul");
	 }
	 
	 if(secondNav != null){
		 for(i=0;i<secondNav.length;i++){
		 	if(secondNav[i].id==tagName+'li')
	            secondNav[i].className = 'hover';
		 	else
		 	    secondNav[i].className = '';
		 }
	 }
	 if(searchMap != null){
		 for(i=0;i<searchMap.length;i++){
		 	if(searchMap[i].id==tagName+'ul')
	            searchMap[i].style.display = 'block';
		 	else
		 	    searchMap[i].style.display = 'none';
		 }
	 }
		 	
	 //Controle category display link.Add by wzhli 20080116
	 if($('catlinkspan')){
	     if(tagName != 'poi'){
	     	$('catlinkspan').hide();
	     }else{
	     	$('catlinkspan').show();
	     }
	 }
	 
	 if (tagName == 'getmap' && $('gmAddress')){
	 	$('gmAddress').focus();
	 }else if(tagName == 'dir' && $('stAddr')){
	 	$('stAddr').focus();
	 }else if(tagName == 'poi' && $('key')){
	 	$('key').focus();
	 }

}



//Left Column Sidebar Nav
//flag == true, doesn't change ui only calculate the length of content pane
//flag == false, open the next pane
function showPane(paneId, flag){
    var currStyle = $(paneId).className;
    var allPanes = $('leftColumn').select('div#tipPane','div#favPane','div#categoryPane','div#result');
    var otherPanes = allPanes.without($(paneId));

    if('result' == paneId){
    //Display result navbar.
        $(paneId).style.display = '';
    }
	var i=0;
	var tmpPane = $(paneId+'#content');
    if ('sidebarNav' == currStyle){//to open
        $(paneId).className = 'sidebarOpen';
        if (otherPanes){
            otherPanes.each(function(item){
            	if($(item.id+"#content")){
                	item.className="sidebarNav";
                	$(item.id+"#content").hide();
                }
            })
        }
        $(paneId+'#content').show();
		tmpPane = $(paneId+'#content');
    }else if('sidebarOpen' == currStyle && !flag){//to close
        $(paneId).className = 'sidebarNav';
        $(paneId+'#content').hide();
        
        //Allow div element.Commented by wzhli 20080118
        //open next if it's the last one open first
        
        var nextPaneId = getNextPaneId(allPanes,paneId);
        if ($(nextPaneId)){
            $(nextPaneId).className = 'sidebarOpen';
            $(nextPaneId+"#content").show();
			tmpPane = $(nextPaneId+"#content");
        }else{ //open first
            $('tipPane').className = 'sidebarOpen';
            $("tipPane#content").show();
			tmpPane = $("tipPane#content");
        }
        
    }
    
    //Commented by wzhli 20080203
	otherPanes.each(function(item){
		if (item.style.display != "none") i++;
    })

	if (tmpPane)
		tmpPane.style.height= 505 - i*22  + "px";
    
}
function getNextPaneId(divs, divId){
	var count = divs.length;
	var nextDivId = divId;
	for (var i = 0;i < count;i++){
		if (divs[i].id == divId){
			for (var j = i+1 ;j < count; j++){
				if (divs[j].style.display !="none"){
					nextDivId = divs[j].id;
					break;
				}
					
			}
		}
	}
	if (nextDivId == divId)
		nextDivId = divs[0].id;
	return nextDivId;
 }
 
 function closePane(divId){
 	if(divId){
 		$(divId).hide();
 	}
 	showPane('tipPane');
 }
 
/**********************************************
 * from popDialog.js
 */ 
 // JavaScript Document

var t_DiglogX,t_DiglogY,t_DiglogW,t_DiglogH;

function gid(id) {
	return document.getElementById?document.getElementById(id):null;
}

function gname(name) {
	return document.getElementsByTagName?document.getElementsByTagName(name):new Array()
}

function Browser() {
	var ua, s, i;
	this.isIE = false;
	this.isNS = false;
	this.isOP = false;
	this.isSF = false;
	ua = navigator.userAgent.toLowerCase();
	s = "opera";
	if ((i = ua.indexOf(s)) >= 0) {
	    this.isOP = true;return;
	}
	s = "msie";
	if ((i = ua.indexOf(s)) >= 0) {
	    this.isIE = true;return;
	}
	s = "netscape6/";
	if ((i = ua.indexOf(s)) >= 0) {
	    this.isNS = true;return;
	}
	s = "gecko";
	if ((i = ua.indexOf(s)) >= 0) {
	    this.isNS = true;return;
	}
	s = "safari";
	if ((i = ua.indexOf(s)) >= 0) {
	    this.isSF = true;return;
	}
}

function DialogLoc() {
	var dde = document.documentElement;
	if (window.innerWidth) {
	    var ww = window.innerWidth;
	    var wh = window.innerHeight;
	    var bgX = window.pageXOffset;
	    var bgY = window.pageYOffset;
	} else {
	    var ww = dde.offsetWidth;
	    var wh = dde.offsetHeight;
	    var bgX = dde.scrollLeft;
	    var bgY = dde.scrollTop;
	}
	t_DiglogX = (bgX + ((ww - t_DiglogW)/2));
	t_DiglogY = (bgY + ((wh - t_DiglogH)/2));
}

function DialogShow(showWin,ow,oh,w,h) {
	var objDialog = document.getElementById("DialogMove");
	if (!objDialog) 
		objDialog = document.createElement("div");
	t_DiglogW = ow;
	t_DiglogH = oh;
	DialogLoc();
	objDialog.id = "DialogMove";
	var oS = objDialog.style;
	oS.display = "block";
	oS.top = t_DiglogY + "px";
	oS.left = t_DiglogX + "px";
	oS.margin = "0px";
	oS.padding = "0px";
	oS.width = w + "px";
	oS.height = h + "px";
	oS.position = "absolute";
	oS.zIndex = "5";
	oS.background = "#fff";
	oS.border = "solid #1d81b2 1px";
	objDialog.innerHTML = showWin;
	document.body.appendChild(objDialog);
}

function DialogHide() {
	ScreenClean();
	var objDialog = document.getElementById("DialogMove");
	if (objDialog) objDialog.style.display = "none";
}

function ScreenConvert() {
	var browser = new Browser();
	var objScreen = gid("ScreenOver");
	if (!objScreen) 
		var objScreen = document.createElement("div");
	var oS = objScreen.style;
	objScreen.id = "ScreenOver";
	oS.display = "block";
	oS.top = oS.left = oS.margin = oS.padding = "0px";
	if (document.body.clientHeight)    {
	    var wh = document.body.clientHeight + "px";
	} else if (window.innerHeight) {
	    var wh = window.innerHeight + "px";
	} else {
	    var wh = "100%";
	}
	oS.width = "100%";
	oS.height = wh;
	oS.position = "absolute";
	oS.zIndex = "3";
	if ((!browser.isSF) && (!browser.isOP)) {
	    oS.background = "#fff";
	} else {
	    oS.background = "#fff";
	}
	oS.filter = "alpha(opacity=60)";
	oS.opacity = 40/100;
	oS.MozOpacity = 40/100;
	document.body.appendChild(objScreen);
	var allselect = gname("select");
	for (var i=0; i<allselect.length; i++) {
		allselect[i].style.visibility = "hidden";
	}
}

function ScreenClean() {
	var objScreen = document.getElementById("ScreenOver");
	if (objScreen) 
		objScreen.style.display = "none";
	var allselect = gname("select");
	for (var i=0; i<allselect.length; i++) 
		allselect[i].style.visibility = "visible";
}


function Report(Type,ID){
   var PostData = "do=" + Type + "&reportid=" + ID;
   //PostRequest(window.location.protocol + "//" + window.location.host + "/AJAX_Comm.aspx", PostData);
   var showWin = document.getElementById('showPopup').innerHTML;
   //ScreenConvert();DialogShow("<div id=\"DialogLoading\">loading...</div>",110,10,124,20);
   ScreenConvert();DialogShow(showWin,500,300,0,0);

}



///////////////////////////////////

function isonWindow(aPars){
    this.aPars = aPars;
    if(typeof isonWindow._initialized == "undefined"){
        isonWindow.prototype.show = function(){
            _openWindow(aPars);
        }
        isonWindow.prototype.hide = function(){
            _closeWindow();
        }
        isonWindow._initialized = true;
    }
}

function _openWindow(aPars){ 
    this.aPars = aPars;
    var objLayout =  $("LayoutDIV");
    var objWindow =  $("WindowDIV");
    var objLock =  $("LockWindows");
    objLock.style.display = "block";

//    var tWidth = document.body.clientWidth ;
//    var tHeight = document.body.clientHeight;
//    if(document.body.scrollWidth < document.body.clientWidth) tWidth = document.body.clientWidth;
//    if(document.body.scrollHeight < document.body.clientHeight) tHeight = document.body.clientHeight;


    objLock.style.width =document.body.clientWidth+"px";
    objLock.style.height = document.body.clientHeight+"px";
    
    //alert(objLock.style.width+","+objLock.style.height);
//    objLock.style.minWidth = tWidth+"px";
//    objLock.style.minHeight = tHeight+"px";

 //   if(aPars.width>document.body.clientWidth) aPars.width = document.body.clientWidth;
 //   if(aPars.height>document.body.clientHeight) aPars.height = document.body.clientHeight;
    objLayout.style.display = "block";

   // objWindow.style.width = aPars.width+"px";
   // objWindow.style.height = aPars.height+"px";
   

    objWindow.style.marginLeft = -aPars.width/2+"px";
    objWindow.style.marginTop = -aPars.height/2+"px";
    //_displaySelect();
    //document.location = "#";
    //_ajaxLoadWindow(aPars.par1,aPars.par2);
   // _displaySelect();
   
   window.onresize = function (){
   	 objLock.style.width =document.body.clientWidth+"px";
   	 objLock.style.height = document.body.clientlHeight+"px";
   }
   window.onscroll = function (){
   	 objLock.style.width =document.body.scrollWidth+"px";
   	 objLock.style.height = document.body.scrollHeight+"px";
   }
}

function _closeWindow(){
    $("LockWindows").style.display = "none";
    $("WindowDIV").innHTML = "";
    $("LayoutDIV").style.display = "none";
    //_displaySelect();
}
  


function showPopupContent(n){ 
    var popNav=document.getElementById("popNav").getElementsByTagName("li");
    var popContent=document.getElementById("popContent").getElementsByTagName("ul");
    for(i=0;i<popNav.length;i++){
    	popNav[i].className=i==n?"hover":"";
    	popContent[i].style.display=i==n?"block":"none";
 	}
}

function checkLoginForm(){  
	var loginKey = $('loginKey').value;	
	if (!IsNum(loginKey) ){		   
	    if($("waring")){	
		
          $("waring").innerHTML =const_msg_notNumb;
	    }
		return false;
	}
	return true;
}

function IsNum(fData)
{
    var str;
    var fDatastr="";
    if (isNullOrEmpty(fData))
        return false;
    for (var i=0;i<fData.length;i++)
    {
        str=fData.substring(i,i+1);
        if (isNaN(str)){          
          return false;         
        }
           
    }   
    return true;   
}	