$.metadata.setType("attr", "validate"); 

$(function() {
	$("#services_list").tabs();
	
	//Jquery Submit Form
	$(".ajaxForm").live('submit', function() {
		var form = $(this);
		var formAction = $(this).attr('action'); 
		var values = $(this).serialize();
		values += "&ajax=true"; 
				$.ajax({  
						type: "POST",  
						url: formAction ,
						dataType: "json",
						data: values,
						success: function(data) {
							if (data.success){
								if (data.form){
									form.fadeOut(1000, function(){
										if(data.parent){
											form.parent().html(data.message)
													.hide()
													.fadeIn(1000);
													$("#create_form").validate();
										} else {
											form.html(data.message)
													.hide()
													.fadeIn(1000);
										}
									});
								} else {
									if(data.parent){
										form.parent().html(data.message)
												.hide()
												.fadeIn(1000);
									} else {
										form.html(data.message)
												.hide()
												.fadeIn(1000);
									}
								}
							} else {
								form.siblings(".message").html(data.message);
							}
						}//end success function
				});
		return false;
	});


	$("#username input").live('focus',function() {			
		$("#username label").hide();
	});
	
	$("#username input").live('blur',function() {	
		if($(this).val() == ""){
			$("#username label").show();
		} 
	});
	
	
	$("#password input").live('focus',function() {			
		$("#password label").hide();
	});
	
	$("#password input").live('blur',function() {	
		if($(this).val() == ""){
			$("#password label").show();
		} 
	});
	
	
	$(".logout").live('click', function() {
			var formAction = $(this).attr("href");
			var values = "ajax=true";   
					 $.ajax({  
						type: "POST",  
						url: formAction ,
						dataType: "json",
						data: values,
						success: function(data) {
							if (data.success){
								$("#logedin").fadeOut(1000, function(){
										$("#logedin").parent().html(data.message)
												.hide()
												.fadeIn(1000);
								});
							}
						}//end success function
					});
				return false;	
			});
	
	//ORDER FORM PAGE
	
		$("#date").datepicker();
		//$("#bank").hide();
		$("[id^=order_type]").click(function(){
				var ordertype = $(this).val();
				if (ordertype != "Bankruptcy"){
					$("#caset_name").empty();
					$("#caset_name").append(ordertype);
					$("#bank").hide();
					$("#case").show();
				} else {
					$("#case").hide();
					$("#bank").show();
				}
		});
	
		$("#order_form").validate({
			  rules: {
				address: {
				  required: true,
				  maxlength: 200
				}
			  }
		});
		$("#create_form").validate();
		
		
		$("[id^=formatcheck]").click(function(){
			var checked_status = this.checked;
			var ordertype = $("#order_type:checked").val();
			if(checked_status){
				if (ordertype != "Bankruptcy"){
					$("#comments").addClass('required');
				} else {
					$("#comments2").addClass('required');
				}
			} else {
				if (ordertype != "Bankruptcy"){
					$("#comments").removeClass('required');
				} else {
					$("#comments2").removeClass('required');
				}
				$("input[id^=formatcheck]:checked").each(function(){
					checked_status	= this.checked;
					if (checked_status){
						if (ordertype != "Bankruptcy"){
							$("#comments").addClass('required');
						} else {
							$("#comments2").addClass('required');
						}
					}
				});
			}
		
		});
		
	//Table sorting
	$("#order-table").tablesorter(
		{sortList:[[2,0],[2,0]], widgets: ['zebra'], headers:{4: {sorter: false}}}); 
	$("#main-order-table").tablesorter(
		{sortList:[[5,1],[5,1]], widgets: ['zebra'], headers:{0: {sorter: false},7:{sorter: false}}}); 
	
	
	$("#compid").change(function(){
				var compid = $(this).val();	
				if (compid != 0){
				$.getJSON("index.php/order/getSingleComp",{compid: compid, ajax: 'true'}, function(j){
					//Add Counties to county drop down	
					//option += '<option value="' + j[i].userid + '">' + j[i].unitno + '</option>';
					$("#title_comp").val(j.compname);
					$("#comp_code").val(j.compcode);
					$("#county").val(j.countyname);
					$("#phone").val(j.phone);
					$("#fax").val(j.fax);
					$("#email").val(j.email);
				 },'json');
				}//end if
		})
	.change();
	
	//Order History
	
	$("[class*='addcode']").live("click", function(){
			var textarea = '<input type="text" name="compcode" id="compcode" value="' + $(this).html() + '" />';
			var button = '<input type="button" value="save" class="saveButton" /> <input type="button" value="cancel" class="cancelButton"/>';
			$(this).removeClass('addcode');
			$(this).addClass('codeEdit');
			$(this).empty();
			$(this).append(textarea + button);
	});
	
	
	$("[class^='cancelButton']").live("click", function(){
			var revert = $(this).parent().children(':first-child').val();
			$(this).parent().removeClass('codeEdit');
			$(this).parent().addClass('addcode');
			$(this).parent().html(revert);				   
	});
	
	$("[class^='saveButton']").live("click", function(){
			var compcode = $(this).parent().children(':first-child').val();
			var orderid = $(this).parent().siblings(':last').children(':first-child').html();
			var parent = $(this);
			var values = "compcode="+compcode+"&orderid="+orderid;
			$.ajax({  
						type: "POST",  
						url: "/order/insertCompcode" ,
						dataType: "json",
						data: values,
						success: function(data) {
								if (data.success){
									parent.parent().removeClass('codeEdit');
									parent.parent().addClass('addcode');
									parent.parent().html(compcode);	
								}
							}
						});
	});		
	
	
	$("[class*='completed']").live("click", function(){
			var parent = $(this).parent().parent();		
			var parentClass = $(this).parent().parent().attr("class");
			var orderid = $(this).parent().siblings(':last').children(':first-child').html();
			parent.addClass('selected');
			$(this).removeClass('completed');
			$(this).addClass('activeCompleted');
			parent.after("<tr class="+parentClass+"><td colspan='8'>Date Completed: <input type='text' name='date' class='date' value=''><input type='button' value='save' class='saveDate' /><input type='hidden' name='orderid' class='dateorderid' value='"+orderid+"'></td></tr>");
			$("[class*='date']").datepicker();
	});
	
	$("[class*='saveDate']").live("click", function(){
			var parent = $(this).parent().parent();	 
			var dateComplete = $(this).siblings(':first').val();
			var orderid = $(this).siblings(':last').val();
			var values = "date_completed="+dateComplete+"&orderid="+orderid;
			$.ajax({  
						type: "POST",  
						url: "/order/dateCompleted" ,
						dataType: "json",
						data: values,
						success: function(data) {
								if (data.success){
									//parent.siblings('').children(':first-child').removeClass('activeCompleted');
									//parent.siblings('').children(':first-child').addClass('completed');
									parent.siblings('.selected').hide();
									parent.hide();
								}
							}
						});
													
	});
	
	
	$("[class*='datePrinted']").live("click", function(){
			var textarea = '<input type="text" name="date_printed" class="date_printed" value="' + $(this).html() + '" />';
			var button = '<input type="button" value="save" class="savePrintDate" /> <input type="button" value="cancel" class="cPrintDate"/>';
			$(this).removeClass('datePrinted');
			$(this).addClass('editDateP');
			$(this).empty();
			$(this).append(textarea + button);
			$("[class*='date_printed']").datepicker();
	});
	
	
	$("[class^='cPrintDate']").live("click", function(){
			var revert = $(this).parent().children(':first-child').val();
			$(this).parent().removeClass('editDateP');
			$(this).parent().addClass('datePrinted');
			$(this).parent().html(revert);				   
	});
	
	$("[class^='savePrintDate']").live("click", function(){
			var date_printed = $(this).parent().children(':first-child').val();
			var orderid = $(this).parent().siblings(':last').children(':first-child').html();
			var parent = $(this);
			var values = "date_printed="+date_printed+"&orderid="+orderid;
			$.ajax({  
						type: "POST",  
						url: "/order/datePrinted" ,
						dataType: "json",
						data: values,
						success: function(data) {
								if (data.success){
									parent.parent().removeClass('editDateP');
									parent.parent().addClass('datePrinted');
									parent.parent().html(date_printed);	
								}
							}
						});
	});	
	
	
});

