function change(elmnt, newClass) {
	document.getElementById(elmnt).className=newClass;
}
		
function changeNav(id, newClass) {
	if (id != null)
		id.className=newClass;
}

function getRadioValue(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 getSelectValue(selectObj) {
	if(!selectObj)
		return "";
	var index = selectObj.selectedIndex;
	
	return selectObj[index].value
}

function brezza_check_user_form(f) {
	required = f.required_fields.value;
	if ( required != "" ) {
		req_fields = required.split(",");
		for (i = 0; i < f.elements.length; i++) {
			for (z = 0; z < req_fields.length; z++) {
				if ( req_fields[z] == f.elements[i].name && f.elements[i].value == "") {
					alert(req_fields[z] +' is a required field.');
					return false;
				}
			}
		}
	}
	
	if ( f.acc_password.value != f.acc_password2.value ) {
		alert('Your password and password confirmation do not match.');
		return false;
	}
}

function brezza_user_validate(f) {
	acc_email = f.acc_email.value;
	acc_password = f.acc_password.value;
	
	var xmlHttp;
	
	if ( acc_email != "" && acc_password != "" ) {
		try {
			// Firefox, Opera 8.0+, Safari 
			xmlHttp=new XMLHttpRequest();
		}
		catch (e) {
			// Internet Explorer    
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			
			catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
				}
				catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
	
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				qResponse = xmlHttp.responseText;

				var pos_index = qResponse.indexOf('BREZZA_VALID_LOGIN');
				if ( pos_index != -1 ) {
					f.submit();
				}else {
					alert('Invalid account details');
					f.acc_password.value = '';
					f.acc_password.focus();
				}
				return false;
			}
		}
		xmlHttp.open("GET","includes/inc.ajax_user_tools.php?action=login&acc_email="+ acc_email +"&acc_password="+ acc_password +"&ran=" + Math.random(),true);
		xmlHttp.send(null);
	}
}

function brezza_user_lost_password(f) {
	acc_email = f.acc_email.value;
	
	var xmlHttp;
	
	if ( acc_email != "" ) {
		try {
			// Firefox, Opera 8.0+, Safari 
			xmlHttp=new XMLHttpRequest();
		}
		catch (e) {
			// Internet Explorer    
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			
			catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
				}
				catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
	
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				qResponse = xmlHttp.responseText;
				var pos_index = qResponse.indexOf('BREZZA_VALID_ACCOUNT');
				if ( pos_index != -1 ) {
					//f.submit();
					alert('A temporary password has been created and emailed to '+ acc_email);
				}else {
					alert('There is no account registered with the email address you have specified.');
					f.acc_email.value = '';
					f.acc_email.focus();
				}
				return false;
			}
		}
		xmlHttp.open("GET","includes/inc.ajax_user_tools.php?action=lost_password&acc_email="+ acc_email +"&ran=" + Math.random(),true);
		xmlHttp.send(null);
	}
}

function brezza_user_registration(f) {
	var xmlHttp;
	
	var errstr = "";
	
	if ( f.acc_email.value == "" ) {
		errstr = "Please enter your email address.";
		f.acc_email.focus();
	}
	
	if ( errstr == "" && f.acc_password.value.length < 8 ) {
		errstr = "Your password must be a minimum of 8 characters.";
		f.acc_password.focus();
	}

	if ( errstr == "" && f.acc_password.value != f.acc_password2.value ) {
		errstr = "Your password and password confirmation do not match.";
		f.acc_password2.focus();
	}
	
	if ( errstr == "" && f.info_first_name.value == "" ) {
		errstr = "Please enter your first name.";
		f.info_first_name.focus();
	}
	
	if ( errstr == "" && f.info_last_name.value == "" ) {
		errstr = "Please enter your last name.";
		f.info_last_name.focus();
	}
	
	if ( errstr != "" ) {
		alert(errstr);
	}else {
	
		url_str = "";
		for ( i = 0; i < f.elements.length; i++ ) {
			if ( f.elements[i].type == "select-one" ) {
				url_str += "&"+f.elements[i].name +"="+ getSelectValue(f.elements[i]);
			}else if ( f.elements[i].type == "radio" ) {
				if ( f.elements[i].checked == true ) {
					url_str += "&"+f.elements[i].name +"="+ getRadioValue(f.elements[i]);
				}
			}else {
				url_str += "&"+f.elements[i].name +"="+ f.elements[i].value;
			}
		}
	
		try {
			// Firefox, Opera 8.0+, Safari 
			xmlHttp=new XMLHttpRequest();
		}
		catch (e) {
			// Internet Explorer    
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			
			catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
				}
				catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
	
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				qResponse = xmlHttp.responseText;
				var pos_index = qResponse.indexOf('BREZZA_ACCOUNT_CREATED');
				if ( pos_index != -1 ) {
					alert("Your new account has been created. Your account details have been sent to you via email.");
					brezza_user_validate_new(f.acc_email.value, f.acc_password.value);
				}else {
					alert(qResponse);
				}
				return false;
			}
		}
		xmlHttp.open("GET","includes/inc.ajax_user_tools.php?action=register"+ url_str +" &ran=" + Math.random(),true);
		xmlHttp.send(null);
	}
}

function brezza_user_validate_new(acc_email, acc_password) {

	var xmlHttp;
	
	if ( acc_email != "" && acc_password != "" ) {
		try {
			// Firefox, Opera 8.0+, Safari 
			xmlHttp=new XMLHttpRequest();
		}
		catch (e) {
			// Internet Explorer    
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			
			catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
				}
				catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
	
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				qResponse = xmlHttp.responseText;
				
				var pos_index = qResponse.indexOf('BREZZA_VALID_LOGIN');
				if ( pos_index != -1 ) {
					document.location = "user_panel.php";
				}
			}
		}
		xmlHttp.open("GET","includes/inc.ajax_user_tools.php?action=login&acc_email="+ acc_email +"&acc_password="+ acc_password +"&ran=" + Math.random(),true);
		xmlHttp.send(null);
	}
}

function brezza_user_update(f) {
	var xmlHttp;
	
	var errstr = "";
	
	if ( f.acc_email.value == "" ) {
		errstr = "Please enter your email address.";
		f.acc_email.focus();
	}
	
	if ( errstr == "" && f.acc_password.value != "" && f.acc_password.value.length < 8 ) {
		errstr = "Your password must be a minimum of 8 characters.";
		f.acc_password.focus();
	}

	if ( errstr == "" && f.acc_password.value != "" && f.acc_password.value != f.acc_password2.value ) {
		errstr = "Your password and password confirmation do not match.";
		f.acc_password2.focus();
	}
	
	if ( errstr == "" && f.info_first_name.value == "" ) {
		errstr = "Please enter your first name.";
		f.info_first_name.focus();
	}
	
	if ( errstr == "" && f.info_last_name.value == "" ) {
		errstr = "Please enter your last name.";
		f.info_last_name.focus();
	}
	
	if ( errstr != "" ) {
		alert(errstr);
	}else {
	
		url_str = "";
		for ( i = 0; i < f.elements.length; i++ ) {
			if ( f.elements[i].type == "select-one" ) {
				url_str += "&"+f.elements[i].name +"="+ getSelectValue(f.elements[i]);
			}else if ( f.elements[i].type == "radio" ) {
				if ( f.elements[i].checked == true ) {
					url_str += "&"+f.elements[i].name +"="+ getRadioValue(f.elements[i]);
				}
			}else {
				url_str += "&"+f.elements[i].name +"="+ f.elements[i].value;
			}
		}
	
		try {
			// Firefox, Opera 8.0+, Safari 
			xmlHttp=new XMLHttpRequest();
		}
		catch (e) {
			// Internet Explorer    
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			
			catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
				}
				catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
	
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				qResponse = xmlHttp.responseText;

				var pos_index = qResponse.indexOf('BREZZA_ACCOUNT_UPDATED');
				if ( pos_index != -1 ) {
					alert("Your account details have been updated.");
					document.location = 'user_panel.php?cmd=account_interface';
				}else {
					alert(qResponse);
				}
				return false;
			}
		}
		xmlHttp.open("GET","includes/inc.ajax_user_tools.php?action=update"+ url_str +" &ran=" + Math.random(),true);
		xmlHttp.send(null);
	}
}

