/*
	registration JavaScript
	
	Created by cpruitt on 2006-06-19.
	Copyright (c) 2006 Cliff Pruitt. All rights reserved.
*/

//	Declare the AJAX controller URL
var ajax_controller = 'ajax_controller.asp'
var CurrentUser = null;


nmAppendOnload(initReg);

function initReg()
{
	if($('userRegInfoForm')){
		initUserRegForm();
	}
	
	if($('userRegInfoForm2')){
		initUserRegForm2();
	}
	
	if($('userSystemLoginForm')){
		initUserLoginForm();
	}
	
	return true;
}

function initUserRegForm2()
{
	if($('userRegFormSubmitWidget')){
		$('userRegFormSubmitWidget').onclick = userRegFormSaveChanges2;
	}
	
	$('userRegInfoForm2').onsubmit = function(){
		userRegFormSaveChanges2();
		return false;
	}
	
	new	Ajax.Request(
		ajax_controller,
		{
			onSuccess: function(response){
				var obj = new Function('return ' + response.responseText);
				CurrentUser = obj();
				
				var formElems = Form.getElements($('userRegInfoForm2'));
				
				formElems.each(
					function(item, index) {
						objValue = eval('CurrentUser.'+item.name.toLowerCase());
						if(objValue != undefined){
							item.value = objValue;
						}
					}
				);
				
				$('userRegInfoForm').bitagreetos.value = 'true';
				
				if(CurrentUser.bitagreetos == 'true'){
					$('userRegInfoForm').bitagreetos.checked = true;
				}
				
			},
			onFailure: function(response){
				alert('An Error Occurred.\n' + response.responseText);
			},
			parameters: "callmethod=openreginfoformdata"
		}
		);
	
	
	
	return true;
}

function initUserRegForm()
{
	new	Ajax.Request(
		ajax_controller,
		{
			onSuccess: function(response){
				var obj = new Function('return ' + response.responseText);
				CurrentUser = obj();
				
				var formElems = Form.getElements($('userRegInfoForm'));
				
				formElems.each(
					function(item, index) {
						objValue = eval('CurrentUser.'+item.name.toLowerCase());
						if(objValue != undefined){
							item.value = objValue;
							/*if(item.type == 'select-one'){
								opts = $A(item.options);
								opts.each(function(item, index){
									alert(item.value.toLowerCase() + ' = ' + objValue.toLowerCase());
									if(item.value.toLowerCase() == objValue.toLowerCase()){
										item.selected = true;
									}
								})
								
							} else {
								item.value = objValue;
							}*/
						}
					}
				);
				
				$('userRegInfoForm').bitagreetos.value = 'true';
				
				if(CurrentUser.bitagreetos == 'true'){
					$('userRegInfoForm').bitagreetos.checked = true;
				}
				
			},
			onFailure: function(response){
				alert('An Error Occurred.\n' + response.responseText);
			},
			parameters: "callmethod=openreginfoformdata"
		}
		);
	
	if($('userRegFormSubmitWidget')){
		$('userRegFormSubmitWidget').onclick = userRegFormSaveChanges;
	}
	
	return true;
}

function userRegFormSaveChanges2()
{
	var reg_form = $('userRegInfoForm2');
	Form.disable(reg_form);
	ajax_params = "callmethod=saveregistrationinfoandpurchase&" + Form.serialize(reg_form);
	new	Ajax.Request(
		ajax_controller,
		{
			onSuccess: function(response){
				allLabels = $A(document.getElementsByTagName('LABEL'));
				allLabels.each(function(item, index){
					item.style.color = 'black';
				});
				
				var obj = new Function('return ' + response.responseText);
				SaveResponse = obj();
				if(SaveResponse.errorCount > 0){
					
					arrErrorFields = eval('[' + SaveResponse.errorFields + ']');
					arrErrorFields.each(function(item, index){
						labelName = 'label_' + item;
						if($($(labelName))){
							$(labelName).style.color = 'red';
						}
					});
					
					alert('Oops! ' + SaveResponse.errorCount + ' errors were found.\nPlease correct indicated fields.' + SaveResponse.errorText);
				} else {
					alert(SaveResponse.feedbackText);
					initUserRegForm();
					
					//window.location.href = '/users/registration/home.asp';
					window.location.href = '/users/credits/purchaseHistory.asp';
				}
				Form.enable(reg_form);
			},
			onFailure: function(response){
				alert('An Error Occurred.\n' + response.responseText);
				Form.enable(reg_form);
			},
			parameters: ajax_params
		}
	);
	
	return true;
}

function userRegFormSaveChanges()
{
	var reg_form = $('userRegInfoForm');
	Form.disable(reg_form);
	new	Ajax.Request(
		ajax_controller,
		{
			onSuccess: function(response){
				allLabels = $A(document.getElementsByTagName('LABEL'));
				allLabels.each(function(item, index){
					item.style.color = 'black';
				});
				
				var obj = new Function('return ' + response.responseText);
				SaveResponse = obj();
				if(SaveResponse.errorCount > 0){
					
					arrErrorFields = eval('[' + SaveResponse.errorFields + ']');
					arrErrorFields.each(function(item, index){
						labelName = 'label_' + item;
						if($($(labelName))){
							$(labelName).style.color = 'red';
						}
					});
					
					alert('Oops! ' + SaveResponse.errorCount + ' errors were found.\nPlease correct indicated fields.' + SaveResponse.errorText);
				} else {
					alert(SaveResponse.feedbackText);
					initUserRegForm();
					window.location.href = '/users/registration/home.asp';
				}
				Form.enable(reg_form);
			},
			onFailure: function(response){
				alert('An Error Occurred.\n' + response.responseText);
				Form.enable(reg_form);
			},
			parameters: "callmethod=saveregistrationinfo&" + Form.serialize(reg_form)
		}
	);
	
	return true;
}

function initUserLoginForm()
{
	if($('passwordReminderDisplayWidget')){
		$('passwordReminderDisplayWidget').onclick = function(){
			if($('appPasswordReminderSubview')){
				$('appPasswordReminderSubview').style.display = 'block';
				$('userSystemLoginReminderForm').onsubmit = function(){
					processPasswordReminder();
					return false;
				}
				$('userLoginFormReminderSubmitWidget').onclick = processPasswordReminder;
			}
		}
	}
	if($('userLoginFormSubmitWidget')){
		document.forms['userSystemLoginForm'].onsubmit = function(){
			userLoginFormLogin();
			return false;
		}
		$('userLoginFormSubmitWidget').onclick = userLoginFormLogin;
	}
	
	return true;
}

function processPasswordReminder(){
	new	Ajax.Request(
		ajax_controller,
		{
			onSuccess: function(response){
				//alert(response.responseText);
				var objJSON = new Function('return ' + response.responseText);
				Rsp = objJSON();
				if(Rsp.errorCount > 0){
					if(!Rsp.errorText){
						alert('Sorry, we found ' + Rsp.errorCount + ' errors.');
					} else {
						alert(Rsp.errorText);
					}
				} else {
					alert(Rsp.responseText);
				}
			},
			onFailure: function(response){
				alert('An Error Occurred.\n' + response.responseText);
			},
			parameters: "callmethod=pwreminder&" + Form.serialize(('userSystemLoginReminderForm'))
		}
	);
}

function userLoginFormLogin()
{
	new	Ajax.Request(
		ajax_controller,
		{
			onSuccess: function(response){
				//alert(response.responseText);
				
				var objJSON = new Function('return ' + response.responseText);
				LoginStatus = objJSON();
				
				if(LoginStatus.errorCount > 0){
					alert('Sorry, we found ' + LoginStatus.errorCount + ' errors.');
				}
				
				//alert(LoginStatus.feedbackMessage);
				
				if(LoginStatus.loginSuccess == 1){
					window.location.href = 'home.asp';
				} else {
					alert('Sorry, we cold\'t find that username with that password.  Please Try again.');
				}
			},
			onFailure: function(response){
				alert('An Error Occurred.\n' + response.responseText);
			},
			parameters: "callmethod=loginAsUser&" + Form.serialize($('userSystemLoginForm'))
		}
	);
	return true
}




















