search_type_value = new Array("1" , "2" , "3");
search_type_desc  = new Array("Buy Property" , "Holiday Rentals" , "Long-term Rentals");

property_type_value = new Array("" , "*" , "2" , "1" , "3" , "4");
property_type_desc  = new Array("Property Type" , "All" , "Apartment/Condo" , "House/Villa" , "Land" , "Commercial");

property_region_value = new Array( "" , "*" , "8" , "9" , "10" , "11" , "12");
property_region_desc  = new Array( "Region" , "All" , "North Phuket" , "West Phuket" , "East Phuket" , "South Phuket" , "Town & Central");

rental_region_value = new Array( "" , "*" , "3" , "4" , "5" , "6" , "7");
rental_region_desc  = new Array( "Region" , "All" , "North Phuket" , "West Phuket" , "East Phuket" , "South Phuket" , "Town & Central");

property_max_bedrooms_ge_value = new Array( "" , "*" , "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" );
property_max_bedrooms_ge_desc  = new Array( "Minimum Bedrooms" , "All" , "Studio" , "1" , "2" , "3" , "4" , "5" , "6" , "7" );

property_min_price_le_value = new Array( ""      ,  "*"       , "5"   ,   "10"    , 
                                "15"   ,  "20"    , "30"  ,   "40"    , 
                                "60"   ,  "80"    , "100" ,   "150"   , 
                                "200"  ,  "*"    );

property_min_price_le_desc  = new Array( "Maximum Price (THB)", "All"         , "5 million"   ,   "10 million"    , 
                                "15 million"        ,  "20 million"  , "30 million"  ,   "40 million"    , 
                                "60 million"        ,  "80 million"  , "100 million" ,   "150 million"   , 
                                "200 million"       ,  "300 million+"    );

rental_type_value = new Array("" , "*" , "1" , "2" );
rental_type_desc  = new Array("Property Type" , "All" , "Apartments" , "Villas & Houses" );

lt_12months_rate_value = new Array( "" , "20000"         ,  "20000-30000"    ,  
                                    "30000-40000"   ,   "40000-50000"   ,
                                    "50000-70000"   ,   "70000-100000"  ,
                                    "100000-150000" ,   "150001" );
                                    
lt_12months_rate_desc = new Array( "Price" , "Under 20,000 THB"      ,  "20,000 - 30,000 THB"    ,  
                                    "30,000 - 40,000 THB"       ,   "40,000 - 50,000 THB"   ,
                                    "50,000 - 70,000 THB"       ,   "70,000 - 100,000 THB"  ,
                                    "100,000 - 150,000 THB"     ,   "Over 150,000 THB" );


rental_num_bedrooms_value = new Array( "" , "*" , "1" , "2" , "3" , "4" , "5" , "6" );      
rental_num_bedrooms_desc  = new Array( "No. of Bedrooms" , "All" , "1" , "2" , "3" , "4" , "5" , "6" );

rental_district_label = new Array();
rental_district_value = new Array();
property_district_label = new Array();
property_district_value = new Array();

rental_district_label = create_rental_district_label();
rental_district_value = create_rental_district_value();

property_district_label = create_property_district_label();
property_district_value = create_property_district_value();

initial();

if(document.getElementById('min_price-le')){
        var min_price_le_dropdown = document.getElementById('min_price-le')
        min_price_le_dropdown.onchange = function()
            { 
                if( document.getElementById("hidden_so") ){
                    var hidden_so = document.getElementById("hidden_so");
                    hidden_so.value = 'DESC'; 
                }
                
                if( document.getElementById("min_price-le").value == '*' ){
                    document.getElementById("min_price-le").name = 'min_price';
                }
                
            };
}

function initial(){

    if( document.getElementById('search_type' ) ) {
        
        if( document.getElementById('search_type').value == 1  ){
        
            var property_form = create_property_form();
        }
        if( document.getElementById('search_type').value == 2 ){

            var holiday_rental_form = create_holiday_rental_form();
        }
    
        if( document.getElementById('search_type').value == 3 ){

            var long_term_rental_form = create_long_term_rental_form();
        }
    }
    else{
        
    }
}
                                                                  
function form_search(){
    
    if( document.getElementById('search_type').value == 1 ){
        
        var property_form = create_property_form();
    }
    
    if( document.getElementById('search_type').value == 2 ){

        var holiday_rental_form = create_holiday_rental_form();
    }
    
    if( document.getElementById('search_type').value == 3 ){

       var long_term_rental_form = create_long_term_rental_form();
    }    
}

function  create_property_form() {

    var sec2 = document.getElementById("sec2");
    var sec3 = document.getElementById("sec3");
    var sec4 = document.getElementById("sec4");
    var sec5 = document.getElementById("sec5");
    var sec6 = document.getElementById("sec6");
    
    if( document.getElementById('hidden_sb') ) {
        var remove_hidden_sb = remove_current_node( 'hidden_sb' );
    }
    if( document.getElementById('hidden_so') ) {
        var remove_hidden_so = remove_current_node( 'hidden_so' );
    }
 
    if( document.getElementById('rental_terms_a') ){
        var remove_hidden_rental_terms = remove_current_node( 'rental_terms_a' );
    }
    if( document.getElementById('rental_terms_b') ){
        var remove_hidden_rental_terms = remove_current_node( 'rental_terms_b' );
    }
    if( document.getElementById('lt_12months_rate_ge') ){
        var remove_lt_12months_rate_ge = remove_current_node( 'lt_12months_rate_ge' );
    }
    if( document.getElementById('lt_12months_rate_le') ){
        var remove_lt_12months_rate_le = remove_current_node( 'lt_12months_rate_le' );
    }
    
    var property_type_dropdown   = dom_create_dropdown( 'property_type'    , 'property_type'    ,  property_type_value  , property_type_desc , property_type_value[0] );
    var region_dropdown          = dom_create_dropdown( 'region'           , 'region'           ,  property_region_value , property_region_desc  , property_region_value[0] );
    var location_dropdown        = dom_create_dropdown( 'location'         , 'location'         ,  property_district_value , property_district_label , property_district_value[0] );
    var max_bedrooms_ge_dropdown = dom_create_dropdown( 'max_bedrooms-ge'  , 'max_bedrooms-ge'  ,  property_max_bedrooms_ge_value , property_max_bedrooms_ge_desc , property_max_bedrooms_ge_value[0] );
    var min_price_le_dropdown    = dom_create_dropdown( 'min_price-le'     , 'min_price-le'     ,  property_min_price_le_value , property_min_price_le_desc , property_min_price_le_value[0] );
    
    
    if( sec2 != null ){
        if ( (sec2.hasChildNodes() == true) ) {
            while( sec2.hasChildNodes() ){
                sec2.removeChild(sec2.lastChild); 
            }
            var add_property_type_dropdown  = sec2.appendChild(property_type_dropdown);
        }
    }
    
    if( sec3 != null ){
        if ( sec3.hasChildNodes() == true) {
            while( sec3.hasChildNodes() ){
                sec3.removeChild(sec3.lastChild); 
            }
            var add_location_dropdown       = sec3.appendChild(location_dropdown);
        }
   } 
    
    if( sec4 != null ){
        if ( sec4.hasChildNodes() == true) {
            while( sec4.hasChildNodes() ){
                sec4.removeChild(sec4.lastChild); 
            }
            var add_max_bedrooms_ge_dropdown= sec4.appendChild(max_bedrooms_ge_dropdown);
            var max_bedrooms_ge_dropdown = document.getElementById("max_bedrooms-ge");
                max_bedrooms_ge_dropdown.onchange= function(){change_compare_feild();}
        }
    }
    
    if( sec5 != null ){
        if ( sec5.hasChildNodes() == true) {
            while( sec5.hasChildNodes() ){
                sec5.removeChild(sec5.lastChild); 
            }  
        }
        var add_min_price_le_dropdown   = sec5.appendChild(min_price_le_dropdown);
        var min_price_le_dropdown = document.getElementById('min_price-le')
        min_price_le_dropdown.onchange = function(){ change_compare_feild(); }
    }
    
    if( sec6 != null ){
        if ( sec6.hasChildNodes() == true) {
            while( sec6.hasChildNodes() ){
                sec6.removeChild(sec6.lastChild); 
            }  
        }
        var add_region_dropdown   = sec6.appendChild(region_dropdown);
        var region_dropdown = document.getElementById('region')
        region_dropdown.onchange = function(){ update_location(this.value); }
    }
    
    var property_type_onchange = document.getElementById("property_type");
    property_type_onchange.onchange= function(){
        check_property_type();
    }

    var search_form = document.getElementById("property-search");
    var sb = create_hidden_input( 'hidden_sb' , 'sb' , 'min_price' );
    var so = create_hidden_input( 'hidden_so' , 'so' , 'ASC' );
    
    search_form.appendChild(sb);
    search_form.appendChild(so);
    
    document.getElementById("property-search").action='http://www.phuket.net/scgi/property/search.cgi'; 
    document.getElementById("advanced_search").href ='http://www.phuket.net/scgi/property/search.cgi';
}

function create_holiday_rental_form() {
    
    var sec2 = document.getElementById("sec2");
    var sec3 = document.getElementById("sec3");
    var sec4 = document.getElementById("sec4");
    var sec5 = document.getElementById("sec5");
    var sec6 = document.getElementById("sec6");
    
    //Remove property Hidden input    
    if( document.getElementById('hidden_sb') ) {
        var remove_hidden_sb = remove_current_node( 'hidden_sb' );
    }
    if( document.getElementById('hidden_so') ) {
        var remove_hidden_so = remove_current_node( 'hidden_so' );
    }
 
    if( document.getElementById('rental_terms_a') ){
        var remove_hidden_rental_terms = remove_current_node( 'rental_terms_a' );
    }
    if( document.getElementById('rental_terms_b') ){
        var remove_hidden_rental_terms = remove_current_node( 'rental_terms_b' );
    }
    if( document.getElementById('lt_12months_rate_ge') ){
        var remove_lt_12months_rate_ge = remove_current_node( 'lt_12months_rate_ge' );
    }
    if( document.getElementById('lt_12months_rate_le') ){
        var remove_lt_12months_rate_le = remove_current_node( 'lt_12months_rate_le' );
    }
    
    //Create rentals dropdown
    var rental_type_dropdown  = dom_create_dropdown( 'property_type' , 'property_type' ,  rental_type_value , rental_type_desc , rental_type_value[0] );
    var region_dropdown       = dom_create_dropdown( 'region'        , 'region'        ,  rental_region_value , rental_region_desc  , rental_region_value[0] );
    var location_dropdown     = dom_create_dropdown( 'location'      , 'location'      ,  rental_district_value , rental_district_label , rental_district_value[0] );
    var num_bedrooms_dropdown = dom_create_dropdown( 'num_bedrooms'  , 'num_bedrooms'  ,  rental_num_bedrooms_value , rental_num_bedrooms_desc , rental_num_bedrooms_value[0] );
    
    //Remove property dropdown
    if( sec2 != null ){
        if ( (sec2.hasChildNodes() == true) ) {
            while( sec2.hasChildNodes() ){
                sec2.removeChild(sec2.lastChild); 
            }
        }
    }
    
    if( sec3 != null ){
        if ( sec3.hasChildNodes() == true) {
            while( sec3.hasChildNodes() ){
                sec3.removeChild(sec3.lastChild); 
            }
        }
    }
    
    if( sec4 != null ){
        if ( sec4.hasChildNodes() == true) {
            while( sec4.hasChildNodes() ){
                sec4.removeChild(sec4.lastChild); 
            } 
        }
    }
    
    if( sec5 != null ){
        
        if ( sec5.hasChildNodes() == true) {
            while( sec5.hasChildNodes() ){
                sec5.removeChild(sec5.lastChild); 
            }
        }
    }
    if( sec6 != null ){
        if ( sec6.hasChildNodes() == true) {
            while( sec6.hasChildNodes() ){
                sec6.removeChild(sec6.lastChild); 
            }  
        }
        var add_region_dropdown   = sec6.appendChild(region_dropdown);
        var region_dropdown = document.getElementById('region')
        region_dropdown.onchange = function(){ update_rental_location(this.value); }
    }
    
    var add_rental_type_dropdown  = sec2.appendChild(rental_type_dropdown);
    var add_location_dropdown     = sec3.appendChild(location_dropdown);
    var add_num_bedrooms_dropdown = sec4.appendChild(num_bedrooms_dropdown);

    //Create hidden rental input
    var search_form = document.getElementById("property-search");
    var sb = create_hidden_input( 'hidden_sb' , 'sb' , 'num_bedrooms' );
    var so = create_hidden_input( 'hidden_so' , 'so' , 'DESC' );
    var rental_terms_a = create_hidden_input( 'rental_terms_a' , 'rental_terms' , '1' );
    var rental_terms_b = create_hidden_input( 'rental_terms_b' , 'rental_terms' , '3' );

    search_form.appendChild(sb);
    search_form.appendChild(so);
    search_form.appendChild(rental_terms_a);
    search_form.appendChild(rental_terms_b);
    
    document.getElementById("property-search").action='http://www.phuket.net/scgi/rentals/search.cgi'; 
    document.getElementById("advanced_search").href ='http://www.phuket.net/scgi/rentals/search.cgi';

}

function change_compare_feild(){
    
    if( document.getElementById("max_bedrooms-ge").value == '*' ){
        document.getElementById("max_bedrooms-ge").name = 'max_bedrooms';
    }else{
        
    }
    
    if( document.getElementById("min_price-le").value == '*' ){
        document.getElementById("min_price-le").name = 'min_price';
    }else{
        
    }

}

function create_long_term_rental_form() {
    
    var sec2 = document.getElementById("sec2");
    var sec3 = document.getElementById("sec3");
    var sec4 = document.getElementById("sec4");
    var sec5 = document.getElementById("sec5");
    var sec6 = document.getElementById("sec6");
    
    //Remove property Hidden input
    if( document.getElementById('hidden_sb') ) {
        var remove_hidden_sb = remove_current_node( 'hidden_sb' );
    }
    if( document.getElementById('hidden_so') ) {
        var remove_hidden_so = remove_current_node( 'hidden_so' );
    }
    if( document.getElementById('rental_terms_a') ){
        var remove_rental_terms_a = remove_current_node( 'rental_terms_a' );
    }
    if( document.getElementById('rental_terms_b') ){
        var remove_rental_terms_b = remove_current_node( 'rental_terms_b' );
    }
    if( document.getElementById('rental_terms_1') ){
        var remove_rental_terms_1 = remove_current_node( 'rental_terms_1' );
    }
    if( document.getElementById('rental_terms_2') ){
        var remove_rental_terms_2 = remove_current_node( 'rental_terms_2' );
    }

    //Create long-term rentals dropdown
    var rental_type_dropdown  = dom_create_dropdown( 'property_type' , 'property_type' ,  rental_type_value , rental_type_desc , rental_type_value[0] );
    var region_dropdown       = dom_create_dropdown( 'region'        , 'region'        ,  rental_region_value , rental_region_desc  , rental_region_value[0] );
    var location_dropdown     = dom_create_dropdown( 'location'      , 'location'      ,  rental_district_value , rental_district_label , rental_district_value[0] );
    var num_bedrooms_dropdown = dom_create_dropdown( 'num_bedrooms'  , 'num_bedrooms'  ,  rental_num_bedrooms_value , rental_num_bedrooms_desc , rental_num_bedrooms_value[0] );
    var lt_12months_rate_dropdown = dom_create_dropdown( ''  , '12months_rate'         ,  lt_12months_rate_value , lt_12months_rate_desc , lt_12months_rate_value[0] );
    
    if ( (sec2.hasChildNodes() == true) ) {
        while( sec2.hasChildNodes() ){
            sec2.removeChild(sec2.lastChild); 
        }
    }
    if ( sec3.hasChildNodes() == true) {
        while( sec3.hasChildNodes() ){
            sec3.removeChild(sec3.lastChild); 
        }
    }
    if ( sec4.hasChildNodes() == true) {
        while( sec4.hasChildNodes() ){
            sec4.removeChild(sec4.lastChild); 
        } 
    }
    if ( sec5.hasChildNodes() == true) {
        while( sec5.hasChildNodes() ){
            sec5.removeChild(sec5.lastChild); 
        }
    }
    
    if( sec6 != null ){
        if ( sec6.hasChildNodes() == true) {
            while( sec6.hasChildNodes() ){
                sec6.removeChild(sec6.lastChild); 
            }  
        }
        var add_region_dropdown   = sec6.appendChild(region_dropdown);
        var region_dropdown = document.getElementById('region')
        region_dropdown.onchange = function(){ update_rental_location(this.value); }
    }

    var add_rental_type_dropdown  = sec2.appendChild(rental_type_dropdown);
    var add_location_dropdown     = sec3.appendChild(location_dropdown);
    var add_num_bedrooms_dropdown = sec4.appendChild(num_bedrooms_dropdown);
    var add_lt_12months_rate_dropdown = sec5.appendChild(lt_12months_rate_dropdown);
    
    
    var search_form = document.getElementById("property-search");
    var sb = create_hidden_input( 'hidden_sb' , 'sb' , 'num_bedrooms' );
    var so = create_hidden_input( 'hidden_so' , 'so' , 'DESC' );
    var rental_terms_a = create_hidden_input( 'rental_terms_a' , 'rental_terms' , '2' );
    var rental_terms_b = create_hidden_input( 'rental_terms_b' , 'rental_terms' , '3' );
    
    var lt_12months_rate_dropdown = document.getElementById('12months_rate')
        lt_12months_rate_dropdown.onchange = function(){ hidden_12months_rate(this.value); }


    search_form.appendChild(sb);
    search_form.appendChild(so);
    search_form.appendChild(rental_terms_a);
    search_form.appendChild(rental_terms_b);
    search_form.appendChild(longterm_12months_rate);
    
    document.getElementById("property-search").action='http://www.phuket.net/scgi/rentals/search.cgi'; 
    document.getElementById("advanced_search").href ='http://www.phuket.net/scgi/rentals/search.cgi';

}

function hidden_12months_rate(rate_12months_value){
    
    var current_lt_12months_rate_le = document.getElementById('lt_12months_rate_le');
    var current_lt_12months_rate_ge = document.getElementById('lt_12months_rate_ge');
    var current_hidden_sb = document.getElementById('hidden_sb');
    var current_hidden_so = document.getElementById('hidden_so');
    
    if(current_lt_12months_rate_le){
        var remove_lt_12months_rate_le = remove_current_node( 'lt_12months_rate_le' );
    }
    if(current_lt_12months_rate_ge){
        var remove_lt_12months_rate_ge = remove_current_node( 'lt_12months_rate_ge' );
    }
    if(current_hidden_sb){
        var remove_current_hidden_sb = remove_current_node( 'hidden_sb' );
    }
    if(current_hidden_so){
        var remove_current_hidden_so = remove_current_node( 'hidden_so' );
    }
    
    var lt_12months_rate_le;
    var lt_12months_rate_ge;
    
    var search_form = document.getElementById("property-search");
    var hidden_sb = create_hidden_input( 'hidden_sb' , 'sb' , 'longterm_12months_rate' );
    var hidden_so = create_hidden_input( 'hidden_so' , 'so' , 'ASC' );

    if(rate_12months_value == '20000' ){
        lt_12months_rate_le = create_hidden_input( 'lt_12months_rate_le' , 'longterm_12months_rate-le' , '20000' );
        search_form.appendChild(lt_12months_rate_le);
    }
    
    if(rate_12months_value == '20000-30000'){
        lt_12months_rate_ge = create_hidden_input( 'lt_12months_rate_ge' , 'longterm_12months_rate-ge' , '20000' );
        lt_12months_rate_le = create_hidden_input( 'lt_12months_rate_le' , 'longterm_12months_rate-le' , '30000' );
    }
    
    if(rate_12months_value == '30000-40000'){
        lt_12months_rate_ge = create_hidden_input( 'lt_12months_rate_ge' , 'longterm_12months_rate-ge' , '30000' );
        lt_12months_rate_le = create_hidden_input( 'lt_12months_rate_le' , 'longterm_12months_rate-le' , '40000' );
    }
    
    if(rate_12months_value == '40000-50000'){
        lt_12months_rate_ge = create_hidden_input( 'lt_12months_rate_ge' , 'longterm_12months_rate-ge' , '40000' );
        lt_12months_rate_le = create_hidden_input( 'lt_12months_rate_le' , 'longterm_12months_rate-le' , '50000' );
    }
    
    if(rate_12months_value == '50000-70000'){
        lt_12months_rate_ge = create_hidden_input( 'lt_12months_rate_ge' , 'longterm_12months_rate-ge' , '50000' );
        lt_12months_rate_le = create_hidden_input( 'lt_12months_rate_le' , 'longterm_12months_rate-le' , '70000' );
    }
    
    if(rate_12months_value == '70000-100000'){
        lt_12months_rate_ge = create_hidden_input( 'lt_12months_rate_ge' , 'longterm_12months_rate-ge' , '70000' );
        lt_12months_rate_le = create_hidden_input( 'lt_12months_rate_le' , 'longterm_12months_rate-le' , '100000' );
    }
    
    if(rate_12months_value == '100000-150000'){
        lt_12months_rate_ge = create_hidden_input( 'lt_12months_rate_ge' , 'longterm_12months_rate-ge' , '100000' );
        lt_12months_rate_le = create_hidden_input( 'lt_12months_rate_le' , 'longterm_12months_rate-le' , '150000' );
    }
    
    if(rate_12months_value == '150001'){
        lt_12months_rate_ge = create_hidden_input( 'lt_12months_rate_le' , 'longterm_12months_rate-ge' , '150000' );
        search_form.appendChild(lt_12months_rate_ge);
    } 
    
    search_form.appendChild(hidden_sb);
    search_form.appendChild(hidden_so);
    search_form.appendChild(lt_12months_rate_le);
    search_form.appendChild(lt_12months_rate_ge);
}

function check_property_type() {

    var property_type_value = document.getElementById("property_type").value;
    var sec4 = document.getElementById("sec4");
    
    if( property_type_value == 3 || property_type_value == 4 ){

        if ( sec4.hasChildNodes() == true) {
            while( sec4.hasChildNodes() ){
                sec4.removeChild(sec4.lastChild); 
            } 
        }

    }
    else if( property_type_value == '' || property_type_value == '*' || property_type_value == 1 || property_type_value == 2 ){
        if ( sec4.hasChildNodes() == false) {
            var max_bedrooms_ge_dropdown = dom_create_dropdown( 'max_bedrooms-ge'  , 'max_bedrooms-ge'  ,  property_max_bedrooms_ge_value , property_max_bedrooms_ge_desc , property_max_bedrooms_ge_value[0] );
            sec4.appendChild( max_bedrooms_ge_dropdown )
        }else{
            //Nothing
        }
    }
    else{
        //Nothing
    }
    
    
}

function addPrice(chk_object){
    
    var html = [];
    var price_search = document.getElementById( "t-price" );
    
    if( (document.getElementById( "rt1" ).checked == false) && (document.getElementById( "rt2" ).checked == true) )
    {
        html.push('<table class="t-search">'); 
        html.push('<tr>'); 
        html.push('  <td><h4>Prince:</h4></td>');
        html.push('  <td>&nbsp;</td>');
        html.push('  <td>&nbsp;</td>');
        html.push('</tr>');
        html.push('<tr>');
        html.push('  <td> <select id="12months_rate" name="" onChange="hidden_12months_rate(this.value);">');
        html.push('      <option value="" selected="selected">Price</option>');
        html.push('      <option value="20000">Under 20,000 THB</option>');
        html.push('      <option value="20000-30000">20,000 - 30,000 THB</option>');
        html.push('      <option value="30000-40000">30,000 - 40,000 THB</option>');
        html.push('      <option value="40000-50000">40,000 - 50,000 THB</option>');
        html.push('      <option value="50000-70000">50,000 - 70,000 THB</option>');
        html.push('      <option value="70000-100000">70,000 - 100,000 THB</option>');
        html.push('      <option value="100000-150000">100,000 - 150,000 THB</option>');
        html.push('      <option value="150001">Over 150,000 THB</option>');
        html.push('    </select></td>');
        html.push('  <td>&nbsp;</td>');
        html.push('  <td>&nbsp;</td>');
        html.push('</tr>');
        html.push('</table>');

        html.join('');
        price_search.innerHTML = html.join('');
    }else{
        price_search.innerHTML = '';
    }
}

function remove_current_node( node_id ) {
    
	var node_object = document.getElementById( node_id );
	node_object.parentNode.removeChild(node_object);
	
	return node_object;
}

function dom_create_dropdown( selector_name , selector_id ,  option_value , option_description , default_value ) {

    /**TAG SELECT**/
    var tag_select = document.createElement("select");

    var att_name = document.createAttribute("name");
    att_name.value = selector_name ;
    tag_select.setAttributeNode(att_name );

    var att_id = document.createAttribute("id");
    att_id.value = selector_id;
    tag_select.setAttributeNode(att_id);
    
    for( current_option = 0; current_option < option_value.length ; current_option++ ) {   

        /**TAG OPTION**/
        var tag_option = document.createElement("option");
        var att_value  = document.createAttribute("value");
        att_value.value = option_value[current_option];
        tag_option.setAttributeNode(att_value);
        
       // if (option_value[current_option] == default_value) {
          if (current_option == 0) {
            
            var att_selected = document.createAttribute("selected");
            att_selected.value = "selected" ;
            tag_option.setAttributeNode(att_selected);
        }
    
        var label_value = document.createTextNode( option_description[current_option] );

        tag_option.appendChild(label_value);
        tag_select.appendChild(tag_option);
     }
     
    return tag_select;
}




function create_hidden_input( input_id , input_name , input_value ){
    
    var input_hidden = document.createElement("input");
    input_hidden.setAttribute('type' , 'hidden');
    input_hidden.setAttribute('id'   , input_id );
    input_hidden.setAttribute('name' , input_name );
    input_hidden.setAttribute('value', input_value);
    
    return input_hidden;
}

function used_map(type_map){
    
    sprite_map_node = document.getElementById("sprite-map");
    north_menu_node = document.getElementById("north-menu");
    east_menu_node  = document.getElementById("east-menu");
    west_menu_node  = document.getElementById("west-menu");
    south_menu_node = document.getElementById("south-menu");
    town_menu_node  = document.getElementById("town-menu");
    
    sprite_map  = document.getElementById("sprite-map").innerHTML;
    north_menu  = document.getElementById("north-menu").innerHTML;
    east_menu   = document.getElementById("east-menu").innerHTML;
    west_menu   = document.getElementById("west-menu").innerHTML;
    south_menu  = document.getElementById("south-menu").innerHTML;
    town_menu   = document.getElementById("town-menu").innerHTML;
    
    if( type_map == 'sales_map' ){
        sprite_map=sprite_map.replace(/rentals/g,"property");
        north_menu=north_menu.replace(/rentals/g,"property");
        east_menu=east_menu.replace(/rentals/g,"property");
        west_menu=west_menu.replace(/rentals/g,"property");
        south_menu=south_menu.replace(/rentals/g,"property");
        town_menu=town_menu.replace(/rentals/g,"property");
    }
    if( type_map == 'rentals_map'){
        sprite_map=sprite_map.replace(/property/g,"rentals");
        north_menu=north_menu.replace(/property/g,"rentals");
        east_menu=east_menu.replace(/property/g,"rentals");
        west_menu=west_menu.replace(/property/g,"rentals");
        south_menu=south_menu.replace(/property/g,"rentals");
        town_menu=town_menu.replace(/property/g,"rentals");
        
    }

    sprite_map_node.innerHTML = sprite_map;
    north_menu_node.innerHTML = north_menu;
    east_menu_node.innerHTML = east_menu;
    west_menu_node.innerHTML = west_menu;
    south_menu_node.innerHTML = south_menu;
    town_menu_node.innerHTML = town_menu;
    
}

function location_checked( region_object ){
    
    if (region_object.checked == true) {
        if( region_object.id == 'reg1'  ){
            document.getElementById('reg1-1').checked = true;
            document.getElementById('reg1-2').checked = true;
            document.getElementById('reg1-3').checked = true;
            document.getElementById('reg1-4').checked = true;
            document.getElementById('reg1-5').checked = true;
            document.getElementById('reg1-6').checked = true;
            document.getElementById('reg1-7').checked = true;
            document.getElementById('reg1-8').checked = true;
        }else if( region_object.id == 'reg2' ){
            document.getElementById('reg2-1').checked = true;
            document.getElementById('reg2-2').checked = true;
            document.getElementById('reg2-3').checked = true;
            document.getElementById('reg2-4').checked = true;
            document.getElementById('reg2-5').checked = true;
            document.getElementById('reg2-6').checked = true;
        
        }else if( region_object.id == 'reg5' ){
            document.getElementById('reg5-1').checked = true;
            document.getElementById('reg5-2').checked = true;
            document.getElementById('reg5-3').checked = true;
            document.getElementById('reg5-4').checked = true;
            document.getElementById('reg5-5').checked = true;
            document.getElementById('reg5-6').checked = true;
        
        }else if( region_object.id == 'reg3' ){ 
            document.getElementById('reg3-1').checked = true;
            document.getElementById('reg3-2').checked = true;
            document.getElementById('reg3-3').checked = true;
            document.getElementById('reg3-4').checked = true;
        
        }else if( region_object.id == 'reg4' ){ 
            document.getElementById('reg4-1').checked = true;
            document.getElementById('reg4-2').checked = true;
            document.getElementById('reg4-3').checked = true;
            document.getElementById('reg4-4').checked = true;
        }else{
            region_object.checked = false;
        }
    }
    else{
        region_object.checked = false;

    }
}

function property_check_location(){
    var property_form = document.getElementById('property-search');
    
    if(  (  (document.getElementById('reg1-1').checked == true) ||
            (document.getElementById('reg1-2').checked == true) ||
            (document.getElementById('reg1-3').checked == true) ||
            (document.getElementById('reg1-4').checked == true) ||
            (document.getElementById('reg1-5').checked == true) ||
            (document.getElementById('reg1-6').checked == true) ||
            (document.getElementById('reg1-7').checked == true) ||
            (document.getElementById('reg1-8').checked == true) ) && 
            (document.getElementById('reg1').checked   == false))
    {
        //North Phuket
        var hidden_reg1_8 = create_hidden_input( 'hidden_reg1_8' , 'region' , '8' );
        property_form.appendChild( hidden_reg1_8 )
    }
    
    if(  (  (document.getElementById('reg2-1').checked == true) ||
            (document.getElementById('reg2-2').checked == true) ||
            (document.getElementById('reg2-3').checked == true) ||
            (document.getElementById('reg2-4').checked == true) ||
            (document.getElementById('reg2-5').checked == true) ||
            (document.getElementById('reg2-6').checked == true) ) && 
            (document.getElementById('reg2').checked   == false))
    {
        //West Coast
        var hidden_reg2_9 = create_hidden_input( 'hidden_reg2_9' , 'region' , '9' );
        property_form.appendChild( hidden_reg2_9 )
    }
    
    if(  (  (document.getElementById('reg3-1').checked == true) ||
            (document.getElementById('reg3-2').checked == true) ||
            (document.getElementById('reg3-3').checked == true) ||
            (document.getElementById('reg3-4').checked == true) ) && 
            (document.getElementById('reg3').checked   == false))
    {
        //East Coast
        var hidden_reg3_10 = create_hidden_input( 'hidden_reg3_10' , 'region' , '10' );
        property_form.appendChild( hidden_reg3_10 )
    }
    
    if(  (  (document.getElementById('reg4-1').checked == true) ||
            (document.getElementById('reg4-2').checked == true) ||
            (document.getElementById('reg4-3').checked == true) ||
            (document.getElementById('reg4-4').checked == true) ) && 
            (document.getElementById('reg4').checked   == false))
    {
        //	Town & Kathu
        var hidden_reg4_12 = create_hidden_input( 'hidden_reg4_12' , 'region' , '12' );
        property_form.appendChild( hidden_reg4_12 )
    }
    
    if(  (  (document.getElementById('reg5-1').checked == true) ||
            (document.getElementById('reg5-2').checked == true) ||
            (document.getElementById('reg5-3').checked == true) ||
            (document.getElementById('reg5-3').checked == true) ||
            (document.getElementById('reg5-3').checked == true) ||
            (document.getElementById('reg5-4').checked == true) ) && 
            (document.getElementById('reg5').checked   == false))
    {
        //	South Phuket
        var hidden_reg5_11 = create_hidden_input( 'hidden_reg5_11' , 'region' , '11' );
        property_form.appendChild( hidden_reg5_11 )
    }
}

function loadForm( form_current ){
    var FORM_SEND = document.createElement('FORM');
    FORM_SEND.method = "POST";

    var number_order = form_current.number_order.value
    var hidden_number_order = create_hidden_input('number_order' , 'number_order' , number_order )
    var hidden_rental_inquiry_cart = create_hidden_input('rental_inquiry_cart' , 'rental_inquiry_cart' , 'Continue to Inquiry Form' )
    
    var rental_type = form_current.rental_type.value
    var hidden_rental_type;

    if(rental_type == 'holiday_long_rental'){
        hidden_rental_type = create_hidden_input( 'c' , 'c' , '4' )
    }
    
    if(rental_type == 'long_term_rental'){  
	 hidden_rental_type = create_hidden_input( 'c' , 'c' , '3' )
    }
    
    if( form_current.page_order ){        
    	var hidden_page_order = create_hidden_input( 'page_order' , 'page_order'  , 'details' );        
	FORM_SEND.appendChild( hidden_page_order );
    }

    for(var i=1;i<=number_order;i++ ){
        var order_id = 'rental_id_' + i;
        var order_name = 'order_' + i;
        var current_order_id = document.getElementById( 'rental_id_'  + i ).value 
        var current_order_name = document.getElementById( 'rental_name_' + i ).value
        
        var hidden_rental_id = create_hidden_input( order_id , order_id  , current_order_id )
        var hidden_rental_name= create_hidden_input(order_name , order_name , current_order_name )
        
        FORM_SEND.appendChild( hidden_rental_id )
        FORM_SEND.appendChild( hidden_rental_name )

    }
    
    FORM_SEND.appendChild( hidden_number_order )
    FORM_SEND.appendChild( hidden_rental_inquiry_cart )
    FORM_SEND.appendChild( hidden_rental_type )

    document.body.appendChild(FORM_SEND);
    FORM_SEND.submit();

}

function hidden_advance_search(){
    
    //Rental Search Form
    var rental_advance_search = document.getElementById('property-search')
    var holiday_rental = document.getElementById('rt1').checked;
    var longterm_rental = document.getElementById('rt2').checked;
    
    if( holiday_rental == true || longterm_rental == true ){
        var hidden_rental_terms = create_hidden_input( 'hidden_rental_terms' , 'rental_terms' , '3' );
        rental_advance_search.appendChild(hidden_rental_terms);
    }
    
    if(  (  (document.getElementById('reg1-1').checked == true) ||
            (document.getElementById('reg1-2').checked == true) ||
            (document.getElementById('reg1-3').checked == true) ||
            (document.getElementById('reg1-4').checked == true) ||
            (document.getElementById('reg1-5').checked == true) ||
            (document.getElementById('reg1-6').checked == true) ||
            (document.getElementById('reg1-7').checked == true) ||
            (document.getElementById('reg1-8').checked == true) ) && 
            (document.getElementById('reg1').checked   == false))
    {
        //North Phuket
        var hidden_reg1_3 = create_hidden_input( 'hidden_reg1_3' , 'region' , '3' );
        rental_advance_search.appendChild( hidden_reg1_3 )
    }
    
    if(  (  (document.getElementById('reg2-1').checked == true) ||
            (document.getElementById('reg2-2').checked == true) ||
            (document.getElementById('reg2-3').checked == true) ||
            (document.getElementById('reg2-4').checked == true) ||
            (document.getElementById('reg2-5').checked == true) ||
            (document.getElementById('reg2-6').checked == true) ) && 
            (document.getElementById('reg2').checked   == false))
    {
        //West Coast
        var hidden_reg2_4 = create_hidden_input( 'hidden_reg2_4' , 'region' , '4' );
        rental_advance_search.appendChild( hidden_reg2_4 )
    }
    
    if(  (  (document.getElementById('reg5-1').checked == true) ||
            (document.getElementById('reg5-2').checked == true) ||
            (document.getElementById('reg5-3').checked == true) ||
            (document.getElementById('reg5-4').checked == true) ||
            (document.getElementById('reg5-5').checked == true) ||
            (document.getElementById('reg5-6').checked == true) ) && 
            (document.getElementById('reg5').checked   == false))
    {
        //West Coast
        var hidden_reg5_6 = create_hidden_input( 'hidden_reg5_6' , 'region' , '6' );
        rental_advance_search.appendChild( hidden_reg5_6 )
    }
    
    if(  (  (document.getElementById('reg3-1').checked == true) ||
            (document.getElementById('reg3-2').checked == true) ||
            (document.getElementById('reg3-3').checked == true) ||
            (document.getElementById('reg3-4').checked == true) ) && 
            (document.getElementById('reg3').checked   == false))
    {
        //East Coast
        var hidden_reg3_5 = create_hidden_input( 'hidden_reg3_5' , 'region' , '5' );
        rental_advance_search.appendChild( hidden_reg3_5 )
    }
    
    if(  (  (document.getElementById('reg4-1').checked == true) ||
            (document.getElementById('reg4-2').checked == true) ||
            (document.getElementById('reg4-3').checked == true) ||
            (document.getElementById('reg4-4').checked == true) ) && 
            (document.getElementById('reg4').checked   == false))
    {
        //Town & Kathu
        var hidden_reg4_7 = create_hidden_input( 'hidden_reg4_7' , 'region' , '7' );
        rental_advance_search.appendChild( hidden_reg4_7 )
    }
}

function update_location( district_name ) {

	    var location_dropdown =   document.getElementById('location');
	    location_dropdown.innerHTML = '';
	    var district_name_sort = new Array();

	    if( district_name == '*' || district_name == ''){
	        //All
	        all_location_list_object = new Array;
	        all_location_list_object = LOCATION;
	        
	        for ( var current_all_location_object in all_location_list_object ) {

	            location_list_object = new Array;
	            location_list_object = LOCATION[current_all_location_object];

	            for( var location_index=0; location_index< location_list_object.length;location_index++  ){

	                location_data_object = new Object;
      	  		    location_data_object = location_list_object[ location_index ];

      	  		    for ( var current_location_name in location_data_object ){
      	  		        district_name_sort.push(current_location_name)
      	  	        }
	            }
	            
	        }
            
            district_name_sort.sort();
	        district_name_sort.unshift( "District", "All" );
	        
            for( var district_index=0; district_index < district_name_sort.length;district_index++  ){
	        
	            var location_name = district_name_sort[district_index];

	            if( location_name == 'District'){
	                location_option = create_select_option( location_name , '' );
	            }else if( location_name == 'All'){
	                location_option = create_select_option( location_name , '*' );
	            }else{
	                location_option = create_select_option( location_name , location_name );
	            }
	            location_dropdown.appendChild( location_option );

	        }
	        

	    }else{
	        
	        location_list_object = new Array();
	        location_list_object = LOCATION[district_name];

	        location_option_0 = new Option("District","")
	        location_dropdown.options[0] = location_option_0;

	        for( var location_index=0; location_index< location_list_object.length;location_index++  ){
	        
      	  		location_data_object = new Array;
      	  		location_data_object = location_list_object[ location_index ];
 
      	  	    for ( var current_location_name in location_data_object ){
      	  		    location_option = create_select_option( current_location_name , current_location_name );
      	      	    location_dropdown.appendChild( location_option );
      	  	    }
	        }
	    }
}


function update_rental_location( district_name ) {

	    var location_dropdown =   document.getElementById('location');
	    location_dropdown.innerHTML = '';
	    var district_name_sort = new Array();

	    if( district_name == '*' || district_name == ''){
	        //All
	        all_location_list_object = new Array;
	        all_location_list_object = RENTAL_LOCATION;
	        
	        for ( var current_all_location_object in all_location_list_object ) {

	            location_list_object = new Array;
	            location_list_object = RENTAL_LOCATION[current_all_location_object];

	            for( var location_index=0; location_index< location_list_object.length;location_index++  ){

	                location_data_object = new Object;
      	  		    location_data_object = location_list_object[ location_index ];

      	  		    for ( var current_location_name in location_data_object ){
      	  		        district_name_sort.push(current_location_name)
      	  	        }
	            }
	            
	        }
	        
	        district_name_sort.sort();
	        district_name_sort.unshift( "District", "All" );
            
            for( var district_index=0; district_index < district_name_sort.length;district_index++  ){
	        
	            var location_name = district_name_sort[district_index];

	            if( location_name == 'District'){
	                location_option = create_select_option( location_name , '' );
	            }else if( location_name == 'All'){
	                location_option = create_select_option( location_name , '*' );
	            }else{
	                location_option = create_select_option( location_name , location_name );
	            }
	            location_dropdown.appendChild( location_option );

	        }
	        

	    }else{
	        
	        location_list_object = new Array();
	        location_list_object = RENTAL_LOCATION[district_name];

	        location_option_0 = new Option("District","")
	        location_dropdown.options[0] = location_option_0;

	        for( var location_index=0; location_index< location_list_object.length;location_index++  ){
	        
      	  		location_data_object = new Array;
      	  		location_data_object = location_list_object[ location_index ];
 
      	  	    for ( var current_location_name in location_data_object ){
      	  		    location_option = create_select_option( current_location_name , current_location_name );
      	      	    location_dropdown.appendChild( location_option );
      	  	    }
	        }
	    }
}

function create_rental_district_label(){
        
    rental_district_name = new Array();
        
    all_location_list_object = new Array;
	all_location_list_object = RENTAL_LOCATION;
	     
	for ( var current_all_location_object in all_location_list_object ) {

        location_list_object = new Array;
        location_list_object = RENTAL_LOCATION[current_all_location_object];
        location_list_object.sort();
                
        for( var location_index=0; location_index< location_list_object.length;location_index++  ){

            location_data_object = new Object;
            location_data_object = location_list_object[ location_index ];

            for ( var current_location_name in location_data_object ){
                rental_district_name.push(current_location_name)
            }
        }
	            
    }
	rental_district_name.sort();
	rental_district_name.unshift( "District", "All" );

    return rental_district_name;

}

function create_rental_district_value(){
        
    rental_district_value = new Array();
        
    all_location_list_object = new Array;
	all_location_list_object = RENTAL_LOCATION;
	     
	for ( var current_all_location_object in all_location_list_object ) {

        location_list_object = new Array;
        location_list_object = RENTAL_LOCATION[current_all_location_object];
        location_list_object.sort();
                
        for( var location_index=0; location_index< location_list_object.length;location_index++  ){

            location_data_object = new Object;
            location_data_object = location_list_object[ location_index ];

            for ( var current_location_name in location_data_object ){
                rental_district_value.push(current_location_name)
            }
        }
	            
    }
	rental_district_value.sort();
	rental_district_value.unshift( "", "*" );

    return rental_district_value;

}

function create_property_district_label(){
        
    property_district_name = new Array();
        
    all_location_list_object = new Array;
	all_location_list_object = LOCATION;
	     
	for ( var current_all_location_object in all_location_list_object ) {

        location_list_object = new Array;
        location_list_object = LOCATION[current_all_location_object];
        location_list_object.sort();
                
        for( var location_index=0; location_index< location_list_object.length;location_index++  ){

            location_data_object = new Object;
            location_data_object = location_list_object[ location_index ];

            for ( var current_location_name in location_data_object ){
                property_district_name.push(current_location_name)
            }
        }
	            
    }
	property_district_name.sort();
	property_district_name.unshift( "District", "All" );

    return property_district_name;

}
function create_property_district_value(){
        
    property_district_value = new Array();
        
    all_location_list_object = new Array;
	all_location_list_object = LOCATION;
	     
	for ( var current_all_location_object in all_location_list_object ) {

        location_list_object = new Array;
        location_list_object = LOCATION[current_all_location_object];
        location_list_object.sort();
                
        for( var location_index=0; location_index< location_list_object.length;location_index++  ){

            location_data_object = new Object;
            location_data_object = location_list_object[ location_index ];

            for ( var current_location_name in location_data_object ){
                property_district_value.push(current_location_name)
            }
        }
	            
    }
	property_district_value.sort();
	property_district_value.unshift( "", "*" );

    return property_district_value;

}

function create_select_option( option_text ,option_value )
{
    
    var tag_option  = document.createElement("option");
    var att_value   = document.createAttribute("value");
    att_value.value = option_value;

    tag_option.setAttributeNode(att_value);
    tag_option.innerHTML = option_text;
       
    return tag_option;
}
