﻿
function BoDauTiengViet(str) {  
  str = str.toLowerCase();  
  str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a");  
  str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,"e");  
  str = str.replace(/ì|í|ị|ỉ|ĩ/g,"i");  
  str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o");  
  str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u");  
  str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y");  
  str = str.replace(/đ/g,"d");  
  str = str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g,"-"); 
  /* tìm và thay thế các kí tự đặc biệt trong chuỗi sang kí tự - */ 
  str = str.replace(/-+-/g,"-"); //thay thế 2- thành 1- 
  str = str.replace(/^\-+|\-+$/g,"");
  //cắt bỏ ký tự - ở đầu và cuối chuỗi  
  
  return str;
}

function TrimString(sInString) {
    sInString = sInString.replace(/^\s+/g, ""); // strip leading
    return sInString.replace(/\s+$/g, ""); // strip trailing
}


// Replaces all instances of the given substring.
String.prototype.replaceAll = function(
            strTarget, // The substring you want to replace
            strSubString // The string you want to replace in.
            ) {
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1) {
        // Relace out the current instance.
        strText = strText.replace(strTarget, strSubString)

        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf(strTarget);
    }

    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return (strText);
}
function addCommas(nStr) {
    nStr = nStr.replaceAll('.', '');
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? ',' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + '.' + '$2');
    }
    document.getElementById('txtpricetour').value = (x1 + x2);

}
function addCommasTo(nStr) {
    nStr = nStr.replaceAll('.', '');
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? ',' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + '.' + '$2');
    }
    document.getElementById('txtTopricetour').value = (x1 + x2);

}

function addCommas1(nStr) {
    nStr = nStr.replaceAll('.', '');
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? ',' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + '.' + '$2');
    }

    return x1 + x2;
}

function isNull(str) {
    if (str == null) return true;

    var NumberOfChar = str.length;
    for (var i = 0; i < NumberOfChar; i++) {
        if (str.charAt(i) != ' ') return false;
    }
    return true;
}

function IsNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;

    if (sText == '') return false;

    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}

function SearchHotel() {

    var selected_index = document.getElementById('select_TP').selectedIndex;
    var selected_TP = document.getElementById('select_TP').options[selected_index].value;

    selected_index = document.getElementById('select_LKS').selectedIndex;
    var selected_LKS = document.getElementById('select_LKS').options[selected_index].value;

    if (selected_TP == '' || selected_TP == null) {
        alert("Chưa có dữ liệu quốc gia và tỉnh/thành phố");
        return;
    }

    var FromPrice = window.document.getElementById('PriceHotelFrom').value;
    var ToPrice = window.document.getElementById('PriceHotelTo').value;

    if (IsNumeric(FromPrice) == false) {
        alert('Bạn nhập Từ giá không phải kiểu số.');
        window.document.getElementById('PriceHotelFrom').focus();
        return;
    }

    if (IsNumeric(ToPrice) == false) {
        alert('Bạn nhập Đến giá không phải kiểu số.');
        window.document.getElementById('PriceHotelTo').focus();
        return;
    }

    var numberFromPrice = parseInt(FromPrice.replaceAll('.', ''));
    var numberToPrice = parseInt(ToPrice.replaceAll('.', ''));
    if (numberToPrice < numberFromPrice) {
        alert('Bạn nhập Từ giá lớn hơn Đến giá.');
        window.document.getElementById('PriceHotelFrom').focus();
        return;
    }

    // OK
    window.location.href = location.protocol + '//' + location.host + "/Hotel/ListHotel_Search/"
                           + selected_TP + '/' + selected_LKS + '/' + numberFromPrice + '/'
                           + numberToPrice + '/10/1/Price_Between_' + FromPrice + '&' + ToPrice + '.html';
}
//tour

function Quitpage() {
    var urlquit = document.location.href;
    var urlpage = location.protocol + '//' + location.host + "/Tour/" + "Finish.aspx";
    if (urlquit == urlpage) {
        alert('Thông tin về booking đã được chuyển về email của bạn. \r\n Bạn kiểm tra lại thông tin booking trong email.');
    }
}
