var user_nicename;
var user_city;
var user_zip_code;
var user_description;	
var user_fb_post_to_profile ="";
var message_settings;
var user_mail_reviews;
// var user_mail_followers;
var user_mail_weekly;
var user_username;
var user_email_new ="";
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"; 

var user_fb_uid 		="";
var user_fb_token 		="";
var user_fb_email 		="";
var user_fb_nicename 	="";


// IF WE ARE LOGGED IN, GET USER DETAILS
function ajax_get_user_details() {
	
	var poststr = "/ajax.php?action=get_user_details";

	poststr = poststr + "&our_review_email=" + escape(encodeURI(our_review_email)); 
	poststr = poststr + "&secure_password=" + secure_password;  

	GDownloadUrl(poststr, function(data, responseCode) {
	//alert(poststr); alert(data);
	
	     var user_details_xml = GXml.parse(data);
	     user_details = user_details_xml.documentElement.getElementsByTagName("user");
		 // --- FOR EACH REVIEW ---
	      for (j = 0; j < user_details.length; j++) {
			  
		  user_nicename = user_details[j].getAttribute("user_nicename");
		  document.getElementById("user_nicename").value = user_nicename; 
		  
		  user_image = user_details[j].getAttribute("user_image");
		  document.getElementById("user_image_show").src = 'http://www.allakartor.se/user_images_75/' + user_image + '.jpg';
		  
		  user_city = user_details[j].getAttribute("user_city");
		  document.getElementById("user_city").value = user_city;
		  
		  user_zip_code = user_details[j].getAttribute("user_zip_code");
		  document.getElementById("user_zip_code").value = user_zip_code;
		  
		  user_description = user_details[j].getAttribute("user_description"); 
		  document.getElementById("user_description").value = user_description;
		  
		  user_fb_post_to_profile = user_details[j].getAttribute("user_fb_post_to_profile");
		  user_mail_reviews = user_details[j].getAttribute("user_mail_reviews");
		 //  user_mail_followers = user_details[j].getAttribute("user_mail_followers");
		  user_mail_weekly = user_details[j].getAttribute("user_mail_weekly");
		  user_fb_uid = user_details[j].getAttribute("user_fb_uid");
		  
		  if(user_mail_reviews=="y"){user_mail_reviews=" checked=\"checked\" ";}
		  // if(user_mail_followers=="y"){user_mail_followers=" checked=\"checked\" ";}
		  if(user_mail_weekly=="y"){user_mail_weekly=" checked=\"checked\" ";}
		  if(user_fb_post_to_profile=="y"){user_fb_post_to_profile=" checked=\"checked\" ";} 
		  
		  
		  message_settings = "<div class=\"comment_part_box\">";
		  if( user_fb_uid!="0"){
		  message_settings += "<input name=\"user_fb_post_to_profile\" id=\"user_fb_post_to_profile\" type=\"checkbox\" value=\"y\" " + user_fb_post_to_profile + "> Visa mina recensioner på Facebook<br>";
		  }
		  message_settings += "<input name=\"user_mail_reviews\" id=\"user_mail_reviews\" type=\"checkbox\" value=\"y\" " + user_mail_reviews + "> Få mail när andra användare gör sånt som berör mig<br><input name=\"user_mail_weekly\" id=\"user_mail_weekly\" type=\"checkbox\" value=\"y\" " + user_mail_weekly + "> Få mail om nya funktioner och annan relevant info</div><br>";
		  // removed: <input name=\"user_mail_followers\" id=\"user_mail_followers\" type=\"checkbox\" value=\"y\" " + user_mail_followers + "> Få mail när nya personer börjar följa mig<br>
		  
		  document.getElementById("message_settings").innerHTML = message_settings;
		  
		  }   
		 
	
	});

}



function my_settings_values() {
// get values
user_nicename = document.getElementById("user_nicename").value;
user_city = document.getElementById("user_city").value;
user_zip_code = document.getElementById("user_zip_code").value;
user_description = document.getElementById("user_description").value;	
user_username_new = document.getElementById("user_username_new").value;	
if (document.getElementById("user_password_new")) {
user_password_new = document.getElementById("user_password_new").value;	
user_password_new_2 = document.getElementById("user_password_new_2").value;
}
if (document.getElementById("user_email_new")) {
user_email_new = document.getElementById("user_email_new").value;
}

if (document.getElementById("comment_part_box_110")) { 
document.getElementById("comment_part_box_110").className = "comment_part_box"; 
document.getElementById("comment_alert_110").innerHTML = "&nbsp;"; 
}

document.getElementById("comment_part_box_130").className = "comment_part_box"; 
document.getElementById("comment_alert_130").innerHTML = "&nbsp;";

document.getElementById("comment_part_box_140").className = "comment_part_box"; 
document.getElementById("comment_alert_140").innerHTML = "&nbsp;";

document.getElementById("comment_part_box_150").className = "comment_part_box"; 
document.getElementById("comment_alert_150").innerHTML = "&nbsp;";
 
	if (document.getElementById("comment_part_box_160")) {
	document.getElementById("comment_part_box_160").className = "comment_part_box"; 
	document.getElementById("comment_alert_160").innerHTML = "&nbsp;";
	}
	if (document.getElementById("comment_part_box_170")) {
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_110() {
my_settings_values();
		if (user_nicename.length < 6) 
		{ 	document.getElementById("comment_part_box_110").className = "comment_part_box_highlighted"; 
			document.getElementById("comment_alert_110").innerHTML = "Ditt användarnamn måste vara minst 6 tecken"; 
			check_form_ok ="no";}
		if (user_nicename.length > 25) 
		{ 	document.getElementById("comment_part_box_110").className = "comment_part_box_highlighted"; 
			document.getElementById("comment_alert_110").innerHTML = "Ditt användarnamn får max vara 25 tecken"; 
			check_form_ok ="no";}	
		var user_nicename_clean = user_nicename.replace("!", "");
		user_nicename_clean = user_nicename_clean.replace("/", "");
		user_nicename_clean = user_nicename_clean.replace("_", "");
		user_nicename_clean = user_nicename_clean.replace("*", "");
		user_nicename_clean = user_nicename_clean.replace("í", "");
		user_nicename_clean = user_nicename_clean.replace("\"", "");
		user_nicename_clean = user_nicename_clean.replace("+", "");
		user_nicename_clean = user_nicename_clean.replace("=", "");
		user_nicename_clean = user_nicename_clean.replace(",", "");
		user_nicename_clean = user_nicename_clean.replace(".", "");
		user_nicename_clean = user_nicename_clean.replace("@", "");
		if (user_nicename != user_nicename_clean) 
		{ 	document.getElementById("comment_part_box_110").className = "comment_part_box_highlighted"; 
			document.getElementById("comment_alert_110").innerHTML = "Tillåtna tecken är endast A-Ö, a-ö, 0-9, och blanksteg."; 
			check_form_ok ="no";}
}

function check_130() {
my_settings_values();
check_110();
	if (user_city.length < 3) 
	{ 	document.getElementById("comment_part_box_130").className = "comment_part_box_highlighted"; 
		document.getElementById("comment_alert_130").innerHTML = "Stadsnamnet måste anges";
		check_form_ok ="no";}
	if (user_city.length > 60) 
	{ 	document.getElementById("comment_part_box_130").className = "comment_part_box_highlighted"; 
		document.getElementById("comment_alert_130").innerHTML = "Stadsnamnet är för långt";
		check_form_ok ="no";}		
		
		
}

function check_140() {
check_130();	
	user_zip_code = user_zip_code.replace(" ","");
	if ( (user_zip_code < 99999) && (user_zip_code > 10000) ) {
	// set user_description field:
	if (user_description == "") { document.getElementById("user_description").value="Jag heter " + user_nicename + " och jag ... "; };	} 
	else 
	{ 	document.getElementById("comment_part_box_140").className = "comment_part_box_highlighted"; 
		document.getElementById("comment_alert_140").innerHTML = "Ditt postnummer måste bestå av fem siffror"; 
		check_form_ok ="no";}
}

function check_150() {
my_settings_values();	

	// max 300 characters
	if (user_description.length > 200 && user_description.length < 251)
	{ document.getElementById("comment_alert_150").innerHTML = "Max antal tecken kvar: " + (250-user_description.length);
	}
	if (user_description.length > 250)
	{ document.getElementById("comment_part_box_150").className = "comment_part_box_highlighted"; 
	  document.getElementById("comment_alert_150").innerHTML = "Texten är för lång"; 
	  check_form_ok ="no";}	
}

function check_160() {
check_140();			
	var user_username_clean = user_username_new.replace("å", "");
	user_username_clean = user_username_clean.replace("ä", "");
	user_username_clean = user_username_clean.replace("ö", "");
	user_username_clean = user_username_clean.replace(",", "");
	if (user_username_new != user_username_clean) 
	{ 	document.getElementById("comment_part_box_160").className = "comment_part_box_highlighted"; 
		document.getElementById("comment_alert_160").innerHTML = "Otillåtet tecken i email-adressen"; 
		check_form_ok ="no";}		
	if ( (user_username_new.split('@').length != 2) || (user_username_new.split('.').length < 2) )
	{   document.getElementById("comment_part_box_160").className = "comment_part_box_highlighted"; 
		document.getElementById("comment_alert_160").innerHTML = "Snabel-a eller punkt saknas"; 
		check_form_ok ="no";} 		
}

function check_170() {
check_160();	
	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";
	if (document.getElementById("user_password_new")) {
	//alert("check 180"); 
	check_180();
	hash_user_password(); }
	else { 
	//alert("check 160"); 
	check_160(); }
	
	// 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";

if(document.getElementById("user_mail_reviews")){
	if(document.getElementById("user_mail_reviews").checked)
	{ user_mail_reviews="y"; }else{ user_mail_reviews="n"; }
}
// if(document.getElementById("user_mail_followers")){
//	 if(document.getElementById("user_mail_followers").checked)
//	 { user_mail_followers="y"; }else{ user_mail_followers="n"; }
// }
if(document.getElementById("user_mail_weekly")){
	if(document.getElementById("user_mail_weekly").checked)
	{ user_mail_weekly="y"; }else{ user_mail_weekly="n"; }
}

if(document.getElementById("user_fb_post_to_profile")){
	if(document.getElementById("user_fb_post_to_profile").checked)
	{ user_fb_post_to_profile="y"; }else{ user_fb_post_to_profile="n"; }
}else{user_fb_post_to_profile="y";}


poststr = "/ajax.php?action=update_user_details";
poststr = poststr + "&user_username_new=" + escape(encodeURI(user_username_new));
poststr = poststr + "&user_username=" + escape(encodeURI(our_review_email));
poststr = poststr + "&user_email_new=" + escape(encodeURI(user_email_new));
poststr = poststr + "&user_first_time_code=" + user_first_time_code;
poststr = poststr + "&secure_password=" + secure_password;
poststr = poststr + "&secure_password_new=" + secure_password_new;
poststr = poststr + "&user_nicename=" + escape(encodeURI(user_nicename));
poststr = poststr + "&user_image_coordinate_values=" + document.getElementById("user_image_coordinate_values").value;                            
poststr = poststr + "&user_city=" + escape(encodeURI(user_city));
poststr = poststr + "&user_zip_code=" + escape(encodeURI(user_zip_code));
poststr = poststr + "&user_fb_uid=" + escape(encodeURI(user_fb_uid));
poststr = poststr + "&user_fb_token=" + escape(encodeURI(user_fb_token));
poststr = poststr + "&user_fb_email=" + escape(encodeURI(user_fb_email));
poststr = poststr + "&user_fb_nicename=" + escape(encodeURI(user_fb_nicename));
poststr = poststr + "&user_fb_post_to_profile=" + escape(encodeURI(user_fb_post_to_profile));
poststr = poststr + "&user_mail_reviews=" + escape(encodeURI(user_mail_reviews));
// poststr = poststr + "&user_mail_followers=" + escape(encodeURI(user_mail_followers));
poststr = poststr + "&user_mail_weekly=" + escape(encodeURI(user_mail_weekly));
poststr = poststr + "&user_description=" + escape(encodeURI(user_description));  

GDownloadUrl(poststr, function(data, responseCode) {

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

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

	if (ajax_array[0]==1) { 
				document.getElementById("comment_alert_110").innerHTML = ajax_array[1]; 
				document.getElementById("comment_part_box_110").className = "comment_part_box_highlighted";
				alert(ajax_array[1]);
				document.getElementById("user_nicename").focus();
	}
	if (ajax_array[0]==2) { 
				document.getElementById("comment_part_box_120").className = "comment_part_box_highlighted";
				alert(ajax_array[1]);
	}
	if (ajax_array[0]==3) { 
				document.getElementById("comment_alert_130").innerHTML = ajax_array[1]; 
				document.getElementById("comment_part_box_130").className = "comment_part_box_highlighted";
				alert(ajax_array[1]);
				document.getElementById("user_city").focus();
				}
	if (ajax_array[0]==4) {  
				document.getElementById("comment_alert_140").innerHTML = ajax_array[1]; 
				document.getElementById("comment_part_box_140").className = "comment_part_box_highlighted";
				alert(ajax_array[1]);
				document.getElementById("user_zip_code").focus();
				}
	if (ajax_array[0]==5) {  
				document.getElementById("comment_part_box_150").className = "comment_part_box_highlighted"; 
	  			document.getElementById("comment_alert_150").innerHTML = ajax_array[1];
				alert(ajax_array[1]);
				document.getElementById("user_description").focus();
				}
	if (ajax_array[0]==6) {  
				document.getElementById("comment_part_box_160").className = "comment_part_box_highlighted"; 
	  			document.getElementById("comment_alert_160").innerHTML = ajax_array[1];
				alert(ajax_array[1]);
				}	
	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]==0) { 
				document.getElementById("comment_alert_185").innerHTML = ajax_array[1]; 
				document.getElementById("comment_part_box_185").className = "comment_part_box_highlighted";
				alert(ajax_array[1]);
				document.getElementById("user_email_new").focus();
	}			
	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();
				location.href = "http://www.allakartor.se/super_login.php?username=" + ajax_array[1] + "&secure_password=" + secure_password_new + "&next=" + "http://www.minkarta.se/" + user_nicename.replace(" ","_") + "/" + "&remember_me=y";
				}				

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

});
}


// FUNCTION TO HASH THE USER_PASSWORD
function hash_user_password() {
		
		if (document.getElementById('user_password_new')) {
		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 = '';
		}
		else
		{
		secure_password_new = document.getElementById('secure_password').value;	
		}
}

