function visibilityToggletextbox(textbox,imgbox)
    {    
	var el=document.getElementById(textbox);
    var im=document.getElementById(imgbox);

   //el.style.display=(el.style.display == 'none')? 'none' : 'block';
    if (im.title=="add")  
        {
        im.src='/images/icons/delete.png';
        im.title='delete';
		el.style.display="block";
        }
    else
        {
        im.src='/images/icons/add.png';
        im.title='add';
		el.style.display="none";
        }
    }

function childrenvisibilityToggletextbox(textbox,imgbox)
    {    
	var el=document.getElementById(textbox);
    var im=document.getElementById(imgbox);

   //el.style.display=(el.style.display == 'none')? 'none' : 'block';
    if (im.title=="add")  
        {
        im.src='/images/levels/LCoordinator.gif';
        im.title='delete';
		el.style.display="block";
        }
    else
        {
        im.src='/images/levels/LCoordinator.gif';
        im.title='add';
		el.style.display="none";
        }
    }

//http://www.somacon.com/p355.php
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}

function reloadImage(image)
{
img = document.getElementById(image);
img.src = "pictures/"+ image + ".jpg";
}

function gethttpobject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

// Function called when changing the unmatched entries on the collaboration
// page.  Highlights the chosen unmatched for the user, his/her previous 
// partner choices and clears anything in the notification div 
// otherunmatchedinfo
function collabchoices(regcollabid)
	{
	for  (var i=0; i<document.collabpartner.mycollabs.length; i++)
		{
		var row = 'row' +i;
		var el=document.getElementById(row);
		if (document.collabpartner.mycollabs[i].checked)
			{
   			el.className='collab_selected';
			}
		else
			{
			el.className='blue';
			}
		}

	// ajax to highlight the potential matches for that option
    http=gethttpobject();
    http.open("GET", "/includes/js/collabsichose.php?regcollabid=" + regcollabid,true);
    http.send(null);
    http.onreadystatechange = handlecollabpartners;

	// ajax to highlight the potential matches for that option
    http2=gethttpobject();
    http2.open("GET", "/includes/js/collabsthatchoseme.php?regcollabid=" + regcollabid,true);
    http2.send(null);
    http2.onreadystatechange = handlecollabpotentialpartners;

	// blank the notification div
	document.getElementById('otherunmatchedinfo').innerHTML = '';


	}

function handlecollabpotentialpartners()
    {
    if (http2.readyState == 4)
        {
        if (http2.responseText.indexOf('invalid') == -1)
            {
            // Split the comma delimited response into an array
            results = http2.responseText.split(",");
            //var results = http2.responseText;
			//alert(http2.responseText);
			if (results['0'] >=1)
				{
				document.getElementById('wantstomatchme').innerHTML = '<br><font class=headersub>Collaboration Matches Made To Your Entry:</font><br><table>';	
				for  (var i=2; i<=results[0]; i=i+2)
					{
					var previousHTML = new String();
					previousHTML = document.getElementById('wantstomatchme').innerHTML;
					document.getElementById('wantstomatchme').innerHTML = previousHTML + results[i];
					//var x=i-1;
					//var check='cbox'+results[x];
					//var cl=document.getElementById(check);
					//cl.style.display='none';
					}

				var previousHTML = new String();
				previousHTML = document.getElementById('wantstomatchme').innerHTML;
				document.getElementById('wantstomatchme').innerHTML = previousHTML + '</table><input type=submit value="Choose a Match Partner">';	
				} // we have results
			else
				{
				document.getElementById('wantstomatchme').innerHTML = '';
				}
			
			}
		}
	}
function handlecollabpartners()
    {
    if (http.readyState == 4)
        {
        if (http.responseText.indexOf('invalid') == -1)
            {
            // Split the comma delimited response into an array
            //results = http.responseText.split(",");
            var results = http.responseText;
			for  (var i=0; i<document.collabpartner.you.length; i++)
				{
				var tosearch = ',' +document.collabpartner.you[i].value +',';
				var strsearch=results.search(tosearch);	
				var row = 'yourow' +i;
				var el=document.getElementById(row);
				if (strsearch !=-1)
					{
					document.collabpartner.you[i].checked=true;
   					el.className='collab_selected';
					}
				else
					{
					document.collabpartner.you[i].checked=false;
					el.className='blue';
					}
				}
					
            }
        }
    }

function test()
	{
	alert('testfunction');
	}

function getmoreevents(logid) 
{
 id= 'nextevents.' + logid;
  document.getElementById(id).innerHTML = ' Fetching data...';
  req=new gethttpobject();
  if (req != undefined) 
	{
    req.onreadystatechange = function() {getmoreeventsdone(id);};
    req.open("GET", "/includes/js/getevents.php?logid=" + escape(logid), true);
    req.send("");
  	}
return false;
}  

function getmoreeventsdone(id) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(id).innerHTML = req.responseText;
    } else {
      document.getElementById(id).innerHTML="Error:\n"+ req.status + "\n" +req.statusText;
    }
  }
}

function handleupdatenamephone() 
	{
  	if (http.readyState == 4) 
		{
		if (http.responseText.indexOf('invalid') == -1) 
			{
			// Split the comma delimited response into an array
			results = http.responseText.split(",");
			if (results[0])
				{
				document.getElementById('name').value = ltrim(results[0]);
				}
			if (results[1])
				{
				document.getElementById('phone').value = results[1];
				}
			}
		}
	}


function updatenamephone()
	{
	http=gethttpobject();
	email=document.getElementById("email").value;
	http.open("GET", "/includes/js/getnamephone.php?email=" + escape(email), true);
	http.onreadystatechange = handleupdatenamephone;
	http.send(null);
	}

	
function like(id,location)
	{
	http=gethttpobject();
	http.open("GET", "/includes/js/regcollabs.php?collabid=" + id+"&loc="+location,true);
	http.send(null);
	http.onreadystatechange = handlecollabupdate;
	}

function handlecollabupdate() 
	{
  	if (http.readyState == 4) 
		{
		if (http.responseText.indexOf('invalid') == -1) 
			{
			// Split the comma delimited response into an array
			results = http.responseText.split(",");
			if (results[0] == 1)
				{
				var previousHTML = new String();
				previousHTML = document.getElementById(results[1]).innerHTML;
				previousHTML = results[3] +'Help others partner with you. Give a bit more info about when you can do it, how you connect (dial in/out), and anything you have to offer as a partner.<br><form method=post><input type=text name=comments size=100 maxlength=100><input type=hidden name=regcollabid value='+results[2]+'><input type=hidden name=loc value=firstcomment><input type=submit value="Add Comments"></form>';
				document.getElementById(results[1]).innerHTML = previousHTML;
			
				} //results passed back
			}// we have a valid response text
		} //readystate =4
	} //handlecollabupdate


function updatecollabinterest(regid)
	{
    comments = document.getElementById(regid).value;
	//alert (comments);
	http=gethttpobject();
	http.open("GET", "/includes/js/updateregcomments.php?regid=" + regid + "&comments=" + escape(comments),true);
	http.onreadystatechange = handlecollabcommentupdate;
	http.send(null);
	}

function handlecollabcommentupdate() 
	{
  	if (http.readyState == 4) 
		{
		if (http.responseText.indexOf('invalid') == -1) 
			{
			// Split the comma delimited response into an array
			results = http.responseText.split(",");
			if (results[0] == 1)
				{
				var updatefield = "commenttextbox" + results[1];
				var el = document.getElementById("comment" + results[1]);
				document.getElementById(updatefield).innerHTML = results[2];
				el.style.display = 'none';
				} //results passed back
			}// we have a valid response text
		} //readystate =4
	} //handlecollabupdate

function updateemailname(value, comboBox) 
	{
	value = value.toLowerCase();
	http=gethttpobject();
	http.open("GET", "/includes/js/emailname.php?name=" + escape(value), true);
	http.onreadystatechange = handleupdateemailname;
	http.send(null);
	results = http.responseText.split(",");

	// could be replaced with "return items;", I'm synthesizing JS remoting
	setTimeout(function() {
		comboBox.setItems(results);
		}, 125);
	}

function handleupdateemailname() 
	{
  	if (http.readyState == 4) 
		{
		if (http.responseText.indexOf('invalid') == -1) 
			{
			// Split the comma delimited response into an array
			results = http.responseText.split(",");
			}
		}
	}


// Found at: 
// http://www.shawnolson.net/a/639/select-all-checkboxes-in-a-form-with-javascript.html
function checkUncheckAll(theElement) {
     var theForm = theElement.form, z = 0;
     for(z=0; z<theForm.length;z++){
      if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
      theForm[z].checked = theElement.checked;
      }
     }
    }


function showeventtime(obj) {
	//create a locally scoped variable to hold the currently selected option
var curproject=obj.options[obj.selectedIndex].value

if (curproject!='Project: Participants sign up and CAPspace matches them to partners'){
document.getElementById('eventtimezone').style.display='inline'
document.getElementById('projecttimezone').style.display='none'

//otherwise hide 
}else{
document.getElementById('eventtimezone').style.display='none'
document.getElementById('projecttimezone').style.display='inline'
	}
	} 

function visibilitydeletecollabinterest(textbox,imgbox)
    {
    var el=document.getElementById(textbox);
    var im=document.getElementById(imgbox);


   //el.style.display=(el.style.display == 'none')? 'none' : 'block';
    if (im.title=="delete")
        {
        im.src='/images/icons/delete-rotate.png';
        im.title='keep';
        el.style.display="block";
        }
    else
        {
        im.src='/images/icons/delete.png';
        im.title='delete';
        el.style.display="none";
        }
    }

function visibilitycollabinterestcomments (commentbox)
	{
   var el=document.getElementById(commentbox);
    if (el.style.display=="none")
        {
        el.style.display="block";
        }
    else
        {
        el.style.display="none";
		}
	}

function getselectedradiobutton(radio)
	{
	for (var i=0; i< radio.length; i++)
		{
		if (radio[i].checked)
			{
			return radio[i].value
			}
		}
	}

function youcollabcheck(row,regcollabid)
	{
	var meradio = document.collabpartner.mycollabs;
	var mevalue = getselectedradiobutton(meradio);
	// if nothing was selected from the radio buttons
	// it is possible that it was the single hidden value
	// so we try.
	if (!mevalue)
		{
		mevalue = meradio.value;
		}
    var yourow = 'yourow' +row;
    var el=document.getElementById(yourow);

	//alert (yourow);
	var youvalue = document.collabpartner.you[row].value;

	if (document.collabpartner.you[row].checked==true)
		{
        el.className='collab_selected';
		var action='add';
        //document.getElementById('otherunmatchedinfo').innerHTML = 'hello world';
		}
	else
		{
        el.className='blue';
		var action='remove';
		}
	http=gethttpobject();
    http.open("GET", "/includes/js/updatecollabpartner.php?you=" + escape(youvalue) + "&me=" +escape(mevalue) + "&action=" + escape(action) + "&regcollabid=" +escape(regcollabid), true);
    http.send(null);
    http.onreadystatechange = handlecollabcheck;

	}

function handlecollabcheck()
    {
    if (http.readyState == 4)
        {
        if (http.responseText.indexOf('invalid') == -1)
            {
            // Split the comma delimited response into an array
            results = http.responseText.split(",");
			if (results[1] == 'add')
				{
				document.getElementById('otherunmatchedinfo').innerHTML = "A request to match for the collaboration has been made to  <b>" + results[2] +  "</b>.  If the match is accepted, an email will be sent confirming the details";
				}
			if (results[1] == 'remove')
				{
				document.getElementById('otherunmatchedinfo').innerHTML = "A previous request for match with <b>" + results[2] + "</b> has been removed.";
				}
            }
        }
    }

// Updates a div with information based on the radio button chosen.
function collabregtypes (choice)
	{
	switch (choice)
		{
		//Open
		case 1:
			var updatechoice = "<b>Suggested:</b> An open collaboration is one actively looking for partners.  With our new exploding collaborations, not only can you find a partner, but others can connect with each other too!";
			break;
		//Filled
		case 0:
			var updatechoice = "If you have found a partner for your collaboration use filled.";
			break;
		//Past
		case -1:
			var updatechoice = "Use Past when the date of a collaboration has transpired";
			break;
		//Ideas
		case -2:
			var updatechoice = "Ideas are just like open collaborations, but are not posted on the CAPspace twitter feed.";
			break;
		}
		document.getElementById('collabregtypesinfo').innerHTML = updatechoice;
	
	}
