function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
		if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
		for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
		if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function validateLogin() {
	var email = document.getElementById('email');
	var password = document.getElementById('password');

	if (email.value == "") {
		alert("You must enter your email address.");
		email.focus();
		return false;
	}
	if (password.value == "") {
		alert("You must enter a password.");
		document.getElementById('password').focus();
		return false;
	}

	encodeMemberPassword(password.value);
	
	return true;
}

function validateEmail(email) {
	// E-mail Validation by Henrik Petersen / NetKontoret
	// Explained at www.echoecho.com/jsforms.htm
	// Please do not remove this line and the two lines above.
	apos=email.indexOf("@");
	dotpos=email.lastIndexOf(".");
	lastpos=email.length-1;
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
		return false;
	} else {
		return true;
	}
}

function encodeMemberPassword(password) {
	var hash = hex_md5(password);
	document.getElementById('password').value = hash;
}

function cancelAdmin(section) {
	document.forms[0].action = baseUrl+"/"+section;
	document.forms[0].submit();
}

function replaceChars(entry) {
	out = "\\"; // replace this
	add = "/";  // with this
	temp = "" + entry; // temporary holder

	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add +
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function isNumeric(strString) {
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function isPhone(strString) {
	var strValidChars = "0123456789()+- ";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function confirmDelete(path) {
	if (confirm("Are you sure want to delete this entry from the database?")) {
		window.location = baseUrl+path;
	}
}

function confirmContinue(path) {
	if (confirm("Are you sure want to perform this action?")) {
		window.location = path;
	}
}

function hideBlock(divId) {
	document.getElementById(divId).style.display = "none";
}

function showBlock(divId) {
	document.getElementById(divId).style.display = "block";
}

function inlineBlock(divId) {
	document.getElementById(divId).style.display = "inline";
}

function valButton(btn) {
	var cnt = -1;
	for (var i=btn.length-1; i > -1; i--) {
		if (btn[i].checked) {cnt = i; i = -1;}
	}
	if (cnt > -1) {
		return btn[cnt].value;
	}  else {
		return null;
	}
}

function validateContactForm() {
	var fullName = document.getElementById('full_name');
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');

	if (fullName.value == "") {
		alert("You must enter your full name.");
		fullName.focus();
		return false;
	}
	if (email.value == "" && phone.value == "") {
		alert("You must enter either a phone number or email address.");
		email.focus();
		return false;
	}
	if (email.value != "") {
		if (!validateEmail(email.value)) {
			alert("You must enter a valid email address");
			email.focus();
			return false;
		}
	}
	if (phone.value != "" && !isPhone(phone.value)) {
		alert("The phone number contains invalid characters.");
		email.focus();
		return false;
	}
	return true;
}


function toggleResults(divId) {
	toggleDiv = document.getElementById(divId);
	new Effect.toggle(toggleDiv, 'slide');
}


function cancel(path){
	if (confirm("Are you sure want to cancel?")) {
		window.location = baseUrl+'/'+path;
	}	
}

function validateRegisterStep1(){
	var email = document.getElementById('email');
	var password = document.getElementById('password');
	var confirmPassword = document.getElementById('confirm_password');
	var firstName = document.getElementById('first_name');
	var surname = document.getElementById('surname');
	var address1 = document.getElementById('address1');
	var townName = document.getElementById('town_name');
	var countryField = document.getElementById('country_id');
    var countrySelection = countryField.selectedIndex;	
	var countryId = countryField.options[countrySelection].value;
	var regionField = document.getElementById('region_id');
    var regionSelection = regionField.selectedIndex;	
	var regionId = regionField.options[regionSelection].value;
		
	if (email.value == "") {
		alert("You must enter an email address.");
		email.focus();
		return false;
	}
	if (email.value != "") {
		if (!validateEmail(email.value)) {
    		alert("You must enter a valid email address");
    	    email.focus();
    		return false;
    	}
	}
	if (password.value == "") {
		alert("You must enter a password.");
		password.focus();
		return false;
	}
	if (confirmPassword.value == "") {
		alert("You must verify your password.");
		confirmPassword.focus();
		return false;
	}	
	if (password.value != confirmPassword.value) {
		alert("Your passwords do not match.");
		password.focus();
		return false;
	}
	if (firstName.value == ""){
		alert("You must enter your first name.");
		firstName.focus();
		return false;		
	}
	if (surname.value == ""){
		alert("You must enter your surname.");
		surname.focus();
		return false;		
	}
	if (countryId == -1){
		alert("You must select a country.");
		countryField.focus();
		return false;		
	}
	if (regionId == -1){
		alert("You must select a region/state.");
		countryField.focus();
		return false;		
	}
	if (address1.value == ""){
		alert("You must enter the first line of your address.");
		address1.focus();
		return false;		
	}	
	if (townName.value == ""){
		alert("You must enter a town/city.");
		townName.focus();
		return false;		
	}	
	
	return true;
}

function searchOrganisations(){
	var searchString = document.getElementById('search_string');

	if (searchString.value == "") {
		alert("You must enter a search string.");
		searchString.focus();
		return false;
	}

	document.register_form.submit();
}

function addOrganisation(){
	var organisationName = document.getElementById('new_organisation');

	if (organisationName.value == "") {
		alert("You must enter an organisation name.");
		organisationName.focus();
		return false;
	}

	document.register_form.action = baseUrl+"/member/register/2";
	document.getElementById('search').value = "false";
	document.getElementById('save').value = "true";
	document.register_form.submit();
}

function joinOrganisation(){
	var orgId = getRadioCheckedValue(document.register_form.org_id);
	
	if (orgId == "" || orgId == -1) {
		alert("You must select an existing organisation.");
		return false;
	}
	document.register_form.action = baseUrl+"/member/register/2";
	document.getElementById('search').value = "false";
	document.getElementById('save').value = "true";
	document.register_form.submit();
}

function skipOrganisation(){
	document.register_form.action = baseUrl+"/member/register/2";
	document.getElementById('search').value = "false";
	document.getElementById('save').value = "true";
	document.register_form.submit();
}

function validateRegisterStep2(){
	var searchString = document.getElementById('search_string');

	if (monthlyDonation == "") {
		alert("You must enter a monthly donation amount.");
		document.register_form.monthly_donation.focus();
		return false;
	}

	return true;
}

function validateRegisterStep3(){
	var monthlyDonation = document.register_form.monthly_donation.value;
    var currencySelection = document.register_form.default_currency_id.selectedIndex;	
	var currencyId = document.register_form.default_currency_id.options[currencySelection].value;

	if (currencyId == -1){
		alert("Your must select a currency.");
		return false;		
	}	
	if (monthlyDonation == "") {
		alert("You must enter a monthly donation amount.");
		document.register_form.monthly_donation.focus();
		return false;
	}
	if (parseFloat(monthlyDonation) < 1.00) {
		alert("The minimum monthly donation is 1.00 in your currency.");
		document.register_form.monthly_donation.focus();
		return false;
	}
	if (monthlyDonation != "") {
		if (!isNumeric(monthlyDonation)) {
    		alert("Your monthly donation amount must be numeric");
    	    document.register_form.monthly_donation.focus();
    		return false;
    	}
	}

	return true;
}

function ensureTermsAccepted() {
	termsBox = document.getElementById('terms');
	if (!termsBox.checked) {
		alert("You must accept our terms and conditions to proceed.");
		return false;
	}
	disableButton(document.register_form.cancel_button);
	disableButton(document.register_form.submit_button);
	return true;
}

function disableButton(button) {
	button.value = "Please wait...";
	button.disabled = true;
}

function validateForgotPasswordForm(){
	var email = document.getElementById('email').value;
	
	if (email == "") {
		alert("You must enter an email address.");
		document.getElementById('email').focus();
		return false;
	}	
}

function validateChangePasswordForm() {
	var password = document.change_password_form.password.value;
	var verifyPassword = document.change_password_form.verify_password.value;
	
	if (password == "") {
		alert("You must enter a password.");
		document.change_password_form.password.focus();
		return false;
	}
	if (verifyPassword == "") {
		alert("You must verify your password.");
		document.change_password_form.verify_password.focus();
		return false;
	}
	if (password != verifyPassword) {
		alert("Password and verification must match.");
		document.change_password_form.password.focus();
		return false;
	}

	return true;
}

function encodeChangePassword(password, verifyPassword) {
	var hash1 = hex_md5(password);
	var hash2 = hex_md5(verifyPassword);
	document.change_password_form.password.value = hash1;
	document.change_password_form.verify_password.value = hash2;
	return true;
}

function validateDedicationForm(){
	var dedication = document.getElementById('dedication_text');
	
	if (dedication.value == "") {
		alert("You must enter a dedication message.");
		dedication.focus();
		return false;
	}
		
	return true;
}

function validateDonationForm(){
	var donation = document.getElementById('donation_amount');
	
	if (donation.value == "") {
		alert("You must enter a donation amount.");
		donation.focus();
		return false;
	}
	if (parseFloat(donation.value) < 1.00) {
		alert("The minimum donation is 1.00 in your currency.");
		donation.focus();
		return false;
	}
	if (donation.value != "") {
		if (!isNumeric(donation.value)) {
    		alert("Your donation amount must be numeric");
    	    donation.focus();
    		return false;
    	}
	}
		
	return true;
}

function addTmpMemberReferal() {
    referralEmail = document.getElementById('friend_email').value;

    if (referralEmail == ""){
 		alert("You have not entered an email address.");
		document.getElementById('friend_email').focus();  	
    }
    if (referralEmail != ""){
		if (!validateEmail(referralEmail)) {
			alert("You must enter a valid email address");
			document.getElementById('friend_email').focus(); 
		}
		else {    
		    new Ajax.Updater('referral_emails_div', baseUrl+'/member/ajaxAddTmpReferral', { 
		        method:'post', parameters:Form.serialize(document.donation_form), 
		        onComplete: function() {
		            document.getElementById('friend_email').value = "";
		        }
		    });
    	}
    }
   
}

function validateUpdateMemberDetailsForm(){
	var password = document.update_member_details_form.password.value;
	var verifyPassword = document.update_member_details_form.verify_password.value;
	if (password != ""){
		encodeUpdateMemberDetailsPassword(password, verifyPassword);
	}
	else {
		return true;
	}
}

function encodeUpdateMemberDetailsPassword(password, verifyPassword) {
	var hash1 = hex_md5(password);
	var hash2 = hex_md5(verifyPassword);
	document.update_member_details_form.password.value = hash1;
	document.update_member_details_form.verify_password.value = hash2;
	return true;
}

function deleteReferralEmail(referralEmail){
	new Ajax.Updater('referral_emails_div', baseUrl+'/member/ajaxDeleteTmpReferral', { 
		method:'post', postBody:'friend_email='+referralEmail});
}

function validateRequestForm() {
	var fullName = document.getElementById('full_name');
	var email = document.getElementById('email');

	if (fullName.value == "") {
		alert("You must enter your full name.");
		fullName.focus();
		return false;
	}
	if (email.value == "") {
		alert("You must enter your email address.");
		email.focus();
		return false;
	}
	if (email.value != "") {
		if (!validateEmail(email.value)) {
			alert("You must enter a valid email address");
			email.focus();
			return false;
		}
	}
	return true;
}

function resetDedicationSearch() {
	document.getElementById('search_string').value = "";
	document.getElementById('search').value = "false";
	document.search_form.submit();
}

function setRegions() {
	var countryField = document.getElementById('country_id');
    var countrySelection = countryField.selectedIndex;	
	var countryId = countryField.options[countrySelection].value;
	
	new Ajax.Updater('regions_div', baseUrl+'/member/ajaxGetRegions', { 
		method:'post', postBody:'country_id='+countryId});
}

function getRadioCheckedValue(radioObj) {
	if(!radioObj) {
		return "";
	}
	
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		if(radioObj.checked) {
			return radioObj.value;
		} else {
			return "";
		}
	}
		
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function validateReferralForm() {
	var emailList = document.getElementById('email_addresses');
	if (emailList.value == "") {
		alert("You must provide some email addresses");
		return false;
	}
	disableButton(document.referral_form.submit_button);
	return true;
}

function resendReferralEmail(divId, code){
	new Ajax.Updater('resend_referral_div_'+divId, baseUrl+'/member/ajaxResendReferralEmail', { 
		method:'post', postBody:'signup_code='+code});
}

function showTerms() {
	newWindow = window.open(baseUrl+'/home/showTerms', 'newWindow', 'toolbar=no, location=no, scrollbars=yes, resizable=yes, width=800, height=570, left=150, top=150');
}

function validateCCForm() {
	var firstName = document.getElementById('first_name');
	var surname = document.getElementById('surname');
	var address1 = document.getElementById('address1');
	var city = document.getElementById('city');
	var countryField = document.getElementById('country_id');
    var countrySelection = countryField.selectedIndex;	
	var countryId = countryField.options[countrySelection].value;
	var ccNumber = document.getElementById('cc_number');
	var cvn = document.getElementById('cvv');
	
	//var ccTypeField = document.getElementById("cc_type");
	//var ccTypeSelection = ccTypeField.selectedIndex;
	//var ccType = ccTypeField.options[ccTypeSelection].value;

	if (firstName.value == "") {
		alert("You must enter your first name.");
		firstName.focus();
		return false;
	}
	if (surname.value == "") {
		alert("You must enter your surname.");
		surname.focus();
		return false;
	}
	if (ccNumber.value == "") {
		alert("You must enter your credit card number.");
		ccNumber.focus();
		return false;
	}
	/*if (ccNumber.value != "") {
		// American Express cards have only 15 digits
		if (ccType == "Amex" && (ccNumber.value).length != 15) {
			alert("Your card number has an incorrect number of digits.");
			ccNumber.focus();
			return false;
		}
	
		if (ccNumber.value.length < 16) {
			alert("Your credit card number is not the correct length.");
			ccNumber.focus();
			return false;
		}
		if (!isNumeric(ccNumber.value)) {
    		alert("Your credit card number contains invalid characters.");
    	    ccNumber.focus();
    		return false;
    	}
	}*/
	if (cvn.value == "") {
		alert("You must enter your credit card verification number.");
		cvn.focus();
		return false;
	}
	if (cvn.value != "") {
		if (!isNumeric(cvn.value)) {
    		alert("Your card verification number contains invalid characters.");
    	    cvn.focus();
    		return false;
    	}
	}
	if (address1.value == "") {
		alert("You must enter your address.");
		address1.focus();
		return false;
	}
	if (city.value == "") {
		alert("You must enter your town/city.");
		city.focus();
		return false;
	}
	if (countryId == -1){
		alert("Your must select your country.");
		countryField.focus();
		return false;		
	}
}

function validateNominateOrganisation(){
	return true;
}

function addOrganisationMemberAcc(){
	var organisationName = document.getElementById('new_organisation');

	if (organisationName.value == "") {
		alert("You must enter an organisation name.");
		organisationName.focus();
		return false;
	}

	document.nominate_organisation_form.action = baseUrl+"/member/nominateOrganisation";
	document.getElementById('search').value = "false";
	document.getElementById('save').value = "true";
	document.nominate_organisation_form.submit();
}

function joinOrganisationMemberAcc(){
	var orgId = getRadioCheckedValue(document.nominate_organisation_form.org_id);
	
	if (orgId == "" || orgId == -1) {
		alert("You must select an existing organisation.");
		return false;
	}
	document.nominate_organisation_form.action = baseUrl+"/member/nominateOrganisation";
	document.getElementById('search').value = "false";
	document.getElementById('save').value = "true";
	document.nominate_organisation_form.submit();
}

function validateUpdateDonation(){
	var monthlyDonation = document.edit_form.monthly_donation.value;

	if (monthlyDonation == "") {
		alert("You must enter a monthly donation amount.");
		document.edit_form.monthly_donation.focus();
		return false;
	}
	if (parseFloat(monthlyDonation) < 1.00) {
		alert("The minimum monthly donation is 1.00 in your currency.");
		document.edit_form.monthly_donation.focus();
		return false;
	}
	if (monthlyDonation != "") {
		if (!isNumeric(monthlyDonation)) {
    		alert("Your monthly donation amount must be numeric");
    	    document.edit_form.monthly_donation.focus();
    		return false;
    	}
	}

	return true;
}