/* CROSS-BROWSER EVENT HANDLER */
		function addEvent(obj, evType, fn){
			 if (obj.addEventListener){
				 obj.addEventListener(evType, fn, true);
				 return true;
			 } else if (obj.attachEvent){
				 var r = obj.attachEvent("on"+evType, fn);
				return r;
			 } else {
				return false;
			 }
		}
	/* END EVENT HANDLER */
	

	/* PAGELOCATOR */
		function PageLocator(propertyToUse, dividingCharacter) 
		{
			this.propertyToUse = propertyToUse;
			this.defaultQS = '';
			this.dividingCharacter = dividingCharacter;
		}
		PageLocator.prototype.getLocation = function() 
		{
			var ret=eval(this.propertyToUse);
			return ret;
		}
		PageLocator.prototype.getHash = function() 
		{
		
			var url = this.getLocation();
			if(url.indexOf(this.dividingCharacter)>-1) 
			{
				var url_elements = url.split(this.dividingCharacter);
				//alert(url_elements)
				return url_elements[url_elements.length-1];
			} 
			else 
			{
				return this.defaultQS;
			}
		}
		PageLocator.prototype.getHref = function() 
		{
			
			var url = this.getLocation();
			
			var url_elements = url.split(this.dividingCharacter)
			return url_elements[0];
		}
		PageLocator.prototype.makeNewLocation = function(new_qs) 
		{
			return this.getHref() + this.dividingCharacter + new_qs;
		}
	/* END PAGELOCATOR */
	
	
	
	
	
	
	/* AjaxIframesFixer */
		function AjaxIframesFixer(iframeid) 
		{
			this.iframeid = iframeid;
			if (document.getElementById('ajaxnav')) {
				this.fixLinks();
				
				this.locator = new PageLocator("document.frames['"+this.iframeid+"'].getLocation()", "?hash=");
				this.windowlocator = new PageLocator("window.location.href", "#");
				this.timer = new Timer(this);
				
				this.delayInit(); // required or IE doesn't fire
			}
		}
		AjaxIframesFixer.prototype.fixLinks = function (iframeid) 
		{
			var links = document.getElementsByTagName("A");
			for(var i=0; i<links.length; i++) {
				var href = links[i].getAttribute("href");
				if(href.indexOf("#")>-1)
				{
					var hash = href.substr(href.indexOf("#")+1);
				links[i].setAttribute("href","javascript:document.getElementById('"+this.iframeid+"').setAttribute('src','"+ ROOT_PATH+"mock-page.php?hash="+hash+"');");
				}
			}
		}
		AjaxIframesFixer.prototype.delayInit = function()
		{
			this.timer.setTimeout("checkBookmark", 100, "");
		}
		AjaxIframesFixer.prototype.checkBookmark = function()
		{
			window.location = this.windowlocator.makeNewLocation(this.locator.getHash());
			this.checkWhetherChanged(0);
		}
		AjaxIframesFixer.prototype.checkWhetherChanged = function(location)
		{	
			
			if(this.locator.getHash() != location) {
				doGetPage(this.locator.getHash());
				window.location = this.windowlocator.makeNewLocation(this.locator.getHash());
			}
			this.timer.setTimeout("checkWhetherChanged", 2000, this.locator.getHash());
		}
	/* END AjaxIframesFixer */
	

	
	/* AjaxUrlFixer */
		function AjaxUrlFixer() {
			this.fixLinks();
			
			this.locator = new PageLocator("window.location.href", "#");
			this.timer = new Timer(this);
			this.checkWhetherChanged(0);
		}
		AjaxUrlFixer.prototype.fixLinks = function () {
			var links = document.getElementsByTagName("A");
			for(var i=0; i<links.length; i++) {
				var href = links[i].getAttribute("href");
//				var hash = href.substr(href.indexOf("hash=")+5);
				//links[i].setAttribute("href","#"+hash);
			}
		}
		AjaxUrlFixer.prototype.checkWhetherChanged = function(location){
		//alert(this.locator.getHash() +'---'+ location)
			if(this.locator.getHash() != location) {
				doGetPage(this.locator.getHash());
			}
			this.timer.setTimeout("checkWhetherChanged", 200, this.locator.getHash());
		}
	/* END AjaxUrlFixer */
	
	function setContent(new_content) {
		if(!document.getElementById || !document.getElementsByTagName) return;
		var container = document.getElementById("container");
		container.innerHTML = new_content;
	}
	function FixBackAndBookmarking() {
		if(!document.getElementById || !document.getElementsByTagName) return;
		if(document.iframesfix){
		fix = new AjaxIframesFixer('ajaxnav');
		} else {
			fix = new AjaxUrlFixer();
		}
	}
	
	var detect = navigator.userAgent.toLowerCase();
	if(detect.indexOf("msie")>-1) {
	document.iframesfix = true;
	}

	addEvent(window, "load", FixBackAndBookmarking);

	
	function setTitle(title){
		var index=getLocation();
		all_history[index]['title'] = title;
		window.document.title=unescape($("<a>"+title+"</a>").text());
	}
	function storeInHistory(){
		historyVariable.unshift(getLocation());
	}

	
	function callHistory(index){
		//index++;
		//alert("call History - > INDEX = "+index);
		//alert(historyVariable[index]['index']);
		//var temp=historyVariable[index];
		//historyVariable.splice(index, 1);
		//alert(historyVariable[index]);

		var a  = all_history[historyVariable[index]]['arguments'];
		var b = [];
		for(var i =0; i<a.length; i++) {
			//alert(a[i]);
			b.push('a['+i+']');
		}
		b.join(',');
		eval('setLocation('+b+')');
	}	
	
	function show_faqs(faq_no){
		for(var i=1; i<=7; i++){
			if(i == faq_no)
				$id('faq_'+i).style.display = '';
			else
				$id('faq_'+i).style.display = 'none';
		}
	}	
	
	function loginPlease(arg){
		//$id('response').innerHTML="<span class='error'>Please Login To Use This Feature</span>";
		if(!arg)
		{
			arg="user.select_itinerary&newUser";
		}
		$id('btn_letmein').onclick=function(){  
										hideDisableDiv('temp_login_div');
										window.location=ROOT_PATH+'index.php?p='+arg;
									}
		showDisableDiv('temp_login_div');
	}
	
	function autosuggestCity(resp){
		// var _actb = new actb('city','', 'aj_request.php?aj=user.aj_autosugest&value=' );  
		var _actb = new actb('city','', ROOT_PATH+'aj_request.php?aj=user.aj_autosugest&only_places=1&value=' );
	}
	
	function showHelp(arg){
		showDisableDiv('help_div');
		setLocation('help_'+arg);
	}
	function learnMore(){
		$.get("aj_request.php?aj=view.home",'',function(resp){$('#sub_div').html(resp)
					//$id('sub_div').style.display = 'none'
					$id('new_to_tmh').innerHTML = ' Back to <a href="javascript:void(0);" onclick="learnMoreBack();">Holiday Suggestions Generator</a>';
					
						});
	}
	function learnMoreBack(){
		$.get("aj_request.php?aj=view.home1",'',function(resp){$('#sub_div').html(resp)});
		//$id('sub_div').style.display = 'none'
		$id('new_to_tmh').innerHTML = 'New to TailorMyHoliday.com? <a href="javascript:void(0);" onclick="learnMore();">Click here to learn more</a>';
		
		
	}
	
	//*****************************search itinerary Functions*******************************************
	
	var months = ['January','February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	var seasons = ['Spring' , 'Summer', 'Monsoon', 'Autumn', 'Winter'];
	var types = ['Luxury' , 'Cultural' , 'Beach/Island' , 'Scenic' , 'Honeymoon' , 'Far Out'];
	var durations = ['3 DAY' , '5 DAY' , '1 WEEK' , '2 WEEK' , '1 MONTH', 'Any Duration'];
	var costs = ['< 200' , '< 500' , '< 1000' , '< 2000' , '< 3000', 'Any Price'];
	
	var selected_month = [false,false,false,false,false,false,false,false,false,false,false,false];
	var selected_seasons=[false, false, false, false, false];
	var selected_types = [false, false, false, false, false];
	var selected_duration = [false, false, false, false, false,false];
	var selected_cost = [false, false, false, false, false,false];
	var selected_anytime = [false];
	
	var search_restults_places = [];
	
	function select_anytime(arg){
		if(selected_anytime[arg]){
			selected_anytime[arg] = false;
			$id('td_anytime').className='tab';
			for(i=0; i<=11; i++){
				selected_month[i]=false;
				$id('td_month_'+i).className='tab';
				$id('td_month_val_'+i).value = '';
			}
			for(i=0; i<=4; i++){
				selected_seasons[i]=false;
				$id('td_season_'+i).className='tab';
				$id('td_season_val_'+i).value = '';
			}
		}
		else{
			selected_anytime[arg] = true;
			$id('td_anytime').className='selected_tab';
			for(i=0; i<=11; i++){
				selected_month[i]=true;
				$id('td_month_'+i).className='selected_tab';
				$id('td_month_val_'+i).value = months[i];
			}
			for(i=0; i<=4; i++){
				selected_seasons[i]=true;
				$id('td_season_'+i).className='selected_tab';
				$id('td_season_val_'+i).value = seasons[i];
			}
		}
	}
	
	function mouseOn_anytime(arg){
		if(!selected_anytime[arg]){
			$id('td_anytime').className='selected_tab';
		}
	}
	
	function mouseOut_anytime(arg){
		if(!selected_anytime[arg]){
			$id('td_anytime').className='tab';
		}
	}
	
//////////////////////////////////////// Months ////////////////////////////////////	
	
	function select_month(arg){
		if(selected_month[arg]){
			selected_month[arg]=false;
			$id('td_month_'+arg).className='tab';
			$id('td_month_val_'+arg).value='';
		}else{
			selected_month[arg]=true;
			$id('td_month_'+arg).className='selected_tab';
			$id('td_month_val_'+arg).value=months[arg];
		}
	}
	function mousOn_month(arg){
		if(!selected_month[arg]){
			$id('td_month_'+arg).className='selected_tab';
		}
	}

	function mousOut_month(arg){
		if(!selected_month[arg]){
			$id('td_month_'+arg).className='tab';
		}
	}

	
//////////////////////////////////////// Type of journey ////////////////////////////////////	
	function select_type(arg){
		if(selected_types[arg]){
			selected_types[arg]=false;
			$id('td_type_'+arg).className='tab';
			$id('td_type_val_'+arg).value='';
		}else{
			selected_types[arg]=true;
			$id('td_type_'+arg).className='selected_tab';
			$id('td_type_val_'+arg).value=types[arg];
		}
	}
	function mousOn_type(arg){
		if(!selected_types[arg]){
			$id('td_type_'+arg).className='selected_tab';
		}
	}

	function mousOut_type(arg){
		if(!selected_types[arg]){
			$id('td_type_'+arg).className='tab';
		}
	}
	
//////////////////////////////////////// Duration ////////////////////////////////////	
	
	function select_duration(arg){
		if(selected_duration[arg]){
			for(i=0; i<=arg; i++)
			{
				selected_duration[i]=false;
				$id('td_duration_'+i).className='tab';
				$id('td_duration_val_'+i).value='';
				if(i == 4)
				{
					$id('td_duration_'+5).className='tab';
					selected_duration[5]=false;
				}
			}
		}else{
			for(i=0; i<=arg; i++)
			{
				selected_duration[i]=true;
				$id('td_duration_'+i).className='selected_tab';
				$id('td_duration_val_'+i).value=durations[i];
			}
		}
	}
	function mousOn_duration(arg){
		if(!selected_duration[arg]){
			$id('td_duration_'+arg).className='selected_tab';
		}
	}

	function mousOut_duration(arg){
		if(!selected_duration[arg]){
			$id('td_duration_'+arg).className='tab';
		}
	}

//////////////////////////////////////// Price range ////////////////////////////////////	

	function select_cost(arg){
		if(selected_cost[arg]){
			for(i=0; i<=arg; i++)
			{
				selected_cost[i]=false;
				$id('td_cost_'+i).className='tab';
				$id('td_cost_val_'+i).value='';
				if(i == 4)
				{
					selected_cost[5]=false;
					$id('td_cost_'+5).className='tab';
				}
			}
		}else{
			for(i=0; i<=arg; i++)
			{
				selected_cost[i]=true;
				$id('td_cost_'+i).className='selected_tab';
				$id('td_cost_val_'+i).value=costs[i];
			}
		}
	}
	function mousOn_cost(arg){
		if(!selected_cost[arg]){
			$id('td_cost_'+arg).className='selected_tab';
		}
	}

	function mousOut_cost(arg){
		if(!selected_cost[arg]){
			$id('td_cost_'+arg).className='tab';
		}
	}

//////////////////////////////////////// Seasons ////////////////////////////////////	

	function selectSeason(arg){
		if(selected_seasons[arg]){
			selected_seasons[arg]=false;
			$id('td_season_'+arg).className='tab';
			$id('td_season_val_'+arg).value='';
		}else{
			selected_seasons[arg]=true;
			$id('td_season_'+arg).className='selected_tab';
			$id('td_season_val_'+arg).value=seasons[arg];
		}
		
	}
	function mousOnSsn(arg) {
		if(!selected_seasons[arg]) {
			$id('td_season_'+arg).className='selected_tab';
		}
	}

	function mousOutSsn(arg){
		if(!selected_seasons[arg])  {
			$id('td_season_'+arg).className='tab';
		}
	}
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	function searchItinerary() {
		var month_checks = document.getElementsByName('month[]');
		var season_checks = document.getElementsByName('season[]');
		var type_checks = document.getElementsByName('type[]');
		var duration_checks = document.getElementsByName('duration[]');
		var cost_checks = document.getElementsByName('cost[]');
		
		var month = new Array();
		var j=0;
		for(var i=0; i<months.length; i++) {
			if(month_checks[i].value) {
				month[j++]=month_checks[i].value;
				selected_month[i] = false;
			}
		}
		
		var season = new Array();
		var j=0;
		for(var i=0; i<seasons.length; i++)	{
			if(season_checks[i].value) {
				season[j++]=season_checks[i].value;
				selected_seasons[i] = false;
			}
		}
		
		var type = new Array();
		var j=0;
		for(var i=0; i<types.length; i++) {
			if(type_checks[i].value) {
				type[j++]=type_checks[i].value;
				selected_types[i] = false;
			}
		}
		
		var duration = new Array();
		var j=0;
		for(var i=0; i<durations.length; i++) {
			if(duration_checks[i].value) {
				duration[j++]=duration_checks[i].value;
				selected_duration[i] = false;
			}
		}
		
		var cost = new Array();
		var j=0;
		for(var i=0; i<costs.length; i++) {
			if(cost_checks[i].value) {
				cost[j++]=cost_checks[i].value;
				selected_cost[i] = false;
			}
		}
		
		if(month.length=='0' && season.length=='0' && type.length=='0' && duration.length=='0' && cost.length=='0')	{
			alert('Please select the Type of Holiday and/or Time of the Year!');
			return;
		}
		var data = {
			'month':month.join(','),
			'season':season.join(','),
			'type':type.join(','),
			'duration':duration.join(','),
			'cost':cost.join(','),
			'place':search_restults_places.join(',')
		};
		search_restults_places = [];
		setLocation('showPage','itinerary.found',data,'Search Results');
		
	}
	
	
	function showSuggestedItinerary(itineraryId){
		$.post(ROOT_PATH+"aj_request.php?aj=journeyplanner.select_jp_elements","itineraryId="+itineraryId+"&match_itinerary=1&reopen=1",  function (resp){
					var jp=drawJP(resp);
					var html='';
					html+="<table>";
					html+="<tr><td align='right'><a href=\"javascript:void(0)\" style=\"color: rgb(115, 115, 115); font-size: 12px;\" onclick=\"importSuggestedToJp('"+itineraryId+"') \"><b>Yes! Import this itinerary to the Holiday Planner</b></a><br></td></tr>";
					
					html+="<tr><td>"+jp+"</td></tr>";
					html+="</table>"
					
					$id('jp_td').innerHTML=html;
					showDisableDiv("jp_div");																															
				} ); 
	}
	
	function importSuggestedToJp(itineraryId){
		hideDisableDiv("jp_div");							
		var url= ROOT_PATH+"aj_request.php?aj=user.check_session"
		var resp = $.ajax({type: "GET", url: ROOT_PATH+"aj_request.php?aj=user.check_user_log", async: false}).responseText;
		
		if(resp=='0')
		{
			loginPlease("itinerary.dump_suggested&newUser&itineraryId="+itineraryId);
		}else
		{
			window.location=ROOT_PATH+'index.php?p=itinerary.dump_suggested&itineraryId='+itineraryId;	
		}
	}
	
// pagination in found.php 
function gtFound(url) {
	var index = window.location.href.substr(window.location.href.indexOf('#')+1);
	var args = all_history[index]['arguments'];
	args[1]=url;
	var str=[];
	for(var i =0; i<args.length; i++) {
		//alert(args[i]);
		str.push('args['+i+']');
	}
	str=str.join(',');
	str = 'setLocation('+str+')';
	eval(str);
}


