// generates the popup window
function popup(mylink, windowname) {
    if (! window.focus)return true;
    var href;
    if (typeof(mylink) == 'string')
        href=mylink;
    else
        href=mylink.href;
    window.open(href, windowname, 'width=450,height=450,scrollbars=yes');
    return false;
}

// closes the popup and returns the user to the original page
function targetopener(mylink, closeme, closeonly) {
    if (! (window.focus && window.opener))return true;
        window.opener.focus();
    if (! closeonly)
        window.opener.location.href=mylink.href;
    if (closeme)
        window.close();
return false;
}

function getTarget(element) {
    currentBox = $(element).previous();
    //alert('currentBox.name: '+currentBox.name);
}

function choosename(ln, fn, pid) {
    alert('currentBox.name: '+currentBox.name);
    if (window.opener && !window.opener.closed) {
        fullname = fn + " " + ln;
        window.opener.document.newrecord.currentBox.value = fullname;
        //window.opener.document.newrecord.interviewerid.value = pid;
    }
    window.close();
}

function clearname() {
    document.newrecord.interviewer.value = "";
    document.newrecord.interviewerid.value = "";
}

//Add more fields dynamically;  readroot identify what to display, writeroot identify where to display ;
var counter = 0;
//var bodyTag = document.getElementsByTagName('body');
//if (bodyTag[0].onload == 'initFields();')
    //counter = 0; 
function moreFields() {
    counter++;
	var newFields = document.getElementById('file_read').cloneNode(true);
	newFields.id = '';
	newFields.style.display = 'block';
    var newField = newFields.getElementsByTagName('input');
	//var newField = newFields.childNodes[1].childNodes; // given in example code, but would not work in IE. grrr.
	for (var i=0;i<newField.length;i++) {
		var theName = newField[i].name
        //alert(theName);
		if (theName) {
			newField[i].name = theName + counter;
            //alert(newField[i].name);
        }
	}
	var insertHere = document.getElementById('file_write');
	insertHere.parentNode.insertBefore(newFields,insertHere);
}

// used in edit.php to keep the counter in sync with any new fields added with moreFields.
function updateCounter(i) {
    counter = i;
    //alert('counter=' + counter);
}

//display the initial set of the expandable fields; called by new.php page as body onload;
function initFields() {
    window.onload = moreFields(); 
}

// give the user one last chance to get out of deleting a participant
function deleteCheck(lastname, firstname, suffix, letter, id) {
    if(suffix) {
        var answer = confirm('Are you sure you want to delete ' + firstname + ' ' + lastname + ', ' + suffix + '?');
    }
    else if(firstname) {
        var answer = confirm('Are you sure you want to delete ' + firstname + ' ' + lastname + '?');
    }
    else {
        var answer = confirm('Are you sure you want to delete ' + lastname + '?');
    }
    
    var cancelurl = 'http://lib11.library.vanderbilt.edu/rpw/participants.php?letter=' + letter;
    var okurl = 'http://lib11.library.vanderbilt.edu/rpw/participants.php?letter=' + letter + '&deleteid=' + id;
    if (answer) { // is the user okays the delete, send them to the delete url
        //alert(okurl);
        window.location = okurl;
    } 
    else {  // if the user cancels the delete, return them to their last search
        //alert(cancelurl);
        window.location = cancelurl;
    }
}

// the next four functions are used for the rollover images.  auto-generated by dreamweaver.
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];}
}

// dumps the user back to wherever they left off in the results list
function scrollToCoordinates(x, y) {
    window.scrollTo(x, y);
}
// saves the user's current spot on the page for later
function saveScrollCoordinates() {
    var x = (document.all)?document.body.scrollLeft:window.pageXOffset;
    var y = (document.all)?document.body.scrollTop:window.pageYOffset;
    return (x + ", " + y);
} 