//----------------------------------------------------------------------------------------------------------------
// File name:contact-us.js
// Script For:contact us page
// Author:	Soraya Benasla
// Version:	3
// Last Updated:	02/02/09
// Updated By: 	
//							02/02/10 Andrew Newnham - Updated form so it can be used as POTM and has local/global specific fields. Also improved coding and reduced by 100 lines.
//							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 initial(){
 	var output="";
	if(folder=='global/en'){
		output+='<ol><li style="line-height:25px;">I want to contact a local Qatar Airways office, <a href="/global/en/worldwide-offices.html">click here</a>,</li>';
		output+='<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>';
		output+='<li style="line-height:25px;">';
	}	
	output+='<form action="" method="post" name="contactForm" id="contactForm">';
	if(folder=='global/en'){output+='I want to contact Qatar Airways in regards to: ';}//Global
	else{output+='To contact Qatar Airways please select from one of these options or see our local office contact details below. ';}//Local
	output+='<select id="contactList" onchange="optionAction(this.contactForm);" name="contactList">';
	output+='<option value="" selected="selected">-- Select --</option>';
	output+='<option value="existingRes">Existing Reservation</option>';
	output+='<option value="bookingQueries">Internet Booking Queries</option>';
	output+='<option value="futureRes">Future Reservation</option>';
	output+='<option value="pastFlight">Past Flight</option>';
	if(folder=='global/en'){output+='<option value="office">Local Qatar Airways Office</option>';}
	output+='<option value="feedback">Website Feedback</option>';
	output+='<option value="ffp">Privilege Club</option>';
	output+='<option value="qrh">Qatar Airways Holidays</option>';
	output+='<option value="cargo">Cargo</option>';
	output+='<option value="almaha">Al Maha Service</option>';
	output+='<option value="discoverQatar">Discover Qatar</option>';
	output+='<option value="pressOffice">Press Office / News</option>';
	output+='<option value="refund">Refund Request</option>';
	output+='</select></form>';
	if(folder=='global/en'){output+='</li></ol>';}
	output+='<div id="result" style="margin:15px 0 0 0;padding:0;"></div>';
	document.getElementById('contactUs').innerHTML=output;
}
initial();

function optionAction(contactForm){
//	document.getElementById('result').style.display='block';
	document.getElementById("result").innerHTML='';
	var option=document.contactForm.contactList.value;
	var output="";
	switch(option){//Display any pre-form details or forward to relevant URI 
		case'futureRes':window.location='http://booking.qatarairways.com/qribe-web/public/showSearch.do';break;
		case'pastFlight':window.location='http://tellus.qatarairways.com';break;
		case'office':window.location='/global/en/worldwide-offices.html';break;
		case'refund':window.location='/'+folder+'/refund-request.html';break;
		case'feedback':case'bookingQueries':
			output+='For inquiries on <a href="/" title="Go to your qatarairways.com homepage">qatarairways.com</a> contact: +974-420-3015 (available 24/7)';
			break;
		case'existingRes':
			if(folder=='global/en'){
				output+='If you already have a reservation please contact your nearest Qatar Airways office or the office of your departure city:';
				output+=getListasString();}
			else{output+='If you already have a reservation please contact your local Qatar Airways office. Details below:';}
			break;
	}
	if(option!='existingRes'){//Open form tags
		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">Please fill out the form and click "submit". Fields marked <img src="/images/required.gif" alt="Required" width="3" height="4" /> are mandatory:</p>';
		output+='<input type="hidden" name="to" value="'+option+'" />';
		output+='<input type="hidden" name="contactEmail" value="';
		switch(option){
			case'feedback':output+='websupport@qatarairways.com.qa';break;
  		case'bookingQueries':output+='websupport@qatarairways.com.qa';break;
			case'ffp':output+='membersvc@qmiles.com';break;
			case'qrh':output+='qrholidays@qatarairways.com';break;
			case'cargo':output+='qrcargoenquiries@qatarairways.com.qa';break;
			case'almaha':output+='almahaservice@qatarairways.com.qa';break;
			case'discoverQatar':output+='discoverqatar@qatarairways.com.qa';break;
			case'pressOffice':output+='qrmedia@qatarairways.com.qa';break;
		}
		output+='" /><fieldset style="margin-bottom:15px;">';
		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="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" onchange="checkoption()">';
			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="refundreq">Refund Request</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>';		
	}document.getElementById("result").innerHTML=output;
}

function checkoption(){if(document.theForm.inRegards.value=="refundreq"){window.location='/global/en/ffp-refund-req.html';}}

//------------------------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=='pastFlight'){
 	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;
}