var IsVisible = 0;
var MouseX;
var MouseY;
var WindowWidth;
var WindowHeight;
var ContextMenuStatus='';
var IE = document.all?true:false;


function showContextMenu(type, strGuidObject, strGuidView,e) {
    setMousePosition(e); 
	qstr = 'type=' + type;
	qstr = qstr + '&obj=' + strGuidObject;
	qstr = qstr + '&view=' + strGuidView;
	var elementIframe = document.getElementById("iframeCM");
	elementIframe.src = 'ContextMenuPopup.aspx?' + qstr;
	elementIframe.allowTransparency=true;
	ContextMenuStatus = 'loading';
}

function showNavigationTree(e)
{
	setMousePosition(e);
	var PosX  = MouseX;
	var PosY = MouseY;
	var elementIframe = document.getElementById("iframeCM");j
	elementIframe.src = 'TreeNavigation.aspx';
	elementIframe.allowTransparency=true;
	var len = 255;
	if (PosX - len < WindowWidth)
		{elementIframe.style.left=PosX - len + 20;}
	else 
		{elementIframe.style.left=0;}
	elementIframe.style.top=PosY+10;
	elementIframe.style.width=len;
	elementIframe.style.height=505;
	elementIframe.style.visibility="visible";
	ContextMenuStatus='loaded';
}

function navigateFromTree(TargetUrl){
	var elementIframe = parent.document.getElementById("iframeCM");
	elementIframe.style.visibility="hidden";
	parent.location.href=TargetUrl;
}


function showContextMenuOnContextmenu(type, strGuidObject, strGuidView, e) {
	setMousePosition(e);
	cancelDefaultContextMenu();
	showContextMenu(type, strGuidObject, strGuidView,e);
}


function cancelDefaultContextMenu(){
	document.oncontextmenu = function () {return false;}
}

function ContextMenuLoaded(){
	if (ContextMenuStatus=='loading') {
		var PosX  = MouseX;
		var PosY = MouseY;
		var len = 120;
		if (len + PosX > WindowWidth){
			PosX = PosX - len - 25;}
		if (PosX < 0){PosX = 0;}
		var elementIframe = document.getElementById("iframeCM");
		elementIframe.style.left=PosX;
		elementIframe.style.top=MouseY-5;
		elementIframe.style.width=500;
		elementIframe.style.height=500;
		elementIframe.style.visibility="visible";
		ContextMenuStatus = 'loaded';
	}
}


function ContextMenu_NavigateFromChild(TargetUrl,mode){
		var elementIframe = parent.document.getElementById("iframeCM");
		elementIframe.style.visibility="hidden";
		if (mode==-1){
			parent.location.href=TargetUrl;}
		else if (mode==0){
			TargetUrl += '&mode=' + mode;
			window.open(TargetUrl);
			}
		else if (mode==1){
			TargetUrl += '&mode=' + mode;
			window.open(TargetUrl,"");
			}
		else if (mode==2){
			TargetUrl += '&mode=' + mode;
			window.open(TargetUrl,"","height=1200, width=800","resizable=yes","menubar=no","scrollbars=yes");
			}
		else if (mode==4){
			window.open(TargetUrl,"","height=1200, width=800","resizable=yes","menubar=no","scrollbars=yes");
			}
		else {			
			window.open(TargetUrl);
			}
}

function ContextMenu_SetMethodFromChild(GuidMethod, GuidObjectChild, InNewWindow){
	parent.document.Form1.hidden_GuidObjectChild.value = GuidObjectChild;
	if (InNewWindow == false){
		parent.document.Form1.hidden_GuidMethod.value = GuidMethod;
		parent.document.Form1.submit();
		hideContextMenu();}
	else {
		parent.document.Form1.hidden_GuidMethod.value = GuidMethod;
		parent.document.Form1.submit();
		hideContextMenu();
	}
}

function ContextMenu_SetMethod(GuidMethod, GuidObject, GuidObjectParent, InNewWindow){
	parent.document.Form1.hidden_GuidObject.value = GuidObject;
	parent.document.Form1.hidden_GuidObjectParent.value = GuidObjectParent;
	if (InNewWindow == false){
		parent.document.Form1.hidden_GuidMethod.value = GuidMethod;
		parent.document.Form1.submit();}
	else {
		parent.document.Form1.hidden_GuidMethod.value = GuidMethod;
		parent.document.Form1.submit();
		hideContextMenu();
		}
}

function ContextMenu_OpenReport(GuidMethod, GuidReport, GuidView) {
    parent.document.Form1.hidden_GuidView.value = GuidView;
    parent.document.Form1.hidden_GuidObjectParent.value = GuidReport;
    parent.document.Form1.hidden_GuidMethod.value = GuidMethod;
    parent.document.Form1.submit();
}

function ExecuteMethod(GuidMethod, GuidObjectChild){
	var element_Form = document.getElementById("Form1");
	var element_GuidObjectChild = document.getElementById("hidden_GuidObjectChild");
	var element_GuidMethod = document.getElementById("hidden_GuidMethod");
	if (element_GuidMethod == null){
		element_Form = parent.document.getElementById("Form1");
		element_GuidObjectChild = parent.document.getElementById("hidden_GuidObjectChild");
		element_GuidMethod = parent.document.getElementById("hidden_GuidMethod");
		}
	element_GuidObjectChild.value = GuidObjectChild;
	element_GuidMethod.value = GuidMethod;
	element_Form.submit();
	hideContextMenu();
}



function ExecuteMethodOnMain(GuidMethod, GuidObjectChild, InNewWindow){
	parent.frames["main"].document.Form1.hidden_GuidObjectChild.value = GuidObjectChild;
	if (InNewWindow == false){
		parent.frames["main"].document.Form1.hidden_GuidMethod.value = GuidMethod;
		parent.frames["main"].document.Form1.submit();}
	else {
		parent.frames["main"].document.Form1.hidden_GuidMethod.value = GuidMethod;
		parent.frames["main"].document.Form1.submit();
		parent.frames["main"].hideContextMenu();
	}
}		

function ContextMenu_NavigateNewWindow(TargetUrl){
	parent.document.all.iframeCM.style.visibility="hidden";
	window.open(TargetUrl);
}

function hideContextMenu() {
    var elementIframe = document.getElementById("iframeCM");
    if (elementIframe == null) { elementIframe = parent.document.getElementById("iframeCM"); }
	elementIframe.style.visibility="hidden";
}

function setMousePosition(e){
	if (IE) { // grab the x-y pos.s if browser is IE 
		MouseX = event.clientX + document.body.scrollLeft;
		MouseY = event.clientY + document.body.scrollTop;
		}
	else {  // grab the x-y pos.s if browser is NS
		MouseX = e.clientX + document.body.scrollLeft;
		MouseY = e.clientY + document.body.scrollTop;
		}
	document_setWindowSize(e);

}

function document_setWindowSize(e){
	if (IE) {WindowWidth = document.body.offsetWidth - 100;
		WindowHeight = document.body.offsetHeight - 100;
		}
	else {WindowWidth = window.innerWidth - 100;
		WindowHeight = window.innerHeight - 100;}
/*		
	document.Form1.hidden_WindowsWidth.value = WindowWidth;
	document.Form1.hidden_WindowsHeight.value = WindowHeight;
*/
}

function onTabSelected(sender, args) {
    var elementDiv = document.getElementById("hidden_GuidViewActiveTab");
    elementDiv.value = args.get_tab().get_index();
}


function TabChange(argsFromTab, argsToTab){
		document.Form1.hidden_GuidViewActiveTab.value = 'ActiveTab=' + argsToTab;
}

function NewWindow_FixedSize(strGuidObject, strGuidView, wmode, vmode){
//	mode:
//	Normal = 0
//	ValueTableOnly = 1
//	ValueTableOnlyPopup = 2
//	PrintPreview = 3

	var url;
	url = 'Main.aspx?'
	url = url + '&obj=' + strGuidObject;
		
	if (strGuidView!=''){url = url + '&view=' + strGuidView;}
	url = url + '&wmode=' + wmode + '&vmode=' + vmode;
	if (wmode<=1){window.open(url);}
	else {
		window.open(url,"","height=800, width=800","resizable=yes","menubar=no","scrollbars=yes");};
}
function OpenObject(strGuidObject, strGuidView) {
    //	mode:
    //	Normal = 0
    //	ValueTableOnly = 1
    //	ValueTableOnlyPopup = 2
    //	PrintPreview = 3

    var url;
    url = 'Main.aspx?'
    url = url + '&obj=' + strGuidObject;
    if (strGuidView != '') { url = url + '&view=' + strGuidView;}
    window.open(url);
}


function NewWindow(strGuidObject, strGuidView, wmode, vmode, w_width, w_hight) {
    //	mode:
    //	Normal = 0
    //	ValueTableOnly = 1
    //	ValueTableOnlyPopup = 2
    //	PrintPreview = 3

    var url;
    url = 'Main.aspx?'
    url = url + '&obj=' + strGuidObject;

    if (strGuidView != '') { url = url + '&view=' + strGuidView; }
    url = url + '&wmode=' + wmode + '&vmode=' + vmode;
    window.open(url, "", "height=" + w_hight + ",width=" + w_width + ",resizable=yes", "menubar=no", "scrollbars=yes");
}

function setCall(Name, NameRelationship, NameCompany, PhoneNumber, Caption){
	    x = screen.availWidth/2-400;
     y = screen.availHeight/2-200;
     
     var popupWindow = window.open('Empty.htm','','width='+400+',height='+200+',left='+x+',top='+y+',screenX='+x+',screenY='+y);
     popupWindow.document.write('<LINK href="GlobalStyle.css" rel="stylesheet">');
     popupWindow.document.title=(Name + ' - ' + Caption);
     popupWindow.document.write('<BODY Class = "PopUp">');
     popupWindow.document.write(Name + '<BR>' + Caption+ '<H1>' + PhoneNumber + '</H1>');
     popupWindow.document.write('</BODY>');
     hideContextMenu();
}

function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE 
	    MouseX = event.clientX + document.body.scrollLeft;
		MouseY = event.clientY + document.body.scrollTop;
		}
	else {  // grab the x-y pos.s if browser is NS
		MouseX = e.pageX
		MouseY = e.pageY
		}
	return
}

function closeParentContextMenu(){
	parent.document.all.iframeCM.style.visibility="hidden";
}

function window_width() {
  var myWidth = 0
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function window_height() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
   myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function setCheckboxValue(CheckBox){
	var idFrom = new String();
	idFrom = CheckBox.id;
	var idTo = new String();
	idTo = idFrom.replace(/_chk_/gi,'_chkh_');
	document.Form1.elements[idTo].value = CheckBox.checked;
}

function setGuidMethod(GuidMethod){
	document.Form1.hidden_GuidMethod.value = GuidMethod;
}

function setGuidMethodAfterUpdate(GuidMethod){
	document.Form1.hidden_GuidMethod.value = GuidMethod;
	document.Form1.submit();
}

function setGuidMethodOnEnter(GuidMethod, e){
	if(e.keyCode==13) {
		document.Form1.hidden_GuidMethod.value = GuidMethod;
	}
}



// CALLBACK //

 // doCallback is the function name set to BeforeClientClick.
 function doCallback(node)	{
		callback.MakeCallback('treeviewnode', node.ID);  // invoking of the callback. 
}

// TOOL TIP / CONTEXT MENU//
var pr_TimerTooltip;
var pr_ToolTipText;
var pr_showAfterMs;

function showToolTip_OnHover(e,TooltipText, showAfterMs){
	var elementDiv = document.getElementById("DivToolTip");
	if (elementDiv.style.visibility=="visible"){return;}
	if (!IE) {setMousePosition(e);} 	else {setMousePosition();}
	if (showAfterMs > 0){pr_showAfterMs = showAfterMs;} else{pr_showAfterMs = 500;}
	pr_ToolTipText=TooltipText;
	pr_TimerTooltip = setTimeout("showToolTip_OnTimer()", pr_showAfterMs);
}

function showToolTip_OnTimer() {
	var PosX  = MouseX+10;
	var PosY = MouseY;
	var elementDiv = document.getElementById("DivToolTip");
	var len = pr_ToolTipText.length * 6;
	var rows = pr_ToolTipText.length / 150;
	if (rows < 1){rows = 1;}
	if (len > 150){len = 150;}
	var height = rows * 15;
	if (elementDiv.style.visibility=="visible"){return;}
	if (len + PosX > WindowWidth){PosX = WindowWidth - len - 10}
	if (height + PosY > WindowHeight){PosY = WindowHeight - height - 10}
	if (PosX < 0){PosX = 0;}
	if (PosY < 0) { PosY = 0; }
	var elementTD = document.getElementById("TD_Tip");
	
	elementTD.innerHTML=pr_ToolTipText;
	elementTD.style.width=len;
	elementDiv.style.left=PosX;
	elementDiv.style.top=PosY;
	elementDiv.style.visibility="visible";
}

function showToolTip_OnClick(e,TooltipText){
	if (!IE) {setMousePosition(e);} else {setMousePosition();}
	var PosX  = MouseX+10;
	var PosY = MouseY;
	var elementDiv = document.getElementById("DivToolTip");
	var len = pr_ToolTipText.length * 6;
	var rows = pr_ToolTipText.length / 150;
	if (rows < 1){rows = 1;}
	if (len > 150){len = 150;}
	var height = rows * 15;
	if (elementDiv.style.visibility=="visible"){return;}
	if (len + PosX > WindowWidth){PosX = WindowWidth - len - 10}
	if (height + PosY > WindowHeight){PosY = WindowHeight - height - 10}
	if (PosX < 0){PosX = 0;}
	if (PosY < 0){PosY = 0;}
	var elementTD = document.getElementById("TD_Tip");
	elementTD.innerHTML=pr_ToolTipText;
	elementTD.style.width=len;
	elementDiv.style.left=PosX;
	elementDiv.style.top=PosY;
	elementDiv.style.visibility="visible";
}

function hideToolTip(){
	clearTimeout(pr_TimerTooltip)
	var elementDiv = document.getElementById("DivToolTip");
	elementDiv.style.visibility = "hidden";
}

var grid_cIndex = -1;
var grid_rIndex = -1;
var grid;


function grid_RowDblClick(sender, eventArgs) {

}

function grid_RowClick(sender, eventArgs) {
    
}

function grid_ColumnMouseOver(sender, eventArgs) {
    grid_cIndex = eventArgs.get_gridColumn().get_element().cellIndex;
}
function grid_ColumnMouseOut(sender, eventArgs) {
    grid_cIndex = -1;
}
function grid_RowMouseOver(sender, eventArgs) {

}
function grid_RowMouseOut(sender, eventArgs) {
    grid_rIndex = -1;
}
function grid_EditItem(sender, eventArgs) {
    grid = parent.document.Form1.hidden_CurrentGrid.value;
    grid_rIndex = parent.document.Form1.hidden_grid_rIndex.value;
    alert('row ' + grid_rIndex + ' / column' + grid_cIndex);
/*    var masterTable = grid.get_masterTableView();
    masterTable.editItem(masterTable.get_dataItems()[].get_element());
    */
}

/* RadRating */
function RadRating_SetValue(ID_RatingControl, ID_ValueControl) {
    var oRating = document.getElementById(ID_RatingControl);
    var value = oRating.get_value;
    var oValue = document.getElementById(ID_RatingControl);
    oValue.text = value;
}

/* Rad Combobox arguments */
function rad_OnClientItemsRequesting(sender, eventArgs)
 {
     var context = eventArgs.get_context();
     context["Filter"] = sender.get_attributes().getAttribute("Test");
 }
 
 
function RowContextMenu(sender, eventArgs) {
    alert('xxx');
 }    

/* Rad Grid */
function grid_EditAllRecords(RadGrid1_ClientID) {
    var masterTable = $find("<%= " + RadGrid1_ClientID + "%>").get_masterTableView();
    masterTable.editAllItems();
} 

/* Toolbar */
 function Toolbar_onButtonClicked(sender, args)
{
    var RadGrid1_ClientID = args.get_item().get_commandArgument();
    var masterTable = $find(RadGrid1_ClientID).get_masterTableView();
    masterTable.editAllItems();

}


