/** 
* 对Date的扩展，将 Date 转化为指定格式的String 
* 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符 
* 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
* eg: 
* (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2007-07-02 08:09:04.423 
* (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2007-03-10 二 20:09:04 
* (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2007-03-10 周二 08:09:04 
* (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2007-03-10 星期二 08:09:04 
* (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2007-7-2 8:9:4.18 
*/ 
Date.prototype.format = function(fmt) {  
    var o = {  
        "M+": this.getMonth() + 1,  
        //月份  
        "d+": this.getDate(),  
        //日  
        "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12,  
        //小时  
        "H+": this.getHours(),  
        //小时  
        "m+": this.getMinutes(),  
        //分  
        "s+": this.getSeconds(),  
        //秒  
        "q+": Math.floor((this.getMonth() + 3) / 3),  
        //季度  
        "S": this.getMilliseconds() //毫秒  
    };  
    var week = {  
        "0": "\u65e5",  
        "1": "\u4e00",  
        "2": "\u4e8c",  
        "3": "\u4e09",  
        "4": "\u56db",  
        "5": "\u4e94",  
        "6": "\u516d"  
    };  
    if (/(y+)/.test(fmt)) {  
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
    }  
    if (/(E+)/.test(fmt)) {  
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f": "\u5468") : "") + week[this.getDay() + ""]);  
    }  
    for (var k in o) {  
        if (new RegExp("(" + k + ")").test(fmt)) {  
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
        }  
    }  
    return fmt;  
}
function $(obj){return document.getElementById(obj);}
function trim(ui){
	var notValid=/(^\s)|(\s$)/;
	while(notValid.test(ui)){
		ui=ui.replace(notValid,"");
	}
	return ui;
} 

/*start---------index.php-----------*/
var tmpdiv=document.createElement('DIV');
function searchrep(qkeystr){
	if(qkeystr!==''){
		var taget_obj = $('reportlist');
		myajax = new DedeAjax(taget_obj,false,false,'','','');
		var url = "/ajax.php?action=searchreport&id="+qkeystr;
		myajax.SendGet2(url);
	}
}
var olddivbg2='';
function showendtime(obj,time,show){
	var newt = new Date(time*1000);
	if(show){
		if(obj.style.backgroundColor!='#9966CC') olddivbg2=obj.style.backgroundColor;
		obj.style.backgroundColor='#9966CC';
		obj.style.color='#FFFFFF';
		tmpdiv.id = 'floatdiv_a';
		tmpdiv.style.zIndex = 1000;
		tmpdiv.innerHTML='End : '+newt.format("yyyy-MM-dd HH:mm");
		tmpdiv.style.display='block';
		document.body.appendChild(tmpdiv);		
		tmpdiv.style.left = getleftPos(obj) + 'px';
		tmpdiv.style.top = getTopPos(obj) + 'px';				
	}else{
		tmpdiv.parentNode.removeChild(tmpdiv);
		obj.style.backgroundColor='#FFFFFF';
		obj.style.color='#444444';	
		obj.style.backgroundColor=olddivbg2;		
	}	
}
var olddivbg='';
function showjobrecord(obj,divobj,show){
	oth=$(divobj);
	if(oth.innerHTML=='') return;
	if(show){
		if(obj.style.backgroundColor!='#9966CC') olddivbg=obj.style.backgroundColor;
		obj.style.backgroundColor='#9966CC';
		obj.style.color='#FFFFFF';
		oth.style.zIndex = 1000;
		oth.style.display='block';
		oth.style.left = getleftPos(obj) + 'px';
		oth.style.top = getTopPos(obj) + 'px';				
	}else{
		oth.style.display='none';
		//obj.style.backgroundColor='#FFFFFF';
		obj.style.backgroundColor=olddivbg;
		obj.style.color='#444444';		
	}	
}
function getTopPos(inputObj){
  var returnValue = inputObj.offsetTop + inputObj.offsetHeight;
  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetTop;
  return returnValue;
}

function getleftPos(inputObj){
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;
  return returnValue;
}
/*end-------index.php---------*/

/*start-----report_add.php----------*/
var Sorting=0;
var Cutting=0;
var Reporting=0;
var Testing=0;
function checkintvalt(obj,recal,showerr){
	var ret=false;
	if(obj.value!==''){
		var re = new RegExp("^[0-9]{1,}\:([0-9]{2})$","ig");
		var arr = re.exec(obj.value);
		if(arr==null){
			if(showerr) alert('Unavailable time interval,Please enter format:"hh:mm"\n Such as 02:25 -> 2 Hours and 25 Minutes.');
			obj.focus();
		}else{
			if(parseInt(RegExp.$1)>59){
				if(showerr) alert('Unavailable time interval,Please enter format:"hh:mm"\n Such as 02:25 -> 2 Hours and 25 Minutes.');
				obj.focus();
			}else{
				ret=true;
			}
		}
		if(ret==false){
			obj.style.backgroundColor='#FF6600';
			$(obj.id+'_di').style.color='#FF6600';	
			$(obj.id+'_di').style.fontWeight='bold';						
			$(obj.id+'_di').innerHTML='Unavailable time interval,Please enter format:"hh:mm"\n Such as 02:25';
		}else{
			obj.style.backgroundColor='#FFFFFF';
			$(obj.id+'_di').style.color='#FFFFFF';
			$(obj.id+'_di').style.fontWeight='normal';			
			$(obj.id+'_di').innerHTML='(Interval Time -> Hour:Minute )';
		}	
	}
	if(ret && recal){
		recaltime();
	}
	return ret;
} 

function intval_str_int(str){
	ret=0;
	if(str!==''){
		var re = new RegExp("([0-9]{1,})\:([0-9]{2})","ig");
		var arr = re.exec(str);
		if(arr!==null){
			ret=parseInt(RegExp.$1)*3600+parseInt(RegExp.$2)*60;
		}
	}
	return ret;
}

function recaltime(){

	Sorting=intval_str_int($('n_sorting').value);
	Sorting_p=parseInt($('n_sorting_p').value);	
	Cutting=intval_str_int($('n_cutting').value);
	Cutting_p=parseInt($('n_cutting_p').value);	
	Testing=intval_str_int($('n_testing').value);
	Testing_p=parseInt($('n_testing_p').value);	
	Reporting=intval_str_int($('n_reporting').value);
	Reporting_p=parseInt($('n_reporting_p').value);
	plustime('n_targetdate',Sorting+Cutting+Testing+Reporting+Sorting_p+Cutting_p+Testing_p+Reporting_p);
}

function get_h_i(int){
	var h=Math.floor(int/3600);
	var m=Math.floor((int % 3600)/60);
	if(m<10 && m>=0){
		return h+':'+'0'+m;
	}else{
		return h+':'+m;	
	}
}
function checkmulti(jobno){
	if(jobno!==''){
		var taget_obj = $('jobno_i');
		myajax = new DedeAjax(taget_obj,false,false,'','','');
		var url = "/ajax.php?action=checkmulti&id="+jobno;
		myajax.SendGet2(url);
	}
}

function plustime(objid,time){
	var obj=$(objid);
	var nlabin=$('n_labin');	
	var newt = new Date(Date.parse(nlabin.value.replace(/-/g,"/")));
	newt.setTime(get_worktime(newt.getTime(),time*1000,worktime));
	//newt.setTime(newt.getTime()+(time)*1000);
	obj.value=newt.format("yyyy-MM-dd HH:mm");
	$(objid+'_v').innerHTML=newt.format("yyyy-MM-dd HH:mm");	
}

//获取工作时间内的时间
function get_worktime(labin,time,worktime){
	//$worktime='9:00,12:30,13:30,18:00';
	noplus=true;
	var num=0;
	var re = new RegExp("^([0-9]+:[0-9]+),([0-9]+:[0-9]+),([0-9]+:[0-9]+),([0-9]+:[0-9]+)","ig");
	var arr = re.exec(worktime);
	if(arr!==null){
		am_start=RegExp.$1;
		am_end=RegExp.$2;
		pm_start=RegExp.$3;
		pm_end=RegExp.$4;
	}else{
		alert('Error Worktime!');
	}

	while(time>0){
		num++;
		if(num>1000){alert('Multi Error!');return;}
		var newt = new Date(labin);		
		var newt2= new Date(Date.parse(newt.format("yyyy/MM/dd")+' '+am_start+':00'));
		now_am_start=newt2.getTime();
		
		var newt2= new Date(Date.parse(newt.format("yyyy/MM/dd")+' '+am_end+':00'));
		now_am_end=newt2.getTime();
		
		var newt2= new Date(Date.parse(newt.format("yyyy/MM/dd")+' '+pm_start+':00'));
		now_pm_start=newt2.getTime();
		
		var newt2= new Date(Date.parse(newt.format("yyyy/MM/dd")+' '+pm_end+':00'));
		now_pm_end=newt2.getTime();						
		
		//AM
		if(labin<now_am_start){
			labin=now_am_start;
		}
		if(labin>=now_am_start && labin<=now_am_end){
			if((labin+time)<=now_am_end){
				labin=labin+time;
				time=0;
			}else{
				time=time-(now_am_end-labin);			
				labin=now_pm_start;
			}
		}
		if(labin>now_am_end && labin<now_pm_start){
			labin=now_pm_start;
		}

		//PM
		if(labin>=now_pm_start && labin<=now_pm_end){
			if((labin+time)<=now_pm_end){
				labin=labin+time;
				time=0;
			}else{
				time=time-(now_pm_end-labin);
			
				var newt5 = new Date(labin+24*3600*1000);				
				var newt6= new Date(Date.parse(newt5.format("yyyy/MM/dd")+' '+am_start+':00'));
				labin=newt6.getTime();						
				noplus=false;
			}
		}
		if(labin>now_pm_end && noplus){
			var newt5 = new Date(labin+24*3600*1000);				
			var newt6= new Date(Date.parse(newt5.format("yyyy/MM/dd")+' '+am_start+':00'));
			labin=newt6.getTime();	
		}

	}
	return labin;
}

function changetime(obj){
	Sorting=0;Cutting=0;Reporting=0;Testing=0;
	var tmpstr=obj.value;
	var re = new RegExp("^([0-9]+),([0-9]+),([0-9]+),([0-9]+)","ig");
	var arr = re.exec(tmpstr);
	if(arr!==null){
		Sorting=parseInt(RegExp.$1);
		Cutting=parseInt(RegExp.$2);
		Testing=parseInt(RegExp.$3);
		Reporting=parseInt(RegExp.$4);
		
		$('n_sorting').value=get_h_i(Sorting);
		$('n_cutting').value=get_h_i(Cutting);
		$('n_testing').value=get_h_i(Testing);
		$('n_reporting').value=get_h_i(Reporting);
		
		recaltime();
		
	}else{
		alert('Error Interval time data!');
	}
}

function check_check(checkname){
	var flag;
	var checks = document.getElementsByName(checkname);
	for(var i=0;i<checks.length;i++){
		if(checks[i].checked==true){
			flag = true;
			return true;
			break;
		}
	}
	if(!flag){
		alert("Please select Lab Group!");
		return false;
	}
}

function checkaddrep(){
	var jobno=$('n_jobno');
	var reqservice=$('n_reqservice');

	if(trim(jobno.value)==''){
		alert("Please input Job No.");
		jobno.focus();
		return false;
	}

	if(!check_check('n_labgroup[]')) return false;

	if(trim(reqservice.value)==''){
		alert("Please select Required service");
		reqservice.focus();
		return false;
	}
	
	return true;
}

function checkeditrep(){
	yz=/^[0-9]+$/;
	if(!yz.test($('n_sorting_p').value)){
		alert("Sorting suspend time format invalid!");
		$('n_sorting_p').focus();
		return false;		
	}
	
	if(!yz.test($('n_cutting_p').value)){
		alert("Cutting suspend time format invalid!");
		$('n_cutting_p').focus();
		return false;		
	}
	
	if(!yz.test($('n_testing_p').value)){
		alert("Testing suspend time format invalid!");
		$('n_testing_p').focus();
		return false;		
	}
	
	if(!yz.test($('n_reporting_p').value)){
		alert("Reporting suspend time format invalid!");
		$('n_reporting_p').focus();
		return false;		
	}	

	return true;
}

function checksustimec(obj){
	yz=/^[0-9]+$/;
	if(yz.test(obj.value)){
		recaltime();
	}else{
		alert("Suspend time format invalid!");
		obj.focus();
	}
}
/*end---------report_add.php--------*/

/*start-------report_do.php-------*/
function enablededit(obj,oth,oldtime){
	var nowt=new Date();
	$(oth).disabled=!obj.checked;
	$(oth+'_sb').disabled=!obj.checked;
	if($(oth).disabled){
		$(oth+'_sb').value='DONE';
		$(oth).style.backgroundColor='#E5E5E5';
		$(oth).value=oldtime;
	}else{
		$(oth).value=nowt.format("yyyy-MM-dd HH:mm");
		$(oth+'_sb').value='DO JOB';
		$(oth).style.backgroundColor='#FFFFFF';		
	}
}

function trim(ui){
	var notValid=/(^\s)|(\s$)/;
	while(notValid.test(ui)){
		ui=ui.replace(notValid,"");
	}
	return ui;
} 
function deletereport(id){
	if(confirm("Delete this report,really?")){
		window.open('/portal.php?action=deletereport&reportid='+id);
	}
}
function checkdojobtime(obj){
	if(suspended){alert('Report suspended! Can\'t do it!');return false;}
	obj=$(obj);
	yz=/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$/;
	if(!yz.test(obj.value)){
		alert('Error datetime format!');
		obj.focus();
		return false;
	}
	return true;
}
