/******************************************************************************
FileName:    main.js
Author:      Josh Morey
Created:     2004-07-26
Description: Global JS functions.
/*****************************************************************************/

// generic functions
function getObj(name) {
	if (document.getElementById) {
		return document.getElementById(name);
	} else if (document.all) {
		return document.all[name];
	}
}
function openDialogWindow(szPage, szTitle, iWidth, iHeight) {
	var szParams = "location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=yes,toolbar=no,height="+iHeight+",width="+iWidth;
	window.open(szPage,szTitle,szParams);
}
function openDialogWindowScroll(szPage, szTitle, iWidth, iHeight) {
	var szParams = "location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=no,height="+iHeight+",width="+iWidth;
	window.open(szPage,szTitle,szParams);
}

// datepicker functions
function openDatePickerWindow(szFieldName) {
	var szPage = "default.php?m=datepicker&alt=yes&f="+szFieldName;
	openDialogWindow(szPage, "DatePicker", 220, 240);
}
function returnDate(dtDate, szFieldName) {
	window.opener.getObj(szFieldName).value = dtDate;
	window.close();
}

// imagepicker functions
function openImagePickerWindow(szFieldName) {
	var szPage = "default.php?m=imagepicker&alt=yes&f="+szFieldName;
	openDialogWindowScroll(szPage, "ImagePicker", 570, 400);
}
function openImagePickerWindowEditor(szFieldName) {
	var szPage = "../../../../default.php?m=imagepicker&alt=yes&f="+szFieldName;
	openDialogWindowScroll(szPage, "ImagePicker", 570, 400);
}
function openImagePickerInsertWindow(iGalleryID) {
	var szPage = "default.php?m=imagepicker&alt=yes&gallery="+iGalleryID;
	openDialogWindowScroll(szPage, "ImagePicker", 570, 400);
}
function returnImage(iID, szName, szFieldName) {
	var fckImage = window.opener.getObj('txtUrl');
	if(fckImage) {
		var url = window.location.href;
		var m = new RegExp("http://[^/]*(.+)/default\.php", "gi");
		var res = m.exec(url);
		if(res) {
			window.opener.SetUrl(res[1]+'/content/'+szName);
			window.close();
		}
	} else {
		window.opener.getObj(szFieldName).value = iID;
		var thumbnail = window.opener.getObj(szFieldName+'Thumb');
		if(thumbnail) {
			thumbnail.src = 'content/thumb_'+szName;
		}
	}
	window.close();
}

// help functions
function openHelpWindow(szSection) {
	var szPage   = "default.php?m=helpfiles&s="+szSection+"&alt=yes";;
	openDialogWindowScroll(szPage, "Help", 300, 400);
}
function openFullHelp(szSection) {
	window.opener.location.href = "default.php?m=helpfiles&s="+szSection;
	window.close();
}
function loadSpecificHelp(list, alt) {
	var szSection = list.value;
	var szLocation = "default.php?m=helpfiles&s="+szSection;
	if(alt == "yes") {
		szLocation += "&alt=yes";
	}
	window.location.href = szLocation;
}

// calendar admin functions
function initEventType(iType) {
	for(var i=1; i<=4; i++) {
		var et = document.getElementById("et"+i);
		if(iType == i) {
			et.className = "";
		} else {
			et.className = "rowdisabled";
		}
	}
}
function updateEventType(selectbox) {
	var iType = selectbox.selectedIndex + 1;
	initEventType(iType);
}

// selectbox ordering functions
function moveItemsUp(selectbox) {
	for(i=1; i<selectbox.length; i++) {
		if(selectbox.options[i].selected) {
			swapItems(selectbox, i, i-1);
		}
	}
}
function moveItemsDown(selectbox) {
	for(i=selectbox.length-2; i>=0; i--) {
		if(selectbox.options[i].selected) {
			swapItems(selectbox, i, i+1);
		}
	}
}
function swapItems(selectbox, i1, i2) {
	var t1 = selectbox.options[i1];
	var t2 = selectbox.options[i2];
	if(!t2.selected) {
		var iVal    = t2.value;
		var szText  = t2.text;
		t2.value    = t1.value;
		t2.text     = t1.text;
		t1.value    = iVal;
		t1.text     = szText;
		t2.selected = true;
		t1.selected = false;
	}
}
function copyItemOrder(selectbox, textbox) {
	textbox.value = selectbox.options[0].value;
	for(i=1; i<selectbox.length; i++) {
		textbox.value += ','+selectbox.options[i].value;
	}
}

// for validation functions
function setMsgText(obj, text) {
	if(obj.childNodes.length == 0) {
		obj.appendChild(document.createTextNode(text));
	} else {
		obj.childNodes[0].value = text;
	}
}

