loadscript("mdialog");

function validateshop(str){
	if(str.length == 0) return -1;
	if(!checkByteLength(str,2,40)) return 1;
	return 0;
}

function validateaddress(str) {
	if(str.length == 0) return -1;
	if(!checkByteLength(str,4,60)) return 1;
	return 0;
}
function geshiphoto(str)
{
	result=false;
	var right_type=new Array(".gif",".jpg",".jpeg",".png");
	str=str.toLowerCase();
	var postfixLen=str.length;
	for (var i=0;i<right_type.length;i++){
		var ext =str.substring(postfixLen-right_type[i].length,postfixLen);
		if(ext==right_type[i]){ 
			result=true;//格式正确
			break;
		}
	}
	return result;
}
function validatephoto(str)
{
	if (str.length==0)return -1;//kongzhi
	if (!geshiphoto(str)) return 1;//geshicuowu
 	return 0;
}

function setErrMsg(errMsg, id) {
	var error = document.getElementById(id);
	//error.setAttribute('class', 'formerror');
	error.className = 'formerror';
	error.innerHTML = errMsg;
}

function clearPreErrMsgs() {
	var ids = [ 'shopnameError','addressError','passwordError','panelError', 'classError'];
	for (id in ids) {
		var error = document.getElementById(ids[id]);
		if (error && error.innerHTML) {
			//error.setAttribute('class', '');
			error.className = '';
			error.innerHTML = '';
		}
	}
}

function clearErrMsg(id) {
	var error = document.getElementById(id);
	if (error && error.innerHTML) {
		//error.setAttribute('class', '');
		error.className = '';
		error.innerHTML = '';
	}
}
function checkPanel(panel) {
	var errMsg = '';
	if (!panel) {
		errMsg = '请选择网站分类!';
		setErrMsg(errMsg, 'panelError');
	} else {
		clearErrMsg('panelError');
	}
}
function checkPhoto(str)
{
	var errMsg='';
	switch(validatephoto(str)){
		case -1:
			errMsg='请选择图片地址!';
			break;
			return;
		case 1:
			errMsg='您上传的格式不正确，允许上传的格式包括jpeg、jpg、gif、png！';
			break;
			return;
	}
	if(errMsg)
	{
		setErrMsg(errMsg,'passwordError');
		return false;
	}else{
		clearErrMsg('passwordError');
	}
}
function checkShopname(shopname) {
	var errMsg = '';
	switch (validateshop(shopname)) {
	case -1:
		errMsg = '请输入网站名称！';
		break;
	case 1:
		errMsg = '网站名称不能小于2个字符或大于40个字符！';
		break;
	}
	if (errMsg) {
		setErrMsg(errMsg, 'shopnameError');
		return false;
	} else {
		clearErrMsg('shopnameError');
	}
}
function checkClasscode(classcode) {
	var errMsg = '';
	if (classcode.length==0) {
		errMsg = '请选择详细分类';
		setErrMsg(errMsg, 'classError');
		return false;
	} else {
		clearErrMsg('classError');
	}
}
function checkAddress(address) {
	var errMsg = '';
	switch (validateaddress(address)) {
	case -1:
		errMsg = '请输入网址！';
		break;
	case 1:
		errMsg = '网址不能小于4个字符或大于60个字符！';
		break;
	}
	if (errMsg) {
		setErrMsg(errMsg, 'addressError');
	} else {
		clearErrMsg('addressError');
	}
}


function checkaddstorefrm() {
	clearPreErrMsgs();
	var errMsg = '';
	var frm = document.addstorefrm;
	var shopname = frm.shopname.value.trim();
	switch (validateshop(shopname)) {
	case -1:
		errMsg = '请输入网站名称！';
		break;
	case 1:
		errMsg = '网站名称不能小于2个字符或大于40个字符！';
		break;
	}
	if (errMsg) {
		setErrMsg(errMsg, 'shopnameError');
		return false;
	}
	var address = frm.address.value.trim();
	switch (validateaddress(address)) {
	case -1:
		errMsg = '请输入网址！';
		break;
	case 1:
		errMsg = '网址不能小于4个字符或大于60个字符！';
		break;
	}
	if (errMsg) {
		setErrMsg(errMsg, 'addressError');
		return false;
	}
	var photo=frm.photo.value.trim();
	
	switch(validatephoto(photo)){
	case 1:
		errMsg='您上传的格式不正确，允许上传的格式包括jpeg、jpg、gif、png！';
		break;
	case -1:
		errMsg='请选择图片地址';
		break;
	}
	if (errMsg) {
		setErrMsg(errMsg, 'passwordError');
		return false;
	}
	if (frm.panel.value.trim() == '') {
		errMsg = '请选择网站分类！';
		setErrMsg(errMsg, 'panelError');
		return false;
	}
	if (frm.classcode.value.trim() == '') {
		errMsg = '请选择详细分类！';
		setErrMsg(errMsg, 'classError');
		return false;
	}
	return true;
}

function validateshopid(obj){
	var str = obj.value;
	if(str.length == 0) return -1;
	if(!str>0) {
		alert('无效的网站！');
		return false;
	}
	return true;
}

function validatesort(form){
	var sorts = form.getElementsByTagName('select');
	for (var i=0; i<sorts.length; i++) {
		sortname = sorts[i].name;
		if(sortname.match(/^sort[1-8]+$/) && sorts[i].value == '') {
			var nodes = sorts[i].childNodes;
			var errMsg = nodes[1].text + '尚未选择！';
			var reviewError = document.getElementById('reviewError');
			reviewError.style.display = "block";
			reviewError.innerHTML = errMsg;
			sorts[i].focus();
			return false;
		}
	}
	return true;
}

function checkScore() {
	var sorts = document.getElementsByTagName('select');
	var errMsg = ''
	for (var i=0; i<sorts.length; i++) {
		sortname = sorts[i].name;
		if(sortname.match(/^sort[1-8]+$/) && sorts[i].value == '') {
			var nodes = sorts[i].childNodes;
			errMsg += nodes[1].text.replace(/\-/g,'') + ',';
		}
	}
	if (errMsg) {
		errMsg += '尚未选择！';
		var reviewError = document.getElementById('reviewError');
		reviewError.style.display = "block";
		reviewError.innerHTML = errMsg;
	} else {
		var reviewError = document.getElementById('reviewError');
		reviewError.style.display = "none";
		reviewError.innerHTML = '';
	}
}
function removeErrMsgIfAllSelected() {
	var sorts = document.getElementsByTagName('select');
	var errMsg = ''
	for (var i=0; i<sorts.length; i++) {
		sortname = sorts[i].name;
		if(sortname.match(/^sort[1-8]+$/) && sorts[i].value == '') {
			var nodes = sorts[i].childNodes;
			errMsg += nodes[1].text.replace(/\-/g,'') + ',';
		}
	}
	if (!errMsg) {
		var reviewError = document.getElementById('reviewError');
		reviewError.style.display = "none";
		reviewError.innerHTML = '';
	}
}
function showTips() {
	var feeling = document.getElementById('feeling');
	var tip = document.getElementById('tip');
	var text = feeling.value;
	var len = text.length;
	//alert(len);
	if (len <1) {
		tip.innerHTML = '你还没有输入内容';
	} else if (len < 1000) {
		tip.innerHTML = '您还可以输入'+(1000-len)+'个字';
	} else if (len == 1000) {
		tip.innerHTML = '您已达到最大输入字数限制';
	} else {
		tip.innerHTML = '您最多只能输入1000个字，多写了'+(len-1000)+'个字';
	}
}

function validateaveprice(obj) {
	var obj_w = document.getElementById('avgprice_w');
	var avg = obj.value.trim();
	avg = tot(avg);
	obj.value=avg;
	//if (avg.length==0){
	//	obj_w.innerHTML = '请输入人均消费额。';
	//	obj_w.style.display = "block";
	//	return false;
	if (is_numeric(avg)&&avg > 999999) {
		obj_w.innerHTML = '金额在1-999999之间。';
		obj_w.style.display = "block";
		obj.focus();
		return false;
	}
	obj_w.style.display = "none";
	return true;
}
function checkTextarea(text) {
	var obj_w = document.getElementById('content_w');
	var str=text.trim();
	if (str.length==0) {
		obj_w.innerHTML = '请输入点评内容！';
		obj_w.style.display = "block";
		return false;
	} else if (str.length > 1000) {
		obj_w.innerHTML = '点评内容最多只能输入1000个字！';
		obj_w.style.display = "block";
		return false;
	}
	if(!checkByteLength(str,1,1000)) {
		obj_w.innerHTML = '点评内容请控制在1-1000个字以内！';
		obj_w.style.display = "block";
		return false;
	}
	obj_w.style.display = "none";
}



function validatecontent(obj) {
	var obj_w = document.getElementById('content_w');
	var str=obj.value.trim();
	if (str.length==0) {
		obj_w.innerHTML = '请输入点评内容！';
		obj_w.style.display = "block";
		return false;
	} else if (str.length > 1000) {
		obj_w.innerHTML = '点评内容最多只能输入1000个字！';
		obj_w.style.display = "block";
		return false;
	}
	if(!checkByteLength(str,1,1000)) {
		obj_w.innerHTML = '点评内容请控制在50-1000个字以内！';
		obj_w.style.display = "block";
		return false;
	}
	obj_w.style.display = "none";
	return true;
}

function checkaddreviewfrm(frm) {
	var content = frm.content;
	if (validatesort(frm)&&validatecontent(content)) {
		return true;
	} else {
		return false;
	}
}

/*----------增加商铺----------*/

function searchshop (action) {
	var shopname = document.getElementById('shopname').value.trim();
	if (shopname == '') {
		msgOpen('请输入网站关键字！');
		return;
	}
	//var param = 'action=checkshop&option=1&shopname='+strcode(shopname);
	var option;
	$.get(getUrl('shop')+'post.php',{action:"checkshop",option:"1",shopname:shopname,in_ajax:1}, function(result) {
		if(result.trim() == '') {
			alert('操作无响应，可能网络忙碌，请稍后尝试！');
		} else if(result.match(/<option.+value.*/) || action == 'add') {
			option = '<select name="shop" onclick="selectshop(this);" size="12" style="width:250px" >';
			option += result.match(/<option.+value.*/) ? result : '';
			option += '<option value="-1" class="font_6">添加新网站</option>';
			option += '</select>';
			$("#searchshop").html(option);
		} else {
			myAlert(result);
		}
	});
}

function selectshop(obj) {
	if (!obj) {
		alert('请先查找相关网站！');
	}
	var shopid = obj.value;
	if (shopid == '') {
		alert('请选择需要进行点评的网站！');
	} else if (shopid == '-1') {
		location.href = getUrl('shop')+'post.php?action=newshop';
	} else if(is_numeric(shopid) && shopid > 0){
		location.href = getUrl('shop')+'post.php?action=newreview&shopid='+shopid;
	}
}

function show_existshop() {

	var div = document.getElementById("existshop");
	var shopname = document.addstorefrm.shopname.value.trim();
	if (shopname=='') {
		msgOpen('请输入网站名称！');
		return;
	}

	$.get(getUrl('shop')+'post.php', { action:"checkshop",shopname:shopname,in_ajax:1 } , function(result) {
		myAlert(result);
	});

}

/*--上传图片--*/


function imgcheckSize(img,min_w,min_h) {
	if(!img) return false;
	if(img.width>=min_w&&img.height>=min_h){
		return true;
	}else {
		return false;
	}
}
function checkupload () {
	var from = document.uploadfrom;
	var filepath = from.picture.value;
	if(!filepath){
		alert('请选择上传的图片！');
		return false;
	}
	if(filepath.lastIndexOf("\\")>0){
		var filename= filepath.substring(filepath.lastIndexOf("\\")+1,filepath.length);
  }
	if(filename){
		if(!checkImgType(filename)){
			alert('您上传的格式不正确，允许上传的格式包括jpeg、jpg、gif、png！');
			return false;
		}
	}
	if(from.title.value.length==0){
		alert('请填写图片标题！');
		return false;
	}else if(from.title.value.length>20){
		alert('图片标题字数不能超过20个！');
		return false;
	}
	from.submitbutton.disabled = true;
	return true;
}