var NIE_CGI = "nie.cgi";
//var NIE_CGI = "nie-adam.cgi";

//var NT_HOST = "http://www.newsinessence.com";
var NT_HOST = "http://lada.si.umich.edu:8080/clair/nie1";

//var NIE_HOST = "http://www.newsinessence.com";
var NIE_HOST = "http://lada.si.umich.edu:8080/clair/nie1";

//var NIE_HOST = "http://perun.si.umich.edu/clair/nie";

var THIS_FILE = NIE_HOST + "/" + NIE_CGI;

function submitTrollForm(which_form, mode) {
    
    if (mode == "seed") {
	
	var url = which_form.url.value;
	if (url.length == 0) {
	    alert ("Please enter a seed URL.");
	    return false;
	}
	
	// wipe out the other.
	which_form.query.value = "";
	
	
    } else if (mode == "query") {
	
	var query = which_form.query.value;
	if (query.length == 0) {
	    alert ("Please enter a query.");
	    return false;
	}
	
	// wipe out the other.
	which_form.url.value = "";
	
    } else {
	alert ("Illegal mode argument!");
	return false;
    }
    
    
    // try to get an email address.
	/* var email = prompt */
/* 	    ("If you would like an email sent to you when " + */
/* 	     "NewsTroll finishes, enter your email address below. " + */
/* 	     "(NewsTroll will still run if you don't specify an email.)", */
/* 	      */
/* 	     ""); */
/*      */
/*     if (email != null) { */
/* 	which_form.email.value = email; */
/*     } */
    
    which_form.submit();
}

// TODO: make summarization a single method, separate from tracking.
function submitSummaryForm(which_form) {
    var cid = which_form.CID.value;

    var compression = getSelectedValue(which_form.compression);
    
    var email = prompt
	("If you would like an email sent to you when " +
	 "the summarization process completes, enter your email " +
	 "address below. " +
	 "(NIE will still summarize the cluster if you don't " +
	 "specify an email.)",

	"");

    if (email != null) {
        which_form.email.value = email;
    }

}

function summarizeCluster() {
    var cid = self.document.summary_form.CID.value;
    
    //var compression = self.document.summary_form.compression.value;
    var compression = getSelectedValue(document.summary_form.compression);
    
    // make sure we want to summarize...
    if (!confirm("Are you sure you want to summarize " +
		 "the cluster at " + compression + "% ?")) {
	return;
    }
    
    var includes = buildIncludes();
    
    if (includes.length == 0) {
	alert ("You must select at least one document to summarize!");
	return;
    }
   
    window.open(THIS_FILE + "?action=summarize" 
		+ "&CID=" + cid
		+ "&compression=" + compression
		+ includes,
		
		"_blank");
}

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

function emailSummary() {
    var email = document.summary_form.email.value;
    var cid = document.summary_form.CID.value;
    var compression =
    getSelectedValue(document.summary_form.compression);

    if (email.length == 0) {
        alert("Please enter an email address.");
        return;
    }

    // make sure we want to summarize...
    if (!confirm("Summarize cluster and send to " + email + "?"))
    {
        return;
    }

    var includes = buildIncludes();

    if (includes.length == 0) {
        alert ("You must select at least one document to summarize!");
        return;
    }

    window.open(THIS_FILE + "?action=summarize"
                + "&CID=" + cid
                + "&compression=" + compression
                + "&email=" + email
                + includes,

                "_blank");
}


// This will have to be fixed in order to
// work around Netscape's horrible inadequacies.
function signupForTracking() {
    var email = document.summary_form.email.value;
    //var time = document.summary_form.time.value;
    var time = getSelectedOptionValue(document.summary_form.time);
    var cid = document.summary_form.CID.value;

    if (email.length == 0) {
    	alert("Please enter an email address.");
       	return; 
    }

    if (time == "NOW") {
         emailSummary();
	 return;
    }
   
        // make sure we want to summarize...
	if (!confirm("Track this cluster at " + email + "?")) {
	    return;
	}

        var includes = buildIncludes();

        window.open(NT_HOST + "/" + NIE_CGI + "?action=tracking_signup"
                    + "&CID=" + cid
                    + "&email=" + email
		    + "&time=" + time,

                    "_blank");
    }

function redraw() {

    var cid = document.summary_form.CID.value;
    var sid = document.summary_form.SID.value; 
    var includes = buildIncludes();
	
    self.document.location = 
	THIS_FILE + "?SID=" + sid + "&CID=" + cid + "&selected=1" + includes;

}
    
function buildIncludes() {
	
	var str = "";
	
	//str += "&fun=now";
	//str += "&length=" + document.summary_form.did.length;
	
	if (! document.summary_form.did.length) {
	    if (document.summary_form.did.checked) {
		str += "&did=" + document.summary_form.did.value;
	    }
	}

	for (var i = 0; i < document.summary_form.did.length; i++) {
	    if (document.summary_form.did[i].checked) {
		str += "&did=" + document.summary_form.did[i].value;
	    }
	}

	return str;
}

function getSelectedRadioValue(buttonGroup) {
	for (var i = 0; i < buttonGroup.length; i++) {
	    if (buttonGroup[i].checked) {
		return buttonGroup[i].value;
	    }
	}
	return -1;
}

function buildCheckedValues(checkboxGroup) {

	var str = "";

	for (var i = 0; i < checkboxGroup.length; i++) {
	    if (checkboxGroup[i].checked) {
		str += "&source_names=" + checkboxGroup[i].value;
	    }
	}

	return str;
}

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