//Added for the captcha

var PUBLIC_KEY;
var ichost = window.location.href;
var captchaCgi;
var protocol = window.location.protocol;
//// End of global variables

function hideDiv(id)
{
	document.getElementById(id).style.display = 'none';
	document.getElementById(id).style.visibility = 'hidden';	
}

function displayDiv(id)
{
	document.getElementById(id).style.display = 'block';
	document.getElementById(id).style.visibility = 'visible';
}

function getPublicKey()
{
	if (ichost.indexOf("quod.lib.umich.edu") >= 0) 
	{
		return '6Lc74QIAAAAAALTmLQ4B4x5qaUbsI6BSxlwNG6t5';
	}
	else if (ichost.indexOf("umdl.umich.edu") >= 0) 
	{
		return '6Lc_4QIAAAAAADemEBAW2CxlZwIOF90T0j99hMvK';
	}
	else 
	{
		return '';
	}
}

function setCaptchaCgi()
{
	if (ichost.indexOf("quod.lib.umich.edu") >= 0 || ichost.indexOf("umdl.umich.edu") >= 0) 
	{
		captchaCgi = protocol + "//" + location.hostname + "/cgi/f/feedback/validatecaptcha";
	}
	else 
	{
		return '';
	}
}

function ToggleFeedbackForm( )
{	
  var me = document.getElementById( "FeedbackForm" );
  var me_linkOff = document.getElementById( "FBLinkDivOff" );
  var me_linkOn = document.getElementById( "FBLinkDivOn" );
  
  
  //   alert( me.style.display );

  if ( me.style.display === "none" ||  me.style.display === "" )
  {
    /* state is collapsed */
    me.style.display = "block";
    me_linkOff.style.display = "none";
    me_linkOn.style.display = "block";
    //    window.scroll( 0, 5000 );
  }
  else
  {
    /* state is expanded */    
    me.style.display = "none";
    me_linkOff.style.display = "block";
    me_linkOn.style.display = "none";     
  }
  
  if (PUBLIC_KEY === undefined)
  {
	PUBLIC_KEY = getPublicKey();
  }
  
  if (captchaCgi === undefined) 
  {
	  setCaptchaCgi();
  }
	
   setTimeout('Recaptcha.create("' + PUBLIC_KEY + '","reCAPTCHA", {theme: "white"})',5);
   hideDiv("ICFBBadResponse");
   hideDiv("ICFBCommFailure");
   hideDiv("EmptyICFB");
}

function ClickClear( thisfield, defaulttext )
{
  if ( thisfield.value == defaulttext ) 
  {
    thisfield.value = "";
  }
}

var callbackICFB = {
        success: function(o){
		captchaValidation = o.responseText;
	
		if(captchaValidation.indexOf("SUCCESS") >=0)
		{
			//Remove the captcha from the feedback form DOM	before emailing
			var frm = document.getElementById("icFBFeedbackForm");
			var olddiv = document.getElementById('reCAPTCHA');
			frm.removeChild(olddiv);
			frm.submit();
		}
		else 
		{		
			displayDiv("ICFBBadResponse");
			Recaptcha.reload();
		}
	},
	failure: function(o) {
		captchaValidation = "COMMUNICATION FAILURE";
		displayDiv("ICFBCommFailure");
	} 
};

function processICFBRequest()
{
	hideDiv("ICFBBadResponse");
	hideDiv("ICFBCommFailure");
	hideDiv("EmptyICFB");
	
	if (document.getElementById("comments").value === '') 
	{
		displayDiv("EmptyICFB");
	}	
	else 
	{
		var icfbfrm = document.getElementById("icFBFeedbackForm");
		var recaptcha_challenge = icfbfrm.recaptcha_challenge_field.value;
		var recaptcha_response = encodeURIComponent(icfbfrm.recaptcha_response_field.value);
		recaptchaArgs = "recaptcha_challenge_field=" + 
			recaptcha_challenge + ";recaptcha_response_field=" + 
			recaptcha_response;
		var request = YAHOO.util.Connect.asyncRequest('POST', captchaCgi, callbackICFB, recaptchaArgs);
	}
  	return false;
}

