var the_url=document.location.href;	
	if(null!=the_url && ""!=the_url){
		the_url=escape(the_url);
}

function showUnicast(l_code,flag){
	var htmlCode="<div class=\"dianbo1\"><div id=\"dianbodiv\" class=\"dianbo2\" style=\"background-image:url(/images/unicast/youjian/0.jpg); background-position:-14px 12px;\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
	htmlCode+="<td height=\"21\" class=\"dianbo3\"><table><tr><td>欢迎您使用库客点播服务</td><td><img align=\"absmiddle\" style=\"margin-left:240px;cursor:hand;\" src=\"/images/unicast/guanbi.gif\" alt=\"关闭\" width=\"43\" height=\"15\" border=\"0\" onclick=\"closeUnicast()\"/></td></tr></table></td></tr><tr>";
	htmlCode+="<td class=\"dianbo4\"><table><tr><td>&nbsp;&nbsp;您的称谓：<label><input class=\"dianbo51\" type=\"text\" name=\"unicast_from_name\" id=\"unicast_from_name\" /></label></td><td>TA的称谓：<label><input class=\"dianbo51\" type=\"text\" name=\"unicast_to_name\" id=\"unicast_to_name\" /></label></td></tr></table></td></tr><tr>";
	htmlCode+="<td class=\"dianbo4\">发送方式：<img align=\"absmiddle\" style=\"margin-left:20px;\" src=\"/images/unicast/youxiang.gif\" alt=\"邮 箱\" width=\"56\" height=\"18\" border=\"0\" /></td></tr><tr>";
	htmlCode+="<td class=\"dianbo4\">邮箱地址：<label><input class=\"dianbo5\" type=\"text\" name=\"unicast_email\" id=\"unicast_email\" /></label></td></tr><tr><td class=\"dianbo4\">信纸样式：</td></tr><td style=\"border-bottom:1px dashed #E1F4FD;\">";
	htmlCode+="<div style=\"width:448px; height:110px; margin:8px 0px; overflow-y:auto;\"><table class=\"dianbo7\" width=\"430\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
	//名称数组
	var c = new Array("温馨", "宁静", "典雅",'节日','情人节','春节'); 
	for(var i=1;i<=6;i++){
		htmlCode+="<td align=\"center\"><img src=\"/images/unicast/yangshi/"+i+".gif\" style=\"cursor:hand;\" width=\"81\" height=\"81\" border=\"1\" onclick=\"chanageMsgCss("+i+");\"/><br />"+c[i-1]+"</td>";
		if(i%4==0){
			htmlCode+="</tr><tr>";
		}
	}
	htmlCode+="</tr></table></div></td></tr><tr><td class=\"dianbo8\">点播祝语：</td></tr><tr><td align=\"center\"><label><textarea class=\"dianbo9\" name=\"unicastMsg\" id=\"unicastMsg\" cols=\"45\" rows=\"5\"></textarea></label></td>";
	htmlCode+="</tr><tr><td height=\"40\" align=\"center\"><label></label><input class=\"dianbo11\" type=\"submit\" name=\"button\" style='cursor:hand;' id=\"button\" value=\"确 定\" onclick=\"submitunicast();\"/></label></td></tr></table></div></div>";
	dwr.util.setValue("div_info", htmlCode, {escapeHtml:false});
	
	document.getElementById("unicast_l_code").value=l_code;
	document.getElementById("unicast_flag").value=flag;
	var x=window.event.clientX+document.documentElement.scrollLeft;
	var y=window.event.clientY+document.documentElement.scrollTop;
	var x_x=window.event.offsetX;
	var y_y=window.event.offsetY;
	if(flag=='2'){
		document.getElementById('div_info').style.top = y+(17-y_y)-20+"px";
		document.getElementById('div_info').style.left = x-x_x-458+"px";
	    document.getElementById('div_info').style.display="block";
	}else{
	Unicast.userIsLogin(function isLogin(str){
	if(str=='1'){
		document.getElementById('div_info').style.top = y+(17-y_y)-20+"px";
		document.getElementById('div_info').style.left = x-x_x-458+"px";
	    document.getElementById('div_info').style.display="block";
	}else{
		window.location.href="/kuke/Redirect?type=login&from="+the_url;
	}
	}
	);}
		
}

function closeUnicast(){
	document.getElementById('div_info').style.display="none";
	dwr.util.setValue("div_info", "", {escapeHtml:false});//移除对像
}

function submitunicast(){
    var opt=false;
    var message="以下内容不符合要求:\n";
	var email=document.getElementById("unicast_email").value;
	var from_name=document.getElementById("unicast_from_name").value;
	var to_name=document.getElementById("unicast_to_name").value;
	var unicast_l_code=document.getElementById("unicast_l_code").value;
	var unicast_msg_css=document.getElementById("unicast_msg_css").value;
	var unicastMsg=document.getElementById("unicastMsg").value;
	var flag=document.getElementById("unicast_flag").value;
	if(isNull(email)){
		message+="请填写邮箱!\n";		
		opt=true;
	}
	if(!unicastIsEmail(email)){
			message+="邮箱地址格式不正确!\n";
			opt=true;
	}
	if(isNull(unicastMsg)){
		message+="祝福语不能为空!\n";
		opt=true;
	}else if(unicastMsg.length>1000){
		message+="祝福语不能超过1000字符!\n";
		opt=true;
	}
	
	if(opt){
		alert(message);
	}else{
		if(flag=='2'){
			addUnicast("1","2");
		}else{
			Unicast.userIsLogin(userIsLogin);
		}
	}
}

function userIsLogin(str){
	if(str=='1'){
			Unicast.getUserType(userType);
		}else{
			window.location.href="/kuke/Redirect?type=login&from="+the_url;
		}
}

function userType(str){
	if(str=='99'||str=='98'){
		addUnicast("1","0");
	}else{
		Unicast.getUserSendNum(userRight);
	}
}

function userRight(str){
	if(str>0){
		confirms("<br /><br />你的免费次数还剩"+str+"次<br />此次将消费1次免费发送！<br /><br />","addUnicast('1','0')","closeUnicast()");
	}else{
		confirms("<br /><br />本次点播将扣除您1个库客币<br /><br /><br />","userIsMoney()","closeUnicast()");
	}
}

function userIsMoney(){
		Unicast.userMoney(addUnicastPay);
}

function addUnicastPay(str){
addUnicast(str,"1");
}

function addUnicast(str,flag){
	if(str=='1'){
			var email=document.getElementById("unicast_email").value;
			var from_name=document.getElementById("unicast_from_name").value;
			var to_name=document.getElementById("unicast_to_name").value;
			var unicast_l_code=document.getElementById("unicast_l_code").value;
			var unicast_msg_css=document.getElementById("unicast_msg_css").value;
			var unicastMsg=document.getElementById("unicastMsg").value;
			
			Unicast.addUnicast(unicast_l_code,from_name,to_name,email,unicast_msg_css,unicastMsg,flag,isSuccessUnicast);
		}else{//
				//跳转充值页面
			confirms("<br /><br />库客币不足，操作失败！<br /><span class='dianbo30'>请充值后再重新尝试。</span><br /><br />","hrefDeposit()","closeUnicast()");
		}			
	
}

function hrefDeposit(){
	window.location.href="/kuke/Redirect?type=pay&from="+document.location.href;
}
/**
*点播成功
*/
function isSuccessUnicast(str){
	if(str=="1"){
		confirms("<br /><br />本次点播已成功。<br /><span class=\"dianbo26\">您的朋友将收到来自KUKE的系统邮件！！<br />请提醒您的朋友及时查收！</span><br /><br />","closeUnicast()","closeUnicast()");
	}
}
/**
*校验邮件格式是否正确
*/
function unicastIsEmail(str){
	var res = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; 
	var re = new RegExp(res); 
	return !(str.match(re) == null); 
}

/**
*是否为空
*true 未通过,false通过
*/
function isNull(obj){
	var slen=getLength(obj);
	if(slen==0){
		return true;
	}else{
		return false;
	}
}

/**
*得到长度
*/
function getLength(str){
	var templen=str.length;
	if(navigator.appName=='Netscape') return templen;
		for(var i=0;i<str.length;i++){
		 var rstr=escape(str.substring(i,i+1)); 
		if (rstr.substring(0,2)=="%u"){ 
			 templen++;
		} 
	}
	return templen;
}
/**
* 改变背景样式
*/
function chanageMsgCss(css){
	document.getElementById("dianbodiv").style.backgroundImage="url(/images/unicast/youjian/"+css+".jpg)";
	document.getElementById("unicast_msg_css").value=css;
}


/**
* 提交用户选中的点播曲目
*/
function submitLcode(){
	var flag = 0;
	var l_code="";
	var length=0;
	try {
		if (document.all["l_code"].length) {
			for (var i = 0; i < document.all["l_code"].length; i++) {
				if (document.all["l_code"][i].checked) {
					l_code=document.all["l_code"][i].value;
					length++;
					flag = 1;
				}
			}
		} else {
			if (document.all["l_code"].checked) {
				flag = 1;
			}
		}
		if (flag == 0) {
			alert("请选中要点播的单曲");
			return false;
		} else {
			if(flag==1&&length==1){
				var l_code_arr=l_code.substring(l_code.indexOf("~")+1,l_code.length)
				showUnicast(l_code_arr,"1");
			}else{
				alert("暂时只支持单曲点播\n请重新选择要点播的曲目");
				return false;
			}
		}
	}
	catch (e) {
		alert("点播出错,请重新选择");
		return false;
	}
}