// JavaScript Document
if (Drupal.jsEnabled) {
	$(document).ready(function(){
                $("a[href$='.pdf']").attr('target','_blank');

		/* initialize vars and form fields */
		var q1 = i = j = '';
		/* reset form fields on load */
		$("#dynosearch-form select option:lt(1)").attr("selected","selected");
		/* change in q1 select menu updates remaining (q2,q3) select menus */
		$("#edit-c1").change(function(){
			q1 = this.value;
			$("option","#edit-c2, #edit-c3").remove();
			if(q1=='') {
				// update fields with default values
				$("#edit-c2-wrapper label, #edit-c3-wrapper label").text("Select Dyno Type");
				$("#edit-c2, #edit-c3").prepend($('<option value=""> - Select Dyno Type - </option>'));
				$("#edit-c2, #edit-c3, #edit-dynosubmit").attr('disabled','disabled');
				return;
			}
			var skip_select = '';
			if (q1 == 'Chassis Dynamometer') {
	  		$("div#edit-c3-wrapper").hide();
				skip_select = true;
	  	}
			else {
				$("div#edit-c3-wrapper").show();
				skip_select = false;
			}

		
			var len = widg1[q1].optgroups.length;
			for(i=0; i<len; i++){
				$("#edit-"+widg1[q1].optgroups[i].question.qid+"-wrapper label").text(widg1[q1].optgroups[i].question.name);
				$("#edit-"+widg1[q1].optgroups[i].question.qid+" option").remove();
				$("#edit-"+widg1[q1].optgroups[i].question.qid).prepend($('<option value=""> - Select ' + widg1[q1].optgroups[i].question.name + '</option>'));
				for(j=0; j<widg1[q1].optgroups[i].question.values.length; j++){
          var partname = (widg1[q1].optgroups[i].question.values[j].value != '') ? widg1[q1].optgroups[i].question.values[j].value : widg1[q1].optgroups[i].question.values[j].name;
					$("#edit-"+widg1[q1].optgroups[i].question.qid).append(
						$('<option>').val(partname).text(widg1[q1].optgroups[i].question.values[j].name)
					);
				}
			}
				$("#dynosearch-form select, #edit-dynosubmit").removeAttr('disabled');
			$("#dynosearch-form select, #edit-dynosubmit").attr('enabled','enabled');
			$("option:eq(1)","#edit-c2" + (skip_select ? "" : ",#edit-c3")).attr("selected","selected");
		});
		/* update form fields on display page */
		var parts = get_form_parts('c1','c2','c3');
		if(parts['c1'] == '') return;
		$("#edit-c1").trigger("change");
		for(var p = 1; p <= 3; p++) {
			if(parts['c'+p] != '') {
        //parts['c'+p] = parts['c'+p].replace(/\+/, " ");
        //alert(parts['c'+p]);
        $("#edit-c" + p + " option[value='" + parts['c'+p] + "']").attr("selected","selected");
				if(p == 1) $("#edit-c1").trigger('change');
			}
		}
	});
}

/**
 * @function get_form_parts retrieve finite list of GET params
 * @param any number of params mapping to the keys in the request string
 * @return associative array of the parameter(s) requested
 */
function get_form_parts() {
	var ar = Array();
	var parts = get_url_parts(ar);
	for(var i = 0; i < arguments.length; i++ ) {
		ar[arguments[i]] = unescape(parts[arguments[i]]).replace(/\+/g, " ").replace(/\%26/, "&");
	}
	return ar;
}
/**
 * @function get_url_parts get array of query string parameters
 * @return associative array of query string parameters
 */
function get_url_parts() {
	var searchString = document.location.search;
	var ar = Array();
	// strip off the leading '?'
	searchString = searchString.substring(1);
	var nvPairs = searchString.split("&");
	for (i = 0; i < nvPairs.length; i++)
	{
		var nvPair = nvPairs[i].split("=");
		var name = nvPair[0];
		var value = nvPair[1];
		ar[name] = value;
	}
	return ar;
}
