//Jquery URL Plugin
jQuery.url=function(){var segments={};var parsed={};var options={url:window.location,strictMode:false,key:["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],q:{name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},parser:{strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}};var parseUri=function(){str=decodeURI(options.url);var m=options.parser[options.strictMode?"strict":"loose"].exec(str);var uri={};var i=14;while(i--){uri[options.key[i]]=m[i]||""}uri[options.q.name]={};uri[options.key[12]].replace(options.q.parser,function($0,$1,$2){if($1){uri[options.q.name][$1]=$2}});return uri};var key=function(key){if(!parsed.length){setUp()}if(key=="base"){if(parsed.port!==null&&parsed.port!==""){return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/"}else{return parsed.protocol+"://"+parsed.host+"/"}}return(parsed[key]==="")?null:parsed[key]};var param=function(item){if(!parsed.length){setUp()}return(parsed.queryKey[item]===null)?null:parsed.queryKey[item]};var setUp=function(){parsed=parseUri();getSegments()};var getSegments=function(){var p=parsed.path;segments=[];segments=parsed.path.length==1?{}:(p.charAt(p.length-1)=="/"?p.substring(1,p.length-1):path=p.substring(1)).split("/")};return{setMode:function(mode){strictMode=mode=="strict"?true:false;return this},setUrl:function(newUri){options.url=newUri===undefined?window.location:newUri;setUp();return this},segment:function(pos){if(!parsed.length){setUp()}if(pos===undefined){return segments.length}return(segments[pos]===""||segments[pos]===undefined)?null:segments[pos]},attr:key,param:param}}();
$(function() {
		   
    $("#categories").accordion({
			autoHeight: false,
			collapsible: true
			
	});

    var categoryselectred = jQuery.url.param("category");

	if(categoryselectred !=undefined){
		try{
			$("#categories h3").each(function(index){
				if($(this).text() == "Categories" && $(this).hasClass("ui-state-default")){
					$("#categories").accordion('activate', index);
				}							  
			});
			
		}catch(e){ }
		
		/*try{
			//if($("#categories h3").eq(1).hasClass("ui-state-default")){
				$("#categories").accordion('activate', 1);
			//}
		}catch(e){ }	*/
	}
   
	
	//ajax start
	 $(".loading").ajaxStart(function(){
	   $(this).show();
	 });
	 $(".loading").ajaxStop(function(){
        	$(this).hide();
	 });
	
	//button hover
	$(".fg-button:not(.ui-state-disabled)")
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		)
		.mousedown(function(){
				$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
				if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
				else { $(this).addClass("ui-state-active"); }	
		})
		.mouseup(function(){
			if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
				$(this).removeClass("ui-state-active");
			}
	
	});
		
	//edit field
	$(".edittext, .edittextarea, .editdate, .editselect").hover(
		function(){
			$(this).addClass("ui-state-highlight"); 
			$(this).css({"padding":"1px 3px"});
			
		},
		function(){
			$(this).removeClass("ui-state-highlight");
			$(this).css({"padding":"1px 3px"});
		}
	).click(function(){$(this).css({"border":"none","padding":"0px"});});

	
	//add to report
	$report = $('#reportbox');
	$(".add_to_report").click(function(){
			var itemid = $(this).val();
			if($(this).is(':checked')){
			
				$.ajax({
					type: "POST",
					url: "php/ajax.php",
					data: "action=report&id="+itemid,
					success: function(msg){						
						
						$(msg).appendTo($report);
						showprintreport();
					}
				});
			}else{
				
				$.ajax({
					type: "POST",
					url: "php/ajax.php",
					data: "action=removereport&id="+itemid,
					success: function(msg){
						
						$("#reportbox #id_"+itemid).remove();
						showprintreport();
					}
				});
				
			}
			
	});
	
	$("#q").keyup(function(){
			$.ajax({
				type: "POST",
				url: "php/adminajax.php",
				data: "type="+$(this).next("#type").val()+"&action=search&q="+$(this).val(),
				success: function(msg){
					$("#content").html(msg);
				}
    		});					   
	});
	
	$("#qs").keyup(function(){
			$.ajax({
				type: "POST",
				url: "php/adminajax.php",
				data: "action=searchserial&qs="+$(this).val(),
				success: function(msg){
					$("#content").html(msg);
				}
    		});					   
	});
	
	$("#clientqs").keyup(function(){
			$.ajax({
				type: "POST",
				url: "php/ajax.php",
				data: "action=clientsearch&q="+$(this).val(),
				success: function(msg){
					$("#content").html(msg);
				}
    		});					   
	});
	
	//email report
	$(".emailreport").click(function(){
		$.ajax({
			type: "POST",
			url: "php/ajax.php",
			data: "action=emailreport",
			success: function(msg){
				$("#reportbox div").remove();
				$(".printreport").hide();
				$(".emailreport").hide();
				$(".add_to_report").each(function(){
						$(this).removeAttr("checked");								  
				});
				$("#sidebar .message").html(msg);
				$("#sidebar .message").fadeIn("fast");
				$("#sidebar .message").animate({opacity: 1.0}, 2000,function(){
        			$("#sidebar .message").fadeOut("slow");
    	 		});
			}
		});
		return false;
	});
	
	
	$('.password').keyup(function(){
		var passwordparent = $(this).parent('div').parent('form').attr('id');
		loadpassword(passwordparent);
	});
	//$('#sidecontent').css('top:60px','right:0px')
	$(window).bind('scroll', function() {
		var offset = $(this).scrollTop() + 60;
		$('#sidecontent').css('top', offset + "px");

		//$('#sidecontent').css('position', sbOffset > $(window).scrollTop() ? 'relative': 'absolute');
	});
	
	
	//delete user
	$('.delete').click(function(){
									var action = $(this).attr('href');
									//confirm dialog
									$('#dialog').dialog({
											bgiframe: true,
											resizable: false,
											height:180,
											modal: true,
											overlay: {
												backgroundColor: '#000',
												opacity: 0.5
											},
											buttons: {
												'Yes! Please delete': function() {
													$('#content').load(action);
													$(this).dialog('close');
													$(this).dialog('destory');
												},
												Cancel: function() {
													$(this).dialog('destroy');
												}
											},
											close: function(){
											$(this).dialog('destroy');	
											}
										});
									return false;
									
									
									
								});
	
	

});

function buttonhovers(){
	$(".fg-button:not(.ui-state-disabled)")
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		)
		.mousedown(function(){
				$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
				if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
				else { $(this).addClass("ui-state-active"); }	
		})
		.mouseup(function(){
			if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
				$(this).removeClass("ui-state-active");
			}
	
	});
}

function uploadimages(itemid, customerid){
	
	window.open("php/adminajax.php?action=upload&uid="+customerid+"&iid="+itemid,"uploadimagewindow","menubar=no,resizable=no,statusbar=no,width=360,height=200");
	
}


function removefromreport(itemid){
	$.ajax({
		type: "POST",
		url: "php/ajax.php",
		data: "action=removereport&id="+itemid,
		success: function(msg){
			$("#reportbox #id_"+itemid).remove();
			showprintreport();
		}
	});	
	
	$("body .add_to_report").each(function(){
		if($(this).val()==itemid){
			$(this).removeAttr("checked");
			showprintreport();
		}
		
	});
	
}

//check if report is empty and display print button
function showprintreport(){
	if($("#reportbox div").length > 0){
		$(".printreport").show();	
		$(".emailreport").show();
		try{
			$("#categories h3").each(function(index){
				if($(this).text() == "Reports" && $(this).hasClass("ui-state-default")){
					$("#categories").accordion('activate', index);
				}							  
			});
			
		}catch(e){ }
	}else{
		
		$.ajax({
			type: "POST",
			url: "php/ajax.php",
			data: "action=clearreport",
			success: function(msg){
				$(".printreport").hide();
				$(".emailreport").hide();
				
			}
		});
	}
	
}
//$(".fullreport").children("td").hide();
function showmore(divclass){ 
/*	$("."+divclass).parent("td").slideToggle('fast');*/
/*	$("."+divclass).parent("td").attr('collspan','9');
*/	$("."+divclass).slideToggle('fast');
	
	
}

function loadpassword(formid){
	var passwordparent = formid;
	var bpos = "";
	var perc = 0 ;
	var minperc = 0 ;
	$('#adduser .result').html(passwordStrength($('#adduser .password').val(),'')) ; 
		perc = passwordStrengthPercent($('#adduser .password').val(),'');
		bpos =" $('#adduser .colorbar').css( {backgroundPosition: \"0px -" ;
		bpos = bpos + perc + "px";
		bpos = bpos + "\" } );";
		bpos=bpos +" $('#adduser .colorbar').css( {width: \"" ;
		bpos = bpos + (perc * 2) + "px";
		bpos = bpos + "\" } );";
		eval(bpos);
		$('#adduser .percent').html(" " + perc  + "% ");	
}
function showRequest(formData, jqForm, options) { 
	var formid = jqForm.attr('id');
	var passperc = passwordStrengthPercent($('#'+formid+' .password').val(),'');
	if(passperc > 70){
		return true;	
	}else{
		$('#'+formid+' .message').html("<p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: 0.3em;'></span>Password not strong enough. Use at least two numbers and two special characters</p>");
		return false;
	} 
} 

function addnewitem(){
	$('select#category').selectmenu({ width: 200});
	$("#year").mask("9999");
	$("#purchasedate").mask("99-99-9999");
	//$("#purchasedate").datepicker({dateFormat: 'mm-dd-yyyy'});
	$("#appraisaldate").mask("99-99-9999");
	//$("#appraisaldate").datepicker({dateFormat: 'yy-mm-dd'});
	$(".addnew").unbind('click').click(function(){
		$("#additemform").toggle();										
													
	});
	
	
	
}

function createbackup(){
	$.ajax({
			type: "POST",
			url: "backup/backup.php",
			success: function(msg){
				$("#content").html(msg);				
			}
		});
}

function viewbackups(){
	$.ajax({
			type: "POST",
			url: "php/adminajax.php",
			data: "action=viewbackups",
			success: function(msg){
				$("#content").html(msg);				
			}
		});
}

function deletebackup(backupfile){
	$.ajax({
			type: "POST",
			url: "php/adminajax.php",
			data: "action=deletebackup&backup="+backupfile,
			success: function(msg){
				$("#content").html(msg);				
			}
		});
}


