$(document).ready(function() {
  $("#loginWindow").dialog({ autoOpen: false, modal: true, width:600, height: 450 });
  $(".ui-dialog-titlebar").hide();
  $("#searchtext").keyup(function(e) {
	  if(e.keyCode == 13) {
		  $("#search_button_image").click();
	  }
  });
  $("#location").keyup(function(e) {
	  if(e.keyCode == 13) {
		  $("#search_button_image").click();
	  }
  });
});
function loadLogin() {
	var cookie = $.cookie("http://warriorgateway.org/ml/gwt/20100129/login-page-username");
	$('#loginWindow').dialog('open');
	if (cookie != null) {
		$('#loginemail').val(cookie);
		$('#loginpassword')[0].focus();
	} else {
		$('#loginemail')[0].focus();
	}
	$('#loginpassword').keyup(function(event){
		if (event.keyCode == '13') {login();}
	});
	$('#loginemail').keyup(function(event){
		if (event.keyCode == '13') {login();}
	});
}

function login() {
	var login = $('#loginemail').val();
	var password = $('#loginpassword').val();
	var message = "";
	var loginUrl = "/service/loginUser";
	$('#loginmessage').html("<b>Please wait...</b>");
	if(login == "" || password == "") {
		message = "You must enter a username and password";
		$('#loginmessage').html("<b>"+message+"</b>");
	} else {
		$.post(loginUrl, { "email": login, "password": password },
			function(xml){
			$('#loginmessage').html("<b>"+$(xml).find("result").text());
			if($(xml).find("result").text() == "Login Successful") {
				$('#loginmessage').html("<b>Login Successful / Please wait...</b>");
				$.cookie("http://warriorgateway.org/ml/gwt/20100129/login-page-username", login);
				window.location.reload();
			}
			if($(xml).find("result").text() == "Login Unsuccessful : Inactive User") {
				$('#loginForm').hide();
				$('#logindetail').html("<p>Your account has not yet been validated. When you registered on the Warrior Gateway you were sent a confirmation email. Please click on the validation link in that email to confirm your account. If you did not receive a confirmation email, please check your email spam filter, or click <a href='/service/resendConfirmationEmail?email=" + $('#loginemail').val() + ">here</a> to be resent the confirmation email.</p>");
			}
			}, "xml");
	}
}

function logout() {
	var request = initRequest();
	request.open("GET", "/service/logoutUser/", false);
    request.send("");
	window.location.href="/";
}


function initRequest() {
	request = false;
	
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = false;
			}  
		}
	}
	if (!request) {
	     alert("Error initializing XMLHttpRequest!");
	}
	
	return request;
}

function enableToggleDisplay(linkId, divId) {
	$(linkId).click(function() {
		$(divId).toggle(400);
		return false;
	});
}

function enableShowDisplay(linkId, divId) {
	$(linkId).click(function() {
		$(divId).show(400);
		return false;
	});
}

function toggleDiv(divId) {
	$('#'+divId).toggle(800);
	return false;
}

function hideDiv(divId) {
	$('#'+divId).hide(800);
	return false;
}

function showDiv(divId) {
	$('#'+divId).show(800);
	return false;
}


//for cleaning the greater than less than for javascript

$.extend({URLClean:function(c){
	var o='';
	o = c.replace("]--&gt;", "");
	o = o.replace(/&lt;/g, "<");
	o = o.replace(/&gt;/g, ">");
	return o;}
});


/*
 * Adds "ghost" style and suggestion text to direct the user.
 *   On click, if value of element is still the same a the original 
 * suggestion text, the text and style are removed.
 */
function addFormSuggestionText(elementId, suggestionText) {
	$('#' + elementId).addClass('formPrefill');
	$('#' + elementId).val(suggestionText)
	$('#' + elementId).focus(function() {
		if ($('#' + elementId).val() == suggestionText) {
			$('#' + elementId).val('').removeClass('formPrefill');
		}
	});
}

/*
 * Limits the number of characters a user can enter into a textarea.
 * Informs the user as to how many characters they have left in the textarea.
 * "You have X characters left."
 * Once limit is reached, it truncates the textarea to the max limit.
 * 
 * @limit the character limit
 * @elementToLimit The element id that is to be limited
 * @infoElement The element id that is to contain the feedback to the user
 * @elementLabel The label of the form field that is being limited (i.e. 'Description'
 */
function limitCharacters(limit, elementToLimit, infoElement, elementLabel) {
	$('#' + infoElement).html('You have ' + limit + ' characters left.');
	
	$('#' + elementToLimit).keyup(function() {
		var text = $('#' + elementToLimit).val(); 
		if (text.length > limit) {
			$('#' + infoElement).addClass('error');
			$('#' + elementToLimit).val(text.substr(0,limit));
		}
		else {
			$('#' + infoElement).removeClass('error');
		}
		var limitDisplay = ((limit - text.length) < 0) ? 0 : (limit-text.length)
		$('#' + infoElement).html('You have ' + limitDisplay + ' characters left.');
	});
}

function getElementsByTagName(xml, namespace, tagName) {
	if (namespace != '' && $(xml).find(namespace + '\\:' + tagName).length > 0) {
		return $(xml).find(namespace + '\\:' + tagName);
	}
	else {
		return $(xml).find(tagName);
	}
}

function getElementValue(xml, namespace, tagName) {
	var el = getElementsByTagName(xml, namespace, tagName);
	return el.text();
}
