// JavaScript Document
function MM_popupMsg(msg) { //v1.0
  alert(msg);
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) 
{ //v4.01
  var p,i,x;  
  
  if(!d) 
  	d = document; 
  if((p=n.indexOf("?"))>0&&parent.frames.length) 
  {
    d = parent.frames[n.substring(p+1)].document; 
	n = n.substring(0,p)
  }
  if(!(x=d[n])&&d.all) 
    x = d.all[n]; 
  for (i=0;!x&&i<d.forms.length;i++) 
    x = d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
    x = MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) 
    x = d.getElementById(n); 
  return x;
}
PositionX = 10;
PositionY = 10;
defaultWidth  = 600;
defaultHeight = 400;
var AutoClose = true;
function popImage(imageURL,imageTitle){
  var imgWin = window.open('','_blank','scrollbars=no,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
  if( !imgWin ) { return true; } //popup blockers should not cause errors
  imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
    'function resizeWinTo() {\n'+
    'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
    'var oH = document.images[0].height, oW = document.images[0].width;\n'+
    'if( !oH || window.doneAlready ) { return; }\n'+ //in case images are disabled
    'window.doneAlready = true;\n'+ //for Safari and Opera
    'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
    'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
    'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
    'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
    'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
    'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
    'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\n'+
    'var scW = screen.availWidth ? screen.availWidth : screen.width;\n'+
    'var scH = screen.availHeight ? screen.availHeight : screen.height;\n'+
    'if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
    '}\n'+
    '<\/script>'+
    '<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();"':'')+'>'+
    (document.layers?('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;display:table;">'))+
    '<img src='+imageURL+' alt="Loading image ..." title="" onload="resizeWinTo();">'+
    (document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
  imgWin.document.close();
  if( imgWin.focus ) { imgWin.focus(); }
  return false;
}

function MM_showHideLayers() 
{ //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  
  for (i=0; i<(args.length-2); i+=3) 
  	if ((obj=MM_findObj(args[i]))!=null) 
	{ 
		v=args[i+2];
    	if (obj.style) 
		{ 
			obj=obj.style; 
			v=(v=='show')?'visible':(v=='hide')?'hidden':v; 
		}
    	obj.visibility=v; 
	}
  return true;
}

function MM_DisplayShowHideLayers() 
{ //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  	if ((obj=MM_findObj(args[i]))!=null) 
  	{ 
  		v=args[i+2];
	    if (obj.style) 
		{ 
			obj=obj.style; 
			v=(v=='show')?'inline':(v=='hide')?'none':v; 
		}
    	obj.display=v; 
	}
}

function MM_swapImgRestore() 
{ //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage()
{ //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function y2k(number) 
{ 
	return (number < 1000) ? number + 1900 : number; 
}

function daysElapsed(date1,date2) 
{
    var difference =
        Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0)
      - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
    return difference/1000/60/60/24;
}

function runit(x) 
{
	if (x) {
		checkandchange(document.getElementById('CID_Day').value,document.getElementById('CID_Month').value,document.getElementById('CID_Day_End').value,document.getElementById('CID_Month_End').value,document.getElementById('CID_Nights').value,x);
	} else {
		checkandchangeFlights(document.getElementById('CID_Day').value,document.getElementById('CID_Month').value,document.getElementById('Return_Day').value,document.getElementById('Return_Month').value,document.getElementById('DtDiff').value,document.getElementById('Roundtrip').checked);
	}
}
function checkandchange(w,x,y,z,a,action) { //Hotel Search start date - end date script
	w= Math.round(w); //make it a number
	y= Math.round(y);
	a= Math.round(a);
	
	var x1 = x.substr(0,x.length-2);
	var x2 = x.substr(x.length-2,2);
	var z1 = z.substr(0,z.length-2);
	var z2 = z.substr(z.length-2,2);
	
	x2 = '20'+x2;
	z2 = '20'+z2;
	
	x2 = Math.round(x2);
	z2 = Math.round(z2);
	

	x1= Math.round(x1);
	z1= Math.round(z1);

	error=false;

	if (action==1) { //from change
		newdate = w+a;
		birthday01 = new Date(x2,x1-1,newdate,'12','00','00');
		n_x = birthday01.getDate();
		n_x1 = birthday01.getMonth()+1; // month array 0-11
		n_x2a = birthday01.getFullYear()+'';
		n_x2 = n_x2a.substr(2,2);
		
		if ((x1==4 || x1==6 || x1==9 || x1==11) && w==31) {
			alert("This Month doesn`t have 31 days!");
			error = true;
		}
		if (x1 == 2) { // check for february 29th
			var isleap = (x2 % 4 == 0 && (x2 % 100 != 0 || x2 % 400 == 0));
			if (w > 29 || (w==29 && !isleap)) {
				alert("This February doesn`t have " + w + " days!");
				error = true;
			}
		}
		if (error==false) { //change the end date fields
			document.getElementById('D_E_'+n_x).selected = true;
			document.getElementById('MY_E_'+n_x1+''+n_x2).selected = true;
		} else { //get end date minus duration and set that to start date
			newdate = y-a;
			birthday01 = new Date(x2,x1-1,newdate,'12','00','00');
			n_x = birthday01.getDate();
			n_x1 = birthday01.getMonth()+1;
			n_x2 = birthday01.getFullYear()+'';
			n_x2 = n_x2.substr(2,2);
			document.getElementById('D_S_'+n_x).selected = true;
			document.getElementById('MY_S_'+n_x1+''+n_x2).selected = true;
		}
	} else if (action==2) { //to change
		//z1 = z1-1;
		//x1 = x1-1;
		if (x1==0) {
			x1=12;	
		}
		if (z1==0) {
			z1=12;	
		}
//w,x,y,z,a,action
//alert(w+'-'+x1+'-'+x2+'-'+y+'-'+z1+'-'+z2);
        //set a aray of the months alphabetic
		var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
		//the check in date
		var check_in_day = w; //day
		var check_in_month = x1-1; //month 0-11
		var check_in_year = x2; // year
		var check_in_date = (check_in_day +" "+ m_names[check_in_month]+" "+ check_in_year); //the check in date in form dd mm yy
        //the check out date        
		var check_out_day = y;
		var check_out_month = z1-1;
		var check_out_year = z2;
		var check_out_date = (check_out_day +" "+ m_names[check_out_month]+" "+ check_out_year);
			
		var m_sec_diff =Date.parse(check_out_date) - Date.parse(check_in_date); //the difference between the dates in miliseconds 
		var dif = m_sec_diff/1000/60/60/24; //the difference as integer
	
		//birthday3 = daysElapsed(new Date(z2,z1,y),new Date(x2,x1,w));
		//alert("##"+birthday3+"##"+x1+z1);
		if (dif>28) {
			alert('Duration cannot be more than 28 nights. Duration will be set at 28 days');
			newdate = w+28;
			birthday01 = new Date(x2,x1-1,newdate,'12','00','00');
			n_x = birthday01.getDate();
			n_x1 = birthday01.getMonth()+1; // month array 0-11
			n_x2 = birthday01.getFullYear()+'';
			n_x2 = n_x2.substr(2,2);
			document.getElementById('D_E_'+n_x).selected = true;
			document.getElementById('MY_E_'+n_x1+''+n_x2).selected = true;
			document.getElementById('D_28').selected = true;
		} else if (dif<1) {
			alert('Please select another date. Duration cannot be less than 1 night');
			newdate = w+1;
			birthday01 = new Date(x2,x1-1,newdate,'12','00','00');
			n_x = birthday01.getDate();
			n_x1 = birthday01.getMonth()+1; // month array 0-11
			n_x2 = birthday01.getFullYear()+'';
			n_x2 = n_x2.substr(2,2);
			document.getElementById('D_E_'+n_x).selected = true;
			document.getElementById('MY_E_'+n_x1+''+n_x2).selected = true;
			document.getElementById('D_1').selected = true;
		} else {
			if ((z1-1==3 || z1-1==5 || z1-1==8 || z1-1==10) && y==31) {
				alert("This Month doesn`t have 31 days!");
				error = true;
			}
			if (z1 == 2) { // check for february 29th
				var isleap = (z2 % 4 == 0 && (z2 % 100 != 0 || z2 % 400 == 0));
				if (y > 29 || (y==29 && !isleap)) {
					alert("This February doesn`t have " + y + " days!");
					error = true;
				}
			}
			if (error==false) { //set the duration field
			
				document.getElementById('D_'+dif).selected = true;
			} else { // get the start date plus the duration and set that to end date
				newdate = w+a;
				birthday01 = new Date(x2,x1-1,newdate,'12','00','00');
				n_x = birthday01.getDate();
				n_x1 = birthday01.getMonth()+2; // month array 0-11
				n_x2 = birthday01.getFullYear()+'';
				n_x2 = n_x2.substr(2,2);
				document.getElementById('D_E_'+n_x).selected = true;
				document.getElementById('MY_E_'+n_x1+''+n_x2).selected = true;
			}
		}
	} else if (action==3) { //dur change
		birthday01 = new Date(x2,x1-1,w,'12','00','00');
		newdate = w+a;
		birthday02 = new Date(x2,x1-1,newdate,'12','00','00');
		n_z = birthday02.getDate();
		n_z1 = birthday02.getMonth()+1; // month array 0-11
		n_z2 = birthday02.getFullYear()+'';
		n_z2 = n_z2.substr(2,2);
		document.getElementById('D_E_'+n_z).selected = true;
		document.getElementById('MY_E_'+n_z1+''+n_z2).selected = true;
	}
	
	return true;
}

function checkandchangeFlights(x1,x2,y1,y2,diff,dur) {
	//alert(diff);
	error1 = false;
	error2 = false;
	x21 = x2.substr(0,x2.length-2);
	x22 = '20'+x2.substr(x2.length-2,2);
	y21 = y2.substr(0,y2.length-2);
	y22 = '20'+y2.substr(y2.length-2,2);
	x1 = Math.round(x1);
	y1 = Math.round(y1);
	x21 = Math.round(x21);
	y21 = Math.round(y21);
	x22 = Math.round(x22);
	y22 = Math.round(y22);
	diff = Math.round(diff);
		   
	dt01 = new Date(x22,x21,x1);
	dt02 = new Date(y22,y21,y1);
	if (daysElapsed(dt02,dt01)<1) {
		error2 = true;	
		//alert(daysElapsed(dt02,dt01));
		
		
	}
	
	if ((x21==4 || x21==6 || x21==9 || x21==11) && x1==31) {
		alert("Departure: This Month doesn`t have 31 days!");
		error1 = true;
	}
	if (x21 == 2) { // check for february 29th
		var isleap = (x22 % 4 == 0 && (x22 % 100 != 0 || x22 % 400 == 0));
		if (x1 > 29 || (x1==29 && !isleap)) {
			alert("Departure: This February doesn`t have " + x1 + " days!");
			error1 = true;
		}
	}
	if ((y21==4 || y21==6 || y21==9 || y21==11) && y1==31) {
		alert("Return: This Month doesn`t have 31 days!");
		error2 = true;
	}
	
	if (y21 == 2) { // check for february 29th
		var isleap = (y22 % 4 == 0 && (y22 % 100 != 0 || y22 % 400 == 0));
		if (y1 > 29 || (y1==29 && !isleap)) {
			alert("Return: This February doesn`t have " + y1 + " days!");
			error2 = true;
		}
	}

	if (error1==true) {
		dt02a = new Date(y22,y21-1,y1-diff);
		dt02d = dt02a.getDate();
		dt02m = dt02a.getMonth()+1;
		dt02y = dt02a.getFullYear()+'';
		dt02y = dt02y.substr(2,2);
		document.getElementById('CIDD_'+dt02d).selected = true;
		document.getElementById('CIDM_'+dt02m+dt02y).selected = true;
	}
	if (error2==true) {
		dt01a = new Date(x22,x21-1,x1+diff);
		dt01d = dt01a.getDate();
		dt01m = dt01a.getMonth()+1;
		dt01y = dt01a.getFullYear()+'';
		dt01y = dt01y.substr(2,2);
		document.getElementById('RD_'+dt01d).selected = true;
		document.getElementById('RM_'+dt01m+dt01y).selected = true;
	}
	
	if (error1==false && error2==false) {
		document.getElementById('DtDiff').value=daysElapsed(dt02,dt01);
		//alert(document.getElementById('DtDiff').value);
	}

}
