/**
 * selectCombo for jQuery
 *
 * Copyright (c) 2007 Shelane Enos
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Initial base logic from Remy Sharp's blog: http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/
 *
 * @example  $('#myselect').selectCombo('myurltoprocess.lasso?additionalparamtoserverifnecessary=myparam', '#mytargetselect', {hidetarget: false});
 *
 * Option: hidetarget - Allows you to override the default hide behavior if set to false.  Default true will hide your target select and its label until and option from your source select is selected.  Use this if your target select is not empty when the page loads and its values correspond to your default selected of your source select.
 *
 * Parameter sent to server is q
 *
 * Expected server response is JSON in this format: [{oV: 'myfirstvalue', oT: 'myfirsttext'}, {oV: 'mysecondvalue', oT: 'mysecondtext'}]
 *
 */
 (function($){
 	$.fn.resetform = function(url,pt){
 		return this.each(function(){
 			//console.log($(this).val());
 			$(this).click(function(){
 				p1selected = 0;
				p2selected = 0;
				p3selected = 0;
 				$("#" + pt + "2").fadeOut(500);
 				$("label[@for='" + pt + "2']").fadeOut(500);
 				$("#" + pt + "3").fadeIn(500);
 				$("label[@for='" + pt + "3']").fadeIn(500);
 				$("#submit" + pt).fadeOut(500);
 				$("#reset"+pt).fadeOut(500);

				$.getJSON(url,{p1s: 0,p2s: 0,p3s: 0,type: type,target: pt+'1',start: start}, function(j){
					var options = '';
 					for (var i = 0; i < j.length; i++) {
 						if (j[i].oV == '-1'){
	 						/*options += '<option disabled="disabled" value="' + j[i].oV + '">' + j[i].oT + '</option>';*/
	 						options += '<option value="' + j[i].oV + '">' + j[i].oT + '</option>';
 						}else{
							options += '<option value="' + j[i].oV + '"' + j[i].oS + '>' + j[i].oT + '</option>';
 						}
 					}
 					$("#"+pt+"1").html(options);
 					$("option:first", "#"+pt+"1").attr("selected","selected");
				});//end JSON

				$.getJSON(url,{p1s: "",p2s: "",p3s: "",type: type,target: pt+'3',start: start}, function(j){
					var options = '';
 					for (var i = 0; i < j.length; i++) {
 						if (j[i].oV == '-1'){
	 						/*options += '<option disabled="disabled" value="' + j[i].oV + '">' + j[i].oT + '</option>';*/
	 						options += '<option value="' + j[i].oV + '">' + j[i].oT + '</option>';
 						}else{
							options += '<option value="' + j[i].oV + '"' + j[i].oS + '>' + j[i].oT + '</option>';
 						}
 					}
 					$("#"+pt+"3").html(options);
 					$("option:first", "#"+pt+"3").attr("selected","selected");
				});//end JSON

 				return false;
 			});//end change fn

 		});//end return for each
 	}
 })(jQuery);
  (function($){
 	$.fn.selectCombo3 = function(url){
 		return this.each(function(){
 			//console.log($(this).val());
 			$(this).change(function(){
 				p2selected = $(this).val();
 				if (p2selected != 0){
	 				$("#submitp").fadeIn(500);
					$("#p3").fadeOut(500);
					$("label[@for='p3']").fadeOut(500);
 				}else{
 					$("#submitp").fadeOut(500);
 				}
 			});//end change fn

 		});//end return for each
 	}
 })(jQuery);
 (function($){
 	$.fn.selectCombo4 = function(url){
 		return this.each(function(){
 			//console.log($(this).val());
 			$(this).change(function(){
 				p2selected = $(this).val();
 				if (p2selected != 0){
	 				$("#submitt").fadeIn(500);
	 				$("#t3").fadeOut(500);
					$("label[@for='t3']").fadeOut(500);
 				}else{
 					$("#submitt").fadeOut(500);
 				}
 			});//end change fn

 		});//end return for each
 	}
 })(jQuery);
 (function($){
 	$.fn.selectCombo2 = function(url, target, settings){//, options

 		var defaults = {hidetarget: false};
 		$.extend(defaults, settings);
 		return this.each(function(){
 			//console.log($(this).val());
 			var targetlabel = target.replace(/#/, '');
 			targetlabel = "label[@for='" + targetlabel + "']";

 			if($(this).val() == '' && defaults.hidetarget){
 				$(targetlabel).hide();
 				$(target).hide();
 				var targetOption = $("option:first", target);
 			}

 			$(this).change(function(){

 				// set the current selected
 				if (this.id == "p1" || this.id == "t1"){
 					p1selected = ($(this).val());
 					if (p1selected == -1){
 						p1selected = "";
 					}
 					submit = 0;
 				}
 				if (this.id == "p2" || this.id == "t2"){
 					p2selected = ($(this).val());
 					if (p2selected == -1){
 						p2selected = "";
 					}
 				}
 				if (this.id == "p3" || this.id == "t3"){
 					p3selected = ($(this).val());
 					if (p3selected == -1){
 						p3selected = "";
 					}
 				}

 				query = $(this).val();

 				$.getJSON(url,{p1s: p1selected,p2s: p2selected,p3s: p3selected,type: type,target: target.replace(/#/, ''),start: start}, function(j){
 				if (j[0].oU == ''){
 					var options = '';
 					var selid = '';
 					var last='';
 					for (var i = 0; i < j.length; i++) {

 						last = last || j[i].oU

 						if (j[i].oS != ''){
 							selid = i;
 						}
 						if (j[i].oV == '-1'){
 							if ( j[i].oS != ''){
 								if (target.replace(/#/, '') == "p1" || target.replace(/#/, '') == "t1"){
 									p1selected = '';
 								}
 								if (target.replace(/#/, '') == "p2" || target.replace(/#/, '') == "t2"){
 									p2selected = '';
 								}
 								if (target.replace(/#/, '') == "p3" || target.replace(/#/, '') == "t3"){
 									p3selected = '';
 									submit = 0;
 								}
 							}
 							/*options += '<option disabled="disabled" value="' + j[i].oV + '" ' + j[i].oS + ' >' + j[i].oT + '</option>';*/
 							options += '<option value="' + j[i].oV + '" ' + j[i].oS + ' >' + j[i].oT + '</option>';
 						}else{
							options += '<option value="' + j[i].oV + '"' + j[i].oS + '>' + j[i].oT + '</option>';
 						}
 					}
 					if ((p2selected == '') && (submit == 0)){
 						$("#submitp").fadeOut(500);
 						$("#submitt").fadeOut(500);
 					}
 					$(target).html(options);
 					$("option:eq("+selid+")", target).attr("selected","selected");
// 					$(targetlabel).show();
					if (p1selected || p2selected || p3selected){
						if (p2selected == '0' && (target == '#p3' || target == '#t3') ){
	 						$(targetlabel).fadeIn(500);
		 					$(target).fadeIn(500);
							if (target == '#p3'){
								if (p1selected == '0' &&  p3selected == '0'){
									$("#p2").fadeOut(500);
 									$("label[@for='p2']").fadeOut(500);
 									$("#submitp").fadeIn(500);
								}
							}else{
								if (p1selected == '0' && p3selected == '0'){
									$("#t2").fadeOut(500);
	 								$("label[@for='t2']").fadeOut(500);
	 								$("#submitt").fadeIn(500);
								}
							}
						}
						if ((p1selected != '0' || p3selected != '0')  && (target == '#p2' || target == '#t2') ){
							$(targetlabel).fadeIn(500);
		 					$(target).fadeIn(500);
						}
					}
					if (p1selected == '0' && p2selected == '0' && p3selected == '0'){
						if (target == '#p1' || target == '#p2' || target == '#p3'  ){
							$("#p2").fadeOut(500);
 							$("label[@for='p2']").fadeOut(500);
 							$("#submitp").fadeOut(500);
						}else{
							$("#t2").fadeOut(500);
 							$("label[@for='t2']").fadeOut(500);
 							$("#submitt").fadeIn(500);
						}
					}

 				}else{
 					submit = 1;
 					if (type == 0){
 						$("#submitp").fadeIn(500);
 						$("#p2").fadeOut(500);
 						$("label[@for='p2']").fadeOut(500);
 					}
 					if (type == 1){
 						$("#submitt").fadeIn(500);
 						$("#t2").fadeOut(500);
 						$("label[@for='t2']").fadeOut(500);
 					}
 				}
 				});//end JSON
 				if (type == 0){
	 				if (!p1selected && !p2selected && !p3selected){
	 					$("#resetp").fadeOut(500);
	 					$("#p2").fadeOut(500);
 						$("label[@for='p2']").fadeOut(500);
 					}else{
 						$("#resetp").fadeIn(500);
 					}
 				}else{
	 				if (!p1selected && !p2selected && !p3selected){
	 					$("#resett").fadeOut(500);
	 					$("#t2").fadeOut(500);
 						$("label[@for='t2']").fadeOut(500);
 					}else{
 						$("#resett").fadeIn(500);
 					}
 				}
 			});//end change fn

 		});//end return for each
 	}
})(jQuery);
(function($){
 	$.fn.displaythis = function(url){
 		return this.each(function(){
 			//console.log($(this).val());
 			$(this).click(function(){
			if ((url == "tissue" && type == "0") || (url == "paper" && type == "1")){
				if ($("#paper").is(":hidden")){
		                    	$("#paper").toggle(500);
                		        $("#tissue").toggle(1000);
	                	}else{
        		            	$("#paper").toggle(1000);
             				$("#tissue").toggle(500);
	                	}
 				p1selected = "";
				p2selected = "";
				p3selected = "";
				if (type == 0){
					type = 1;
				}else{
					type= 0;
				}
			}
 			});//end change fn

 		});//end return for each
 	}
})(jQuery);



	$(document).ready(function() {
		
		// Preload all rollovers
		$("#resetp").each(function() {
			// Set the original src
			rollsrc = $(this).attr("src");
			rollON = rollsrc.replace(/.gif$/ig,"_hl.gif");
			$("<img>").attr("src", rollON);
		});
		
		// Navigation rollovers
		$("#resetp").mouseover(function(){
			imgsrc = $(this).attr("src");
			matches = imgsrc.match(/_hl/);
			
			// don't do the rollover if state is already ON
			if (!matches) {
			imgsrcON = imgsrc.replace(/.gif$/ig,"_hl.gif"); // strip off extension
			$(this).attr("src", imgsrcON);
			}
			
		});
		$("#resetp").mouseout(function(){
			$(this).attr("src", imgsrc);
		});
		
		// Preload all rollovers
		$("#resett").each(function() {
			// Set the original src
			rollsrc = $(this).attr("src");
			rollON = rollsrc.replace(/.gif$/ig,"_hl.gif");
			$("<img>").attr("src", rollON);
		});
		
		// Navigation rollovers
		$("#resett").mouseover(function(){
			imgsrc = $(this).attr("src");
			matches = imgsrc.match(/_hl/);
			
			// don't do the rollover if state is already ON
			if (!matches) {
			imgsrcON = imgsrc.replace(/.gif$/ig,"_hl.gif"); // strip off extension
			$(this).attr("src", imgsrcON);
			}
			
		});
		$("#resett").mouseout(function(){
			$(this).attr("src", imgsrc);
		});
	
		// Preload all rollovers
		$("#submitp").each(function() {
			// Set the original src
			rollsrc = $(this).attr("src");
			rollON = rollsrc.replace(/.gif$/ig,"_hl.gif");
			$("<img>").attr("src", rollON);
		});
		
		// Navigation rollovers
		$("#submitp").mouseover(function(){
			imgsrc = $(this).attr("src");
			matches = imgsrc.match(/_hl/);
			
			// don't do the rollover if state is already ON
			if (!matches) {
			imgsrcON = imgsrc.replace(/.gif$/ig,"_hl.gif"); // strip off extension
			$(this).attr("src", imgsrcON);
			}
			
		});
		$("#submitp").mouseout(function(){
			$(this).attr("src", imgsrc);
		});
		// Preload all rollovers
		$("#submitt").each(function() {
			// Set the original src
			rollsrc = $(this).attr("src");
			rollON = rollsrc.replace(/.gif$/ig,"_hl.gif");
			$("<img>").attr("src", rollON);
		});
		
		// Navigation rollovers
		$("#submitt").mouseover(function(){
			imgsrc = $(this).attr("src");
			matches = imgsrc.match(/_hl/);
			
			// don't do the rollover if state is already ON
			if (!matches) {
			imgsrcON = imgsrc.replace(/.gif$/ig,"_hl.gif"); // strip off extension
			$(this).attr("src", imgsrcON);
			}
			
		});
		$("#submitt").mouseout(function(){
			$(this).attr("src", imgsrc);
		});
	});
