﻿// Base Scripts
// Scripts For Events And Mouse Positions
var offsetfrommouse=[15,5];			//object x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var Mouse_XPos;						// x position of mouse cursor
var Mouse_YPos;						// y position of mouse cursor
//===================================
//			All Initials
//===================================
//document object
function getDocumentBody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}
//Screen Width
function DocumentWidth()
{
	return document.all? getDocumentBody().scrollLeft + getDocumentBody().clientWidth : pageXOffset + window.innerWidth - 15;
}
//Screen Height
function DocumentHeight()
{
	return document.all? Math.min(getDocumentBody().scrollHeight, getDocumentBody().clientHeight) : Math.min(window.innerHeight);
}

// this function ads an event to a document element
function AddEvent(ObjectVar, EventName, EventFunction)
{
	if (ObjectVar.addEventListener)
	{
		ObjectVar.addEventListener(EventName, EventFunction, true);
		return true;
	}
	else if (ObjectVar.attachEvent)
	{
		var r = ObjectVar.attachEvent("on"+EventName, EventFunction);
		return r;
	}
	else
	{
		return false;
	}    
}

function RemoveEvent(ObjectVar, EventName, EventFunction)
{
	if (ObjectVar.removeEventListener)
	{
		ObjectVar.removeEventListener(EventName, EventFunction, false);
		return true;
	}
	else if (ObjectVar.detachEvent)
	{
		var r = ObjectVar.detachEvent("on"+EventName, EventFunction);
		return r;
	}
	else
	{
		return false;
	}
}

//initialize the variables and events
function DocInit()
{
	AddEvent(getDocumentBody(), "mousemove", MonitorMouse);
}

// monitoring the mouse position
function MonitorMouse(e)
{
    var evt = e || window.event; 
    // getting the event target object
    //var obj = evt.target || evt.srcElement 
    
    Mouse_XPos = evt.pageX ? evt.pageX : evt.clientX;
    Mouse_YPos = evt.pageY ? evt.pageY : evt.clientY;
    
}

// move the object near mouse cursor
function MoveToCursor(MObject, objectWidth, objectHeight)
{
	var xcoord=offsetfrommouse[0];
	var ycoord=offsetfrommouse[1];
	var docwidth = DocumentWidth();
	var docheight = DocumentHeight();
	
	if (docwidth - Mouse_XPos < objectWidth + 2*offsetfrommouse[0]){
		xcoord = Mouse_XPos - xcoord - objectWidth; // Move to the left side of the cursor
	} else {
		xcoord += Mouse_XPos;
	}
	if (docheight - Mouse_YPos < (objectHeight + 2*offsetfrommouse[1])){
		ycoord += Mouse_YPos - 8*ycoord - objectHeight;//Math.max(0,(2*offsetfrommouse[1] + objectHeight + Mouse_YPos - docheight));
	} else {
		ycoord += Mouse_YPos;
	}
	
	if (typeof window.event != "undefined")
	{
		xcoord += getDocumentBody().scrollLeft;
		ycoord += getDocumentBody().scrollTop;
	}

	MObject.left=xcoord+"px"
	MObject.top=ycoord+"px"
}

function MoveToCenter(MObject, objectWidth, objectHeight)
{
	var xcoord = 0;
	var ycoord = 0;
	var docwidth = DocumentWidth();
	var docheight = DocumentHeight();
	
	if(!objectWidth)
	{
		var objectWidth = MObject.style.width.toLowerCase().replace('px','');
	}
	if(!objectHeight)
	{
		var objectHeight = MObject.style.height.toLowerCase().replace('px','');
	}
	
	xcoord = docwidth/2 - objectWidth/2;
	ycoord = docheight/2 - objectHeight/2;
	
	
	if (typeof window.event != "undefined")
	{
		xcoord += getDocumentBody().scrollLeft;
		ycoord += getDocumentBody().scrollTop;
	}
	if(MObject.style)
	{
		MObject.style.left=xcoord+"px"
		MObject.style.top=ycoord+"px"
	}
	else
	{
		MObject.left=xcoord+"px"
		MObject.top=ycoord+"px"
	}
	

}










//=========================================================
//					CMS FUNCTIONS
//=========================================================

function OpenTree(TreeType, RelatedID)
{
	/*<Mojtaba>
	window.open('/Popups/Tree.aspx?TreeType=' + TreeType,'TreeView','width=400,height=500,scrollbars=1,status=0');
	</Mojtaba>*/
	//<Mojtaba>
	var path = '/Popups/Tree.aspx?TreeType=' + TreeType;
	if(RelatedID && RelatedID != '')
	{
		path += '&RelatedID=' + RelatedID;
	}
	window.open(path,'TreeView','width=400,height=500,scrollbars=1,status=0');
	//</Mojtaba>
}

function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();

}
var lang = 1;	// 1: Farsi, 0: English
// Farsi keyboard map based on ISIRI-2901
var farsikey = [
   0x0020, 0x0021, 0x061B, 0x066B, 0x00A4, 0x066A, 0x060C, 0x06AF,
   0x0029, 0x0028, 0x002A, 0x002B, 0x0648, 0x002D, 0x002E, 0x002F,
   0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
   0x0038, 0x0039, 0x003A, 0x0643, 0x003E, 0x003D, 0x003C, 0x061F,
   0x066C, 0x0624, 0x200C, 0x0698, 0x0649, 0x064D, 0x0625, 0x0623,
   0x0622, 0x0651, 0x0629, 0x00BB, 0x00AB, 0x0621, 0x004E, 0x005D,
   0x005B, 0x0652, 0x064B, 0x0626, 0x064F, 0x064E, 0x0056, 0x064C,
   0x0058, 0x0650, 0x0643, 0x062C, 0x005C, 0x0686, 0x00D7, 0x0640,
   0x200D, 0x0634, 0x0630, 0x0632, 0x064A, 0x062B, 0x0628, 0x0644,
   0x0627, 0x0647, 0x062A, 0x0646, 0x0645, 0x067E, 0x062F, 0x062E,
   0x062D, 0x0636, 0x0642, 0x0633, 0x0641, 0x0639, 0x0631, 0x0635,
   0x0637, 0x063A, 0x0638, 0x007D, 0x007C, 0x007B, 0x007E ];


function changeLang() {
    if (lang == 0) {
    lang = 1;
    return true;
  }
  else {
    lang = 0;
    return true;
  }
}

function KeyDown (evt){
 return true;
 var e = (typeof evt != "undefined") ? evt : window.event;
 var key = e.keyCode;
 var Ctrlkey = e.ctrlKey;
 var Shiftkey = e.shiftKey;
 if( Ctrlkey && Shiftkey) {
 //if (key == 145) {
    if (lang == 0) {
      lang = 1;
      return true;
    }
    else {
      lang = 0;
      return true;
    }

}
     
}
function KeyPress(evt) {
 return true;
 var e = (typeof evt != "undefined") ? evt : window.event;
 var key = e.keyCode;

  if (key == 13) { e.keyCode = 13; return true; }

   if (lang == 1) { // If Farsi
     if (key == 0x0020 && e.shiftKey) // Shift-space -> ZWNJ
       e.keyCode = 0x200C;
     else
       e.keyCode = farsikey[key - 0x0020];
     if (farsikey[key - 0x0020] == 92) {
        e.keyCode = 0x0698;
     }
     if (farsikey[key - 0x0020] == 8205) {
        e.keyCode = 0x067E;
     }
   }
   return true;
}

function change(obj){
return true;
if (lang==0)
lang=1
else
lang=0;
obj.focus();
}

function setEditorStyle(editor, args)
{
    var style = editor.get_contentArea().style;
    style.backgroundImage = "None";
    style.backgroundColor = "#FFFFFF";
    style.color= "#000000";
    style.fontFamily= "Tahoma";
}