﻿// -----------------------------------------------------------------------------------------------------------------
// const string definition 

var 
	conjStrInvaildEmail, conjStrStringLengthRange, conjStrInvalidNumber, conjStrNumberRange, 
	conjStrRequired, conjStrErrorList, conjStrSelectRequired, conjStrOtherRequired, 
	conjStrInvalidDate, conjStrInvalidYear, conjStrInvalidMonth, conjStrInvalidDay, 
	conjStrRecordConfirmDelete, conjStrInvaildZipCode

if( true ){

	conjStrInvaildEmail = '输入的Email地址无效'
	conjStrStringLengthRange = '输入的文字长度须在范围'
	conjStrInvalidNumber = '输入的数字无效'
	conjStrNumberRange = '输入的数字须在范围'
	conjStrRequired = '必须提供'
	conjStrErrorList = '(出错信息如下，请更正)'
	conjStrSelectRequired = '必须选择'
	conjStrOtherRequired = '其他 要求输入'
	conjStrInvalidFormat = '提供的格式无效'

	conjStrInvalidDate = '输入的日期无效'
	conjStrInvalidYear = '输入的年无效'
	conjStrInvalidMonth = '输入的月无效'
	conjStrInvalidDay = '输入的日无效'
	
	conjStrRecordConfirmDelete = '确认删除记录?'
	
	conjStrInvaildZipCode = '输入的邮编无效'
	conjStrInconsistent = '不一致'
}else{	

	conjStrInvaildEmail = 'must contain an e-mail address'
	conjStrStringLengthRange = 'must contain a string with length between'
	conjStrInvalidNumber = 'must contain a number'
	conjStrNumberRange = 'must contain a number between'
	conjStrRequired = 'is required'
	conjStrErrorList = 'The following error(s) occurred'
	conjStrSelectRequired = 'is required'
	conjStrOtherRequired = 'other is required'
	conjStrInvalidFormat = 'must contain valid format'

	conjStrInvalidDate = 'invalid date'
	conjStrInvalidYear = 'invalid year'
	conjStrInvalidMonth = 'invalid month'
	conjStrInvalidDay = 'invlid day'
	
	conjStrRecordConfirmDelete = 'Confirm to delete?'
	conjStrInvaildZipCode = 'must contain a zip code'
	conjStrInconsistent = 'inconsistent'
}

// -----------------------------------------------------------------------------------------------------------------
/* -- function definition

---- form validation on submitting ----
function MM_findObj(n, d)
function MM_validateForm()
function exMM_validateForm()
function exValidateRadioBox(fldName, fldDsr)
function exValidateRadioBox2(fldName, fldDsr, fldOtherName)
function exValidateCheckBox(fldName, fldDsr)
function exValidateCheckBox2(fldName, fldDsr, fldOtherName)
function exValidateDate(fldDate, fldDsr)
function validateDate(fldDate, fldDsr)

---- form confirmation on submitting ----
function confirmDeleteRecord()

---- form obj operation ----
function disableForm(strFormName)
function disableTextbox(fldName)
function enableTextbox(fldName)
function setTextBoxVal(fldName, fldValue)
function unselectListBox(fldName)
function selectListBox(fldName, fldValue)
function chkCheckBox(fldName, blnCheck)

---- form dt ----
function dtSelectDT(obj)
*/

// -----------------------------------------------------------------------------------------------------------------
// -- form validation on submitting

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  	var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
// 	var objReEmail = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
	var objReEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  	var objReZipCode = /^[0-9]{4,6}$/;
	for (i=0; i<(args.length-2); i+=3) { 
		test=args[i+2]; 
		val=MM_findObj(args[i]);
    		if (val) { 
    			nm=val.tagname; 
    			if ((val=trim(val.value))!="") {
      				if (test.indexOf('isEmail')!=-1) { 
      					if( !objReEmail.test(val))
						errors+='- '+nm+' '+conjStrInvaildEmail+'.<br>';
      				/*	p=val.indexOf('@');
        				if (p<1 || p==(val.length-1)) 
        					errors+='- '+nm+' '+conjStrInvaildEmail+'.<br>';*/
      				} 
      				else if (test.indexOf('isDate')!=-1) { 
      					errors+=validateDate(val, nm);
      				}
      				else if (test.indexOf('isZipCode')!=-1) { 
      					if( !objReZipCode.test(val))
						errors+='- '+nm+' '+conjStrInvaildZipCode+'.<br>';
      				} 
        			else if (test.indexOf('isStrLenInRange') != -1) { 
        				p=test.indexOf(':');
        				q= (test.charAt(0) == 'R')? 1:0;
          					min=test.substring(15+q,p); max=test.substring(p+1);
          					min = parseInt(min); max = parseInt(max); val = parseInt(val.length);
          					if (val<min || max<val) 
          						errors+='- '+nm+' '+conjStrStringLengthRange+' ['+min+', '+max+'].<br>';
    					}       					
      				else if (test!='R') {
        				if (isNaN(val)) 
        					errors+='- '+nm+' '+conjStrInvalidNumber+'.<br>';
        				if (test.indexOf('inRange') != -1) { 
        					p=test.indexOf(':');
          					min=test.substring(8,p); max=test.substring(p+1);
          					min = parseInt(min); max = parseInt(max); val = parseInt(val);
          					if (val<min || max<val) 
          						errors+='- '+nm+' '+conjStrNumberRange+' ['+min+', '+max+'].<br>';
    					} 
    				} 
    			} 
    			else if (test.charAt(0) == 'R') 
    				errors += '- '+nm+' '+conjStrRequired+'.<br>'; 
    		}
	} 
	if (errors) 
		divSysMsg.innerHTML = conjStrErrorList + '<br>' + errors
	//	alert(conjStrErrorList+':<br>'+errors); 
  	
  	document.MM_returnValue = (errors == '');
}

function exMM_validateForm() { //v4.0
  	var i,p,q,nm,test,num,min,max,errors='',args=exMM_validateForm.arguments;
//  var objReEmail = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
  	var objReEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  	var objReZipCode = /^[0-9]{4,6}$/;
	for (i=0; i<(args.length-2); i+=3) { 
		test=args[i+2]; 
		val=MM_findObj(args[i]);
    		if (val) { 
    			nm=val.tagname; 
    			if ((val=trim(val.value))!="") {
      				if (test.indexOf('isEmail')!=-1) { 
      					if( !objReEmail.test(val))
						errors+='- '+nm+' '+conjStrInvaildEmail+'.<br>';
      				/*	p=val.indexOf('@');
        				if (p<1 || p==(val.length-1)) 
        					errors+='- '+nm+' '+conjStrInvaildEmail+'.<br>';*/
      				} 
      				else if (test.indexOf('isDate')!=-1) { 
      					errors+=validateDate(val, nm);
      				}
      				else if (test.indexOf('isZipCode')!=-1) { 
      					if( !objReZipCode.test(val))
						errors+='- '+nm+' '+conjStrInvaildZipCode+'.<br>';
      				} 
        			else if (test.indexOf('isStrLenInRange') != -1) { 
        				p=test.indexOf(':');
        				q= (test.charAt(0) == 'R')? 1:0;
          					min=test.substring(15+q,p); max=test.substring(p+1);
          					min = parseInt(min); max = parseInt(max); val = parseInt(val.length);
          					if (val<min || max<val) 
          						errors+='- '+nm+' '+conjStrStringLengthRange+' ['+min+', '+max+'].<br>';
    					}       					
      				else if (test!='R') {
        				if (isNaN(val)) 
        					errors+='- '+nm+' '+conjStrInvalidNumber+'.<br>';
        				if (test.indexOf('inRange') != -1) { 
        					p=test.indexOf(':');
          					min=test.substring(8,p); max=test.substring(p+1);
          					min = parseInt(min); max = parseInt(max); val = parseInt(val);
          					if (val<min || max<val) 
          						errors+='- '+nm+' '+conjStrNumberRange+' ['+min+', '+max+'].<br>';
    					} 
    				} 
    			} 
    			else if (test.charAt(0) == 'R') 
    				errors += '- '+nm+' '+conjStrRequired+'.<br>'; 
    		}
	}
	errors += exValidate(); 
	if (errors) 
		divSysMsg.innerHTML = conjStrErrorList + '<br>' + errors
	//	alert(conjStrErrorList+':<br>'+errors); 
  	
  	document.MM_returnValue = (errors == '');
}

function validateFieldType(strName, strValue, strType){
	var strRet = ''
	
	if(strType == 'i'){
		if (isNaN(strValue)) strRet = '- '+strName+' '+conjStrInvalidNumber+'.<br>'
	}	
	else if(strType == '+i'){
		if (isNaN(strValue)) strRet = '- '+strName+' '+conjStrInvalidNumber+'.<br>'
		else if( strValue<0) strRet = '- '+strName+' '+conjStrInvalidNumber+'.<br>'
	}	
	
	return strRet		
}	

function exValidateRadioBox(fldName, fldDsr){
	var coll, i, j;
	var sRet = '';
	
	coll = frm.elements(fldName)
	for(i=0, j=0; i<coll.length; i++){
		if( coll(i).checked ) j++;
	}
	if( j == 0 ) sRet += '- '+fldDsr+' '+conjStrSelectRequired+'.<br>';
	
	return sRet;
}

function exValidateRadioBox2(fldName, fldDsr, fldOtherName){
	return exValidateCheckBox2(fldName, fldDsr, fldOtherName);
}

function exValidateCheckBox(fldName, fldDsr){
	return exValidateRadioBox(fldName, fldDsr);
}

function exValidateCheckBox2(fldName, fldDsr, fldOtherName){
	var coll, i, j;
	var sRet = '';
	var bOther = false, sOther = '';

	coll = frm.elements(fldName);
	for(i=0, j=0; i<coll.length; i++){
		if( coll(i).checked ){
			j++;
			if( coll(i).value == 2 ){
				bOther = true;
				sOther = trim(eval('frm.'+fldOtherName+'.value'));
			}
		}		
	}
	
/*	if( j == 0 || (j == 1 && bOther && sOther == '') ){
		sRet += '- '+fldDsr+' '+conjStrSelectRequired+'.<br>';
	}	
*/
	if( j == 0 ) sRet += '- '+fldDsr+' '+conjStrSelectRequired+'.<br>';
	if( bOther && sOther == '' ) sRet += '- '+fldDsr+' '+conjStrOtherRequired+'.<br>';
		
	return sRet;
}


function exValidateDate(fldName, fldDsr){
// sDate format: year(4)-month(1/2)-day(1/2)	

	var obj = MM_findObj(fldName)
	var aDate = obj.value.split("-")
//	var aDate = eval('form1.'+fldDate+'.value').split("-"); 
	var i, s, s2, n;	

	if( aDate.length != 3 ) return '- '+fldDsr+' '+conjStrInvalidDate+'.<br>';
	if( isNaN(aDate[0]) || aDate[0].length != 4 ) return '- '+fldDsr+' '+conjStrInvalidYear+'.<br>';
	if( isNaN(aDate[1]) || aDate[1].length > 2 ) return '- '+fldDsr+' '+conjStrInvalidMonth+'.<br>';
	if( isNaN(aDate[2]) || aDate[2].length > 2 ) return '- '+fldDsr+' '+conjStrInvalidDay+'.<br>';

	n = parseInt( aDate[0] );
	if( isNaN(n) || n<1900 || n>2050 ) return '- '+fldDsr+' '+conjStrInvalidYear+'.<br>';

	s = aDate[1];
	i = s.indexOf('0')
	s2 = ( i == 0)? s.substr(1): s.substr(0)
	n = parseInt( s2 );
	if( isNaN(n) || n<1 || n>12 ) return '- '+fldDsr+' '+conjStrInvalidMonth+'.<br>';

	s = aDate[2];
	i = s.indexOf('0')
	s2 = ( i == 0)? s.substr(1): s.substr(0)
	n = parseInt( s2 );
	if( isNaN(n) || n<1 || n>31 ) return '- '+fldDsr+' '+conjStrInvalidDay+'.<br>';
		
	return '';	
}

function validateDate(fldDate, fldDsr){
// sDate format: year(4)-month(1/2)-day(1/2)	

	var aDate = fldDate.split("-")
	var i, s, s2, n;	
	
	if( aDate.length != 3) return '- '+fldDsr+' '+conjStrInvalidDate+'.<br>';
	if( isNaN(aDate[0]) || aDate[0].length != 4 ) return '- '+fldDsr+' '+conjStrInvalidYear+'.<br>';
	if( isNaN(aDate[1]) || aDate[1].length > 2 ) return '- '+fldDsr+' '+conjStrInvalidMonth+'.<br>';
	if( isNaN(aDate[2]) || aDate[2].length > 2 ) return '- '+fldDsr+' '+conjStrInvalidDay+'.<br>';

	n = parseInt( aDate[0] );
	if( isNaN(n) || n<1900 || n>2050 ) return '- '+fldDsr+' '+conjStrInvalidYear+'.<br>';

	s = aDate[1];
	i = s.indexOf('0')
	s2 = ( i == 0)? s.substr(1): s.substr(0)
	n = parseInt( s2 );
	if( isNaN(n) || n<1 || n>12 ) return '- '+fldDsr+' '+conjStrInvalidMonth+'.<br>';

	s = aDate[2];
	i = s.indexOf('0')
	s2 = ( i == 0)? s.substr(1): s.substr(0)
	n = parseInt( s2 );
	if( isNaN(n) || n<1 || n>31 ) return '- '+fldDsr+' '+conjStrInvalidDay+'.<br>';
		
	return '';	
}

function exValidatePassword( fldNamePasswd, fldNamePasswd2){
	
	var sRet = '';
	var fldPasswd = MM_findObj(fldNamePasswd)
	var fldPasswd2 = MM_findObj(fldNamePasswd2)
	
	if( fldPasswd.value != fldPasswd2.value)
		sRet = '- '+fldPasswd.tagname+','+fldPasswd2.tagname+' '+conjStrInconsistent+'.<br>';
		
	return sRet;	
}


// -----------------------------------------------------------------------------------------------------------------
// -- form confirmation on submitting ----

function confirmDeleteRecord(obj){
	if(confirm(conjStrRecordConfirmDelete)) return true
	return false
}	

// -----------------------------------------------------------------------------------------------------------------
// -- form obj operation
/*
function disableTextbox(obj){
	obj.value = "";
	obj.disabled = true;
	obj.style.backgroundColor = "#cccccc"
}
function enableTextbox(obj){
	obj.disabled = false;
	obj.style.backgroundColor = "#ffffff"
}


function CheckCheckBox(fldName, fldValues){
	return CheckRadioBox(fldName, fldValues);
}

function CheckCheckBox2(fldName, fldValues, fldOtherName, fldOtherValue){
	var coll, i, j;
	
	coll = frm.elements(fldName)
	for(i=0; i<coll.length; i++){
		for(j=0; j<fldValues.length; j++){
			if( coll(i).value == fldValues[j] ){
				coll(i).checked = true;
				break;
			}	
		}	
	}
	SetTextBoxVal(fldOtherName, fldOtherValue)
}

function SelectListBox(fldName, fldValues){
	var coll, i, j;
	
	coll = frm.elements(fldName)
	for(i=0; i<coll.length; i++){
		for(j=0; j<fldValues.length; j++){
			if( coll(i).value == fldValues[j] ){
				coll(i).selected = true;
				break;
			}	
		}	
	}	
}
*/
function disableForm(strFormName){
	disableObjs( eval(strFormName+'.all'))
}

function disableObjs(obj){
	var i, strObjType
	for(i=0; i<obj.length; i++){
		strObjType = obj(i).type
		if(strObjType == 'text' || strObjType == 'textarea' || strObjType == 'password'){
			obj(i).readOnly = true
			obj(i).style.backgroundColor = "#cccccc"
		}
		else if(strObjType == 'radio' || strObjType == 'checkbox' 
			|| strObjType == 'select-one' || strObjType == 'select-multiple'){
			obj(i).style.backgroundColor = "#cccccc"
		//	obj(i).disabled = true
		}
	}
}

function disableObjsByID(strID){
	disableObjs( MM_findObj(strID))
}	

function disableTextbox(fldName){
	var obj = MM_findObj(fldName)
	obj.readOnly = true
	obj.style.backgroundColor = '#cccccc'
}	

function enableTextbox(fldName){
	var obj = MM_findObj(fldName)
	obj.readOnly = false
	obj.style.backgroundColor = '#ffffff'
}	

function disableListbox(fldName){
	var obj = MM_findObj(fldName)
	obj.style.backgroundColor = '#cccccc'
}	

function enableListbox(fldName){
	var obj = MM_findObj(fldName)
	obj.style.backgroundColor = '#ffffff'
}	

function disableObj(fldName){
	var obj = MM_findObj(fldName)
	obj.readOnly = true
	obj.style.backgroundColor = '#cccccc'
}	

function enableObj(fldName){
	var obj = MM_findObj(fldName)
	obj.readOnly = false
	obj.style.backgroundColor = '#ffffff'
}	


function setTextBoxVal(fldName, fldValue){
	var obj = MM_findObj(fldName)
	obj.value = fldValue
}

function unselectListBox(fldName){
	var coll = MM_findObj(fldName), i

	for(i=0; i<coll.length; i++){
		if( coll(i).selected ) {
			coll(i).selected = false;
		//	break;
		}	
	}	
}

function selectListBox(fldName, fldValue){
	var coll = MM_findObj(fldName), i

	for(i=0; i<coll.length; i++){
		if( coll(i).value == fldValue ){
			coll(i).selected = true;
			break;
		}	
	}	
}

function chkCheckBox(fldName, blnCheck){
	var obj = MM_findObj(fldName)
	obj.checked = blnCheck
}

function chkCheckBox2(fldName, fldValue, blnCheck){
	var coll = MM_findObj(fldName), i

	for(i=0; i<coll.length; i++){
		if( coll(i).value == fldValue ){
			coll(i).checked = blnCheck;
			break;
		}	
	}	
}

function enableObjs(obj){
	var i, strObjType
	for(i=0; i<obj.length; i++){
		strObjType = obj(i).type
		if(strObjType == 'text' || strObjType == 'textarea' || strObjType == 'password'){
			obj(i).readOnly = false
			obj(i).style.backgroundColor = "#ffffff"
		}
		else if(strObjType == 'radio' || strObjType == 'checkbox' 
			|| strObjType == 'select-one' || strObjType == 'select-multiple'){
			obj(i).style.backgroundColor = "#ffffff"
		//	obj(i).disabled = true
		}
	}
}

function enableObjsByID(strID){
	enableObjs( MM_findObj(strID))
}	

// -----------------------------------------------------------------------------------------------------------------

function dtSelectDT(obj){
	var str = window.showModalDialog(
		'/include/calendar.htm', 
		'', 
		'dialogWidth:270px; dialogHeight:247px; center: yes; status:0; help:0;'
		)
	
	if(str != null && str != ''){
		eval("frm."+obj+".value='"+str+"'")
	}	
}

