// JScript 文件
var _shopID;
var _shopOwnerID;
var _controlID;
var url = "../../WebService/ShopService.ashx";
function HttpAjax() { 
    var IsFirefox=false;
	var self = this;    
    this.createXMLHttpRequest = function() {
	    try { 
		    return new ActiveXObject("Msxml2.XMLHTTP"); 
	    } 
	    catch (e) {
	    }
	    try { 
		    return new ActiveXObject("Microsoft.XMLHTTP"); 
	    } 
	    catch (e) {
	    }
	    try {
            IsFirefox=true;
		    return new XMLHttpRequest(); 
	    } 
	    catch(e) {
                  IsFirefox=false;
	    }
	    return null;
    }
    this.toString = function() {
        return "Howave.Web.HttpAjax";
    }
  
    this.create = function(url, callbackHandler, onErrorHandler) {
        var xmlReq = this.createXMLHttpRequest();
        if (xmlReq != null) {        
		    xmlReq.open("GET", url, true);
		    xmlReq.onreadystatechange = function() {
                switch(xmlReq.readyState)
                {
                    case 0: 
                        break;
                    case 1: 
                        break;
                    case 2: 
                        break;
                    case 3: 
                        break;
                    case 4: 
                        callbackHandler(xmlReq);
                        break;
                    default:
                        onErrorHandler();
                        break;
                    }
                }
		    xmlReq.send(null);
		    return xmlReq;
        }
        else {
			throw "System does not support Ajax.";
        }
    }
    this.synchronCreate=function(url, callbackHandler, onErrorHandler) {
        
        var xmlReq = this.createXMLHttpRequest();
        
        if (xmlReq != null) {  
	    xmlReq.open("GET", url, false);
     
	    xmlReq.onreadystatechange = function() { 
                 
                switch(xmlReq.readyState)
                {
                    case 0: 
                        break;
                    case 1: 
                        break;
                    case 2: 
                        break;
                    case 3: 
                        break;
                    case 4: 
                        
                        callbackHandler(xmlReq);
                        break;
                    default:
                        onErrorHandler();
                        break;
                }
            }
           
	    xmlReq.send(null);
             if(IsFirefox)
            {callbackHandler(xmlReq);}
	    return xmlReq;
        }
        else {
			throw "System does not support Ajax.";
        }
    }
	
	this.post = function(url,sendString, callbackHandler, onErrorHandler) {
        var xmlReq = this.createXMLHttpRequest();
        if (xmlReq != null) {        
		    xmlReq.open("post", url, true);
			xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		xmlReq.onreadystatechange = function() {
                switch(xmlReq.readyState)
                {
                    case 0: 
                        break;
                    case 1: 
                        break;
                    case 2: 
                        break;
                    case 3: 
                        break;
                    case 4: 
                        callbackHandler(xmlReq);
                        break;
                    default:
                        onErrorHandler();
                        break;
                    }
                }
		    xmlReq.send(sendString);
		    return xmlReq;
        }
        else {
			throw "System does not support Ajax.";
        }
    }
    this.setVisible = function(controlId, visible) {
        if (document.getElementById(controlId) != null)
            document.getElementById(controlId).style.visibility = (visible ? "visible" : "hidden");
    }
	this.setDisplay = function(controlId, displayMode) {
        if (document.getElementById(controlId) != null)
            document.getElementById(controlId).style.display = displayMode;
	}
    this.updateContent = function (url, controlId) {
      var processingIcon;
      var _controlContent=document.getElementById(controlId);
      if( _controlContent!=null){
         _controlContent.innerHTML="<img src=\"../../IMS_Img/Index/Loading.gif\"/>";
      }		
        var xmlReq = this.createXMLHttpRequest();
        if (xmlReq != null) {
		    xmlReq.open("GET", url, true);
		    xmlReq.onreadystatechange = function() {
                switch(xmlReq.readyState)
                {
                    case 0: 
                        break;
                    case 1: 
                        break;
                    case 2: 
                        break;
                    case 3: 
                        break;
                    case 4: 
                        updateContent_callback(xmlReq, controlId);
                        break;
                    default:
                        updateContent_onError(controlId);
                        break;
                    }
                }
		    xmlReq.send(null);
        }
        else {
           
        }
    }
	
    this.update=function(url,update_callback){
	  var processingIcon;
     		
        var xmlReq = this.createXMLHttpRequest();
        if (xmlReq != null) {
		    xmlReq.open("GET", url, true);
		    xmlReq.onreadystatechange = function() {
                switch(xmlReq.readyState)
                {
                    case 0: 
                        break;
                    case 1: 
                        break;
                    case 2: 
                        break;
                    case 3: 
                        break;
                    case 4: 
                        update_callback(xmlReq);
                        break;
                    default:
                        
                        break;
                    }
                }
		    xmlReq.send(null);
        }
        else {
           
        }
	}
    this.getResult=function(url){
        var xmlReq = this.createXMLHttpRequest();
        if (xmlReq != null) {
		    xmlReq.open("GET", url, true);
		    xmlReq.onreadystatechange = function() {
                switch(xmlReq.readyState)
                {
                    case 0: 
                        break;
                    case 1: 
                        break;
                    case 2: 
                        break;
                    case 3: 
                        break;
                    case 4:                       
                        return xmlReq.responseText;
                        break;
                    default:
                        return "";
                        break;
                    }
                }
		    xmlReq.send(null);
        }
        else {
           return "Ajax is not support!";
        }
    
    }   
 
    function updateContent_callback(rValue, controlId) {
      var rsp = rValue.responseText;
      var _control=document.getElementById(controlId);
      if(_control!=null)
          _control.innerHTML=rsp;  
    }   
    function updateContent_onError(controlId) {
        if (document.getElementById(controlId) != null)
            document.getElementById(controlId).innerHTML = "Data unavailable. Please try again.";    
    }
}
function getRequestBody(oForm){
	var aParams=new Array();
	for(var i=0;i<oForm.elements.length;i++){
		var sParam=encodeURIComponent(oForm.elements[i].name);
		sParam+="=";
		sParam+=encodeURIComponent(oForm.elements[i].value);
		aParams.push(sParam);
	}
	return aParams.join("&");
}
function $(id){
  return document.getElementById(id);
}

var MsgObj=null;
function ShowShopMsg(ShopID,ShopOwnerID,ControlID)
{
    _shopID=ShopID;
    _shopOwnerID=ShopOwnerID;
    _controlID=ControlID;
     MsgObj=$(ControlID);
    if(MsgObj!=null && ShopID>0)
    {
        var sendString = "Action=GetShopMsg&ShopID=" + ShopID + "&ShopOwnerID="+ShopOwnerID+"&rnd="+Math.random();               
       
        var ajax = new HttpAjax();
        ajax.post(url,sendString, ShowMessageOnSuccess, onErrorHandler) ;
       
    }

}
function ShowMessageOnSuccess(req)
{ 
   MsgObj.innerHTML=req.responseText;
}
function onErrorHandler(){
  
}

function submitComment()
{
    var actionObj = $("h_action");
    var msgIDObj = $("h_msgID");
    var txtValidate = $("CheckCode");
    var txtCommentBody = $("TxtMessage");
    var shop_id = $("h_shop_id").value;
    var saler_id = $("h_saler_id").value;
    var saler_name = $("h_saler_name").value;
    var product_id =  "0";  
    if(txtValidate != null && txtCommentBody != null)
    {
        if(txtValidate.value.length != 5)
        {
            alert("没有填写验证码！");
            txtValidate.focus();
            return;
        }
        if(txtCommentBody.value == "")
        {
            alert("没有填写评论内容！");
            txtCommentBody.focus();
            return;
        }
        else if(txtCommentBody.value.length>400)
        {
            alert("当前字数为"+txtCommentBody.value.length+"填写评论内容超出字数限制！");
            txtCommentBody.focus();
            return;
        }
    
        var sendString = "Action=LeaveMsg&h_action=" + actionObj.value + "&msgID=" + msgIDObj.value              
              + "&product_id="+product_id+"&saler_name="+encodeURIComponent(saler_name)+"&saler_id="+saler_id+"&shop_id="+shop_id
              + "&checkcode=" + encodeURIComponent(txtValidate.value) + "&content=" + txtCommentBody.value;      
       
        var ajax =new HttpAjax();
       ajax.post(url,sendString, SubmitMessageOnSuccess, onErrorHandler) ;
      
        
        changeControlStatus(true);
        setTimeout('changeControlStatus(false);', 15000)
    }
}
function SubmitMessageOnSuccess(req)
{
        var txtValidate = $("CheckCode");
        var txtCommentBody = $("TxtMessage");
        var t = parseInt(req.responseText);
        if(t == -1)
        {
            alert("验证码错误！");
            changeControlStatus(false);
        }
        else if(t == -2)
            alert("发表留言失败");
        else if(t == -4)
            alert("发表留言失败，有被禁用词汇！");
        else if(t > 0)
        {
            alert("发表留言成功");
            txtValidate.value = "";
            txtCommentBody.value = "";                   
            ShowShopMsg(_shopID,_shopOwnerID,_controlID);                    
        }
}
function changeControlStatus(disable)
{
    var c0 = $("btnCommitComment"), c1 = $("CheckCode"), c2 = $("Message_Content");
    var actionObj = $("h_action");
    var msgIDObj = $("h_msgID");
    var msgTitle=$("leave_message_title");
    var showMsgDIV=$("ShowLeaveMesageDiv");
    if(c0 != null)
        c0.disabled = disable;
    if(c1 != null)
        c1.disabled = disable;
    if(c2 != null)
        c2.disabled = disable;
    if(actionObj!=null)
        actionObj.value="";
    if(msgIDObj!=null)
        msgIDObj.value="";  
        
    if(msgTitle!=null)
       msgTitle.innerHTML="发表留言"; 
    if(disable == false)
    {      
       reloadValidateImage();   
    }
    if(showMsgDIV!=null)
       showMsgDIV.style.display="none";   
    
}

function reloadValidateImage()
{
    var img = $("imgValidate");
    if(img != null)
    {
        img.src = "../../../Passport/CheckCode.aspx?rnd=" + Math.random();
    }
}

function leaveShopMessage(ID,SenderName,e){     
   
     var actionObj=$("h_action");
     var msgIDObj=$("h_msgID");
     var msgTitleObj=$("leave_message_title"); 
     if(actionObj!=null && msgIDObj!=null)
     {
        msgTitleObj.innerHTML="回复 [<span style=\"color:red\">"+SenderName+"</span>] 留言：";           
        actionObj.value="replay";
        msgIDObj.value=ID;
        
     }
     ShowLeaveMesage('display',e);
//    var LeaveMessageObj=document.getElementById('ShowLeaveMesageDiv');
//    if(LeaveMessageObj!=null)
//    {
//         LeaveMessageObj.style.display="";    
//         var showDiv=e.target||e.srcElement;
//         var pos=getPosXY(showDiv,[-50,-220]);     
//         LeaveMessageObj.style.left=pos[0]+'px';
//	     LeaveMessageObj.style.top=pos[1]+'px';
//       
//       
//    }
 
   
}
// JScript 文件
function ShowEditMenu(e)
{
  var menuHtml="<ul id=\"EditMenu\">";
  menuHtml+="<li><a href=\"../../Manage/Shop_Setup.aspx\" target=\"_blank\">店铺设置</a></li>";
  menuHtml+="<li><a href=\"../../Manage/Shop_Product_Category.aspx\" target=\"_blank\">店铺分类</a></li>";
  menuHtml+="<li><a href=\"../../Manage/Shop_Product_Recommend.aspx\" target=\"_blank\">店铺推荐</a></li>";
  menuHtml+="<li><a href=\"../../Manage/Shop_Friend_Link.aspx\" target=\"_blank\">友情链接</a></li>";
  menuHtml+="<li><a href=\"../../Manage/Shop_Leave_Message.aspx\" target=\"_blank\">店铺留言</a></li>";
  menuHtml+="<li><a href=\"../../Manage/Shop_Style.aspx\" target=\"_blank\">店铺风格</a></li>";
  menuHtml+="</ul>";
  var menuObj=document.getElementById("ShopEditLinkDiv");
  if(menuObj!=null)
  {
     menuObj.innerHTML=menuHtml;         
     menuObj.style.display = "block"; 
     var showDiv=e.target||e.srcElement;
     var pos=getPosXY(showDiv,[-80,15]);     
     menuObj.style.left=pos[0]+'px';
	 menuObj.style.top=pos[1]+'px';

  }


}
 function ShowLeaveMesage(action,e)
  {
   
    var LeaveMessageObj=document.getElementById('ShowLeaveMesageDiv');
    if(LeaveMessageObj!=null)
    {
       switch(action)
       {
          case "display":
             LeaveMessageObj.style.display="";    
             var showDiv=e.target||e.srcElement;
             var pos=getPosXY(showDiv,[-50,-220]);     
             LeaveMessageObj.style.left=pos[0]+'px';
		     LeaveMessageObj.style.top=pos[1]+'px';
             break;
          case "close":
             LeaveMessageObj.style.display="none";
             break;
          
       }
       
       
    }
  }
  
  function getPosXY(a,offset)
  {
    var tn=null;
	var p=offset?offset.slice(0):[0,0];
	while(a)
	{
		tn=a.tagName.toUpperCase();
		
		p[0]+=a.offsetLeft-(tn=="DIV"&&a.scrollLeft?a.scrollLeft:0);
		p[1]+=a.offsetTop-(tn=="DIV"&&a.scrollTop?a.scrollTop:0);
		if(tn=="BODY")
		   break;
		a=a.offsetParent;
	}
	
	return p;
  }


