//----------------------------------------------------------------------------------------------------------------
// File name:contact-us.js
// Script For:contact us page
// Author:	Soraya Benasla
// Version:	3
// Last Updated:	26/01/09
// Updated By: 	26/01/09 Andrew Newnham - updated text
//							Andrew Newnham - changed dropdown titles and future res' link
//							Andrew Newnham - streamlined code
//							08/01/09 Andrew Newnham - updated code structure from <li> tags to standard <div> form layout
// Description:
//-----------------------------------------------------------------------------------------------------------------

function init(){
	document.write('<script src="/scripts/office-locator.js" type="text/javascript"></script>');
	document.write('<ol><li style="line-height:25px;">I want to contact a local Qatar Airways office, <a href="worldwide-offices.html">click here</a>,</li>');
	document.write('<li style="line-height:25px;">I would like to give Qatar Airways feedback, <a href="http://tellus.qatarairways.com/CR/" title="send us your feedback">click here</a>.</li>');
	document.write('<li style="line-height:25px;"><form action="" method="post" name="contactForm" id="contactForm">');
	document.write('I want to contact Qatar Airways in regards to: ');
	document.write('<select id="contactList" onchange="checkContact(this.contactForm);" name="contactList">');
	document.write('<option value="" selected="selected">-- Select --</option>');
	document.write('<option value="EXISTING_RES">Existing Reservation</option>');
	document.write('<option value="FUTURE_RES">Future Reservation</option>');
	document.write('<option value="CUSTOMER_RELATIONS">Past Flight</option>');
	document.write('<option value="OFFICE">Local Qatar Airways Office</option>');
	document.write('<option value="FEEDBACK">Website Feedback</option>');
	document.write('<option value="FFP">Privilege Club</option>');
	document.write('<option value="QRH">Qatar Airways Holidays</option>');
	document.write('<option value="CARGO">Cargo</option>');
	document.write('<option value="ALMAHA">Al Maha Service</option>');
	document.write('<option value="DISCOVER_QATAR">Discover Qatar</option>');
	document.write('<option value="PRESS_OFFICE">Press Office / News</option>');
	document.write('<option value="REFUND_REQUEST">Refund Request</option>');
	document.write('</select></form></li>');
	document.write('</ol><div id="result" style="margin:0;padding:0;"></div>');
}
init();

function displayContent(option){
	var output="";
	document.getElementById("result").innerHTML="";
	
	if(option=="EXISTING_RES"){
		output+='<p style="margin-top:15px">If you already have a reservation please contact your nearest Qatar Airways Office or the office of your departure city: ';
//		output+='<div style="position:absolute; left:325px; top:423px ">';
		output+=getListasString();
//		output+='</div></p>';
		
	}else{
		output+='<div class="form">';
		output+='<form name="theForm" action="/modules/contact-qr-mailer.php" method="post">';
		output+='<p style="border-top:none; margin-top:10px">3. Please fill out the form and click "submit". Fields marked <img src="/images/required.gif" alt="Required" width="3" height="4" /> are mandatory:</p>';
		if(option=="FEEDBACK"){
			output+='<input type="hidden" name="to" value="FEEDBACK" />';
			output+='<input type="hidden" name="contactEmail" value="websupport@qatarairways.com.qa" />';
		}else if(option=="FFP"){
			output+='<input type="hidden" name="to" value="FFP" />';
			output+='<input type="hidden" name="contactEmail" value="membersvc@qmiles.com" />';
		}else if(option=="QRH"){
			output+='<input type="hidden" name="to" value="QRH" />';
			output+='<input type="hidden" name="contactEmail" value="qrholidays@qatarairways.com" />';
		}else if(option=="CARGO"){
			output+='<input type="hidden" name="to" value="CARGO" />';
			output+='<input type="hidden" name="contactEmail" value="qrcargoenquiries@qatarairways.com.qa" />';
		}else if(option=="ALMAHA"){
			output+='<input type="hidden" name="to" value="ALMAHA" />';
			output+='<input type="hidden" name="contactEmail" value="almahaservice@qatarairways.com.qa" />';
		}else if(option=="DISCOVER_QATAR"){
			output+='<input type="hidden" name="to" value="DISCOVER_QATAR" />';
			output+='<input type="hidden" name="contactEmail" value="discoverqatar@qatarairways.com.qa" />';
		}else if(option=="PRESS_OFFICE"){
			output+='<input type="hidden" name="to" value="PRESS_OFFICE" />';
			output+='<input type="hidden" name="contactEmail" value="qrmedia@qatarairways.com.qa" />';
		}
		output+='<fieldset>';
		if (option=="FEEDBACK"){
			output+='<div class="label"><label for="feedbackType">Feedback Type</label></div>';
			output+='<div><select name="feedbackType">';
			output+='<option value="" selected="selected">-- Select --</option>';
			output+='<option value="BROKEN_LINK">Broken Link</option>';
			output+='<option value="BOOKING_QUERIES">Internet Booking Queries</option>';
			output+='<option value="PAYMENT_QUERIES">Internet Payment Queries</option>';
			output+='<option value="NAVIGATION">Navigation</option>';
			output+='<option value="check-in">Online Check-In</option>'; 
			output+='<option value="PAGE_CONTENT_INCORRECT">Page Content Incorrect</option>';
			output+='<option value="PERFORMANCE">Performance</option>';
			output+='<option value="OTHER">Other</option>';
			output+='</select></div>';
		}
		if(option=="FFP"){
			output+='<div class="label required"><label for="inRegards">Contact us in regards to</label></div>';
			output+='<div><select name="inRegards">';
			output+='<option value="" selected="selected">-- Select --</option>';
			output+='<option value="pinCode">Pin Code Request</option>';
			output+='<option value="loungeAccess">Lounge Access</option>';
			output+='<option value="changeBooking">Change of Award Booking</option>';
			output+='<option value="missingQmiles">Missing Qmiles</option>';
			output+='<option value="other">Other</option>';
			output+='</select></div>';
		}
		output+='<div class="label required"><label for="message">Message</label></div>';
	  output+='<div><input type="textarea" name="message" class="textarea" /></div>';
		if(option=="FFP"){
			output+='<div class="label required"><label for="memno">Privilege Club No.</label></div>';
			output+='<div><input type="text" name="memno" maxlength="9" /></div>';
		}
	  output+='<div class="label"><label for="title">Title</label></div>';
		output+='<div><select name="title" >';
		output+='<option value="" selected="selected">-- Select --</option>';
		output+='<option value="Mr.">Mr.</option>';
		output+='<option value="Mrs.">Mrs.</option>';
		output+='<option value="Ms.">Ms.</option>';
		output+='	</select></div>';
		output+='<div class="label required"><label for="fname">First Name</label></div>';
		output+='<div><input id="fname" name="fname" type="text" /></div>';
		output+='<div class="label required"><label for="lname">Last Name</label></div>';
		output+='<div><input name="lname" type="text" /></div>';
		if(option=="FFP"){
			output+='<div class="label required"><label for="memno">Passport/ID No.</label></div>';
			output+='<div><input type="text" name="passport" maxlength="9" /></div>';
			output+='<div class="label"><label for="dob">Date Of Birth:</label></div>';
			output+='<div><select id="dob" name="dob"><option value="day" selected="selected">Day</option>';
			for(i=0;i<31;i++){output+='<option value='+(i+1)+'">'+(i+1)+'</option>';}
			output+='</select>';
			output+='<select id="mob" name="mob"><option value="month" selected="selected">Month</option><option value="January">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option><option value="May">May</option><option value="June">June</option><option value="July">July</option><option value="August">August</option><option value="September">September</option><option value="October">October</option><option value="November">November</option><option value="December">December</option></select>';
			output+='<select id="yob" name="yob"><option value="year" selected="selected">Year</option>';
			for(i=1920;i<2007;i++){output+='<option value='+(i+1)+'">'+(i+1)+'</option>';}
			output+='</select></div>';
		}
		output+='<div class="label required"><label for="email">Email</label></div>';
		output+='<div><input name="email" type="text" /></div>';
		output+='<div class="label"><label for="telDay">Tel (Day)</label></div>';
		output+='<div><input name="telDay" type="text" /></div>';
		output+='<div class="label"><label for="telEvening">Tel (Evening)</label></div>';
		output+='<div><input name="telEvening" type="text" /></div>';
		output+='<div class="label"><label for="city">City</label></div>';
		output+='<div><input name="city" type="text" /></div>';
		output+='<div class="label"><label for="country">Country of residence</label></div>';
		output+='<div><select name="country">';
		output+='<option value="" selected="selected">-- Select --</option>';
		for(i=0;i<Countries.length;i++){output+='<option value="'+Countries[i].toUpperCase()+'">'+Countries[i]+'</option>';}
		output+='</select></div></fieldset>';
  	output+='<div class="label"></div><input type="submit" name="submit" value="submit" onClick="return validateForm(this.theForm,\''+option+'\');" class="button buttonLL" />';
	  output+='</form></div>';		
	  output+='<p style="border-top:none; margin-top:10px">4. For inquiries on <a href="http://www.qatarairways.com">qatarairways.com</a> contact: +974-420-3015 (available 24/7)</p>';
	}document.getElementById("result").innerHTML=output;
}

//Check if a contact list option is selected before submitting the contactForm
function checkContact(contactForm){
	if(document.contactForm.contactList.selectedIndex<=0){document.contactForm.contactList.focus();return false;
	}else{
		if(document.contactForm.contactList.value=="FUTURE_RES"){window.location='http://booking.qatarairways.com/qribe-web/public/showSearch.do';}
		else if(document.contactForm.contactList.value=="CUSTOMER_RELATIONS"){window.location='http://tellus.qatarairways.com';}
		else if(document.contactForm.contactList.value=="OFFICE"){window.location='/global/en/worldwide-offices.html';}
		else if(document.contactForm.contactList.value=="REFUND_REQUEST"){window.location='/global/en/refund-request.html';}
		else{displayContent(document.contactForm.contactList.value);}
	}return true;
}

//------------------------Function validateForm starts-------------------------------------------------------------
// check if all mandatory fields are provided 
// check if email has a valid format (NOT if the email is avalid email)
// check if the flight number is a valid QR flight number (i.e. 3 or 4 numerals) --> For CUSTOMER RELATIONS form ONLY
// check if the flight date is less than or equal to current date  --> For CUSTOMER RELATIONS form ONLY
function validateForm(theForm,contact){
	if(contact=='OTHER'){
		if(document.theForm.fname.value.length<=0){
			alert('Please enter First Name.');document.theForm.fname.focus();return false;
		}
		if(document.theForm.lname.value.length<=0){
			alert('Please enter Last Name.');document.theForm.lname.focus();return false;
		}
		if(document.theForm.email.value.length<=0){
			alert('Please enter your Email.');document.theForm.email.focus();return false;
		} 
		var str = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		if(!str.test(document.theForm.email.value)){
			alert('Invalid E-mail Address! Please re-enter.');document.theForm.email.focus();return false;
		}
		if(document.theForm.OCR.value.length<=0){
			alert('Please enter the Confimation Number.');document.theForm.OCR.focus();return false;
		}
		if(document.theForm.origin.value.length<=0){
			alert('Please enter Origin City.');document.theForm.origin.focus();return false;
		}
		if(document.theForm.CC4Digits.value.length<=0){
			alert('Please enter the Credit Card last 4 digits.');document.theForm.CC4Digits.focus();return false;
		} 
		if( document.theForm.code1.value.length<=0){
			alert('Please enter the Country code.');document.theForm.code1.focus();return false;
 		} 
		if( document.theForm.telDay.value.length<=0){
			alert('Please enter the Telephone Number.');document.theForm.telDay.focus();return false;
		} 
	}
if(contact=='FEEDBACK'){
	if(document.theForm.feedbackType.value.length<=0){
		alert('Please select a feedback type.');document.theForm.feedbackType.focus();return false;
	}
}
if(contact=='CUSTOMER_RELATIONS'){
 	if(document.theForm.requestType.value.length<=0){
		alert('Please select a request type.');document.theForm.requestType.focus();return false;
	}
	if(document.theForm.subject.value.length<=0){
		alert('Please select a subject.');document.theForm.subject.focus();return false;
	}
	if( document.theForm.airlineCode.value.length<=0){
		alert('Please enter the airline code.');document.theForm.airlineCode.focus();return false;
	}
	//validate flight number format -> should be exactly 2 chars followed by 3 or 4 digits
	var reg1=new RegExp("^[a-zA-Z]{2}$", "gi"); // compile the regular expression
 	var str=document.theForm.airlineCode.value;
	if(reg1.test(str)==false){
		alert('Invalid airline code format! Please re-enter.');document.theForm.airlineCode.focus();return false;
	}
	if( document.theForm.flightNo.value.length<=0){
		alert('Please enter the flight number.');document.theForm.flightNo.focus();return false;
	}
	//validate flight number format -> should be exactly 2 chars followed by 3 or 4 digits
	var reg1 = new RegExp("^[0-9]{3,4}$", "gi"); // compile the regular expression
 	var str = document.theForm.flightNo.value;
	if( reg1.test(str)==false){
		alert('Invalid flight number format! Please re-enter.');document.theForm.flightNo.focus();return false;
	}
	if(document.theForm.flightDate.value.length<=0){
		alert('Please select the day of your flight date.');document.theForm.flightDate.focus();return false;
	}
	if(document.theForm.flightMonth.value.length<=0){
		alert('Please select the month of your flight date.');document.theForm.flightMonth.focus();return false;
	}
	if(document.theForm.flightYear.value.length<=0){
		alert('Please select the year of your flight date.');document.theForm.flightYear.focus();return false;
	}
	//validate flight date -> should be less than or equal to today's date
	var now=new Date();
	var year=document.theForm.flightYear.value;
	var month=document.theForm.flightMonth.value;
	var date=document.theForm.flightDate.value;
	var date=new Date(year,(month-1),date,now.getHours(),now.getMinutes(),now.getSeconds(),now.getMilliseconds());
	if(now.getTime()<date.getTime()){
		alert("Flight date cannot be later than today's date. Please re-enter your flight date.");document.theForm.flightYear.focus();return false;
	}	
	if( document.theForm.flightOrigin.value.length<=0){
		alert('Please select the departure city.');
		document.theForm.flightOrigin.focus();
		return false;
	}
	if( document.theForm.flightDestination.value.length<=0){
		alert('Please select the destination city.');document.theForm.flightDestination.focus();return false;
	}
	if(document.theForm.flightOrigin.value==document.theForm.flightDestination.value){
		alert('Origin and destination cities cannot be the same.');document.theForm.flightDestination.focus();return false;
	}	
}
if(contact=='FFP'){
	if(document.theForm.inRegards.selectedIndex==3){document.theForm.contactEmail.value='ffbe@qmiles.com';}
	if(document.theForm.memno.value.length>0){//validate FFP number format -> exactly 9 digits
		var reg1=new RegExp("^[0-9]{9}$");// compile the regular expression
 		var str=document.theForm.memno.value;
		if(reg1.test(str)==false ){
			alert('Invalid Privilege Club number! Please re-enter.');document.theForm.memno.focus();return false;
		}
	}
}
if(document.theForm.message.value.length<=0){
	alert('Please enter a message.');document.theForm.message.focus();return false;
}
if(document.theForm.title.value.length<=0){
	alert('Please select your title.');document.theForm.title.focus();return false;
}
if(document.theForm.fname.value.length<1){
	alert('Please enter your first name.');document.theForm.fname.focus();return false;
}
if(document.theForm.lname.value.length<1){
	alert('Please enter your last name.');document.theForm.lname.focus();return false;
}
if(document.theForm.email.value.length<=0){
	alert('Please enter your email address.');document.theForm.email.focus();return false;
}
var str = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(!str.test(document.theForm.email.value)){
	alert('Invalid E-mail Address! Please re-enter.');document.theForm.email.focus();return false;
}
document.theForm.submit();
return true;
}