var popMenuTemplate = '<table cellspacing="0" cellpadding="0" border="0"><tr id="POPM_ID">    <td width="9" height="21"><img src="images/interface/popmenu/sp-titlebar-top-left.gif" width="9" height="21" alt="" border="0"></td>    <td background="images/interface/popmenu/sp-titlebar-top-bg.gif" style="background-repeat:repeat-x;font-weight:bold;background-color:silver" align="center">POPM_TOP</td>    <td width="11" height="21"><img src="images/interface/popmenu/sp-titlebar-top-right.gif" width="11" height="21" alt="" border="0"></td></tr><tr>    <td width="9" height="5"><img src="images/interface/popmenu/sp-titlebar2-top-left.gif" width="9" height="5" alt="" border="0"></td>    <td height="5" background="images/interface/popmenu/sp-titlebar2-top-bg.gif" style="background-repeat:repeat-x"><img src="images/interface/pixel.gif" width="1" height="5" border="0" alt=""></td>    <td width="11" height="5"><img src="images/interface/popmenu/sp-titlebar2-top-right.gif" width="11" height="5" alt="" border="0"></td></tr><tr>    <td width="9" background="images/interface/popmenu/sp-left.gif" style="background-repeat:repeat-y"><img src="images/interface/pixel.gif" width="9" height="1" border="0" alt=""></td>    <td style="background-color:#E4E4E4;padding-left:20px;padding-right:20px;padding-top:10px" align="center">POPM_MIDDLE</td>    <td width="11" background="images/interface/popmenu/sp-right.gif" style="background-repeat:repeat-y"><img src="images/interface/pixel.gif" width="9" height="1" border="0" alt=""></td></tr><tr>    <td width="9" height="44"><img src="images/interface/popmenu/sp-bottom-left.gif" width="9" height="44" alt="" border="0"></td>    <td height="44" background="images/interface/popmenu/sp-bottom-bg.gif" style="background-repeat:repeat-x;background-color:whitesmoke" align="center">POPM_BOTTOM</td>    <td width="11" height="44"><img src="images/interface/popmenu/sp-bottom-right.gif" width="11" height="44" alt="" border="0"></td></tr></table>';

function preloadAllImages() {
var imgdir = 'images/interface/';
preloadImg(imgdir+'popmenu/sp-bottom-bg.gif');
preloadImg(imgdir+'popmenu/sp-bottom-left.gif');
preloadImg(imgdir+'popmenu/sp-bottom-right.gif');
preloadImg(imgdir+'popmenu/sp-left.gif');
preloadImg(imgdir+'popmenu/sp-right.gif');
preloadImg(imgdir+'popmenu/sp-titlebar2-top-bg.gif');
preloadImg(imgdir+'popmenu/sp-titlebar2-top-left.gif');
preloadImg(imgdir+'popmenu/sp-titlebar2-top-right.gif');
preloadImg(imgdir+'popmenu/sp-titlebar-top-bg.gif');
preloadImg(imgdir+'popmenu/sp-titlebar-top-left.gif');
preloadImg(imgdir+'popmenu/sp-titlebar-top-right.gif');
preloadImg(imgdir+'hourglass.gif');
preloadImg(imgdir+'dropdown-dot.gif');
preloadImg(imgdir+'iconbg.gif');
};

function clipSelected() {
	clipboard.items.length = 0;
	clipboard.fromtabid = AfianHandler.activeTab.id;
	clipboard.fromtabbasepath = AfianHandler.activeTab.basepath;
	if (AfianHandler.activeTab.uid) {
	clipboard.fromuid = AfianHandler.activeTab.uid;
	}
	for (var i = 0 ; i < AfianHandler.activeTab.items.length; i++) {
		if (AfianHandler.activeTab.items[i] != undefined) {
			if (AfianHandler.activeTab.items[i].selected) {
				clipboard.items[clipboard.items.length] = AfianHandler.activeTab.items[i].input_value;
			}
		}
	}
	if (AfianHandler.activeTab.section != 'browseshare') {
	getById('MyFilesPasteButton').style.visibility='visible';
	} else {
	getById('BSharePasteButton').style.visibility='visible';
	}
	AfianHandler.activeTab.unselectAll();
}

function resetClipboard() {
	clipboard.from = false;
	clipboard.to = false;
	clipboard.items = new Array;
	clipboard.length = 0;
	clipboardAction = 'copy';
	clipboard.fromuid = false;
}

function fillHiddenFormItemList() {
	document.hiddenForm.itemlist.value = '';
	for (var i = 0 ; i < AfianHandler.activeTab.items.length; i++) {
		if (AfianHandler.activeTab.items[i] != undefined) {
			if (AfianHandler.activeTab.items[i].selected) {
				document.hiddenForm.itemlist.value += '|'+encodeURIComponent(AfianHandler.activeTab.items[i].input_value);
			}
		}
	}
}

function showMenu(menuId) {
		if (activeMenu) {
			hideMenu(activeMenu);
		}
	meniu = getById(menuId);
	scroll_left = getScrollLeft();
	scroll_top = getScrollTop();
	if (meniu) {
		var leftPos = parseInt(currentX+20);
		var topPos = parseInt(currentY+15);
		if (leftPos+meniu.offsetWidth > getBodyW()+scroll_left-25) {
			leftPos = leftPos+((getBodyW()+scroll_left)-(leftPos+meniu.offsetWidth+25));
		}
		if (topPos+meniu.offsetHeight > getBodyH()+scroll_top-10) {
			topPos = topPos+((getBodyH()+scroll_top)-(topPos+meniu.offsetHeight)-10);
		}
		
		meniu.style.visibility = 'visible';
		meniu.style.left = leftPos;
		meniu.style.top = topPos;
		zindexcount = zindexcount++;
		meniu.style.zIndex = zindexcount;
		hideStupidMSIEElements(meniu);
		if (menuId != 'popmenu-alert' && menuId != 'popmenu-prompt' && menuId.indexOf('context') == -1) {
		activeMenu = menuId;
		}
	}
}

function hideMenu(menuId) {
	getById(menuId).style.visibility='hidden';
	showStupidMSIEElements();
}

function centeredMenu(menuId) {
	if (activeMenu) {
		hideMenu(activeMenu);
	}
	scroll_left = getScrollLeft();
	scroll_top = getScrollTop();
	meniu = getById(menuId);
	leftPos = (getBodyW()/2)-(meniu.offsetWidth/2);
	topPos = (getBodyH()/2)-(meniu.offsetHeight/2);
	if (meniu.offsetHeight > getBodyH()) {
		topPos = 20;
	}
	meniu.style.left = leftPos;
	meniu.style.top = topPos+scroll_top;
	meniu.style.visibility='visible';
	zindexcount = zindexcount++;
	meniu.style.zIndex = zindexcount;
	hideStupidMSIEElements(meniu);
	if (menuId != 'popmenu-alert' && menuId != 'popmenu-prompt') {
		activeMenu = menuId;
	}
}

function getBreadCrumbs(startHTML, path, endHTML) {
	str = '';
	splited = path.split('/');
	pt = '';
	if (splited.length == 1) {
		return path;
	} else {
	for (var i = 0 ; i < splited.length; i++) {
		pt = gluePath([pt, splited[i]]);
		str += startHTML+splited[i]+endHTML;
		str = str.replace(/PATH/g, pt);
		if (splited[i+1] != undefined) {
			if (splited[i+1].substring(0, 1) != '/') {
				str += '/';
			}
		}
	}
	return str;
	}
}

function btnOnMUp() {this.className='button';}
function btnOnMDown() {this.className='pushedbutton';}
function btnOnMOut() {this.className='button';}
function buttonize() {
	buttons = document.getElementsByTagName('TABLE');
	for (var i = 0 ; i < buttons.length; i++) {
		if (buttons[i].className == 'button') {
			histd = buttons[i];
			histd.onmouseup = btnOnMUp;
			histd.onmousedown = btnOnMDown;
			histd.onmouseout = btnOnMOut;
		}
	}
}
function styleDisableButton(id) {
	if (!AfianUserPermissions['ui_has_tree']) {
		return true;
	}
	btn = getById(id);
	if (is.IE) {
		btn.disabled=true;
	} else {
		btn.style.color='gray';
	}
}
function styleEnableButton(id) {
	if (!AfianUserPermissions['ui_has_tree']) {
		return true;
	}
	btn = getById(id);
	if (is.IE) {
		btn.disabled=false;
	} else {
		btn.style.color='black';
	}
}

function toggleView(how) {
	if (AfianHandler.activeTab.section == 'browseshare') {
		postfix = '-bshare';
	} else {
		postfix = '';
	}
		iBtn = getById('iconBtn'+postfix);
		lBtn = getById('listBtn'+postfix);
		tBtn = getById('thumbBtn'+postfix);
		if (how == 'icons') {
			iBtn.className = 'viewModeSelected';
			lBtn.className = 'viewMode';
			tBtn.className = 'viewMode';
		} else if (how == 'list') {
			iBtn.className = 'viewMode';
			lBtn.className = 'viewModeSelected';
			tBtn.className = 'viewMode';
		} else {
			iBtn.className = 'viewMode';
			lBtn.className = 'viewMode';
			tBtn.className = 'viewModeSelected';
		}
}

function changeView(how) {
	tb = AfianHandler.activeTab;
	tb.viewMode = how;
	tb.destroyItems();
	tb.buildItems();
	tb.showItems();
	setCookie('AfianViewMode', how);
}


afian_submenus = new Array;

function Afian_SubMenu(menuid, left, top) {
	this.obj = getById(menuid);
	afian_submenus[menuid] = this;
	this.obj.onmouseover= new Function('Afian_SubMenu.Pause(\''+menuid+'\')');
	this.obj.onmouseout= new Function('Afian_SubMenu.Out(\''+menuid+'\')');
	this.obj.onclick= new Function('Afian_SubMenu.Out(\''+menuid+'\')');
	this.obj.onfocus = new Function('blur()');
}

Afian_SubMenu.Over = function(menuid, left, top) {
	if (!afian_submenus[menuid].draw) {
		clearTimeout(afian_submenus[menuid].stopTimer);
afian_submenus[menuid].startTimer = setTimeout('Afian_SubMenu.Draw(\''+menuid+'\', \''+left+'\', \''+top+'\')', '0');
	} else {
		Afian_SubMenu.Pause(menuid);
	}
	if (Afian_SubMenu.activeMenuId != menuid && Afian_SubMenu.activeMenuId != undefined) {
		Afian_SubMenu.Hide(Afian_SubMenu.activeMenuId);
	}
}

Afian_SubMenu.Draw = function(menuid, left, top) {
	afian_submenus[menuid].draw = true;
	menu = afian_submenus[menuid].obj;
	menu.style.left=left;
	menu.style.top=top;	
	menu.style.display='block';	
	zindexcount++;
	getById('afian_hmenu').style.zIndex = zindexcount;
	Afian_SubMenu.activeMenuId = menuid;
}

Afian_SubMenu.Out = function(menuid) {
	clearTimeout(afian_submenus[menuid].startTimer);
	afian_submenus[menuid].stopTimer = setTimeout('Afian_SubMenu.Hide(\''+menuid+'\')', 100);
}

Afian_SubMenu.Hide = function(menuid) {
	menu = afian_submenus[menuid].obj;
	menu.style.display='none';
	afian_submenus[menuid].draw = false;
}

Afian_SubMenu.Pause = function(menuid) {
	clearTimeout(afian_submenus[menuid].stopTimer);
}

function fileNameExists(name) {
	for (var i=0; i < AfianHandler.activeTab.items.length; i++) {
		if (AfianHandler.activeTab.items[i] != undefined) {
			if (AfianHandler.activeTab.items[i].icon_title == name) {
				return true;
			}
		}
	}
	return false;
}

function makeDragable(objid) {
	Drag.init(getById(objid),null);
}

function titleDrag(objid) {
	Drag.init(getById('popmenu-titlebar-'+objid), getById('popmenu-'+objid));
}

function makePopMenu(id) {
	tp = getById('popmenu-top-'+id);
	mid = getById('popmenu-middle-'+id);
	bot = getById('popmenu-bottom-'+id);
	content = popMenuTemplate.replace('POPM_TOP', tp.innerHTML);
	content = content.replace('POPM_MIDDLE', mid.innerHTML);
	content = content.replace('POPM_BOTTOM', bot.innerHTML);
	content = content.replace('POPM_ID', 'popmenu-titlebar-'+id);
	tp.innerHTML = '';
	mid.innerHTML = '';
	bot.innerHTML = '';
	getById('popmenu-'+id).innerHTML = content;
}

function AfianAlert(str) {
	getById('popmenu-alert-message').innerHTML = str;
	centeredMenu('popmenu-alert');
}

function AfianPrompt(str, buttons) {
	getById('popmenu-prompt-message').innerHTML = str;
	str = '';
	for (var i = 0 ; i < buttons.length; i++) {
	str += '<td><table cellspacing="0" cellpadding="0" class="button" onclick="javascript:'+buttons[i]['onclick']+'"><tr><td align="center">'+buttons[i]['title']+'</td></tr></table></td>';
	}
	getById('popmenu-prompt-buttons').innerHTML = '<table border="0" cellpadding="1" cellspacing="0"><tr>'+str+'</tr></table>';
	buttonize();
	centeredMenu('popmenu-prompt');
}

function addUploadFields() {
	if (is.Opera) {
		AfianAlert('Opera browser does not support this feature.');
	} else {
		tr = document.createElement('TR');
		td = document.createElement('TD');
		td.align = 'right';
		td.innerHTML = '<input type="file" name="uploadfiles[]" class="inputfile" onFocus="javascript:writing=true;" onBlur="javascript:writing=false;" name="upload_extra_fileds[]">';
		tr.appendChild(td);
		getById('afian_upload_fields').appendChild(tr);
	}
}


function msg(type, simplealert) {
	if (simplealert) {
		fnc = alert;
	} else {
		fnc = AfianAlert;
	}
	if (type == 'noperm') {
		fnc(AfianMessages['You are not allowed to perform this operation']);
	} else if (type == 'nospace') {
		fnc(AfianMessages['Not enough space']);
	} else if (type == 'notraffic') {
		fnc(AfianMessages['Traffic quota exceeded']);
	} else {
		fnc(AfianMessages['Option unavailable']);
	}
}

function setQuotaIndicators (wich, curr, quota) {
	prefix = (wich == 'space')?'space':'traffic';
	proc = Math.round((curr*100)/quota);
	getById(prefix+'_curr').style.width = proc+'%';
	getById(prefix+'_curr_num').innerHTML = formatFileSize(curr);
	getById(prefix+'_quota_num').innerHTML = formatFileSize(quota);
	getById(prefix+'_txt').style.color= (curr >= quota)?'red':'dimgray';
}

function AdminLoadCalendar(uid) {
	cali = getById('califrame');
	cali.setAttribute('src', 'javascript:document.write(\'<body style="border:1px solid silver;padding:5px;font-size:11px;font-family: verdana, tahoma, arial, sans-serif;color:#363636;" bgcolor="#E4E4E4">'+AfianMessages['Loading, please wait...']+'</body>\')');
	window.setTimeout('loadCalendarUID('+uid+')', 500);
}
function loadCalendarUID(uid) {
	cali.setAttribute('src', '?file=calendar'+((uid)?'&uid='+uid:'')+'');
}

var hideinterval;
function AfianBubbleMessage(str, delay) {
	if (str.length == 0) {
		hidePopupMessage();
	}
	if (!delay) {
		delay = 2100;
	}
	if (hideinterval) {
		window.clearInterval(hideinterval);
	}
	container = getById('bubbleMessage');
	container.innerHTML = str;
	container.style.visibility='visible';
	hideinterval = window.setInterval('hidePopupMessage()',delay);
}
function hidePopupMessage() {
	getById('bubbleMessage').style.visibility='hidden';
	if (hideinterval) {
		window.clearInterval(hideinterval);
	}
}

function debugMode(how) {
	ic=document.getElementById('iframesContainer');
	if (how == false) {
	ic.style.display='none';
	} else {
	ic.style.display='block';
	}
}