var imageext = new Array('jpg', 'jpeg', 'gif', 'png');
var flashext = new Array('swf');

function strLen(str) {
	var charset = document.charset; 
	var len = 0;
	for(var i = 0; i < str.length; i++) {
		len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? (charset == 'utf-8' ? 3 : 2) : 1;
	}
	return len;
}

function fileext(filename) {
	if(filename == null || filename == '') {
		return '';
	}
	var ext = null;
	var num = filename.lastIndexOf(".");
	if(num != -1) {
		ext = filename.substring(num + 1);
	} else {
		ext = '';
	}
	return ext;
}

function isfileext(filename, extarr) {
	var ext = fileext(filename).toLowerCase();
	for(var i = 0; i < extarr.length; i++) {
		if(extarr[i] == ext){
			return true;
		}
	}
	return false;
}

function fill(setid, parentid, arr, value) {
	setid = document.getElementById(setid);
	if(setid != null) {
		setid.options[0]=new Option('请选择','');
		opt = 0;
		if(parentid == '') {
			for(i=0;i<arr.length;i++) {
				setid.options[i+1]=new Option(arr[i][1],arr[i][0]);
				if(arr[i][1] == value) {
					opt = i+1;
				}
			}
			setid.options[opt].selected=true;
			setid.length=i+1;
		} else {
			parentcode = document.getElementById(parentid).value;
			count=1;
			if(parentcode != '') {
				for(i=0;i<arr.length;i++) {
					if(arr[i][0].toString().substring(0,parentcode.length)==parentcode.substring(0, parentcode.length)) {
						setid.options[count]=new Option(arr[i][1],arr[i][0]);
						if(value != null && arr[i][1] == value) {
							opt = count;
						}
						count=count+1;
					}
				}
			}
			setid.options[opt].selected=true;
			setid.length=count;
		}
	}
}

function validate(theform) {
	if(fieldinfo.length > 0) {
		for(i = 0; i < fieldinfo.length; i++) {
			obj = null;
			if(fieldinfo[i][2] == 'checkbox' && fieldinfo[i][4] == '1') {
				ischoose = false;
				var nodes = document.getElementsByTagName('input');
				if(nodes) {
					for(j = 0; j < nodes.length; j++) {
						var node = nodes[j];
						if (node.name == fieldinfo[i][0]+'[]') {
							if(obj == null) obj = node;
							if(node.checked == true) {
								ischoose = true;
								break;
							}
							
						}
					}
					if(!ischoose) {
						alert('请您选择一个'+fieldinfo[i][1]);
						obj.focus();
						return false;
					}
				}
			} else {
				ischoose = true;
				obj = document.getElementById(fieldinfo[i][0]);
				if(fieldinfo[i][4] == '1' && obj && strLen(obj.value) < 1) {
					ischoose = false;
					if(fieldinfo[i][2] == 'text' || fieldinfo[i][2] == 'textarea') {
						alert('请您输入'+fieldinfo[i][1]);
					} else if(fieldinfo[i][2] == 'img' || fieldinfo[i][2] == 'flash' || fieldinfo[i][2] == 'file' || fieldinfo[i][2] == 'timestamp') {
						objvalue = document.getElementById(fieldinfo[i][0]+'_value');
						if(obj && strLen(objvalue.value) < 1) {
							alert('您未设置'+fieldinfo[i][1]+',请检查确认');
						} else {
							ischoose = true;
						}
					} else {
						alert("请您选择一个"+fieldinfo[i][1]);
					}
				}
				if(obj && obj.value != '') {
					if(fieldinfo[i][2] == 'text' || (fieldinfo[i][2] == 'textarea' && fieldinfo[i][3] != 0)) {
						if(fieldinfo[i][5] != 'TEXT' && fieldinfo[i][5] != 'MEDIUMTEXT' && fieldinfo[i][5] != 'LONGTEXT' && fieldinfo[i][5] != 'FLOAT' && fieldinfo[i][5] != 'DOUBLE') {
							if (strLen(obj.value) > fieldinfo[i][3]) {
								ischoose = false;
								alert('您输入的'+fieldinfo[i][1]+'长度不符合要求,目前长度为'+strLen(obj.value)+'字符,请保证在'+fieldinfo[i][3]+'字符以内');
							}
						}
					} else if(fieldinfo[i][2] == 'img' || fieldinfo[i][2] == 'flash') {
						if (!isfileext(obj.value, (fieldinfo[i][2] == 'img' ? imageext : flashext))) {
							ischoose = false;
							alert('您输入的'+fieldinfo[i][1]+'格式不正确,请检查确认');
						}
					}
				}
				if(!ischoose) {
					obj.focus();
					return false;
				}
			}
		}
	}

	return true;
}

var fieldinfo = new Array(
new Array('subject', '影片名称', 'text', '80', '1', 'CHAR'),
new Array('catid', '分类', 'select', '6', '1', 'SMALLINT'),
new Array('subjectimage', '海报', 'img', '80', '0', 'CHAR'),
new Array('translation', '译名', 'text', '100', '0', 'VARCHAR'),
new Array('starring', '主演', 'text', '40', '0', 'CHAR'),
new Array('director', '导演', 'text', '50', '0', 'VARCHAR'),
new Array('otherstarring', '其他主演', 'text', '255', '0', 'VARCHAR'),
new Array('type', '类型', 'checkbox', '50', '1', 'VARCHAR'),
new Array('issue', '发行', 'select', '20', '1', 'CHAR'),
new Array('region', '地区', 'text', '20', '0', 'CHAR'),
new Array('time', '片长', 'text', '10', '1', 'CHAR'),
new Array('color', '色彩', 'select', '12', '1', 'CHAR'),
new Array('url', '下载地址', 'text', '255', '0', 'VARCHAR'),
new Array('introduction', '简介', 'textarea', '255', '1', 'CHAR'),
new Array('stills_1', '剧照', 'img', '150', '0', 'VARCHAR'),
new Array('stills_2', '剧照', 'img', '150', '0', 'VARCHAR'),
new Array('stills_3', '剧照', 'img', '150', '0', 'VARCHAR'),
new Array('evaluation', '影片评价', 'textarea', '0', '0', 'TEXT'),
new Array('snapshots', '精彩花絮', 'textarea', '0', '0', 'TEXT')
);
