﻿// JavaScript 文件

var webSiteUrl = "/";

//正则表达式验证
var emailReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/; 
var userNameReg = /^[a-z0-9A-Z-_.]+$/;
var verifyReg = /^[0-9]{4,4}$/;

//获取当前栏目ID
function getCurrentSubjectID()
{
    return document.getElementById("CurrentSubjectID").value.split('|')[0];
}

//获取当前频道Ajax页面所在目录
function getCurrentAjaxPosition()
{
    var info = document.getElementById("CurrentSubjectID");
    if(info)
    {
        info = info.value.split('|');
        if(info.length > 1)
        {
            return info[1];
        }
        else
        {
            return "../common/";
        }
    }
    else
    {
        return "../common/";
    }
}

//页面Div加载innerHTML
function setDivHtml(divIDs, subjectID)
{
    if(divIDs.length > 0)
    {
        var divID = divIDs.split(',');
        for(var i=0; i<divID.length; i++)
        {
            if(document.getElementById(divID[i]) != null)
            {
                AjaxRequest(getCurrentAjaxPosition() + "LoadPage.aspx?DivName=" + divID[i] + "&SubjectID=" + subjectID, true, "GET", null, setDivHtmlProc, null, divID[i]);
            }
        }
    }
}

//页面加载处理方法
function setDivHtmlProc(divId, divHtml)
{

     document.getElementById(divId).innerHTML = divHtml;

    if(divId == "login")
    {
        initUser();
    }

}

//更换验证码
function ChangeImg()
{
    document.getElementById("imgVerify").src = webSiteUrl + "Register/Verify.aspx?rnd=" + Math.random();
}

//用户登录
function login()
{
    var userName = document.getElementById("username");
    if(userName.value == "")
    {
        alert("请输入用户名!");
        userName.focus();
        return;
    }
    if(!userNameReg.test(userName.value))
    {
        alert("用户名只能是字母、数字和下划线!");
        userName.select();
        return;
    }
    var userPwd = document.getElementById("password");
    if(userPwd.value == "")
    {
        alert("请输入密码!");
        userPwd.focus();
        return;
    }
    var verify = document.getElementById("verify");
    if(!verifyReg.test(verify.value))
    {
        alert("请输入合法的验证码!");
        ChangeImg();
        verify.select();
        return;
    }
    var data = "Action=Login&UserName=" + userName.value + "&UserPwd=" + userPwd.value + "&Verify=" + verify.value;
    AjaxRequest(getCurrentAjaxPosition() + "Users.aspx", true, "POST", data, userInfoProc, null, null, '');
}

//用户注销
function logout()
{
    var data = "Action=Logout";
    AjaxRequest(getCurrentAjaxPosition() + "Users.aspx", true, "POST", data, userInfoProc, null, null, '');
}

//用户版块初始化
function initUser()
{
    var data = "Action=Init";
    AjaxRequest(getCurrentAjaxPosition() + "Users.aspx", true, "POST", data, userInfoProc);
}

//用户信息处理方法
function userInfoProc(info)
{
    if(info == "Verify Error")
    {
        alert("验证码错误!");
        document.getElementById("verify").select();
        ChangeImg();
    }
    else if(info == "DataBase Updating")
    {
        alert("正在更新数据库, 请稍候再试!");
    }
    else if(info == "UserName or UserPwd Error")
    {
        alert("登录失败, 请输入正确的用户名和密码!");
    }
    else if(info == "Verify Lost")
    {
        alert("验证码超时, 请重新登录!");
        ChangeImg();
    }
    else if(info == "Fail to Login")
    {
        alert("登录失败!");
    }
    else if(info == "Logout Success")
    {
        alert("注销成功!");
        setHomePageLoginHtml(getCurrentSubjectID());
    }
    else if(info == "No User")
    {

    }
    else
    {
        var divhtml = "<h5>会员信息</h5>"
                    + "<form><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>"
                    + "<div style=\"height:70px;\">"
                    + info
                    + "</div>"
                    + "</td></tr>"
                    + "<tr><td><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
                    + "<tr><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>"
                    + "<tr><td>&nbsp;&nbsp;&nbsp;</td><td><input id='btnLogout' class='button_small' name=\"btnLogout\" type=\"button\" value=\"&nbsp;\" onclick=\"logout();\" /></td>"
                    + "<td><input id='btnUserInfo' class='button_large' name=\"btnUserInfo\" type=\"button\" value=\"&nbsp;\" onclick=\"userPannel();\" /></td>"
                    + "</table></form>";
        document.getElementById("login").innerHTML = divhtml;
    }
}
//用户控制面板
function userPannel()
{
    var userType = document.getElementById('userType').value;
    if('1' == userType)
    {
        window.open("MainForm/member_pers/persmember.aspx");
    }
    else
    {
        window.open("MainForm/member_corp/corpmember.aspx");
    }
}
//处理首页注销后的界面
function setHomePageLoginHtml(subjectID)
{
    AjaxRequest(getCurrentAjaxPosition() + "LoadPage.aspx?DivName=login&SubjectID=" + subjectID, true, "GET", null, setDivHtmlProc, null, "login");
}

//产品搜索
function searchProd()
{
    var type = document.getElementById("prodType").value;
    var brand = document.getElementById("prodBrand").value;
    var name = document.getElementById("prodName").value;
    window.open(webSiteUrl + "MainForm/product/products.aspx?ID=0&TypeID=" + type + "&BrandID=" + brand + "&Name=" + encodeURI(name), "_blank");
}

//机构搜索
function searchOrg()
{
    var type = document.getElementById("orgType").value;
    var address = document.getElementById("orgAddress").value;
    var name = document.getElementById("orgName").value;
    window.open(webSiteUrl + "MainForm/organ/orgs.aspx?ID=0&TypeID=" + type + "&Address=" + encodeURI(address) + "&Name=" + encodeURI(name), "_blank");
}

//机构按地区搜索
function orgArea(province)
{
    window.open(webSiteUrl + "MainForm/organ/orgs.aspx?ID=0&Address=" + encodeURI(province), "_blank");
}

//提交调查
function Vote(themeID)
{
    var opt = null;
    if(!themeID)
    {
        opt = document.getElementsByName('questOption');
    }
    else
    {
        opt = document.getElementsByName('questOption' + themeID);
    }
    var items = '';
    var options = '';
    for(var i=0; i<opt.length; i++)
    {
        if(opt[i].checked)
        {
            items += opt[i].value + ",";
        }
        options += opt[i].value + ",";
    }
    items = items.substring(0, items.length - 1);
    options = options.substring(0, options.length - 1);
    if(items != "")
    {
        var url = "";
        url = getCurrentAjaxPosition() + "Ajax.aspx?Action=vote&items=" + items + "&options=" + options;
        if(!themeID)
        {
            AjaxRequest(url, true, "GET", null, VoteProc, null, null, '');
        }
        else
        {
            AjaxRequest(url, true, "GET", null, VoteProc1, null, themeID, '');
        }
    }
    else
    {
        alert("请先选择选项!");
    }
}

//调查处理方法(for 其他页面)
function VoteProc(result)
{
    if(result == "false")
    {
        alert("抱歉，投票失败，请稍候再试！");
    }
    else if(result == "Time Out")
    {   
        alert("请不要重复投票!");
    }
    else
    {
        alert("投票成功，感谢您的支持！");
    }
}

//调查处理方法(for 调查首页)
function VoteProc1(themeID, result)
{
    if(result == "false")
    {
        alert("抱歉，投票失败，请稍候再试！");
    }
    else if(result == "Time Out")
    {   
        alert("同一调查题目重复投票间隔至少10分钟!");
    }
    else
    {
        alert("投票成功，感谢您的支持！");
        var res = result.split(',');
        var totalNum = 0;
        var radio = 0.0;
        var img = "", imgHtml = "", indexHtml = "";
        for(var i=0; i<res.length; i++)
        {
            totalNum += parseInt(res[i].toString());
        }
        document.getElementById("totalNum" + themeID).innerHTML = "总投票数:" + totalNum;
        for(i=0; i<res.length; i++)
        {
            radio = 100 * parseFloat(res[i].toString()) / totalNum;
            if(radio > 0)
            {
                img = "<img width=\"10\" height=\""
                     + parseInt(radio.toString())
                     + "\" src=\"images/bar.gif\" />";
            }
            else
            {
                img = "";
            }
            radio = radio.toString() + ".00";
            imgHtml += "<td align=\"center\" valign=\"bottom\">"
                     + radio.substring(0, radio.indexOf('.') + 3)
                     + "%<br />"
                     + img 
                     + "</td>";
            indexHtml += "<td align=\"center\">(" 
                       + (i + 1).toString() 
                       + ")</td>";
        }
        document.getElementById("divChart" + themeID).innerHTML = "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border:0px;\"><tr height=\"138\"><td width=\"30\">&nbsp;</td>" 
                   + imgHtml + "<td width=\"15\">&nbsp;</td></tr><tr height=\"22\"><td width=\"30\">&nbsp;</td>"
                   + indexHtml + "<td width=\"15\">&nbsp;</td></tr></table>";
        
    }
}

//查看调查结果
function viewVoteResult()
{
    var theme = document.getElementById('questThemeID');
    if(theme != null)
    {
        var left = window.screenLeft + document.body.clientWidth / 2 - 150;
        var top = window.screen.height / 2 - 150;
        if(!document.all)
        {
            left = window.screen.width / 2 - 150;
            top = window.screen.height / 2 - 120;
        }
        window.open(webSiteUrl + "MainForm/vote/voteResult.aspx?ID=" + theme.value, null, "left=" + left + ",top=" + top + ",height=240,width=300,status=no,toolbar=no,menubar=no,location=no"); 
    }
}

//订阅GIS时代周刊
function Buy(isBuy)
{
    var email = document.getElementById("mail").value;
    if(email != "")
    {
        if(emailReg.test(email))
        {
            var action = "";
            var url = "";
            var result = "false";
            if("1" == isBuy)
            {
                action = "buy";
            }
            else if("0" == isBuy)
            {
                action = "cancelbuy";
            }
            url = getCurrentAjaxPosition() + "Ajax.aspx?Email=" + email + "&Action=" + action;
            AjaxRequest(url, true, "GET", null, BuyProc, null, isBuy, '');
        }
        else
        {
            alert("请输入正确的邮箱地址！");
        }
    }
    else
    {
        alert("请输入邮箱地址！");
    }
}

//订阅GIS时代周刊处理方法
function BuyProc(isBuy, result)
{
    var info = "";
    if("1" == isBuy)
    {
        info = "订阅";
    }
    else if("0" == isBuy)
    {
        info = "退订";
    }
    if(result == "true")
    {
        alert(info +"成功，感谢您的支持！");
    }
    else if(result == "false")
    {
        alert("抱歉，" + info + "失败，请稍候再试！");
    }
    else
    {
        alert("该邮箱没有订阅过GIS周刊！");
    }
}

//NCIE 证书查询
function ncieSearch(cardId, pwdId)
{
    var idObj = document.getElementById(cardId);
    var pwdObj = document.getElementById(pwdId);
    if(idObj.value == '')
    {
        alert("请输入证件号码!!");
        idObj.focus();
    }
    else if(pwdObj.value == '')
    {
        alert("请输入密码!!");
        pwdObj.focus();
    }
    else
    {
        var url = getCurrentAjaxPosition() + "Ajax.aspx";
        var data = "ID=" + idObj.value + "&Password=" + pwdObj.value + "&Action=NcieSearch";
        AjaxRequest(url, true, "POST", data, ncieSearchProc, null, null, '');
    }
}

//NCIE 证书查询处理方法
function ncieSearchProc(result)
{
    if(result == "No User")
    {
        alert("用户不存在");
    }
    else if(result == "Password Error")
    {
        alert("密码错误");
    }
    else if(result == "True")
    {
        window.open("../Ncie/Socre.aspx", "_blank");
    }
}

//广告点击次数增一
function adClickTimesUp(adID)
{
    var url = getCurrentAjaxPosition() + "Ajax.aspx?AdID=" + adID + "&Action=AdClick";
    AjaxRequest(url, true, "GET");
}

//-----------------------------------------职位搜索----------------------------------------------//
//执行查询
function searchInvit(flg)
{
    var invitType = document.getElementById("workType");
    if(invitType.value != "0")
    {
        invitType = invitType.options[invitType.options.selectedIndex].text;
    }
    else
    {
        invitType = "";
    }
    var invitPost = document.getElementById("workPost");
    if(invitPost.value != "0")
    {
        invitPost = invitPost.options[invitPost.options.selectedIndex].text;
    }
    else
    {
        invitPost = "";
    }
    var provinceID = document.getElementById("province").value;
    var cityID = document.getElementById("city").value;
    var publish = document.getElementById("publishDate").value;
    var type = document.getElementById("corpType").checked ? 1 : 2;
    var keyword = document.getElementById("key").value;
    if(keyword == "公司/ 职位关键词")
    {
        keyword = "";
    }
    keyword = keyword.replace(/[\s;|]/g, ',').replace('，', ',').replace('；', ',');

    var target = "_blank";
    if(1 == flg)
    {
        target = "_self";
    }
    else if(2 == flg)
    {
        target = "_blank";
    }
    window.open(webSiteUrl + "MainForm/job/jobs.aspx?WorkType=" + encodeURI(invitType) + "&WorkPost=" + encodeURI(invitPost) + "&ProvinceID=" + provinceID + "&CityID=" + cityID + "&PublishDate=" + publish + "&Keyword=" + encodeURI(keyword) + "&Type=" + type + "&Rnd=" + Math.random(), target);
}

//在结果中搜索
function searchInResult()
{
    var keyword = document.getElementById("anotherKey").value;
    var flg = document.getElementById("summary").checked;
    var url = document.location.toString();
    var index = url.indexOf("Flag");
    if(index > 0)
    {
        url = url.substring(0, index - 1);
    }
    else
    {
        index = url.indexOf("PageID");
        if(index > 0)
        {
            url = url.substring(0, index - 1);
        }
    }
    if(keyword != '')
    {
        keyword = "&Keyword=" + encodeURI(keyword);
    }
    window.open(url + keyword + "&Flag=" + flg + "&Rnd1=" + Math.random(), "_self");
}

//清楚输入框内容
function clearValue(iptObj)
{
    if(iptObj.value == '公司/ 职位关键词')
    {
        iptObj.value = '';
    }
}

//选择职位
function changePost(select, subSelect)
{
    var objSelect    = document.getElementById(select);
    var subObjSelect = document.getElementById(subSelect);
    if (objSelect && subObjSelect)
    {
        var typeValue = objSelect.value.substring(0, 2);
        if (typeValue == "0")
        {
            subObjSelect.length=0;
            subObjSelect.options[0]= new Option("请选择", "0");
        }
        else
        {
            subObjSelect.length=0;
            var subCategory = document.getElementById("postCode").value.split(";");
            subObjSelect.options[0]= new Option("请选择", "0");
            var index = 1;
            for (var i=0; i<subCategory.length; i++ )
            {
                var post = subCategory[i].split(",");
                if (post[0].substring(0, 2) == typeValue && post[0].substring(2, 4) != "00" )
                {
                    subObjSelect.options[index] = new Option(post[1], post[0]);
                    index++;
                }
            }
        }
    }
}

//选择城市
function changeCity(select, subSelect)
{
    var objSelect    = document.getElementById(select);
    var subObjSelect = document.getElementById(subSelect);
    if (objSelect && subObjSelect)
    {
        var typeValue = objSelect.value;
        if (typeValue == "0")
        {
            subObjSelect.length=0;
            subObjSelect.options[0]= new Option("请选择", "0");
        }
        else
        {
            subObjSelect.length=0;
            var subCategory = document.getElementById("cityCode").value.split(";");
//            subObjSelect.options[0]= new Option(objSelect.options[objSelect.options.selectedIndex].text, objSelect.options[objSelect.options.selectedIndex].value);
            subObjSelect.options[0]= new Option("请选择", "0");
            var index = 1;
            for (var i=0; i<subCategory.length; i++ )
            {
                var post = subCategory[i].split(",");
                if (post[0] == typeValue)
                {
                    subObjSelect.options[index] = new Option(post[2], post[1]);
                    index++;
                }
            }
        }
    }
}
//---------------------------------------------Ajax--------------------------------------------------//
//创建XMLHttpRequest对象
function createXmlHttpRequest()
{
    var xmlHttp;
    if(window.ActiveXObject)
    {
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(exp)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    else if(window.XMLHttpRequest)
    {
        xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}

//Ajax请求，返回请求结果
function AjaxRequest(url, isAjax, method, data, callback, charSet, parm, stateInfo)
{
    if(stateInfo != null)
    {
        showAjaxState(stateInfo);
    }
    var xmlHttp = createXmlHttpRequest();
    var result;
	xmlHttp.onreadystatechange = function()	
	{
		if(xmlHttp.readyState == 4)		
		{
			if(xmlHttp.status == 200)
			{
				hideAjaxState();
				if(xmlHttp.responseXML != null && xmlHttp.responseXML.xml != null && xmlHttp.responseXML.xml != '')
				{
					result = xmlHttp.responseXML ;
				}
				else 
				{
					result = xmlHttp.responseText ;
			    }
			    if(isAjax && typeof(callback)=='function')
			    {
			        if(parm != null)
			        {
			            callback(parm, result);
			        }
			        else
			        {
				        callback(result);
				    }
				}
			}
			xmlHttp = null;
		}
	}
	if(url.indexOf("?")>0)
	{
		url += "&random =" +Math.random() ;
	}
	else
	{
		url += "?&random =" +Math.random() ;
	}
	xmlHttp.open(method, url, isAjax);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	var charset = charSet?charSet:"gb2312";
	xmlHttp.setRequestHeader("CharSet",charset);
	xmlHttp.send(data);
	try
	{
        if(!isAjax)
	    {
	        var res = result||xmlHttp.responseText||xmlHttp.responseXML;
	        if(typeof(callback)=='function')
	        {
		        if(parm != null)
		        {
		            callback(parm, result);
		        }
		        else
		        {
			        callback(result);
			    }
	        }
	        hideAjaxState();
	        return res;
	    }
	}
	catch(e)
	{
	}
	if(typeof(callback) != 'function')
	{
	    hideAjaxState();
        return result;
    }
}

//Ajax显示"正在处理"之类的提示信息
function showAjaxState(stateInfo)
{
    var stateDiv = top.document.getElementById('ajaxState');
    if(!stateDiv)
    {
        stateDiv = createDiv(top.document.body, 'ajaxState', 100, 100, null, '1000', "hidden", "background-color:#D4D0C8; border:1px solid blue;", "");
    }
    
    stateDiv.style.left = '50%';
    stateDiv.style.top = '50%';
    stateDiv.style.marginLeft = "-85px" ;
    stateDiv.style.marginTop = -80 + top.document.documentElement.scrollTop + "px";
    
    stateDiv.innerHTML = (stateInfo ? stateInfo + "<br>" : "") + "<img src='" + webSiteUrl + "MainForm/images/state.gif'/>";
    stateDiv.style.visibility = "visible";    
}

//隐藏Ajax状态条
function hideAjaxState()
{
    var stateDiv = top.document.getElementById('ajaxState');
    if(stateDiv)
    {
        stateDiv.style.visibility = "hidden";
    }
}

//创建Div
function createDiv(pElement,id,left,top,position,zIndex,visibility,style,content)
{
    this.obj = window.top.document.createElement("div");
    this.obj.id = id ? id : ("infoDiv" + Math.random());
    this.obj.name = id ? id : ("infoDiv" + Math.random());  
    this.obj.style.left = parseInt(left) + "px";
    this.obj.style.top = parseInt(top) + "px";
    this.obj.style.position = position ? position : "absolute";
    this.obj.unselectable = "on";
    if(zIndex) this.obj.style.zIndex = zIndex;
    if(visibility)this.obj.style.visibility = visibility; 
    if(style)this.obj.style.cssText = this.obj.style.cssText + ";" + style;
    if(content)this.obj.innerHTML = content;
    if(pElement != null)
    {
        pElement.appendChild(this.obj);    
    }
    return this.obj; 
}

//图片上传
function upload(size, type, file, img, save, path)
{
    var objFile = document.getElementById(file);
    if(objFile.value)
    {
        var i = objFile.value.lastIndexOf('.');
        if(i > 0)
        {
            var fileType = objFile.value.substring(i + 1, objFile.value.length).toLowerCase();
            var typeReg = '/' + type + '/.test(fileType)';
            if(eval(typeReg))
            {
                if(!save)
                {
                    save = '';
                }
                if(!size)
                {
                    size = '0';
                }
                var url = path;
                if(!url)
                {
                    url = "../common/Users.aspx";
                }
                var data = "Action=UploadFile&FilePath=" + objFile.value + "&ServerPath=" + save + "&Size=" + size;
                AjaxRequest(url, true, "POST", data, uploadProc, null, img + '|' + size + '|' + path, "");
            }
            else
            {
                alert("选择的图片格式不正确!");
            }
        }
        else
        {
            alert("选择的图片格式不正确!");
        }
    }
    else
    {
        alert("请先从本地选择图片!");
    }
}
function uploadProc(img_size, result)
{
    var parm = img_size.split('|');
    if(result == 'False')
    {
        alert("图片上传失败, 请选择正确格式的图片!");
    }
    else if(result == 'Larger')
    {
        alert('上传的图片太大，不能超过' + parm[1] + 'K！');
    }
    else
    {
        if(parm[2] == undefined || parm[2] == null || parm[2] == '')
        {
            alert(parm[2]);
            alert(img_size);
            document.getElementById(parm[0]).style.display = '';
        }
        document.getElementById(parm[0]).src = webSiteUrl + result.substring(result.indexOf("UploadFile/"), result.length);
    }
}

//GIS时代 首页中，pdf下载
function setGISPDFProc(action,res)
{
    if(action=="gispdf")
    {
        if(res!="")
        {
            document.getElementById("pdf").innerHTML = res;
        }
        else if(res="false")
        {
            alert("查找失败！请稍候再试！");
        }
        else
        {}
    }
}


