// Source Script from mVn 5.0 website : www.mangvietnam.info

var util = {

  shouldDebug: false,

  // Note: Will fail in pathological cases (where the members contain
  // strings similar to describe() result).
  membersEqual: function(array1, array2) {
    return util.describe(array1)==util.describe(array2);
  },

  describe: function(obj) {
    if (obj==null) { return null; }
    switch(typeof(obj)) {
      case 'object': {
        var message = "";
        for (key in obj) {
          message += ", [" + key + "]: [" + obj[key] + "]";
        }
        if (message.length > 0) {
          message = message.substring(2); // chomp initial ', '
        }
        return message;
      }
      default: return "" + obj;
    }
  },

  debug: function(message) {
      if (this.shouldDebug) {
        alert("AjaxJS Message:\n\n" + message);
      }
  },

  error: function(message) {
      if (this.shouldDebug) {
        alert("AjaxJS ERROR:\n\n" + message);
      }
  },

  // trim() From Shawn Milo
  // https://lists.latech.edu/pipermail/javascript/2004-May/007567.html
  trim: function(str) {
    return str.replace(/^(\s+)?(.*\S)(\s+)?$/, '$2');
  },

  strip: function(str) {
    return str.replace(/\s+/, "");
  }

}

function $() {

    var elements = new Array();

    for (var i = 0; i < arguments.length; i++) {

      var element = arguments[i];

      if (typeof element == 'string') {
        if (document.getElementById) {
          element = document.getElementById(element);
        } else if (document.all) {
          element = document.all[element];
        }
      }

      elements.push(element);

    }

    if (arguments.length == 1 && elements.length > 0) {
      return elements[0];
    } else {
      return elements;
    }
}

function $C(elType) {
  return document.createElement(elType);
}

// From prototype library. Try.these(f1, f2, f3);
var Try = {
  these: function() {
    var returnValue;
    for (var i = 0; i<arguments.length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) {}
    }
    return returnValue;
  }
}

function getElementsByClassName(classname) {
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = document.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function extractIFrameBody(iFrameEl) {

  var doc = null;
  if (iFrameEl.contentDocument) { // For NS6
    doc = iFrameEl.contentDocument; 
  } else if (iFrameEl.contentWindow) { // For IE5.5 and IE6
    doc = iFrameEl.contentWindow.document;
  } else if (iFrameEl.document) { // For IE5
    doc = iFrameEl.document;
  } else {
    alert("Error: could not find sumiFrame document");
    return null;
  }
  return doc.body;

}

var DELAY = 1000;
var steps = 0;
function andThen(action) {
  var delayTime = (++steps * DELAY);
  setTimeout(action, delayTime);
}

function log(message) {
  $("log").innerHTML += message + "<br/>";
}

function createXMLHttpRequest() {
  try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
  try { return new XMLHttpRequest();                   } catch(e) {}
  alert("XMLHttpRequest not supported");
  return null;
}

/*window.onload = function() {
	getBanner('1');
	getDetail('?pc=home&p=home');
	getRightDetail('?pc=tour&p=hot');	
}*/
function onServerResponse(html, headers, elementId) {
	window.scroll(0,0);
  $(elementId).innerHTML = html;
}

function getDetail(url){
	$('bigBanner').innerHTML = '';
	ajaxCaller.get(url, null, onServerResponse, false, "mainDetail");
}

function getBanner(id){
	ajaxCaller.get('index.php?pc=advert&p=top&id='+id, null, onServerResponse, false, "bigBanner");
}

function getHotTypeTour(id){
	ajaxCaller.get('index.php?pc=tour&p=hot_type&pid='+id, null, onServerResponse, false, "bigBanner");
}

function getRightDetail(url){

	var rightId = 'rightDetail';
	ajaxCaller.get(url, null, onServerResponse, false, "rightDetail");
}

function openImage(img,h,w){
	window.scroll(0,0);
	if(w>510){
		$('bigBanner').innerHTML = '<span style="width:100%;text-align:center"><br><img src="' +img + '" width="510" border="0"  align="middle"/></span>';
	}else{
		$('bigBanner').innerHTML = '<span style="width:100%;text-align:center"><br><img src="' +img + '" width="' +w + '" border="0"  align="middle"/></span>';
	}
}

function searchHotel(obj) {
      var getstr = "";
			
			if(obj.keyHotel.value != "")getstr = getstr + "&key=" + obj.keyHotel.value;
			if(obj.zone.options[obj.zone.selectedIndex].value != "")getstr = getstr + "&zone=" + obj.zone.options[obj.zone.selectedIndex].value;
			if(obj.rate.options[obj.rate.selectedIndex].value != "")getstr = getstr + "&rate=" + obj.rate.options[obj.rate.selectedIndex].value;
			if(obj.priceH.options[obj.priceH.selectedIndex].value != "")getstr = getstr + "&price=" + obj.priceH.options[obj.priceH.selectedIndex].value;
			
      getDetail('index.php?pc=hotel&p=search' + getstr);
   }
	 
	 function searchTour(obj) {
     // var getstr = "";
			obj.action = 'index.php?pc=tour&p=search' ;
			obj.submit();
			//if(obj.keyTour.value != "")getstr = getstr + "&key=" + obj.keyTour.value;
			//if(obj.type.options[obj.type.selectedIndex].value != "")getstr = getstr + "&type=" + obj.type.options[obj.type.selectedIndex].value;
			//if(obj.dur.options[obj.dur.selectedIndex].value != "")getstr = getstr + "&dur=" + obj.dur.options[obj.dur.selectedIndex].value;
			//if(obj.priceT.options[obj.priceT.selectedIndex].value != "")getstr = getstr + "&price=" + obj.priceT.options[obj.priceT.selectedIndex].value;
			
     // getDetail('index.php?pc=tour&p=search' + getstr);
   }
   
    function sendFeedBack() {
      var obj = document.postComment; 
	  var getstr = "";
			
			if(obj.name.value != ""){getstr = getstr + "&name=" + obj.name.value;}
			else{
				alert("Please enter your name !");
				obj.name.focus();
				return false;
			}
			if(obj.email.value != ""){getstr = getstr + "&email=" + obj.email.value;}
			else{
				alert("Please enter your Email !");
				obj.email.focus();
				return false;
			}
			if(obj.title.value != ""){getstr = getstr + "&title=" + obj.title.value;}
			else{
				alert("Please enter subject !");
				obj.title.focus();
				return false;
			}
			if(obj.comment.value != ""){getstr = getstr + "&comment=" + obj.comment.value;}
			else{
				alert("Write your comments, please !");
				obj.comment.focus();
				return false;
			}
			
      	getDetail('index.php?pc=comment&p=post' + getstr);
   }
	 function open_window(the_url, width, height){
	top_val		= (screen.height - height)/2 - 30;
	if (top_val < 0){ top_val	= 0; }
	left_val	= (screen.width - width)/2;

	var new_win = window.open(the_url, "", "toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=1,width="+ width +",height="+ height +", top="+ top_val +",left="+ left_val);
}
