// Remember the scroll position
function getScrollXY() 
{
    var x = 0, y = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    return [x, y];
}

// Reset the scroll position
function setScrollXY(y) 
{
    window.scrollTo(0, y);
}

// Get a query string parameter
function get_url_var( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


function contact_us()
{
	if ($('#email').val() == '')
	{
		alert('Please enter your email address');
		return false;
	}
	
	if ($('#subject').val() == '') 	
	{
		alert('Please enter a subject');
		return false;
	}
	
	if ($('#message').val() == '')
	{
		alert('Please enter a message');
		return false;
	}
	
	$.post("Messages", {form_method: "email_contact_us", name: $('#name').val(), phone: $('#phone').val(), subject: $('#subject').val(), message: $('#message').val(), from: $('#email').val()},
		   function(data){
			   
			   
				if (trim(data) == 'success') 
				{
					alert('Thank you for your feedback.');
					window.location = 'Parents_Home';
				}
				else
				{
					alert('Hmm.... there was an error sending the message.  Please try again a little later.');	
				}
				
		   }, "text");
}


function submit_testimonial()
{
	
	$.post("Testimonial", {form_method: "add", id: $('#user_id').val(), user_type: $('#user_type').val(), message: $('#message').val()},
		   function(data){


				if (trim(data) == 'success')
				{
					alert('Thank you for your feedback.');
					window.location = 'Parents_Home';
				}
				else
				{
					alert('Hmm.... there was an error sending the message.  Please try again a little later.');
				}

		   }, "text");
}


function delete_account()
{
	if (confirm('Are you sure you want to leave us and delete your account?  If you decide to use our services in the future, you will be required to sign up, create a new account, and pay for a membership again!'))
	{
			$.post("Users", {form_method: "delete_account"}, 
		   function(data){			   
			   
				if (trim(data) == 'success') 
				{
					alert('Your account has been deleted.  Mahalo for using our services and we hope to have you back again sometime.');
					window.location = 'Home';
				}
				else
				{
					alert('Hmm.... there was an error deleting your account.  Please try again later.');	
				}
				
		   }, "text");
	
	}
	
}

function suspend_account()
{
	if (confirm('Are you sure you want to suspend your account?  Parents and sitters will not be able to view your profile or find you in searches.  You will be able to resume your account next time you log in.'))
	{
			$.post("Users", {form_method: "suspend_account"}, 
		   function(data){			   

				if (trim(data) == 'success') 
				{
					alert('Your account has been suspended.  Please log back in to resume your account.');
					window.location = 'Home';
				}
				else
				{
					alert('Hmm.... there was an error suspending your account.  Please try again later.');	
				}
				
		   }, "text");
	
	}
	
}

function delete_job(id)
{
	if (confirm('Are you sure you want to delete this job?'))
	{
			$.post("Jobs", {form_method: "delete", id: id}, 
		   function(data){ 
			$('#job_header_' +ltrim(data)).fadeOut();
		   }, "text");
	
	}
	
}


// Add a favorite
function add_favorite_sitter(id)
{	
	$.post("Favorites", {form_method: "add_favorite_sitter", sitter_user_id: id}, 
		   function(data){
			    img = document.getElementById('star_' +ltrim(data));
				img.src = img.src.replace('yellow', 'white');
				text = document.getElementById('fave_text_' +ltrim(data));
				text.innerHTML = 'Added to Faves';
				button = document.getElementById('fave_text_' +ltrim(data));
				button.className = 'action_link_disabled';
				
		   }, "text");
	
}

// Delete a favorite
function remove_favorite_sitter($id)
{
	
	$.post("Favorites", {form_method: "remove_favorite_sitter", sitter_user_id: $id}, 
		   function(data){
				$('#sitter_header_' +ltrim(data)).fadeOut();
				
		   }, "text");
}

// Add a favorite
function add_favorite_parent(id)
{	
	$.post("Favorites", {form_method: "add_favorite_parent", parent_user_id: id}, 
		   function(data){
			    img = document.getElementById('star_' +ltrim(data));
				img.src = img.src.replace('yellow', 'white');
				text = document.getElementById('fave_text_parent_' +ltrim(data));
				text.innerHTML = 'Added to Faves';
//				button = document.getElementById('fave_text_parent' +ltrim(data));
				text.className = 'action_link_disabled';
				
		   }, "text");
	
}

// Delete a favorite
function remove_favorite_parent($id)
{
	
	$.post("Favorites", {form_method: "remove_favorite_parent", parent_user_id: $id}, 
		   function(data){
				$('#parent_header_' +ltrim(data)).fadeOut();
				
		   }, "text");
}

// Add a favorite
function add_favorite_job(id)
{	
	$.post("Favorites", {form_method: "add_favorite_job", job_id: id}, 
		   function(data){
			    img = document.getElementById('star_' +ltrim(data));
				img.src = img.src.replace('yellow', 'white');
				text = document.getElementById('fave_text_' +ltrim(data));
				text.innerHTML = 'Added to Faves';
				button = document.getElementById('fave_text_' +ltrim(data));
				button.className = 'action_link_disabled';
				
		   }, "text");
	
}

// Delete a favorite
function remove_favorite_job($id)
{
	$.post("Favorites", {form_method: "remove_favorite_job", job_id: $id}, 
		   function(data){
			   
				$('#job_header_' + ltrim(data)).fadeOut();
				
		   }, "text");
}



$(document).ready(function() {
		

	
});

 
	
function send_message()
{
	if ($('#subject').val() == '') 	
	{
		alert('Please enter a subject');
		return false;
	}
	
	if ($('#message').val() == '')
	{
		alert('Please enter a message');
		return false;
	}
	
	$.post("Messages", {form_method: "send_message", recipient: $('#recipient').val(), subject: $('#subject').val(), message: $('#message').val(), conversation: $('#conversation').val()}, 
		   function(data){
			   
				if (trim(data) == 'success') 
				{
					alert('Your message was sent successfully');
					close_message();
				}
				else
				{
					alert('Hmm.... there was an error sending the message.  Please try again.');	
				}
				
		   }, "text");
}

function send_application()
{
	if ($('#message').val() == '')
	{
		alert('Please enter a message');
		return false;
	}
	
	$.post("Messages", {form_method: "send_application", jobId: $('#jobId').val(), message: $('#message').val()}, 
		   function(data){
			   
			   
				if (trim(data) == 'success') 
				{
					alert('Your application was sent successfully');
					close_message();
				}
				else
				{
					alert('Hmm.... there was an error sending the message.  Please try again.');	
				}
				
		   }, "text");
}



function close_message()
{
    api.close();
    
					
}


// Delete a message
function delete_message(id)
{
	
	
	$.post("Messages", {form_method: "delete_message", id: id}, 
		   function(data){
			   
				$('#row_' +ltrim(data)).fadeOut();				
		   }, "text");
	
}

function update_password()
{
	if ($('#old_password').val() == '')
	{
		alert('Please enter your old password');
		return false;
	}
	
	if ($('#new_password').val() == '')
	{
		alert('Please enter a new password');
		return false;
	}
	
	if ($('#confirm_password').val() == '')
	{
		alert('Please confirm your new password');
		return false;
	}
	
	if ($('#new_password').val() != $('#confirm_password').val())
	{
		alert('Your new password and confirmation password must match!');
		return false;
	}
	
	$.post("Users", {form_method: "update_password", old_password: $('#old_password').val(), new_password: $('#new_password').val()}, 
		   function(data){
			   
			   
				if (trim(data) == 'success') 
				{
					alert('Your password has been changed.');
					close_message();
				}
				else if (trim(data) == 'wrong')
				{
					alert('The password you provided is incorrect');	
				}
				else if (trim(data) == 'invalid')
				{
					alert('The new password you provided is invalid.  Please make sure it is at least 6 characters long.');	
				}
				else 
				{
					alert('Hmm.... there was an error changing your password.  Please try again.');	
				}
				
		   }, "text");
}

function create_job() 
{ 
	type = $('[name=job_type]:checked').val();
	url = "Jobs?form_method=add&type=" + type;
	window.location=url;
}


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function show_emails() {
    $('#email_address_list').fadeIn();
}