function getParameter(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) {
		return decodeURIComponent(r[2]);
	}
	return null;
}

String.prototype.replaceAll = function(s, e) {
	return this.split(s).join(e);
}

String.prototype.startWith = function(s) {
	var reg = new RegExp("^" + s);
	return reg.test(this);
}

Array.prototype.indexOf = function(val) {
	for(var i = 0; i < this.length; i++) {
		if(this[i] == val) return i;
	}
	return -1;
};

/**
 * 返回格式化日期字符串
 * @param 日期对象
 * @return yyyy-MM-dd
 */
$.formatDateStyle1 = function(myDate) {
	var year = myDate.getFullYear();
	var month = ("0" + (myDate.getMonth() + 1)).slice(-2);
	var day = ("0" + myDate.getDate()).slice(-2);
	return year + "-" + month + "-" + day;
};

/**
 * 返回格式化日期字符串
 * @param 日期对象
 * @return hh-mm-ss
 */
$.formatDateStyle2 = function(myDate) {
	var h = ("0" + myDate.getHours()).slice(-2);
	var m = ("0" + myDate.getMinutes()).slice(-2);
	var s = ("0" + myDate.getSeconds()).slice(-2);
	return h + ":" + m + ":" + s;
};

/**
 * 返回格式化日期字符串
 * @param 日期对象
 * @return yyyy-MM-dd hh-mm-ss
 */
$.formatDateStyle3 = function(myDate) {
	var year = myDate.getFullYear();
	var month = ("0" + (myDate.getMonth() + 1)).slice(-2);
	var day = ("0" + myDate.getDate()).slice(-2);
	var h = ("0" + myDate.getHours()).slice(-2);
	var m = ("0" + myDate.getMinutes()).slice(-2);
	var s = ("0" + myDate.getSeconds()).slice(-2);
	return year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s;
};

$.formatDateStyle4 = function(myDate) {
	var year = myDate.getFullYear();
	var month = ("0" + (myDate.getMonth() + 1)).slice(-2);
	var day = ("0" + myDate.getDate()).slice(-2);
	var h = ("0" + myDate.getHours()).slice(-2);
	var m = ("0" + myDate.getMinutes()).slice(-2);
	return year + "-" + month + "-" + day + " " + h + ":" + m;
};

$.FormatDateTime = function(obj, IsMi) {
	var myDate = new Date(obj);
	var year = myDate.getFullYear();
	var month = ("0" + (myDate.getMonth() + 1)).slice(-2);
	var day = ("0" + myDate.getDate()).slice(-2);
	var h = ("0" + myDate.getHours()).slice(-2);
	var m = ("0" + myDate.getMinutes()).slice(-2);
	var s = ("0" + myDate.getSeconds()).slice(-2);
	var mi = ("00" + myDate.getMilliseconds()).slice(-3);
	if(IsMi == true) {
		return year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s;
	} else {
		return year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s + "." + mi;
	}
};

String.prototype.Trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}

//手机校验
function checkMobile(sMobile) {
	return /^1[3|4|5|7|8][0-9]\d{8}$/.test(sMobile);
}

//邮箱校验
function isEmail(obj) {
	var result = false;
	var IValue = obj;
	var tem = /\w/;
	if(IValue.Trim().length > 0) {
		result = new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(IValue);
	}
	return result;
}

function formatFileSize(fileSize) {
	if(!fileSize) {
		return 0;
	}
	if(fileSize > 1024 * 1024 * 1024) {
		return(fileSize / (1024 * 1024 * 1024)).toFixed(2) + "GB";
	}
	if(fileSize >= 1024 * 1024) {
		return(fileSize / (1024 * 1024)).toFixed(2) + "MB";
	}
	if(fileSize >= 1024) {
		return(fileSize / 1024).toFixed(2) + "KB";
	}
	if(fileSize < 1024) {
		return "1KB";
	}
	return fileSize;
}

function isNum(str) {
	isNumber = /^\d+(\.\d+)?$/;
	return(isNumber.test(str));
}

function hasHtml(str) {
	var ishtml = /<(.*)>.*<\/\1>|<(.*) \/>/;
	var str2 = str.replace(/<(.*)>.*<\/\1>|<(.*) \/>/ig, "");
	return(ishtml.test(str));
}

/**
 * 转换头像尺寸
 */
var HEAD_REGEXP = /(60|64|100)\.(jpg|png|bmp)$/g;

function transferHeadSize(headName, size) {
	var matchArr = headName.match(HEAD_REGEXP);
	if(matchArr == null) {
		return headName;
	}
	return headName.replace(HEAD_REGEXP, '') + matchArr[0].replace(/\d{2,3}/g, size);
}

/**
 * 根据固定尺寸大小缩放图片
 *
 * @author liyanwei
 * @param imageId
 * @param newUrl
 * @param maxWidth
 * @param maxHeight
 */
function loadImg(imgObj, src, imgSize, maxSize) {
	imgObj.next('.load').show();
	imgObj.next('b').show();
	imgObj.next('.success').hide();
	imgObj.show();
	var height, width;
	imgObj.prev('b').hide();
	var imgWidth = imgSize[0];
	var imgHeight = imgSize[1];
	var maxWidth = maxSize[0];
	var maxHeight = maxSize[1];
	if($('.record_per').length == 0){
		var listDots = listDot == 50 ? false : true;
	}
	var ratio = 0; // 缩放比例
	// 检查图片是否超宽
	if(imgWidth >= maxWidth && imgHeight < maxHeight) {
		try {
			imgObj.attr("src", src);
			imgObj.load(function(){
				imgObj.css("width", maxWidth);
				imgObj.last().parents('p.pdd').css({
					'background-position': '20px ' +  imgObj.height() / 2 + 'px'
				}).addClass('listStyle loadStyle');
				if($('.record_per').length == 0){
					if( $('.list_' + listDot).siblings().length == 1){
						$('.list_' + listDot).removeClass('listStyle loadStyle');
					}
				}
				if(imgObj.parents('p.pdd').prev().parent('dl').find('img').length == 1 || imgObj.parents('p.pdd').prev().parent('dl').find('p.pdd').length == 1){
					imgObj.parents('p.pdd').prev().parent('dl').find('p.pdd').removeClass('listStyle loadStyle');
				}
				if(!listDots){
					imgObj.parents('p.pdd').removeClass('listStyle loadStyle');
				}
			})
			sendCommand('{command:9015, params:{title:"设定图片成功", content:' + src + ', iswritefile:1}}');
			sendCommand('{command:9042, params:{imgid:"'+imgObj.attr("md5")+'",tolvid:'+Number(tolvid)+',groupid:'+Number(groupId)+',finish:true}}');
		} catch(e) {
			sendCommand('{command:9015, params:{title:"设定图片出错", content:' + e + ', iswritefile:1}}');
			sendCommand('{command:9042, params:{imgid:"'+imgObj.attr("md5")+'",tolvid:'+Number(tolvid)+',groupid:'+Number(groupId)+',finish:false}}');
		}
	}
	if(imgHeight > maxHeight && imgWidth < maxWidth) {
		try {
			imgObj.attr("src", src);
			imgObj.load(function(){
				imgObj.css("height", maxHeight);
				imgObj.last().parents('p.pdd').css({
					'background-position': '20px ' +  imgObj.height() / 2 + 'px'
				}).addClass('listStyle loadStyle');
				if($('.record_per').length == 0){
					if( $('.list_' + listDot).siblings().length == 1){
						$('.list_' + listDot).removeClass('listStyle loadStyle');
					}
				}
				if(imgObj.parents('p.pdd').prev().parent('dl').find('img').length == 1 || imgObj.parents('p.pdd').prev().parent('dl').find('p.pdd').length == 1){
					imgObj.parents('p.pdd').prev().parent('dl').find('p.pdd').removeClass('listStyle loadStyle');
				}
				if(!listDots){
					imgObj.parents('p.pdd').removeClass('listStyle loadStyle');
				}
			})
			sendCommand('{command:9015, params:{title:"设定图片成功", content:' + src + ', iswritefile:1}}');
			sendCommand('{command:9042, params:{imgid:"'+imgObj.attr("md5")+'",tolvid:'+Number(tolvid)+',groupid:'+Number(groupId)+',finish:true}}');
		} catch(e) {
			sendCommand('{command:9015, params:{title:"设定图片出错", content:' + e + ', iswritefile:1}}');
			sendCommand('{command:9042, params:{imgid:"'+imgObj.attr("md5")+'",tolvid:'+Number(tolvid)+',groupid:'+Number(groupId)+',finish:false}}');
		}
	}
	if(imgHeight >= maxHeight && imgWidth >= maxWidth){
		if(imgHeight > imgWidth) { // 高图
			ratio = imgHeight / imgWidth; // 缩放比例
			height = maxHeight;
			width = maxHeight / ratio;
		} else if(imgHeight <= imgWidth) { // 宽图
			ratio = width / imgHeight; // 缩放比例
			height = maxWidth / ratio;
			width = maxWidth;
		}
		try {
			imgObj.load(function(){
				imgObj.css({
					"width": width,
					"height": height
				});
			})
			if(imgWidth <= 80 && imgHeight <= 80){
				imgObj.next('.load').remove();
				imgObj.next('b').hide();
			}
			sendCommand('{command:9015, params:{title:"设定图片成功", content:' + src + ', iswritefile:1}}');
			sendCommand('{command:9042, params:{imgid:"'+imgObj.attr("md5")+'",tolvid:'+Number(tolvid)+',groupid:'+Number(groupId)+',finish:true}}');
		} catch(e) {
			sendCommand('{command:9015, params:{title:"设定图片出错", content:' + e + ', iswritefile:1}}');
			sendCommand('{command:9042, params:{imgid:"'+imgObj.attr("md5")+'",tolvid:'+Number(tolvid)+',groupid:'+Number(groupId)+',finish:false}}');
		}
		imgObj.load(function(){
			imgObj.last().parents('p.pdd').css({
					'background-position': '20px ' +  imgObj.height() / 2 + 'px'
				}).addClass('listStyle loadStyle');
			if($('.record_per').length == 0){
				if( $('.list_' + listDot).siblings().length == 1){
					$('.list_' + listDot).removeClass('listStyle loadStyle');
				}
			}
			if(imgObj.parents('p.pdd').prev().parent('dl').find('img').length == 1 || imgObj.parents('p.pdd').prev().parent('dl').find('p.pdd').length == 1){
				imgObj.parents('p.pdd').prev().parent('dl').find('p.pdd').removeClass('listStyle loadStyle');
			}
			if(!listDots){
				imgObj.parents('p.pdd').removeClass('listStyle loadStyle');
			}
		})
	}
	imgObj.prev('b').remove();
	imgObj.show();
	imgObj.attr("src",src);
	if(imgHeight <= maxHeight && imgWidth <= maxWidth){
		imgObj.load(function(){
			imgObj.last().parents('p.pdd').css({
					'background-position': '20px ' +  imgObj.height() / 2 + 'px'
				}).addClass('listStyle loadStyle');
			if($('.record_per').length == 0){
				if( $('.list_' + listDot).siblings().length == 1){
					$('.list_' + listDot).removeClass('listStyle loadStyle');
				}
			}
			$('p.notStyle').removeClass('listStyle loadStyle');
			if(!listDots){
				imgObj.parents('p.pdd').removeClass('listStyle loadStyle');
			}
		})
	}
	$('p.notStyle').removeClass('listStyle loadStyle');	
	if(imgObj.parents('p.pdd').prev().parent('dl').find('img').length == 1 || imgObj.next('p.pdd').prev().parent('dl').find('p.pdd').length == 1){
		imgObj.parents('p.pdd').prev().parent('dl').find('p.pdd').removeClass('listStyle loadStyle');
	}
	if(!listDots){
		imgObj.parents('p.pdd').removeClass('listStyle loadStyle');
	}
}
function getImServer(imServer) {
	if(!imServer) {
		return serverUrl;
	}
	if(!imServer.startWith("http://")) {
		imServer = "http://" + imServer;
	}
	if(imServer.lastIndexOf("/") != imServer.length - 1) {
		imServer = imServer + "/";
	}
	return imServer;
}

/**
 * 向客户端发送指令
 */
function sendCommand(command) {
	return LvClientWebFunc(clientKey, command);
}
clientKey = getParameter("clientkey");

/**
 * 获取各项业务请求前缀
 */
var requestUrlMap = {
	'userInfo': {
		type: 2,
		url: 'user/userinfo.action?callback=?'
	},
	'projectInfo': {
		type: 1,
		url: 'project/projectinfo.action?callback=?'
	},
	'getPersonMsg': {
		type: 5,
		url: 'chat/findsinglemsg.action?callback=?'
	},
	'getGroupMsg': {
		type: 5,
		url: 'chat/findgroupmsg.action?callback=?'
	},
	'saveFileToCloud': {
		type: 4,
		url: 'chat/save-file-to-cloud.action?callback=?'
	},
	'searchGroupMsg': {
		type: 5,
		url: 'chat/search-group-msg.action?callback=?'
	},
	'searchPersonMsg': {
		type: 5,
		url: 'chat/search-single-msg.action?callback=?'
	},
	'getChatImg': {
		type: 5,
		url: 'images/get-image-by-md5.action'
	},
	'updateUserInfo': {
		type: 2,
		url: 'user/updateuser.action?callback=?'
	},
	'updateFriendGroup': {
		type: 2,
		url: 'user/update-friend-group.action?callback=?'
	},
	'createFriendGroup': {
		type: 2,
		url: 'user/create-friendgroup.action?callback=?'
	},
	'superGroupList': {
		type: 2,
		url: 'user/supergroup-list.action?callback=?'
	},
	'getUserLevel': {
		type: 7,
		url: 'stat/user-level.action'
	},
	'cloudGroupInfo': {
		type: 3,
		url: 'group/cloudgroupinfo.action?callback=?'
	},
	'noticeList':{
		type: 6,
		url: 'notice/notice-list.action?callback=?'
	},
	'updateNotice':{
		type: 6,
		url: 'notice/update-notice.action?callback=?'
	},
	'clearNotice':{
		type: 6,
		url: 'notice/clean-notice.action?callback=?'
	},
	'classicsHead':{
		type: 0,
		url: 'head/change-classics-head.action?callback=?'
	},
	'classicsHeadPrefix':{
		type: 2,
		url: 'user/head/classic/'
	},
	'cutImage':{
		type: 0,
		url: 'head/cut-head.action?callback=?'
	},
	'listGroupProject':{
		type: 1,
		url: 'project/listgroupprojects.action?callback=?'
	},
	'copyGroupList':{
		type: 3,
		url: 'group/grouplistnew.action?callback=?'
	},
	'copyProject':{
		type: 1,
		url: 'project/copyproject.action?callback=?'
	},
	'updateProName':{
		type: 1,
		url: 'project/updateprojectname.action?callback=?'
	},
	'delProject':{
		type: 1,
		url: 'project/deleteGroupProject.action?callback=?'
	},
	'cameraScript':{
		type: 1,
		url: 'project/checkCameraScript.action?callback=?'
	},
	'groupUserAndChatlist':{
		type: 6,
		url: 'group/cloudgroupuserandchatlist.action?callback=?'
	},
	'groupUpdate':{
		type: 3,
		url: 'group/updategroup.action'
	},
	'updataName':{
		type: 3,
		url: 'group/update-cloudgroup-nickname.action?callback=?'
	},
	'groupVerify':{
		type: 3,
		url: 'group/resetgroupverify.action?callback=?'
	},
	'groupUpdateMsgType':{
		type: 3,
		url: 'group/update-group-msg-type.action?callback=?'
	},
	'offGroup':{
		type: 3,
		url: 'group/off-group.action?callback=?'
	},
	'inGroup':{
		type: 3,
		url: 'group/ingroup.action?callback=?'
	},
	'searchFriend':{
		type: 0,
		url: 'search/search-user.action?callback=?'
	},
	'searchGroup':{
		type: 0,
		url: 'search/search-group.action?callback=?'
	},
	'groupNotice':{
		type: 6,
		url: 'notice/create-notice.action?callback=?'
	},
	'recommend':{
		type: 2,
		url: 'user/recommend-users.action?callback=?'
	},
	'creatGroup':{
		type: 3,
		url: 'group/create-cloudgroup.action?callback=?'
	},
	'chatData':{
		type: 5,
		url: 'chat/get-singlemsg-day.action?callback=?'
	},
	'chatDataG':{
		type: 5,
		url: 'chat/get-groupmsg-day.action?callback=?'
	},
	'vipInfo':{
		type: 2,
		url: 'user/vip/info.action?callback=?'
	},
	'vipBuildOrder':{
		type: 2,
		url: 'user/buildorder.action'
	},
	'friendName':{
		type: 2,
		url: 'user/update-friend-nickname.action?callback=?'
	},
	'reportUser':{
		type: 8,
		url: 'user/feedbacksubmit.action?callback=?'
	},
	'reportPic':{
		type: 8,
		url: 'user/complainsubmit.action?callback=?'
	}
}

for(var i in requestUrlMap) {
	var result = JSON.parse(sendCommand('{command:9034, params:{name:' + requestUrlMap[i]['type'] + '}}'));
	if(result['errorid'] == 0 || result['errorid'] == -1) {
		requestUrlMap[i]['url'] = (result['url'] + '/' + requestUrlMap[i]['url']);
	}
}