function confLogout() {
	var r=confirm("You are about to logout, do you want to continue?");
	
	if (r==true) {
  		document.location.href='logout.php';
  	}	
	
}


function createObject() {
	var request_type;
	
	try{
		// Opera 8.0+, Firefox, Safari
		request_type = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			request_type = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				request_type = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your current browser of choice does not support the technologies required for online registration. Please use another browser.");
				return false;
			}
		}
	}

	return request_type;
}


function lightsOff() {
	
	errors = 0;
	
	//Do form validation first...
	if(document.getElementById('FirstName').value=="" && errors == 0) {
		alert('Please enter the donar\'s First Name');
		document.getElementById('FirstName').focus();
		errors++;
	}
	if(document.getElementById('LastName').value=="" && errors == 0) {
		alert('Please enter the donar\'s Last Name');
		document.getElementById('LastName').focus();
		errors++;
	}
	if(document.getElementById('Email').value=="" && errors == 0) {
		alert('Please enter the E-Mail Address');
		document.getElementById('Email').focus();
		errors++;
	}
	if (document.getElementById('Email').value.length != 0) {
		PosAT = document.getElementById('Email').value.indexOf('@',0)
		PosDot = document.getElementById('Email').value.indexOf('.', PosAT + 2)
		PosUlt = document.getElementById('Email').value.indexOf('.',document.getElementById('Email').value.length-1)	
		
		if (PosDot > -1) {
			CharsAfterDot = document.getElementById('Email').value.length - PosDot - 1
		} else {
			CharsAfterDot = -1 
			if ((PosAT == -1) || (PosDot == -1) || (CharsAfterDot <= 1) || (CharsAfterDot >= 10) || (PosUlt != -1)){
				alert('Please enter a valid e-mail address.');    
				document.getElementById('Email').focus();
				errors++;
			}
		}
	}		
	if(document.getElementById('BillAddress1').value=="" && errors == 0) {
		alert('Please enter the billing address');
		document.getElementById('BillAddress1').focus();
		errors++;
	}
	if(document.getElementById('BillCity').value=="" && errors == 0) {
		alert('Please enter the billing city');
		document.getElementById('BillCity').focus();
		errors++;
	}
	if(document.getElementById('BillState').value=="" && errors == 0) {
		alert('Please enter the billing state');
		document.getElementById('BillState').focus();
		errors++;
	}
	if(document.getElementById('BillZip').value=="" && errors == 0) {
		alert('Please enter the billing zip code');
		document.getElementById('BillZip').focus();
		errors++;
	}
	if(document.getElementById('CardBrand').value=="select" && errors == 0) {
		alert('Please select the credit / debit card type');
		document.getElementById('CardBrand').focus();
		errors++;
	}
	if(document.getElementById('CardName').value=="" && errors == 0) {
		alert('Please enter the name as it appears on the card');
		document.getElementById('CardName').focus();
		errors++;
	}
	if(document.getElementById('CardNumber').value=="" && errors == 0) {
		alert('Please enter the card number');
		document.getElementById('CardNumber').focus();
		errors++;
	}
	if(document.getElementById('CardCVV').value=="" && errors == 0) {
		alert('Please enter the card security code');
		document.getElementById('CardCVV').focus();
		errors++;
	}
	if(document.getElementById('legacyFund').value=="0" && errors == 0) {
		alert('Please enter a donation amount');
		document.getElementById('legacyFund').focus();
		errors++;
	}
		
	if(errors == 0) {
		//If okay, put address into verification area
		
		document.getElementById('verifyAddress1').innerHTML=document.getElementById('BillAddress1').value;
		document.getElementById('verifyAddress2').innerHTML=document.getElementById('BillAddress2').value;
		document.getElementById('verifyCity').innerHTML=document.getElementById('BillCity').value;
		document.getElementById('verifyState').innerHTML=document.getElementById('BillState').value;
		document.getElementById('verifyZip').innerHTML=document.getElementById('BillZip').value;
		document.getElementById('verifyCountry').innerHTML=document.getElementById('BillCountry').value;
		
		
		//Show verification bloxk
		document.getElementById('lightsOut').style.display='block';
		document.getElementById('lightsOut').style.visibility='visible';
	}
	
}

function lightsOn() {
	
	document.getElementById('lightsOut').style.display='none';
	document.getElementById('lightsOut').style.visibility='hidden';
	
}


function show_cardinformation() {

	document.getElementById('card_holder').style.display='';
	document.getElementById('money_text').style.display='';

}

var httpProcessPayment = createObject();

function processPayment() {
	
	lightsOn();
	showProcessing();	
	
	nocache = Math.random();
	
	//Get all the variables together
	var CardBrand=encodeURI(document.getElementById('CardBrand').value);
	var CardName=encodeURI(document.getElementById('CardName').value);
	var CardNumber=encodeURI(document.getElementById('CardNumber').value);
	var CardCVV=encodeURI(document.getElementById('CardCVV').value);
	var CardMonth=encodeURI(document.getElementById('CardMonth').value);
	var CardYear=encodeURI(document.getElementById('CardYear').value);	
	
	//Get Billing Information
	var FirstName=encodeURI(document.getElementById('FirstName').value);
	var LastName=encodeURI(document.getElementById('LastName').value);
	var Email=encodeURI(document.getElementById('Email').value);
	
	var BillAddress1=encodeURI(document.getElementById('BillAddress1').value);
	var BillAddress2=encodeURI(document.getElementById('BillAddress2').value);
	var BillCity=encodeURI(document.getElementById('BillCity').value);
	var BillState=encodeURI(document.getElementById('BillState').value);
	var BillZip=encodeURI(document.getElementById('BillZip').value);
	var BillCountry=encodeURI(document.getElementById('BillCountry').value);
	
	//Get Payment Parameters
	var LegacyFund=encodeURI(document.getElementById('legacyFund').value);
	
	var Parameters = "LegacyFund="+LegacyFund+"&CardBrand="+CardBrand+"&CardName="+CardName+"&CardNumber="+CardNumber+"&CardCVV="+CardCVV+"&CardMonth="+CardMonth+"&CardYear="+CardYear+"&FirstName="+FirstName+"&LastName="+LastName+"&Email="+Email+"&BillAddress1="+BillAddress1+"&BillAddress2="+BillAddress2+"&BillCity="+BillCity+"&BillState="+BillState+"&BillZip="+BillZip+"&BillCountry="+BillCountry;
	
	var url = "processCreditCard.php?nocache="+nocache;
			
	httpProcessPayment.open("POST", url, true);
	httpProcessPayment.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpProcessPayment.setRequestHeader("Content-length", Parameters .length);
	httpProcessPayment.setRequestHeader("Connection", "close");
			
			
	httpProcessPayment.onreadystatechange = function() {//Handler function for call back on state change.
		if(httpProcessPayment.readyState == 4) {
						
			var response = httpProcessPayment.responseText;
				
			if(response == "ok") { //If payment was okay
							
				window.location.href='donationComplete.php';
				
							
			} else {
				
				document.getElementById('errorMsg').className='Portal_Message_Red';
				document.getElementById('errorMsg').innerHTML=response;
				window.scrollTo(0,0); //Bring user to top of page
				hideProcessing();				
						
			}
						
						
		}	
	}
		
	httpProcessPayment.send(Parameters);
}

function showProcessing() {
	
	document.getElementById('processingBlock').style.display='block';
	document.getElementById('processingBlock').style.visibility='visible';
}

function hideProcessing() {
	document.getElementById('processingBlock').style.display='none';
	document.getElementById('processingBlock').style.visibility='hidden';
}

var httpProcessLogin = createObject();

function login() { 
	
	var Email=encodeURI(document.getElementById('logEmail').value);
	var Password=encodeURI(document.getElementById('logPassword').value);
	
	nocache = Math.random();
	
	showProcessing();
		
		var Parameters = "Email="+Email+"&Password="+Password;
	
		var url = "guestBookLoginProcess.php?nocache="+nocache;
				
		httpProcessLogin.open("POST", url, true);
		httpProcessLogin.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpProcessLogin.setRequestHeader("Content-length", Parameters .length);
		httpProcessLogin.setRequestHeader("Connection", "close");
				
				
		httpProcessLogin.onreadystatechange = function() {//Handler function for call back on state change.
			if(httpProcessLogin.readyState == 4) {
							
				var response = httpProcessLogin.responseText;
					
				if(response == "ok") { //If payment was okay
								
					window.location.href='guestBookSign.php';
					
								
				} else if(response == "error") {
					
					alert('Invalid e-mail address and / or password. Please try again.');
					hideProcessing();				
							
				} else {
						
					alert('Invalid e-mail address and / or password. Please try again.');
					hideProcessing();		
					
				}
				
							
			}	
		}
			
		httpProcessLogin.send(Parameters);
}

var httpProcessRegister = createObject();

function register() { 
	
	var error = "no";
	var type = "";
	
	nocache = Math.random();
	
	//Let's get all the information
	var FirstName=encodeURI(document.getElementById('regFirstName').value);
	var LastName=encodeURI(document.getElementById('regLastName').value);
	var Phone=encodeURI(document.getElementById('Phone').value);
	var Email=encodeURI(document.getElementById('regEmail').value);
	var Email2=encodeURI(document.getElementById('regEmail2').value);
	var Password=encodeURI(document.getElementById('regPassword').value);
	var Password2=encodeURI(document.getElementById('regPassword2').value);
	var Verification=encodeURI(document.getElementById('regVerify1').value);
	var Verification2=encodeURI(document.getElementById('regVerify2').value);
	
	if(FirstName == "" && error == "no") { //If no values
		alert('Please enter in your first name');
		document.getElementById('regFirstName').focus();
		error = "yes";
	}
	if(LastName == "" && error == "no") { //If no values
		alert('Please enter in your last name');
		document.getElementById('regLastName').focus();
		error = "yes";
	}
	if(Email == "" && error == "no") { //If no values
		alert('Please enter in your e-mail address');
		document.getElementById('regEmail').focus();
		error = "yes";
	}
	if (document.getElementById('regEmail').value.length != 0) {
		PosAT = document.getElementById('regEmail').value.indexOf('@',0)
		PosDot = document.getElementById('regEmail').value.indexOf('.', PosAT + 2)
		PosUlt = document.getElementById('regEmail').value.indexOf('.',document.getElementById('regEmail').value.length-1)	
		
		if (PosDot > -1) {
			CharsAfterDot = document.getElementById('regEmail').value.length - PosDot - 1
		} else {
			CharsAfterDot = -1 
			if ((PosAT == -1) || (PosDot == -1) || (CharsAfterDot <= 1) || (CharsAfterDot >= 10) || (PosUlt != -1)){
				alert('Please enter a valid e-mail address.');    
				document.getElementById('regEmail').focus();
				error = "yes";	
			}
		}
	}
	if(Email2 == "" && error == "no") { //If no values
		alert('Please enter in your e-mail address again');
		document.getElementById('regEmail2').focus();
		error = "yes";
	}
	if (document.getElementById('regEmail2').value.length != 0) {
		PosAT = document.getElementById('regEmail2').value.indexOf('@',0)
		PosDot = document.getElementById('regEmail2').value.indexOf('.', PosAT + 2)
		PosUlt = document.getElementById('regEmail2').value.indexOf('.',document.getElementById('regEmail2').value.length-1)	
		
		if (PosDot > -1) {
			CharsAfterDot = document.getElementById('regEmail2').value.length - PosDot - 1
		} else {
			CharsAfterDot = -1 
			if ((PosAT == -1) || (PosDot == -1) || (CharsAfterDot <= 1) || (CharsAfterDot >= 10) || (PosUlt != -1)){
				alert('Please enter a valid e-mail address.');    
				document.getElementById('regEmail2').focus();
				error = "yes";	
			}
		}
	}
	if(Email != Email2 && error == "no") { //If emails dont match
		alert('The email addresses entered do not match.');
		document.getElementById('regEmail2').focus();
		error = "yes";		
	}
	if(Password == "" && error == "no") { //If no values
		alert('Please enter in a password');
		document.getElementById('regPassword').focus();
		error = "yes";
	}
	if(Password2 == "" && error == "no") { //If no values
		alert('Please enter in the same password again');
		document.getElementById('regPassword2').focus();
		error = "yes";
	}
	if(Password != Password2 && error == "no") { //If emails dont match
		alert('The passwords entered do not match.');
		document.getElementById('regPassword2').focus();
		error = "yes";		
	}
	if(Verification2 == "" && error == "no") { //If no values
		alert('Please enter in the verification number');
		document.getElementById('regVerify2').focus();
		error = "yes";
	}
	if(Verification != Verification2 && error == "no") { //If emails dont match
		alert('The verification number you entered is not correct.');
		error = "yes";		
	}
	if(document.getElementById('staff').checked==false && document.getElementById('alumni').checked==false && document.getElementById('friend').checked==false && document.getElementById('student').checked==false && error == "no") {
		alert('Are you staff, an alumnus, a student or a friend to the ministry?');
		document.getElementById('staff').focus();
		error = "yes";
	}
	
	if(error == "no") { //If no errors, lets register them

		if(document.getElementById('staff').checked==true) {
			Type="staff";
		}
		if(document.getElementById('alumni').checked==true) {
			Type="alumni";
		}
		if(document.getElementById('student').checked==true) {
			Type="student";
		}
		if(document.getElementById('friend').checked==true) {
			Type="friend";
		}

		showProcessing();
		
		var Parameters = "FirstName="+FirstName+"&LastName="+LastName+"&Phone="+Phone+"&Email="+Email+"&Password="+Password+"&Type="+Type;
	
		var url = "guestBookRegister.php?nocache="+nocache;
				
		httpProcessRegister.open("POST", url, true);
		httpProcessRegister.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpProcessRegister.setRequestHeader("Content-length", Parameters .length);
		httpProcessRegister.setRequestHeader("Connection", "close");
				
				
		httpProcessRegister.onreadystatechange = function() {//Handler function for call back on state change.
			if(httpProcessRegister.readyState == 4) {
							
				var response = httpProcessRegister.responseText;
					
				if(response == "ok") { //If payment was okay
								
					window.location.href='guestBookSign.php';
					
								
				} else if(response == "error") {
					
					alert('An error occured during registration. Please try again.');
					hideProcessing();				
							
				} else if(response == "already") {

					alert('E-Mail address already in use.');
					hideProcessing();		
				}
							
							
			}	
		}
			
		httpProcessRegister.send(Parameters);
	}
	
}

var httpProcessGuestBook = createObject();

function signGuestBook() {
	
	nocache = Math.random();
	var Comment=encodeURIComponent(document.getElementById('comment').value);
	var City=encodeURIComponent(document.getElementById('city').value);
	var Country=encodeURIComponent(document.getElementById('country').value);
	
	showProcessing();
		
		var Parameters = "Comment="+Comment+"&City="+City+"&Country="+Country;
	
		var url = "guestBookSignProcess.php?nocache="+nocache;
				
		httpProcessGuestBook.open("POST", url, true);
		httpProcessGuestBook.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpProcessGuestBook.setRequestHeader("Content-length", Parameters .length);
		httpProcessGuestBook.setRequestHeader("Connection", "close");
				
		httpProcessGuestBook.onreadystatechange = function() {//Handler function for call back on state change.
			if(httpProcessGuestBook.readyState == 4) {
							
				var response = httpProcessGuestBook.responseText;
					
				if(response == "ok") { //If payment was okay
								
					window.location.href='guestBook.php';
					
								
				} else if(response == "error") {
					
					alert('An error occured during guestbook signing. Please try again.');
					hideProcessing();				
							
				} else if(response == "login") {
					
					alert('You need to be logged in to do that!');
					hideProcessing();		
					
				}
							
							
			}	
		}
			
		httpProcessGuestBook.send(Parameters);
	
	
}
