function checkLoginId(obj) {
    try {
        if(obj.value != '') {
            var query = 'index.php?view=ajax&action=checkloginid&id=&value=' + obj.value;
            var loader = new net.ContentLoader(query, parseCheckLoginId);
        }   
    } catch(e) {
        alert(e.message);
    }
}

function parseCheckLoginId() {
    try {
        var answer = this.req.responseText;
        var obj = document.getElementById('newuser');
        if(obj != null) {        
            if(parseInt(answer) > 0) {
                alert('This username already exists, please re-enter!');
                obj.focus();
                obj.style.backgroundColor = '#ff0000';
                obj.style.color = '#ffffff';
            } else {
                obj.style.backgroundColor = '#ffffff';
                obj.style.color = '#000000';
            }
        }    
    } catch(e) {
        alert(e.message);
    }
}

function clearSelectOptions(elName) {
    var obj = document.getElementById(elName);
    if(obj != null) {
        while(obj.options.length > 0) {
            obj.options[0] = null;
        }
    }
}

function setSelectOptions(elName, strpairs) {
    var obj = document.getElementById(elName);
    if(obj != null) {
        var cnt = 0;
        var pairs = strpairs.split("|");
        for(var i = 0; i < pairs.length; i++) {
            var items = pairs[i].split("~");
            if(items[0] != "undefined") {
                var opt = new Option();
                opt.value = items[0];
                opt.text = items[1];
                obj.options[cnt++] = opt;
            }
        }
        obj.options.length = cnt;
    }
}        

function getSelectValue(elName) {
    var obj = document.getElementById(elName);
    if(obj != null) {
        return(obj.options[obj.selectedIndex].value);
    } else {
        return('');
    }
}

function getRegionsForCountry() {
    try {
        clearSelectOptions('region_id');
        clearSelectOptions('city_id');
        var query = "index.php?view=ajax&action=regions&country=" + getSelectValue('country_id');
        var loader = new net.ContentLoader(query, function() {
            setSelectOptions('region_id', this.req.responseText);
        });
    } catch(e) {
        alert(e.message);
    }
}

function getCitiesForRegion() {
    try {
        clearSelectOptions('city_id');
        var query = "index.php?view=ajax&action=cities&region=" + getSelectValue('region_id') +
                                                    "&country=" + getSelectValue('country_id');
        var loader = new net.ContentLoader(query, function() {
            setSelectOptions('city_id', this.req.responseText);
        });
    } catch(e) {
        alert(e.message);
    }
}

function doAddNewRegion() {
    try {
        var newRegion = prompt("Add A New Region/State/Province/County...", "");
        if(newRegion != null) {            
            var query = "index.php?view=ajax&action=addregion&value=" + newRegion + "&country=" + getSelectValue('country_id');
            var loader = new net.ContentLoader(query, function() {
                var answer = this.req.responseText;
                if(answer == '') {
                    alert('Region is invalid or is already on file');
                } else {
                    var obj = document.getElementById("region_id");
                    if(obj != null) {
                        var parts = answer.split('|');
                        var opt = new Option();
                        lnth = obj.options.length;
                        obj.options[lnth] = opt;
                        obj.options[lnth].value = parts[0];
                        obj.options[lnth].text = parts[1];
                        obj.options.selectedIndex = lnth;
                    }
                    clearSelectOptions('city_id');
                }                
            });
        }
    } catch(e) {
        alert(e.message);
    }   
}

function doAddNewCity() {
    try {
        var newCity = prompt("Add A New City", "");
        if(newCity != null) {            
            var query = "index.php?view=ajax&action=addcity" +
                "&value=" + newCity + 
                "&region=" + getSelectValue('region_id') +
                "&country=" + getSelectValue('country_id');
            var loader = new net.ContentLoader(query, function() {
                var answer = this.req.responseText;
                if(answer == '') {
                    alert('City is invalid or is already on file');
                } else {
                    var obj = document.getElementById("city_id");
                    if(obj != null) {
                        var parts = answer.split('|');
                        var opt = new Option();
                        lnth = obj.options.length;
                        obj.options[lnth] = opt;
                        obj.options[lnth].value = parts[0];
                        obj.options[lnth].text = parts[1];
                        obj.options.selectedIndex = lnth;
                    }
                }                
            });
        }
    } catch(e) {
        alert(e.message);
    }   
}

function doDelayedAddNewRegion() {
    try {
        var newRegion = prompt("Add A New Region/State/Province/County...", "");
        if(newRegion != null) {            
            var obj = document.getElementById("region_id");
            if(obj != null) {
                var opt = new Option();
                lnth = obj.options.length;
                obj.options[lnth] = opt;
                obj.options[lnth].value = newRegion;
                obj.options[lnth].text = newRegion;
                obj.options.selectedIndex = lnth;
            }
            clearSelectOptions('city_id');
        }
    } catch(e) {
        alert(e.message);
    }   
}

function doDelayedAddNewCity() {
    try {
        var newCity = prompt("Add A New City", "");
        if(newCity != null) {            
            var obj = document.getElementById("city_id");
            if(obj != null) {
                var opt = new Option();
                lnth = obj.options.length;
                obj.options[lnth] = opt;
                obj.options[lnth].value = newCity;
                obj.options[lnth].text = newCity;
                obj.options.selectedIndex = lnth;
            }
        }
    } catch(e) {
        alert(e.message);
    }   
}


function getRelatedRegionsForCountry() {
    try {
        clearSelectOptions('rel_region_id');
        clearSelectOptions('rel_city_id');        
        clearSelectOptions('related_id');
        var query = "index.php?view=ajax&action=regions&country=" + getSelectValue('rel_country_id');
        var loader = new net.ContentLoader(query, function() {
            setSelectOptions('rel_region_id', this.req.responseText);                    
        });
    } catch(e) {
        alert(e.message);
    }
}

function getRelatedCitiesForRegion() {
    try {
        clearSelectOptions('rel_city_id');        
        clearSelectOptions('related_id');
        var query = "index.php?view=ajax&action=cities" +
                    "&country=" + getSelectValue('rel_country_id') +
                    "&region=" + getSelectValue('rel_region_id');
        var loader = new net.ContentLoader(query, function() {
            setSelectOptions('rel_city_id', this.req.responseText);                    
        });
    } catch(e) {
        alert(e.message);
    }
}

function doAddNewRelatedRegion() {
    try {
        var newRegion = prompt("Add A New Region", "");
        if(newRegion != null) {
            var query = "index.php?view=ajax&action=addregion&value=" + newRegion + 
                        "&country=" + getSelectValue('rel_country_id');
            var loader = new net.ContentLoader(query, function() {
                var answer = this.req.responseText;
                if(answer == '') {
                    alert('Region is invalid or is already on file');
                } else {
                    var obj = document.getElementById("rel_region_id");
                    if(obj != null) {
                        var parts = answer.split('|');
                        var opt = new Option();
                        lnth = obj.options.length;
                        obj.options[lnth] = opt;
                        obj.options[lnth].value = parts[0];
                        obj.options[lnth].text = parts[1];
                        obj.options.selectedIndex = lnth;
                    }
                }           
            });
        }
    } catch(e) {
        alert(e.message);
    }    
}

function doAddNewRelatedCity() {
    try {
        var newCity = prompt("Add A New City", "");
        if(newCity != null) {
            var query = "index.php?view=ajax&action=addcity&value=" + newCity + 
                        "&country=" + getSelectValue('rel_country_id') +
                        "&region=" + getSelectValue('rel_region_id');
            var loader = new net.ContentLoader(query, function() {
                if(answer == '') {
                    alert('City is invalid or is already on file');
                } else {
                    var obj = document.getElementById("rel_city_id");
                    if(obj != null) {
                        var parts = answer.split('|');
                        var opt = new Option();
                        lnth = obj.options.length;
                        obj.options[lnth] = opt;
                        obj.options[lnth].value = parts[0];
                        obj.options[lnth].text = parts[1];
                        obj.options.selectedIndex = lnth;
                    }
                }            
            });
        }
    } catch(e) {
        alert(e.message);
    }
    
}

function getPanoramasForCity() {
    clearSelectOptions('related_id');
    var query = "index.php?view=ajax&action=citypanos" +
        "&country=" + getSelectValue('rel_country_id') +
        "&region=" + getSelectValue('rel_region_id') +
        "&city=" + getSelectValue('rel_city_id');
    var loader = new net.ContentLoader(query, function() {
        setSelectOptions('related_id', this.req.responseText);
    });
        
}

