/* Prevent caching and setup the loading animated gif */
//$.ajaxSetup ({
//        cache: false
//});

var ajax_load = "<img src='images/loader.gif' alt='loading........' />";

function ajax_error_message(){
        alert("We are unable to process your request at this time.  Please try again later. Thank you.");
}

function conf(){
        return confirm("Are you sure you wish to delete this record?");
}

function v_email(ref) {
    var flag = true;   
    if(ref != "") { // don't check the pattern if there is no phone number entered
        var pattern = new RegExp(/^[A-Z0-9a-z._\-+*=?&'\/]*@[A-Z0-9a-z.\-]*\.[a-z]*$/);
        flag = pattern.test(ref);
    }    
    return flag;
}

function v_phone(ref) {
    var flag = true;     
    if(ref != "") { // don't check the pattern if there is no phone number entered        
       var pattern = new RegExp(/^[0-9+()\- ]*$/);
        flag = pattern.test(ref);        
    }
    return flag;
}

function b2I(bool){
        return (bool)? 1:0;
}

/* Checks the value currently entered into the text field. If it's empty reset it to the
   desired message.  If focus comes onto the field and the message is there then it is removed. */
function checkTextValue(state, message) {        
    field = document.getElementById('searchBoxText')
    if(state == "on") {
        if(field.value == message){
            field.value = "";
        }
    }
    else if(state == "off"){
        if(field.value == ""){
            field.value = message;
        }
    }    
}

$(document).ready(function(){
    $('.home_ta').autocomplete({      
        minLength: 3,
        dataType: 'jsonp',
        delay: 200,
        source: function(request, response) {
            $.ajax({
                url: "/scripts/city_search.php",                
                data: {
                    s: request.term
                },
                success: function(data) {               
                    test = clean_data(data).split('","');                
                    response(test);
                }
            });            
          }
   });
    
        $('.home_ta').keypress(function(event){        
                c = event.which ? event.which : event.keyCode;            
                if(c == 13) {                    
                        event.preventDefault();        
                        var travel = $(".home_ta").val()
                        if(travel != "Type in your destination...") {                
                            var city_page = $.ajax ({
                                url: "/scripts/city_search.php",                    
                                data: "c="+ travel,
                                success: function(msg){
                                    window.location.href = msg;   
                                }                    
                            });                
                        }
                }                
        });
   
   $('#search_btn2').click(function(event) {
        event.preventDefault();
        var travel = $(".home_ta").val()
        if(travel != "Type in your destination...") {                
            var city_page = $.ajax ({
                url: "/scripts/city_search.php",                    
                data: "c="+ travel,
                success: function(msg){
                    window.location.href = msg;   
                }                    
            });                
        }
   });   
   
   $(".chg_city").click(function(event) {
        event.preventDefault();
        $("#city_search").slideToggle('slow');
   });
});

function countryFormat(cName) {
        names = cName.split("-");
        fixedNames = "";
        for(i = 0; i < names.length; i++) {
                fLetter = (names[i].substr(0, 1)).toUpperCase();
                word = names[i].substr(1);
                fixedNames += fLetter + word + " ";
                if(i + 1 < names.length) {
                        fixedNames += " ";
                }               
        }
        return fixedNames;
}

/* Prepares an address to be submitted to google by stripping tags, and removings spacing */
function url_format(addy){
    addy = addy.replace(/ /g, "+");
    fixed = addy.replace(/,/g, "");
    fixed = addy.replace(/"/g, "\\\"");    
    return "http://maps.google.com?q=" + fixed + "t=h&z=16";
}

function changeImage(ref){    
    $('.h_cur_img').attr('src', ref);
}

function searchBlog(){
    document.getElementById('blog_srch').submit();
}

function clean_data(data) {
    regex1 = new RegExp(/\["/);
    regex2 = new RegExp(/"\]/);
    regex3 = new RegExp(/\[/);
    regex4 = new RegExp(/\]/);
    data = data.replace(regex1, "");
    data = data.replace(regex2, "");
    data = data.replace(regex3, "");        
    return data.replace(regex4, "");
}
