// JavaScript Document
function servOC(id)
	{
	if (document.getElementById('tr'+id).style.display == "none"){
		document.getElementById('tr'+id).style.display = "";
		document.getElementById('tr'+id).style.background = "#CCCCCC";
	}
	else if(document.getElementById('tr'+id).style.display == ""){
		document.getElementById('tr'+id).style.background = "#FFFFFF";
		document.getElementById('tr'+id).style.display = "none";
	}
}

function servOCM(id)
	{
	if (document.getElementById('tr'+id).style.display == "none"){
		document.getElementById('tr'+id).style.display = "";
		document.getElementById('trm'+id).innerHTML= "Hide details";
		document.getElementById('tr'+id).style.background = "#CCCCCC";
	}
	else if(document.getElementById('tr'+id).style.display == ""){
		document.getElementById('tr'+id).style.background = "#FFFFFF";
		document.getElementById('trm'+id).innerHTML= "Show details";
		document.getElementById('tr'+id).style.display = "none";
	}
}
function servOCA(id)
	{
	if (document.getElementById('tr'+id).style.display == "none"){
		document.getElementById('tr'+id).style.display = "";
		document.getElementById('trm'+id).innerHTML= "Hide Message";
		document.getElementById('tr'+id).style.background = "#CCCCCC";
	}
	else if(document.getElementById('tr'+id).style.display == ""){
		document.getElementById('tr'+id).style.background = "#FFFFFF";
		document.getElementById('trm'+id).innerHTML= "Show Message";
		document.getElementById('tr'+id).style.display = "none";
	}
}function servOCLayers()
	{
		if(document.getElementById('searchby').options[document.getElementById('searchby').selectedIndex].value == "Category"){
				document.getElementById('articlesdiv1').style.display = "";
				document.getElementById('articlesdiv1').innerHTML = "Please select a category";
				document.getElementById('articlesdiv3').style.display = "";
				document.getElementById('articlesdiv2').style.display = "none";
		}
		else if (document.getElementById('searchby').options[document.getElementById('searchby').selectedIndex].value == "Author" ||
				document.getElementById('searchby').options[document.getElementById('searchby').selectedIndex].value == "Date" ||
				document.getElementById('searchby').options[document.getElementById('searchby').selectedIndex].value == "ArticleID" ||
				document.getElementById('searchby').options[document.getElementById('searchby').selectedIndex].value == "Subject" ){
				document.getElementById('articlesdiv1').style.display = "";
				document.getElementById('articlesdiv2').style.display = "";
				document.getElementById('articlesdiv1').innerHTML = "Please enter the value";
				document.getElementById('articlesdiv3').style.display = "none";

		}
		else {
				document.getElementById('articlesdiv1').style.display = "none";
				document.getElementById('articlesdiv2').style.display = "none";
				document.getElementById('articlesdiv3').style.display = "none";
		}
		return true;
}

function textCounter(field,counter,maxlimit,linecounter) {
	// text width//
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value.length;

	// trim the extra text
	if (charcnt > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}

	else {
	// progress bar percentage
	var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
	document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
	document.getElementById(counter).innerHTML="Limit: "+percentage+"%"
	// color correction on style from CCFFF -> CC0000
	setcolor(document.getElementById(counter),percentage,"background");
	}
}
function wordCounter(field,counter,maxlimit,linecounter) {
	// text width//
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value;
	var splitString = charcnt.split(" ");
	var word_count = splitString.length -1;


	// trim the extra text
	if (word_count > maxlimit) {

	}

	else {
	// progress bar percentage
	var percentage = parseInt(100 - (( maxlimit - word_count) * 100)/maxlimit) ;
	document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
	document.getElementById(counter).innerHTML="Limit: "+percentage+"%"
	// color correction on style from CCFFF -> CC0000
	setcolor(document.getElementById(counter),percentage,"background-color");
	}
}
function setcolor(obj,percentage,prop){
	obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}
function validates(){
	if(document.getElementById('l').value =='' || document.getElementById('p').value =='') {
	        	document.getElementById('messagej').innerHTML ='Gebruikersnaam en wachtwoord zijn verplichte velden.';
	        	return false;
	        	}
	        	else if(document.getElementById('p').value.length <6){
	        		document.getElementById('messagej').innerHTML ='Wachtwoord moet minimaal 6 characters zijn.';
	        		return false;
	        	}
	        	document.login.submit();
	        	return true;
}
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=2,height=2,left = 500,top = 400');");
}
var offsetxpoint=-60; //Customize x offset of tooltip
var offsetypoint=20 ;//Customize y offset of tooltip
var ie=document.all;
var ns6=document.getElementById && !document.all;
var enabletip=false;

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function ddrivetip(thetext, thecolor, thewidth){
	if (ns6||ie){
			if (typeof thewidth!="undefined") document.getElementById("dhtmltooltip").style.width=thewidth+"px"
			if (typeof thecolor!="undefined" && thecolor!="") document.getElementById("dhtmltooltip").style.backgroundColor=thecolor
			document.getElementById("dhtmltooltip").innerHTML=thetext;
			enabletip=true
			return false
	}
}

function positiontip(e){
	if (enabletip){
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;

		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<document.getElementById("dhtmltooltip").offsetWidth)
		//move the horizontal position of the menu to the left by it's width
		document.getElementById("dhtmltooltip").style.left=ie? ietruebody().scrollLeft+event.clientX-document.getElementById("dhtmltooltip").offsetWidth+"px" : window.pageXOffset+e.clientX-document.getElementById("dhtmltooltip").offsetWidth+"px"
		else if (curX<leftedge)
		document.getElementById("dhtmltooltip").style.left="5px"
		else
		//position the horizontal position of the menu where the mouse is positioned
		document.getElementById("dhtmltooltip").style.left=curX+offsetxpoint+"px"

		//same concept with the vertical position
		if (bottomedge<document.getElementById("dhtmltooltip").offsetHeight)
		document.getElementById("dhtmltooltip").style.top=ie? ietruebody().scrollTop+event.clientY-document.getElementById("dhtmltooltip").offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-document.getElementById("dhtmltooltip").offsetHeight-offsetypoint+"px"
		else
		document.getElementById("dhtmltooltip").style.top=curY+offsetypoint+"px"
		document.getElementById("dhtmltooltip").style.visibility="visible"
	}
}
function lastfive(e){

			if (!e)
				var e = window.event;
		if (true){
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;

		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<document.getElementById("lastfive").offsetWidth)
		//move the horizontal position of the menu to the left by it's width
		document.getElementById("lastfive").style.left=ie? ietruebody().scrollLeft+event.clientX-document.getElementById("lastfive").offsetWidth+"px" : window.pageXOffset+e.clientX-document.getElementById("lastfive").offsetWidth+"px"
		else if (curX<leftedge)
		document.getElementById("lastfive").style.left="5px"
		else
		//position the horizontal position of the menu where the mouse is positioned
		document.getElementById("lastfive").style.left=curX+offsetxpoint+"px"

		//same concept with the vertical position
		if (bottomedge<document.getElementById("lastfive").offsetHeight)
		document.getElementById("lastfive").style.top=ie? ietruebody().scrollTop+event.clientY-document.getElementById("lastfive").offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-document.getElementById("lastfive").offsetHeight-offsetypoint+"px"
		else
		document.getElementById("lastfive").style.top=curY+offsetypoint+"px"
		document.getElementById("lastfive").style.visibility="visible"
		}
}
function mytemplate(e){

		if (!e)
			var e = window.event;
		if (true){
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;

		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<document.getElementById("mytempdiv").offsetWidth)
		//move the horizontal position of the menu to the left by it's width
		document.getElementById("mytempdiv").style.left=ie? ietruebody().scrollLeft+event.clientX-document.getElementById("mytempdiv").offsetWidth+"px" : window.pageXOffset+e.clientX-document.getElementById("mytempdiv").offsetWidth+"px"
		else if (curX<leftedge)
		document.getElementById("mytempdiv").style.left="5px"
		else
		//position the horizontal position of the menu where the mouse is positioned
		document.getElementById("mytempdiv").style.left=curX+offsetxpoint+"px"

		//same concept with the vertical position
		if (bottomedge<document.getElementById("mytempdiv").offsetHeight)
		document.getElementById("mytempdiv").style.top=ie? ietruebody().scrollTop+event.clientY-document.getElementById("mytempdiv").offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-document.getElementById("mytempdiv").offsetHeight-offsetypoint+"px"
		else
		document.getElementById("mytempdiv").style.top=curY+offsetypoint+"px"
		document.getElementById("mytempdiv").style.visibility="visible"
		}
}

function lf(){
	lastfive;
}
function hideddrivetip(){
if (ns6||ie){
enabletip=false
document.getElementById("dhtmltooltip").style.visibility="hidden"
document.getElementById("dhtmltooltip").style.left="-1000px"
document.getElementById("dhtmltooltip").style.backgroundColor=''
document.getElementById("dhtmltooltip").style.width=''
}
}

document.onmousemove=positiontip;
function LTrim( value ) {

	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");

}

// Removes ending whitespaces
function RTrim( value ) {

	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");

}

// Removes leading and ending whitespaces
function trim( value ) {

	return LTrim(RTrim(value));

}

function validateRules(){

		var checks = document.getElementsByTagName("input");
		var len = checks.length;
		var thisCheckID;
		var pattern = /^\d*$/;
		for (var i = 0; i < len; i++)
		   {
				switch(checks[i].name)
				{
					case "spam": case "abuse": case "logintries": case "smiliesstorage": case "flood":
					case "domain": case "udirectory": case "ulog":
							if(trim(checks[i].value) == "" ){
								checks[i].className = 'inputerror';thisCheckID = true;}
							else if(checks[i].name == "spam" || checks[i].name == "abuse" || checks[i].name == "logintries" || checks[i].name == "flood")							if(!pattern.test(trim(checks[i].value))){
								checks[i].className = 'inputerrornumeric';thisCheckID = true;}
							else {
								checks[i].className = "";}
					break;
				}
		   }
		   if(thisCheckID){

		   	document.getElementById("errors").style.display ="";
			return false;
			}
		   else
		   	return true;

}
function toglefield(fieldset){

	if(fieldset.style.display =="block")
		fieldset.style.display ="none";
	else
		fieldset.style.display ="block";
}
function insertSmiley(imagecode){
	updatewusiwug('messagebody',imagecode);


}


// menu collapsable

var contractall_default= false; //Should all submenus be contracted by default? (true or false)

var menu, titles, submenus, arrows, bypixels;
var heights = new Array();

var n = navigator.userAgent;
if(/Opera/.test(n)) bypixels = 2;
else if(/Firefox/.test(n)) bypixels = 3;
else if(/MSIE/.test(n)) bypixels = 2;

/////DD added expandall() and contractall() functions/////

function slash_expandall(){
if (typeof menu!="undefined"){
	for(i=0; i<Math.max(titles.length, submenus.length); i++){
		titles[i].className="title";
		arrows[i].src = "images/expanded.gif";
		submenus[i].style.display="";
		submenus[i].style.height = heights[i]+"px";
	}
}
}

function slash_contractall(){
if (typeof menu!="undefined"){
	for(i=0; i<Math.max(titles.length, submenus.length); i++){
		titles[i].className="titlehidden";
		arrows[i].src = "images/collapsed.gif";
		submenus[i].style.display="none";
		submenus[i].style.height = 0;
	}
}
}


/////End DD added functions///////////////////////////////


function init(){
    menu = getElementsByClassName("sdmenu", "div", document)[0];
    titles = getElementsByClassName("title", "span", menu);
    submenus = getElementsByClassName("submenu", "div", menu);
    arrows = getElementsByClassName("arrow", "img", menu);
    for(i=0; i<Math.max(titles.length, submenus.length); i++) {
        titles[i].onclick = gomenu;
        arrows[i].onclick = gomenu;
        heights[i] = submenus[i].offsetHeight;
        submenus[i].style.height = submenus[i].offsetHeight+"px";
    }
   }

function restore() {
    if(getcookie("menu") != null) {
        var hidden = getcookie("menu").split(",");
        for(var i in hidden) {
            titles[hidden[i]].className = "titlehidden";
            submenus[hidden[i]].style.height = "0px";
            submenus[hidden[i]].style.display = "none";
            arrows[hidden[i]].src = "images/collapsed.gif";
        }
    }
}

function gomenu(e) {
    if (!e)
        var e = window.event;
    var ce = (e.target) ? e.target : e.srcElement;
    var sm;
    for(var i in titles) {
        if(titles[i] == ce || arrows[i] == ce)
            sm = i;
    }
    if(parseInt(submenus[sm].style.height) > parseInt(heights[sm])-2) {
        hidemenu(sm);
    } else if(parseInt(submenus[sm].style.height) < 2) {
        titles[sm].className = "title";
        showmenu(sm);
    }
}

function hidemenu(sm) {
    var nr = submenus[sm].getElementsByTagName("a").length*bypixels;
    submenus[sm].style.height = (parseInt(submenus[sm].style.height)-nr)+"px";
    var to = setTimeout("hidemenu("+sm+")", 50);
    if(parseInt(submenus[sm].style.height) <= nr) {
        clearTimeout(to);
        submenus[sm].style.display = "none";
        submenus[sm].style.height = "0px";
        arrows[sm].src = "images/collapsed.gif";
        titles[sm].className = "titlehidden";
    }
}

function showmenu(sm) {
    var nr = submenus[sm].getElementsByTagName("a").length*bypixels;
    submenus[sm].style.display = "";
    submenus[sm].style.height = (parseInt(submenus[sm].style.height)+nr)+"px";
    var to = setTimeout("showmenu("+sm+")", 30);
    if(parseInt(submenus[sm].style.height) > (parseInt(heights[sm])-nr)) {
        clearTimeout(to);
        submenus[sm].style.height = heights[sm]+"px";
        arrows[sm].src = "images/expanded.gif";
    }


}
function getElementsByClassName(strClassName, strTagName, oElm){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}


window.onload = init;


function createXHR()
{
    var request = false;
        try {
            request = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (err2) {
            try {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (err3) {
				try {
				request = new XMLHttpRequest();
				}
				catch (err1)
				{
					request = false;
				}
            }
        }
    return request;
}

function read(url,param)
{
	var param1 = param.substring(0, param.length-1)

	if(document.getElementById("templatename").value == '')
	{
		document.getElementById("savemessage").innerHTML = "Please specify the template name.";
		return false;
	}
	var xhr=createXHR();
	var param2 =document.getElementById("templatename").value;
	xhr.open("GET", url+"?name="+param2+"&id="+param1, true);
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState == 4)
		{
			if(xhr.status != 200)
			{
				document.getElementById("savemessage").innerHTML = "URL not valid.";
				return false;
			}

			var content = trim(xhr.responseText);

			if(content == "1")
			{

				document.getElementById("savemessage").innerHTML = "<a href='addarticle.php?tn="+param1+"'>"+param2+"</a> Template saved successfully";
				document.getElementById("save").style.display = 'none';
				document.getElementById("templatename").style.display = 'none';
				return true;
			}
			else{
				document.getElementById("savemessage").innerHTML = "Duplicate Template name";
			}
		}
	};

	xhr.send(null);

}
function getCode(val)
{
		var xhr=createXHR();
		xhr.open("GET", url+"?name="+param2+"&id="+param1, true);
		xhr.onreadystatechange=function()
		{
			if(xhr.readyState == 4)
			{
				var content = trim(xhr.responseText);
				if(content == "0")
				{

				}
				else
				{
					return true;
				}
			}
		}
}
function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} 

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}
function MyFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}
