﻿//****************** dragItems Section ************
function enableDraging(obj)
{
	if(obj.style.left ==  '')obj.style.left = event.clientX + parseInt(document.body.scrollLeft)-10;
	if(obj.style.top ==  '')obj.style.top = event.clientY + parseInt(document.body.scrollTop)-10;
	if(obj.resizeMode == true)
	{
	    if(obj.style.width ==  '')obj.style.width = (event.clientX + parseInt(document.body.scrollLeft))-parseInt(obj.style.left);
	    if(obj.style.width ==  '')obj.style.height = (event.clientY + parseInt(document.body.scrollTop))-parseInt(obj.style.top);
	}
	obj.style.position = 'absolute';
	obj.style.zIndex = typeof(document._oldClickedobject) !=  'undefined' ? document._oldClickedobject.style.zIndex + 1 : obj.style.zIndex + 1;
	obj.dragable = true;
    obj.mouseLeftPosition = event.clientX + parseInt(document.body.scrollLeft) - parseInt(obj.style.left);
    obj.mouseTopPosition = event.clientY + parseInt(document.body.scrollTop) - parseInt(obj.style.top);
	obj.oldcursor = obj.style.cursor;
	obj.style.cursor= obj.resizeMode == true ? 'nw-resize' : 'move';
	
	document.oldonmouseup = document.onmouseup;
	document.onmouseup = stopDrag;
	
	document.oldonmousemove = document.onmousemove;
	document.onmousemove = startDrag;
	
	document.onselectstart = function (){return false;}
	document._oldClickedobject = obj;
	dvPrintArea = document.getElementById('PrintArea');

}
function stopDrag(obj)
{
	if(obj == null)
		obj = document._oldClickedobject;
	if(obj == null)return;
	
	obj.resizeMode = false;
	if(!obj.IsMessageBox)
	    if(obj.oldborder)
	    {
	        obj.style.borderStyle = obj.oldborder.split('{}')[0];
            obj.style.borderColor = obj.oldborder.split('{}')[1];
            obj.style.borderWidth = obj.oldborder.split('{}')[2];
            obj.style.backgroundColor = obj.oldborder.split('{}')[3];
            obj.oldborder = null;
        }
    obj.style.cursor = obj.oldcursor;
    obj.dragable = false;
    obj.style.cursor = 'default';
    
	document.onmousemove = document.oldonmousemove;
	document.onmouseup = document.oldonmouseup;
	document.onselectstart = null;
}
var dvPrintArea = null;
function startDrag(obj)
{
	if(obj == null)
		obj = document._oldClickedobject;
	if(obj == null)return;

	if(!obj.dragable)return;
	obj.onselectstart = function (){return false;}
	MoveMyObj(obj);
}
var dvPrintArea = document.getElementById('PrintArea');

function MoveMyObj(obj)
{
    if(obj.resizeMode == true)
    {
        var width = event.clientX + parseInt(document.body.scrollLeft) - parseInt(obj.style.left) ;
        var height = event.clientY + parseInt(document.body.scrollTop) - parseInt(obj.style.top) ;
	    obj.style.width = width < 5 ? 5 : width ;
	    obj.style.height = height < 5 ? 5 : height ;
    }
    else
    {
        obj.style.left = event.clientX + parseInt(document.body.scrollLeft) - obj.mouseLeftPosition;
        obj.style.top = event.clientY + parseInt(document.body.scrollTop) - obj.mouseTopPosition;
	}
    if(dvPrintArea)
    {
        if(obj.resizeMode == true)
        {
            if(parseInt(obj.style.left) + parseInt(obj.style.width) >= parseInt(PrintArea.style.width))
                obj.style.width = parseInt(PrintArea.style.width) - parseInt(obj.style.left);
            if(parseInt(obj.style.top) + parseInt(obj.style.height) >= parseInt(PrintArea.style.height))
                obj.style.height = parseInt(PrintArea.style.height) - parseInt(obj.style.top);
        }
        else
        {
            if(parseInt(obj.style.left) + parseInt(obj.style.width) >= parseInt(PrintArea.style.width))
                obj.style.left = parseInt(PrintArea.style.width) - parseInt(obj.style.width);
            if(parseInt(obj.style.top) + parseInt(obj.style.height) >= parseInt(PrintArea.style.height))
                obj.style.top = parseInt(PrintArea.style.height) - parseInt(obj.style.height);
        }
    }
    if(parseInt(obj.style.left) <= 0)
        obj.style.left = 0;
    if(parseInt(obj.style.top) <= 0)
        obj.style.top = 0;
}
//******************************
function writeCookie(name, value)
{
//	var expire = "";
//    expire = new Date((new Date()).getTime() + 5000 * 3600000);
//    expire = "; expires=" + expire.toGMTString();

//  document.cookie = name + "=" + escape(value) + expire;

  document.cookie = name + "=" + escape(value);
}
//**********************************
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}
//**********************************
function SetFocusTo(targetId)
{
    if(event.keyCode == 13 & targetId != '')
        document.getElementById(targetId).focus();
    //event = null;
}
//**********************************
/******************************************************/
function ShowHide(tblBodySection,_checkbox)
{
    _checkbox = document.getElementById(_checkbox);
    tblBodySection = document.getElementById(tblBodySection);
    if(_checkbox == null)return;
    if(tblBodySection == null)return;
    if(_checkbox.checked)
        tblBodySection.style.display='';
    else
        tblBodySection.style.display='none'
}
/***********************************************/
function createPopup(url,argument,width,height,resizable,minimize,scroll)
{
   createPopupForm("modeless",url,argument,width,height,resizable,minimize,scroll)
}
function createModalPopup(url,argument,width,height,resizable,minimize,scroll)
{
   createPopupForm("modal",url,argument,width,height,resizable,minimize,scroll)
}
var PopReturnedValue=false;
function createPopupForm(windowType,url,argument,width,height,resizable,minimize,scroll)
{
   document.popArgsValue = argument;
   PopReturnedValue = false;
   //url = url + (url.indexOf("?") > -1 ? "&" : "?") + Math.random();
   var Args = 
        "dialogWidth:"+width+"px;"
       +"dialogHeight:"+height+"px;"
       +"dialogTop:"+((screen.height-height)/2)+"px;"
       +"dialogLeft:"+((screen.width-width)/2)+"px;"
       +"resizable:"+(resizable==1?"yes":"no")+";"
       +"minimize:" + (minimize==1?"yes":"no")+ ";"
       +"scroll:"+(scroll==1?"yes":"no")+";"
       +"status:no;"
       +"center:yes;"
       +"help:no;";

   if(windowType == "modal")
      PopReturnedValue = window.showModalDialog(rootFolder+'PopUp.htm?url='+url ,document ,Args);
   else if(windowType == "modeless")
   {
      if(window.showModelessDialog)
        window.showModelessDialog(rootFolder+'PopUp.htm?url='+url ,document ,Args);
      else
         window.showModalDialog(rootFolder+'PopUp.htm?url='+url ,document ,Args);      
   }
}
/*************************************************/
function pause(millis)
{
    var date = new Date();
    var curDate = null;
    do { curDate = new Date(); }
    while(curDate-date < millis);
}
/****************************************************/
var timeSpeed = 0;
var speed = 3;

function clickMenu(clickedMenu)
{
    while(clickedMenu.parentNode.tagName != "DIV")
        clickedMenu = clickedMenu.parentNode;
    clickedMenu = clickedMenu.parentNode.parentNode;
    for(var i=0;i<clickedMenu.childNodes.length;i++)
        if(clickedMenu.childNodes[i].tagName == "DIV")
            if(clickedMenu.childNodes[i].getAttribute('baseMode') == "consoleBody")
                menuContent = clickedMenu.childNodes[i];
    
    menuContent.lastOverFlow = menuContent.style.overflow;
    menuContent.style.overflow = "hidden";
    if(!menuContent.mainHeight)
        menuContent.mainHeight = menuContent.offsetHeight;
    try
    {
        menuContent.oPointer = menuContent.filters.item("DXImageTransform.Microsoft.Alpha");
    }
    catch(e)
    {
        menuContent.oPointer = document.createElement("div");
        menuContent.oPointer.opacity = 100;
    }
    if(menuContent.style.display == "none")
    {
        menuContent.style.display = 'block';
        if(!menuContent.mainHeight)
            menuContent.mainHeight = menuContent.offsetHeight;
        menuContent.oPointer.opacity = 0;
        expandMenu(menuContent.id);
    }
    else
        collapseMenu(menuContent.id);
}
function expandMenu(content)
{
    content = document.getElementById(content);
    if(content.oPointer.opacity >= 100)
    {
        /*content.className = "enabledMenuContent";*/
        content.oPointer.opacity = 100;
        clearTimeout(content.timer);
        content.style.overflow = content.lastOverFlow;
    }
    else
    {
        content.oPointer.opacity += speed;
        content.style.height = content.mainHeight * (content.oPointer.opacity) / 100 + "px";
        content.timer = setTimeout('expandMenu(\''+content.id+'\')',timeSpeed);
    }
}
function collapseMenu(content)
{
    content = document.getElementById(content);
    if(content.oPointer.opacity <= 0)
    {
        content.style.display = 'none';
        content.oPointer.opacity = 0;
        /*content.className = "disabledMenuContent";*/
        clearTimeout(content.timer);
    }
    else
    {
        content.oPointer.opacity -= speed;
        content.style.height = content.mainHeight * (content.oPointer.opacity) / 100 + "px";
        content.timer = setTimeout('collapseMenu(\''+content.id+'\')',timeSpeed);
    }
}
function getMouseX()
{
    return 100;//event.clientX + document.documentElement.scrollLeft;
}
function getMouseY()
{
    return 100;//event.clientY + document.documentElement.scrollTop;
}
function getLoadingHTML()
{
    return "<div class=loading><table><tr><td><img src='"+rootFolder+"Images/Loading.gif' /></td><td>در حال دریافت اطلاعات ...</td></tr></table></div>";
}
function getOffset(obj, dim) {
  var oLeft, oTop, oWidth, oHeight;
  if(dim == "left")
  {
    oLeft = obj.offsetLeft;
    while(obj.offsetParent!=null) 
    {    
      oParent = obj.offsetParent;
      oLeft += oParent.offsetLeft;
      obj = oParent;
    }
    return oLeft;
  }
  else if(dim=="top")
  {
    oTop = obj.offsetTop;
    while(obj.offsetParent!=null) 
    {
      oParent = obj.offsetParent;
      oTop += oParent.offsetTop;
      obj = oParent;
    }
    return oTop;
  }
  else if(dim=="width")
  {
    oWidth = obj.offsetWidth;
    return oWidth;
  }  
  else if(dim=="height")
  {
    oHeight = obj.offsetHeight;
    return oHeight;
  }    
  else
  {
    alert("Error: invalid offset dimension '" + dim + "' in getOffset()");
    return false;
  }
}
function getLocationWithWaiting(url)
{
    return rootFolder+"waiting.htm?"+ url;
}
function printDocument(preview)
{
    if(preview == null)
        window.print();
    else
    {
//        var OLECMDID = 7;
//        /* OLECMDID values:
//        * 6 - print
//        * 7 - print preview
//        * 1 - open window
//        * 4 - Save As
//        */
//        var PROMPT = 1; // 2 DONTPROMPTUSER
//        
//        var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
//        document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
//        WebBrowser1.ExecWB(OLECMDID, PROMPT);
//        WebBrowser1.outerHTML = "";
        var target = document.getElementById(preview);
        if(target)
        {
            var popup = window.createPopup()
            popup.document.body.innerHTML = target.innerHTML;
            popup.show(100, 100, 180, 25, document.body);
        }
    }
}
