
/*
	Html Popup
*/
function Pop(url,width,height,status,toolbar,scrollbars,resizable) {
	if (width == null) w = 600; else w = width;
	if (height == null) h = 400; else h = height;
	if (status == null) st = 0; else st = status;
	if (toolbar == null) tb = 0; else tb = toolbar;
	if (resizable == null) r = 0; else r = resizable;
	if (scrollbars == null) sb = 1; else sb = scrollbars;
	open(url,"_blank","status="+st+",toolbar="+tb+",scrollbars="+sb+",resizable="+r+",width="+w+",height="+h+",screenX=30,screenY=30,left=30,top=30");
}

function test_alert() {
	window.alert('Test...');
}

// Check if a given variable/object is an array
function isArray(obj) {
	if (obj.constructor.toString().indexOf("Array") == -1) {
		return false;
	}
	else {
		return true;
	}
}

// toggle visibility
function toggle_display(targetId){
	if (document.getElementById && document.getElementById(targetId)) {
		var target = document.getElementById( targetId );
		if (target.style.display == "none"){
			target.style.display = "block";
		}
		else {
			target.style.display = "none";
		}
	}
}

//
function display_inline(element_id_1,element_id_2,element_id_3,element_id_4,element_id_5) {
	if (element_id_1 != null) document.getElementById(element_id_1).style.display="inline";
	if (element_id_2 != null) document.getElementById(element_id_2).style.display="inline";
	if (element_id_3 != null) document.getElementById(element_id_3).style.display="inline";
	if (element_id_4 != null) document.getElementById(element_id_4).style.display="inline";
	if (element_id_5 != null) document.getElementById(element_id_5).style.display="inline";
}

//
function display_block(element_id_1,element_id_2,element_id_3,element_id_4,element_id_5) {
	if (element_id_1 != null) document.getElementById(element_id_1).style.display="block";
	if (element_id_2 != null) document.getElementById(element_id_2).style.display="block";
	if (element_id_3 != null) document.getElementById(element_id_3).style.display="block";
	if (element_id_4 != null) document.getElementById(element_id_4).style.display="block";
	if (element_id_5 != null) document.getElementById(element_id_5).style.display="block";
}

// 
function display_none(element_id_1,element_id_2,element_id_3,element_id_4,element_id_5) {
	if (element_id_1 != null) document.getElementById(element_id_1).style.display="none";
	if (element_id_2 != null) document.getElementById(element_id_2).style.display="none";
	if (element_id_3 != null) document.getElementById(element_id_3).style.display="none";
	if (element_id_4 != null) document.getElementById(element_id_4).style.display="none";
	if (element_id_5 != null) document.getElementById(element_id_5).style.display="none";
}

function display_swap(hide,display) {
	display_none(hide);
	display_block(display);
}

function scrollVerticalToElement(elementID) {
	var object = document.getElementById(elementID);
	if (object) {
		var top = object.offsetTop;
		window.scrollTo(0,top);
	}
	else {
		//window.alert('No object with id: ' + elementID);
	}
}

function heightIncrease100(elementID) {
	var obj = document.getElementById(elementID);
	var height = obj.offsetHeight;
	if (height < 2000) {
		obj.style.height = (height + 100) + 'px';
	}
}
function heightDecrease100(elementID) {
	var obj = document.getElementById(elementID);
	var height = obj.offsetHeight;
	if (height > 199) {
		obj.style.height = (height - 100) + 'px';
	}
}

function sizeIncrease100(elementID, amount, maxWidth, maxHeight) {
	var obj = document.getElementById(elementID);
	var width = obj.offsetWidth;
	var height = obj.offsetHeight;
	if (width <= (maxWidth - amount)) {
		obj.style.width = (width + amount) + 'px';
	}
	else {
		obj.style.width = maxWidth + 'px';
	}
	
	if (height <= (maxHeight - amount)) {
		obj.style.height = (height + amount) + 'px';
	}
	else {
		obj.style.height = maxHeight + 'px';
	}
}
function sizeDecrease100(elementID, amount, minWidth, minHeight) {
	var obj = document.getElementById(elementID);
	var width = obj.offsetWidth;
	var height = obj.offsetHeight;
	if (width >= (minWidth + amount)) {
		obj.style.width = (width - amount) + 'px';
	}
	else {
		obj.style.width = minWidth + 'px';
	}
	
	if (height >= (minHeight + amount)) {
		obj.style.height = (height - amount) + 'px';
	}
	else {
		obj.style.height = minHeight + 'px';
	}
}

// Forms
function submitForm(formid, hiddenid1, hiddenvalue1, hiddenid2, hiddenvalue2) {
	if (hiddenid1) {
		document.getElementById(hiddenid1).value = hiddenvalue1;
	}
	if (hiddenid2) {
		document.getElementById(hiddenid2).value = hiddenvalue2;
	}
	document.getElementById(formid).submit();
}


// TinyMCE functions

function tinymceCustomExecCommandHandler(editor_id, elm, command, user_interface, value) {
	var linkElm, imageElm, inst;
	
	switch (command) {
		/*
		case "mceLink":
			inst = tinyMCE.getInstanceById(editor_id);
			linkElm = tinyMCE.getParentElement(inst.selection.getFocusElement(), "a");

			if (linkElm)
				alert("Link dialog has been overriden. Found link href: " + tinyMCE.getAttrib(linkElm, "href"));
			else
				alert("Link dialog has been overriden.");

			return true;
		*/
		case "mceImage":
			inst = tinyMCE.getInstanceById(editor_id);
			imageElm = tinyMCE.getParentElement(inst.selection.getFocusElement(), "img");
			
			Pop('pop.php?module=myfilearea',700,500,'Test',0,1,1);
			/*
			if (imageElm)
				alert("Image dialog has been overriden. Found image src: " + tinyMCE.getAttrib(imageElm, "src"));
			else {
				alert("Image dialog has been overriden.");
				alert('Editor id: ' + editor_id + ', Elm: ' + elm + ', Command: ' + command + ', User_interface: ' + user_interface + ', Value: ' + value);
			}
			*/
			return true;
		case "mceEmotion":
			//inst = tinyMCE.getInstanceById(editor_id);
			Pop('pop.php?module=emoticons',700,500,'Test',0,1,1);
			return true;
	}
	//return true;
	return false; // Pass to next handler in chain
}

function myFileBrowser (field_name, url, type, win) {
	//alert("Field_Name: " + field_name + "\nURL: " + url + "\nType: " + type + "\nWin: " + win); // debug/testing
}

function tinymceImageInsert(element, imageid, preurl, insert_parent, close_window, image_title, image_maxwidth, image_maxheight) {
	
	if (image_title) {
		image_title = ' alt="' + image_title + '" title="' + image_title + '"';
	}
	else {
		image_title = '';
	}
	
	var imageUrl = preurl + '/image.php?i=' + imageid;
	
	if (image_maxwidth) {
		imageUrl = imageUrl + '&amp;mw=' + image_maxwidth;
	}
	if (image_maxheight) {
		imageUrl = imageUrl + '&amp;mw=' + image_maxheight;
	}
	
	var htmlCode = '<img src="' + imageUrl + '"' + image_title + 'border="0" />';
	
	if (insert_parent) {
		// Insert image in parent window element
		window.opener.tinyMCE.execInstanceCommand(element, 'mceInsertContent', false, htmlCode);
	}
	else {
		window.alert('insert, same window javascript');
	}
	
	if (close_window) {
		window.close();
	}
}

// Countdown timer
function countDownReload(elementid, seconds, daystext, hourstext, minutestext, secondstext) {
	
	v = new Date();
	var initSeconds = seconds;
	var DisplayElement = document.getElementById(elementid);
	n = new Date();
	seconds = seconds - Math.round((n.getTime()-v.getTime())/1000.);
	var minutes = 0;
	var hours = 0;
	var days = 0;
	
	if(initSeconds < 0){
		// End of timer action
		window.location.reload();
	}
	else{
		
		if(seconds > 59){
			minutes = Math.floor(seconds/60);
			seconds = seconds - (minutes*60);
		}
		if(minutes > 59) {
			hours = Math.floor(minutes/60);
			minutes = minutes - (hours*60);
		}
		if (hours > 23) {
			days = Math.floor(hours/24);
			hours = hours - (days*24);
		}
		
		if(seconds < 10){
			//seconds = "0" + seconds;
		}
		if(minutes < 10){
			//minutes = "0" + minutes
		}
		
		var displayText = seconds + '' + secondstext;
		if (minutes > 0) {
			displayText = minutes + '' + minutestext + ":" + displayText;
		}
		if (hours > 0) {
			displayText = hours + '' + hourstext + ":" + displayText;
		}
		if (days > 0) {
			displayText = days + '' + daystext + ":" + displayText;
		}
		
		
		DisplayElement.innerHTML = displayText;
		
		initSeconds = initSeconds - 1;
		window.setTimeout("countDownReload('" + elementid + "', " + initSeconds + ", '" + daystext + "', '" + hourstext + "', '" + minutestext + "', '" + secondstext + "');", 999);
	}
	
	
}

// Element width/height
function getElementHeight(Elem) {
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	}
	else if (document.all){
		var elem = document.all[Elem];
	}
	if (typeof(elem.style.pixelHeight) == 'number') {
		xPos = elem.style.pixelHeight;
	}
	else if (typeof(elem.offsetHeight) == 'number') {
		xPos = elem.offsetHeight;
	}
	else {
		xPos = FALSE;
	}
	return xPos;
}

function getElementWidth(Elem) {
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	}
	else if (document.all){
		var elem = document.all[Elem];
	}
	if (typeof(elem.style.pixelWidth) == 'number') {
		xPos = elem.style.pixelWidth;
	}
	else if (typeof(elem.offsetWidth) == 'number') {
		xPos = elem.offsetWidth;
	}
	else {
		xPos = FALSE;
	}
	return xPos;
}