/*
	$Id: ide-customfuncs.js,v 1.6 2006-03-30 20:56:14+02 chriz Exp chriz $
		All code (c)2004 by ://steinmeier.dk - Chriztian Steinmeier
*/

var IDE_GALLERY_NAVLINKS = "galleryNavigator";
var IDE_BASEURL = "/Default.asp";
var IDE_KONTAKT_PAGEID = 10;	// ID of Contactform ("Kontakt")
var IDE_KONTAKT_NAME = "postform4";
var IDE_KONTAKT_HEADER_TAG = "H2";

var aPageSet = [];
var iMyPageID;

// This is called onload by DW
function csOnloadHandler() {
	iMyPageID = +csGetParamValueFromQueryString("ID");	// Yep - that '+' is OK -- try to find out why :-)
	
	switch (iMyPageID) {
		case IDE_KONTAKT_PAGEID :
			csRearrangeForm();
		break;
		
		// Handle other specific pages here...
	}
	
	csFixDWDropdownMenus();
}

function csGalleryPrevious() {
	csSetupVars();
	for (var p=0; p<aPageSet.length; ++p) {
		if (aPageSet[p] == iMyPageID) {
			document.location = IDE_BASEURL + "?ID=" + (p > 0 ? aPageSet[p-1] : aPageSet[aPageSet.length-1]);
		}
	}
	return false;
}

function csGalleryNext() {
	csSetupVars();
	for (var p=0; p<aPageSet.length; ++p) {
		if (aPageSet[p] == iMyPageID) {
			document.location = IDE_BASEURL + "?ID=" + (p < aPageSet.length-1 ? aPageSet[p+1] : aPageSet[0]);
		}
	}
	return false;
}

function csSetupVars() {
	aPageSet = csGetPageSet();
	iMyPageID = csGetParamValueFromQueryString("ID");
}

function csGetPageSet() {
	var oLinkContainer, aAnchors, curr;
	
	if (aPageSet.length > 0) {
		return aPageSet;
	} else {
		oLinkContainer = document.getElementById(IDE_GALLERY_NAVLINKS);
		if (oLinkContainer) {
			aAnchors = oLinkContainer.getElementsByTagName("A");
			for (var l=0; l<aAnchors.length; ++l) {
				curr = aAnchors[l];
				if (curr.href.indexOf("ID") >= 0 && curr.className != "jslink") {
					aPageSet[aPageSet.length] = csGetParamValueFromQueryString("ID", aAnchors[l].href);
					csPostMessage("aPageSet = [" + aPageSet.join(", ") + "].");
				}
			}
			
			return aPageSet;
		}
	
	}
}

// Utility function; returns value of 'sParam' parameter from URL, eg. "...?ID=23" -> '23'
function csGetParamValueFromQueryString(sParam/* Optional */, sData) {
    var strParameters = typeof(sData) != "undefined" ? sData : document.location.search;

	csPostMessage("sData = '" + sData + "'");
	if (strParameters > "") {
		if (strParameters.indexOf("?") >= 0) {
			strParameters = strParameters.substring(strParameters.indexOf("?") + 1, strParameters.length);
		}
		var arrParams = strParameters.split("&");
		for (var p=0; p<arrParams.length; ++p) {
			var arrCurrent = arrParams[p].split("=");
			if (arrCurrent[0] == sParam) {
				return arrCurrent[1].split("#")[0];		// remove potential hashed stuff from value
			}
		}
	}

	return "";
}


// Rearrange the checkboxes from a one-column view to a 3-column one.
function csRearrangeForm() {
var oForm = document.forms[IDE_KONTAKT_NAME];
	var oTable = oForm.getElementsByTagName("TABLE")[0];	// 1st table in form
	
	var aChecks = oForm.getElementsByTagName("INPUT");
	var oNewTable = document.createElement("TABLE");
	var oNewTableTBody = oNewTable.appendChild(document.createElement("TBODY"));
	var oHeaderRow = document.createElement("TR");
	var oHeaderCell = document.createElement("TH");
	oHeaderCell.appendChild(document.getElementsByTagName(IDE_KONTAKT_HEADER_TAG)[0]);
	oHeaderCell.setAttribute("colSpan", "2");
	oHeaderCell.setAttribute("align", "left");
	oHeaderRow.appendChild(oHeaderCell);
	oNewTableTBody.appendChild(oHeaderRow);
	
	for (var c=0, added=0; c<aChecks.length; ++c) {
		if (aChecks[c].type == "checkbox") {
			oNewTableTBody.appendChild(aChecks[c--].parentNode.parentNode);	// postdecrement VERY important - the INPUT is effectively removed from the array!
			++added;
		}
	}
	
	var oWrapperTable = document.createElement("TABLE");
	var oWrapperTableTBody = oWrapperTable.appendChild(document.createElement("TBODY"));
	var oLayoutRow = oWrapperTableTBody.appendChild(document.createElement("TR"));
	var oCell1 = document.createElement("TD");
	var oCell2 = document.createElement("TD");
	
	oCell1.appendChild(oNewTable);	// First cell is the checkboxes
	oCell2.appendChild(oTable);	// Second cell, the rest of the orig. form
	
	//oLayoutRow.setAttribute("valign", "top");	// Should work, but doesn't in IE.
	oLayoutRow.vAlign = "top";
	oLayoutRow.appendChild(oCell1);
	oLayoutRow.appendChild(oCell2);
	
	oWrapperTable.setAttribute("align", "center");
		
	oForm.appendChild(oWrapperTable);

}

function csRearrangeForm_DEPRECATED() {
	var oForm = document.forms[IDE_KONTAKT_NAME];
	var oTable = oForm.getElementsByTagName("TABLE")[0];	// 1st table in form
	var oNewRow = document.createElement("TR");
	
	var aChecks = oForm.getElementsByTagName("INPUT");
	var oNewTable = document.createElement("TABLE");
	var oNewTableTBody = oNewTable.appendChild(document.createElement("TBODY"));
	
	for (var c=0, added=0; c<aChecks.length; ++c) {
		if (aChecks[c].type == "checkbox") {
			oNewRow.appendChild(aChecks[c--].parentNode);	// postdecrement VERY important - the INPUT is effectively removed from the array!
			++added;
			if (added % 3 == 0) {
				oNewTableTBody.appendChild(oNewRow);
				oNewRow = document.createElement("TR");
			}
		}
	}
	oNewRow = null;
	oForm.insertBefore(oNewTable, oTable);
	
	oTable.setAttribute("align", "center");
	oNewTable.setAttribute("align", "center");

}

// This takes care of hiding the arrows in the dropdownmenus for non-existing submenus.
function csFixDWDropdownMenus() {
	var aTables = document.getElementsByTagName("table");
	var oTable, t=0;
	while (oTable = aTables[t++]) {
		if (oTable.rows.length == 0) {
			csKillDropdownArrow(oTable.parentNode.id);
		}
	}
}

function csKillDropdownArrow(sID) {
	var oTR = document.getElementById(sID.replace("submenu", ""));
	if (oTR) {
		oTR.cells[2].getElementsByTagName("img")[0].style.visibility = "hidden";
	}
}

/*
	Very simple debugging
*/
IDE_DEBUG_STACK = "";
function csPostMessage(msg) {
	IDE_DEBUG_STACK += ((IDE_DEBUG_STACK == "" ? "" : "|") + msg);
}

function csViewMessages() {
	var dbgWin = window.open("", "", "width=500,height=500");
	with (dbgWin) {
		document.write('<body><h1>Debug Info</h1><ol><li>' + IDE_DEBUG_STACK.split("|").join("</li><li>") + '</li></ol></body>');
	}
}

// Create dummy for old browsers
if (typeof(document.getElementById) == "undefined") {
	document.getElementById = new Function("return null;");
}