isOldNS = ((navigator.appName=='Netscape') && (navigator.appVersion < "5"))

var maxCorrect=0;
var userCorrect=0;
var m2=0;
var s=0;

function theFeedback(which) {
// returns the appropriate feedback
	maxCorrect = eval(which).possCorrect;
	userCorrect = eval(which).totalCorrect;

	if (maxCorrect == userCorrect) {
		returnValue = LB_correctFeedback; 
		disable(which); 
	} else {
		if (userCorrect == 0) {
			returnValue = LB_incorrectFeedback; 
		} else {
			if (userCorrect==-100) {
				returnValue = LB_solutionFeedback; 
			} else {
				returnValue = LB_partialFeedback;
			}
		}
	}
	
	return "<span class='questionAndFeedbackText'>"+replaceXandY(returnValue)+"</span>";
}



function MM_setTextOfLayer(objName,x,newText) {
// sets the text of the feedback
	obj=MM_findObj(objName);
	if (obj!=null) {
		with (obj) {
			if (isOldNS) {
				document.write(unescape(newText));
 				document.close();
			} else {
				innerHTML = unescape(newText);
			}
		}
	}
}

function showFeedback(which) {
// shows the feedback
	MM_setTextOfLayer('Feedback','',theFeedback(which))
	MM_showHideLayers('Feedback','','show');
	showticks(which,'show');
}

function LB_judgeIt(what) {
	eval(what).judge(); 
	showFeedback(what);
}

function LB_resetIt(what) {
	if (LB_resetIncorrectOnly) {
		showticks(what,'hide');
	} else {
//		MM_resetInt(what,'reset','')
//		for (x=1;x<=countDrags();x++) {
//			MM_showHideLayers(what+'Tick'+x,'','hide');
//		}
		tmp=document.location.href;
		document.location=tmp;
	}

	MM_showHideLayers('Feedback','','hide');
	MM_initInteractions();
}

function replaceXandY(theText) {
	numCorrect = G01.totalCorrect;
	posCorrect = G01.possCorrect;

	if (theText.indexOf(" X ") != -1) {
		xLoc = theText.indexOf(" X ")+1;
		theText = theText.substring(0, xLoc) + numCorrect + theText.substring(xLoc+1);
	} 

	if (theText.indexOf(" Y ") != -1) {
		yLoc = theText.indexOf(" Y ")+1;
		theText = theText.substring(0, yLoc) + posCorrect + theText.substring(yLoc+1);		
	}

	return theText;
}


function showticks(which,whatToDo) {
	for (x=1;x<=countDrags();x++)  { 
		for (y=1;y<=countDrags();y++) {
			o1=MM_findObj(which+"Drag"+x)
			o2=MM_findObj(which+"Target"+y);

			if ((o1!=null) && (o2!=null)) {
				dropKO=eval(which+".e[\'Target"+y+"']");
				dragKO=eval(which+".e[\'Drag"+x+"']");
			        if (eval(which+".e['Drag"+x+"'].c['Target"+y+"']").selected == true) { 
					tickname=which+"Tick"+x;
					dragname=which+"Drag"+x;
// get the position of it
					theLayer1=MM_findObj(tickname)
					theLayer2=MM_findObj(dragname)

					if (isOldNS) {
						theLayer1.top = parseInt(theLayer2.top);
						theLayer1.left = parseInt(theLayer2.left)+LB_tickOffset;
					} else {
	      					theLayer1.style.top=parseInt(theLayer2.style.top);
    						theLayer1.style.left=parseInt(theLayer2.style.left)+LB_tickOffset;
				  	}

					textOfTick="<img src='images/";
					if (eval(which+".e['Drag"+x+"'].c['Target"+y+"']").isCorrect) {
						textOfTick+="yes"
					} else {
						textOfTick+="no"
						if (whatToDo=='hide') {
							dragKO.reset();
						}
					}	
					textOfTick+=".gif'>";
					MM_setTextOfLayer(tickname,"",textOfTick);
			      		MM_showHideLayers(tickname,"",whatToDo);
				}
			}
		}
	}
}



function isCorrect(t,d) {
	var testString='|'+LB_targets[t].join('|')+'|'
	if (testString.indexOf('Drag'+d)==-1) {
		return false 
	}
	return true
}

function countDrags() {
	lastOne='Drag0'
	for (var x=1;x<=LB_targets.length;x++) {
		for (var y=0;y<LB_targets[x-1].length;y++) {
			if (LB_targets[x-1][y]>lastOne) lastOne=LB_targets[x-1][y];
		}
	}
	return Math.floor(lastOne.substring(4));

}

// Portions Copyright 1998,1999 Macromedia, Inc. All rights reserved.
function newG01() {
    	G01 = new MM_interaction('G01',0,1,1,null,0,0,0,'','','m','',0);
	var x=0;
	var y=0;
	for (x=1;x<=countDrags();x++) {
	    G01.add('drag','Drag'+x,null,1,0,1,0);
	}
	for (x=1;x<=LB_targets.length;x++) {
	    G01.add('drag','Target'+x,null,0,1,1,0);
	}
	for (x=1;x<=LB_targets.length;x++) {
		for (var y=1;y<=countDrags();y++) {
			x_offSet=LB_dragSpacing; x_offset="";
			y_offSet=LB_dragSpacing // +((LB_dragHeight+LB_dragSpacing)*y); y_offset="";
			G01.add('dragTarg','Drag'+y,'Target'+x,isCorrect(x-1,y),0,LB_marginForError,'','',false);
		}
	}
    	G01.init();
}

if (window.newG01 == null) window.newG01 = newG01;
if (!window.MM_initIntFns) window.MM_initIntFns = ''; window.MM_initIntFns += 'newG01();';
MM_initInteractions();

function disable(which) {
        MM_setIntProps(which+'.setDisabled(true)');
	MM_findObj(which+'judge').disabled=true;
	MM_findObj(which+'solution').disabled=true;
//	MM_findObj(which+'reset').disabled=true;
}

function LB_solveIt(which) {
	if (!eval(which)) return;
	var x=0;
	var y=0;
	disable(which);
	for (x=0;x<LB_targets.length;x++) {
		for (var y=0;y<LB_targets[x].length;y++) {
			tar='Target'+Math.floor(x+1);
			dra=LB_targets[x][y];

			dropGO = eval(which+".e['"+tar+"']");
			dragGO = eval(which+".e['"+dra+"']");

			// Get drop position
			dropGO.getPosition();
	
			// Reposition drag
			dragGO.snapTo(dropGO._posX, dropGO._posY+LB_dragSpacing+((LB_dragHeight+LB_dragSpacing)*y));
		}
	}
	for (x=1;x<=countDrags();x++) {
		MM_showHideLayers(which+'Tick'+x,'','hide');
	}

	eval(which).totalCorrect=-100;
	MM_setTextOfLayer('Feedback','',theFeedback(which))
	MM_showHideLayers('Feedback','','show');
}
