function showMessage(msg, status) {
	if (status) {
		$("#infoDiv").removeClass("TipsBox03");
		$("#infoDiv").addClass("TipsBox02");
	} else {
		$("#infoDiv").removeClass("TipsBox02");
		$("#infoDiv").addClass("TipsBox03");
	}
	$("#infoDiv span").empty();
	$("#infoDiv span").append(msg);
	$("#infoDiv").show();
	
	setTimeout("hideMessage()", 5000);
}

function hideMessage() {
	$("#infoDiv").hide();
}

function refreshVerifyCode() {
	$("#verifyCodeImg").attr("src", "/image.jsp?" + new Date().getTime());
}

function isValidEmail(content) {
	var patt = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
	return patt.test(content);
}

function isValidMobile(content) {
	var patt = /^0*(13|14|15|18)\d{9}$/;
	return patt.test(content);
}

function isValidPassword(content) {
	return jQuery.trim(content).length >= 6;
}

function isValidVerifyCode(content) {
	var patt = /^[\w]{4}$/;
	return patt.test(content);
}

function isValidDateTime(content) {
	var patt = /^(20\d{2}-((0[1-9]{1})|(1[0-2]{1}))-((0[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1}))){1}(\s\d{2}:\d{2}:\d{2})?$/;
	return patt.test(content);
}

function readFromCookie(name) {
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = decodeURI(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function isLogin() {
	//显示cookie中的用户
	var userId = readFromCookie("userid");
	var userName = readFromCookie("user");
	var flag = false;
	
	if (userId != "" && userName != "") {
		flag = true;
	}
	return flag;
}

function closeMessageDialog() {
	$("#messageDialog").dialog('destroy');
}

function showLoginDialog(title, value) {
	$("#messageDialog").attr("title", title);
	$("#messageDialog").append(value);

	$("#messageDialog").dialog({
		bgiframe: true,
		modal: true,
		buttons: {
			"取消": function() {
				closeMessageDialog();
			},
			"立即登录": function() {
				closeMessageDialog();
				window.location.href="http://www.hiknow.com/login.do";
			},								
			"免费注册": function() {
				closeMessageDialog();
				window.location.href="http://www.hiknow.com/register.jsp?sms";
			}
		},
		close: function() {
			closeMessageDialog();
		}
	});
}

function submitMessageDialog(title, value, url, requireLogin) {

	if (requireLogin) {

		if (!isLogin()) {
			showLoginDialog(title, value);
			return false;
		}
	}
	
	if (url != null && url != "") {
		$("#messageDialog").attr("title", title);
		$("#messageDialog").append("数据提交中,请等待...");
		
		$("#messageDialog").dialog({
			bgiframe: true,
			modal: true
		});
		$(".ui-dialog-titlebar").hide();	
		$.ajax({
		    type:"GET",
		    url: url,
		    timeout:20000,
		    success:function(data){
		    	$("#messageDialog").empty();
			    $("#messageDialog").append(data);
			    setTimeout("closeMessageDialog()", 3000);
		    },
		    error:function() {
			    $("#messageDialog").empty();
			    $("#messageDialog").append("数据提交失败,请重试!");		    
		    	setTimeout("closeMessageDialog()", 3000);
		    }		             
	    });		
	}
} 

function collectSession(sessionId) {
	var url = "http://www.hiknow.com/sessionCollection.do?method=addSessionCollection&sessionId=" + sessionId; 
	submitMessageDialog("收藏课程", "收藏课程请登录或者免费注册", url, true);
}

function progressBarOpen(content) {
	$("#progressBarInfo").empty();
	if (content == null || content == "") {
		$("#progressBarInfo").append("正在提交中，请稍侯...");
	} else {
		$("#progressBarInfo").append(content);
	}
	$("#progressBar").parent().find("div.ui-dialog-titlebar").hide();
	
	$("#progressBar").dialog("open");
}

function progressBarClose() {
	$("#progressImage").show();
	$("#progressBar").dialog("close");
}


function progressBarReadyClose(content) {
	$("#progressBarInfo").empty();
	$("#progressImage").hide();
	if (content == null || content == "") {
		setTimeout("progressBarClose()", 0);
	} else {
		$("#progressBarInfo").append(content);
		setTimeout("progressBarClose()", 2000);
	}
}

function showSmsDialog(title, url) {
	$("#smsDialog").attr("title", title);
	
	$("#smsDialog").dialog({
		bgiframe: true,
		modal: true,
		buttons: {
			"取消": function() {
				$("#smsDialog").dialog('destroy');
			},
			"发送短信提醒": function() {
				if (isValidMobile($("#telphone").val())) {			
					$("#smsDialog").dialog('destroy');
					
					progressBarOpen("");
			
					$.ajax({
					    type:"GET",
					    url: url + "&telphone=" + $("#telphone").val(), 
					    timeout:50000,
					    success:function(data){
							progressBarReadyClose(data);
					    }         
				    });
				} else {
					$("#smsInfoDiv").empty();
					$("#smsInfoDiv").append("您输入的手机格式有误,请重新输入!");
					$("#smsInfoDiv").show();
				}
			}
		},
		close: function() {
			$("#smsDialog").dialog('destroy');
		}
	});	
}

function smsRemember(sessionId) {
	if (!isLogin()) {
		showLoginDialog("登录提醒", "您尚未登录,您需要登录后才能发送上课短信提醒!");
		return false;
	}
	var url = "http://www.hiknow.com/sessionCollection.do?method=addSessionCollection&&smsNotify=1&sessionId=" + sessionId; 
	
	showSmsDialog("接受短信提醒的手机号码:", url)

}

function submitConsutation(){

	var realNameValue = document.getElementById("model.consultation.realName").value;
	if (realNameValue == "") {
		$("#messageDialog").attr("title", "请输入您的姓名");
		$("#messageDialog").empty();
		$("#messageDialog").append("请输入您的姓名,方便乐知客服可以准确的称呼您");
	
		$("#messageDialog").dialog({
			bgiframe: true,
			modal: true
		});
		setTimeout("closeMessageDialog()", 3000);
		return false;		
	}
	
	var contactValue = document.getElementById("model.consultation.contact").value;
	if (contactValue == "") {
		$("#messageDialog").attr("title", "请输入您的联系电话");
		$("#messageDialog").empty();
		$("#messageDialog").append("请输入您的联系电话，如果是外地电话请填写所在地区号");
		$("#messageDialog").dialog({
			bgiframe: true,
			modal: true
		});
		setTimeout("closeMessageDialog()", 3000);
		return false;		
	}
	
	var demanValue = document.getElementById("model.consultation.demand").value;
	if (demanValue == "") {
		$("#messageDialog").attr("title", "请输入您的需求!");
		$("#messageDialog").empty();
		$("#messageDialog").append("请输入您的需求，如果您的需求更加详细可以帮助乐知给您更加准确的建议!");
		$("#messageDialog").dialog({
			bgiframe: true,
			modal: true
		});
		setTimeout("closeMessageDialog()", 3000);
		return false;
	}		
	
	$.ajax({
	    type:"POST",
	    url:"/studycard/orderHelp.do",
	    data:{
	    	"model.consultation.realName": realNameValue,
	    	"model.consultation.contact": contactValue,
	    	"model.consultation.demand": demanValue
	    },  
	    timeout:20000,
	    success:function(data){
			$("#messageDialog").attr("title", "您的咨询");
			$("#messageDialog").empty();
			$("#messageDialog").append("您的咨询已经提交,乐知客服会尽快联系您!");
			$("#messageDialog").dialog({
				bgiframe: true,
				modal: true
			});
			setTimeout("closeMessageDialog()", 3000);    	
	    }         
    });
	
	
}

function initialMenu(){
	$("#manageMenu a").each(
		function() {
			var href = window.location.href;
			if (href.indexOf($(this).attr("href")) > -1) {
				$(this).parent().addClass("l01");
			}
		}
	);
	
	var userType = readCookieValue("usertype");
	if (userType == 1) {
		$("#teacherDiv").parent().show();
	}
}

function showNetEnterDiv() {

	var flag = isLogin();

	if (flag) {
		if ($("#netEnterDiv").length > 0) {
			$("#netEnterDiv").attr("title", "");
			$("#netEnterDiv").dialog({
				bgiframe: true,
				modal: true,
				resizable: false,
				draggable: false,
				height: 320,
				width: 700,
				close: function(event, ui) {
					$("#netEnterDiv").dialog("destroy");
				}
			});
			$("#nextPathLink").attr("href", "#");
		}
	} else {
		var href = window.location.href;
		window.location.href = "http://www.hiknow.com/login.do?prepath=" + escape(href);
	}
}

function switchDisplayDiv(parentId, chlidId, class1, class2) {
	$("#" + parentId).children().each(
		function () {
			var divId = $(this).attr("id")
			if (divId == chlidId) {
				$("#" + divId + "L").removeClass(class2);
				$("#" + divId + "L").addClass(class1);
				$("#" + divId).show();
			} else {
				$("#" + divId  + "L").removeClass(class1);
				$("#" + divId  + "L").addClass(class2);
				$("#" + divId).hide();
			}
		}
	);
}

function shareKaixin001() {
	var link = "http://www.kaixin001.com/repaste/share.php?rtitle="+encodeURIComponent(document.title);
	link += "&rurl="+encodeURIComponent(document.location.href);
	link += "&rcontent=" + encodeURIComponent(document.title + "课程地址: " + document.location.href);
	window.open(link);
}

function shareRenRen() {
	var link = "http://share.renren.com/share/buttonshare.do?title="+encodeURIComponent(document.title);
	link += "&link="+encodeURIComponent(document.location.href);
	window.open(link);
}

function shareSina() {
	var link = "http://v.t.sina.com.cn/share/share.php?title="+encodeURIComponent(document.title.substring(0,76));
	link += "&url="+encodeURIComponent(document.location.href);
	window.open(link);
}

//function queryISPType(ip) {
//	var ipaddress, address, isp;
//	
//	var ipqueryURL = "http://www.hiknow.com/ipquery.html";
//	
//	if (ip != "") {
//		ipqueryURL += "?ipaddress=" + ip;
//	}
//	
//	$.ajax({   
//        url: ipqueryURL,   
//        
//        success: function(data) {  
//				var ipaddress, address, isp;
//
//				$(data).find("p.item_list").each(
//					function() {
//						text = $(this).text();
//						if (text.indexOf("IP") > -1){
//							ipaddress = $(this).parent().parent().find("input[name='ipaddress']").val();	
//						} else if (text.indexOf("所属省份") > -1) {
//							address = $(this).parent().next().text();	
//						} else if (text.indexOf("所属运营商") > -1) {
//							isp = $(this).parent().next().text();	
//						} 
//					}
//				);
//
//				settingCurrentISPType(ipaddress, address, isp);
//        }   
//    }); 
//	
//
//} 

function settingCurrentISPType(ipaddress, address, isp) {
	if (ipaddress != "" && isp != "") {
		var netInfo = "您当前IP地址 " + ipaddress + "," + address + " " + isp + " 用户,";
		
		if (isp == "中国电信") {
			netInfo += "乐知建议您选择电信宽带用户入口进入听课。"
			$("#ctlP").attr("style", "position:absolute;top:120px;left:30px;");
			$("#cncP").attr("style", "position:absolute;top:170px;left:30px;");
			$("#cncP a").text("点此链接选择" + $("#cncP img").attr("alt"));
			$("#cncP img").remove();	
		} else if (isp == "中国网通") {
			netInfo += "乐知建议您选择联通网通入口进入听课。"
			$("#cncP").attr("style", "position:absolute;top:120px;left:30px;");
			$("#ctlP").attr("style", "position:absolute;top:170px;left:30px;");
			$("#ctlP a").text("点此链接选择" + $("#ctlP img").attr("alt"));
			$("#ctlP img").remove();
		} else {
			netInfo += "您可以分别2个入口分别进入对比,选择适合的。";
		}
		
		$("#recommendNetInfo > span").text(netInfo);
		$("#defaultNetInfo").hide();
		$("#recommendNetInfo").show();
	}
}
