function addComment(commentId,sltObj)
{
	var temp = sltObj.options[sltObj.selectedIndex].text;
	commentId.value = temp;

	//window.alert(itemId.value);
}

function getEndUsers(slt,selectedSlt)
{
	var temp = slt.options[slt.selectedIndex].value;
    new Ajax.Request('/ssl-bin/sequencing/getEndUser.cgi',
        {
            method: 'post',
            parameters: 'agent='+temp,
            onSuccess: function setuser(ajaxRequest)
                {
                    var result = ajaxRequest.responseText;    //return text only
                    $(selectedSlt).options.length = 0;
					var segment = result.split(";");
					if (segment.length > 1){
						for(i = 0; i < segment.length-1; i++){
							var part = segment[i].split("|");
							var uId = part[0];
							var uName = part[1];
							$(selectedSlt).options.add(new Option(uName,uId));
						}
						for(i =0; i<segment.length-1;i++){
							selectedSlt.options[i].selected= true;
						}
					}else{
						$(selectedSlt).options.add(new Option("----","----"));
					}
                }
        }
    );
}


function change(from,to)
{
	var pos = from.selectedIndex;
	if (pos > -1){
		var usernameIndex = from.options[pos].value;
		var usernameValue = from.options[pos].text;
		
		from.options[pos] = null;
		
		//remove the first one
		if (to.options[0].text == "----"){
			to.options[0] = null;
		}
		//alert (usernameValue + "|" +usernameIndex);
		to.options.add(new Option(usernameValue,usernameIndex)); 
		
		// when empty 
		if (from.length == 0){
			from.options.add(new Option("----","----")); 
		}
	}
}


function setAction(from,to)
{
	from.disabled=true;
	to.disabled=false;
	
}

function selectAll(slt)
{
	for(i =0; i<slt.options.length ;i++){
		slt.options[i].selected= true;
	}	
}

function confirmDelOrder(mgrType){
	if (mgrType == 'seq'){
		alert ("Delete now allowed.");
		return false;
	}else{
		if (!confirm("Are you sure to delete this order?")){
			return false;		
		}else{
			return true;
		}
	}

}

function generateCaiwu(searchType)
{
	if (searchType == 'period')
	{
		var starts = document.getElementById('starts').value;
		var ends = document.getElementById('ends').value;		
		var che_finalCus = document.getElementById('che_finalCus').checked ;
		var selectGroupTime = document.getElementById('selectGroupTime').value ;
		window.location="excelForFinance.cgi?search=time&startTime="+starts+"&endTime="+ends+"&che_finalCus="+che_finalCus+"&selectGroupTime="+selectGroupTime;
	}
	else if (searchType == 'name')
	{
		var custName = document.getElementById('custNameValue').value;	
		window.location="excelForFinance.cgi?search=customer&"+"&custName="+custName;
	}
	else if (searchType == 'salesman')
	{
		var salesManObj = document.getElementById( 'salesman' ) ;
		var salesManName = salesManObj.options[salesManObj.selectedIndex].text ;
		var salesManStart = document.getElementById('manStart').value;
		var salesManEnd = document.getElementById('manEnd').value;		window.location="excelForFinance.cgi?search=salesman&startTime="+salesManStart+"&endTime="+salesManEnd+"&salesManName="+salesManName;
	}
	else if (searchType == 'agent')
	{	
		var agentId = document.getElementById( 'agentName' ).value ;
		var agentNameObj = document.getElementById( 'agentName' ) ;
		var agentName = agentNameObj.options[agentNameObj.selectedIndex].text ;
		var agentStart = document.getElementById('agentStart').value;
		var agentEnd = document.getElementById('agentEnd').value;		window.location="excelForFinance.cgi?search=agent&startTime="+agentStart+"&endTime="+agentEnd+"&agentId="+agentId+"&agentName="+agentName;
	}
	else if (searchType == 'operator')
	{	
		var operatorObj = document.getElementById( 'operator' ) ;
		var operatorName = operatorObj.options[operatorObj.selectedIndex].text ;
		var operatorStart = document.getElementById('operatorStart').value;
		var operatorEnd = document.getElementById('operatorEnd').value;		window.location="excelForFinance.cgi?search=operator&startTime="+operatorStart+"&endTime="+operatorEnd+"&operatorName="+operatorName;
	}
}

function confirmRepayComment(){
	if (!confirm("确认留言内容无误?提交后将不可更改!")){
		return false;
	}else{
		return true;
	}
}

function confirmUpdateCustType(agentId,repId){
	var toAgent = document.getElementById(agentId)[document.getElementById(agentId).selectedIndex].text;
	var salesman = document.getElementById(repId)[document.getElementById(repId).selectedIndex].text;
	if (toAgent){
		if (!confirm("确认更改此客户的代理商为 " + toAgent + " ?")){
			return false;
		}
	}
	if (salesman){
		if (!confirm("确认更改此客户的业务员为 " + salesman + " ?")){
			return false;
		}	
	}
	return true;
}

function confirmBatchUpdateOrder(){
	if (!confirm("确认以上输入无误并更新所有测序结果吗? ")){
		return false;
	}else{
		return true;
	}
}


//in seqencing front

function popItemComment(id) {
	window.open("https://www.jinsite.com.cn/ssl-bin/sequencingFront/message.cgi?op=item&itemId="+id,'new','top=20 ,left=20,height=250,width=380,toolbar=0,location=0,directories=0,menubar=0,scrollbars=1,resizable=yes,status=no');
}

function popOrderComment(id) {
	window.open("https://www.jinsite.com.cn/ssl-bin/sequencingFront/message.cgi?op=order&orderId="+id,'new','top=20 ,left=20,height=250,width=380,toolbar=0,location=0,directories=0,menubar=0,scrollbars=1,resizable=yes,status=no');
}


function checkIfEnter(){
    if (window.event.keyCode == 13){
    	//alert() ;
    }
}

function seqFrontLogin(){
	var email = document.getElementById('custEmail').value;
	var pwd = document.getElementById('custPwd').value;
	var warnSpan = document.getElementById('logInfoWarn'); 
	
	email.replace( /'/ , "" ) ; 
	
	if( email != "" && pwd != "")
	{
		new Ajax.Request('/ssl-bin/sequencingFront/seqFrontLogin.cgi',
        	{
        		method: 'post',
        		parameters: 'custEmail='+email+'&custPwd='+pwd,
			onLoaded: warnSpan.innerHTML = "<font color='blue'>登录中...</font>",
      onSuccess: function show(ajaxRequest)
			{
				var result = ajaxRequest.responseText;
				if (result == "no"){
					warnSpan.innerHTML = "<font color='red'>对不起,您的邮箱或密码错误</font>";
				}else if(result == "yes"){
					window.location.href="menu.html";
				}
			}
        	}
		);
	}else if(email == ""){
		warnSpan.innerHTML = "<font color='red'>请输入您的邮箱</font>";
	}else if(pwd == ""){
		warnSpan.innerHTML = "<font color='red'>请输入您的密码</font>";
	}
	return ;	
}

//change customer pwd 
var bPwdNow = false;
var bPwdNew = false;

function checkCurrentPwd(){ 
	var pwd = document.getElementById('currentPwd').value;
	var warnSpan = document.getElementById('warnNow');
	if (pwd.length != 6){
		warnSpan.innerHTML = "<font color='red'>长度不对</font>";
		 bPwdNow = false;
		return;
	}else{
		var methodName = "";
		if( document.getElementById("op").value == "changePwdSale" )
		{
			methodName = "checkPwdSale" ;
		}
		else
		{
			methodName = "checkPwd" ;
		}
		new Ajax.Request('/ssl-bin/sequencingFront/seqFrontLogin.cgi',
        	{
        		method: 'post',
        		parameters: 'op='+methodName+'&pwd='+pwd,
				onLoaded: warnSpan.innerHTML = "<font color='blue'>检查中...</font>",
        		onSuccess: function show(ajaxRequest)
				{
					var result = ajaxRequest.responseText;
					if (result == "no"){
						warnSpan.innerHTML = "<font color='red'>错误</font>";
						 bPwdNow = false;
					}else if(result == "yes"){
						warnSpan.innerHTML ="<font color='blue'>正确</font>";
						bPwdNow = true;
					}
				}
        	}
		);
	}
	return;
}



function checkNewPwd(){
	var pwd1 = document.getElementById('custPwd').value;
	var pwd2 = document.getElementById('reCustPwd').value;
	var warnSpan = document.getElementById('warnNew');
	if (pwd1 == "" || pwd2==""){
		warnSpan.innerHTML = "<font color='red'>新密码不能为空</font>";
		bPwdNew = false;
		return;
	}
	if (pwd1.length != 6 || pwd2.length !=6){
		warnSpan.innerHTML = "<font color='red'>新密码长度须为6位</font>";
		bPwdNew = false;
		return;
	}
	if (pwd1 != pwd2){
		warnSpan.innerHTML = "<font color='red'>新密码不匹配</font>";
		bPwdNew = false;
		return;
	}else{
		bPwdNew = true;
		if (bPwdNow == true && bPwdNew == true){
			var methodName = "";
			if( document.getElementById("op").value == "changePwdSale" )
			{
				methodName = "changePwdSale" ;
			}
			else
			{
				methodName = "changePwd" ;
			}
			new Ajax.Request('/ssl-bin/sequencingFront/seqFrontLogin.cgi',
			{
				method: 'post',
				parameters: 'op='+methodName+'&pwd='+pwd2,
				onLoaded: warnSpan.innerHTML = "<font color='blue'>更改中...</font>",
				onSuccess: function show(ajaxRequest)
				{
					var result = ajaxRequest.responseText;
					if (result == "yes"){
						warnSpan.innerHTML ="<font color='blue'>更改密码成功</font>";
						document.getElementById('custPwd').value = "";
						document.getElementById('reCustPwd').value = "";
						document.getElementById('currentPwd').value = "";
						document.getElementById('warnNow').innerHTML = "";
					}else{
						warnSpan.innerHTML ="<font color='red'>更改密码失败</font>";
					}
				}
			}
			);			
		}
	}
}


function resetPwd4Cust(cid){
	var warnSpan = document.getElementById('warnInfo');
	new Ajax.Request('/ssl-bin/sequencing/searchCustomer.cgi',
	{
		method: 'post',
		parameters: 'op=resetPwd&cid='+cid,
		onLoaded: warnSpan.innerHTML = "<font color='blue'>正在进行重设密码...</font>",
		onSuccess: function show(ajaxRequest)
		{
			var result = ajaxRequest.responseText;
			if (result == "yes"){
				warnSpan.innerHTML ="<font color='blue'>更改密码成功</font>";
				document.getElementById('custPwd').value = "";
				document.getElementById('reCustPwd').value = "";
				document.getElementById('currentPwd').value = "";
				document.getElementById('warnNow').innerHTML = "";
			}else{
				warnSpan.innerHTML ="<font color='red'>更改密码失败</font>";
			}
		}
	}
	);		
}