
function objToString(o){s='';for(aProp in o){s += aProp + "\t";}return s;}
var cal_only=0;
var is_nav=0;
var Events = Array();

function clearTravelContent(){
	setTimeout("try{clearTravelContent2();}catch(e){}",3000);
	return true;
}

function clearTravelContent2(){
	var c = document.getElementById("s_c");
	var w = document.getElementById("s_w");
	var y = document.getElementById("s_c2");
	c.style.display='none';
	w.style.display='';
	y.style.display='none';
}

function initTrav(){

	if(typeof(M[3]) != 'undefined' && typeof(DEST) != 'undefined'){
		for(x in M[3]){
			var selects = document.getElementById("mMCPID"+x).getElementsByTagName("select");
			for(y in selects){
				if(selects[y].id == 'leavingFrom'){
					var val = selects[y].value;
					if(val != AIR_DEPT || 1==1){ // proximity dependent
						selects[y].value = AIR_DEPT;
						populateDest2(selects[y],'packages',1,15,1);
					}
				}
			}
		}
	}

	sAgent=navigator.userAgent.toLowerCase();

	if(default_dest==''){
		populateDest(pcfl,1);
	}

	if(default_dest!='' && mode=='pc'){
		populateDur(pcfl);
	}

}

function unloadTravel(){
	try{
		wait = document.getElementById('s_c');
		wait.style.display = 'none';
	}catch(e){};
	return true;
}

function set_day(day){
	if(cal_only==1){
		mt = document.getElementById('month');
		yr = document.getElementById('year');
		deptCond = window.opener.document.getElementById('deptDateCondensed');
		deptCond.value = yr.value + '/' + mt.value + '/' + day;
		window.close();
	}else if(is_nav!=1){
		clearTravelContent();
	}else{ // is_nav must equal one
		document.getElementById('mini_cal').submit();
	}
	return true;
}

function set_year(){populateCalEvents(0,1);}
function get_mycalFilter(){try{var MapKey = document.getElementById('mycal_filter').value;}catch(e){var MapKey = 'Events';}return MapKey;}
function populateCalEvents(dir,update_only){
	var y=document.getElementById('year');var m=document.getElementById('month');var mth=m.options[m.selectedIndex].value;var yr=y.options[y.selectedIndex].value;var MapKey = get_mycalFilter();
	if(!update_only){if(dir){mth++;if(mth>12){mth=1;yr++;}}else{mth--;if(mth==0){mth=12;yr--;}}}
	pm=mth;if(mth<10){mth="0"+mth;}ind = yr + "" + mth;p=-1;
	if(is_nav==1){for(x=0;x< Events[MapKey].length;x++){if(Events[MapKey][x][0]==ind){p=x;break;}}eveArr=(p!=-1)? Events[MapKey][p][1]: [];write_month(yr,mth,0,eveArr);}
	for(x=0;x < m.options.length;x++){if(m.options[x].value == pm){m.selectedIndex=x;}}
	for(x=0;x < y.options.length;x++){if(y.options[x].value == yr){y.selectedIndex=x;}}
	if(is_nav!=1){set_month(1);}
}

function populateDest2(obj,pcfl,id,min,supp){

	var val = obj.value;
	var mod = document.getElementById("mod3-"+id);
	var str = '';
	var price = 0;
	var href = '';
	var min = (!min)? 15: min;
	var supp = (!min)? 0: min;
	var link = '';
	if(typeof(DEST[val]) != 'undefined'){
		var l = min;
		str += '<h3>Departing: '+DEPT[val]+'</h3>';
		for(x in DEST[val]){
			if(typeof(DEST[val][x]) != 'object'){continue;}
			price = DEST[val][x][0];
			href = DEST[val][x][1];
			link = (href != null)? '<a href="'+href+'">'+DEPT[x]+'</a>': DEPT[x];
			str += '<div><span>&#36;'+price+'</span>'+link+'</div>';
			//alert(DEST[val][x]);
			l--;
		}
	}
	if(l > 0 && supp){
		// alert("SEARCH FOR SUPPLEMENTARY METHOD="+supp+" l="+l);
		// I need an array with pointers to distances...
		for(x in SUPP[0][val]){
			if(typeof(SUPP[0][val][x]) != 'object'){continue;}
			// alert(x);
			str += '<h3>Departing: '+DEPT[x]+'</h3>';
			for(y in DEST[x]){
				if(typeof(DEST[x][y]) != 'object'){continue;}
				price = DEST[x][y][0];
				href = DEST[x][y][1];
				link = (href != null)? '<a href="'+href+'">'+DEPT[y]+'</a>': DEPT[y];
				str += '<div><span>&#36;'+price+'</span>'+link+'</div>';
				l--;
			}
			if(l <= 0){
				break;
			}
		}
	}

	mod.innerHTML = str;
}

function populateDest(pcfl,is_onload){
//	if(is_onload){return;}
	if(default_dest!=''){populateDur(pcfl);return;}
	if(document.getElementById){
		var is_header = false;
		lF=document.getElementById('leavingFrom');
		gT=document.getElementById('goingTo');
		gI = gT.selectedIndex;
		fC=(default_dept=='')?[lF.options[lF.selectedIndex].value]:default_dept.split('|');
		fDest=(default_dest=='')?[gT.options[gT.selectedIndex].value]:default_dest.split('|');
		fDestName=gT.options[gT.selectedIndex].text;
		oGT=gT.options[gT.selectedIndex].value;
		//alert('Original GoingTo'+oGT);
		gT.options.length=0;
		ns=0;
		gT.options[0]=new Option('Please Select...','');
		if(fC==0){return;}
		optionCache=new Array();
		var ax=0;
		var lx = fC.length;
		var piv;
		var sm = SortDest[pcfl][sortMethod];
		for(w=0;w < lx;w++){
			piv = Dept[pcfl][fC[w]];
			if(piv){
				var lxc = sm.length;
				for(z=0;z < lxc;z++){
					if(fDest == sm[z]){
						optionCache[fDest]=fDestName;
						continue;
					}
					var lxb = piv.length;
					for(x=1;x <= lxb;x++){
						if(piv[x-1][0] == sm[z]){
							if(!piv[x-1][1].length){
								continue;
							}						
							optionCache[[piv[x-1][0]]]=piv[x-1][1];
						}
					}
				}
			}
		}

		y=1;
		lI = 'NA';
		pad = (sortMethod > 0)? ' - ': '';
		var lx = sm.length;
		var piv2,piv3;
		for(x=0;x <= lx;x++){
			for(z in optionCache){
				if(typeof(optionCache[z]) != 'function' && z == sm[x]){
					if(sortMethod > 0){
						piv3 = Dind[z][sortMethod];						
						if(piv3 != lI){
							piv2 = Cj[sortMethod][piv3];
							gT.options[y]=new Option(Ci[sortMethod][piv3],'*|'+piv2);
							lI = piv3;
							y++;
							if(piv2.length == 3){
								continue;
							}
						}
					}
					gT.options[y]=new Option(pad +optionCache[z],z);
					if(z==oGT){
						ns=y;
					}							
					y++;
				}
			}
		}
		if(ns){
			gT.selectedIndex=ns;
		}
	}
	try{
		set_month();
	}catch(e){
				
	}							
}

function populateDur(pcfl){
	populateCalendar();
	if((pcfl!='pc' || incl=='on') && expfldur==0){return;}

	lF=document.getElementById('leavingFrom');
	fC=(default_dept=='')?[lF.options[lF.selectedIndex].value]:default_dept.split('|');
	if(fC==0){return;}
	if(default_dest!=''){
		oGT = default_dest;
	}else{	
		gT=document.getElementById('goingTo');
		oGT=gT.options[gT.selectedIndex].value;
	}
	if(oGT.substr(0,2) == '*|'){
		oGT = oGT.substr(2);
	}
	oGT=oGT.split('|');

	if(pcfl=='pc'){
		dU=document.getElementById('duration');
		odU=dU.options[dU.selectedIndex].value;
		dU.options.length = 0;
		dU.options[0]=new Option('Any Length...','all');
		P=['All Inclusive','Semi Inclusive','Breakfast','No Meals','2 Meals Per Day','3 Meals Per Day'];
		P2=['p3','p22','p11','p1','p2','p21'];
		pC=document.getElementById('package_code');
		opC=pC.options[pC.selectedIndex].value;
		pC.options.length = 0;
		pC.options[0]=new Option('All Packages...','any');
	}else{
		dU=document.getElementById('duration');
		odU=dU.options[dU.selectedIndex].value;
		dU.options.length = 0;
		dU.options[0]=new Option('Round Trip and One Way','return');
		dU.options[1]=new Option('One Way Only','oneway');
	}
	ns=0;nt=0;
	dUOptionsUsed = [];
	pCOptionsUsed = [];
	var piv;
	for(q in oGT){
		for(z=0;z < fC.length;z++){
			for(x=1;x <= Dept[pcfl][fC[z]].length;x++){
				piv = Dept[pcfl][fC[z]][x-1];
				if(piv[0]==oGT[q]){
					for(y=0;y < piv[2].length;y++){
						if(piv[2][y] in dUOptionsUsed){continue;}
						dU.options[dU.options.length]=new Option(piv[2][y]+' Days',piv[2][y]);
						dUOptionsUsed[piv[2][y]]=1;
						if(piv[2][y]==odU){
							ns=y+1;
						}
					}
					if(pcfl=='pc'){
						var num;
						var pl = piv[3].length;
						for(y=0;y < pl;y++){
							num = piv[3][y];
							if(P2[num] in pCOptionsUsed || typeof(num) == 'undefined'){continue;}
							pC.options[pC.options.length]=new Option(P[num],P2[num]);
							pCOptionsUsed[P2[num]]=1;
							if(P2[num]==opC){
								nt=pC.options.length;
							}
						}
					}
				}
			}
		}
	}
	if(ns){dU.selectedIndex=ns;}
	if(nt && pcfl == 'pc'){pC.selectedIndex=nt;}
}


function populateCalendar(){try{set_month();}catch(e){}}

function set_month(update_only){
	if(cal_only==1 || is_nav==1){m=document.getElementById('mini_cal');m.submit();return true;}
	var m=document.getElementById('month');
	var year=document.getElementById('year');
	var mth = m.value;
	if(mth.length==1){mth="0"+mth;}
	var yr=year.value;
	var highlight = [];
	var dept=(default_dept=='')?[document.getElementById('leavingFrom').value]:default_dept.split('|');
	var dest=document.getElementById('goingTo');
	DEST=(dest==null)? default_dest.split("|") : dest.value.split("|");
	s_i= yr+""+mth;
	var f = -1;
	var cL=0;
	var cR=0;
	var piv,pl,pm;
	for(d in DEST){
		for(w=0;w < dept.length;w++){
			piv = Dept[pcfl][dept[w]];
			pl = piv.length;
			for(x=0;x < pl;x++){
				if(piv[x][0] == DEST[d]){
					pm = piv[x][4][0].length;
					for(y=0;y < pm;y++){
						if(piv[x][4][0][y] == s_i){
							for(z in piv[x][4][1][y]){						
								highlight.push(piv[x][4][1][y][z]);
							}
							break;
						}
					}
					if(cL && cL > piv[x][4][2][0] || !cL){cL=piv[x][4][2][0];}
					if(cR && cR < piv[x][4][2][1] || !cR){cR=piv[x][4][2][1];}
					break;
				}
			}
		}
	}

	aR=document.getElementById('nav-month-right');
	aL=document.getElementById('nav-month-left');
	if(dest==null){
		defflag = 1;
	}else{
		defflag=dest.value;
	}

//	alert("cL("+cL+") s_i("+s_i+")");

	aL.style.display=(cL >= s_i || typeof(cL) == 'undefined' || !cL || defflag == 0)?'none':'inline';
	aR.style.display=(cR <= s_i || typeof(cR) == 'undefined' || !cR || defflag == 0)?'none':'inline';
	m.value = parseFloat(mth);
	year.value = yr;

	write_month(yr,mth,'POST',highlight);
	return true;
}


Array.prototype.find = function (s){for(var i=0;i<this.length;i++)if(this[i] == s) return true; return false;}
function isLeap(year){
 if ((year%400==0)||((year%4==0)&&(year%100!=0))) {return true;}
 else {return false;}
}


function write_month(yr,mth,mode,highlight){
	var myDate = new Date();
	myDate.setFullYear(yr,mth-1,1);
	var theFirst = myDate.getDay();
	ML=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	dayMax = ((mth-1)==1 && isLeap(yr))? ML[mth-1]+1: ML[mth-1];
	//alert(dayMax);
	// 7 == Saturday
	// wipe the grid clean
	day=0;
	for(z=1;z<43;z++){
		c=document.getElementById('cal-'+z);
		c.className = "empty_day_cell";
		c.innerHTML = '';
		if(z > theFirst && day < dayMax){
			day++;
			c.className = "day_cell";
			c.innerHTML = day
			if(highlight.find(day)){
				c.className = "travel_cell";
				c.innerHTML = "<input class='day_cell_input' type='submit' name='day' onclick='set_day("+day+");' value='"+day+"' />";
			}
		}	
	}
}









