
function clearSelect(selectorname) {
    var selector;
    selector = $(selectorname);

    if (! selector) return false;
    for (i = selector.options.length-1; i >0 ; i--) {
        selector.options[i] = null;
    }
    selector.selectedIndex == 0;
    selector.disabled  = true;
}

function Country() {
    clearSelect('state');
    clearSelect('city');

    var country = $('country');
    var state = $('state');
    if (country.value == 0) return false;
    var url = '/ajax/myprofile/get_states_name_by_country';
    var pars = 'country=' + country.value;
    $('state_message').innerHTML = "<img src=\"http://img.zorpia.com/loader.gif\" width=\"16\" height=\"16\" hspace=5>";
    var myAjax = new Ajax.Request( url, {
        method: 'get',
        parameters: pars,
        onSuccess: showCountryResponse,
        onFailure: reportCountryError
    } );
}

// to reduce the traffic, we use a static JS for four countries.
// see state.js for more details
function Country_JS() {
    
    clearSelect('state');
    clearSelect('city');

    var country = $('country');
    if (country.value == 0) return false;
    
    eval('state = ' + state_str);
    eval('states = state.c' + country.value);

    if (states == undefined) {
        $('state').disabled = true;
        State();
        return;
    } else {
        $('state').disabled = false;
        $('city').disabled  = true;
    }
    
    for (i = 0; i < states.length; i++) {
        NewOption = new Option(states[i].state_name, states[i].state_id);
        $('state').options.add(NewOption);
    }
}

function showCountryResponse(request) {
    response  = request.responseText;

    var xotree = new XML.ObjTree();
    xotree.force_array = [ "state" ];
    var tree = xotree.parseXML( response ); 

    $('state_message').innerHTML = "";
    
    if ( ! tree.root) {
        $('state').disabled = true;
        State();
    if (LANG == 'cn') City();
        return;
    } else {
        $('state').disabled = false;
        $('city').disabled  = true;
    if (LANG == 'cn') City();
    }
    
    
    for (i = 0; i < tree.root.state.length; i++) {
        NewOption = new Option(tree.root.state[i].state_name, tree.root.state[i].state_id);
        $('state').options.add(NewOption);
    }
}
function reportCountryError(request) {
    $('state_message').innerHTML = '<p>'+ZORPIA_JS_LANG.ENCOUTER_SOME_ERROR+'</p>';
}

function State() {
    clearSelect('city');
    
    var country = $('country');
    var state = $('state');
    var city = $('city');
    if (country.value == 0) return false;
    var url = '/ajax/myprofile/get_cities_name_by_state';
    var pars = 'country=' + country.value + '&state=' + state.value;
    $('city_message').innerHTML = "<img src=\"http://img.zorpia.com/loader.gif\" width=\"16\" height=\"16\" hspace=5>";
    var myAjax = new Ajax.Request( url, {
        method: 'get',
        parameters: pars,
        onSuccess: showStateResponse,
        onFailure: reportStateError
    } );
}

function showStateResponse(request) {
    response  = request.responseText;

    var xotree = new XML.ObjTree();
    xotree.force_array = [ "city" ];
    var tree = xotree.parseXML( response ); 

    $('city_message').innerHTML = "";

    if($('not_in_list')){
        Element.show('not_in_list');
    }
    
    if ( ! tree.root) {
        $('city').disabled  = true;
    if (LANG == 'cn') City();       
        return;
    } else {
        $('city').disabled  = false;
    if (LANG == 'cn') City();
    }

    for (i = 0; i < tree.root.city.length; i++) {
        NewOption = new Option(tree.root.city[i].city_name, tree.root.city[i].city_id);
        $('city').options.add(NewOption);
    }
}
function reportStateError(request) {
    $('city_message').innerHTML = '<p>'+ZORPIA_JS_LANG.ENCOUTER_SOME_ERROR+'</p>';
}

function City() {
    clearSelect('school');

    var country = $('country');
    var state = $('state');
    var city  = $('city');
    var school = $('school');
    var school_type = $('school_type');
    if (country.value == 0) return false;
    if (!school) return;

    var url = '/ajax/myprofile/get_school_by_location_for_search';
    var pars = 'country=' + country.value + '&state=' + state.value + '&city=' + city.value + '&school_type=' + school_type;
    $('school_message').innerHTML = "<img src=\"http://img.zorpia.com/loader.gif\" width=\"16\" height=\"16\" hspace=5>";
    var myAjax = new Ajax.Request( url, {
        method: 'get',
        parameters: pars,
        onSuccess: showCityResponse,
        onFailure: reportCityError
    } );
}

function showCityResponse(request) {
    response  = request.responseText;
    
    var xotree = new XML.ObjTree();
    xotree.force_array = [ "school" ];
    var tree = xotree.parseXML( response );

    $('school_message').innerHTML = "";

    if ( ! tree.root) {
        $('school').disabled  = true;
        return;
    } else {
        $('school').disabled  = false;
    }
    //Element.show('not_in_list');

    for (i = 0; i < tree.root.school.length; i++) {
        NewOption = new Option(tree.root.school[i].school_name, tree.root.school[i].school_id);
        $('school').options.add(NewOption);
    }
}
function reportCityError(request) {
    $('school_message').innerHTML = '<p>'+ZORPIA_JS_LANG.ENCOUTER_SOME_ERROR+'</p>';
}


function check_country(the_form){
    var age_from = the_form.age_from.value;
    var age_to   = the_form.age_to.value;
    var country  = the_form.country.value;
    var msg = ZORPIA_JS_LANG.MISSING_COUNTRY;
    if (!msg)
    {
        msg = "Please select a country/territory!";
    }
    // advanced search need country
    // basic search when search age nned country
    if( (age_from || age_to || the_form.name == 'advanced_search') && country==0){
        alert(msg);
    the_form.country.focus();
        return 0;
    }
    return 1;
}

function sort(the_form, order_by, view_mode) 
{   
    the_form.order.value = order_by;
    the_form.view.value = view_mode;
    dolocation(the_form,'zorpians');
    
}

function change_view( the_form, view_mode )
{     
    the_form.view.value = view_mode;
    dolocation(the_form,'zorpians');
}

function dolocation(the_form,se_kind) {
        var r, re;  
        
        var country,state,city,gender,genre,sexual;
        var url = '/search/'+ se_kind + '/';
        if (!the_form){
            return false;
        }
        
        // if search for zorpian, need check age & country
        if (se_kind == 'zorpians' || se_kind == 'vp_contest')
        {
            if (check_country(the_form) == '0')
            {
                return false;
            }
        }
    
        if ( the_form.name == 'group_search'){
            select_more();
        }

        country = format_search(the_form.country.value);
        state = format_search(the_form.state.value);
        city = format_search(the_form.city.value);

        for(var i = 0; i < the_form.gender.length; i++){
            if(the_form.gender[i].checked){
                gender = the_form.gender[i].value;
                break;
            }
        }

        if(the_form.sexual){
            //alert(the_form.sexual.type);
            if(the_form.sexual.type == undefined){
                for(var i = 0; i < the_form.sexual.length; i++){
                    if(the_form.sexual[i].checked){
                        sexual = the_form.sexual[i].value;
                        break;
                    }
                }
            }else {
                sexual = the_form.sexual.value;
            
            }
            if(sexual == undefined){
                sexual = "";
            }
        

            //alert("gender:"+gender+"sexual:"+sexual);
            
            if(gender == "" && sexual != ""){
                alert("Please select either men or women in seeking for option.");
                return false;
            }

            if (gender != "" && sexual != ""){
                if (gender == 'male' && sexual == '1'){
                    the_form.sexual_preference.value = "Female";
                } else if(gender == 'male' && sexual == '2'){
                    the_form.sexual_preference.value = "Male";
                }

                if (gender == 'female' && sexual == '1'){
                    the_form.sexual_preference.value = "Male";
                } else if (gender == 'female' && sexual == '2'){
                    the_form.sexual_preference.value = "Female";
                }

                if (sexual == "3"){
                    the_form.sexual_preference.value = "Male and Female";
                }
            } 
        
        }
        if (the_form.genre){
            genre = format_search(the_form.genre.value);
        }

        // get country state city gender, genre 's value, add it in url beginning, move off string type of country=value.
        if (country && country != '0'){
            url += country + "/";  
        
        }
    
        if (state && state != '0'){
            url += state + "/";  
        }
    
        if (city && city != '0'){
            url += city + "/";  
        }
    
        if (gender){
            url += gender + "/";  
        }

        if (genre && genre != '0'){
            url += genre + "/";  
        }
        
        // add order
        if (the_form.order) {
            url += '?order=' + the_form.order;
        }
            
        the_form.action = url;
        the_form.submit();
    
}

function format_search(string){
        var re;
        if (!string){
                return '';
        }

        re = /%20/g;
        string = string.replace(re,"_");
        string = string.replace(/\'/g,"_c_");
        string = string.replace(/-/g,"_h_");
        string = string.replace(/\./g, "_p_");


        //delete  item like [select ....]
        re = /(\w+)=0/g;
        string = string.replace(re,"");
        return string;
}




