/**
 * 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(pt);
		$(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.selectCombo5 = function(url){
	return this.each(function(){
		//console.log($(this).val());
		$(this).change(function(){
		p2selected = $(this).val();
		if (p2selected != 0){
			$("#submitw").fadeIn(500);
			$("#w3").fadeOut(500);
			$("label[for='w3']").fadeOut(500);
		}else{
			$("#submitw").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" || this.id == "w1"){
				p1selected = ($(this).val());
				if (p1selected == -1){
					p1selected = "";
				}
				submit = 0;
			}
			if (this.id == "p2" || this.id == "t2" || this.id == "w2"){
				p2selected = ($(this).val());
				if (p2selected == -1){
					p2selected = "";
				}
			}
			if (this.id == "p3" || this.id == "t3" || this.id == "w3"){
				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){
//					console.log(target);
//					console.log(type);
//					console.log(j);
//					console.log(j[0].oU);
					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" || target.replace(/#/, '') == "w1"){
									p1selected = '';
								}
								if (target.replace(/#/, '') == "p2" || target.replace(/#/, '') == "t2" || target.replace(/#/, '') == "w2"){
									p2selected = '';
								}
								if (target.replace(/#/, '') == "p3" || target.replace(/#/, '') == "t3" || target.replace(/#/, '') == "w3"){
									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);
						$("#submitw").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' || target == '#w3') ){
							$(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(target == '#w3'){
								if (p1selected == '0' && p3selected == '0'){
									$("#w2").fadeOut(500);
									$("label[for='w2']").fadeOut(500);
									$("#submitw").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' || target == '#w2') ){
							$(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 if(target == '#w1' || target == '#w2' || target == '#w3'){
							$("#w2").fadeOut(500);
							$("label[for='w2']").fadeOut(500);
							$("#submitw").fadeIn(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);
						}
						if (type == 3){
							$("#submitw").fadeIn(500);
							$("#w2").fadeOut(500);
							$("label[for='w2']").fadeOut(500);
						}
					}
			});//end JSON
//			console.log(type);
			if (type == 0){
				if (!p1selected && !p2selected && !p3selected){
					$("#resetp").fadeOut(500);
					$("#p2").fadeOut(500);
					$("label[for='p2']").fadeOut(500);
				}else{
					$("#resetp").fadeIn(500);
				}
			} else if (type == 3){
				if (!p1selected && !p2selected && !p3selected){
					$("#resetw").fadeOut(500);
					$("#w2").fadeOut(500);
					$("label[for='w2']").fadeOut(500);
				}else{
					$("#resetw").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){
	// default value
	prevClicked = 'paper';
	return this.each(function(){
		$(this).click(function(){
		if ((url != prevClicked)){
			if (url =='paper') {
			type = 0;
			if(prevClicked == 'tissue') {
				$("#paper").toggle(500);
				$("#tissue").toggle(1000);
			} else if(prevClicked == 'wd') {
				$("#paper").toggle(500);
				$("#wd").toggle(1000);
			}
			} else if(url == 'tissue'){
			type = 1;
			if(prevClicked == 'paper') {
				$("#paper").toggle(1000);
				$("#tissue").toggle(500);
			} else if(prevClicked == 'wd') {
				$("#wd").toggle(1000);
				$("#tissue").toggle(500);
			}
			} else if(url == 'wd') {
			type = 3;
			if(prevClicked == 'paper') {
				$("#wd").toggle(500);
				$("#paper").toggle(1000);
			} else if(url == 'wd' && prevClicked == 'tissue') {
				$("#wd").toggle(500);
				$("#tissue").toggle(1000);
			}
			}

			p1selected = "";
			p2selected = "";
			p3selected = "";
		}

		prevClicked = url;
		});//end change fn

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



jQuery(document).ready(function() {

	// Preload all rollovers
	jQuery("#resetp").each(function() {
		// Set the original src
		rollsrc = jQuery(this).attr("src");
		rollON = rollsrc.replace(/.gif$/ig,"_hl.gif");
		jQuery("<img>").attr("src", rollON);
	});

	// Navigation rollovers
	jQuery("#resetp").mouseover(function(){
		imgsrc = jQuery(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
			jQuery(this).attr("src", imgsrcON);
		}

	});
	jQuery("#resetp").mouseout(function(){
		jQuery(this).attr("src", imgsrc);
	});

	// Preload all rollovers
	jQuery("#resett").each(function() {
		// Set the original src
		rollsrc = jQuery(this).attr("src");
		rollON = rollsrc.replace(/.gif$/ig,"_hl.gif");
		jQuery("<img>").attr("src", rollON);
	});

	// Navigation rollovers
	jQuery("#resett").mouseover(function(){
		imgsrc = jQuery(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
			jQuery(this).attr("src", imgsrcON);
		}

	});
	jQuery("#resett").mouseout(function(){
		jQuery(this).attr("src", imgsrc);
	});

	// Preload all rollovers
	jQuery("#resetw").each(function() {
		// Set the original src
		rollsrc = jQuery(this).attr("src");
		rollON = rollsrc.replace(/.gif$/ig,"_hl.gif");
		jQuery("<img>").attr("src", rollON);
	});

	// Navigation rollovers
	jQuery("#resetw").mouseover(function(){
		imgsrc = jQuery(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
			jQuery(this).attr("src", imgsrcON);
		}
	});
	jQuery("#resetw").mouseout(function(){
		jQuery(this).attr("src", imgsrc);
	});

	// Preload all rollovers
	jQuery("#submitp").each(function() {
		// Set the original src
		rollsrc = jQuery(this).attr("src");
		rollON = rollsrc.replace(/.gif$/ig,"_hl.gif");
		jQuery("<img>").attr("src", rollON);
	});

	// Navigation rollovers
	jQuery("#submitp").mouseover(function(){
		imgsrc = jQuery(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
			jQuery(this).attr("src", imgsrcON);
		}
	});
	jQuery("#submitp").mouseout(function(){
		jQuery(this).attr("src", imgsrc);
	});
	// Preload all rollovers
	jQuery("#submitt").each(function() {
		// Set the original src
		rollsrc = jQuery(this).attr("src");
		rollON = rollsrc.replace(/.gif$/ig,"_hl.gif");
		jQuery("<img>").attr("src", rollON);
	});

	// Navigation rollovers
	jQuery("#submitt").mouseover(function(){
		imgsrc = jQuery(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
			jQuery(this).attr("src", imgsrcON);
		}
	});
	jQuery("#submitt").mouseout(function(){
		jQuery(this).attr("src", imgsrc);
	});


	// Preload all rollovers
	jQuery("#submitw").each(function() {
		// Set the original src
		rollsrc = jQuery(this).attr("src");
		rollON = rollsrc.replace(/.gif$/ig,"_hl.gif");
		jQuery("<img>").attr("src", rollON);
	});

	// Navigation rollovers
	jQuery("#submitw").mouseover(function(){
		imgsrc = jQuery(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
			jQuery(this).attr("src", imgsrcON);
		}
	});

	jQuery("#submitw").mouseout(function(){
		jQuery(this).attr("src", imgsrc);
	});
});

