function getSignInAndSignUpStatus(result, node_name) {
	if(result) {
		var root;
		for(var i = 0; i < result.childNodes.length; ++i) {
			root = result.childNodes[i];
			
			if(root.nodeType == 1) {
				if(root && root.childNodes) {
					for(var i = 0; i < root.childNodes.length; ++i) {
						var sub_node = root.childNodes[i];
				
						if(sub_node.nodeName == node_name) {
							return sub_node.firstChild ? sub_node.firstChild.data : "";
						}
					}
				}
			}
		}
	}
	return -1;
}

function showRegistrationStatus(result) {
	var status = getSignInAndSignUpStatus(result, "status");
	
	var redirect_url = "";
	if(partner_id == 3) {
		redirect_url = "/events/";
	}
	else {
		var url = document.location;
		if(url) {
			url = url.toString();
			var pos = url.indexOf("#");
			if(pos > 0) {
				url = url.substring(0, pos);
			}
			
			if(partner_id == 19) {
				pos = url.indexOf("?");
				url += (pos > 0 ? "&" : "?") + "callconversion=1";
			}
		}
		redirect_url = url;
	}
	
	if(status == 1) {
		document.location = redirect_url;
	}
	else if(status == -1) {//segmentation fault from the servers
		document.location = redirect_url;
	}
	else {
		var error = getSignInAndSignUpStatus(result, "error");
		
		var message = false;
		switch(error) {
			case "UNDER_AGE": 
				message = "You must be "+(ISUK ? 18 : 21)+" or older to join the Patron Social Club."; 
				underAgeRedirect(true);
				break;
			case "INVALID_AGE_DATE": 
				message = "You must select a valid birth date."; 
				break;
			case "ALREADY_EXISTS": 
				message = "Email or Username already exists! Please try another email."; 
				break;
			case "EMAIL_AND_SCREEN_NAME_ARE_EMPTY": 
				message = "You must enter your Email and Username."; 
				break;
			case "WRONG_EMAIL_FORMAT": 
				message = "You must enter a correct email format."; 
				break;
			case "GENDER_EMPTY": 
				message = "You must select your gender."; 
				break;
			case "ZIP_INVALID": 
				message = "If you are in the United States, you must enter a valid 5-digit ZIP code."; 
				break;
			default: 
				message = "Looks like your registration was unsuccessful!"; 
		}
		
		document.getElementById('sign-message').innerHTML = message;
		
		if(error == -1) {//segmentation fault from the servers
			window.setTimeout("document.location='"+redirect_url+"';", 1500);
		}
		
		var registration_submit_button = document.getElementById('registration_submit_button');
		if(registration_submit_button) {
			registration_submit_button.disabled = false;
		}
	}
}

function showLoginStatus(result) {
	var status = getSignInAndSignUpStatus(result, "status");
	
	var redirect_url = "";
	if(partner_id == 3) {
		redirect_url = "/events/";
	}
	else {
		var url = document.location;
		if(url) {
			url = url.toString();
			var pos = url.indexOf("#");
			if(pos > 0) {
				url = url.substring(0, pos);
			}
		}
		redirect_url = url;
	}
	
	if(status == 1) {
		document.location = redirect_url;
	}
	else if(status == -1) {//segmentation fault from the servers
		document.location = redirect_url;
	}
	else {
		document.getElementById('sign-message').innerHTML = "Incorrect email or password, please check that you entered them correctly.";
		
		var login_submit_button = document.getElementById('login_submit_button');
		if(login_submit_button) {
			login_submit_button.disabled = false;
		}
	}
}

function checkDOBAndShowRegistration() {
	var dob_year = document.getElementById('dobYear');
	var dob_month = document.getElementById('dobMonth');
	var dob_day = document.getElementById('dobDay');
	
	var dob = dob_year.options[dob_year.selectedIndex].value + "" + dob_month.options[dob_month.selectedIndex].value + "" + dob_day.options[dob_day.selectedIndex].value;
	
	var current_date = new Date();
	var current_year = current_date.getFullYear();
	var current_month = current_date.getMonth() + 1;
	var current_day = current_date.getDate();
	
	if(current_month < 10) current_month = "0" + current_month;
	if(current_day < 10) current_day = "0" + current_day;
	
	var current_time = current_year + "" + current_month + "" + current_day;
	
	var minimum_age = (ISUK ? 18 : 21) * 10000;
	var is_dob_valid = parseInt(current_time) - parseInt(dob) >= minimum_age ? true : false;
	
	if(is_dob_valid) {
		document.getElementById('signin').style.display = 'none';
		document.getElementById('signup').style.display = 'block';
		
		document.getElementById('registration_dob_year').selectedIndex = dob_year.selectedIndex;
		document.getElementById('registration_dob_month').selectedIndex = dob_month.selectedIndex;
		document.getElementById('registration_dob_day').selectedIndex = dob_day.selectedIndex;
	}
	else {
		document.getElementById('sign-message').innerHTML = "You must be "+(ISUK ? 18 : 21)+" or older to join the Patron Social Club.";
		underAgeRedirect(true);
	}
}
