// Mostly admin and public JS functions, some used elsewhere.


function newWin(url,img,title,w,h) {

	   if (! (url) && ! (img)) { return; }
	   var day= new Date();
	   var id = day.getTime();
	   var ww = w+75;
	   var wh = h+125;
	   if ((screen.height) && (wh > screen.height-100)) { wh = screen.height-100; }
	   var params = 'width='+ww+',height='+wh+',scrollbars,resizable';
	   var t = (title != '')?title:img;
	   if (url=='') {
	      var msg='<!DOCTYPE HTML PUBLIC "-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN" "http:\/\/www.w3.org/TR\/html4\/loose.dtd">\n'+
		          '<meta http-equiv="Content-Type" content="text\/html; charset=iso-8859-1">\n'+
		          '<html><head><title>'+t+'<\/title><\/head>'+
		          '<style type="text\/css">\n'+
				  'html,body { font-family: Arial,Helvetica,Sans-Serif; }\n'+
				  'h3 { text-align: center; }\n'+
				  'td { text-align: center; }\n'+
				  '<\/style><body>\n'+
				  '<h3>'+t+'<\/h3>\n <table align="center" border="0"><tr><td><img src="'+img+'" width="'+w+'" height="'+h+'" border="0" alt="'+title+'"></td></tr>\n'+
				  '<tr><td><hr width="100%" size="1"><form><input type="button" onClick="javascript:window.close();" value="Close Window">\n'+
				  '<hr width="100%" size="1"><\/form><\/td><\/tr><\/table><\/body><\/html>\n';
	      var win = open('',id,params);
		  win.document.write(msg);
		  win.document.close();
	   }
	   else { var win = open(url,id,params); } 
	   return false;
}

function checkContact(form) {

	var requireds = new Array('your_name', 'email_address', 'how_you_found_us');
	var eng = new Array('your name', 'your email address', 'how you found out about us');
	var obj,msg = '';

	for (i=0; i<requireds.length; i++) {
		obj = document.getElementById(requireds[i]);
		if (obj.value == '') {
			msg = 'Please provide ' + eng[i] + ' before submitting this form.';
			break;
		}
	}
	if ((msg == '') && (form.email_address.value != '')) {
		var found = false;
		var emailString = form.email_address.value;
		for (j=0; j < emailString.length; j++) {
			if (emailString.charAt(j) == "@") { found = true; }
		}
		if (found == false) {
			msg = 'Is that your true email address?\n' +
			'Please enter your correct email address.\n' +
			'We will not place you on any mailing lists!\n';
		}
	}
	if (msg == '') { form.submit(); }
	else {  alert(msg);  }
	return false;
}

function delWarn(url) {
   var msg = 'Deleting this record is permanent\n and irreversible. CONTINUE?';
   var ok = confirm(msg);
   if (ok == true) { document.location=url; }
}


// Control panel search widgets.

function sendSearch(form,fieldList) {
    var loc = '/cgi/atp-admin.cgi?display=admin_cp'; 
    if (form.tbl.selectedIndex > 0) { 
        loc += '&amp;tbl='+form.tbl[form.tbl.selectedIndex].value;
    }
	if (fieldList) {
		if (fieldList.selectedIndex > 0) {
			loc += '&amp;tblfld='+fieldList[fieldList.selectedIndex].value;
			if (document.getElementById('search_term_fld')) {
				document.getElementById('search_term_fld').innerHTML='<em><strong>Loading Search Field . . .</strong></em>';
			}
		}
	}
	for (i=0;i<form.elements.length;i++) {
		var type = form.elements[i].type;
		var nm = form.elements[i].name;
		if ((type == 'checkbox') && (form.elements[i].checked)) { loc += '&amp;'+nm+'=1'; }
	}
    document.location=(loc);
	return false;
}

function checkTbl(form) {
	if (form.tbl) {
		if (form.tbl.selectedIndex == 0) { alert('Select a resource to search.'); } 
		else { form.submit(); }
	}
	return false;
}

function chkSearch(e,form) {
    if (window.Event) { var whichCode = e.which; } //NN
    else if (e.type == "keypress") {  var whichCode = e.keyCode; } // IE
    else { return; }
    if (whichCode == 13) { // only if enter is pressed
        form.submit();
    }
	return false;
}

function checkBoxes(form,onoff) {
   var sw = (onoff==1)?true:false;
   for (i=0;i<form.elements.length;i++) {
      var obj = form.elements[i];
	  if ((obj.type=='checkbox') && (obj.name != 'chkson') && (obj.name != 'chksoff')) {
	     obj.checked = sw;
	  }
   }
   if (onoff == 1) { form.chksoff.checked = false; }
   else { form.chkson.checked = false; }
}

// The following functions are used in editing items in admin, a popup window
// allows the task, then it closes and refreshes the edit window. NOTE: need to 
// do something about the add function, which refreshes the add page and adds another item.
function refreshSearch() {
	if (window.opener && !window.opener.closed) { window.opener.location.reload(); }
	window.close();
}
			   
function closeRefresh(pid,state,func) {
    if ((! pid) || (! state)) { alert('Source ID and display required to close and refresh.'); return; } 
    if (window.opener && !window.opener.closed) {
       //window.opener.document.location.reload()
	   
	   window.opener.document.location='atp-admin.cgi?'+func+'='+state+'&id='+pid; 
       window.close();
    }
}

// Add a list populated by script to the window opener filter textarea and close the current window.
// Used in mailing list functions.

	function addExcludes(form) {
		if (window.opener && !window.opener.closed) {
			if (window.opener.document.forms['compose-form'].filterlist.value!='') {
			window.opener.document.forms['compose-form'].filterlist.value+= ',';
			}
			window.opener.document.forms['compose-form'].filterlist.value+=form.filterlist.value;
			window.close();
		}
   }
   

