var activeMenu = null;
var menuActive = 0;
var evaluator = null;
var currentsidemenu = 'menu';
var ToolTipExpandSize;

window.onload = init;

document.onclick = bodyClick
window.onresize = CalcDisplaySize;


function popUpProperties(obj) {
	op = window.open();
	op.document.open('text/plain');
	op.document.write('BEGIN\n');
	if(obj != null) {
	  for(objprop in obj) {
	    op.document.write(objprop + ' => ' + obj[objprop] + '\n');
	  }
	}
	op.document.write('END\n');
	op.document.close();
}

function CalcDisplaySize(){
	var width = document.body.offsetWidth;
	var actualwidth = 0;
	
	if(!document.all.SideNavHead) 
		return false;
		
	var navwidth = document.all.SideNavHead.style.width;
	if(navwidth.indexOf("px"))
		navwidth = navwidth.substring(0, navwidth.length - 2);
	if(navwidth == 10)
		actualwidth = width - 50;
	else
		actualwidth = width - 208;
	if(actualwidth < 600)
		document.all.content.style.width = 552;
	else
		document.all.content.style.width = actualwidth;
	document.all.tiptable.style.width = document.all.toptable.style.width;	
}

function bodyClick(){
  if(activeMenu){
    menuHide(activeMenu);    
  }
}

function fScrollIntoView(objToScrollTo){
  objToScrollTo.scrollIntoView(true);
}

function init(){
	if(document.all.SideNav){
    var maxHeight = (document.body.clientHeight > document.body.scrollHeight)?document.body.clientHeight-50:document.body.scrollHeight;
		content.style.height = SideNav.style.height = maxHeight-2;
    NavFooter.style.top = maxHeight - NavFooter.offsetHeight;
    footer.style.top = maxHeight;
    footer.style.visibility = 'visible';
  }

  if(document.all.TipBox && document.all.Header_lblTipTitle){
    var toolTip = document.all.Header_lblTipTitle;
    var rect = toolTip.getBoundingClientRect();  
    ToolTipExpandSize = rect.bottom - 23;
    if(ToolTipExpandSize > 22){
      toolTip.style.height = '15px';
      toolTip.style.overflow = 'hidden';
      document.all.Header_lblMore.style.visibility = 'visible';
      document.all.ToolTipImg.style.visibility = 'visible';
      
    }
  }
}

function ShowAboutPage() {
	var theTop = (screen.height / 2) - (300 / 2);
	var theLeft = (screen.width / 2) - (600 / 2);
	var params = 'resizable=no,scrollbars=no,height=300,width=600,top=' + theTop + ',left=' + theLeft;
	var about = "../includes/about_evolutra.aspx";
	window.open(about,'Evolutra',params);
}

function ShowHelpCenter(lang) {
  var helpWidthPercent = ((screen.width / 100) * 80).toFixed(0);
  var helpHeightPercent = ((screen.height / 100) * 80).toFixed(0);
  var fromLeft = ((screen.width - helpWidthPercent) - 11).toFixed(0);
  var params = 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=auto,resizable=yes,directories=no,location=no,height=' + helpHeightPercent + ',width=' + helpWidthPercent + ',top=0,left=' + fromLeft;
  var helpCenterMain = "../helpcenter/" + lang + "/index.html";
  window.open(helpCenterMain,'Help',params);
}

function menuTabClick(menu){
  menuActive = 1;
  if(evaluator != null){
      clearTimeout(evaluator);
  }
  if(menu.style.visibility != 'visible'){    
      var tab = menu.parentElement;
      if(menu.filters[1]){
        menu.filters[1].Apply();
        menu.filters[1].Play();
        menu.filters[2].Apply();
        menu.filters[2].Play();
      }

      if(activeMenu && activeMenu.id != menu.id){
        menuHide(activeMenu);    
      }

      activeMenu = menu;
      tab.className = 'menuTabOver';
      tab.style.zIndex = 2;
      menu.style.visibility = 'visible';
      menu.style.top = tab.offsetHeight+1;
      menu.style.borderTop = '0px';
      menu.style.zIndex = 2;
      menu.style.left = tab.offsetLeft + 12;
  } else {
    menuHide(activeMenu);
  }
  event.cancelBubble = true;
}

function menuTabOver(tab, menu){
  tab.className = 'menuTabOver';
  tab.style.zIndex = 2;
  if(activeMenu && activeMenu.id != menu.id){
    menuHide(activeMenu);
  }
}

function menuTabOut(menu){
  menuActive = 0;
  if(!menu.style.visibility || menu.style.visibility == 'hidden'){
    var tab = menu.parentElement;
    tab.style.zIndex = 0;
    tab.className = 'menuTab';
  }else{
    evaluator = setTimeout("btnOut()", 1000);
  }
}

function menuOver(){
  clearTimeout(evaluator);
  menuActive = 1;
  event.cancelBubble = true;

}
function menuOut(menu){ 
  menuActive = 0;
  evaluator = setTimeout("btnOut()", 1000);

}
function menuHide(){
  var menu = activeMenu;
  var tab = menu.parentElement;
  menu.style.visibility = 'hidden';
  tab.style.zIndex = 0;
  tab.className = 'menuTab';
}
function menuItemOver(menuItem){
  menuItem.className = 'menuItemOver';

}
function menuItemOut(menuItem){
  menuItem.className = 'menuItem';


}

function btnOut(){
  if(menuActive == 0)
    menuHide();
}

function ContentTableOver(tab){
    if(tab.className != 'ContentTabActive'){
        tab.style.height = '25px';
        tab.style.top = '1px';
    }
}

function ContentTableOut(tab){
    if(tab.className != 'ContentTabActive'){
        tab.style.height = '20px';
        tab.style.top = '5px';
    }
}

function buttonOut(btn){
  btn.style.border = '2px outset';
}
function buttonDown(btn){
  btn.style.border = '2px inset';
}

function jumpTo(link){
  top.location = link;
}

function AddFocus(control) {
  control.style.background = '#E1E1E1';
  control.style.color = '#000000';
}

function RemoveFocus(control) {
	control.style.background = '#FFFFFF';
	control.style.color = '#000000';
}

function BuildUserName(firstname, lastname, username) {
	username.value = firstname.value.substring(0, 1)
	username.value += lastname.value;
	username.value = username.value.toLowerCase();	
}

function TryNHide(AppPath){
  var sn = document.all.SideNav;
  var snh = document.all.SideNavHead;
  
  var nav = document.all.NavMenuItems;
  var tv = document.all.Header_CategoryTreeView;
  if(sn.className == 'SideNavHidden'){
    snh.style.width = '178px';    
    stretcher.style.width = '178px';
    stretcher2.style.width = '178px';
    snh.style.visibility = 'visible';
    snh.style.position = 'static';
		nav.style.width = '178px';  
		snh.style.borderBottom = 'none';
		
		if(tv) {  
			tv.style.width = '178px';
		}
		
		if(currentsidemenu == 'menu') {
			nav.style.visibility = 'visible';
			nav.style.position = 'static';
		} else {
			if(tv) { 
				tv.style.visibility = 'visible';
				tv.style.position = 'static';
			}
		}

    sn.className = 'SideNav';
    sn.style.height = document.body.scrollHeight;
    document.all.SideNavImg.src = AppPath + '/includes/_images/template/pi_hidenav.gif';
    document.all.SideNavImg.title = 'Hide Side Menu';
    document.cookie = "HideNav=False; path=/";
  }else{
    snh.style.width = '10px';  
    snh.style.borderBottom = '1px black solid';
		nav.style.width = '0px';
    stretcher.style.width = '0px';
    stretcher2.style.width = '0px';
		if(tv) { 
			tv.style.width = '0px'; 
		}

    if(sn.filters[0]){
      sn.filters[0].Apply();
    }
    sn.className = 'SideNavHidden';
    
    if(currentsidemenu == 'menu') {
			nav.style.visibility = 'hidden';
			nav.style.position = 'absolute';
		} else {
			if(tv) { 
				tv.style.visibility = 'hidden';
				tv.style.position = 'absolute';
			}
		}
		
    if(sn.filters[0]){
      sn.filters[0].Play();
    }       
    document.all.SideNavImg.src = AppPath + '/includes/_images/template/pi_shownav.gif';
    document.all.SideNavImg.title = 'Show Side Menu';
    document.cookie = "HideNav=True; path=/";
 }
 CalcDisplaySize();
}

function ShowCalendar(target, dateselected) {
	var theTop = (screen.height / 2) - (250 / 2);
	var theLeft = (screen.width / 2) - (250 / 2);
	var params = 'resizable=no,scrollbars=no,height=250,width=250,top=' + theTop + ',left=' + theLeft;		
	var target = "?target=" + target;
	var dateselected = "&dateselected=" + dateselected;
	var win = "../includes/calendar.aspx" + target + dateselected;
	var parent = window.open(win,'Calendar', params);
  parent.focus();
}

function HidePopup(popup){ 
  popup.style.visibility = 'hidden'; 
}

function ShowImageInfo(name, filename, desc, w, h, container){ 
  if(document.all.ImageName) document.all.ImageName.innerText = name; 
  if(document.all.FileName) document.all.FileName.innerText = filename;
  if(document.all.Description) document.all.Description.innerText = desc; 
  if(document.all.Dimensions) document.all.Dimensions.innerText = w + ' x ' + h; 

  var popup = document.all.ImgInfo; 
  if(popup.style.visibility != 'visible'){ 
    popup.style.visibility = 'visible'; 
    movePopup(popup);
  }
} 

function expandTip(){

  var div = document.all.TipBox;
  var div2 = document.all.TipBoxMore;

if(!div || !div2) 
{

return;
}

  div2.style.zIndex = div.style.zIndex = 6;
  div2.style.position = div.style.position = 'absolute';

  div2.style.height = div.style.height = ToolTipExpandSize;
	div2.style.top = div.style.top = "27px";

   var toolTip = document.all.Header_lblTipTitle;

  if(!toolTip) return;
  toolTip.style.height = '15px';
  toolTip.style.overflow = 'visible';
 
}
function hideTip(){
  var div = document.all.TipBox;
  var div2 = document.all.TipBoxMore;
  if(!div || !div2) return;
  div2.style.height = div.style.height = '22px';
  div2.style.zIndex = div.style.zIndex = 1;

  var toolTip = document.all.Header_lblTipTitle;
  if(!toolTip) retrn;
  toolTip.style.height = '15px';
  toolTip.style.overflow = 'hidden';
}

function movePopup(popup){
  popup.style.top = event.clientY+document.body.scrollTop+10;
  var popwidth = new Number(popup.style.width.slice(0, -2));
  if(event.clientX + popwidth > document.body.offsetWidth)
    popup.style.left = event.clientX-10-popwidth;
  else
	  popup.style.left = event.clientX+10+document.body.scrollLeft;
}


function closeNRefreshRent(){
	if(window.opener.__doPostBack) {
		window.opener.__doPostBack("lbtnSearch","");
	}	else {
		opener.location.reload(true);
	}
  self.close();
}

function ToggleNodes(node, image, isRoot) {
	if((node.style.display == "") || (node.style.display == "none")) {
		node.style.display = "block";
		if(isRoot)
			image.src = "../includes/_images/treeview/root_expanded.gif";
		else
			image.src = "../includes/_images/treeview/node_expanded.gif";
	} else {
		node.style.display = "none";
		if(isRoot)
			image.src = "../includes/_images/treeview/root_collapsed.gif";
		else
			image.src = "../includes/_images/treeview/node_collapsed.gif";
	}

	return false;
}

function ToggleMenu(enabledmenu, disabledmenu, button, button1) {
	if(currentsidemenu == 'menu') {
		currentsidemenu = 'tree';
		document.all.SideMenuButton.style.visibility = 'visible';
		document.all.SideMenuButton.style.position = 'static';		
		document.all.SideMenuButtonHighlighted.style.visibility = 'hidden';
		document.all.SideMenuButtonHighlighted.style.position = 'absolute';
		document.all.SideCategoryButtonHighlighted.style.visibility = 'visible';
		document.all.SideCategoryButtonHighlighted.style.position = 'static';		
		document.all.SideCategoryButton.style.visibility = 'hidden';
		document.all.SideCategoryButton.style.position = 'absolute';
	} else {
		currentsidemenu = 'menu';
		document.all.SideMenuButtonHighlighted.style.visibility = 'visible';
		document.all.SideMenuButtonHighlighted.style.position = 'static';		
		document.all.SideMenuButton.style.visibility = 'hidden';
		document.all.SideMenuButton.style.position = 'absolute';
		document.all.SideCategoryButton.style.visibility = 'visible';
		document.all.SideCategoryButton.style.position = 'static';		
		document.all.SideCategoryButtonHighlighted.style.visibility = 'hidden';
		document.all.SideCategoryButtonHighlighted.style.position = 'absolute';			
	}
	
	enabledmenu.style.visibility = 'visible';
	enabledmenu.style.position = 'static';
	disabledmenu.style.visibility = 'hidden';
	disabledmenu.style.position = 'absolute';	
}

function getAllowedClasses(arrCSS, arrAllowed, allowAll){
  var allowed = new Array();
  for (var i = 0; i < arrCSS.length; ++i){
    var stle = arrCSS[i];
    var dot_pos = stle.indexOf(".");
    var tag = stle.substring(0, dot_pos).toUpperCase();
    if((allowAll && dot_pos==0 && stle.indexOf(":")<0 && stle.indexOf(" ")<0) || (inArray(arrAllowed, tag, true) && stle.indexOf(":")<0 && stle.indexOf(" ")<0)){
      allowed.push(stle);
    }
  }
  return allowed;
}

function inArray(arrHaystack, needle, ignoreCase){
  if(ignoreCase) needle = needle.toUpperCase();
  for (var i = 0; i < arrHaystack.length; ++i){
    var check = (ignoreCase)?arrHaystack[i].toUpperCase():arrHaystack[i];
    if(check == needle) return true;
  }
  return false;            
}

function DropDownOver(dropdown){
  var pos = String(getAbs(dropdown)).split('-');;
  var x = pos[0];
  var y = parseInt(pos[1]) + 1;
  dropdown.style.overflow = 'visible';
  dropdown.style.border = '1px solid #e9e9e9';
  dropdown.style.filter = 'progid:DXImageTransform.Microsoft.Shadow(color=#777777,direction=150,strength=3)';
  dropdown.style.position = 'absolute';
  dropdown.style.left = x;
  dropdown.style.top = y;
}

function DropDownOut(dropdown){
  dropdown.style.overflow = 'hidden';
  dropdown.style.border = '1px solid #ffffff';
  dropdown.style.filter = '';
  dropdown.style.position = 'static';
}

function getAbs(element){
  var x = 0;
  var lastX = 0;
  var y = 0;
  var lastY = 0;
  while(element && element.id != 'content'){
    x += element.offsetLeft;
    y += element.offsetTop;
    if(element.offsetLeft) lastX = element.offsetLeft;
    if(element.offsetTop) lastY = element.offsetTop;
    element = element.offsetParent;
  }
  return (x-lastX) + '-' + (y - lastY);
}

function ShowEmail(username, password, senderror) {
	var theTop = (screen.height / 2) - (350 / 2);
	var theLeft = (screen.width / 2) - (400 / 2);
	var params = 'resizable=no,scrollbars=yes,height=350,width=400,top=' + theTop + ',left=' + theLeft;
	var path = "email_details.aspx?username=" + username + "&password=" + password + "&senderror=" + senderror;
	var popwin = window.open(path ,'Email',params);	
}

function stringTrim () {
  return this.replace(/^\s+/, '').replace(/\s+$/, '');
}
String.prototype.trim = stringTrim;

function ForceLogout() {
	self.close();
	opener.location.href = "../index.aspx";
}

function checkFileUp(s,e){
  var path = document.upload.txtFileUp.value;
  var filename = path.substring(path.lastIndexOf('\\')+1);
  var regex = /[^A-Za-z0-9_\.]/;
  return e.IsValid = !regex.test(filename); 
}

function AssignAllSelectedItems(src, dest, isdelete) {
  if(src==null || dest==null || !(src.selectedIndex>=0))
    return;

  for(i = 0;i<src.length;i++) {
    if(src.options[i].selected) {
      with(dest) {
        length++;
        options[options.length-1] = new Option(src.options[i].text,src.options[i].value);
      }

      if(isdelete) {
        src.options[i] = null;
        i = -1;
      }
    }
  }
}
