// document.getElementById('mydiv').className = 'input_bg_fail'

var req;
var reqTimeout;
var mode;

function loadXMLDoc(url) {
    req = null;
    if (window.XMLHttpRequest) {
        try {
            req = new XMLHttpRequest();
        } catch (e){}
    } else if (window.ActiveXObject) {
        try {
            req = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e){
            try {
                req = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e){}
        }
    }
 
    if (req) {
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
        reqTimeout = setTimeout("req.abort();", 5000);
    } else {
        alert("Non-AJAX browser");
    }
}

var text = ""; 

function show_userdata(text) {
	$("#login_userinfo").html(text);
	$("#login_userinfo").show("drop","",700);
//	document.getElementById('login_form_container').innerHTML = text;
}

function show_rater(rating) {
	rating_px = 125/5*rating;
	$("#rating").html("<ul class='star-rating'>" +
		"<li class='current-rating' style='width:" + rating_px + "px;'>Currently " + rating + "/5 Stars.</li>" +
		"<li><a href='javascript:;' onclick='rate(1)' title='1 star out of 5' class='one-star'>1</a></li>" +
		"<li><a href='javascript:;' onclick='rate(2)' title='2 stars out of 5' class='two-stars'>2</a></li>" +
		"<li><a href='javascript:;' onclick='rate(3)' title='3 stars out of 5' class='three-stars'>3</a></li>" +
		"<li><a href='javascript:;' onclick='rate(4)' title='4 stars out of 5' class='four-stars'>4</a></li>" +
		"<li><a href='javascript:;' onclick='rate(5)' title='5 stars out of 5' class='five-stars'>5</a></li>" +
		"</ul>");
}

function processReqChange() {
    if (req.readyState == 4) {
        clearTimeout(reqTimeout);
 
        if (req.status == 200) {
        	if (mode == "update_sort_by") {
        		document.getElementById('sort_selector').innerHTML = req.responseText;
        	}
        	
        	if (mode == "user_info_update") {
        		if (req.responseText == "ok") {
        			$("#myform").hide("blind","",500);
        			setTimeout('$("#success").fadeIn(700);', 550);
        		} else if (req.responseText == "user_deleted") {
        			$("#myform").hide("blind","",500);
        			setTimeout('$("#user_deleted").fadeIn(700);', 550);
        		} else {
        			alert(req.responseText);
        		}
        		return;
        	}
        	
        	if (mode == "send_to_friends") {
        		if (req.responseText == "wrong_mail") {
        			$("#email_error").show("drop","",500);
        			email_seems_wrong = true;
        		} else if (req.responseText == "ok") {
        			$("#form_container").hide("drop","",500);
        			setTimeout('$("#success_msg").show("blind","",500)', 500);
        		} else {
        			alert(req.responseText);
        		}
        	}
        	
        	if (mode == "log_in") {
        		if (req.responseText == "wrong") {
        			document.getElementById('login_pwd').className = "input_bg_fail";
        		} else {
        			$("#login_form_container").toggle("drop","",500,show_userdata(req.responseText));
					if (logged_in_actions) {
						logged_in_actions();
					}
        		} 
        	}
        	if (mode == "pwd_check") {
        		if (req.responseText == "ok") {
        			document.login_form.submit();
        		} else {
        			document.getElementById('login_pwd').className = "input_bg_fail";
        			// alert(req.responseText);
        		}
        	}
        	if (mode == "move_gift") {
        		if (req.responseText != "ok") {
        			alert(req.responseText);
        		}
        	}
        	if (mode == "delete_gift_from_my_list") {
        		if (req.responseText == "deleted") {
        			$("#" + deleted_element_id).hide("drop","",500);
        		}
        		
//        		YAHOO.example.DDApp.init();
        	}
        	if (mode == "delete_comment") {

        	}
        	if (mode == "rate") {
//        		alert(req.responseText);
        		if (req.responseText == "log_in") {
        			window.location = "/register.php?mode=login";
        			return;
        		} else if (req.responseText == "already_voted") {
        			alert(already_voted_for_gift);
        		} else {
        			show_rater(req.responseText);
        		}
        	}
        	if (mode == "shop_redirect") {
        		//Won't use url but id's instead - so disabling the check for a valid url
            //if (req.responseText.indexOf("http://") >= 0) {
        			document.location = "http://" + host + "/shop.php?url=" + escape(req.responseText);
        		//} else {
        		//	alert(req.responseText);
        		//}
        	}
        	if (mode == "add_to_list2") {
        		if (req.responseText == "not logged in") {
        			window.location = "/register.php?mode=login";
        			return;
        		} else if (req.responseText == "ok") {
        			document.getElementById("add_div_" + gift_id_g).innerHTML = "OK, added. Check your <a class='wishlink' href='/sort_my_list.php'>wishlist</a> if you wish";
        		} else {
        			alert(req.responseText);
        		}
        	}
        	if (mode == "add_as_friend") {
        		if (req.responseText == "not logged in") {
        			window.location = "/register.php?mode=login";
        			return;
        		} else if (req.responseText == "added") {
        			document.getElementById("friend_link").innerHTML = msg_added;
        		} else if (req.responseText == "removed") {
        			document.getElementById("friend_link").innerHTML = msg_removed;
        		} else {
        			alert(req.responseText);
        		}
        	}        	
			if (mode == "add_to_list") {
        		if (req.responseText == "not logged in") {
					$("#add_div_unlogged").toggle("drop","",500);
					ready_to_add = 0;
        			return;
        		}
				if (req.responseText == "ok") {
					$("#add_div").toggle("puff","");
					$("#add_div_success").toggle("drop");
				} else {
					alert(req.responseText);
				}
			}
        	if (mode == "add_comment") {
        		if (req.responseText == "not logged in") {
        			window.location = "/register.php?mode=login";
        			return;
        		}
        		var xmlDoc = req.responseXML;
        		var commentsArray = xmlDoc.getElementsByTagName("comment");
        		
        		if (commentsArray.length == 0) {
        			alert(req.responseText);
        		}
        		
        		for (var i = 0; i < commentsArray.length; i++) {
        			id = xmlDoc.getElementsByTagName("id")[i].childNodes[0].nodeValue;
        			author = xmlDoc.getElementsByTagName("author")[i].childNodes[0].nodeValue;
        			date = xmlDoc.getElementsByTagName("date")[i].childNodes[0].nodeValue;
        			text = xmlDoc.getElementsByTagName("text")[i].childNodes[0].nodeValue;
//        			is_own = xmlDoc.getElementsByTagName("is_own")[i].childNodes[0].nodeValue;
        			
        			show_comment(id, author, date, text, 1, 1);
        		}
        	}
        } else {
            alert("Fail: \n" + req.statusText);
        }
    }  
}

function check_password() {
	mode = "pwd_check";
	email = escape(document.getElementById('email').value);
	password = escape(document.getElementById('password').value);

	loadXMLDoc("/login.php?ajax_check&email=" + email + "&password=" + password);
	return false;
}



function log_in() {
	mode = "log_in";
	email = escape(document.getElementById('email').value);
	password = escape(document.getElementById('password').value);
	//	$("#login_form_container").toggle("drop","",500,show_userdata("test"));
	loadXMLDoc("/login.php?ajax_login&email=" + email + "&password=" + password);
//	document.location = "/login.php?ajax_login&email=" + email + "&password=" + password;
	return false;
}

// Regform functions:
function reg_check_form() {
	if (reg_check_email() == 1 && reg_check_name() == 1) {
		return true;
	} else {
		return false;
	}
}

var already_alerted = 0;
var is_this_submit = 0;

function reg_check_email() {
	if (valid_email(document.getElementById('reg_email').value) != 1) {
		document.getElementById('reg_email_error').style.display = 'inline';
		document.getElementById('reg_email_success').style.display = 'none';
		alert("Please check your e-mail adress as it does not seem to be valid.");
		return 0;
	} else {
		$.get(
				"/register.php?check_email=" + document.getElementById('reg_email').value,
				"{key:value}",
				function(data) {
					if (data == "ok") {
						document.getElementById('reg_email_error').style.display = 'none';
						document.getElementById('reg_email_success').style.display = 'inline';
						if (is_this_submit == 1 && reg_check_name() == 1 && reg_check_pwd() == 1) {
							document.regform.submit();
						}
						return 1;
					} else {
						if (already_alerted == 0) {
							alert(data);
							already_alerted = 1;
						}
						return 0;
						is_this_submit = 0;
					}
				},
				"html"
			);
	}
}

function reg_check_name() {
	if (document.getElementById('reg_name').value.length < 2) {
		document.getElementById('reg_name_error').style.display = 'inline';
		document.getElementById('reg_name_success').style.display = 'none';
		return 0;
	} else {
		document.getElementById('reg_name_error').style.display = 'none';
		document.getElementById('reg_name_success').style.display = 'inline';
		return 1;
	}
}

function reg_check_pwd() {
	if (document.getElementById('reg_password').value.length < 5) {
		document.getElementById('reg_pwd_error').style.display = 'inline';
		document.getElementById('reg_pwd_success').style.display = 'none';
		alert("Please choose a code that is at least 5 characters long.");
		return 0;
	} else {
		document.getElementById('reg_pwd_error').style.display = 'none';
		document.getElementById('reg_pwd_success').style.display = 'inline';
		return 1;
	}
}

function valid_email(email) {
	  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	  if(reg.test(email) == false) {
		return 0;
	  } else {
		return 1;
	  }
}
// Regform end

function add_comment(comment) {
	mode = "add_comment";
	loadXMLDoc("/comments.php?ajax_add&gift_id=" + gift_id + "&comment=" + escape(comment));
}

function date_dropdown (start, end, order, selected, name, css, caption) {
	document.write ("<div class='dob_block'>");
	if (css != "sm_dob" && css != "sm_dob_year") {
		 document.write(caption + ":<br />");
	}
	
	if (css == "") {
		document.write('<select name="' + name + '" style="padding-left:0px; margin-left:0px;">');
	} else {
		document.write('<select name="' + name + '" style="padding-left:0px; margin-left:0px;" class="' + css + '">');
	}
	if (order == "asc") {
		for (c = start; c <= end; c++) {
  			if (c == selected) {
  				document.write('<option name="'+c+'" selected>' + c + '</option>');
  			} else {
  				document.write('<option name="'+c+'">' + c + '</option>');
  			}
  		}
	} else {
      	for (c = start; c >= end; c--) {
      		if (c == selected) {
      		  document.write('<option name="'+c+'" selected>' + c + '</option>');
      		} else {
      		  document.write('<option name="'+c+'">' + c + '</option>');
      		}
      	}
	}
  	document.write("</select></div>");
}

var gift_id_g = 0;

function ajax_addthis2(gift_id) {
	gift_id_g = gift_id;
	mode = "add_to_list2";
	loadXMLDoc("/my_wish_list.php?ajax_add_gift=" + gift_id + "&gift_ideas");
}

function add_as_friend(id) {
	mode = "add_as_friend";
	loadXMLDoc("/friends.php?ajax_add_friend=" + id);	
}

function add_gmail_friend(id) {
	document.getElementById(id).style.visibility='hidden';
	loadXMLDoc("/sort_my_list.php?add=" + id);	
}

function send_friendship_request(mail) {
	//loadXMLDoc("/friends.php?friend_mail=" + mail);	
	//document.getElementById('friends_email').value = "Add more...";
	window.location = "?friends_email=" + mail;
}

function check_friend_email() {
	 if (valid_email(document.getElementById('friends_email').value) != 1) {
		alert("E-mail is invalid!");
		document.getElementById('btn_div').style.visibility = 'hidden';
	  } else {
	  document.getElementById('btn_div').style.visibility = 'visible';
	  }
}
