

var thishost = window.location.host;
lUrl = 'http://'+thishost+'/wpsubmit/backend/';

function CheckDoChangeAuthor (obj,id) {
    if ( obj.value == obj.lastvalue ) {
	return;
    }
    if ( obj.mybusy == 1 ) {
	return;
    }
    obj.mybusy=1;
    thefield='authorinfo'+id;
    var oldvalue;
    while(oldvalue != obj.value) {
	oldvalue = obj.value;
	if (  obj.value.length < 3 ) {
	    SetInnerHTML(window,thefield,'Please type at least 3 characters of the author\'s first or last name<br>');
	} else {
	    SetInnerHTML(window,thefield,'Please wait while we search for matching authors...<br>');
	    thecontent=lUrl+'search?searchstring='+obj.value+'&authid='+id;
	    SendChange(window,thefield,thecontent);
	}
    }
    obj.mybusy=0;
    obj.lastvalue=obj.value;
}

function emptyCheckboxArrayP (myarray) {
    if (myarray == null) {
	return false;
    }
    for ( idx = 0; idx < myarray.length; idx++ ) {
	var i = myarray[idx];
	if ( i.checked ) {
	    return false;    
	}
    }
    return true;
}


function verifyme (myform) {
    try {
	var errortxt="";
	var noauthors=1;
	var nofam=1;
	
	for ( i = 1; i < 6; i++ ) {
	    var authuserfield=myform['author_user'+i];
	    var authsearchfield=myform['authorsearch'+i];
	    var authuservalue =
		typeof(authuserfield) == "undefined" ? "" : authuserfield.value;
	    var authsearchvalue =
		typeof(authsearchfield) == "undefined" ? "" : authsearchfield.value;
	    
	    if ( authuservalue ) {
		// this author was found OK
		var isfam = myform['author_family'+i].value;
		if (isfam == 1) {
		    nofam=0;
		}
		if (! myform['author_email'+i].value) {
		    errortxt += 'You must provide an email address for author number '+i;
		}
		noauthors=0;
	    } else if( authsearchvalue ) {
		// search string but no author_user
		errortxt += 'It appears that you tried to search for author number '+i+' but did not select a matching listed author. This can also happen if you use the back button to return to this form.\n';
	    }
	}
	
	if (noauthors == 1) {
	    errortxt += 'You must specify at least one author in section A\n';
	} else {
	    if ( nofam == 1 && ! myform.conference_presentation.value ) {
		errortxt += 'Unless one of the authors is an NBER family member, you must specify what conference the paper was presented at in section G\n';
	    }
	}
	if (! myform.paper_title.value) {
	    errortxt += 'You must specify a title for your paper in section B\n';
	}
	if (emptyCheckboxArrayP(myform.jel)) {
	    errortxt += 'You must specify at least one JEL code in section C\n';
	}
	if (emptyCheckboxArrayP(myform.program)) {
	    errortxt += 'You must specify at least one Program in section D\n';
	}
	
	if ( errortxt ) {
	    alert( 'Please correct these errors before submitting this form:\n\n'+errortxt+'\n\nIf you still have difficulty, please email wpsubmit@nber.org' );
	    return false;
	}
    }
    
    catch (ex) {
	alert( 'An error occurred while attempting to verify your inputs: '+ex+'\nThis seems to happen with Firefox. Please try the following:\n 1. Click the Submit button again.\n 2. Reload the entire form and re-enter your submission.\n In either case, please be sure to wait until the page is completely finished loading before trying to do anything further.' );
	return false;
    }
    return true;
}

