

String.prototype.lenB = function(){
	return this.replace(/[^\x00-\xff]/g,"**").length;
}

String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g, "").replace(/^[\s¡¡\t]+|[\s¡¡\t]+$/, "");
}

Array.prototype.indexOf = function (subString) {
    for (var i = 0; i < this.length; i++) {
        if (this[i].trim() == subString.trim()) {
            return i;
        }
    }
    return -1;
}

window.create = function (url, width, height, title, scrollbar, win, isModal) {
    width = width || 600;
    height = height || 400;
    var left = (window.screen.width - width) / 2;
    var top = (window.screen.height - height) / 2;
    var scrollbar = scrollbar || "no";
    window.openUrl = url;
    if (!isModal)
        var winPopup = window.open(url, win,"scrollbars=" + scrollbar + ",channelmode=no,location=no,menubar=no,resizable=yes,status=no,titlebar=no,directories=no,fullscreen=no,toolbar=no,left="+left+",top="+top+",width="+width+",height="+height);
    else
        var winPopup = window.showModelessDialog(url, window,"status:no;center:yes;scroll:" + scrollbar + ";unadorned:no; dialogWidth:" + width + "px;dialogHeight:" + height + "px");
    try
    {
        winPopup.document.title = title || "New window";
    } catch(e) { };
    winPopup.focus();
    return winPopup;
}

function _ () {}

function escapeHTML (str) {
   var div = document.createElement('div');
   var text = document.createTextNode(str);
   div.appendChild(text);
   return div.innerHTML;
}

function unescapeHTML(html) {   
    var htmlNode = document.createElement("DIV");   
    htmlNode.innerHTML = html;   
    if (document.all)
        return htmlNode.innerText;
    else
        return htmlNode.textContent;
}

function show_picture(HotelId) {
    window.create("../Get_Photos.aspx?HotelID=" + HotelId, 600, 400, null, "yes", "picture");
}

function show_map(MapURL, Title) {
    window.create(MapURL, 600, 400, Title, null, "map");
}

function show_book(BookURL) {
    window.create(BookURL, 900, 600, null, "yes", "book");
}

function show_video(BookURL) {
    window.create(BookURL, 340, 340, "Show 360", null, "video");
}

function show_customer(BookURL) {
    window.create(BookURL, 570, 425, null, null, "customer");
}

function show_speak(BookURL) {
    window.create(BookURL, 250, 220, null, null, "speak");
}

function sortMe(oSel){
	var ln = oSel.options.length;
	var selectedValue;
	var arrNames = [];
	var arrValues = {};
	for (var i = 0; i < ln; i++)
	{
		arrNames[i] = oSel.options[i].innerHTML;
		arrValues[arrNames[i]] = oSel.options[i].value;
		if (oSel.options[i].selected) {
		    selectedValue = oSel.options[i].value;
		}
	}
    if (I18N.lang == 'zh-CN') {
        arrNames.sort(function(a,b){return a.localeCompare(b)});
    } else {
        arrNames.sort();
    }
	//arrNames.sort();
	while (ln--)
	{
		oSel.options[ln] = null;
	}
	for (i = 0; i < arrNames.length; i++)
	{
	    oSel.options.add(new Option(arrNames[i], arrValues[arrNames[i]]));
	    if (arrValues[arrNames[i]] == selectedValue) {
	        oSel.options[i].selected = true;
	    }
	}
}

function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
    //deleteCookie(cookieName);
    document.cookie =
        escape(cookieName) + '=' + escape(cookieValue)
        + (expires ? '; expires=' + expires.toUTCString() : '')
        + (path ? '; path=' + path : '')
        + (domain ? '; domain=' + domain : '')
        + (secure ? '; secure' : '');
}

function getCookie(cookieName){
  var aryCookie = document.cookie.split("; ");
  for (var i=0; i < aryCookie.length; i++){
    var aryElement = aryCookie[i].split("=");
    if (cookieName == aryElement[0]) 
      return unescape(aryElement[1]);
  }
  return null;
}

function deleteCookie(name){
       var date=new Date();
       date.setTime(date.getTime()-10000);
       document.cookie=name+"=v; expire="+date.toGMTString();
}

function clearCookie(cookieName) {
 var now = new Date();
 var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
 setCookie(cookieName, "", yesterday);
}

function getSmallImageUrl (url) {
    if (typeof url == "undefined" || url == "" || url == null)
        return "/gif/no-picture.gif";
    else if (url.toLowerCase().indexOf("http://") != -1)
        return url;
    else if (url.toLowerCase().indexOf("images/") != -1)
        return "http://" + location.host + "/" + url.replace("images/", "images/small/");
    else if (url.toLowerCase().indexOf("videos/") != -1)
        return "http://" + location.host + "/" + url;
    else if (url.toLowerCase().indexOf("pictures/") != -1)
        return "http://" + location.host + "/" + url.replace(".jpg", "_small.gif");
    else
        return url;
}

function getImageUrl (url) {
    if (typeof url == "undefined" || url == "")
        return "/gif/no-picture.gif";
    else if (url.toLowerCase().indexOf("http://") != -1)
        return url;
    if (url.toLowerCase().indexOf("images/") != -1)
        return "http://" + location.host + "/" + url;
    else if (url.toLowerCase().indexOf("pictures/") != -1)
        return "http://" + location.host + "/" + url;
    else
        return url; //82005432 ËÕ³É
}

function getVideoUrl (url) {
    if (typeof url == "undefined" || url == "") 
        return "/gif/no-picture.gif";
    else if (url.toLowerCase().indexOf("http://") != -1)
        return url;
    if (url.toLowerCase().indexOf("images/video_") != -1)
        return "http://" + location.host + "/" + url;
    else if (url.toLowerCase().indexOf("videos/") != -1)
        return "http://" + location.host + "/" +  url;
    else
        return url;
}

function SearchEventSource() {
	if(document.all) 
		return window.event.srcElement;
	else 	// FF
	    return SearchEventSource.caller.arguments[0].target;
}

function fillSelectControl(ctlId, values, selectedIndex, isSort){
    var selectControl = document.getElementById(ctlId);
    
    //remove all item
    selectControl.length = 0;
    var temp;
    var item;
    var itemText;
    for(var i = 0; i < values.length; i++){
        temp = values[i].split("|");
        itemText = (I18N.lang == "zh-CN") ? temp[1].substring(temp[1].lastIndexOf(" ")) : temp[1].substring(0, temp[1].lastIndexOf(" "));
        item = new Option(unescapeHTML(itemText), temp[0]);
		
		try{
			selectControl.options.add(item);
		}catch(e){
			selectControl.options.add(item, -1);
		}
    }
    if(typeof(selectedIndex) != "undefined"){
        selectControl.options[selectedIndex].selected = true;
    } else {
        selectControl.options[0].selected = true;
    }
    if (isSort != false)
        sortMe(selectControl);
    if (ctlId == "cityName") {
        selectControl.add(new Option("All ²»ÏÞ", "all"));
    }
}
function isDate(d){
	if(d.length > 10 || d.length < 8){
		return false;
	}

	var temp = d.replace(/-/g, "/").split("/");

	if(temp.length != 3){
		return false;
	}
	
	if(temp[0].length != 4 || temp[1].length > 2 || temp[2].length > 2){
		return false;
	}
	
	if(!isPositiveInt(temp[0]) || parseInt(temp[0], 10) < 1000 || parseInt(temp[0], 10) == 0 ||
		!isPositiveInt(temp[1]) || parseInt(temp[1], 10) > 12 || parseInt(temp[1], 10) == 0 ||
		!isPositiveInt(temp[2]) || parseInt(temp[2], 10) > 31 || parseInt(temp[2], 10) == 0){
		return false;
	}
	
	return true;
}

function isPositiveInt(value){
	var returnValue = parseInt(value, 10);
	
	if(!isNaN(returnValue) && returnValue >= 0){
		return true;
	}
	
	return false;
}

function compareDate(d1, d2){
	var temp1 = new Date(d1.replace(/-/g, "/"));
	var temp2 = new Date(d2.replace(/-/g, "/"));
	
	return (temp1 > temp2 ? 1 : (temp1 < temp2 ? 2 : 0));
}

function validateDate(dateValue, title){
    var errorMsg = "";
    var isDateflag = false;

    if(dateValue.trim().length == 0){
        errorMsg += notNullErrorMsg.replace("{0}", title) + "\n";
    }else if(!isDate(dateValue)){
        errorMsg += dateFormatErrorMsg.replace("{0}", title) + "\n";
    }else{
        isDateflag = true;
    }
    
    return {errorMsg : errorMsg, isDate : isDateflag};
}

function AddSelOption (sel, content, value) {
    var option = new Option(content, value);
    sel.options.add(option);
}

var currentType = 0;
function flightInternal(type){
    if(currentType == type){
        return;
    }
    
    var citys = null;
    
    if(type == 0){
	    citys = domesticCitys.CN;
    }else if(type == 1){
        citys = internationalCitys;
    }
    
    if(citys != null){
	    fillSelectControl("srriveCityName", citys);
	}
	
	 currentType = type;
}


function flightType(type){
    var display = null;
    
    if(type == 0){
        display = "none";
    }else if(type == 1){
        display = "block";
    }
    
    if(display != null){
        document.getElementById("returnDate").style.display = display;
        document.getElementById("returnTime").style.display = display;
    }
}

function removeParentChild (Node) {
    try
    {
        var removeChild = Node.parentElement;
        var parentChild = removeChild.parentElement;
        parentChild.removeChild(removeChild); 
    }catch (e) {}
}

function displyOrHidden (Node) {
    Node.style.display = (Node.style.display == "block") ? "none" : "block";
}

function checkPaste(o, maxlength, minlength) {
    var max = maxlength || 5000;
    var min = minlength || 0;
    if(window.clipboardData.getData('Text').length + o.value.length>max)
        return false;
    else if(window.clipboardData.getData('Text').length + o.value.length<min)
        return false;
    else
        return true;
}
function checkStr(o, maxlength, minlength) {
    var max = maxlength || 5000;
    var min = minlength || 0;
    if(o.value.length > max)
        return false;
    else if(o.value.length < min)
        return false;
    else
        return true;
}

function OpenCalendar(thisdate, beginDate, ev, method) {

    beginDate = beginDate || new Date();//new Date((new Date()).getYear(), (new Date()).getMonth() + 1, (new Date()).getDate());
    if (thisdate.value.match(/\d{4}-\d{1,2}-\d{1,2}/ig) != null)
        beginDate = new Date(thisdate.value.split('-')[0], thisdate.value.split('-')[1], thisdate.value.split('-')[2]);
    else
        beginDate.setMonth((new Date()).getMonth() + 1);
    var win = window; //(opener || top || parent || self).window;
    if (win.calendar == null) {
        win.calendar = win.document.createElement("iframe");
        with (win.calendar.style) {
            width = "180px";
            height = "120px";
            position = "absolute";
            border = "solid 1px #cccccc";
            backgroundColor = "#ffffff";
            overflow = "hidden";
            //display = "none";
            zIndex = "9000";
            backgroundPosition = "center center";
            backgroundRepeat = "no-repeat";
        }
        win.calendar.frameBorder = "no";
        win.calendar.name = "Calendar";
        win.calendar.style.backgroundImage = "url(../images/loader_green.gif)";
        //win.calendar.src = "http://www.baidu.com";
        win.calendar.update = function (date) { this.sourceElt.value = date; };
        win.calendar.hide = function () { this.style.display = "none"; try { this.sourceElt.onReLoad(); } catch (e) { } };
        win.calendar.onload = function () { this.style.backgroundImage = "none"; };
        win.document.body.appendChild(win.calendar);
    }
    var e = mousePosition(ev || window.event);
    var year = (typeof ActiveXObject != "undefined") ? beginDate.getYear() : beginDate.getYear() + 1900;
    win.calendar.src = "../common/Calendar.html?begindate=" + year + "-" + beginDate.getMonth() + "-" + beginDate.getDate() + "&method=" + method;
    win.calendar.sourceElt = thisdate;
    win.calendar.style.left = e.x + document.documentElement.scrollLeft - 140 + "px";
    win.calendar.style.top = e.y + document.documentElement.scrollTop + "px";
    win.calendar.style.display = "block";
}

function mousePosition(ev){
    if (ev.pageX || ev.pageY) {         
        return {x:ev.pageX, y:ev.pageY};     
    }     
    return { x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
            y:ev.clientY + document.body.scrollTop  - document.body.clientTop }; 
} 
function mouseMove(ev){
    ev = ev || window.event;
    var mousePos = mousePosition(ev);
} 

u = {};
u.onload = function (func) {
    if (u.onload.funcs == null) {
        u.onload.funcs = [];
        if (window.onload != null) {
            u.onload.funcs.push(window.onload);
        }
        window.onload = function () {
            while (u.onload.funcs.pop()() || u.onload.funcs.length > 0);
        }
    }
    u.onload.funcs.push(func);
}
u.funcs = [];
u.ready = function (func) {
    if (typeof(func) == "function")
        u.funcs.push(func);
};
u.nocatch = function (func) {
    if (typeof(func) == "function") func();
};

(function () {
    var timeOut = setInterval(function () {
        if (document.readyState == "complete") {
            //if (u.funcs != null) {
            //    for (var i = 0; i < u.funcs.length; i++) {
            //        u.funcs[i]();
            //    }
            //}
            //alert(u.funcs.length);
            while (u.nocatch(u.funcs.pop()) || u.funcs.length > 0);
            clearInterval(timeOut);
        }
    }, 10);
})();





(function addDOMLoadEvent(func) { 
//   if (!window.__load_events) { 
//      var init = function () { 
//          if (arguments.callee.done) return; 
//  
//          arguments.callee.done = true; 
//      
//          if (window.__load_timer) { 
//              clearInterval(window.__load_timer); 
//              window.__load_timer = null; 
//          } 
//          
//          for (var i=0;i < window.__load_events.length;i++) { 
//              window.__load_events[i](); 
//          } 
//          window.__load_events = null; 
//      }; 
   
      // for Mozilla/Opera9 
      if (document.addEventListener) { 
          document.addEventListener("DOMContentLoaded", function () { document.readyState = "complete"; }, false); 
      } 
      
      // for Internet Explorer 
      /*@cc_on @*/ 
      /*@if (@_win32) 
          document.write("<scr"+"ipt id=__ie_onload defer src=//0><\/scr"+"ipt>"); 
          var script = document.getElementById("__ie_onload"); 
          script.onreadystatechange = function() { 
              if (this.readyState == "complete") { 
                  //document.readyState = "complete";
                  //init();
              } 
          }; 
      /*@end @*/ 
      
      // for Safari 
      if (/WebKit/i.test(navigator.userAgent)) {
          window.__load_timer = setInterval(function() { 
              if (/loaded|complete/.test(document.readyState)) { 
                  document.readyState = "complete";
                  //init(); // call the onload handler 
              } 
          }, 10); 
      } 
      
//      window.onload = init; 
//      
//      window.__load_events = []; 
//   }
//   window.__load_events.push(func); 
})();

(function () {
    if (typeof ActiveXObject == "undefined") {
        window.navigate = function (url) {
            location.href = url;
        }
    }
})();