/* The following function creates an XMLHttpRequest object... */

function file_check(){if(!document.referrer) location.href='index.php';}

function clear_inst(){
document.cruise_form.cruise_combo.selectedIndex = 0;
document.cruise_form.cruise_combo.length =0;
}

function createRequestObject() {
  var ro;
  try {ro = new XMLHttpRequest();}
  catch(e) {
    try {ro = new ActiveXObject("Msxml2.XMLHTTP");}
    catch(e) {
      try {ro = new ActiveXObject("Microsoft.XMLHTTP");}
      catch(e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  return ro;
}


/*function createRequestObject(){
	var request_o; 
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_o = new XMLHttpRequest();
	}
	return request_o; 
}*/

var http = createRequestObject(); 
var http4msgs = createRequestObject();
var http2save = createRequestObject();
var http4users = createRequestObject();
var http4invite = createRequestObject();
var http4private;
var response4chat = "0";
var response4private = "0";
var response4invite = "0";
var http4alert = createRequestObject(); 
// function select_shipine is called on "on_change()" event of cruise_combo drop down object
// this fills data in conjunction of the function format_cruise_lines()

function select_shipline(){
	http.open('get', 'get_cruiselines.php?cruiselineid='+ document.cruise_form.cruise_combo.value);
	http.onreadystatechange = format_cruiselines; 
	http.send(null);
}

function format_cruiselines(){
	if(http.readyState == 4){
		var response = http.responseText;
		if(http.status == 200){
			document.cruise_form.ship_combo.length =0;
			document.cruise_form.ship_combo.options[document.cruise_form.ship_combo.length] = new Option('Name-Of-Ship');
			 m_array = response.split("|");
			 for(i=1;i<m_array.length;i+=2)
			 document.cruise_form.ship_combo.options[document.cruise_form.ship_combo.length] = new Option(m_array[i+1],m_array[i]);
		}
	} // endif http.readystate
} // end function format_cruiselines()


// function select_airlines() is called on "on_change()" event of airline_country drop down object
// this fills data in conjunction of the function format_air_lines()

function select_airlines(){
	http.open('get', 'get_airlines.php?countryid='+ document.airline_form.airline_country.value);
	http.onreadystatechange = format_air_lines; 
	http.send(null);
}

function format_air_lines(){
	if(http.readyState == 4){
		var response = http.responseText;
		if(http.status == 200){
			document.airline_form.airline_combo.length =0;
			document.airline_form.airline_combo.options[document.airline_form.airline_combo.length] = new Option('-Select-Airline-');
			a_array = response.split("|");	 
			
			for(j=1;j<a_array.length;j+=2)
			document.airline_form.airline_combo.options[document.airline_form.airline_combo.length] = new Option(a_array[j+1],a_array[j]);
		}
	} // endif http.readystate
} // end function format_air_lines()

// SAVE CHAT MESSAGES IN THE DB
function save_chat_message(status){
	http2save.open('get', 'save_chat_message.php?msgtxt='+ document.chatting.TempChatString.value+'&status='+status);
	http2save.onreadystatechange = blank_function; 
	http2save.send(null);
	//document.getElementById('testdivforstarttime').innerHTML = document.getElementById('testdivforstarttime').innerHTML + "<br />"+ document.chatting.TempChatString.value;
} // end function save_chat_message()

function blank_function(){
	if(http2save.readyState == 4)		
		{
		var response1 = http2save.responseText;
		if(response1) 
			{
			//alert(response1);
			document.chatting.SystemMessage.value='Session is Expired. Please login again !';
			document.chatting.action = 'index.php';
			document.chatting.submit();				
		}
		//alert(document.chatting.chatstring.value);
		// document.chatting.chatstring.value = '';  // empty the user typed message text box
		//get_chat_message();
	}

}

// GET CHAT MESSAGES FROM THE DB
function get_chat_message(){
	//alert("chat message");
	if(response4chat == "0"){
		response4chat = "1";
		http4msgs.open('get', 'get_chat_message.php');
		http4msgs.onreadystatechange = fill_chat_message; 
		http4msgs.send(null);
		//check_for_private_chat();
	}
} // end function get_chat_message()

function fill_chat_message(){
	if(http4msgs.readyState == 4){
		var response = http4msgs.responseText;
		if(http4msgs.status == 200){
			chat_grid = document.getElementById('showChat'); // assign chat grid to a variable
			CurrentData = chat_grid.innerHTML ; 			 // store chat grid to a variable			
			//DatatoAdd = response;
			DatatoAdd = '';
			msg_array = response.split("|");	
			if(msg_array[0] == "MAINLOBBY"){
				DatatoAdd = msg_array[1];
				//alert("its main lobby");
				//alert(DatatoAdd);
				//document.getElementById('testdivforendtime').innerHTML = document.getElementById('testdivforendtime').innerHTML + "<br />"+ DatatoAdd;
			}
			/*	
			if(msg_array[0] == "PRIVATECHAT")	
				{
				//alert(msg_array[1]+msg_array[2]);
				tempa = 'dummy text';
				var MessageForDiv = 'showChat000' + msg_array[1];
				//alert("the name="+MessageForDiv);
				var div_id = 'm000'+msg_array[1];
				//CurrentChatInWindow = MessageForDiv.innerHTML;
				//MessageForDiv.innerHTML =  CurrentChatInWindow + msg_array[2]+ '<br>';  
				var CurrentChatInWindow = document.getElementById(MessageForDiv).innerHTML;
				
				//toggle_layer('m000'+msg_array[1]);
				//alert('good1111');
				if(document.getElementById(div_id).style.visibility == 'hidden'){
					//alert('good'+document.getElementById(div_id).style.visibility);
					toggle_layer1(div_id);
				//document.getElementById(div_id).style.visibility == 'visible';
				}
				document.getElementById(MessageForDiv).innerHTML = CurrentChatInWindow + msg_array[2]+ '<br>';
				MessageForDiv.scrollTop = 90000000;
			}
			*/
		//	alert(msg_array[1]);	
			
			if(DatatoAdd)
			{
				chat_grid.innerHTML = CurrentData + DatatoAdd;	// append new message with previous messages of chat grid				
				chat_grid.scrollTop = 90000000;			// scroll down the chat grid to some very high value. This will cause grid focus to always remain at the bottom			
						
			}
			//document.getElementById('testdivforstarttime').innerHTML = document.getElementById('testdivforstarttime').innerHTML + "<br />"+ Date()+'=Request';
					
		}
		response4chat = "0";
		get_lobby_users();
	} // endif http.readystate
	
} // end function fill_caht_message()


function check_for_private_chat(){
	//if(response4private == "0"){
		response4private = "1";
		var theRand = Math.random();
		http4private = createRequestObject();
		http4private.open('get','get_private_new_chat.php',true);
		http4private.onreadystatechange = do_for_private;
		http4private.send(null);
	//}
}

function do_for_private(){
	if(http4private.readyState == 4){
		
		var MessageForDiv;
		if(http4private.status == 200){
			
			var response = http4private.responseText;
			
			
			if(response != ""){
				//alert("its is ="+response);
				//document.getElementById('testdivforendtime').innerHTML = document.getElementById('testdivforendtime').innerHTML + "<br />" + Date() + response;
				var msg_array = response.split("|");
				var tempa = 'dummy text';
				if(msg_array[1] != ""){
					var MessageForDiv = 'showChat000' + msg_array[1];
					var div_id = 'm000'+msg_array[1];
					/*	CurrentChatInWindow = MessageForDiv.innerHTML;
					MessageForDiv.innerHTML =  CurrentChatInWindow + msg_array[2]+ '<br>';*/  
					var CurrentChatInWindow = document.getElementById(MessageForDiv).innerHTML;
					
					//toggle_layer('m000'+msg_array[1]);
					//alert('good1111');
					if(document.getElementById(div_id).style.visibility == 'hidden'){
						//alert('good'+document.getElementById(div_id).style.visibility);
						toggle_layer1(div_id);
					//document.getElementById(div_id).style.visibility == 'visible';
					}
					document.getElementById(MessageForDiv).innerHTML = CurrentChatInWindow + msg_array[2]+ '<br>';
					document.getElementById(MessageForDiv).scrollTop = 90000000;
				}
			}
			response4private = "0";
		}
	}
}


// GET CHAT MESSAGES FROM THE DB
function get_lobby_users(){
	//alert("lobby users");
	http4users.open('get', 'get_lobby_users.php');
	http4users.onreadystatechange = fill_lobby_users; 
	http4users.send(null);	
	
	get_current_rating();
//redirect_to_main();				
} // end function get_chat_message()

function fill_lobby_users(){
	if(http4users.readyState == 4){
		var response = http4users.responseText;
		if(http4users.status == 200){
			users_grid = document.getElementById('showUserList'); // assign chat grid to a variable
			users_grid.innerHTML = response;
		}
		//document.getElementById('testdivforendtime').innerHTML = document.getElementById('testdivforendtime').innerHTML + "<br />"+ Date()+'=Response';
		//get_chat_message();
	} // endif http.readystate
} // end function fill_lobby_users()


// SEND INVITATION TO OTHER USER FOR PRIVATE CHAT
function send_private_chat_invitation(){
	http.open('get', 'send_private_chat_invitation.php?RequestedTo='+ document.chatting.RequestedTo.value);
	http.onreadystatechange = do_nothing_2; 
	http.send(null);
//redirect_to_main();				
} // end function send_private_chat_invitation()

function do_nothing_2(){
	if(http.readyState == 4){
		var response = http.responseText;		
			//users_grid = document.getElementById('showUserList'); // assign chat grid to a variable
			//users_grid.innerHTML = response;
			} // endif http.readystate
} // end function do_nothing_2()


// SHOW INVITATION MESSAGE FOR PRIVATE CHAT
function check_for_invitation(){
	if(response4invite == "0"){
		response4invite = "1";
		//document.getElementById('testdivforstarttime').innerHTML = document.getElementById('testdivforstarttime').innerHTML + "<br />"+ Date();
		//alert('check');
		rn = Math.random();
		//alert("invitation check");
		http4invite.open('GET', 'check_for_invitation.php?randnumber='+rn, true);
		http4invite.onreadystatechange = do_three_nothing; 
		http4invite.send(null);
	}
} // end function send_private_chat_invitation()

function do_three_nothing(){
	
	
	if(http4invite.readyState == 4){
		var response = http4invite.responseText;
		
		if(http4invite.status == 200){
			//alert("before="+response);
			//document.getElementById('testdivforendtime').innerHTML = document.getElementById('testdivforendtime').innerHTML + "<br />" + Date() + response;
			
			//var response = http.responseText;		
			//alert("after"+response);
			// the response will be pipe-delimited-value-set
			// b_array[0] will contain YES or NO
			// b_array[1] will contain the UserNick who requested for private chat to show in alert message
			// b_array[2] will contain the tableID to update back status
			b_array = response.split("|");
			//alert(b_array);
			if(b_array[0] == "YES"){
				//alert(b_array);
				
				AlertForPrivateSession(b_array[1],b_array[2]);
			}
			response4invite = "0";
		}
		//}
	}
	
	//check_for_private_chat();
	// endif http.readystate
} // end function do_nothing_2()

function update_private_chat_invitation(PrivateChatRecordID,Status){
	rn = Math.random();
	http.open('get', 'update_chat_invitation_status.php?randnumber='+rn+'&PrivateChatRecordID='+PrivateChatRecordID+'&Status='+Status);
	http.onreadystatechange = do_four_nothing; 
	http.send(null);
} // end function send_private_chat_invitation()

function do_four_nothing(){
	if(http.readyState == 4){	
		check_for_private_chat();
		//var response = http.responseText;
		//alert(response);
	} // endif http.readystate
} // end function do_four_nothing_()

function update_private_chat_message(Message,DivNo){
	//document.getElementById('testdivforstarttime').innerHTML = document.getElementById('testdivforstarttime').innerHTML + "<br />"+ Date()+"-"+Message;
	http.open('get', 'update_private_chat_message.php?Message='+Message+'&DivNo='+DivNo);
	http.onreadystatechange = do_five_nothing; 
	http.send(null);
} // end function send_private_chat_invitation()

function do_five_nothing(){
	if(http.readyState == 4){	
		var response = http.responseText;	
		//alert(response);
	} // endif http.readystate
} // end function do_four_nothing_()

function get_private_chat_message(DivNumber){
	
	http.open('get', 'get_private_chat_message.php?DivNumber='+DivNumber);
	http.onreadystatechange = do_get_private_chat_message; 
	http.send(null);
}  //// end function get_private_chat_message()


function do_get_private_chat_message(){
	if(http.readyState == 4){	
		var response = http.responseText;
		if(http.status == 200){
			
			//alert(response);
			//var div_id=document.chatting.DIVNO.value;
			//var currDiv = div_id-1;
			document.getElementById('showChat0001').innerHTML=response;
		}
	} // endif http.readystate
} 
function show_banner(){
//alert('helo'+document.getElementById('homeTopBanner'));
	http.open('get', 'get_topBottom_home_banner.php');
	http.onreadystatechange = do_get_top_bottom_banner; 
	http.send(null);

}
function do_get_top_bottom_banner()
{
	if(http.readyState == 4){	
		var response = http.responseText;		
		//alert(response+'helo');
		//document.getElementById('showChat0001').innerHTML=response;
		var timer=setTimeout("show_banner()",1000);
	} // endif http.readystate	
}
function Block_User(div_no){
	document.getElementById('m'+div_no).style.visibility = 'hidden';
	http.open('get', 'block_user.php?div_no='+div_no);
	http.onreadystatechange = do_sixe_nothing; 
	http.send(null);
}
function do_sixe_nothing(){
	if(http.readyState == 4){	
		var response = http.responseText;	
		
		//alert(response);
	} // endif http.readystate
} // end function do_four_nothing_()


function flag_the_message(the_msg_id){
	http.open('get', 'flag_message.php?lobby_msg_id='+the_msg_id);
	http.onreadystatechange = do_flag_message; 
	http.send(null);
}

function do_flag_message(){
	if(http.readyState == 4){	
		var response = http.responseText;
		if(http.status == 200){
			var msg_array = response.split("|");
			if(msg_array[0] == "Your Session is Expired"){
				alert("Your Session is Expired");
				window.close();
				return;
			}
			else{
				document.getElementById('flagDiv'+msg_array[1]).innerHTML = '<img src="images/flagged2.png" style="border:none;" height="15" width="16" />';
				var theParent = document.getElementById('flagDiv'+msg_array[1]).parentNode;
				document.getElementById('flagDiv'+msg_array[1]).style.backgroundColor="#C8F9FD";
				//var testCount = 0;
				
				//while(theParent.){
					//testCount++;
				//}
				for(j=0; j < theParent.childNodes.length; j++){
					//alert(theParent.childNodes[j].tagName);
					if(theParent.childNodes[j].tagName == 'TD'){
						theParent.childNodes[j].style.backgroundColor="#C8F9FD";
					}
					//var testId = theParent.childNodes[j].getAttribute("id");
					//document.getElementById(testId).style.backgroundColor="#C8F9FD";
				}
				
				//alert(theParent.innerHTML);
				//theParent.style.backgroundColor="#C8F9FD";
				return;
			}
		}
		
	}
}

function delete_the_message(the_msg_id){
	document.getElementById('flagDiv'+the_msg_id).innerHTML = 'Deleting';
	http.open('get', 'delete_chat_message.php?lobby_msg_id='+the_msg_id);
	http.onreadystatechange = do_delete_message; 
	http.send(null);
}

function do_delete_message(){
	if(http.readyState == 4){	
		var response = http.responseText;
		if(http.status == 200){
			var msg_array = response.split("|");
			if(msg_array[0] == "Your Session is Expired"){
				alert("Your Session is Expired");
				window.close();
				return;
			}
			else{
				document.getElementById('flagDiv'+msg_array[1]).innerHTML = '';
				var theParent = document.getElementById('flagDiv'+msg_array[1]).parentNode;
				//document.getElementById('flagDiv'+msg_array[1]).style.display="none";
				//alert(theParent.innerHTML);
				theParent.style.display="none";
				return;
			}
		}
		
	}
}
function save_rating(theVal){
	//alert("the val="+theVal);
	http.open('get', 'rate_the_lobby.php?the_rating='+theVal);
	http.onreadystatechange = do_lobby_rating; 
	http.send(null);
}

function do_lobby_rating(){
	if(http.readyState == 4){	
		var response = http.responseText;
		if(http.status == 200){
			var msg_array = response.split("|");
			if(msg_array[0] == "Your Session is Expired"){
				//alert("Your Session is Expired");
				return;
			}
			else{
				//document.getElementById('flagDiv'+msg_array[1]).innerHTML = "<strong>Flagged</strong>";
				return;
			}
			
		}
		//document.getElementById('the_current_rating').innerHTML = response;
	}
}


function get_current_rating(){
	http.open('get', 'get_rating_value.php');
	http.onreadystatechange = show_rating; 
	http.send(null);
	
}

function show_rating(){
	if(http.readyState == 4){	
		var response = http.responseText;
		if(response != ""){
			if(response != "NOCHANGE"){
				var theRedStar_1 = '<img src="images/red_star.jpg" height="16" width="16" title="Poor" />';
				var theRedStar_2 = '<img src="images/red_star.jpg" height="16" width="16" title="Fair" />';
				var theRedStar_3 = '<img src="images/red_star.jpg" height="16" width="16" title="Average" />';
				var theRedStar_4 = '<img src="images/red_star.jpg" height="16" width="16" title="Good" />';
				var theRedStar_5 = '<img src="images/red_star.jpg" height="16" width="16" title="Excellent" />';
				var theGreyStar_1 = '<img src="images/grey_star.jpg" height="16" width="16" title="Poor" />';
				var theGreyStar_2 = '<img src="images/grey_star.jpg" height="16" width="16" title="Fair" />';
				var theGreyStar_3 = '<img src="images/grey_star.jpg" height="16" width="16" title="Average" />';
				var theGreyStar_4 = '<img src="images/grey_star.jpg" height="16" width="16" title="Good" />';
				var theGreyStar_5 = '<img src="images/grey_star.jpg" height="16" width="16" title="Excellent" />';
				
				var theCurrRate = Math.round(Number(response));
				if(theCurrRate == 0){
					document.getElementById('the_current_rating').innerHTML = theGreyStar_1+theGreyStar_2+theGreyStar_3+theGreyStar_4+theGreyStar_5;
				}
				if(theCurrRate == 1){
					document.getElementById('the_current_rating').innerHTML = theRedStar_1+theGreyStar_2+theGreyStar_3+theGreyStar_4+theGreyStar_5;
				}
				if(theCurrRate == 2){
					document.getElementById('the_current_rating').innerHTML = theRedStar_1+theRedStar_2+theGreyStar_3+theGreyStar_4+theGreyStar_5;
				}
				if(theCurrRate == 3){
					document.getElementById('the_current_rating').innerHTML = theRedStar_1+theRedStar_2+theRedStar_3+theGreyStar_4+theGreyStar_5;
				}
				if(theCurrRate == 4){
					document.getElementById('the_current_rating').innerHTML = theRedStar_1+theRedStar_2+theRedStar_3+theRedStar_4+theGreyStar_5;
				}
				if(theCurrRate == 5){
					document.getElementById('the_current_rating').innerHTML = theRedStar_1+theRedStar_2+theRedStar_3+theRedStar_4+theRedStar_5;
				}
				
				//document.getElementById('the_current_rating').innerHTML = 'Current Rating : '+response;
			}
		}
	}
}

function isDate(dateStr) {
	
	var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) {
		alert("Please enter date as mm/dd/yyyy.");
		return false;
	}
	
	day = matchArray[3]; // p@rse date into variables
	month = matchArray[1];
	year = matchArray[5];
	
	if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}
	
	if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn`t have 31 days!")
		return false;
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn`t have " + day + " days!");
			return false;
		}
	}
	return true; // date is valid
}

function sendAlertEmail(){
	if(document.getElementById('name4alert').value == "" || document.getElementById('name4alert').value == 'Enter recipients name'){
		alert('Recipent name is missing');
		return;
	}
	if(document.getElementById('email4alert').value == "" || document.getElementById('email4alert').value == 'Enter recipients email'){
		alert('Recipent email is missing');
		return;
	}
	
	document.getElementById('name4alert').disabled = true;
	document.getElementById('email4alert').disabled = true;
	document.getElementById('btn4alert').disabled = true;
	
	http4alert.open('get', 'sendalertemail.php?person_name='+ document.getElementById('name4alert').value+'&person_email='+document.getElementById('email4alert').value+'&lobby_name='+document.getElementById('lobby4alert').value);
	http4alert.onreadystatechange = confirmEmailSent; 
	http4alert.send(null);
}

function confirmEmailSent(){
	if(http4alert.readyState == 4){	
		var response = http4alert.responseText;
		if(response != ""){
			if(response == "OK"){
				document.getElementById('name4alert').value = 'Enter recipients name';
				document.getElementById('email4alert').value = 'Enter recipients email';
				
				document.getElementById('name4alert').disabled = false;
				document.getElementById('email4alert').disabled = false;
				document.getElementById('btn4alert').disabled = false;
				document.getElementById('alertSendForm').style.display = 'none';
			}
			else{
				alert("Response="+response);	
			}
		}
		
	}
}