// +----------------------------------------------------------------------+
// | The Omnium Project v3.3.0 Interface                                  |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003-2005 The Omnium Project - UNSW                    |
// +----------------------------------------------------------------------+
// | This source file is protected under standard copyright acts.         |
// | Re-use and distribution is expressly forbidden without the consent   |
// | of The Omnium Project - UNSW                                         |
// +----------------------------------------------------------------------+
// | Authors: Sam Bauers <sam@omnium.edu.au>                              |
// +----------------------------------------------------------------------+


// Variables that are determined by PHP are set in the header


function delayedFetchImage(obj,url) {
 if (obj.src != url) {
    if (pageLoaded) {
       obj.onload = null; obj.src = url; return true;
    } else {
       setTimeout(function () { delayedFetchImage(obj,url); },200); return false;
    }
 } else { return true; }
}

function findPosY(obj) {
 curtop = 0;
 if (obj.offsetParent) {
    while (obj.offsetParent) {
       curtop += obj.offsetTop
       obj = obj.offsetParent;
    }
 } else if (obj.y) {
    curtop += obj.y;
 }
 return curtop;
}

function findPosWindow(xy) {
    if (xy == 'top') {
        if (self.screenTop) {
            return self.screenTop;
        } else {
            return self.screenY;
        }
    } else {
        if (self.screenLeft) {
            return self.screenLeft;
        } else {
            return self.screenX;
        }
    }
}


// FORM FUNCTIONS

// Alerts users to be patient when uploading files
function uploadProgress(state) {
   d = document.getElementById('uploadMeter');
   if (state) {
      d.style.display = 'block';
   } else {
      d.style.display = 'none';
   }
}


function formFocus(fieldid) {
   d = document.getElementById(fieldid);
   d.focus();
}

// Show/hides a div or other object
function togglerules(ruleid,textclosed,textopen,force) {
	obj = document.getElementById(ruleid);
	lin = document.getElementById(ruleid + 'link');
	if (obj.style) {
	    if (force) {
	        if (force == 'block') {
    			obj.style.display = 'block';
    			if (lin && textopen != null) {
    				lin.innerText = textopen;
    			}
    		} else {
    			obj.style.display = 'none';
    			if (lin && textclosed != null) {
    				lin.innerText = textclosed;
    			}
    		}
	    } else {
	    	if (obj.style.display == 'none') {
    			obj.style.display = 'block';
    			if (lin && textopen != null) {
    				lin.innerText = textopen;
    			}
    		} else {
    			obj.style.display = 'none';
    			if (lin && textclosed != null) {
    				lin.innerText = textclosed;
    			}
    		}
	    }
	}
}


function divGroupSelect(divGroup, number, total) {
    for (i=1; i<=total; i++) {
        if (i != number) {
            obj = document.getElementById(divGroup + String(i));
            if (obj) {
                obj.style.display = 'none';
            }
        }
    }
    
    obj = document.getElementById(divGroup + String(number));
    obj.style.display = 'block';
}

function adjustScroller(div, newtop) {
    if (window.resizeHandler) {
        resizeHandler();
    } else {
        sty = document.getElementById(div).style;
        sty.top = newtop + 'px';
    }
}


// COMMUNITY FUNCTIONS

// Switches between the different rating displays
function rate(close,open) {
	cls = document.getElementById(close);
	opn = document.getElementById(open);
	if (cls.style && opn.style) {
		cls.style.display = 'none';
		opn.style.display = 'block';
	}
}


// CUSTOM NAVIGATION FUNCTIONS

// For accessing URLs via a dropdown form element
function go(obj) {
    opts = obj.options;
    if (opts[opts.selectedIndex].value != ''){
        location.href = opts[opts.selectedIndex].value;
    }
}

// Returns a generic "Are you sure" message for actions
function ays(a){
	if (!a) {
		a = '';
	} else {
		a = '\n' + a;
	}
	
	if (confirm('Are you sure you wish to do this?' + a)) {
		return true;
	} else {
		return false;
	}
}



// POPUP FUNCTIONS

// Opens the users online window
function openonline() {
	window.open(omURL + 'base/online.php','onlinewin','width=500,height=400,resizable=yes,toolbar=no,scrollbars=yes')
}

function resizeView(h,w) {
    
    var x,y;
    if (self.innerHeight) // all except Explorer
    {
    	x = self.innerWidth;
    	y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    	// Explorer 6 Strict Mode
    {
    	x = document.documentElement.clientWidth;
    	y = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
    	x = document.body.clientWidth;
    	y = document.body.clientHeight;
    }
    
	if (w && h && x < 170 && y < 170) {
	    
	    var boom = false;
	    var px;
	    var py;
	    
	    if (w + 40 > self.screen.availWidth) {
	        boom = true;
	        w = self.screen.availWidth - 40;
	        h = h + 16;
	        px = 5;
	        py = findPosWindow('top');
	        if (py < 0) {
	            py = self.screen.availHeight + py;
	        }
	    }
	    
	    if (h + 40 > self.screen.availHeight) {
	        boom = true;
	        h = self.screen.availHeight - 40;
	        w = w + 16;
	        px = findPosWindow('left');
	        if (px < 0) {
	            px = self.screen.availWidth + px;
	        }
	        py = 5;
	    }
	    
	    if (boom) {
	        window.moveTo(px,py);
	    }
		window.resizeBy(w-x,h-y);
	}
}

var myChild;

function spawnHandler(url,name) {
    
    if (myChild && !myChild.closed) {
        myChild.focus();
    } else {
        topPos = findPosWindow('top');
        leftPos = findPosWindow('left');
        
        if (topPos > -1) {
            theTopPos = "top=" + (topPos + 40) + ",";
        } else {
            theTopPos = "top=40,";
        }
        if (leftPos > -1) {
            theLeftPos = "left=" + (leftPos + 40) + ",";
        } else {
            theLeftPos = "left=40,";
        }
        
        myChild = window.open(url + '&child=1',name,theTopPos + theLeftPos + 'width=160,height=160,dependent=yes,resizable=yes,toolbar=no,scrollbars=no,status=no');
    }
}

function closeHandler() {
    if (myChild) {
        myChild.close();
    }
    
    window.close();
}


// CHAT FUNCTIONS

// Open the chat window
function openchat(d){
	window.open(omURL + d,'chatwin','width=535,height=515,resizable=no,toolbar=no,scrollbars=no');
}

// Closes the chat window
function omCloseChat() {
	setTimeout('omOpenLogoutChat();',500);
}

// Logs the user out of chat by opening a small logout window
function omOpenLogoutChat() {
	window.open(omURL + 'mod/chat/logout.php','chatclose','height=1,width=200,toolbars=no,scrollbars=no,statusbar=no,scrolling=no,top=10,left=10');
}