var xmlHttp;

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

function refreshCount() {
   document.getElementById("count").value="";    
    var url = "SelectTrainingCount.aspx" ;              
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange;	
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

    
function handleStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
            updateCount();
        }
    }
}

function updateCount() {
//获取返回值的xml
    var results=xmlHttp.responseXML.getElementsByTagName("Count");
    document.getElementById("count").innerText="该文章被阅读"+results[0].firstChild.nodeValue+"次";
}

//新模式页面计数器
function refreshModeCount() {
   document.getElementById("count").value="";    
    var url = "RzhhMode.aspx" ;              
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateModeChange;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

    
function handleStateModeChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
            updateModeCount();
        }
    }
}

function updateModeCount() {
//获取返回值的xml
    var results=xmlHttp.responseXML.getElementsByTagName("Count");
    document.getElementById("count").innerText="该文章被阅读"+results[0].firstChild.nodeValue+"次";
}

//以下是用户咨询页面处理
//提交
//
function userInput()
{
	if(checkInput()==true)
	{
		var url = "UserConsultation.aspx?name="+encodeURI(document.getElementById("txtName").value)+"&pho="+document.getElementById("txtPho").value+"&mail="+document.getElementById("txtMail").value+"&que="+encodeURI(trim(document.getElementById("txtQue").value)); 
		createXMLHttpRequest() ;
		xmlHttp.onreadystatechange = userQueModeChange;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
		
	}
}

function userQueModeChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
            insertUserQue();
        }
    }
    }
    
    //获取插入返回值,如果返回值是0k则插入成功
function insertUserQue()
{
	var results=xmlHttp.responseXML.text;
	if(results=="OK")
	{
		window.alert("提交成功!");
	}
	else
	{
		window.alert("对不起,由于网络原因提交失败!请将咨询问题发送至:bjrzhh@126.com");
	}
}


//去左空格; 
function ltrim(s){ 
return s.replace( /^\s*/, ""); 
} 
//去右空格; 
function rtrim(s){ 
return s.replace( /\s*$/, ""); 
} 
//去左右空格; 
function trim(s){ 
return rtrim(ltrim(s)); 
}

//提交问题检查输入是否正确
function checkInput()
{
    if(!checkPhoneNumber(document.all("txtPho").value))
    {
		return;
    }
    else if(!checkMail(document.all("txtMail").value))
    {
		return;
    }
	else if(document.getElementById("txtName").value=="")
	{
		window.alert("姓名不能为空");
		return false;
	}
	else if(document.getElementById("txtPho").value=="")
	{
		window.alert("电话不能为空");
		return false;
	}
	else if(document.getElementById("txtMail").value=="")
	{
		window.alert("邮箱不能为空");
		return false;
	}
	else if(trim(document.getElementById("txtQue").value)=="")
	{
		window.alert("问题不能为空");
		return false;
	}
	else if(document.getElementById("txtQue").value.length>300)
	{
		window.alert("问题不能大于300字");
		return false;
	}
	
	else
	{
		return true;
	}
	
	
}

//使用正则表达式验证电话号码都为数字

function checkPhoneNumber(num)
{
	var mode=/^\d{1,}$/;
	if(!mode.test(num))
	{
		alert("电话号码必须是数字!");
		return false;
	}
	else if(num.length<7)
	{
		alert("电话号码格式不对,格式为:'区号电话号码'");
		return false;
	}
	return true;
}
//验证邮箱

function checkMail(num)
{
	var mode=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if(!mode.test(num))
	{
		alert("邮箱地址不正确!");
		return false;
	}
	return true;
	
	
}


