/**
*	社会关系 id field pattern: sr1rel1,sr1name1, sr1workUnit1, sr1pos1, sr1contact1, sr1graduInt,(sr-社会关系，1-亲朋好友，rel-关系字段，1-第一条记录)
*	学历 id field pattern: xlDegree1, xlGraduNo1, xlEntryYear1, xlGraduYear1, xlGraduSchool1, xlGraduInst1, xlGraduProf1 
*	工作经历 id pattern: gzWorkStart1,gzWorkEnd1, gzWorkCity1, gzWorkUnit1, gzWorkPos1 对应： 工作开始时间， 工作结束时间， 工作城市， 工作单位， 职位 的第一条记录
*	
*/

/****
**	Note!!! The property is read/write for all objects except the following, for which it is read-only: 
**			COL, COLGROUP, FRAMESET, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR. 
** 	So, is there any easier way to add row dynamic by js? DOM?
****/

// eg: 'xx303', return '303'
function getLastDigits(str) {
	var re = /\d+$/;
	var arr = re.exec(str);
	return (arr == null)? 'error getting Last digits in String.':arr[0];
}

// eg: str = 'sr2name3', pattern = 'sr2', return true, or false return
/**
*	str: the str may containe pattern str,
*	pattern: the pattern str
*/
function beginWidth(str, pattern) {
	var re = new RegExp(pattern);
	return re.test(str);
}

function endWithDigit(str) {
	var re = /\d+$/;
	var arr = re.exec(str);
	return (arr == null)? false : true;
}

/************** data all this id(name) in form is split by ',' and there is no space between it*******************/
// 注意！这些逗号分隔作为表单元素ID, 或者Name的字符串逗号前后都不能有空格，否则UpdateXyinfoAction.java取不到数据
// first of all, defind the column sequence
// 亲朋信息(合并后只用这个类别的sr id pattern)	idPattern - sr1id
var sr1 = 'sr1type,sr1rel,sr1name,sr1workUnit,sr1pos,sr1contact,sr1graduInt,sr1hidden';
// 班主任信息	idPattern - sr2id
var sr2 = 'sr2rel,sr2name,sr2workUnit,sr2pos,sr2contact,sr2graduInt,sr2hidden';
// 伴侣配偶信息	idPattern - sr3id
var sr3 = 'sr3rel,sr3name,sr3workUnit,sr3pos,sr3contact,sr3graduInt,sr3hidden';

// 学历信息	idPattern - xlId
var xl = 'xlDegree,xlEduStat,xlGraduNo,xlEntryYear,xlGraduYear,xlGraduSchool,xlGraduInst,xlGraduProf,xlHidden';
// 工作经历	idPattern - gzId
var gz = 'gzWorkStart,gzWorkEnd,gzWorkCity,gzWorkUnit,gzWorkPos,gzHidden';

// 投票选项
var tpOpt = 'tpOpt';


/********************** functions ***************/

var Tmpl = {
	// idPattern: string not include the last postfix digit(list in sr1, sr2, sr3, xl, gz below)
	getNextId: function (idPattern) {	// the next idx of table rows pattern 
		// get all elements
		var elems = document.getElementsByTagName('body')[0].getElementsByTagName('*');
		var max = 0;
		for (var i = 0; i < elems.length; i++) {
			if (elems[i].id != null && beginWidth(elems[i].id, idPattern)) {
				var temp = getLastDigits(elems[i].id);
				max = (parseInt(temp) > max)? parseInt(temp):max;
			}
		}
		return max + 1;
	},
	addSr: function (tbId, srType) {
		srType = 1; // 合并后只用sr1
		var nextId = this.getNextId('sr' + srType + 'id');
		var oRow = document.getElementById(tbId).insertRow(1);
		var colArr = [];
		var hiddenIdPattern = '';
		hiddenIdPattern = 'sr1id';
		colArr = sr1.split(',');
		for (var i = 0; i < colArr.length; i++) {
			var oCell = oRow.insertCell(i);
			// 判断第i列的类型来决定生成的表单控件
			var inputType = 'text';
			switch (i) {
				case 0: 
					inputType = 'select';
					break;
				case 7:
					inputType = 'checkbox';
					break;
				default:
					inputType = 'text';
			}
			//alert(i + ', ' + inputType);
			if (i == 0) {
				var idnName = hiddenIdPattern + nextId;
				this.addHiddenId(oCell, idnName);
			}
			var idnName = colArr[i] + nextId;
			oCell.innerHTML += this.genSrCellContent(i, idnName, inputType);
		}
	}, 
	addXl: function (tbId) {
		var nextId = this.getNextId('xlDegree');
		var tb = document.getElementById(tbId);
		var oRow = tb.insertRow(1);
		var colArr = xl.split(',');
		for (var i = 0; i < colArr.length; i++) {
			var oCell = oRow.insertCell(i);
			// oCell.align = colArr.children[i].align; 
			// oCell.class = colArr.children[i].class; 
			var inputType = 'text';
			switch(i) {
				case 0: case 1:
					inputType = 'select';
					break;
				case 8:
					inputType = 'checkbox';
					break;
				default:
					inputType = 'text';
			}
			var idPattern = 'xlId';
			// add id hidden field
			if (i == 0) {
				var idnName = idPattern + nextId;
				this.addHiddenId(oCell, idnName);
			}
			var idnName = colArr[i] + nextId;
			oCell.innerHTML += this.genXlCellContent(i, idnName, inputType);
			//alert(oCell.innerHTML);
		}
	},
	addGz: function (tbId) {
		var nextId = this.getNextId('gzWorkStart');
		var oRow = document.getElementById(tbId).insertRow(1);
		var colArr = gz.split(',');
		for (var i = 0; i < colArr.length; i++) {
			var oCell = oRow.insertCell(i);
			var inputType = 'text';
			switch(i) {
				case 0:
					inputType = 'select_gz';
					break;
				case 1:
					inputType = 'select_gz';
					break;
				case 5:
					inputType = 'checkbox';
					break;
				default: 
					inputType = 'text';
			}
			var idPattern = 'gzId';
			// add id hidden field
			if (i == 0) {
				var idnName = idPattern + nextId;
				this.addHiddenId(oCell, idnName);
			}
			var idnName = colArr[i] + nextId;
			oCell.innerHTML = this.genGzCellContent(i, idnName, inputType);
			//alert(oCell.innerHTML);
		}
	},
	deleteRow: function (tbId, rowIndex) {
		var oTable = document.getElementById(tbId);
		if (oTable.rows.length > 0) {
			oTable.deleteRow(0);
		}
	},
	// generate a hidden field for id, init value is empty string.
	addHiddenId: function (oCell, idnName) {	
		oCell.innerHTML += '<input type="hidden" id="' + idnName + '" name="' + idnName + '" value="" />';
	},
	
	// genXXCellContent 根据具体的界面需求和功能来定制
	genSrCellContent: function (colIndex, idnName, inputType) { // inputType: 'text', 'select', colIndex use for custom select control
		var inputHtml = '';
		switch(inputType) {
			case 'text':
				inputHtml += '<input size="15" class="zof" type="' + inputType + '" id="' + idnName + '" name="' + idnName + '" />';
				break;
			case 'select':
				inputHtml += '<select' + ' id="' + idnName + '" name="' + idnName + '" >';
				inputHtml += '<option value="" />';
				// get options from page xlOptStr
				var gxlxOptStr = (document.getElementById('gxlxOptStr') == null) ? '' : document.getElementById('gxlxOptStr').value;
				//alert(gxlxOptStr);
				var gxlxOptArr = gxlxOptStr.split(',');
	        	for (var i = 0; i < gxlxOptArr.length; i++) {
	        		inputHtml += '	<option value="' + gxlxOptArr[i] + '">' + gxlxOptArr[i] + '</option>';
	        	}
				//inputHtml += '	<option value="xx">xx</option>';
				//inputHtml += '	<option value="xx2">xx2</option>';
				inputHtml += '</select>';
				break;
			case 'checkbox':
				inputHtml += '<input type="checkbox" id="' + idnName + '" name="' + idnName + '"  onclick="this.value=this.checked" />';
				break;
		}
		return inputHtml;
	}, 
	genXlCellContent: function (colIndex, idnName, inputType) {
		var inputHtml = '';
		if (colIndex == 0 && inputType == 'select') {
			inputHtml += '<select id="' + idnName + '" name="' + idnName + '">';
			inputHtml += '<option value="" />';
			// get options from page xlOptStr
			var xlOptStr = (document.getElementById('xlOptStr') == null) ? '' : document.getElementById('xlOptStr').value;
			//alert(xlOptStr);
			var xlOptArr = xlOptStr.split(',');
			
        	for (var ixl = 0; ixl < xlOptArr.length; ixl++) {
        		inputHtml += '	<option value="' + xlOptArr[ixl] + '">' + xlOptArr[ixl] + '</option>';
        	}
           	inputHtml += '</select>';
           	
		}
		if (colIndex == 1 && inputType == 'select') {
			inputHtml += '<select  id="' + idnName + '" name="' + idnName + '">';
			inputHtml += '<option value="" />';
			// get options from page byztOptStr
			var byztOptStr = (document.getElementById('byztOptStr') == null) ? '' : document.getElementById('byztOptStr').value;
			//alert(byztOptStr);
			var byztOptArr = byztOptStr.split(',');
        	
        	for (var ibyzt = 0; ibyzt < byztOptArr.length; ibyzt++) {
        		inputHtml += '	<option value="' + byztOptArr[ibyzt] + '">' + byztOptArr[ibyzt] + '</option>';
        	}
           	inputHtml += '</select>';
		}
		if (inputType == 'text') {
			inputHtml += '<input class="zof"  id="' + idnName + '" name="' + idnName + '"';
			if (colIndex == 2) {
				inputHtml += ' size="8"';
			}
			if (colIndex == 3 || colIndex == 4) {
				inputHtml += ' size="5"';
			}
			if (colIndex == 5 || colIndex==6 || colIndex==7) {
				inputHtml += ' size="10"';
			}
			inputHtml += 'type="' + inputType + '" id="' + idnName + '" name="' + idnName + '" />';
			if (colIndex == 3 || colIndex == 4) {
				inputHtml += ' 年';
			}
		}
		if (inputType == 'checkbox') {
			inputHtml += '<input type="checkbox" id="' + idnName + '" name="' + idnName + '" onclick="this.value=this.checked" />';
		}
		//alert(inputHtml);
		return inputHtml;
	},
	genGzCellContent: function (colIndex, idnName, inputType) {
		var inputHtml = '';
		if (inputType == 'text') {
			inputHtml += '<input class="zof" size="15" type="' + inputType + '" id="' + idnName + '" name="' + idnName + '" />';
		} else if (inputType == 'select') {
			inputHtml += '<select' + ' id="' + idnName + '" name="' + idnName + '" >';
			inputHtml += '	<option value="xx">xx</option>';
			inputHtml += '	<option value="xx2">xx2</option>';
			inputHtml += '</select>';
		} else if (inputType == 'select_gz') {
			var now = new Date();
			inputHtml += '<select' + ' id="' + idnName + '" name="' + idnName + '" >';
			inputHtml += '	<option value=""></option>';
			for ( var g=now.getYear();g>=1900;g-- ) {
				inputHtml += '	<option value="'+g+'">'+g+'</option>';
			}
			inputHtml += '</select>';
		}
		if (inputType == 'checkbox') {
			inputHtml += '<input type="checkbox" id="' + idnName + '" name="' + idnName + '" onclick="this.value=this.checked" />';
		}
		return inputHtml;
	}
	 
};

