$(document).ready(function() {					   
						   
	//验证					   
	$("#qos").validate({
		errorContainer: $("div.err")
	});
	
	//分页
	$('#qlist').pager('dt',{
		prevText: 'Prev',
		nextText: 'Next',
		linkWrap: '<span></span>'					  
	})	
	
	//乱序
	if($("#mix").val()==1){
		lkt_n=$('.mix').length;							//个数
		for(i=0;i<=lkt_n-1;i++){						//遍历
			sub_n=$('.mix:eq('+i+') .sub').length		//子DIV数
			
			arr=new Array();
			arrhtml=new Array();
			for(j=0;j<sub_n;j++){						//排除第一个div,从1到sub_n-1的数字产生一个随机序列数组
				arr.push(j);
				arrhtml.push($('.mix:eq('+i+') .sub:eq('+j+')').html());
			}
			arr=randomOrder(arr);
			for(k=0;k<arr.length;k++){					//乱序	
				$('.mix:eq('+i+') .sub:eq('+k+')').html(arrhtml[arr[k]]);
			}		
		}
	}	
		
	//密码和配对码
	if($("#needpass").val()=='1'){
		$.blockUI({ message: $('#pass'), css: { width: '275px' } }); 
	}else{
		couple();	
	}
	
	//预览或过期状态下不提交
	//过期****************
	$('#qos').bind('submit',function(){
		if($("#debug").val()==1){
			$.growlUI('', '处在预览状态下不能提交');
			return false;	
		}
	})
	
	if($("#debug").val()!=1){
		$.post("/key/key_hit.php",{id:$("#id").val()});
	}

	//置顶
	if($('#pagetop').val()!=0 && $('#pagetop').val()!=null){
		$('.item:eq(0)').addClass('top');		
		$('#nav>span>a').bind('click',function(){
			scrollTo(0, 0);										   
			if($('dt:eq(0)').css('display')=='none'){
				$('dt:eq(0)').show()
				$('dt:eq(0)>.item:gt(0)').hide()	
				$("#showintro").hide();				
			}else{
				$('dt:eq(0)').show()
				$('dt:eq(0)>.item:gt(0)').show()
				$("#showintro").show();
			}						
		});		
	}else{
		$('#nav>span>a').bind('click',function(){		
			scrollTo(0, 0);
			if($('dt:eq(0)').css('display')=='none'){
				$("#showintro").hide();
			}else{
				$("#showintro").show();
			}
		})		
	}
});

//AJAX检查密码
function checkpass(){
	$.post('/key/key_checkpass.php',{userpass:$("#getpass").val(),qosid:$("#id").val()},function(getr){
		if(getr=='ok'){
			$.unblockUI(); 
			couple();
		}else{
			if(!confirm('你输入的密码错误,是否重新输入?\n点击确定再次输入,点击取消返回网站首页')){
				location.href="/index.php";	
			}else{
				$('#getpass').select();	
			}
		}		  
	})	
}
//AJAX检查配对码
function checkcouplecode(){
	$.unblockUI(); 
	$('#couplecode').val($("#getcouplecode").val());
	/*
	$.post('/key/key_checkcouplecode.php',{couplepass:$("#getcouplecode").val(),qosid:$("#id").val()},function(getr){
		if(getr=='ok'){
			$.unblockUI(); 
			$('#couplecode').val($("#getcouplecode").val());
		}else{
			if(!confirm('没有找到匹配的答卷,是否重新输入?\n点击确定再次输入,点击取消返回网站首页')){
				location.href="/index.php";	
			}else{
				$('#getcouplecode').select();	
			}
		}		  
	})
	*/
}


//配对
function couple(){
	//配对问卷
	switch($('#couple').val()){
		case '0':	
			//普通问卷
			break;
		case '1':
			//主问卷			
			code=Math.floor(Math.random()*100)+''+getDT();
			$('#couplecode').val(code);
			break;
		case '2':
			//辅问卷
			$.blockUI({ message: $('#docouple'), css: { width: '275px' } });			
			break;
		default:
			break;
	}	
}

//时间
function getDT(){
   d = new Date();
   s='';
   s += (d.getMinutes() + 1)
   s += d.getSeconds();
   return(s);                                // 返回日期。
}

//乱序
function randomOrder (targetArray){
    var arrayLength = targetArray.length
    //
    //先创建一个正常顺序的数组
    var tempArray1 = new Array();

    for (var i = 0; i < arrayLength; i ++){
        tempArray1 [i] = i
    }
    //
    //再根据上一个数组创建一个随机乱序的数组
    var tempArray2 = new Array();

    for (var i = 0; i < arrayLength; i ++){
        //从正常顺序数组中随机抽出元素
        tempArray2 [i] = tempArray1.splice (Math.floor (Math.random () * tempArray1.length) , 1)
    }
    //
    //最后创建一个临时数组存储 根据上一个乱序的数组从targetArray中取得数据
    var tempArray3 = new Array();

    for (var i = 0; i < arrayLength; i ++){
        tempArray3 [i] = targetArray [tempArray2 [i]]
    }
    //
    //返回最后得出的数组
    return tempArray3
}

//其它
function toggleOther(e,id){
	if($(e).attr('checked')){
		$('#other'+id).show();	
	}	
}


