dojo.require("dojo.parser");

//////////////////////
//  ON PAGE LOAD
////////////////////////
/*
function checkAnchor(){
	//m2 = new dojo.dnd.Moveable("overpreview", {handle: "overpreviewhandler"});
	makeOverpreviewerDragable();
	if(window.location.hash!=""){
		var hash = window.location.hash;
		
		submenuitems = dojo.query(".submenuitem");
		for(var i=0;i<submenuitems.length;i++){
			var it = submenuitems[i];
			if(it.href.indexOf(hash)!=-1){
				it.onclick();
				return true;
			}
		}
		menuitems = dojo.query("a",dojo.byId("menu"));
		for(var i=0;i<menuitems.length;i++){
			var it = menuitems[i];
			if(it.href.indexOf(hash)!=-1){
				it.onclick();
				return true;
			}
		}
	}
	return false;
}
dojo.addOnLoad(checkAnchor);
*/


//////////////////////
//	MENU
////////////////////////
function setMenuActive(which){
	var items = dojo.query(".title","menu");
	for(var i=0;i<items.length;i++){
		while(items[i].className.indexOf("selected")!=-1){
			items[i].className = items[i].className.replace("selected","");
		}
		items[i].className += items[i].className.indexOf(which)!=-1?" selected":"";
	}
}
//////////////////////
//  ON RESIZE
////////////////////////

function fixResize(){
	fixBlackPanel();
	fixOverContents();
}




//////////////////////
//  TOOL FUNCTIONS
////////////////////////
function inArray(arr,value){
	for(var i=0;i<arr.length;i++){
		if(arr[i]==value) return true;
	}
	return false;
}

function functionName(fn){
	var name=/\W*function\s+([\w\$]+)\(/.exec(fn);
	if(!name)return 'null';
	return name[1];
}

function openLink(url){
	window.open(url);
}
function addslashes(str) {
	if(str!=null){
		str=str.replace(/\'/g,'\\\'');
		str=str.replace(/\"/g,'\\"');
		str=str.replace(/\\/g,'\\\\');
		str=str.replace(/\0/g,'\\0');
	}
	return str;
}
function stripslashes(str) {
	if(str!=null){
		str=str.replace(/\\'/g,'\'');
		str=str.replace(/\\"/g,'"');
		str=str.replace(/\\\\/g,'\\');
		str=str.replace(/\\0/g,'\0');
	}
	return str;
}
function SelectAllText(who){
    who.focus();
    who.select();
}
function utf8_encode ( argString ) {
    var string = (argString+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");
 
    var utftext = "";
    var start, end;
    var stringl = 0;
 
    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;
 
        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }
 
    if (end > start) {
        utftext += string.substring(start, string.length);
    }
 
    return utftext;
}
function utf8_decode ( str_data ) {
    var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;
    
    str_data += '';
    
    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
 
    return tmp_arr.join('');
}
function onCheckChange(who,action,editid){
	var imgc = dojo.byId(who.name+"checkingimg");
	var msgc = dojo.byId(who.name+"msg");
	if(imgc!=null) imgc.style.display='';
	if(msgc!=null) msgc.style.display='none';
	if(msgc!=null) msgc.innerHTML="";
	var url = oxoadmindir+"utils.php?action="+action+"&value="+who.value+(editid!=null?"&editid="+editid:"");
	console.log("CHECK URL: "+url);
	dojo.xhrGet({
		url: url,
		load: function(responseObject, ioArgs){
				if(responseObject==null){ this.error(); return; }
				if(imgc!=null) imgc.style.display='none';
				if(msgc!=null) msgc.style.display='block';
				if(responseObject.success){
					if(responseObject.onSuccess!=null)
						responseObject.onSuccess();
					if(msgc!=null) msgc.className="formmsgok";
					switch(action){
						case "checkuser":
							var hd = dojo.byId("homedir");
							var val= hd.value;
							if(val=='/ftp/'){
								hd.value='/ftp/'+who.value+'/';
							}
							break;
						case "checkgroup":
							var hd = dojo.byId("mainfolder");
							var val= hd.value;
							if(val=='/ftp/'){
								hd.value='/ftp/'+who.value+'/';
							}
							break;
						case "checksharingfolderpermits":
							Custom.clear();
							break;
					}
				}else{
					if(msgc!=null) msgc.className="formmsgerror";
					if(msgc!=null) msgc.innerHTML = responseObject.error;
				}
				return responseObject;
			},
		error: function(response, ioArgs){
			if(msgc!=null) msgc.innerHTML = "ERROR ON CHECK";
			if(msgc!=null) msgc.style.display='block';
			return response;
		},
		handleAs: "json"
	});
}



//////////////////////
//  SIZE CONTENTS
////////////////////////
function toggleDiv(id,fromwho){
	var ob = dojo.byId(id);
	ob.style.display=ob.style.display=='none'?'':'none';
	var hidden = ob.style.display=='none';
	if(fromwho!=null) fromwho.className = fromwho.className.replace(hidden?"opened":"closed",hidden?"closed":"opened");
}
function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
function getPageScroll(){
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}
function getElementXPos(who){
	//var it = dojo.byId(who);
	//var cont = dojo.byId("toolsph");
	//var tool = dojo.byId("toolpl");
	//return cont.offsetLeft-tool.offsetWidth+it.offsetLeft;
	var it = dojo.byId(who);
	var xPos = 0;
	do{
		xPos += it.offsetLeft;
		it=it.parentNode;
	}while(it.parentNode!=null)
	return xPos;
}
function getElementHeight(who){
	return dojo.byId(who).offsetHeight;
}
function getElementWidth(who){
	return dojo.byId(who).offsetWidth;
}

function getElementTop(element) {
	sniffBrowsers();
	if (ns4) {
		var elem = dojo.byId(element);
		return elem.pageY;
	} else {
		var elem = dojo.byId(element);
		yPos = elem.offsetTop;
		tempEl = elem.offsetParent;
		while (tempEl != null) {
  			yPos += tempEl.offsetTop;
	  		tempEl = tempEl.offsetParent;
  		}
		return yPos;
	}
}
function getElementLeft(element) {
	sniffBrowsers();
	if (ns4) {
		var elem = dojo.byId(element);
		return elem.pageX;
	} else {
		var elem = dojo.byId(element);
		xPos = elem.offsetLeft;
		tempEl = elem.offsetParent;
		while (tempEl != null) {
  			xPos += tempEl.offsetLeft;
	  		tempEl = tempEl.offsetParent;
  		}
		return xPos;
	}
}



var ns4;
var op5;
var op6;
var agt;
var mac;
var ie;
var mac_ie;
function sniffBrowsers() {
	ns4 = document.layers;
	op5 = (navigator.userAgent.indexOf("Opera 5")!=-1) 
		||(navigator.userAgent.indexOf("Opera/5")!=-1);
	op6 = (navigator.userAgent.indexOf("Opera 6")!=-1) 
		||(navigator.userAgent.indexOf("Opera/6")!=-1);
	agt=navigator.userAgent.toLowerCase();
	mac = (agt.indexOf("mac")!=-1);
	ie = (agt.indexOf("msie") != -1); 
	mac_ie = mac && ie;
}

function disableSelection(target){
	target.onmousedown=function(){ return false; }
	target.onselectstart=function(){ return false; }
	/*
	if (typeof target.onselectstart!="undefined") //IE route
		target.onselectstart=function(){return false}
	else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
		target.style.MozUserSelect="none"
	else //All other route (ie: Opera)
		target.onmousedown=function(){return false}
	target.style.cursor = "default"*/
}


///////////////
//// KEY FUNCS
///////////////

var keyDowns=Array();
function onKeyDown(evt){
	keyDowns[evt.keyCode]=true;
	if(evt.keyCode == dojo.keys.ESCAPE){
		closeOverContents();
	}
}
function onKeyUp(evt){
	keyDowns[evt.keyCode]=false;
}
function isKeyDown(code){
	return keyDowns[code];
}

dojo.connect(document,"onkeydown",null,onKeyDown);
dojo.connect(document,"onkeyup",null,onKeyUp);







//////////////////////
//  MOUSE MOVE
////////////////////////

var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var mouseX = 0;
var mouseY = 0;
function getMouseXY(e) {
	if(IE) {
		mouseX = event.clientX + document.body.scrollLeft;
		mouseY = event.clientY + document.body.scrollTop;
	}else{
		mouseX = e.pageX;
		mouseY = e.pageY;
	}  
	if (mouseX < 0){mouseX = 0;}
	if (mouseY < 0){mouseY = 0;}  
	return Array(mouseX,mouseY);
}





function getSuccessButton(onClickFunc){
	return "<br /><input type=\"button\" name=\"button\" id=\"onsuccessbutton\" value=\"Ok\" onclick=\""+onClickFunc+"\" />";
}


var unloadChecks = new Array();
function onUnloadCheck(){
	var ret = "";
	for(var i=0;i<unloadChecks.length;i++){
		var retval = unloadChecks[i].check();
		if(retval !== false){
			ret += retval;
		}
	}
	if(ret!=""){
		return ret;
	}
}
window.onbeforeunload = onUnloadCheck;