/*
cmix_controls.js
John Oatis
8/20/2007
General functions for controlling input behavior in surveys.
*/

/*This top section is for miscellaneous functions for 
 * different surevy questions
 * 
 */
function dk_radio(b, clientcode){
	/*
	 * This function unchecks all radio buttons when a don't know box is checked
	 */
	var radios = document.getElementsByTagName('INPUT');
	if (b.checked == true){
		for (i = 0; i < radios.length; i++){
			cc = radios[i].getAttribute('clientcode');
			if(cc == clientcode){
				radios[i].checked = false;
			}
		}
	}
}

function dk_radio_row(b, spss_name){
	/*
	 * This function unchecks all radio buttons when a row level 
	 * don't know box is checked
	 */
	var radios = document.getElementsByTagName('INPUT');
	if (b.checked == true){
		for (i = 0; i < radios.length; i++){
			cc = radios[i].getAttribute('row');
			if(cc == spss_name){
				radios[i].checked = false;
			}
		}
	}
}




/* 
 * Slider functions 
 */

/* slider_clickMin, sets an input(f) to a value(m)
   constructed for row_DK checkboxes when using sliders.
   often m = $min
*/
function slider_clickMin(s, f, m){
	if (s.disabled != true){
		f = document.getElementById(f);
		if(f.value == ''){
			f.value = m;
		}
	}
}

/* slider_toggleHandle, sets visibility of a slider's handle
   depending on the input value for the slider. code, is the 
   spss name of the choice.
*/
function slider_toggleHandle(code){
	handle = (code + "_handle");
	
    h = document.getElementById(handle);
	v = document.getElementById(code);
 	if (isNaN(v.value)){
		h.style.visibility = 'hidden';
	}
	else {
		h.style.visibility = 'visible';
	}
}


/* slider_DK, if a DK box is checked. Slider is disabled/enabled and 
   the handle visiblity is set. s is the slider id and c is the spss 
   name for that choice.
*/
function slider_DK(s,c){
	handle = (c + "_handle");
 	if (s.disabled != true){
		s.disabled = true;
		handle = document.getElementById(handle);
		handle.style.visibility = 'hidden';
	}
	else {
		s.disabled = false;
		handle = document.getElementById('Q1_handle');
		handle.style.visibility = 'visible';
		s.setValue('');
	}
	x = document.getElementById(c);
	x.value = '';
}


/* 
	THE FOLLOWING FUNCTIONS ARE FOR DRAG AND DROPS
*/

/* 
	cmix_drop1 appends dragged choices as a child node
*/
function cmix_drop1(choice, header, spss_stem){
	// Get this target's header val
	val = header.getAttribute('hval');
	choiceVal = choice.getAttribute('choiceval');
	// construct hidden var name
	varbl = spss_stem + choiceVal;
	// construct hidden variable object
	hvar = document.getElementById(varbl);
	// Assign header val to hidden variable
	hvar.value = val;
	choice.style.position = 'relative';
	cmix_dropSnapChoice(choice, header);
	// Set order of choices dropped on header
	order_var = header.getAttribute('order_var');
	

}

/* 
	cmix_drop2 appends dragged choices as an offset stack
*/
function cmix_drop2(choice, header, spss_stem, x, y, startXoffset, startYoffset){
	// Get this target's header val
	val = header.getAttribute('hval');
	choiceVal = choice.getAttribute('choiceval');
	
	// construct hidden var name
	varbl = spss_stem + choiceVal;
	// construct hidden variable object
	hvar = document.getElementById(varbl);
	// Assign header val to hidden variable
	hvar.value = val;
	cmix_dropStackChoice(choice, header, x, y);
	if ( (startXoffset + startYoffset) > 0){
		bdy = document.getElementById('choice_bin');
		cmix_reorderArea(bdy,startXoffset,startYoffset);
	}
}

function cmix_dragClearVal(spss_name, choice_id){
	//varbl = spss_name;
	hvar = document.getElementById(spss_name);
	hvar.value = '';
	var choice = document.getElementById(choice_id);
	choice.setAttribute("hval", "");
	choice.style.zIndex = "20000";
}

function cmix_dropSnapChoice(choice, header){
	choice.style.left = choice.style.top = 0;
	header.appendChild(choice);
}

function cmix_dropStackChoice(choice, header, x, y){
    var children = 0;
	if(header.hasChildNodes()){
	    children++;
		var node = header.firstChild;
		while(node.nextSibling){
		    if(node.nextSibling.tagName == "DIV"){
				children++;
			}
			node = node.nextSibling;
		}
	}
	var xOffset = (children * x) + 'px';
	var yOffset = (children * y) + 'px';
	var zOffset = 100 + header.childNodes.length;
	var hval = header.getAttribute('hval');
	choice.style.position = 'absolute';
	choice.style.left = xOffset;
	choice.style.top = yOffset;
	choice.setAttribute('hval', hval);
	header.appendChild(choice);
}

function cmix_showVar(draggable, evnt, id){
	var e = document.getElementById(id);
	var hval = e.getAttribute('hval');
	if (hval == ""){
		draggable.revert = true;
	}
	else{draggable.revert = false;}
}

function cmix_revert(drag_id, start_area, bin_x_offset, bin_y_offset){
	var obj = document.getElementById(drag_id);
	var hval = obj.getAttribute('hval');
	if (hval == ""){
		start_area = document.getElementById(start_area);
		cmix_reorderArea(start_area,bin_x_offset,bin_y_offset);
	}
}

function cmix_dragClearOrderVar(choice){
	choice = document.getElementById(choice);
	ord_var = choice.getAttribute('order_var');

	if(ord_var){
		ord_var = document.getElementById(ord_var);
		cval = choice.getAttribute('choiceval');
		var val = "-" + cval;
		var re = new RegExp(val);
		temp = ord_var.value.split(val);
		ord_var.value = temp[0] + temp[1];
	}
}

function cmix_dragRemoveFromHeader(choice, xOffset, yOffset){
	child = document.getElementById(choice);
	bdy = document.getElementById('choice_bin');
	mother = child.parentNode;
	if(mother.getAttribute('hval')){
		mother.removeChild(child);
		cmix_reorderArea(mother, xOffset, yOffset);
		bdy.appendChild(child);
	}
}

function cmix_reorderArea(mother, xOffset, yOffset){
	var x = 0;
	var y = 0;
	for (c = 0; c < mother.childNodes.length; c++){
		var child = mother.childNodes[c];
		
		if (child.style){
			child.style.left = (x + "px");
			child.style.top = (y + "px");
			x += parseInt(xOffset);
			y += parseInt(yOffset);
		}
		
	}
}

// as of 0324 page 1 of a survey tries to collect info 
// about a respondents machine.
function cmix_page1_scan(){
	var f = document.getElementById('cmixform');
	if (screen.width && screen.height){f.res.value = (screen.width + 'x' + screen.height);}
	if (navigator.appName){f.app.value = navigator.appName;}
	if (navigator.appVersion){f.appv.value = navigator.appVersion;}
	if (navigator.cookieEnabled){f.cook.value = 1;}
	if (navigator.userLanguage){f.lang.value = navigator.userLanguage;}
	else if (navigator.language){f.lang.value = navigator.language;}
	if (window.location.search){f.orgQstring.value = window.location.search.substring(1);}
	if (navigator.plugins.length > 0){
		for(p = 0; p < navigator.plugins.length; p++){
			if(navigator.plugins[p].name){f.plugins.value += navigator.plugins[p].name;}
		}
	}
}




