/********************************************************************************
 *** Originally copied from JS_colleage.js, but greatly reduced!
 ********************************************************************************/
function checkDeleteFields()
{
  fields = getFields();
  var good = true;

  if(!notBlank(fields['reason'])){ good = false; }
  if(!notBlank(fields['verify'])){ good = false; }
  return good;
}


function submitDeleteRequest(){
   if(checkDeleteFields()==false)
	{alert('Please fill the required fields: reason and verify code.');}
   else
   {
      //document.bob.submit();
    }
}
 
 
function fillContactInfo()
{
  
  nameSelect=document.getElementsByName("requesterName")[0];
  remover=nameSelect.options[nameSelect.selectedIndex].value;
  removee=document.getElementsByName("Removee")[0].value;
  reason=document.getElementsByName("reason")[0].value;
  window.location="http://dendrome.ucdavis.edu/treegenes/RequestMgr/colleague2.php?type=delete&CIX="+removee+"&Remover="+remover+"&reason="+reason;
  window.location.replace();
}
//end Elly edited



function checkPiName(pi)
{
	if (pi.value != '') {
		return true;
	}
	else {
		return false;
	}
}

function checkPiEmail(em)
{
	return checkEmail(em);
}

function checkEmail(em)
{
    var emailExp = /\S+@\S+\.\S+/;
    var emailExp2 = /["<"|">"]/;
    res = emailExp.exec(em.value);
    res2 = emailExp2.exec(em.value);
    
    img = getIMG(em);
    if(res && !res2)
    {
		imageGreen(img);
		return true;
    }

    imageRed(img);
    return false;
}

function checkPhone(ph)
{
    var phExp = /^[\d\- \(\)]+$/;
    res = phExp.exec(ph.value);

    img = getIMG(ph);
    if(res)
    {
		imageGreen(img);
		return true;
    }

	imageRed(img);
	return false;
}

function checkOrg(O1)
{
    return notBlank(O1);
}

function notBlank(caller)
{
    img = getIMG(caller);
    if(caller.value != '')
    {
		s = new String(caller.value);
		s = s.replace(/'/g,"&apos;");
		s = s.replace(/"/g,"&quot;");
		caller.value=s;
		imageGreen(img);
		return(true);
    }
    else
    {
		imageRed(img);
		return(false);
    }
}

function getIMG(caller)
{
	// DEBUG.
	//alert('caller.name: ' + caller.name);

    current = caller;
    var row;
    while(current)
    {
		if(current.nodeName=='TR')
		{
			row=current;
			break;
		}
		current=current.parentNode;
    }

    var othertd;
    for(i=0;i<row.childNodes.length;i++)
    {
		if(row.childNodes[i].nodeName=='TD')
		{
			othertd = row.childNodes[i];
			break;
		}
    }

    for(i=0;i<othertd.childNodes.length;i++)
    {
		if(othertd.childNodes[i].nodeName=='IMG')
		{
			img = othertd.childNodes[i];
			break;
		}
    }
    return(img);
}
	    
function imageRed(img)
{
	img.src='/images/RedX.png';
}
function imageGreen(img)
{
	img.src='/images/GreenCheck.png';
}

function getFields()
{
	frm = document.forms['bob'];
	var arr = new Array();
	for(i=0;i<frm.elements.length;i++)
	{
		if(frm.elements[i].name)
		{
			var a = frm.elements[i].name;
			if(arr[a])
			{
				var target = arr[a];
				if(isArray(arr[a]))
				{
					var b = target;
					b.push = frm.elements[i];
					arr[a] = b;
				}
				else
				{
					var b = new Array();
					b[0]=target;
					b[1]=frm.elements[i];
					arr[a] = b;
				}
			}
			else
			{
				arr[a] = frm.elements[i];
			}
		}
	}
	return(arr);
}

function checkFields()
{
	fields = getFields();
	var good = true;

	if(!notBlank(fields['form_of_address'])){ good = false; }
	if(!notBlank(fields['surname'])){ good = false; }
	if(!notBlank(fields['given_name'])){ good = false; }
	if(!notBlank(fields['position'])){ good = false; }
	if(!notBlank(fields['pi_name'])){ good = false; }
	if(!checkEmail(fields['pi_email'])){ good = false; }

	if(!checkOrg(fields['organization'])){ good = false; }
	if(!notBlank(fields['department'])){ good = false; }
	if(!checkEmail(fields['e_mail'])){ good = false; }
	if(!checkPhone(fields['phone'])){ good = false; }
	if(!notBlank(fields['addr_1'])){ good = false; }
	if(!notBlank(fields['city'])){ good = false; }
	if(!notBlank(fields['state'])){ good = false; }

	if(!notBlank(fields['interest[]'][1])){ good = false; }
	if(!notBlank(fields['taxon[]'][1])){ good = false; }
	if(!notBlank(fields['species[]'][1])){ good = false; }

	if(good){
		return fields;
	}
	else {
		return false;
	}
}

function checkDeleteForm()
{
 alert("check deleteform");
}

function isArray(obj) {
	return obj.constructor == Array;
}

/* INCOMPATIBLE!!!
function isArray(obj) {
    if (obj.constructor.toString().indexOf("Array") == -1)
	return false;
    else
	return true;
}
*/

function fillFields(cix)
{
    AJAX_FillFields(cix);
}




function addNewLine(caller)
{
    var targetNode;
    for(i=0;i<caller.parentNode.childNodes.length;i++)
    {
		if(caller.parentNode.childNodes[i].nodeName=='INPUT' || caller.parentNode.childNodes[i].nodeName=='SELECT')
		{
			targetNode = caller.parentNode.childNodes[i];
		}
    }
    if(targetNode.value != '')
    {

		var clone = targetNode.cloneNode(true);
		clone.value='';
		caller.parentNode.insertBefore(clone, targetNode.nextSibling);
		caller.parentNode.insertBefore(document.createElement('br'), targetNode.nextSibling);

//		var cloned = clone(targetNode,caller.parentNode);
//		caller.parentNode.appendChild(caller);
//		return(clone);
    }
    else
    {
		alert('You may not add another item until you have filled out the current one.');
    }
}

function clone(target,container)
{
    var clone = target.cloneNode(true);
    container.appendChild(clone);
    clone.value='';
    return(clone);
}

/*
function addNewResearchInterestField()
{
	// Find the index of the next field.
	var index = 1;
	var objTarget;
	while (true) {
		objTarget = eval('document.bob.interest_' + index);
		if (typeof(objTarget) == "undefined") {
			break;
		}
		index++;
	}

	// Check if the previous field is filled in.
	objTarget = eval('document.bob.interest_' + (index - 1));
	if (objTarget.value == '') {
		alert('You may not add another item until you have filled out the current one.');
		return;
	}

	// Create the new field.
	var newNode   = objTarget.cloneNode(true);
//	newNode.name  = 'interest_' + index;
	newNode.name  = 'interest[]';
	newNode.value = '';
	objTarget.parentNode.insertBefore(newNode, objTarget.nextSibling);
	objTarget.parentNode.insertBefore(document.createElement('br'), objTarget.nextSibling);
}

function addNewGenusField()
{
	// Find the index of the next field.
	var index = 1;
	var objTarget;
	while (true) {
		objTarget = eval('document.bob.taxon_' + index);
		if (typeof(objTarget) == "undefined") {
			break;
		}
		index++;
	}

	// Check if the previous field is filled in.
	objTarget = eval('document.bob.taxon_' + (index - 1));
	if (objTarget.selectedIndex == 0) {
		alert('You may not add another item until you have made a selection for the current one.');
		return;
	}

	// Create the new field.
	var newNode   = objTarget.cloneNode(true);
//	newNode.name  = 'taxon_' + index;
	newNode.name  = 'taxon[]';
	newNode.options[0].selected = true;
	objTarget.parentNode.insertBefore(newNode, objTarget.nextSibling);
	objTarget.parentNode.insertBefore(document.createElement('br'), objTarget.nextSibling);
}

function addNewSpeciesField()
{
	// Find the index of the next field.
	var index = 1;
	var objTarget;
	while (true) {
		objTarget = eval('document.bob.species_' + index);
		if (typeof(objTarget) == "undefined") {
			break;
		}
		index++;
	}

	// Check if the previous field is filled in.
	objTarget = eval('document.bob.species_' + (index - 1));
	if (objTarget.selectedIndex == 0) {
		alert('You may not add another item until you have made a selection for the current one.');
		return;
	}

	// Create the new field.
	var newNode   = objTarget.cloneNode(true);
//	newNode.name  = 'species_' + index;
	newNode.name  = 'species[]';
	newNode.options[0].selected = true;
	objTarget.parentNode.insertBefore(newNode, objTarget.nextSibling);
	objTarget.parentNode.insertBefore(document.createElement('br'), objTarget.nextSibling);
}
*/

function submitCheck(type)
{
	var fields = checkFields();
	if(!fields) {
		alert('One or more of the fields are not filled out properly.');
	}
	else {
//		AJAX_submitInfo(fields);
		document.bob.submit();
	}
}

/*
function AJAX_submitInfo(fields) 
{ 
    var formVars = ajaxObj.getForm('bob');
    ajaxObj.call('action=AJAX_colleague'+formVars,afterSubmit);
}

function afterSubmit(resp)
{
	if(resp != '1')
	{
		alert(resp);
	}
	else
	{
		window.location='thanks.php';
	}
}
*/


