﻿String.prototype.lengthW = function() { return this.replace(/[^\x00-\xff]/g,"**").length; } 

String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); } 

function checkclick(msg){if(confirm(msg)){event.returnValue=true;}else{event.returnValue=false;}}

function checkNum(ctrl, message)
{
	var ret = isNaN(ctrl.value);
	if (ret)
	{
		alert(message);
		ctrl.focus();
		return false;
	}
	return true;
}

function changeSelect(ctrl, str) 
{
	if (str == '')
		return;
	with (ctrl) {
		len = options.length;
		for (i = 0; i < len; i++)
			if (options[i].value == str)
				break;
		if ( i < len )
			selectedIndex = i;
	}
}

function checkFormControl(ctrl, message)
{
	if (ctrl.value == "")
	{
		alert(message);
		ctrl.focus();
		return false;
	}
	return true;
}

function RefreshSelect(ctrl, strList)
{
	ctrl.options.length = 0;
	for (var i = 0; i < strList.length; i += 2)
	{
		ctrl.options[ctrl.options.length] = new Option(strList[i], strList[i + 1]);
	}
}

function limitTextArea(ctrl, maxlength)
{
	if (ctrl.value.lengthW() > maxlength)
	{
		alert("输入的长度太长，请重新修改！");
		ctrl.focus();
		return false;
	}
	return true;
}

/* 图片上传函数
 例子:图片名字为house_pic1

1.在HTML里面添加如下部分
<TABLE name="tbl_house_pic1" id="tbl_house_pic1" cellnum=0><TR></TR></TABLE>
        <input id="house_pic1" type="hidden" name="house_pic1" />
    <a href="javascript:uploadpic('../', 'house_pic1', 2)">上传(可以多幅)</a>
	
	
2.在onload的时候调用
   load_piclist('../', '<%=rs("house_pic1")%>', 'house_pic1');
   将数据库中的图片列表加载到tbl_house_pic1表格中

3.在checkForm中调用
   save_piclist('house_pic1');
   将tbl_house_pic1表格中的图片列表写到 hidden的house_pic1控件中，再上传到服务器动态页面
 */
// 上传图片，参数：
// path：网站根目录相对于当前目录的路径，如 "../"或者""
// image_name： input控件的名字，如"house_pic1"
// max_num：最大图片数
function uploadpic(path, image_name, max_num)
{
	var table = document.getElementById("tbl_" + image_name);
	var r1 = table.rows[0];
	if (r1.cells.length >= max_num)
	{
		alert("您已经上传了" + max_num + "个图片，请先删除旧的图片再上传！");
		return;
	}

  	var arr = showModalDialog(path + "inc/pic_up.asp", "", "dialogWidth:28em; dialogHeight:9em; status:0;help:0");
	if (arr == null)
  		return;
  
	var ss;
	ss=arr.split("*");
	a=ss[0];
	b=ss[1];
	c=ss[2];
	d=ss[3];
	e=ss[4];
	f=ss[5];
	
	var c1 = r1.insertCell(-1);
	c1.index = table.cellnum++;
	c1.picname = a;
	c1.picalt = b;
	a = path + a;
	var str = "<a title=点击图片看全图 target='_blank' href='"+a+"'><img src='"+a+"' alt='"+b+"' border=0 width=60 height=60></a>";
	str += "<BR><a class='from' href='#' onclick='javascript:delete_cell();'>删除</a>";
	c1.innerHTML=str;
}

function load_piclist(path, piclist, image_name)
{
	var pics;
	pics = piclist.split("|");
	var table = document.getElementById("tbl_" + image_name);
	var r1 = table.rows[0];
	var c1;
	var ss;
	var a;
	var b;
	var str;
	for (i = 0; i < pics.length; i++)
	{
		if (pics[i] == "")
			continue;
		ss = pics[i].split("*");
		a = ss[0];
		b = ss[1];
		if (b == null) b = '';
		c1 = r1.insertCell(table.cellnum);
		c1.index = table.cellnum++;
		c1.picname = a;
		c1.picalt = b;
		a = path + a;
		str = "<a title=点击图片看全图 target='_blank' href='"+a+"'><img src='"+a+"' alt='"+b+"' border=0 width=60 height=60></a>";
		str += "<BR><a onclick='javascript:delete_cell();'>删除</a>";
		c1.innerHTML = str;
	}
}

function save_piclist(image_name)
{
	var table = document.getElementById("tbl_" + image_name);
	var txt_ctrl = document.getElementById(image_name);
	var r1 = table.rows[0];
	var str;
	txt_ctrl.value = "";
	for (i = 0; i < r1.cells.length; i++)
	{
		str = r1.cells[i].picname;
		if (r1.cells[i].picalt != "")
			str = str + "*" + r1.cells[i].picalt;
		if (txt_ctrl.value.length == 0)
			txt_ctrl.value = str;
		else
			txt_ctrl.value = txt_ctrl.value + "|" + str;
	}
}

function delete_cell()
{
	var cell = event.srcElement;
	if (cell.tagName.toLowerCase() != "td")
		cell = cell.parentElement;
	var row = cell.parentElement;
	for (i = 0; i < row.cells.length; i++)
   	{
   		if (row.cells[i].index == cell.index)
   		{
   			row.deleteCell(i);
   			break;
   		}
	}
}
// 图片上传部分结束




