﻿// 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;


// 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()
{
	clearErrorMessages();
	friendsEmail = "";
	sendersEmail = "";
	sendersName = "";
	
	getUserInput();

	var inputValid = validateSendLinkInput();
	var linkUrl = "";

	if( inputValid == true )
	{
		pageTitle = document.title;
		linkUrl = escape(this.location.href);

        // function below efined in page sending link
        SendPageLink(friendsEmail,sendersEmail,linkUrl);
	}
	

}


// 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";	
	}

	if( sendersEmail == "" )
	{
		boolInputValid = false;
		validationMessageSendLink = "<li> * Please complete</li>";
		document.getElementById("sendersEmailMsg").style.display = "inline";
	}

// 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>";
	}

	if ( !isEMailAddr( sendersEmail ) )
	{
		boolInputValid = false;
		validationMessageSendLink += "<li> Correct invalid Senders email address!</li>";
	}


	// 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 clearErrorMessages()
{
	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";
}


// 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>";
	//CloseSendToFriend();
	setTimeout("CloseSendToFriend()",1000);
}


function fadeOutSendDiv()
{
    // FadeOpacity("sendToFriendWrapper",100,0,20,true);
    $("#sendToFriendWrapper").fadeOut("fast");
}


function closeSendArea()
{
	clearErrorMessages();
	CloseSendToFriend();
	//document.getElementById("sendToFriendWrapper").style.visibility = "hidden";
	//FadeOut('sendToFriendWrapper');
}

//function FadeOut(objid)
//{
//    var obj = $get(objid);                            //This is a shortcut for document.getElementById. It will return the javascript element. This is the element that you want to fade.
//    var target = new Sys.UI.Control(obj);               //The animation does not need a javascript element. It wants a Control. So use the element to instantiate the control.
//    var animation = new Sys.UI.FadeAnimation();         //Instantiate
//    animation.set_target(target);                       //An animation has to have a target to act upon.
//    animation.set_effect(Sys.UI.FadeEffect.FadeOut);    //This determines whether a fade out or fade in is going to be executed.
//    animation.set_duration(2);                          //How long the animation will last.
//    animation.set_fps(25);                              //How many frames per second the animation should execute.
//    animation.play();                                   //Once everything is setup, it can be executed.
//}

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();
		// try setting oipacity to ) bfore animate fade in
//		if(IsIE == true)
//		{
//		    //document.getElementById(objId).filters.alpha.opacity = 0;
//		    document.getElementById(objId).style.filter="alpha(opacity=0)";        
//		}
//		else
//		{
//		    document.getElementById(objId).style.MozOpacity = 0;
//		}
		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.visibility = "visible";
		document.getElementById(objId).style.top = "20px";
        ShowHideObj('ModalSendToBg','hidden');
		// alert("send to friend exception = "+ showEx.ToString() );
	}
}


function fadeInSendDiv()
{
    //FadeOpacity("sendToFriendWrapper", 0, 100, 20, false);

    HorizontallyCenterDiv("sendToFriendWrapper", 280, 100)
    $("#sendToFriendWrapper").fadeIn("fast");
}

//function FadeIn(objid)
//{
// var el = $get("sendToFriendWrapper");
//    AjaxControlToolkit.Animation.OpacityAction.play(el, 0, 30, 100);
////    var obj = $get(objid);                            //This is a shortcut for document.getElementById. It will return the javascript element. This is the element that you want to fade.
////    var target = new Sys.UI.Control(obj);               //The animation does not need a javascript element. It wants a Control. So use the element to instantiate the control.
////    var animation = new Sys.UI.FadeAnimation();         //Instantiate
////    animation.set_target(target);                       //An animation has to have a target to act upon.
////    animation.set_effect(Sys.UI.FadeEffect.FadeIn);    //This determines whether a fade out or fade in is going to be executed.
////    animation.set_duration(2);                          //How long the animation will last.
////    animation.set_fps(25);                              //How many frames per second the animation should execute.
////    animation.play();                                   //Once everything is setup, it can be executed.
//}



function CloseSendToFriend()
{
    ShowHideObj('ModalSendToBg','hidden');
    clearErrorMessages();
   // 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;
}