/// <reference path="jQuery/jquery.intellisense.js" />
/*
'===========================================================================
'Name:			Main.js
'Purpose:		General javascript functions for the website
'Author:		Selvakumar Palanisamy, Srinivas R Bheem Reddy
'Creation Date:	unknown
'
'Change History:
'Date		Author			STP#		Comments
'??/??/????	??				751			Removed from MasterINC.asp
'02/17/2004	Daniel Ducat	392			Created showTimeOutWarning
'12/05/2006	Kevin Weese		???			Created getTimeZoneOffset
'04/13/2007	Jacob Stuart	920			Additions for AJAX form saving
'05/22/2008	Jacob Stuart	549/986		Drop-down menu stuff
'10/07/2008	Jacob Stuart	986			I56313 - Change null to undefined
'03/20/2009	Jacob Stuart	I68766		Bug fix clean-up: fixed right-click
'05/14/2009	Jacob Stuart	I68766		Made to work with Opera as well.
'===========================================================================
*/

// Global Variables
var g_intTimer;
var g_intTimerCount;
var EnableRightClick = false;

$(function() {
	// Set-up Toolbar menus
	$('.IconItems')
	.superfish({
		onBeforeShow: function() {
			if ($(this).hasClass('Up')) {
				// Need to move menu up
				var objChild = $(this);
				objChild.css({ top: '-' + (objChild.height() + 5) });
			}
		}
	});
	// Image Discouragement
	$('img[id^="childimage"]').each(function() {
		$(this).bind('mousedown', function(event) {
			if (event.button == 2) {
				if (!EnableRightClick) {
					alert('Compassion has disabled the right click feature on \nphotographs of children on our site.\n\nThis additional effort has been implemented to protect\nthe security, dignity and privacy of children in our program.');
				}
			}
		});
		$(this).bind('contextmenu', function() {
			if (!EnableRightClick) {
				return false;
			}
		});
	});
});

function ShareThis(strSite, strTitle, strURL) {
	var strWindowURL = '';
	if (strURL === undefined) strURL = location.href;
	if (strTitle !== undefined) {
		strTitle = unescape(strTitle);
	}
	else {
		strTitle = document.title;
	}
	switch (strSite.toLowerCase()) {
		case 'delicious':
			strWindowURL = 'http://del.icio.us/post?v=4&noui&jump=close&url=' + encodeURIComponent(strURL) + '&title=' + encodeURIComponent(strTitle);
			break;
		case 'digg':
			strWindowURL = 'http://digg.com/submit?url=' + encodeURIComponent(strURL) + '&title=' + encodeURIComponent(strTitle) + '&media=news&topic=world_news';
			break;
		case 'mixx':
			strWindowURL = 'http://mini.mixx.com/submit/story?page_url=' + encodeURIComponent(strURL) + '&title=' + encodeURIComponent(strTitle);
			break;
		case 'reddit':
			strWindowURL = 'http://reddit.com/submit?url=' + encodeURIComponent(strURL) + '&title=' + encodeURIComponent(strTitle);
			break;
		case 'facebook':
			strWindowURL = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(strURL) + '&t=' + encodeURIComponent(strTitle);
			break;
		case 'stumbleupon':
			strWindowURL = 'http://www.stumbleupon.com/submit?url=' + encodeURIComponent(strURL) + '&title=' + encodeURIComponent(strTitle);
			break;
		case 'technorati':
			strWindowURL = 'http://technorati.com/faves?add=' + encodeURIComponent(strURL);
			break;
		case 'addthis':
			strWindowURL = 'http://www.addthis.com/bookmark.php?&url=' + encodeURIComponent(strURL) + '&title=' + encodeURIComponent(strTitle);
			break;
	}
	if (strWindowURL.length > 0) {
		window.open(strWindowURL, 'share', 'width=700,height=550,scrollbars=yes,toolbar=yes,menubar=yes,location=yes,status=yes,resizable=yes');
	}
}

function SendThisPage() 
{
	showSendAPage(sGuid,500,625);
}

function openFlash(sURL) 
{
	window.open(sURL,'','resizable');
}

function showSendAPage(sGuid,windowHeight,windowWidth ) 
{
	var xPos, yPos, sProps;
	
	if (document.all) {
		xPos = screen.width;
		yPos = screen.height;
		}
	else if (document.layers) {
		xPos = window.outerWidth;
		xPos = window.outerHeight; 
		}
	else {
		xPos = 640;
		yPos = 480; 
		}
	
	xPos = (xPos/2) - (windowWidth / 2);	
	yPos = (yPos/2) - (windowHeight / 2);
	
	sProps='scrollbars=no,status=no,location=no,toolbar=no';
	sProps=sProps+',width=' + windowWidth + ',height=' + windowHeight;
	sProps=sProps+',left=' + xPos + ',top=' + yPos ;
	window.open('/SendAPage.htm?Guid=' + sGuid + '&Page=' + sPage,'',sProps);
}

var timeoutTimer;
function showTimeoutWarning() {
	//--------------------------------------------------------------------------------------
	//Function:	showTimeoutWarning
	//Type:		JavaScript
	//Purpose:	This code schedules a timeout warning for 15 minutes in the future if the 
	//			user has a child selected, and they do not surf away from the page 
	//			currently being viwed.  Currently set to display after 900 seconds 
	//			have passed (15 minutes)
	//--------------------------------------------------------------------------------------
	alert("Your child sponsorship session will be discontinued in approximately five minutes if we do not detect any further activity.  Please continue the online sign-up process.  Otherwise, the child you have selected will no longer be reserved for your review.");
	clearTimeout(timeoutTimer)
}

function showTimeoutWarningPayment() {
	alert("Your session will be discontinued in approximately five minutes if we do not detect any further activity.  Please continue the online payment process.  Otherwise, the information you have entered into the form will need to be re-entered.");
	clearTimeout(timeoutTimer)
}
//***********************

function getTimeZoneOffset() {
	//--------------------------------------------------------------------------------------
	//Function:	getTimeZoneOffset
	//Type:		JavaScript
	//Purpose:	This code gets the time zone offset and multiplies it times -1 to get the 
	//          actual offset
	//--------------------------------------------------------------------------------------
	timeZoneDate = new Date();
	timeZoneOffset = timeZoneDate.getTimezoneOffset()/60*-1;
	return(timeZoneOffset);
}
//***********************

//Removed from Navigation.asp
//***************************
function popUp(url,width,height) {
	if (width == null || height == null) {
		sealWin=window.open(url,"win",'toolbar=0,location=1,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=400');
	} else {
		sealWin=window.open(url,"win",'toolbar=0,location=1,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width='+ width +',height=' + height);
	}
	self.name = "mainWin";
}
//***************************

// Right-click Bypass
$().keypress(function(event) {
	var keyboardCode = event.charCode || event.keyCode || 0;
	switch (keyboardCode) {
		case 96:
			EnableRightClick = true;
			break;
	}
});


function RegisterFormEvents( objForm, strEvent, strFunction ) {
	//--------------------------------------------------------------------------------------
	//	Function:	RegisterFormEvents
	//	Purpose:	Loop through a form and register an event for all the form elements
	//--------------------------------------------------------------------------------------
	for (var intIndex = 0; intIndex < objForm.elements.length; intIndex++) {
		AddEventHandler(objForm.elements[intIndex], strEvent, strFunction, false);
		}
	}

function AddEventHandler( objElement, strEvent, fncCalled, blnCapture ) {
	//--------------------------------------------------------------------------------------
	//	Function:	AddEventHandler
	//	Purpose:	Add an event listener depending on the browser's capability
	//--------------------------------------------------------------------------------------
	if (objElement.addEventListener) {
		objElement.addEventListener(strEvent, fncCalled, blnCapture);
		return true;
		}
	else if (objElement.attachEvent) {
		var blnReturn = objElement.attachEvent('on'+strEvent, fncCalled);
		return blnReturn;
		}
	else {
		return false;
		}
	}

function AddOnloadEvent( fncNewLoad ) {
	//--------------------------------------------------------------------------------------
	//	Function:	AddOnloadEvent
	//	Purpose:	Add an onload event without over-riding existing events. Not using
	//				AddEventHandler due to a problem with IE5 on a Mac
	//	Reference:	http://simonwillison.net/2004/May/26/addLoadEvent/
	//--------------------------------------------------------------------------------------
	var fncOldLoad = window.onload;
	if (typeof(window.onload) != 'function') {
		window.onload = fncNewLoad;
		}
	else {
		window.onload = function() {
			if (fncOldLoad) {
				fncOldLoad();
				}
			fncNewLoad();
			}
		}
	}
	
function FormPersistData( objForm, strFormPage, intResetTimerCount, fncResponse ) {
	//--------------------------------------------------------------------------------------
	//	Function:	FormPersistData
	//	Purpose:	Call to save form data via AJAX
	//	Parameters:
	//	  objForm				Reference to the document.form
	//	  strFormPage			String with the name of the page containing the form
	//	  intResetTimerCount	Counter for allowing the count-down of recursively calling the function
	//	  fncResponse			[optional] Function for calling when the post is successful
	//							NOTE: When calling, the function name should not contain '()'
	//--------------------------------------------------------------------------------------
	var strFormNameValues = GetFormParameters(objForm);
	strFormNameValues += 'FormPage=' + encodeURIComponent(strFormPage);
	XMLHttpPost( '/Includes/Functions/FormPersist.asp', strFormNameValues, fncResponse );
	clearTimeout(FormPersistTimer);
	if (intResetTimerCount > 1) FormPersistTimer = setTimeout('FormPersistData(document.' + objForm.name + ', \'' + strFormPage + '\', ' + (intResetTimerCount - 1) + ', ' + fncResponse + ')', g_intTimer);
	}

function GetFormParameters( objForm ) {
	//--------------------------------------------------------------------------------------
	//	Function:	GetFormParameters
	//	Purpose:	Loop through the form object that is passed, concatenating a string
	//				of name-value pairs for handing off in an XMLHTTPPost event
	//--------------------------------------------------------------------------------------
	var strFormParameters = '';
	for (var intIndex = 0; intIndex < objForm.elements.length; intIndex++) {
		switch(objForm.elements[intIndex].type) {
			case 'select-one':
				strFormParameters += encodeURIComponent(objForm.elements[intIndex].name) + '=' + encodeURIComponent(objForm.elements[intIndex].options[objForm.elements[intIndex].selectedIndex].value) + '&';
				break;
			case 'select-multiple':
				// Not being handled - needs to probably loop and build the string then tested
				// No current multi-selects in the form I'm using
				break;
			case 'checkbox':
			case 'radio':
				if (objForm.elements[intIndex].checked) strFormParameters += encodeURIComponent(objForm.elements[intIndex].name) + '=' + encodeURIComponent(objForm.elements[intIndex].value) + '&';
				break;
			default:
				strFormParameters += encodeURIComponent(objForm.elements[intIndex].name) + '=' + encodeURIComponent(objForm.elements[intIndex].value) + '&';
			}
		}
	return strFormParameters;
	}
