﻿
 var MSG_BackgroundElement = document.createElement('div');
 var MSG_ForegroundElement = document.createElement('div');
 var OKBtn="确定";
 var CancelBtn = "取消";
 var YesBtn = "是";
 var NoBtn = "否";
 var MSG_Return_Value = "";
 var MSG_Call_Back_Function;
 var MSG_Call_Back_Args ;
 var ox;
 var oy;
 var md =false;
 var Msg_Sender = null;
 var Msg_Event = null;
 var Message_IsShow = false;
 var Message_Type ={"question2":-1,"alert":0,"confirm":1,"question":2,"comment":3,"required":4};
 var Element_Top =0;
 var Element_Left =0;
 var Msg_Comment_Requeired = false;
 var _Msg_HTML=null;
 var _Msg_content ="";
 var _Msg_Error= null;
var MessageBox_Title = "家政服务综合管理系统";
var _Msg_Width;
var Msg_Width_Init = 300;
var MSG_LEFT =null;
var MSG_TOP = null;
//add by ari.he,used as a flag for initialize.
var bInitialized=false;
var _FocusCallBackFunction;
 
String.prototype.Len2 = function()
 {    
	 var i,str1,str2,str3,nLen;
	 str1 = this;
	 nLen = 0;	 
	 for(i=1;i<=str1.length;i++) {
		str2=str1.substring(i-1,i)
		str3=escape(str2);
		if(str3.length>3){
			nLen = nLen + 1.9;
		}else {
			nLen = nLen + 1;
		}
	 }
	 return nLen;	
 }
 
 function addEvent(element,eventName,eventMethod)
 {
    if(document.attachEvent)  element.attachEvent('on'+eventName,  eventMethod);  
   else  element.addEventListener(eventName,  eventMethod,  false);  
 }
 function removeEvent(element,eventName,eventMethod)
 {
    if(document.detachEvent)  element.detachEvent('on'+eventName,  eventMethod);  
   else  element.removeEventListener(eventName,  eventMethod,  false);  
 } 
 function Message_Layout()
 {
 //debugger;
  var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
  var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);  

  var clientWidth;
  var clientHeight;
  var clientHeight1;
  var clientWidth1;
  
  if(navigator.userAgent.indexOf("MSIE")!=-1)
  {
     clientWidth = document.documentElement.clientWidth;
	 clientHeight = document.documentElement.clientHeight;
	 }
  else if(navigator.userAgent.indexOf("Firefox")!=-1)
  {
    clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
	clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
  }  
 
  MSG_BackgroundElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth)+'px';
  MSG_BackgroundElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight)+'px';
  clientHeight1 = clientHeight;
 clientWidth1 =clientWidth;
  if(parent!=window &&parent.document.body&&parent.document.body.tagName =="BODY"){
  
      scrollLeft = (parent.document.documentElement.scrollLeft ? parent.document.documentElement.scrollLeft : parent.document.body.scrollLeft);
      scrollTop = (parent.document.documentElement.scrollTop ? parent.document.documentElement.scrollTop : parent.document.body.scrollTop);
      if(navigator.userAgent.indexOf("MSIE")!=-1)
      {
         clientWidth = parent.document.documentElement.clientWidth;
	     clientHeight =parent.document.documentElement.clientHeight;
	     }
      else if(navigator.userAgent.indexOf("Firefox")!=-1)
      {
        clientWidth = Math.min(parent.innerWidth, parent.document.documentElement.clientWidth);
	    clientHeight = Math.min(parent.innerHeight,parent.document.documentElement.clientHeight);
      }
      
  }     
  if(MSG_TOP!=null)
  { MSG_ForegroundElement.style.top =scrollTop+MSG_TOP+"px";}
  else{
      if(clientHeight1>=clientHeight){

      MSG_ForegroundElement.style.top = Math.max(0, scrollTop+((clientHeight-200)/2))+'px';
      
      }else
      {
     
      MSG_ForegroundElement.style.top = Math.max(0,(clientHeight1-200)/2)+'px';
      }
  }
    if(MSG_LEFT!=null)
  { MSG_ForegroundElement.style.left =scrollLeft+MSG_LEFT;}
  else{

      if(clientWidth1<=300)
      {
      
      MSG_ForegroundElement.style.left ="2px";
      }
      else 
      if(clientWidth1>=clientWidth){
      MSG_ForegroundElement.style.left =  Math.max(0,scrollLeft+((clientWidth-_Msg_Width)/2))+'px';
      }else
      {
      MSG_ForegroundElement.style.left = Math.max(0,(clientWidth1-_Msg_Width)/2)+'px';
      }
   }
  
  

 }
 function Message_Init(){ 
  //add by ari.he
  if(bInitialized) return ;
  bInitialized=true;
  
  MSG_BackgroundElement.className = "gis-message-page-background";
  MSG_BackgroundElement.style.display = 'none';
  MSG_BackgroundElement.style.position = 'absolute';
  MSG_BackgroundElement.style.left = '0px';
  MSG_BackgroundElement.style.top = '0px'; 
  MSG_BackgroundElement.style.zIndex = 10000;  
  MSG_ForegroundElement.style.zIndex = 10001; 
  // Message_Layout();
  MSG_ForegroundElement.className = "gis-message-page-foreground";
  MSG_ForegroundElement.style.display = 'none';
  MSG_ForegroundElement.style.position = 'absolute';
   
 
  var body = document.getElementsByTagName('body')[0];
  body.appendChild(MSG_BackgroundElement);
  body.appendChild(MSG_ForegroundElement);
 
   var clientWidth;    
      if(navigator.userAgent.indexOf("MSIE")!=-1)     clientWidth = document.documentElement.clientWidth;	
     else if(navigator.userAgent.indexOf("Firefox")!=-1) clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
	/* if(clientWidth<=400)
      {  */
      Msg_Width_Init = clientWidth * 0.85;
      //}
 }
 function Message_Show(msgLeft,msgContent,msgType,msgTop,callBackFunction)
 {        
//   debugger 
    if(typeof(msgLeft)=="number")
         MSG_LEFT =msgLeft;
    else
         MSG_LEFT =null;
      if(typeof(msgTop)=="number")
             MSG_TOP =msgTop;
       else
        MSG_TOP =null;
      Element_Left =(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
      Element_Top = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);    
 
      MSG_ForegroundElement.innerHTML=Message_Html('',msgContent,msgType,'');      
      Message_Layout();     
      MSG_BackgroundElement.style.display = '';
      MSG_ForegroundElement.style.display = '';
      //debugger;
      MsgForIE6(MSG_BackgroundElement);
     // ForIE6(MSG_ForegroundElement);
      Message_IsShow = true;
      MSG_Call_Back_Function = callBackFunction;
      MSG_Call_Back_Args = new Array();	  
	 for ( var i = 5;i<Message_Show.arguments.length;i++)
	    {
	        MSG_Call_Back_Args.push(Message_Show.arguments[i]);
	    }
	// debugger;  
	if(	 document.getElementById('txtContent_Msg'))
	   document.getElementById('txtContent_Msg').focus();
	else if(	 document.getElementById('btnOK_Msg'))
    	document.getElementById('btnOK_Msg').focus();
  addEvent(window, 'resize', Message_Layout);
  addEvent(window, 'scroll', Message_Layout);
  addEvent(document, 'mousedown', document_onmousedown);
  addEvent(document, 'mouseup', document_onmouseup);
  addEvent(document, 'mousemove', document_onmousemove);
 
  //window.scrollTo(Element_Left,Element_Top);
   if(typeof(ResizeFrame)!='undefined') ResizeFrame();

  return false; 
 }
 function Message_Html(msgTitle,msgContent,msgType,imgSrc)
 {
// debugger;
if(msgType == "comment"|| msgType == 'required'){
  _Msg_Width = Math.min(300,Msg_Width_Init);
}
else{
   _Msg_Width = Math.min(msgContent.Len2(),70)*7+50;
   if(_Msg_Width >Msg_Width_Init)
    _Msg_Width = Msg_Width_Init;
    }
   msgTitle =MessageBox_Title; 
    Msg_Comment_Requeired = false;
    if(msgType == 'required') Msg_Comment_Requeired = true;
 
     if(imgSrc==null || imgSrc =="")
        { if(msgType=="confirm"||msgType=="question")
            imgSrc=ImgPath+"confirm2.gif";
            else
            imgSrc=ImgPath+"confirm2.gif";
        }
    
    var OutStr="<table calss='gis-message-page-foreground gis-row-inner-table' style='font-family: Verdana, Helvetica, Arial;font-size: 9pt;'><tr><td style='vertical-align:middle;'><div style='font-size: 9pt;vertical-align:middle; width:"+_Msg_Width+"px;text-align:left;cursor:move;BACKGROUND-COLOR: #42609A;font-weight: bold;' canmove='true'><font color='#FFFFFF' size='2'>"+msgTitle+"</font></div>"
   // debugger;
    if(Message_Type[msgType]<3)
    {
     OutStr=OutStr+"<div class ='gis-message-row' style=\"height:auto;width:"+_Msg_Width+"px;\"> <table calss='gis-row-inner-table' ><tr><td width='30px'style='text-align:left;vertical-align:top;'><img alt='' src='"+imgSrc+"'></td><td style='text-align:left;width:"+(_Msg_Width-30)+"px;'><font color='black'>"+msgContent+"</font></td></tr></table></div>"
     if(msgType=="confirm")
    		OutStr=OutStr+"<div class ='gis-message-row'style=\"text-align:right;width:"+_Msg_Width+"px;\"><input id='btnOK_Msg' type=\"button\" value=\""+OKBtn+"\"  onclick=\"Message_Hide('Y')\" class='com_btn2' >&nbsp;&nbsp;<input type=\"button\" value=\""+CancelBtn+"\" onclick=\"Message_Hide('C')\" class='com_btn2' ></div>"
    	
     else if(msgType=="alert")
    		OutStr=OutStr+"<div class ='gis-message-row'style=\"text-align:right;width:"+_Msg_Width+"px;\"><input id='btnOK_Msg' type=\"button\" value=\""+OKBtn+"\" onclick=\"Message_Hide('OK')\" class='com_btn2' ></div>"
    	
     else if (msgType=="question")		
			OutStr=OutStr+"<div class ='gis-message-row'style=\"text-align:right;width:"+_Msg_Width+"px;\"><input id='btnOK_Msg' type=\"button\" value=\""+YesBtn+"\" onclick=\"Message_Hide('Y')\" class='com_btn2' >&nbsp;&nbsp;<input type=\"button\" value=\""+NoBtn+"\"class='com_btn2'  onclick=\"Message_Hide('N')\" >&nbsp;&nbsp;<input type=\"button\" value=\""+CancelBtn+"\" onclick=\"Message_Hide('C')\" class='com_btn2' ></div>"
	 else if (msgType=="question2")		
			OutStr=OutStr+"<div class ='gis-message-row'style=\"text-align:right;width:"+_Msg_Width+"px;\"><input id='btnOK_Msg' type=\"button\" value=\""+YesBtn+"\" onclick=\"Message_Hide('Y')\" class='com_btn2'>&nbsp;&nbsp;<input type=\"button\" value=\""+NoBtn+"\" onclick=\"Message_Hide('N')\" class='com_btn2' ></div>"
	  
	         	
    }    
    else if(msgType == "comment"|| msgType == 'required')
    {
      
        OutStr=OutStr+"<div class ='gis-message-row' style=\"width:"+_Msg_Width+"px;height:65px;\"> <table calss='gis-row-inner-table'><tr><td width='50px'style='text-align:left;'><font color='black'>"+msgContent+"</font></td><td style='text-align:center;width:250px;'><textarea id='txtContent_Msg'  style='width:200px;overflow:auto;height:50px;'onfocus='TxtFocus();' onblur='MSG_Return_Value=this.value;'></textarea></td></tr></table></div>"
        OutStr=OutStr+"<div class ='gis-message-row'style=\"text-align:right;width:"+_Msg_Width+"px;\"><input type=\"button\" id='btnOK_Msg' value=\""+OKBtn+"\" onclick=\"Message_Hide(MSG_Return_Value)\" class='gis-message-button'>&nbsp;&nbsp;<input type=\"button\" value=\""+CancelBtn+"\" onclick=\"Message_Hide('C')\" class='com_btn2'></div>"    	
    }
    _Msg_Content =msgContent;
    
    	OutStr=OutStr+"<div class ='gis-message-row'style=\"text-align:right;width:"+_Msg_Width+"px;height:5px\"><span>&nbsp;&nbsp;</span>"
	OutStr=OutStr+"</td></tr></table>";
	return OutStr;
 }
 function Message_Hide(value)
 { 
   MSG_Return_Value = value;   
//   removeEvent(window, 'resize', Message_Layout);
//   removeEvent(window, 'scroll', Message_Layout); 
   if(Msg_Comment_Requeired && MSG_Return_Value.replace(/(^[\s]*)|([\s]*$)/g, "")=="") 
   {      
       if(_Msg_HTML == null){
           _Msg_HTML = MSG_ForegroundElement.innerHTML;
           if(_Msg_Error ==null)
                      MSG_ForegroundElement.innerHTML += _Msg_Content+" is required, Please try again!"  
           else
              MSG_ForegroundElement.innerHTML += _Msg_Error;
       }  
       setTimeout(" if(_Msg_HTML){MSG_ForegroundElement.innerHTML=_Msg_HTML;_Msg_HTML=null;}",2000);
      
   if(typeof(ResizeFrame)!='undefined') ResizeFrame();
 
       return;
   }   
    
  MSG_BackgroundElement.style.display = 'none';	
  MSG_ForegroundElement.style.display = 'none';  
 //ForIE6(MSG_ForegroundElement); 
 MsgForIE6(MSG_BackgroundElement); 
  if(Message_IsShow)
  {       
      if(Message_Layout)
         removeEvent(window, 'resize', Message_Layout);         
      if(Message_Layout)
       removeEvent(window, 'scroll', Message_Layout); 
      removeEvent(document, 'mousedown', document_onmousedown);
      removeEvent(document, 'mouseup', document_onmouseup);
      removeEvent(document, 'mousemove', document_onmousemove);
   
	  Message_IsShow = false;
  }  
   window.scrollTo(Element_Left,Element_Top);
   if(MSG_Call_Back_Args)
            MSG_Call_Back_Args.push(MSG_Return_Value);    
    if(MSG_Call_Back_Function!=null)
       // MSG_Call_Back_Function.apply(window,MSG_Call_Back_Args);	
      MSG_Call_Back_Function.apply(window,MSG_Call_Back_Args); 
    MSG_Return_Value="";
//    debugger ;
    if(_FocusCallBackFunction!=null) _FocusCallBackFunction();   
 }
 
var document_onmousedown=function(ev)
     	{
			var ev=ev||window.event;
			var evt=ev.srcElement||ev.target;
			if(!(evt.getAttribute)) return;
         	if(typeof(evt.getAttribute("canmove"))=="undefined")
         	{
            	 return;
         	}
         	if(evt.getAttribute("canmove"))
         	{
             	md = true;
             	ox = MSG_ForegroundElement.offsetLeft - ev.clientX;
             	oy = MSG_ForegroundElement.offsetTop - ev.clientY;
         	}
     	}
 var document_onmouseup= function(){md=false;}
 var document_onmousemove= function(ev)
     	{
			var ev=ev||window.event;
         	if(md)
         	{
             	MSG_ForegroundElement.style.left= (ev.clientX +ox)+"px";
             	MSG_ForegroundElement.style.top= (ev.clientY +oy)+"px";
         	}
     	}
addEvent(window, 'load', Message_Init);
//addEvent(document, 'mousedown', document_onmousedown);
//addEvent(document, 'mouseup', document_onmouseup);
//addEvent(document, 'mousemove', document_onmousemove);
function TxtFocus()
{
 if(_Msg_HTML)
 {
     MSG_ForegroundElement.innerHTML=_Msg_HTML;
     _Msg_HTML=null;
     document.getElementById('txtContent').focus();
 }
}

function MsgForIE6(elt)
{

 if(navigator.userAgent.indexOf("MSIE 6")!=-1||navigator.userAgent.indexOf("Firefox")!=-1) 
     {          
                var childFrame = elt._hideWindowedElementsIFrame;
                if (!childFrame) {                    
                    childFrame = document.createElement("iframe");
                    childFrame.src = "javascript:'<html></html>';";
                    childFrame.style.position = "absolute";
                    childFrame.style.display = "none";
                    childFrame.scrolling = "no";
                    childFrame.frameBorder = "0";
                    childFrame.tabIndex = "-1";
                    childFrame.id = "IframeForIE6";
                    childFrame.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
                    elt.parentNode.insertBefore(childFrame, elt);                                   
                    elt._hideWindowedElementsIFrame = childFrame;
            }

           // CommonToolkitScripts.setBounds(childFrame, newPosition);
            childFrame.style.left =elt.style.left;
            childFrame.style.top =elt.style.top;
            childFrame.style.width =elt.style.width;
            childFrame.style.height =elt.style.height;            
            childFrame.style.display = elt.style.display;
            if (elt.style.zIndex) {
                childFrame.style.zIndex = elt.style.zIndex;
            }
        }
}
