// survey system javascript start @by mr

function ajax_basic() {
 	var ajaxRequest;
    needconfirmation=true;
	try{
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Your browser broke!");
				return false;
			}
		}
	}
	return ajaxRequest;
}


	function gonext(id,itemtype) {
		if(itemtype=='select') {
			var e = document.getElementById(id);
			var selval = e.options[e.selectedIndex].value;
			if(e.selectedIndex>0) {
						jQuery(function($) {
							$("#qbutton").find("img").attr({src:"/poll/img/next-active.jpg"});
							$('#qbutton').click(function() {
									nextid = document.getElementById('thisid').value;
  									movetonext(nextid,id,selval);
								});
							});

					} else {
						jQuery(function($) {
							$("#qbutton").find("img").attr({src:"/poll/img/next-inactive.jpg"});
							$('#qbutton').unbind('click');
							});
					}
				}
				
		if(itemtype=='radio') {
			var e = document.questionnaire.elements[id];
			var radioLength = e.length;
				for(var i = 0; i < radioLength; i++) {
						if(e[i].checked) selval = e[i].value;
						}
						
			if(selval!='undefined') {
						jQuery(function($) {
							$("#qbutton").find("img").attr({src:"/poll/img/next-active.jpg"});
							$('#qbutton').click(function() {
									nextid = document.getElementById('thisid').value;
  									movetonext(nextid,id,selval);
								});
							});
			} else {
						jQuery(function($) {
							$("#qbutton").find("img").attr({src:"/poll/img/next-inactive.jpg"});
							$('#qbutton').unbind('click');
							});
			}
		}
		
		
		if(itemtype=='text') {
			var must = document.getElementById(id+'must').value;
			var selval = document.getElementById(id).value;
				if(!selval && must=='yes') {
						jQuery(function($) {
						$("#qbutton").find("img").attr({src:"/poll/img/next-inactive.jpg"});
						$('#qbutton').unbind('click');
							});
					} else {
						jQuery(function($) {
				$("#qbutton").find("img").attr({src:"/poll/img/next-active.jpg"});
							$('#qbutton').click(function() {
									nextid = document.getElementById('thisid').value;
  									movetonext(nextid,id,selval);
								});
							});
					}
				}
		
		if(itemtype=='checkbox') {
			var e = document.questionnaire.elements[id];
			var maxval = document.getElementById(id+'max').value;
			var chkboxLength = e.length;
			itemschecked = 0;
				for(var i = 0; i < chkboxLength; i++) {
					if(e[i].checked) 
							{
							itemschecked = itemschecked+1;
							if(maxval>0 && itemschecked>maxval) {
									e[i].checked = false;
									alert('You can only choose '+maxval+' options!');
									}
							}
						}
			if(itemschecked>0) {
						jQuery(function($) {
				$("#qbutton").find("img").attr({src:"/poll/img/next-active.jpg"});
				$('#qbutton').click(function() {
					var selval = getcheckedboxes(id); 
					nextid = document.getElementById('thisid').value;
  					movetonext(nextid,id,selval);
								});
							});
				} else {
						jQuery(function($) {
				$("#qbutton").find("img").attr({src:"/poll/img/next-inactive.jpg"});
				$('#qbutton').unbind('click');
							});
				}
		}
		
	}
	
	function getcheckedboxes(id) {
			var e = document.questionnaire.elements[id];
			var chkboxLength = e.length;
			var selval;
				for(var i = 0; i < chkboxLength; i++) {
					if(e[i].checked) 
							{
							if(e[i].value) selval = selval ? selval + '|' + e[i].value : e[i].value;
							}
						}
			return selval;
	}
	
	
	function movetonext(nextid,id,selval){
		var ajaxRequest = ajax_basic();
						jQuery(function($) {
		$('#qbutton').unbind('click');
							});
		ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState != 4){
			var ajaxDisplay = document.getElementById('qitem');
			ajaxDisplay.innerHTML = '<center><img src="/poll/img/indicator.gif" width="16" height="16"></center>';
	
		}
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('qitem');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
			var matchresult = ajaxRequest.responseText.match("mustitem");
 					if(matchresult == "mustitem" && nextid!=-1){
						document.getElementById('qbutton').innerHTML = '<img src="/poll/img/next-inactive.jpg" width="101" height="32">';
						//$("#qbutton").find("img").attr({src:"/poll/img/next-inactive.jpg"});
						//$("#qbutton").find("a").remove();
					} 
					if(matchresult != "mustitem" && nextid!=-1){
						document.getElementById('qbutton').innerHTML = '<img src="/poll/img/next-active.jpg" width="101" height="32">';
						//$("#qbutton").find("img").attr({src:"/poll/img/next-active.jpg"});
						//$("#qbutton").find("a").remove();
						jQuery(function($) {
							$('#qbutton').click(function() {
									nextid = document.getElementById('thisid').value;
									id = document.getElementById('itemname').value;
									selval = document.getElementById(id).value;
  									movetonext(nextid,id,selval);
								});
							$("#qbutton").css("cursor", "pointer");
							});

					}
					if(nextid==-1) {
						jQuery(function($) {
							$("#qbutton").find("img").attr({src:"/poll/img/button-start.jpg"});
								$('#qbutton').click(function() {
									movetonext(0,"","");
								});
								$("#qbutton").css("cursor", "pointer");
							});
					}
			var matchresult2 = ajaxRequest.responseText.match("polldone");
			if(matchresult2 == 'polldone')  {
					document.getElementById('qreset').innerHTML = ""; 
					document.getElementById('qbutton').innerHTML = ""; 
			}
			//$('#qitem').animate({ opacity: 1.00 }, 1000, function() { });
		}
	}
	var queryString = "?nextid=" + nextid + "&id="+id+"&selval="+selval;
	ajaxRequest.open("GET", "/poll/_getnext.php" + queryString, true);
	ajaxRequest.send(null); 
		
		
	}
