var user_nicename;
var user_city;
var user_zip_code;
var user_description;	
var user_username;
var user_username_new;
var user_password_new;	
var user_password_new_2;	
var secure_password_new="";	
var username_field_loaded ="no";
var password_fields_loaded ="no"; 

function my_settings_values() {
// get values
user_username = document.getElementById('user_username').value;
user_password_new = document.getElementById("user_password_new").value;	
user_password_new_2 = document.getElementById("user_password_new_2").value;	

document.getElementById("comment_part_box_170").className = "comment_part_box"; 
document.getElementById("comment_alert_170").innerHTML = "&nbsp;";
document.getElementById("comment_part_box_180").className = "comment_part_box"; 
document.getElementById("comment_alert_180").innerHTML = "&nbsp;";
	
}

// CHECK FORM BOX BY BOX
function check_170() {	
	if ( user_password_new.length < 6 )
	{   document.getElementById("comment_part_box_170").className = "comment_part_box_highlighted"; 
		document.getElementById("comment_alert_170").innerHTML = "Måste vara minst sex tecken"; 
		check_form_ok ="no";} 
}

function check_180() {
check_170();	
	if ( user_password_new != user_password_new_2 )
	{   document.getElementById("comment_part_box_180").className = "comment_part_box_highlighted"; 
		document.getElementById("comment_alert_180").innerHTML = "Lösenorden stämmer inte överens"; 
		check_form_ok ="no";} 
}


// CHECK IF THE FORM IS OK
function check_form_my_settings ()
{
	// first reset	
	check_form_ok ="yes";
	//alert("check 180"); 
	my_settings_values();
	check_180();
	hash_user_password(); 

	
	// if everything looks ok, submit review
	if (check_form_ok=="no") { alert("Formuläret är inte korrekt ifyllt."); } 
	else { 
	submit_my_settings(); 
	}
		
}

		// SUBMIT THE REVIEW
	function submit_my_settings()
{	

document.getElementById("send_review_indicator").style.visibility = "visible";

poststr = "/ajax.php?action=update_user_password";
poststr = poststr + "&user_username=" + escape(encodeURI(our_review_email));
poststr = poststr + "&user_first_time_code=" + user_first_time_code;
poststr = poststr + "&secure_password=" + secure_password;
poststr = poststr + "&secure_password_new=" + secure_password_new;

GDownloadUrl(poststr, function(data, responseCode) {

//alert(poststr);alert(data);

// split return into an array
ajax_array = data.split('|');

	if (ajax_array[0]==7) 
				{ alert(ajax_array[1]); }
	if (ajax_array[0]==8) {  
				document.getElementById("comment_part_box_170").className = "comment_part_box_highlighted"; 
	  			document.getElementById("comment_alert_170").innerHTML = ajax_array[1];
				alert(ajax_array[1]);
				document.getElementById("user_password_new").focus();
				}	
	if (ajax_array[0]==9) 
				{ alert(ajax_array[1]); }
	if (ajax_array[0]==10) 
				{  alert("Dina inställningar har nu uppdaterats.");location.href = 'http://www.minkarta.se/' + user_nicename.replace(" ","_") + '/'; }
	if (ajax_array[0]==11) { 
				hash_user_password();
				var answer = confirm("Du kommer nu att loggas in med dina nya inställningar.\n\nVill du att vi ska komma ihåg dig nästa gång du kommer tillbaka?")
				if (answer){location.href = "http://www.allakartor.se/super_login.php?username=" + user_username + "&secure_password=" + secure_password_new + "&next=" + "http://www.minkarta.se/" + user_nicename.replace(" ","_") + "/" + "&remember_me=y";}
				else{ location.href = "http://www.allakartor.se/super_login.php?username=" + user_username + "&secure_password=" + secure_password_new + "&next=" + "http://www.minkarta.se/" + user_nicename.replace(" ","_") + "/";}
				}				

document.getElementById("send_review_indicator").style.visibility = "hidden";

});
}




		// SUBMIT FORGOTTEN PASSWORD
	function submit_my_forgotten_password()
{	

document.getElementById("send_review_indicator").style.visibility = "visible";

	user_username_new = document.getElementById('user_username_new').value;
	// forgot password
	if (document.getElementById("forgot_password")) {
		if ( document.getElementById("forgot_password").checked) { forgot_password= "y"; } else { forgot_password= ""; } 	
	}

poststr = "/ajax.php?action=request_forgotten_password";
poststr = poststr + "&user_username=" + escape(encodeURI(user_username_new));
poststr = poststr + "&secure_password=" + secure_password_new;
if (forgot_password=="y") { poststr += "&forgot_password=y"} 

GDownloadUrl(poststr, function(data, responseCode) {

// split return into an array
ajax_array = data.split('|');

//alert(data);

// clear fields
document.getElementById("comment_part_box_160").className = "comment_part_box"; 
document.getElementById("comment_part_box_170").className = "comment_part_box"; 
document.getElementById("comment_info_160").innerHTML = "";
document.getElementById("comment_info_170").innerHTML = "";
document.getElementById("comment_alert_160").innerHTML = "";
document.getElementById("comment_alert_170").innerHTML = "";

	if (ajax_array[0]==3) {  
				document.getElementById("comment_part_box_160").className = "comment_part_box_highlighted"; 
	  			document.getElementById("comment_alert_160").innerHTML = ajax_array[1];
				}
		if (ajax_array[0]==4) {  
				document.getElementById("comment_part_box_170").className = "comment_part_box_highlighted"; 
	  			document.getElementById("comment_info_170").innerHTML = ajax_array[1];
				document.getElementById("comment_alert_170").innerHTML = "<input id=\"forgot_password\" type=\"checkbox\" value=\"y\"/>Klicka i denna ruta om du glömt ditt lösenord.";
				}	
		if (ajax_array[0]==5) {  
				 alert("Vi har nu skickat ett email till din emailadress. I mailet finns en länk som du måste klicka på för att uppdatera ditt lösenord.");location.href =  ajax_array[1]; 
				}	
		if (ajax_array[0]==6) {  
		alert("Du kommer nu att loggas in.")
				location.href ="http://www.allakartor.se/super_login.php?username=" + user_username_new + "&secure_password=" + secure_password_new + "&remember_me=y&next=" + encodeURI(document.getElementById("next_page").value);  
				}			
				
document.getElementById("send_review_indicator").style.visibility = "hidden"; 

});
}


// FUNCTION TO HASH THE USER_PASSWORD
function hash_user_password() {
		// Get password
		user_password_new = document.getElementById('user_password_new').value;
		
		// Hash password
		secure_password_new = md5(user_password_new);
		//alert("New password: " + secure_password_new);
		// Save hashed password
			if (document.getElementById('next') != null )  {
			// Set location so we get back to the same page
			document.getElementById('next').value = location.href;
			}
		
		// Remove plain password
		user_password_new.value = '';
}

