function selectAll(checkbox)
{

   /* only one item in list */	
   if(checkbox.length == null){
	checkbox.checked = true;
	return;
   }


   for(var x = 0; x < checkbox.length; x++){
	checkbox[x].checked = true;

   }
}

function deselectAll(checkbox)
{
   /* only one item in list */	
   if(checkbox.length == null){
	checkbox.checked = false;
	return;
   }


   for(var x = 0; x < checkbox.length; x++){
	checkbox[x].checked = false;

   }
  
}


function deleteSelectedContent(checkbox)
{

    var url = "/tms-bin/delete_content.cgi?multi_ids="
    var multi_ids = "";


    if(checkbox.length == null){
 	 if(checkbox.checked == true){
	   multi_ids = checkbox.value;
         }
    }
    else{
        for(var x = 0; x < checkbox.length; x++){
	  if(checkbox[x].checked == true){
	   	multi_ids += checkbox[x].value + "+";
           }

         }
    } 


    if(multi_ids.length == 0){
	return false;
    }
	
  
    if(confirm('Delete item(s)?')){
	this.location.href = url + multi_ids;

	return false;
    }

    return false;

}



function setSavedContentFilter(form)
{

	var url = "/tms-bin/manage_content.cgi?date_filter=";
	
	if(form.filter_date.selectedIndex > 0){
		url += form.filter_date[form.filter_date.selectedIndex].value
		this.location.replace(url);
	}
}


function setParentState(start_idx)
{
	var childState;
	var allChecked;

	childState = this.document.forms['advsearch'].wire[start_idx].checked;
	allChecked = this.document.forms['advsearch'].wire[start_idx].checked;


	var x = start_idx - 1;

	/** Walk back to base wire. If this wire was unchecked and
          * we find another unchecked subwire, we can stop because the
          * basewire was already unchecked/
          */
	 while(x >= 0 && this.document.forms['advsearch'].wire[x].value != "HDR"){
		if(this.document.forms['advsearch'].wire[x].checked == false){
			if(childState == false){
			   return;
			}
			allChecked = false;
		}
		
		x--;
	}


	//We should now be sitting on the basewire
        if(x < 0){
             alert("Could not find base wire.");
             return;
        }


	//If a subwire was desleected, deselect basewire
        if (childState == false){
	       	this.document.forms['advsearch'].wire[x].checked = false;	
		return;
	}


	var base = x;
	x = start_idx + 1;

	/** Scan the rest of the wires in this group. If they
          * all are selected check the base wire.  If we find one that
          * is unchecked, we can stop.
         */
        while(x < this.document.forms['advsearch'].wire.length && this.document.forms['advsearch'].wire[x].value != "HDR"){
	        if(this.document.forms['advsearch'].wire[x].checked == false){
                	return;
                }
                x++;
         }
         
	this.document.forms['advsearch'].wire[base].checked = allChecked;
	
}




function toggleServices(start_idx)
{
	var     x = 0;
	var     setChecked;

	
	x = start_idx + 1;


	setChecked = this.document.forms['advsearch'].wire[start_idx].checked;


	while(x < this.document.forms['advsearch'].wire.length && this.document.forms['advsearch'].wire[x].value != "HDR"){	

		this.document.forms['advsearch'].wire[x].checked = setChecked;

		x++;
	}

}


function openEmailPopup(winName, newUrl)
{

  	var   newWin;

        newWin = window.open(newUrl, winName, 'toolbar=no,location=no,menubar=no,personalbar=no,resizable=yes,scrollbars=no,width=720,height=320');
        newWin.moveTo(0, 0);
        newWin.focus();



}

function openNewWindowPrint(winName, newUrl)
{
        var   newWin;

        newWin = window.open(newUrl, winName, 'toolbar=no,location=no,menubar=yes,personalbar=no,resizable=yes,scrollbars=yes,width=775,height=550');
        newWin.moveTo(0, 0);
        newWin.focus();

}


function openSend2EditorialPopup(winName, newUrl)
{

  	var   newWin;

        newWin = window.open(newUrl, winName, 'toolbar=no,location=no,menubar=no,personalbar=no,resizable=yes,scrollbars=no,width=720,height=320');
        newWin.moveTo(0, 0);
        newWin.focus();


}




function saveTopic()
{
	var url = "";


 	url = "/tms-bin/save_topic.cgi?";


	var title = prompt('Save this search as:');


	if(title == null || title.length <= 0){
		return;
	}	

	url += "title=" + escape_string(title);

        this.location.href = url;	
	
}


function deleteTopics(form)
{


    var url = "/tms-bin/delete_topics.cgi?multi_ids="
    var multi_ids = "";


    if(form.tid.length == null){


        if(form.tid.checked == true){
	   multi_ids = form.tid.value;
        }
    }
    else{		
    	for(x = 0; x < form.tid.length; x++){

        	if(form.tid[x].checked == true){
	   		multi_ids += form.tid[x].value + "+";
        	}
	 }
    }


   
   if(multi_ids.length == 0){
	return false;
   }
	
  
   if(confirm('Delete topic(s)?')){
	this.location = url + multi_ids;

	return false;
   }

   return false;
	
}



function submitChangePassword(http_target)
{

     if(this.document.changepassword.password.value.length == 0){
	alert("Please specify current password");
        this.document.changepassword.password.focus();
        this.document.changepassword.password.select();
	return false;
     }

     if(!verifyPassword(this.document.changepassword)){
         return false;
     }


     return true;
}

function submitChangeEmail(http_target)
{

     if(!verifyEmail(this.document.changeemail)){
         return false;
     }


     return true;
}

function validateLogin(form)
{

	if(form.accountname.value.length == 0){
		alert("Please enter your username.");
		form.accountname.focus();
		return false;
	}

	
	if(form.password.value.length == 0){
		alert("Please enter your password.");
		form.password.focus();
		return false;
	}

	return true;

}


function verifyPassword(form)
{

        if(form.newpassword.value.length < 6){
                alert("Password must be at least six characters long.");
                form.newpassword.focus();
                form.newpassword.select();
                return false;
        }

        if(form.newpassword.value != form.verify.value){
                alert("Passwords don't match.");
                form.verify.focus();
                form.verify.select();
                return false;
        }

        return true;
}


function verifyEmail(form)
{

        if(!validate_email(form.new_email.value)){
		form.new_email.focus();
                form.new_email.select();
                return false;
        }

        if(form.new_email.value != form.verify.value){
                alert("E-mail addresses don't match.");
                form.verify.focus();
                form.verify.select();
                return false;
        }

        return true;
}




function validate_email(email)
{
	if(email.length == 0){
		alert("Please enter an E-mail address");
		return false;
	}

       if(email.match(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/) == null){
	
	alert("Please enter a valid E-mail address");
	return false;
    }


    return true;
}



function submitQuickSearch(form)
{
        var url = "";

        if(form.inputText.value.length == 0 || form.modified.value == 'false'){
		form.inputText.value = "";
        }

        url = "/tms-bin/blender.cgi?";
        url += "page=" + form.page.value;
        url += "&andq=" + escape_string(form.inputText.value);


	if(form.contentTypes.selectedIndex > 0){
		url += "&codes=";

		/* See pagelib.c:insert_toolbar() */

		/* Text */
		if(form.contentTypes.selectedIndex == 1){
			url += "&notq=" + escape_string(form.contentTypes[form.contentTypes.selectedIndex].value);
		}
		/* Text w/images */
		else if(form.contentTypes.selectedIndex == 2){
			url += " pc/hasmedia";
			url += "&notq=" + escape_string(form.contentTypes[form.contentTypes.selectedIndex].value);
		}
		/* Photos, Illustrations & Caricatures, Editorial Cartoons */
		else if(form.contentTypes.selectedIndex > 2){
			url += " " + form.contentTypes[form.contentTypes.selectedIndex].value	
		}
	}


        this.location.href = url;

        return false;
}


function submitSOFSQsearch(form)
{

 	url = "/sofs-bin/sofs.cgi?";
        url += "page=" + form.page.value;
	url += "&andq=" + escape_string(form.andq.value);
        url += "&sourceSet=" + form.sourceSet.value;

	if(form.contentTypes.selectedIndex > 0){
		url += "&codes=";

		/* See pagelib.c:insert_toolbar() */

		/* Text */
		if(form.contentTypes.selectedIndex == 1){
			url += "&notq=" + escape_string(form.contentTypes[form.contentTypes.selectedIndex].value);
		}
		/* Text w/images */
		else if(form.contentTypes.selectedIndex == 2){
			url += " pc/hasmedia";
			url += "&notq=" + escape_string(form.contentTypes[form.contentTypes.selectedIndex].value);
		}
		/* Photos, Illustrations & Caricatures, Editorial Cartoons */
		else if(form.contentTypes.selectedIndex > 2){
			url += " " + form.contentTypes[form.contentTypes.selectedIndex].value	
		}
	}



	 this.location.href = url;

        return false;

}



function submitEditorialEmail(form, url)
{
	var x;
	var email = "";

	url += "?";

	for(x = 0; x < form.email.length; x++){
		if(form.email[x].checked == true){
		
			if(form.email[x].value == "writein"){
				email = form.writein.value;
			}
			else{
				email = form.email[x].value;
			}

	        	if(!validate_email(email)){
				return false;
			}

			url += "resid=" + form.resid.value;
	
			url += "&passthrough=" + escape_string(email);		
	
			url += "&title=" + escape_string(form.title.value);
	
			this.location.href = url;


			return false;
		}
	}


	alert("Email address not specified.");
	
	return false;
	

}


function submitAdvSearch(form)
{
	var qurl = convertForm2URL(form);
	
	if(qurl.length == 0){
		return false;
	}	

        this.location.href = "/tms-bin/blender.cgi?" + qurl;
	
        return false;
}


function saveModifiedTopic(form, title, id)
{

	var qurl = convertForm2URL(form);
	
	if(qurl.length == 0){
		return false;
	}	

	var url = "/tms-bin/save_topic.cgi?" + qurl;

        // ID for saved topic
	url += "&id=" + id;

	//Topic title
	url += "&title=" + escape_string(title);

	this.location.href = url;
}


function convertForm2URL(form)
{
	var dateType;
	var wire_list = "";	
	var url = "";
        var ccodes = "";
	var tcodes = "";


        url += "page=" + form.page.value;


	if(form.orq.value.length > 0){
	        url += "&orq=" + escape_string(form.orq.value);
	}

	if(form.andq.value.length > 0 || form.wordCount.selectedIndex > 0){
		var andq = "";

		if(form.andq.value.length > 0){
			andq = escape_string(form.andq.value);
		}

		if(form.wordCount.selectedIndex > 0){
			andq += " " + form.wordCount[form.wordCount.selectedIndex].value;
		}
		

	        url += "&andq=" + andq;
	}

	if(form.notq.value.length > 0){
	        url += "&notq=" + escape_string(form.notq.value);
	}



        /* Content Type */
	var idx = 0;

	for(var x = 0; x < form.contentTypes.length; x++){
		if(form.contentTypes[x].checked == true){
			idx = x;
			break;
		}
 	}	  
	
	if(idx > 0){
		/* See pagelib.c:insert_advanced_search_panel() */
		if(idx == 1){
			/* In there are no NOT terms, we need to set the not var,
			   otherwise just concatenate to URL string which already
			   has NOT term string.
			*/
			if(form.notq.value.length <= 0){
				 url += "&notq="
			}
			url += escape_string(form.contentTypes[idx].value);
		}
		else if(idx == 2){
			/* In there are no NOT terms, we need to set the not var,
			   otherwise just concatenate to URL string which already
			   has NOT term string.
			*/
			if(form.notq.value.length <= 0){
				 url += "&notq="
			}
			url += escape_string(form.contentTypes[idx].value);

			url += "&codes=pc/hasmedia";
		}	
		else if(idx > 2){
			url += "&codes=" + escape_string(form.contentTypes[idx].value);
		}
        }




	/* The zero position is the 'ALL' choice */
/***VERSION_20
	if(form.topicsearch[0].checked == false){
	   

           for(var x = 0; x < form.topics.length; x++){
		if(form.topics[x].checked == true){
			tcodes += form.topics[x].value + " ";
		}
	   }	  
	}


	if(form.typechoice[0].checked == false){
	  

           for(var x = 0; x < form.contentTypes.length; x++){
		if(form.contentTypes[x].checked == true){
			ccodes += form.contentTypes[x].value + " ";
		}
	   }	  
	}



	if(tcodes.length > 0 || ccodes.length > 0){
	    url += "&codes=";


	    if(tcodes.length > 0){
		url += escape_string(tcodes);
	    }


	    if(ccodes.length > 0){
		url += escape_string(ccodes);
	    }
        }
******/



	//Date Type
	for(var x = 0; x < form.datechoice.length; x++){
		if(form.datechoice[x].checked == true){
			dateType = form.datechoice[x].value;
			break;
		}
	}


	//Range Date
	if(dateType == 0){
		url += "&date_filter=" + form.date_range[form.date_range.selectedIndex].value;
        }



  	// After Date
	if(dateType == 1){
		var dateStr;

	        dateStr = formatDate(form.afterYear, form.afterMonth, form.afterDay);

		if(dateStr != "" && validDate(dateStr) == false){
			alert("Invalid Date");
			form.afterMonth.focus();
			return false;
		}
		else if(dateStr != ""){
			url += "&date_filter=>" + dateStr;
		}
		else{
			dateType = 0;	
		}
	}


	// Before Date
	if(dateType == 2){
		var dateStr;

	        dateStr = formatDate(form.beforeYear, form.beforeMonth, form.beforeDay);

		if(dateStr != "" && validDate(dateStr) == false){
			alert("Invalid Date");
			form.beforeMonth.focus();
			return false;
		}
		else if(dateStr != ""){
			url += "&date_filter=<" + dateStr;
		}	
		else{
			dateType = 0;	
		}
	}


	// Add date type to the url
	url += "&date_type=" + dateType;


	// Add Source set
	for(var x = 0; x < form.sourceSet.length; x++){
		if(form.sourceSet[x].checked == true){
			url += "&sourceSet=" + form.sourceSet[x].value;
			break;
		}
	}

	// Check if specific sources are set.
	if(form.sourceSet[3].checked == true){
		for(var x = 0; x < form.wire.length; x++){
			if(form.wire[x].checked == true){
				if(form.wire[x].value != "HDR"){
					wire_list += form.wire[x].value + " ";
				}
			}
		}

		if(wire_list.length == 0){
			alert("No providers have been selected");
			return "";
		}

		url += "&wire=" + wire_list;	
	}


	return url;
}


function escape_string(inStr)
{
    var x;
    var outStr = "";

    for(x = 0; x < inStr.length; x++){
       switch(inStr.charAt(x)){
       case '+':
           outStr += "%2B";
           break;
       case '>':
           outStr += "%3E";
           break;
       case '^':
           outStr += "%5E";
           break;
       case '{':
           outStr += "%7B";
           break;
       case '}':
           outStr += "%7D";
           break
       case '<':
           outStr += "%3C";
           break;
       case '\\':
           outStr += "%5C";
           break;
       case '`':
           outStr += "%60";
           break;
       case '|':
           outStr += "%7C";
           break;
       case '"':
           outStr += "%22";
           break;
       case '&':
           outStr += "%26";
           break;
       case '#':
           outStr += "%23";
           break;
       case '%':
           outStr += "%25";
           break;
       case ' ':
           outStr += "%20";
           break;
        case '/':
           outStr += "%2F";
           break;
       default:
           outStr += inStr.charAt(x);
           break;
       }
    }

    return outStr;
}


function setInitialAdvFormState(form)
{
    /* Expand Topics if neeeded */
/***VERSION_20
    for(var x = 0;  x  < form.topics.length; x++){
	if(form.topics[x].checked == true){
		form.topicsearch[1].checked = true;
		reveal('topiclist');
		break;
        }

    }
****/
    /* Expand content type if neeeded */

/***VERSION_20
    for(var x = 0;  x  < form.contentTypes.length; x++){
	if(form.contentTypes[x].checked == true){
		form.typechoice[1].checked = true;
		reveal('typelist');
		break;
        }
    }
****/

   /* Check 'All Content'[0] option if no others are seleted */
   for(var x = 0; x < form.contentTypes.length; x++){
	if(form.contentTypes[x].checked == true){
		break;
	}
   }

   if(x >= form.contentTypes.length){
	form.contentTypes[0].checked = true;
   }


   /* Expand sources if neeeded */
    if(form.sourceSet[3].checked == true){
	    for(var x = 0;  x  < form.wire.length; x++){
		if(form.wire[x].checked == true){
			form.sourceSet[4].checked = true;
			reveal('providerlist');
			break;
	        }
    	    } 
    }


}




function formatDate(Year, Month, Day)
{
	var fDate = "";
	
	// Ignore if partial date
	if(Year.selectedIndex && Month.selectedIndex && Day.selectedIndex){
		// YYYYMMDD	
		fDate += Year[Year.selectedIndex].value;
		fDate += Month[Month.selectedIndex].value;
		fDate += Day[Day.selectedIndex].value;

		return fDate;
	}

	return "";
}



function validDate(DateStr){
	var Datevalue = "";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;

	/* YYYYMMDD */
        DateValue = DateStr;

	if (DateValue.length != 8) {
	      err = 19;
   	}

	/* year is wrong if year = 0000 */
	year = DateValue.substr(0,4);
	if (year == 0) {
	      err = 20;
	}

	/* Validation of month*/
	month = DateValue.substr(4,2);
	if ((month < 1) || (month > 12)) {
	      err = 21;
	}

	/* Validation of day*/
	day = DateValue.substr(6,2);
	if (day < 1) {
	     err = 22;
	}

	/* Validation leap-year / february / day */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
	      leap = 1;
        }

	if ((month == 2) && (leap == 1) && (day > 29)) {
	      err = 23;
	}

	if ((month == 2) && (leap != 1) && (day > 28)) {
	      err = 24;
	}

	/* Validation of other months */
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
	      err = 25;
	}

	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
	      err = 26;
	}
	

       if (err != 0) {
	     return false;
       }

       return true;
}


function modified(oForm) {
	var el, opt, hasDefault, i = 0, j;

	while (el = oForm.elements[i++]) {
		switch (el.type) {
			case 'text' :
			case 'textarea' :
			case 'hidden' :
			if (!/^\s*$/.test(el.value) && el.value != el.defaultValue) return true;
			break;

			case 'checkbox' :
			case 'radio' :
			if (el.checked != el.defaultChecked) return true;
			break;

			case 'select-one' :
			case 'select-multiple' :
			j = 0, hasDefault = false;
			while (opt = el.options[j++])
			if (opt.defaultSelected) hasDefault = true;
			j = hasDefault ? 0 : 1;
			while (opt = el.options[j++])
			if (opt.selected != opt.defaultSelected) return true;
			break;
		}
	}
	return false;
}



/***********************************************
* IFrame SSI script- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of original DHTML scripts
* This notice must stay intact for legal use
***********************************************/

//Input the IDs of the IFRAMES you wish to dynamically resize to match its content height:
//Separate each ID with a comma. Examples: ["myframe1", "myframe2"] or ["myframe"] or [] for none:
var iframeids=["related"]

//Should script hide iframe from browsers that don't support this script (non IE5+/NS6+ browsers. Recommended):
var iframehide="yes"

var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
var FFextraHeight=parseFloat(getFFVersion)>=0.1? 16 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers
var IEextraHeight=18; // extra height, in px, for Internet Explorer

function dyniframesize() {
var dyniframe=new Array()
for (i=0; i<iframeids.length; i++){
if (document.getElementById){ //begin resizing iframe procedure
dyniframe[dyniframe.length] = document.getElementById(iframeids[i]);
if (dyniframe[i] && !window.opera){
dyniframe[i].style.display="block"
if (dyniframe[i].contentDocument && dyniframe[i].contentDocument.body.offsetHeight) //ns6 syntax
dyniframe[i].height = dyniframe[i].contentDocument.body.offsetHeight+FFextraHeight;
else if (dyniframe[i].Document && dyniframe[i].Document.body.scrollHeight) //ie5+ syntax
dyniframe[i].height = dyniframe[i].Document.body.scrollHeight+IEextraHeight;
}
}
//reveal iframe for lower end browsers? (see var above):
if ((document.all || document.getElementById) && iframehide=="no"){
var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i])
tempobj.style.display="block"
}
}
}

if (window.addEventListener)
window.addEventListener("load", dyniframesize, false)
else if (window.attachEvent)
window.attachEvent("onload", dyniframesize)
else
window.onload=dyniframesize



