﻿// AJAX HTTPREQUEST based send page link js by davidleghorn.com
// this js file replaces sendToFriend.js and sendToFriend2.js
// This file renames functions for generic use (rather than for sending Link links as originally used)

//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;

var SendPopupDivWidth = 280; // Width of send page link popup div (including any padding/borders)
var ValidationFailTextboxBgColour = "#ffcc00";
var SendPageLinkNormalTextboxBgColour = "#ffffff";

// global variables storing form input values

var friendsEmail = "";
var sendersEmail = "";
var sendersName = "";
var pageLinkUrl = "";
var validationMessageSendLink = "";


// SEND Link TO FRIEND BUTTON ON CLICK HANDLER

function SendToFriendBtnClick()
{
	clearValidationMessages();
	friendsEmail = "";
	sendersEmail = "";
	sendersName = "";
	
	getUserInput();

	var inputValid = validateSendLinkInput();
	var linkUrl = "";

	if( inputValid == true )
	{
		var pageTitle = document.title;
		linkUrl = this.location.href;
        // function below defined in page sending link
        SendPageLink(sendersName,friendsEmail,sendersEmail,linkUrl,pageTitle);
	}

}


// 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 validateSendLinkInput()
{
	var boolInputValid = true;

	if( friendsEmail == "" )
	{
		boolInputValid = false;
		validationMessageSendLink = "<li> * Please complete</li>.";
		document.getElementById("friendsEmailMsg").style.display = "inline";
		document.getElementById("friendsEmailTxtBox").style.backgroundColor = ValidationFailTextboxBgColour;	
	}

	if( sendersEmail == "" )
	{
		boolInputValid = false;
		validationMessageSendLink = "<li> * Please complete</li>";
		document.getElementById("sendersEmailMsg").style.display = "inline";
		document.getElementById("sendersEmailTxtBox").style.backgroundColor = ValidationFailTextboxBgColour;
	}

// senders name will not be mandatory
//	if( sendersName == "" )
//	{
//		boolInputValid = false;
//		validationMessageSendLink = "<li> * Please complete</li>";
//		document.getElementById("sendersNameMsg").style.display = "inline";
//	}

	
	if ( !isEMailAddr( friendsEmail ) )
	{
		boolInputValid = false;
		validationMessageSendLink += "<li> Correct invalid friends email address!</li>";
		document.getElementById("friendsEmailTxtBox").style.backgroundColor = ValidationFailTextboxBgColour;	
	}

	if ( !isEMailAddr( sendersEmail ) )
	{
		boolInputValid = false;
		validationMessageSendLink += "<li> Correct invalid Senders email address!</li>";
		document.getElementById("sendersEmailTxtBox").style.backgroundColor = ValidationFailTextboxBgColour;
	}


	// show correction message

	if( boolInputValid == false )
	{
		document.getElementById("messageRow").innerHTML = "<ul>" + validationMessageSendLink + "</ul>";
		document.getElementById("messageRow").style.display = "block";
	}

	return boolInputValid;

}


// CLEAR VALIDATION MESSAGES

function clearValidationMessages()
{
	validationMessageSendLink = "";
	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";
	// rest validation bg colour
	document.getElementById("friendsEmailTxtBox").style.backgroundColor = SendPageLinkNormalTextboxBgColour;
	document.getElementById("sendersEmailTxtBox").style.backgroundColor = SendPageLinkNormalTextboxBgColour;
    	
}


// 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 ShowSendingPageLinkMessage()
{
	document.getElementById("messageRow").style.display = "block";
	document.getElementById("messageRow").innerHTML = "<div class='sendingMessage'>Sending Page Link To Friend ....</div>";
	//setTimeout("closeSendArea()",2000);
	setTimeout("showSentMessage()",1000);

}

function showSentMessage()
{
	document.getElementById("messageRow").innerHTML = "<div class='sendingMessage'>Page Link Sent!</div>";
	setTimeout("CloseSendToFriend()",1000);
}


function fadeOutSendDiv()
{
    $("#sendToFriendWrapper").fadeOut("fast");
}


function closeSendArea()
{
	clearValidationMessages();
	CloseSendToFriend();
}


function showSendPageToFriend()
{
	document.getElementById("sendToFriendWrapper").style.visibility = "visible";
}


// 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
	{
        ShowHideObj('ModalSendToBg','visible');
		SetSendToYpos();
		document.getElementById(objId).style.visibility = "visible";
		document.getElementById(objId).style.top = yPos+"px";
        fadeInSendDiv();
	}
	catch(showEx)
	{
	    //alert("exception in show send div - " + showEx);
		// if exception occurred during positioning calculation or fade in
		// this will ensure set to visible and positioning will default to css settings
        document.getElementById(objId).style.visibility = "visible";
		document.getElementById(objId).style.top = "20px";
        ShowHideObj('ModalSendToBg','hidden');
		// alert("send to friend exception = "+ showEx.ToString() );
	}
}


function fadeInSendDiv()
{
    HorizontallyCenterDiv("sendToFriendWrapper", SendPopupDivWidth, 100)
    $("#sendToFriendWrapper").fadeIn("fast");
}


function CloseSendToFriend()
{
    ShowHideObj('ModalSendToBg','hidden');
    clearValidationMessages();
   // ShowHideObj('sendToFriendWrapper','hidden'); 
    fadeOutSendDiv();
}


// ----------------------------- NEW CODE TO ACCOMODATE USERS SCROLL OFFSET ------------------------

// if is ie, isIE set to true via ie conditional comment in page

var yPos = 100;

function SetSendToYpos()
{
	
	var winY = 100;
// can now get info below via get scroll y offset via get scroll y offset function in universal.js
	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 == true)
	{
		ah = document.documentElement.clientHeight;   // document.documentElement used doctype xhtml1.1 or strict
	}
	else
	{
		ah = window.innerHeight-20;
	}


	topOff = parseInt(ah * 0.3);

	return topOff;
}
