// JavaScript Document
var fullUrl='http://www.city-discovery.com/';


//---------------------------------------------------------------------------------------------------------------------------------------
// VALIDATIONS
//---------------------------------------------------------------------------------------------------------------------------------------
// FOR EMAIL VALIDATION
function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false
	 }

	 return true					
}

//---------------------------------------------------------------------------------------------------------------------------------------
// FOR NUMERIC VALUE VALIDATION
function Numeric(num) {
     var ValidChars = "0123456789.-+";
	 var IsNumber = true;
	 var Char;

	 for(i=0; i < num.length && IsNumber == true; i++) {
	      Char = num.charAt(i);

		  if(ValidChars.indexOf(Char) == -1) {
		       IsNumber = false;
		  }
	 }
	 return IsNumber;
}
//---------------------------------------------------------------------------------------------------------------------------------------
// OPEN WINDOW POP-UP
function openWindow(theURL,winName){
	var features="'resizable=yes,scrollbars=yes,width=600,height=400,left=350,top=140'";
	window.open(theURL,winName,features);
}
//---------------------------------------------------------------------------------------------------------------------------------------
function clearValue(id) {
	document.getElementById(id).value = "";
}

//---------------------------------------------------------------------------------------------------------------------------------------
// VALIDATE FORM
function validate_form()
{
	var frm = document.form1;
	
	if(frm.adults.value=="")
	{
		alert("Please provide us the number of adults for the tour.");
		frm.adults.focus();
	}
	else if(frm.orderdate.value=='')
	{
		alert('Please provide us the date of the tour.');
		frm.orderdate.focus();
	}
	else if(parseInt(frm.adults.value) / parseInt(frm.adults.value) != 1 )
	{
		alert("Please provide us the number of adults for the tour.");
		frm.adults.focus();	
	}
	else 
	{
		frm.submit();
	}
}

//---------------------------------------------------------------------------------------------------------------------------------------
// CHOOSE THEME
function chooseTheme(){
	document.frm_theme.submit();
}

function stateProblem(alrM)
{
	alert(alrM);
}
function clear_searchForm(){
	document.header_searchForm.keywrd.value = '';
	return false;
}

//---------------------------------------------------------------------------------------------------------------------------------------
// VALIDATE SEARCH
function check_searchForm(){
	var f = document.searchForm;
	if(f.keycountry.options[f.keycountry.selectedIndex].value == 0){
		return false;
	}
	if(f.keycity.options[f.keycity.selectedIndex].value == 0){
		return false;
	}
}

	function openWindow(theURL,winName){
		var features="'resizable=yes,scrollbars=yes,width=600,height=680,left=350,top=140'";
		window.open(theURL,winName,features);
	}
	function chooseTheme(){
		document.frm_theme.submit();
	}
	
//---------------------------------------------------------------------------------------------------------------------------------------
// TOGGLE LAYER
function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

//---------------------------------------------------------------------------------------------------------------------------------------
/**************************************************************
this is the message box for the form validation on ta log in section
nilagay ni: Juven              date: Nov 11, 2008
**************************************************************/     

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}
    
// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = fullUrl+"images/message_validator1.gif"; 
}

/***********
end
*/
/*******************************************************
added by juven array_push function like in php array_push()
Jan 21, 2009
*/
    function array_push(array) {
        var i, argv = arguments, argc = argv.length;   
        for (i=1; i < argc; i++){
            array[array.length++] = argv[i];
        }
        return array.length;
    }
/***********
end
*/

/*******************************************************
added by juven in_array function like in php in_array()
Nov 14, 2008
*/
function in_array(val, arr_val){
    var a=false;
    for(var i=0;i<arr_val.length;i++){
      if(val == arr_val[i]){
          a=true;
          break;
      }
    }
    return a;
}
/*******************************************************
added by juven this function will check if the key press
input is only numeric

e.g
<input type="text" name="amount" onkeypress="return keypress_numonly(event);">

Date added: Nov 17, 2008
*/

function keypress_numonly(entity){
   var key_code = navigator.appName=='Netscape' ? entity.charCode : window.event.keyCode;
   if( (key_code >= 48 && key_code <= 57) || (key_code==0) || (key_code == 46)){
      return true ;
   }else{
      return false ;
   }
}
/*********************************************************************************
this script is for the ballon tooltip
date added: nov 19,2007                         added by: juven
*********************************************************************************/
var ToolTipEvents = {
    offsetLeft : (-12),
    offsetTop : (10),
    posRef : function(){
        return (
            (document.documentElement.scrollTop)?
                document.documentElement : document.body
            );
    },
    showTooltip : function(e){
        ToolTipEvents._cleanup();
        BubbleTips.bubbleNode.appendChild(this.tooltip);
        Effects.fadeIn(this.tooltip,BubbleTips.opacity);
        ToolTipEvents.followMouse(e);
    },
    hideTooltip : function(e){
        Effects.fadeOut(this.tooltip, ToolTipEvents._cleanup);
    },
    followMouse : function(e){
        if(e == null){ e = window.event };
        var posx = ToolTipEvents.offsetLeft;
        var posy = ToolTipEvents.offsetTop;
        if(e.pageX || e.pageY){
            posx += e.pageX;
            posy += e.pageY;
        } else if(e.clientX || e.clientY) {
            posx += e.clientX + ToolTipEvents.posRef().scrollLeft;
            posy += e.clientY + ToolTipEvents.posRef().scrollTop;
        }
        BubbleTips.bubbleNode.style.top  = (posy) + "px";
        BubbleTips.bubbleNode.style.left = (posx) + "px";
    },
    _cleanup : function(){
        var bubble = BubbleTips.bubbleNode;
        if( bubble.childNodes.length > 0 ){
            bubble.removeChild(bubble.firstChild);
        }
    }
};

var Effects = {
    fadeIn : function (elem,maxOpac){
        elem.fadeIn = Effects._fadeIn;
        elem.maxOpac = maxOpac;
        elem.curOpac = 0;
        this.cancelCurrent();
        elem.fadeIn();
    },
    fadeOut : function (elem,fadeDoneF){
        elem.fadeOut = Effects._fadeOut;
        elem.fadeOutDone = fadeDoneF;
        this.cancelCurrent();
        elem.fadeOut();
    },
    cancelCurrent : function() {
        clearTimeout(window.evtId);
    },
    _fadeIn : function() {
        if( (+this.curOpac) < (+this.maxOpac) ){
            this.curOpac = (+this.curOpac)+(0.05);
            Effects.setOpacity(this,this.curOpac);
            window.fadeInElem = this;
            window.evtId = setTimeout(function(){this.fadeInElem.fadeIn()},30);
        } else {
            Effects.setOpacity(this,this.maxOpac);
            window.fadeInElem = null;
        }
    },
    _fadeOut : function() {
        if( (+this.curOpac) > 0 ){
            this.curOpac = Math.max(0,(+this.curOpac)-(0.05));
            Effects.setOpacity(this,this.curOpac);
            window.fadeOutElem = this;
            window.evtId = setTimeout(function(){this.fadeOutElem.fadeOut()},30);
        } else if(this.fadeOutDone) {
            this.fadeOutDone();
            window.fadeOutElem = null;
        }
    },
    setOpacity : function (elem,opac){
        elem.style.filter="alpha(opacity:"+ ((+opac)*100) +")";
        elem.style.KHTMLOpacity=opac;
        elem.style.MozOpacity=opac;
        elem.style.opacity=opac;
    }
};

var BubbleTips = {
    opacity : "0.99",
    bubbleNode : null,
    activateTipOn : function(type){
        var bubble = document.createElement("span");
        bubble.style.position = "absolute";
        bubble.style.zIndex = "100";
        this.bubbleNode = bubble;
        document.getElementsByTagName("body")[0].appendChild(bubble);
        var tipTags = document.getElementsByTagName(type);
        for(var i=0;i<tipTags.length;i++){
            this.bindBubbleTip(tipTags[i]);
        }
    },
    bindBubbleTip : function(elem) {
        var tipText=elem.getAttribute("title");
        if(tipText==null || tipText.length==0){
            tipText="No title attribute, how sad :-(";
        }
        elem.removeAttribute("title");
        var bubble = this.createElem("span","bubbleTooltip");
        var tipTop = this.createElem("span","top");
        tipTop.appendChild(document.createTextNode(tipText));
        bubble.appendChild(tipTop);
        bubble.appendChild(this.createElem("span","bottom"));
        Effects.setOpacity(bubble,this.opacity);
        elem.tooltip = bubble;
        elem.onmouseover = ToolTipEvents.showTooltip;
        elem.onmouseout  = ToolTipEvents.hideTooltip;
        elem.onmousemove = ToolTipEvents.followMouse;
    },
    createElem : function(tag,className){
        var elem = document.createElement(tag);
        elem.className = className;
        elem.style.display = "block";
        return elem;
    }
};

if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = fullUrl+"images/bubble.png"; 
}
//---------------------------------------------------------------------------------------------------------------------------------------
// shadow for firefox
//---------------------------------------------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------------------------------------------
// CALENDAR Functions
//---------------------------------------------------------------------------------------------------------------------------------------
	function defaultDate(input) { 
		return {minDate: 0, maxDate: 1825}; 
	} 
	function customRange(input) { 
		return {minDate: (input.id == "season_to" ? $("#season_from").datepicker("getDate") : null), 
			maxDate: (input.id == "season_from" ? $("#season_to").datepicker("getDate") : null)}; 
	}
	function customRange_transfer(input) { 
		return {minDate: (input.id == "depart_date" ? $("#arrival_date").datepicker("getDate") : null), 
			maxDate: (input.id == "arrival_date" ? $("#depart_date").datepicker("getDate") : null)}; 
	} 

//---------------------------------------------------------------------------------------------------------------------------------------
// VALIDATE2 TOUR Functions
//---------------------------------------------------------------------------------------------------------------------------------------
	function validate_form22(frm) {
	var frm = document.form1;
		if(frm.orderdate.value=='') {
			inlineMsg('orderdate','Please provide us the date of the tour.',3);
			return false;
			
		}
		return true;
	}
	
	function validate_form2(){
		go = validate_form22(document.getElementById('form1'));
		if(go){
			document.getElementById('form1').submit();   
		}else{
			return false;
		}		
	}
	
//---------------------------------------------------------------------------------------------------------------------------------------
// URL REWRITE SEARCH - Tours
//---------------------------------------------------------------------------------------------------------------------------------------
	function rewriteSearchMain(main_url)
	{
		var frm = document.getElementById('search-input').value;
		location.href = main_url + "/search/" + frm;
	}
	
//---------------------------------------------------------------------------------------------------------------------------------------
// URL REWRITE SEARCH - Transfers
//---------------------------------------------------------------------------------------------------------------------------------------
	function rewriteSearchMain_transfer(main_url)
	{
		var frm = document.getElementById('search-input').value;
		location.href = main_url + "/search_transfers/" + frm;
	}

//---------------------------------------------------------------------------------------------------------------------------------------
// ADVANCE SEARCH
//---------------------------------------------------------------------------------------------------------------------------------------

function toggle_adv_search(){
    document.getElementById('adv_search').style.display = 'block';
    document.getElementById('season_from').value = '';
    document.getElementById('season_to').value = '';
    document.getElementById('keywrd').value = '';
    document.getElementById('find').style.display = 'none';
}
function close_adv_search(){
    document.getElementById('adv_search').style.display = 'none';
    document.getElementById('season_from').value = '';
    document.getElementById('season_to').value = '';
    document.getElementById('keywrd').value = '';
    document.getElementById('find').style.display = 'block';
}
function check_advSearchForm(main_url){
    var f = document.searchForm;
    var advSearchForm = document.advSearchForm;
    
    var keycountry_val = f.keycountry.options[f.keycountry.selectedIndex].value
    var keycity_val = f.keycity.options[f.keycity.selectedIndex].value
    var season_from    = advSearchForm.season_from.value
    var season_to      = advSearchForm.season_to.value
    var keywrd = advSearchForm.keywrd.value
    var keycity_lowercase
    
    if( keycountry_val == 0){
        alert('Please select a country');
        return false;
    }
    
    if( keycity_val== 0 || keycity_val=='City' || keycity_val=='Cities'){
        alert('Please select a city');
        return false;
    }

    /*
    
    if( season_from == ""){
        alert('Please select date from');
        advSearchForm.season_from.focus();
        return false;
    }
    
    if( season_to == ""){
        alert('Please select date to');
        advSearchForm.season_to.focus();
        return false;
    }
    
    if( keywrd == ""){
        alert('Please enter keyword');
        advSearchForm.keywrd.focus();
        return false;
    }
    */
    advSearchForm.keycountry.value = keycountry_val;
    advSearchForm.keycity.value = keycity_val;
    
    keycity_lowercase = keycity_val.toLowerCase();

/*
    if( keycity_lowercase == 'alice springs' || keycity_lowercase == 'mexico city' ){
        advSearchForm.action = main_url + "/" + keycity_lowercase + "/search/" + keywrd;
    }else if( keycity_lowercase == 'da nang' || keycity_lowercase == 'greece athens tours' || keycity_lowercase == 'ho chi minh' || keycity_lowercase == 'london tours' || keycity_lowercase == 'nha rang' ){
        advSearchForm.action = main_url + "/" + keycity_lowercase.replace(/ /gi, "_") + "/search/" + keywrd;
    }else{
        advSearchForm.action = main_url + "/" + keycity_lowercase.replace(/ /gi, "-") + "/search/" + keywrd;
    }
    
    advSearchForm.submit();
*/    
    //window.location = main_url + "/" + keycity_val.toLowerCase() + "/search/" + keywrd;
    if( keywrd == '' || keywrd == null ){
        window.location = main_url + "/" + keycity_val.toLowerCase();
    }else{
        window.location = main_url + "/searchAdvPlaceHolder.php?keycountry=" + keycountry_val + "&keycity=" + keycity_val + "&keywrd=" + keywrd + "&search";
    }
    //window.location = main_url + "/searchPlaceHolder.php?keycountry=" + keycountry_val + "&keycity=" + keycity_val;
    
}
function submitFormWithEnter(myfield,e,main_url){  
    var keycode;  
    if (window.event){  
        keycode = window.event.keyCode;  
    }  
    else if (e)  {  
        keycode = e.which;  
    }else{  
        return true;  
    }  
    if(keycode == 13){  
        check_advSearchForm(main_url);
        return false;  
    }  
}    