﻿function $(el)
{
    return document.getElementById(el);
}
var map;
var center;

function check()
{
    if(!hideText())
        return false;
    if(isnull("TextBox_name","餐馆名","") || isnull("TextBox_addr","地址","") || isnull("TextBox_tel","电话","") || isnull("TextBox_db","地标","") || isnull("TextBox_csfw","菜式风味","")|| isnull("TextBox_yysj","营业时间","")|| isnull("TextBox_cthj","餐厅环境",""))
        return false;
    if(!/^\d+$/.test($("TextBox_tel").value))
    {
        alert("电话号码输入错误");
        $("TextBox_tel").focus();
        return false;
    }
    return true;
}

function showMore()
{
    if(check())
    {
        $("wsxx").style.display = "none";
        $('head_l').className = 'head_l2';
        $('head_r').className = 'head_r2';
        $("comm").style.display = "none";
        $("more").style.display = "block";
        document.documentElement.scrollTop = 0;
        if(!map)
            initialize();
    }
    else
    {
        $("wsxx").style.display = "block";
        window.setTimeout("$('wsxx').style.display = 'none';", 3000);
    }
    
}
function showCom()
{
    $('head_l').className = 'head_l';
    $('head_r').className = 'head_r';
    $("comm").style.display = "block";
    $("more").style.display = "none";
}

//google地图初始化
function initialize()
{
    if (GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById("gmap"));
        map.addControl(new GSmallMapControl());
        map.enableGoogleBar();
        map.addControl(new GMapTypeControl());
     
       center = new GLatLng(29.557,106.567);
       map.setCenter(center, 12);
    
       marker = new GMarker(center, {draggable: true});

        GEvent.addListener(marker, "dragend", function(point) {
            var index = point.x.toString().indexOf(".");
            var newX = point.x.toString().substring(0, index) + point.x.toString().substring(index+1, index+6);
            index = point.y.toString().indexOf(".");
            var newY = point.y.toString().substring(0, index) + point.y.toString().substring(index+1, index+6);
            document.getElementById("HiddenField_map").value = "x=" + newX + ",y=" + newY;
        });

        /*GEvent.addListener(map, "click", function(o, p) {
         alert("您点击的位置的经纬度是：" + p);
     
       });*/

        map.addOverlay(marker);
    }
    //document.getElementById("gmap").style.visibility = "false";
}

function setInMiddle() {
    var center = map.getCenter();
    marker.setLatLng(center);
    
    var index = center.x.toString().indexOf(".");
    var newX = center.x.toString().substring(0, index) + center.x.toString().substring(index+1, index+6);
    index = center.y.toString().indexOf(".");
    var newY = center.y.toString().substring(0, index) + center.y.toString().substring(index+1, index+6);
    document.getElementById("HiddenField_map").value = "x=" + newX + ",y=" + newY;
    //alert("当前中心的经纬度是："+center);
}


function onUploadImgChange(sender, i){   
    if( !sender.value.match( /.jpg|.gif|.png|.bmp/i ) ){   
        alert('图片格式无效！');   
        return false;   
    }   
       
    var objPreview = document.getElementById( 'preview'+ i );   
    var objPreviewFake = document.getElementById( 'preview_fake'+ i );   
    var objPreviewSizeFake = document.getElementById( 'preview_size_fake' );   
       
    if( sender.files && sender.files[0] ){   
        objPreview.style.display = 'block';   
        objPreview.style.width = 'auto';   
        objPreview.style.height = 'auto';   
        // Firefox 因安全性问题已无法直接通过 input[file].value 获取完整的文件路径   
        objPreview.src = sender.files[0].getAsDataURL();       
    }else if( objPreviewFake.filters ){    
        // IE7,IE8 在设置本地图片地址为 img.src 时出现莫名其妙的后果   
        //（相同环境有时能显示，有时不显示），因此只能用滤镜来解决   
           
        // IE7, IE8因安全性问题已无法直接通过 input[file].value 获取完整的文件路径   
        sender.select();   
        var imgSrc = document.selection.createRange().text;   
           
        objPreviewFake.filters.item(   
            'DXImageTransform.Microsoft.AlphaImageLoader').src = imgSrc;   
        objPreviewSizeFake.filters.item(
            'DXImageTransform.Microsoft.AlphaImageLoader').src = imgSrc;   
           
        autoSizePreview( objPreviewFake,    
            objPreviewSizeFake.offsetWidth, objPreviewSizeFake.offsetHeight );   
        objPreview.style.display = 'none';   
    } 
    if(i < 4)
    {
        document.getElementById("imgDiv" + (i+1)).style.display = "block";
    }
}   

function onPreviewLoad(sender){   
    autoSizePreview( sender, sender.offsetWidth, sender.offsetHeight );   
}   

function autoSizePreview( objPre, originalWidth, originalHeight ){   
    var zoomParam = clacImgZoomParam( 129, 88, originalWidth, originalHeight );   
    objPre.style.width = zoomParam.width + 'px';   
    objPre.style.height = zoomParam.height + 'px';   
    objPre.style.marginTop = zoomParam.top + 'px';   
    objPre.style.marginLeft = zoomParam.left + 'px';  
}


function clacImgZoomParam( maxWidth, maxHeight, width, height ){   
    var param = { width:width, height:height, top:0, left:0 };   
       
    if( width>maxWidth || height>maxHeight ){   
        rateWidth = width / maxWidth;   
        rateHeight = height / maxHeight;   
           
        if( rateWidth > rateHeight ){   
            param.width = maxWidth;   
            param.height = height / rateWidth;   
        }else{   
            param.width = width / rateHeight;   
            param.height = maxHeight;   
        }   
    }   
       
    param.left = (maxWidth - param.width) / 2;   
    param.top = (maxHeight - param.height) / 2;   
       
    return param;   
}

function showDiqu()
{
    $("doing").style.height = getRange().height + "px";
    $("doing").style.width = getRange().width + "px";
    $("selectDiqu").style.display = "block"
    $("selectDiqu").style.left = Math.floor((document.documentElement.clientWidth - document.getElementById("selectDiqu").clientWidth)/2)+"px";
    var shadowObj1 = new boxShadow(new boderTemplate_whiteBgOrangeBorder(), false);
    $("DropDownList_cx").style.visibility = "hidden";
    $("DropDownList_xf").style.visibility = "hidden";
    shadowObj1.bind($("selectDiqu"), $("form1"));
    shadowObj1.setCloseButton(document.getElementById("winCloseBtn"));
    $("TextBox_name").blur();
}

function hideDiqu()
{
    $("doing").style.display = "none";
    $("selectDiqu").style.display = "none";
    $("DropDownList_cx").style.visibility= "visible";
    $("DropDownList_xf").style.visibility= "visible";
    $("TextBox_name").focus();
}

function getRange()                      //得到屏幕的大小
{
      var top     = document.body.scrollTop;
      var left    = document.body.scrollLeft;
      var height  = document.body.clientHeight;
      var width   = document.body.clientWidth;

      if (top==0 && left==0 && height==0 && width==0)
      {
        top     = document.documentElement.scrollTop;
        left    = document.documentElement.scrollLeft;
        height  = document.documentElement.clientHeight;
        width   = document.documentElement.clientWidth;
      }
      return  {top:top  ,left:left ,height:height ,width:width };
}
var isDoingSelection = false;
var previousValue = "";
var NameId = "TextBox_name";
function CookNameTextPress(obj)
{
    if(obj.value == "") 
    {
        document.getElementById("cookList").style.visibility = "hidden";
        return;
    }
    try
    {
        pname = event.propertyName;
    }catch(e){pname = "";}
    setTimeout(function(objValue,pn){return function()
    {
        if(previousValue != objValue)
        {
            //document.getElementById(HideCookId).value = "";
            if(window.attachEvent)
            {
                if(isDoingSelection)
                {
                    isDoingSelection = false;
                    return;
                }
                if(pn == "value")
                {
                    GetRelateList(objValue);
                    previousValue = objValue;
                }
            }
            else
            {
                GetRelateList(objValue);
                previousValue = objValue;
            }
        }
    }}(obj.value,pname),1)
}


function hideText()
{
    var cn = document.getElementById(NameId).value;
    if(cn)
    {
        var htmstr = document.getElementById("cookList").innerHTML;
        if(htmstr.indexOf(">"+cn+"<")>0)
        {
            alert("此餐馆已经添加，请添加别的餐馆。");
            setTimeout(function(){
                document.getElementById(NameId).focus();
                },10);
            return false;
        }
    }
    else
    {
        alert("餐馆名不能为空。");
        setTimeout(function(){
                document.getElementById(NameId).focus();
                },10);
        return false;
    }
    return true;
    //document.getElementById("cookList").style.visibility = "hidden";
    //document.getElementById("txt1").style.visibility = "hidden";
}

function focusShow(obj)
{
    hideCookList();
    document.getElementById(obj).style.display = "block";
}

function focusHide(obj)
{
    document.getElementById(obj).style.display = "none";
}

function hideCookList()
{
    document.getElementById("cookList").style.visibility = "hidden";
}

function GetRelateList(value)
{
    var ajaxobj = new DTAjax();
    ajaxobj.onGet = 
    function(str)
    {
        matchArr = new Array();
        matchArr = str.match(/([\d\D]+?)\([\d]+\)\,/g)
        if(!matchArr) 
        {
            document.getElementById("cookList").style.visibility = "hidden";
            return;
        }
        document.getElementById("cookList").innerHTML = "";
        document.getElementById("cookList").style.height =  ((matchArr.length>10?10:matchArr.length)*22+36)+"px";
        
        var count = 0;
        document.getElementById("cookList").innerHTML+="<div style='font-weight:bold;color:black;font-size:14px'>下面是已经添加过的餐馆</div>"
        for(var i=0;i<matchArr.length;i++)
        {
            matchArr[i] = matchArr[i].match(/([\d\D]+?)\(([\d]+)\)\,$/);
            count++;
            if(count>10) break;
            document.getElementById("cookList").innerHTML+= "<div style='line-height:19px;text-align:left;margin:2px;cursor:pointer; ' id='crromm_list_"+ matchArr[i][2] +"'>"+matchArr[i][1]+"</div><div style='clear:both;'></div>";
        }
        
        getDomPostion(document.getElementById(NameId),document.getElementById("cookList"));
    }
    ajaxobj.addPostData("key",value);
    ajaxobj.addPostData("area",document.getElementById("DropDownList_dq").value);
    ajaxobj.getStringFromUrl("post","addcookrooms.aspx",true);
}
function getDomPostion(dom,menuDom)
{
    var postionX = 0;
    var postionY = 0;
    var tobj = dom;
    
    postionX = getXY(dom)[0];
    postionY = getXY(dom)[1];
    menuDom.style.width = "400px";
    //menuDom.style.left =  postionX+"px";
    menuDom.style.left =  (postionX+404)+"px"; 
    //menuDom.style.top =  (postionY+dom.clientHeight)+"px";
    menuDom.style.top =  (postionY+dom.clientHeight-25)+"px";
    //menuDom.style.position =  "absolute";
    menuDom.style.visibility = "";
}

function getXY(el)
{
    var pos;
    if(this.getExplorerType()==1)
    {
        var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
        var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
        pos = [el.getBoundingClientRect().left+scrollLeft, el.getBoundingClientRect().top+scrollTop];
    }
    else
    {
        pos = [el.offsetLeft, el.offsetTop];
        var parentNode = el.offsetParent;
        if (parentNode != el) 
        {
            while (parentNode) 
            {
                pos[0] += parentNode.offsetLeft;
                pos[1] += parentNode.offsetTop;
                parentNode = parentNode.offsetParent;
            }
        }
        if (el.parentNode) { parentNode = el.parentNode; }
        else { parentNode = null; }

        while (parentNode && parentNode.tagName.toUpperCase() != 'BODY' && parentNode.tagName.toUpperCase() != 'HTML')
        {
            if (parentNode.style.display != 'inline') 
            {
                pos[0] -= parentNode.scrollLeft;
                pos[1] -= parentNode.scrollTop;
            }

            if (parentNode.parentNode) 
            {
                parentNode = parentNode.parentNode; 
            } 
            else { parentNode = null; }
        }
    }
    return pos;
}
function getExplorerType()
{
    var ua = navigator.userAgent.toLowerCase();
    if(window.ActiveXObject)
    {
        return 1;
    }
    else if((ua.indexOf('firefox')>-1))
    {
        return 2;
    }
    else if((ua.indexOf('opera')>-1))
    {
        return 3;
    }
}
