/// <reference path="jquery-1.3.2-vsdoc.js" />

/** 
 * Toggle the visibility of a field
 * 
 * Note: css must contain the following selectors:
 *
 * .visible {
 *     display: block;
 * }
 * .invisible {
 *     display: none;
 * }
 *
 * Example:
 *
 * <a href="#" onclick="toggleVisibility('fieldId')">toggle</a>
 * <div id="fieldId" class="invisible">hello world</div>
 */
function toggleVisibility(myfieldname) 
{
    var myfield = document.getElementById(myfieldname);
    
    if (myfield.className == "invisible") 
	{
        // myfield.className = "visible"; 
        // NOTE: using class "visible" instead of empty quotes will cause table rows to grow horizontally
        myfield.className = "";
    } 
	else 
	{
        myfield.className = "invisible";
    }
    
    return false;
}

/** 
 * Make a hidden field visible
 * 
 * Example:
 *
 * <a href="#" onclick="makeVisible('fieldId')">show</a>
 * <div id="fieldId" class="invisible">hello world</div>
 */
function makeVisible(myfieldname) 
{
    var myfield = document.getElementById(myfieldname);
    
    if (myfield !== null) 
	{ 
        // NOTE: using class "visible" instead of empty quotes will cause table rows to grow horizontally
        myfield.className = "";
    }

    return false;
} 

/** 
 * Make a field invisible
 * 
 * Example:
 *
 * <a href="#" onclick="makeInvisible('fieldId')">hide</a>
 * <div id="fieldId" class="visible">hello world</div>
 */
function makeInvisible(myfieldname) 
{
    var myfield = document.getElementById(myfieldname);
    
    if (myfield !== null) 
	{ 
        myfield.className = "invisible";
    }
    
    return false;
} 

function deleteTag(tagId) 
{
    var myfield = document.getElementById('delete_tag_id');
    
    if (myfield !== null) 
	{ 
		myfield.value = tagId;
		document.delete_tag_form.submit();
    }
    return false;
} 

function selectTag(tagId) 
{
    var myfield = document.getElementById('search_tag_id');
    
    if (myfield !== null) 
	{ 
		myfield.value = tagId;
		document.search_tag_form.submit();
    }
    return false;
} 



/** 
 * Hide the peek section on page load
 */
$(document).ready(
function() 
{
	$('#config').hide();
	$('#config').css("height", "300px");

	$('a#slideout').click(function() 
	{
		$('#config').slideToggle("normal");
		return false;
	});
});




function editUser(id)
{
	$("#newUserbutton").hide();
	$("#editUserDialog").dialog('option', 'title', 'Edit User');
	$("#userDialogSubmitButton").val("Save");
	$("#formAction").val("edit");
	
	$(document).ready(function(){
	    $.getJSON("/admin/ajaxHandler?action=user&userId=" + id,
	        function(data)
	        {
	    		$("#dialogUserId").val(data.userId);
	    		$("#loginId").val(data.loginId);
	    		$("#userNameFirst").val(data.nameFirst);
	    		$("#userNameLast").val(data.nameLast);
	    		$("#userEmail").val(data.email);
	    		$("#userStatus").val(data.status);
	    		$("#userCompany").val(data.company);
	    		$("#userPhone").val(data.phone);
	    		$("#userAddress1").val(data.address1);
	    		$("#userAddress2").val(data.address2);
	    		$("#userCity").val(data.city);
	    		$("#userState").val(data.state);
	    		$("#userZip").val(data.zip);
        	});
	  });
	
	if ($(".ui-state-error").length > 0)
	{
		$("#editUserDialog").dialog('option', 'position', 'bottom');
	}
	
	$("#editUserDialog").dialog('open');
}

function saveUser(id)
{
	$("#userId").val(id);
	$("#userAction").val("save");
	
	$("#loginIdHidden").val($("#loginIdInline").val());
	$("#nameFirstHidden").val($("#nameFirstInline").val());
	$("#nameLastHidden").val($("#nameLastInline").val());
	$("#statusHidden").val($("#statusInline").val());
	
	$("#userForm").submit();
}

function deleteUser(id)
{
	if (confirm("Are you sure you would like to delete this user?")) 
	{
		$("#userId").val(id);
		$("#userAction").val("delete");
		$("#userForm").submit();
	}
}

function cancelUser(id)
{
	$("#userId").val("");
	$("#userAction").val("");
	$("#userForm").submit();
}



$(document).ready(function(){
	$("#passwordChange").validate({
		rules: 
		{
			password1: "required",
			password2: 
			{
				equalTo: "#password1"
			}
		}
	});
	
	$("#passwordKeyGen").validate(
	{
		rules: 
		{
			email: "required",
			email: true
		}
	});
	
	$("#newUserForm").validate(
	{
		rules: 
		{
			loginId: "required",
			nameFirst: "required",
			nameLast: "required"
		}
	});
	
	$("#dialogUserForm").validate(
	{
		rules: 
		{
			loginId: "required",
			userNameFirst: "required",
			userNameLast: "required",
			userEmail: "required"
		}
	});

});


$(document).ready(function(){
	$("#editUserDialog").dialog({ autoOpen: false, width: 320 }); 
	
	$("#userDialogCancelButton").click(
		function () 
		{
			// if error is displayed, refresh page
			if ($(".ui-state-error").length > 0)
			{
				cancelUser(0);
			}
			
			// otherwise, just close the dialog without the roundtrip
			$("#editUserDialog").dialog('close');
		}
	);
	
	// Create a new user
	$("#userDialogSubmitButton").click(
		function () 
		{
			if ($("#dialogUserForm").validate().form())
			{
				$("#dialogUserForm").submit();
				$("#editUserDialog").dialog('close');
			}
		}
	);
	
	
	$("#newUserbutton").click(
		function () 
		{
			$("#newUserbutton").hide();
			$("#editUserDialog").dialog('option', 'title', 'Create New User');
			$("#userDialogSubmitButton").val("Create");
			$("#formAction").val("create");
			
			// TODO: make this a funtion
			// blank all values
    		$("#dialogUserId").val("");
    		$("#loginId").val("");
    		$("#userNameFirst").val("");
    		$("#userNameLast").val("");
    		$("#userEmail").val("");
    		$("#userStatus").val("");
    		$("#userCompany").val("");
    		$("#userPhone").val("");
    		$("#userAddress1").val("");
    		$("#userAddress2").val("");
    		$("#userCity").val("");
    		$("#userState").val("");
    		$("#userZip").val("");
			
			$("#editUserDialog").dialog('open');
		}
	);
	
	$("#editUserDialog").bind('dialogclose', function(event, ui) {
		$("#newUserbutton").show();
		$("#dialogUserForm").validate().resetForm();
	});


});
$(document).ready(function(){
	$('#tabs').tabs();
	$('#tabs').show();
});

//$(document).ready(function(){
//	console.log("in doc ready");
//});

$(document).ready(function(){
	$("#contentAccessType").change(
		function () 
		{
			var contentType = $("#contentAccessType").val();
			
			if (contentType == 0)
			{
				// switching to single mode, check multiple editors before roundtripping.
				//var content_0_isDirty = CKEDITOR.instances.page_content.checkDirty();
				//if (content_0_isDirty)
				//{
					//console.log("Content 0 is dirty");
				//}
				
				$("#page_content_form_save").val("Edit");
				$("#page_content_form_save").click();
			}
			else 
			{
				// switching to multiple mode, check single editor before roundtripping.
				//var content_0_isDirty = CKEDITOR.instances.page_content.checkDirty();
				//if (content_0_isDirty)
				//{
					//console.log("Content is dirty");
				//}
				
				$("#page_content_form_save").val("Edit");
				$("#page_content_form_save").click();
			}
		}
	);
});


$(document).ready(function()
{
	$("#adminEditPageContentButton").click(function() 
	{
		$("#editPageContentButton").click();
	});
	
	//$("#addProductForm #submitbutton").click(function() 
	//{
		//if ($("#addProductForm").validate().form())
		//{	
			//$("#addProductForm #submitbutton").attr('disabled', 'disabled');
			//$("#addProductForm").submit();
		//}
	//});
});

