// CWLS general javascripts
function openEditProfile(urlRoot) {
    var profileUrl = '/private/edit_profile.php';

    if (urlRoot.length > 1) {
        profileUrl = urlRoot + profileUrl;
    }

	var profile = window.open(profileUrl, 'profile', 'status=yes,menubar=no,toolbar=no,scrollbars=yes,resizeable=yes,width=700,height=760,top=25,left=25');
}

function validation() {
	return true;
}

function PrintEnvelopes(form, envelopeSize) {
	form.envelopeSize.value = envelopeSize;
//	var envelope = window.open("", "envelope");
	if (!countCheckedBoxes(form, "ids[]")) {
        if (envelopeSize == "letter") {
		    alert("Please select at least one record to print a letter(s) for.");
        } else {
            alert("Please select at least one record to print an envelope for.");
        }
		return false;
	}
	form.action.value = 'envelopes';
	form.target = 'new';
//	form.target = env;
	form.submit();
}

function PrintLabels(form, labelType) {
	form.labelType.value = labelType;
	if (!countCheckedBoxes(form, "ids[]")) {
		alert("Please select at least one record to print an label for.");
		return false;
	}
	form.action.value = 'labels';
	form.target = 'new';
	form.submit();
}

function ExportEmails(form) {
	if (!countCheckedBoxes(form, "ids[]")) {
		alert("Please select at least one record to export an email for.");
		return false;
	}
	form.action.value = 'exportEmails';
	form.target = 'new';
	form.submit();
}

function ExportToCSV(form) {
	form.action.value = 'exportToCSV';
	form.target = 'new';
	form.submit();
}

function deleteCategory(id, name) {
	var b = confirm("Are you sure you wish to delete the category '" + name + "'? All files in " + name + " will be moved to the root category.");
	if (b) {
		var f = document.forms['deleteCategory'];
		f.elements['id'].value = id;
		f.submit();
	}
}

function addExecutiveFileToCategory(categoryId) {

	hideAddEditForms();
	var f = document.forms['addFile'];
	show(f);
	selectAnOption(f.elements['executive_category_id'], categoryId);

	f.elements['action'].value = 'actnAddFile';
}

function addChildToExecutiveCategory(parentId) {

	hideAddEditForms();
	var f = document.forms['addCategory'];
	show(f);
	var fss = getByTagName(f, "fieldset")
	var fs = getByTagName(fss[0], "legend");
	setText(fs[0], "Add Category");

	selectAnOption(f.elements['parent_id'], parentId);

	f.elements['action'].value = 'actnAddCategory';
}

function downloadExecutiveFile(id) {
	window.location = '/private/admin/files/download.php?id=' + id;
}

function hideAddEditForms() {
	hide(document.forms['addFile']);
	hide(document.forms['editFile']);
	hide(document.forms['addCategory']);
}

function editfile(id, name, description, parentId) {
	hideAddEditForms();

	var f = document.forms['editFile'];
	show(f);
	selectAnOption(f.elements['executive_category_id'], parentId);
	f.elements['id'].value = id;
	f.elements['filename'].value = name;
	f.elements['description'].value = description;
}

function editcategory(id, name, description, parentId) {

	hideAddEditForms();
	var f = document.forms['addCategory'];
	var fss = getByTagName(f, "fieldset")
	var fs = getByTagName(fss[0], "legend");
	setText(fs[0], "Edit Category");

	if (id != 1) {
		show(f);
		selectAnOption(f.elements['parent_id'], parentId);
		f.elements['name'].value = name;
		f.elements['id'].value = id;
		f.elements['action'].value = 'editCategory';
		f.elements['description'].value = description;
	}
}


// sets the text of a node (or its first child)
function setText(id, words) {
	var e = get(id);
	if (! e) { return false; }

	if (e.nodeType == TEXT_NODE) {
		e.nodeValue = words;
	} else {
		while (e.childNodes.length > 0) {
			e.removeChild(e.childNodes[0]);
		}
		e.appendChild(text(words));
	}

	return true;
}

function selectAnOption(s, value) {

	if (arguments.length == 1) {
		value = 0;
	}

	var select = get(s);
	if (! select) { return false; }

	var options = select.options; // getByTagName(sel, "option");
	if (! options) { return false; }

	for (var i = 0; i < options.length; i++) {
		if (options[i].value == value) {
			options[i].selected = true;
			return options[i];
		}
	}

	return false;
}

// retrieves a list of all child tags with the appropriate name
function getByTagName(tag, name) {
	if (arguments.length == 1) {
		return document.getElementsByTagName(tag);
	} else {
		var t = get(tag);
		if (! t) { return false; }
		return t.getElementsByTagName(name);
	}
}

function get(id) {
	if (typeof id == "string") {
		return document.getElementById(id);
	} else {
		return id;
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// Miscellaneous scripts
// --------------------------------------------------
var newWindow2 = null;
function MakeNewWindow2(winTarget,winName,winTop,winLeft,winWidth,winHeight) {
   newWindow = window.open(winTarget,winName,"status=yes,menubar=no,toolbar=no,scrollbars=yes,resizeable=yes,width=" + winWidth + ",height=" + winHeight + ",top=" + winTop + ",left=" + winLeft)
   newWindow.focus()
}

var newWindow = null;
function MakeNewWindow(x,y) {
	newWindow = window.open(x,y,"status=0,menubar=no,toolbar=no,scrollbars=yes,resizeable=yes,width=730,height=530,top=15,left=15")
	newWindow.focus()
}

function PrintThisWindow() {
   window.print()
}

function CloseThisWindow() {
   window.close()
}

function CloseFocus() {
	top.opener.focus();
	window.opener.location.href = window.opener.location;
	parent.close();
}

function CheckAll(theForm) {
    for (i=0,n=theForm.elements.length;i<n;i++)
        if (theForm.elements[i].name.indexOf('ids[]') !=-1)
            theForm.elements[i].checked = !theForm.elements[i].checked;
}

function UncheckAll(theForm) {
    for (i=0,n=theForm.elements.length;i<n;i++)
        if (theForm.elements[i].name.indexOf('ids') !=-1)
            theForm.elements[i].checked = false;
}


/**
 * Toggle the visiblity of an element
 */
function toggle ( id ) {
	var el = document.getElementById(id);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

/**
 * anti-spam email address writer
 */
function writeEmail( name, domain ) {
		document.write('<a href=\"mailto:' + name + '@' + domain + '\">');
		document.write(name + "@" + domain + '</a>');
}