

// AJAX HTTPREQUEST based send to friend js by davidleghorn.com



var httpResponseHeader = "text";	// set via param in each http request call to identify what response content type is to be parsed 'text' or 'xml' (note text also includes html)

var HttpRequestObj;


// global variables storing form input values

var friendsEmail = "";
var sendersEmail = "";
var sendersName = "";
var pageLinkUrl = "";
var validationMessage = "";
var pageTitle = "";



// -- FUNCTION NOTES  FOR createRequestObject(): instantiates an http request object and returns reference to object or null if object could not be created

// REMARKS: FOR IMPROVED PERFORMANCE - branch to create depending on ie or non ie browser. use try catch for non ie browser
// and if catch fails, try creating a ms object anyways???? like wise if ie browser but try catch fails
// try and create an instance of a regular request object in case opera,ff or nav is presenting itself as ie???


function createRequestObject()
{

	var requestObj;


	if ( window.ActiveXObject ) 	// IE BROWSER
	{

		try 
 		{ 
   			requestObj = new ActiveXObject("Microsoft.XMLHTTP"); 
 		} 

 		catch (error) 
 		{ 

			try
			{
  				requestObj = new ActiveXObject("Msxml2.XMLHTTP");
 			} 
			
			catch(err)
			{
   				requestObj = null; 	// NO REQUEST OBJECT COULD BE INSTANTIATED
			}

 		}

	}


	else if ( window.XMLHttpRequest ) // NON IE BROWSER 
	{


		try	// try to create request obj for all non ie browsers
		{ 
			requestObj = new XMLHttpRequest(); 
		} 

		catch (error) 	// Error thrown if ie browser, no try creating specific ie active x objects
		{ 
			requestObj = null;
		}

	}

	else
	{
		//alert("httpRequest functionality is not supported by this browser - pop up window with send to aspx page should be displayed!");
		requestObj = null;
	}



	return requestObj;


}




// SUBMIT SEND TO FRIEND LINK BUTTON ON CLICK HANDLER

function sendPageLinkBtnClick()
{
	clearErrorMessages();
	friendsEmail = "";
	sendersEmail = "";
	sendersName = "";
	
	getUserInput();

	var inputValid = validateInput();
	var requestUrl = "";

	if( inputValid == true )
	{
		pageTitle = document.title;
		pageLinkUrl = escape(this.location.href);
		requestUrl = "http://www.qed-i.com/SendLinkToPage.aspx?FriendsEmail=" + escape(friendsEmail) + "&SendersEmail=" + escape(sendersEmail) + "&SendersName=" + escape(sendersName) + "&Link=" + pageLinkUrl + "&PageTitle=" + escape(pageTitle);

		//alert("input valid, request url = " + requestUrl);

		HttpRequestObj = createRequestObject();

		if( HttpRequestObj != null )
		{
			//alert("http request object created!");
			
			HttpRequestObj.open('GET',requestUrl,true);
			HttpRequestObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			// HttpRequestObj.onreadystatechange = function(){}	// currently not expecting to process a response
			HttpRequestObj.send(null);


			// show few seconds sending message then close
			showSendingMessage();
		}
		else
		{
			// http request not supported, open pop up and set url to send link aspx page
			// pop up has sending message and then auto closes itself after a few seconds, or reports
			// link sent and waits for user to close popup?
		}

	}

}


// GETS USER SEND TO FRIEND INPUT

function getUserInput()
{
	friendsEmail = document.getElementById("friendsEmailTxtBox").value;
	sendersEmail = document.getElementById("sendersEmailTxtBox").value;
	sendersName = document.getElementById("sendersNameTxtBox").value;
}


// VALIDATE FORM INPUT

function validateInput()
{

	var boolInputValid = true;

	if( friendsEmail == "" )
	{
		boolInputValid = false;
		validationMessage = "<li> * Please complete</li.";
		document.getElementById("friendsEmailMsg").style.display = "inline";	
	}

	if( sendersEmail == "" )
	{
		boolInputValid = false;
		validationMessage = "<li> * Please complete</li>";
		document.getElementById("sendersEmailMsg").style.display = "inline";
	}

// senders name will not be mandatory
//	if( sendersName == "" )
//	{
//		boolInputValid = false;
//		validationMessage = "<li> * Please complete</li>";
//		document.getElementById("sendersNameMsg").style.display = "inline";
//	}

	
	if ( !isEMailAddr( friendsEmail ) )
	{
		boolInputValid = false;
		validationMessage += "<li> Correct invalid friends email address!</li>";
	}

	if ( !isEMailAddr( sendersEmail ) )
	{
		boolInputValid = false;
		validationMessage += "<li> Correct invalid Senders email address!</li>";
	}


	// show correction message

	if( boolInputValid == false )
	{
		document.getElementById("messageRow").innerHTML = "<ul>" + validationMessage + "</ul>";
		document.getElementById("messageRow").style.display = "block";
	}

	return boolInputValid;

}


// CLEAR VALIDATION MESSAGES

function clearErrorMessages()
{
	validationMessage = "";
	document.getElementById("messageRow").innerHTML = "";
	document.getElementById("messageRow").style.display = "none";
	document.getElementById("friendsEmailMsg").style.display = "none";
	document.getElementById("sendersEmailMsg").style.display = "none";
	document.getElementById("sendersNameMsg").style.display = "none";
}


// VALIDATE EMAIL ADDRESSES



// VALIDATES THAT EMAIL ENTRY IS A VALID EMAIL ADDRESS 


function isEMailAddr( emailStr ) 
{
	var str = emailStr;

	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;

	if ( !str.match(re) ) 
	{
		//alert("Verify the e-mail address format.");
		//setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
		return false;
	}
	
	else 
	{
		return true;
	}


}


// SHOW BRIEF SENDING MESSAGE AND THEN CLOSE SEND TO FRIEND POP UP LAYER

function showSendingMessage()
{
	document.getElementById("messageRow").style.display = "block";
	document.getElementById("messageRow").innerHTML = "<div class='sendingMessage'>Sending Page Link To Friend ....</div>";
	//setTimeout("closeSendArea()",2000);
	setTimeout("showSentMessage()",2000);

}

function showSentMessage()
{
	document.getElementById("messageRow").innerHTML = "<div class='sendingMessage'>Page Link Sent!</div>";
	setTimeout("closeSendArea()",2000);
}


function closeSendArea()
{
	clearErrorMessages();
	document.getElementById("sendToFriendWrapper").style.visibility = "hidden";
}


function showSendPageToFriend()
{
	document.getElementById("sendToFriendWrapper").style.visibility = "visible";
}



// --- detect if this browser supports httpRequest onject
// if httpRequest object not supported, render css to hide Send Page To Friend button to gracefully degrade in 
// non compliant browsers


var ajaxSupport = createRequestObject();


// try
// {
//	if( typeof window.opera != "undefined" )
//	{
//		 ajaxSupport = null;	// opera8 actually supports ajax, but will not render send layer above an iframe! : - (
//	}
// }
// catch( err )
// {}


if( ajaxSupport == null)
{
	//alert("ajax = null not supported");	// testing

	document.write("<style type='text/css'>");
	document.write("#tellacolleagueIcon { display:none; }");
	document.write("</style>");
		
}



// Show hide layer function

function ShowHide(objid, vis)
{
	document.getElementById(objid).style.visibility = vis;
}


// SHOW SEND TO FRIEND POP UP - Calculate page center / scroll offset and move to page center

function ShowSendToFriend(objId)
{
	try
	{
		SetSendToYpos();
		document.getElementById(objId).style.visibility = "visible";
		document.getElementById(objId).style.top = yPos+"px";
	}
	catch(showEx)
	{
		// if exception occurred during positioning calculation
		// this will ensure set to visible and positioning will default to css settings
		document.getElementById(objId).style.visibility = "visible";

		// alert("send to friend exception = "+ showEx.ToString() );
	}
}


// ----------------------------- NEW CODE TO ACCOMODATE USERS SCROLL OFFSET ------------------------


var isIE = false;

// if is ie, isIE set to true via ie conditional comment in page

var yPos = 100;



function SetSendToYpos()
{
	
	var winY = 100;

	if( isIE == true )
	{
		winY = document.documentElement.scrollTop;
	}
		
	else
	{
		winY = window.pageYOffset;
	}
   

	var yOffset = getTopOffset();

	yPos = winY + yOffset;

	// update position of send to layer

	document.getElementById("sendToFriendWrapper").style.top = yPos+"px";
 
}


// top offset is 30% screen height

function getTopOffset()
{
	var topOff = 100;

	if(isIE)
	{
		ah = document.documentElement.clientHeight;   // document.documentElement used doctype xhtml1.1 or strict
	}
	else
	{
		ah = window.innerHeight-20;
	}


	topOff = parseInt(ah * 0.3);

	return topOff;
}


// assign onscroll handler

 
 window.onscroll = SetSendToYpos

