/*
'===========================================================================
'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
'04/13/2007		Jacob Stuart		920		Additions for AJAX form saving
'===========================================================================
*/

// Global Variables
var g_intTimer;
var g_intTimerCount;

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);
}

//--------------------------------------------------------------------------------------
//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)
//Date:		2/17/2004
//Author:    Daniel Ducat
//Revisions:	2/17/2004 - Created for WES 392
//--------------------------------------------------------------------------------------
var timeoutTimer;
function showTimeoutWarning() {
	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
//Type:		JavaScript
//Purpose:	This code gets the time zone offset and multiplies it times -1 to get the 
//          actual offset
//Date:		12/05/2006
//Author:    Kevin Weese
//--------------------------------------------------------------------------------------
  function getTimeZoneOffset() {
		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"; }
//***************************

//Removed from /Sponsor/Includes/RightClick.asp (File Deleted)
//************************
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if(isNS) document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
var EnableRightClick = 0;

document.oncontextmenu 	= HandleOther;
document.onkeypress 	= HandleKeyboard;
document.onmousedown 	= MouseClicked;
//document.onmouseup 		= MouseClicked;	

function HandleKeyboard(e) {
  var thisEvent = (isNS) ? e : window.event;
  if (thisEvent.keyCode==96)
    EnableRightClick = 1;
  return;
}
				
function HandleOther(){
				  if(EnableRightClick==1){ return true; }
				  else {
					if (isNS)   return false;
					}
				}

function MouseClicked(e){
	var sMessage="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.";
	if(EnableRightClick==1){ return true; }
	var thisEvent = (isNS) ? e : event;
	var eventbutton = (isNS) ? thisEvent.which : thisEvent.button;
		if((eventbutton==2)||(eventbutton==3)) {
				if (isNS) {
					alert(sMessage);
					}
				else {
					if (thisEvent.srcElement.id.substring(0,10).toLowerCase()=="childimage") alert(sMessage);
				}
				return false;
			}
}
//************************


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;
	}

