﻿var arrCon = new Array();
var arrPro = new Array();
var arrCity = new Array();

function getcity() {
    ajax("/handler/city.ashx?com=get", "getcityCallback");
}
function getcityCallback(res) {
    var arrTmp = res.split("\n");
    arrCon = arrTmp[0].split("|");
    arrPro = arrTmp[1].split("|");
    arrCity = arrTmp[2].split("|");
    loadPageControl();
}

function loadPageControl() {
    var objCountry = $("selLocalityCountry");
    var objCountry2 = $("selNativeplaceCountry");
    objCountry.options.length = 0;
    objCountry2.options.length = 0;

    for (i = 0; i < arrCon.length; i++) {
        if (arrCon[i] == "") continue;
        var tmpArray = new Array();
        tmpArray = arrCon[i].split(",");
        addSelectItem(objCountry, tmpArray[1], tmpArray[0]);
        addSelectItem(objCountry2, tmpArray[1], tmpArray[0]);
    }

    addSelectItem(objCountry, "不限", "0");
    addSelectItem(objCountry2, "不限", "0");

    objCountry.onchange = function(e) { objCountry_OnChange(this); };
    objCountry2.onchange = function(e) { objCountry_OnChange(this); };

    objCountry.selectedIndex = objCountry.options.length - 1;
    objCountry2.selectedIndex = objCountry2.options.length - 1;
    objCountry_OnChange(objCountry);
    objCountry_OnChange(objCountry2);

}

function objCountry_OnChange(obj) {
    var objCity = $(obj.id + "selProvince" + "selCity");
    if (objCity) obj.parentNode.removeChild(objCity);

    var id = obj.options[obj.selectedIndex].value;

    var hasP = false;
    var objPro = $(obj.id + "selProvince");
    if (!objPro) {
        objPro = document.createElement("select");
        objPro.id = obj.id + "selProvince";
        objPro.className = "select_2";
        obj.parentNode.appendChild(objPro);
        objPro.onchange = function(e) { objPro_OnChange(this); };
    }
    objPro.options.length = 0;

    addSelectItem(objPro, "不限", "0");
    for (i = 0; i < arrPro.length; i++) {
        var tmpArray = new Array();
        tmpArray = arrPro[i].split(",");
        if (tmpArray[1] == id) {
            addSelectItem(objPro, tmpArray[2], tmpArray[0]);
            hasP = true;
        }
    }
    if (!hasP) obj.parentNode.removeChild(objPro);
    else objPro_OnChange(objPro);


}

function objPro_OnChange(obj) {
    var id = obj.options[obj.selectedIndex].value;

    var hasP = false;
    var objCity = $(obj.id + "selCity");
    if (!objCity) {
        objCity = document.createElement("select");
        objCity.id = obj.id + "selCity";
        objCity.className = "select_3";
        obj.parentNode.appendChild(objCity);
        objCity.onchange = function(e) { objCity_OnChange(this); };
    }
    objCity.options.length = 0;

    addSelectItem(objCity, "不限", "0");
    for (i = 0; i < arrCity.length; i++) {
        var tmpArray = new Array();
        tmpArray = arrCity[i].split(",");
        if (tmpArray[1] == id) {
            addSelectItem(objCity, tmpArray[2], tmpArray[0]);
            hasP = true;
        }
    }
    if (!hasP) obj.parentNode.removeChild(objCity);

}

function objCity_OnChange(obj) {
    var id = obj.options[obj.selectedIndex].value;
}

function addSelectItem(objSelect, text, value) {
    var item = new Option(text, value);
    objSelect.options.add(item);
}

function FormIni() {
    getcity();
    $("btnSearch").onclick = function(e) { search(); }
    $("btnSimpleSearch").onclick = function(e) { simpleSearch(); }

    $("simSearch").onkeydown = function(e) {
        var theEvent = window.event || e;
        var code = theEvent.keyCode || theEvent.which;
        if (code == 13) {
            ObjClick($("btnSimpleSearch"));
        }
    };
    $("higSearch").onkeydown = function(e) {
        var theEvent = window.event || e;
        var code = theEvent.keyCode || theEvent.which;
        if (code == 13) {
            ObjClick($("btnSearch"));
        }
    };
}

function simpleSearch() {
    var objFormat = document.createElement("textarea");
    function formatstr(str) {
        objFormat.value = str;
        return objFormat.innerHTML;
    }

    var nickName = $("txtNickName").value.Trim();
    var userID = $("txtUserID").value.Trim();
    var z = "";
    var para = "";
    if (nickName != "") {
        para += "&nickname=" + escape(formatstr(nickName));
        z += ",昵称:" + nickName;
    }
    if (userID != "") {
        if (!IsUInt(userID)) {
            alert("请输入正确的会员ID");
            $("txtUserID").focus();
            return;
        }
        para += "&userid=" + escape(formatstr(userID));
        z += ",会员ID:" + userID;
    }
    if (z!="") z=z.substr(1);
    if (para == "")
        alert("请先填入搜索条件");
    else
        window.location.href = ("searchinfo.aspx?page=1" + para + "&z=" + escape(z));
}

function search() {
    var sex = ($("radioMan").checked ? 0 : 1); //性别

    var lcountry = 0; //所在地国家
    var lprovince = 0; //所在地省
    var lcity = 0; //所在地市
    if ($("selLocalityCountry").value != "")
        lcountry = $("selLocalityCountry").options[$("selLocalityCountry").selectedIndex].value;
    if ($("selLocalityCountry" + "selProvince"))
        if ($("selLocalityCountry" + "selProvince").value != "")
        lprovince = $("selLocalityCountry" + "selProvince").options[$("selLocalityCountry" + "selProvince").selectedIndex].value;
    if ($("selLocalityCountry" + "selProvince" + "selCity"))
        if ($("selLocalityCountry" + "selProvince" + "selCity").value != "")
        lcity = $("selLocalityCountry" + "selProvince" + "selCity").options[$("selLocalityCountry" + "selProvince" + "selCity").selectedIndex].value;
    var age1 = $("selAge1").options[$("selAge1").selectedIndex].value//年龄1
    var age2 = $("selAge2").options[$("selAge2").selectedIndex].value//年龄2
    var height1 = $("selHeight1").options[$("selHeight1").selectedIndex].value//身高1
    var height2 = $("selHeight2").options[$("selHeight2").selectedIndex].value//身高2
    var education = $("selEducation").options[$("selEducation").selectedIndex].value; //学历
    var salary = $("selSalary").options[$("selSalary").selectedIndex].value; //月薪
    var personalproperty = $("selPersonalproperty").options[$("selPersonalproperty").selectedIndex].value; //个人资产
    var civilstate = $("selCivilstate").options[$("selCivilstate").selectedIndex].value; //婚姻状况
    var childstate = $("selChildstate").options[$("selChildstate").selectedIndex].value; //子女状况
    var profession = $("selProfession").options[$("selProfession").selectedIndex].value; //职业
    var companytype = $("selCompanytype").options[$("selCompanytype").selectedIndex].value; //公司类型
    var house = $("selHouse").options[$("selHouse").selectedIndex].value; //住房
    var car = $("selCar").options[$("selCar").selectedIndex].value; //购车

    var ncountry = 0; //籍贯国家
    var nprovince = 0; //籍贯省
    var ncity = 0; //籍贯市
    if ($("selNativeplaceCountry").value != "")
        ncountry = $("selNativeplaceCountry").options[$("selNativeplaceCountry").selectedIndex].value;
    if ($("selNativeplaceCountry" + "selProvince"))
        if ($("selNativeplaceCountry" + "selProvince").value != "")
        nprovince = $("selNativeplaceCountry" + "selProvince").options[$("selNativeplaceCountry" + "selProvince").selectedIndex].value;
    if ($("selNativeplaceCountry" + "selProvince" + "selCity"))
        if ($("selNativeplaceCountry" + "selProvince" + "selCity").value != "")
        ncity = $("selNativeplaceCountry" + "selProvince" + "selCity").options[$("selNativeplaceCountry" + "selProvince" + "selCity").selectedIndex].value;
    var nationality = $("selNationality").options[$("selNationality").selectedIndex].value; //民族
    var religion = $("selReligion").options[$("selReligion").selectedIndex].value; //宗教信仰
    var haspp = ($("rdHavephoto").checked ? 1 : 0); //有无照片

    var z = "";
    var para = "";
    if (!$("radioSexNo").checked) {
        para += "&sex=" + sex;                                                          //性别
        z += "," + ((sex == "0") ? "男" : "女");
    }
    if (lcountry != "0") {
        para += "&lcountry=" + lcountry;                                                //所在地国家
        z += "," + $("selLocalityCountry").options[$("selLocalityCountry").selectedIndex].text;
    }
    if (lprovince != "0") {
        para += "&lprovince=" + lprovince;                                              //所在地省
        z += "," + $("selLocalityCountry" + "selProvince").options[$("selLocalityCountry" + "selProvince").selectedIndex].text;
    }
    if (lcity != "0") {
        para += "&lcity=" + lcity;                                                      //所在地市
        z += "," + $("selLocalityCountry" + "selProvince" + "selCity").options[$("selLocalityCountry" + "selProvince" + "selCity").selectedIndex].text;
    }
    if (age1 != "0") {
        para += "&age1=" + age1;                                         //年龄1
        z += "," + age1 + "岁";
    }
    if (age2 != "0") {
        para += "&age2=" + age2;                                         //年龄2
        if (age1 != "0")
            z += "-" + age2 + "岁";
        else
            z += ",至" + age2 + "岁";

    }
    if (height1 != "0") {
        para += "&height1=" + height1;                                //身高1
        z += "," + height1 + "cm";
    }
    if (height2 != "0") {
        para += "&height2=" + height2;                                //身高2
        if (height2 != "0")
            z += "-" + height2 + "cm";
        else
            z += ",至" + height2 + "cm";
    }
    if (education != "0") {
        para += "&education=" + education;                          //学历
        z += "," + $("selEducation").options[$("selEducation").selectedIndex].text;
    }
    if (salary != "0") {
        para += "&salary=" + salary;                                   //月薪
        z += "," + $("selSalary").options[$("selSalary").selectedIndex].text;
    }
    if (personalproperty != "0") {
        para += "&personalproperty=" + personalproperty;     //个人资产
        z += "," + $("selPersonalproperty").options[$("selPersonalproperty").selectedIndex].text;
    }
    if (civilstate != "0") {
        para += "&civilstate=" + civilstate;                       //婚姻状况
        z += "," + $("selCivilstate").options[$("selCivilstate").selectedIndex].text
    }
    if (childstate != "0") {
        para += "&childstate=" + childstate;                       //子女状况
        z += "," + $("selChildstate").options[$("selChildstate").selectedIndex].text;
    }
    if (profession != "0") {
        para += "&profession=" + profession;                       //职业
        z += "," + $("selProfession").options[$("selProfession").selectedIndex].text;
    }
    if (companytype != "0") {
        para += "&companytype=" + companytype;                    //公司类型
        z += "," + $("selCompanytype").options[$("selCompanytype").selectedIndex].text;
    }
    if (house != "0") {
        para += "&house=" + house;                                      //住房
        z += "," + $("selHouse").options[$("selHouse").selectedIndex].text;
    }
    if (car != "0") {
        para += "&car=" + car;                                            //购车
        z += "," + $("selCar").options[$("selCar").selectedIndex].text;
    }
    if (ncountry != "0") {
        para += "&ncountry=" + ncountry;                             //籍贯国家
        z += "," + $("selNativeplaceCountry").options[$("selNativeplaceCountry").selectedIndex].text;
    }
    if (nprovince != "0") {
        para += "&nprovince=" + nprovince;                          //籍贯省
        z += "," + $("selNativeplaceCountry" + "selProvince").options[$("selNativeplaceCountry" + "selProvince").selectedIndex].text;
    }
    if (ncity != "0") {
        para += "&ncity=" + ncity;                                      //籍贯市
        z += "," + $("selNativeplaceCountry" + "selProvince" + "selCity").options[$("selNativeplaceCountry" + "selProvince" + "selCity").selectedIndex].text;
    }
    if (nationality != "0") {
        para += "&nationality=" + nationality;                    //民族
        z += "," + $("selNationality").options[$("selNationality").selectedIndex].text;
    }
    if (religion != "0") {
        para += "&religion=" + religion;                             //宗教信仰
        z += "," + $("selReligion").options[$("selReligion").selectedIndex].text;
    }
    if (!$("rdHavephotoNo").checked) {
        para += "&haspp=" + haspp;                     //有无照片
        z += "," + ((haspp == "0") ? "无照片" : "有照片");
    }
    
    if (z!="") z=z.substr(1);
    //alert(para);
    if (para == "")
        alert("请先选择搜索条件");
    else
        window.location.href = ("searchinfo.aspx?page=1" + para + "&z=" + escape(z));
}

FormIni();