// JavaScript Document

/* ******

Verification

****** */

function process_membership(){
	
	mForm = document.getElementById('signup');
	
	var strContent = '';
	var goSubmit = true;
	
	for(var i=0;i<mForm.elements.length;i++){
		mEl = mForm.elements[i];
		//alert(mEl.getAttribute('name'));
		if(mEl.getAttribute('name')!=null&&mEl.getAttribute('name')!=''){
			if(mEl.getAttribute('required')=='true'&&mEl.value==''){
				mEl.style.backgroundColor = "#EAEAEA";
				goSubmit = false;
			}else{
				if(strContent==''){
					strContent = mEl.getAttribute('name') + '^' + mEl.value;
				}else{
					strContent += '|' + mEl.getAttribute('name') + '^' + mEl.value;
				}
			}
		}
	}
	
	if(goSubmit==false){
		ajax_report('Please fill in the sections highlighted in grey before proceeding','none');
		return false;
	}
	
	e = document.getElementById('existing');
	p = document.getElementById('payment_join_fee');
	p.value = e.value;
	
	e = document.getElementById('email');
	p = document.getElementById('receipt_address');
	p.value = e.value;
	
	p = document.getElementById('reply_link_url');
	p.value = p.value + e.value + '&payment_number=';
		
	create_member(strContent);

}

function modify_membership(){
	
	mForm = document.getElementById('signup');
	
	var strContent = '';
	var goSubmit = true;
	
	for(var i=0;i<mForm.elements.length;i++){
		mEl = mForm.elements[i];
		//alert(mEl.getAttribute('name'));
		if(mEl.getAttribute('name')!=null&&mEl.getAttribute('name')!=''){
			if(mEl.getAttribute('required')=='true'&&mEl.value==''){
				mEl.style.backgroundColor = "#EAEAEA";
				goSubmit = false;
			}else{
				if((mEl.value!='')&&(mEl.value.indexOf('&')!=-1)){
					while(mEl.value.indexOf('&')!=-1){
						mEl.value = mEl.value.replace('&', 'and');
					}
				}
				if(strContent==''){
					strContent = mEl.getAttribute('name') + '^' + mEl.value;
				}else{
					strContent += '|' + mEl.getAttribute('name') + '^' + mEl.value;
				}
			}
		}
	}
	
	if(goSubmit==false){
		ajax_report('Please fill in the sections highlighted in grey before proceeding','none');
		return false;
	}
	
	modify_member(strContent);

}



/* ******

Member details submission

****** */

function ajax_animate(i,dir){
	if(dir=='down'){
		if(i==0){return false;}
		if(i==undefined){i=10}
		i = i-1
	}else{
		//if(i==0){}
		if(i==10){return false;}
		if(i==undefined){i=0}
		i = i+1
	}
	
	eStyle('ajax_report','height',(i*15)+'px','set')
	
	setTimeout("ajax_animate(" + i + ", '" + dir + "')",5);
							 
}

function ajax_report(message,decoration){
	var e;
	e = document.getElementById('ajax_report');
	
	if(eStyle('ajax_report','dislpay','','get')!='none'){
		//animate out
		e.innerHTML = '';
		ajax_animate(10,'down');
		//setTimeout("boolExpand('ajax_report',false)",150);
	}
	
	e.innerHTML = message;
	boolExpand('ajax_report',true);
	eStyle('ajax_report','width','200px','set');
	
	ajax_animate(0,'up');
	
	//eStyle('ajax_report','height','150px','set');
	eStyle('ajax_report','text-decoration',decoration,'set');	
}

function check_member(email,available){
	ajax_report('Confirming availability', 'none');
	
	if(available==true){ // we want it to be available to proceed
		ajaxSend('http://www.aavpa.org/functions/membership.asp?action=check&content=' + email, check_member_done);
	}
}

function check_member_done(result){
	//alert(result);
	if(result!='ok'){
	ajax_report('The email address you specified is already registered in our database, please select another or <a href="login.asp">click here to login</a>','none');
		eStyle('email','background-color','#EAEAEA','set');
		//alert(eStyle('email','background-color','#EAEAEA','get'));
	}else{
		ajax_report('That email address is available')
		setTimeout("ajax_animate(10,'down')",5000);
		setTimeout("boolExpand('ajax_report',false)",5150);
	}
}

function check_login(email,password){
	ajax_report('Logging in', 'none');
	ajaxSend('http://www.aavpa.org/functions/membership.asp?action=login&content=' + email + '|' + password, check_login_done);
}

function check_login_done(result){
	//alert(result);
	if(result=='ok'){
		f = document.getElementById('login');
		f.submit();
	}else if(result=='not registered'){
		ajax_report('The email address you specified is not registered in our database, please select another or <a href="new_member.asp">click here to register</a>','none');
		eStyle('email','background-color','#EAEAEA','set');
	}else if(result=='wrong password'){
		ajax_report('Incorrect password','none');
		eStyle('password','background-color','#EAEAEA','set');
	}else{
		ajax_report('There was an error logging you in, please check your details and try again','none');
	}
}


function create_member(strDetails){
	ajax_report('Communicating with AAvPA database...', 'blink');
	//window.location.href='http://www.aavpa.org/functions/membership.asp?action=create&content=' + strDetails;
	ajaxSend('http://www.aavpa.org/functions/membership.asp?action=create&content=' + strDetails, create_member_done);

}		
	
function create_member_done(result){
	if(result=='ok'){
		ajax_report('Thankyou, your membership has been submitted, you will be redirected to the Westpac website where you can complete the membership transaction','none');
		setTimeout("boolExpand('ajax_report',false);",4000);
		p = document.getElementById('payment')
		if(p){p.submit()}
	}else{
		//alert(result);
		ajax_report('There was a problem submitting your membership, please try again later \n ' + result,'none');
		setTimeout("boolExpand('ajax_report',false);",8000);
		setTimeout("window.location.href='http://www.aavpa.org';",8500);		
	}

}

function modify_member(strDetails){
	ajax_report('Communicating with AAvPA database...', 'blink');
	//window.location.href='http://www.aavpa.org/functions/membership.asp?action=create&content=' + strDetails;
	ajaxSend('http://www.aavpa.org/functions/membership.asp?action=update&content=' + strDetails, modify_member_done);

}		
	
function modify_member_done(result){
	if(result=='ok'){
		ajax_report('Thankyou, the changes have been made. This page will be refreshed in five seconds','none');
		setTimeout("boolExpand('ajax_report',false);",4000);
		setTimeout("window.location='members.asp';",5000);
	}else{
		//alert(result);
		ajax_report('There was a problem modifying your details, please try again later \n ' + result,'none');
		setTimeout("boolExpand('ajax_report',false);",8000);
	}

}

function delete_member(iID) {
	if (confirm('Are you sure you want to delete this user?')) {
		ajax_report('Communicating with AAvPA database...', 'blink');
		ajaxSend('http://www.aavpa.org/functions/membership.asp?action=delete&content=' + iID, delete_member_done);
	}
}

function delete_member_done(result){
	if(result=='ok'){
		ajax_report('Thankyou, the changes have been made. This page will be refreshed in five seconds','none');
		setTimeout("boolExpand('ajax_report',false);",4000);
		setTimeout("window.location='admin.asp';",5000);
	}else{
		//alert(result);
		ajax_report('There was a problem modifying your details, please try again later \n ' + result,'none');
		setTimeout("boolExpand('ajax_report',false);",8000);
	}
}

function activate_member(sEmail) {
	alert(sEmail);
	if (confirm('Are you sure you want to activate this user?')) {
		ajax_report('Communicating with AAvPA database...', 'blink');
		ajaxSend('http://www.aavpa.org/functions/membership.asp?action=activate&content=' + sEmail, delete_member_done);
	}
}