var g_paxNum = 2;
var g_propNum = 1;
var g_deptNum = 1;
var g_retNum = 1;
var alertMsg = "";
var dYear, dMonth, dDay, rYear, rMonth, rDay;

var id = new Array();
var property_name = new Array();
var prop_row_num = 1 ;
var dept_row_num = 1 ;
var ret_row_num = 1;

/*
      This function creates one new row for departure date dynamically

*/

function addDeparture() {
	
	// Remove the previous More Departure date button
	var MoreDept = document.getElementById("MoreDept" + g_deptNum);
	var cellMoreDept = document.getElementById("tdMoreDept" + g_deptNum);
	
	var btnMoreDept = document.getElementById("btnMoreDept");
	cellMoreDept.removeChild(btnMoreDept);
	
	
	var tr1 = document.createElement("tr");
	var td = document.createElement("td");
	tr1.appendChild(td);
	var td = document.createElement("td");
	dept_row_num = dept_row_num + 1;
	var name = "txtDeptDate"+dept_row_num;
	    
	td.innerHTML = "<input name=\"" + name + "\" id=\"" + name + "\" type=\"text\" valign=\"top\" size=\"15\"> <img id=\"imgDepDate\" src=\"modules/dateIcon.gif\" onClick=\"showCalendar('" + name + "', 'imgDepDate', 2006, 2010);\">";
	setAttribute(td, "valign", "top");
	
	tr1.appendChild(td);
	var tr2 = document.createElement("tr");
		
		td = document.createElement("td");
		g_deptNum += 1;
		setAttribute(td, "id", "tdMoreDept" + g_deptNum);		
		setAttribute(td, "align", "center");
		setAttribute(td, "colspan", "2");
		setAttribute(td, "valign", "top");
		td.innerHTML = "<input type=\"button\" name=\"btnMoreDept\" id=\"btnMoreDept\" value=\"More departure dates\" size=\"15\" style=\"width:180px\" onClick=\"javascript: addDeparture()\">";
		tr2.appendChild(td);

	var theTable = document.getElementById('tblDept');
	// Add the row
		if (theTable.firstChild.nodeType == "1") // IE
		   {
			theTable.firstChild.appendChild(tr1);
			theTable.firstChild.appendChild(tr2);
		   }
		else if	(theTable.firstChild.nodeType == "3") // FireFox
		   {	theTable.firstChild.nextSibling.appendChild(tr1);
			theTable.firstChild.nextSibling.appendChild(tr2);
		   }
	
	
	document.frmEnquiry.hidDept.value = g_deptNum;
				 
			}
		
/*
      This function creates one new row for departure date dynamically

*/

function addReturn() {
	
	// Remove the previous More Departure date button
	var MoreRet = document.getElementById("MoreRet" + g_retNum);
	var cellMoreRet = document.getElementById("tdMoreRet" + g_retNum);
	
	var btnMoreRet = document.getElementById("btnMoreRet");
	cellMoreRet.removeChild(btnMoreRet);
	ret_row_num = ret_row_num + 1;
	var name = "txtRetDate"+ret_row_num;
	
	var tr1 = document.createElement("tr");
	var td = document.createElement("td");
	tr1.appendChild(td);
	var td = document.createElement("td");
	
	td.innerHTML = "<input name=\"" + name + "\" id=\"" + name + "\" type=\"text\" valign=\"top\" size=\"15\"> <img id=\"imgRetDate\" src=\"modules/dateIcon.gif\" onClick=\"showCalendar('" + name + "', 'imgRetDate', 2006, 2010);\">";
	setAttribute(td, "valign", "top");
		
	tr1.appendChild(td);
	var tr2 = document.createElement("tr");
		
		td = document.createElement("td");
		g_retNum += 1;
		setAttribute(td, "id", "tdMoreRet" + g_retNum);		
		setAttribute(td, "align", "center");
		setAttribute(td, "colspan", "2");
		setAttribute(td, "valign", "top");
		td.innerHTML = "<input type=\"button\" name=\"btnMoreRet\" id=\"btnMoreRet\" value=\"More return dates\" size=\"15\" style=\"width:180px\" onClick=\"javascript: addReturn()\">";
		tr2.appendChild(td);

	var theTable = document.getElementById('tblRet');
	// Add the row
		if (theTable.firstChild.nodeType == "1") // IE
		   {
			theTable.firstChild.appendChild(tr1);
			theTable.firstChild.appendChild(tr2);
		   }
		else if	(theTable.firstChild.nodeType == "3") // FireFox
		   {	theTable.firstChild.nextSibling.appendChild(tr1);
			theTable.firstChild.nextSibling.appendChild(tr2);
		   }
	
	
	document.frmEnquiry.hidRet.value = g_retNum;
				 
			}
		


/*
      This function creates one new row for a property dynamically

*/

function addProperty() {
	
	// Remove the previous More Properties button
	var MoreProp = document.getElementById("MoreProp" + g_propNum);
	var cellMoreProp = document.getElementById("tdMoreProp" + g_propNum);
	
	var btnMoreProp = document.getElementById("btnMoreProp");
	cellMoreProp.removeChild(btnMoreProp);
	
	
	var tr1 = document.createElement("tr");
	var td = document.createElement("td");
	tr1.appendChild(td);
	var td = document.createElement("td");
		prop_row_num = prop_row_num + 1;
		//alert(prop_row_num);
		name = "property"+prop_row_num;
		var jj = "<select name="+name+" class='dropdown'><option selected value=''>Select Property</option>"
			for (i=0;i<property_name.length;i++)
			{
				jj+="<option value='" + id[i] + "'>" + property_name[i] + "</option>";
			}
		jj += "</select>";

	td.innerHTML = jj;
	
	tr1.appendChild(td);
	var tr2 = document.createElement("tr");
		
		td = document.createElement("td");
		g_propNum += 1;
		setAttribute(td, "id", "tdMoreProp" + g_propNum);		
		setAttribute(td, "align", "center");
		setAttribute(td, "colspan", "2");
		td.innerHTML = "<input type=\"button\" name=\"btnMoreProp\" id=\"btnMoreProp\" value=\"Select more properties\"  style=\"width:150px\" onClick=\"javascript: addProperty()\">";
		tr2.appendChild(td);

	var theTable = document.getElementById('tblProp');
	// Add the row
		if (theTable.firstChild.nodeType == "1") // IE
		   {
			theTable.firstChild.appendChild(tr1);
			theTable.firstChild.appendChild(tr2);
		   }
		else if	(theTable.firstChild.nodeType == "3") // FireFox
		   {	theTable.firstChild.nextSibling.appendChild(tr1);
			theTable.firstChild.nextSibling.appendChild(tr2);
		   }
	
	
	document.frmEnquiry.hidPropNum.value = g_propNum;
				 
			}
			



/*
	This function creates 1 more pax fields dynamically
	080708 changed by Sunny: 4 more to noOfPaxes more
*/
function morePax(noOfPaxes) {
	// Remove the previous More Passengers button
	var cellMorePax = document.getElementById("tdMorePax" + g_paxNum / noOfPaxes);
	var btnMorePax = document.getElementById("btnMorePax");
	cellMorePax.removeChild(btnMorePax);	
	
	// Create noOfPaxes more pax
	var tblPax = document.getElementById("tblPax");
	
	for (var i = 1; i <= noOfPaxes; i++) {
		
		var trPax = document.createElement("tr");
		// Header text
	  	var td = document.createElement("td");
		var header = document.createTextNode("Passenger #" + (g_paxNum + i));
		td.appendChild(header);
	  	trPax.appendChild(td);
	  	// Title
		td = document.createElement("td");
		name = "txtPaxTitle" + (g_paxNum + i);
		var jj = "<select name="+name+" class='dropdown'>"
				+ "<option selected value=''>Title</option>"
				+ "<option value=\"Mr\">Mr</option>"
				+ "<option value=\"Dr\">Dr</option>"
				+ "<option value=\"Mrs\">Mrs</option>"
				+ "<option value=\"Miss\">Miss</option>"
				+ "<option value=\"Ms\">Ms</option>"
			+ "</select>";													
		td.innerHTML = jj;
		td.setAttribute("valign","middle");
		trPax.appendChild(td);
		// Surname
	  	td = document.createElement("td");
	  	var surname = document.createElement("input");
    		setAttribute(surname, "type", "text");
		setAttribute(surname, "name", "txtPaxSurname" + (g_paxNum + i));	    
		td.appendChild(surname);
		trPax.appendChild(td);
		// First names
		td = document.createElement("td");
		var firstName = document.createElement("input");
		setAttribute(firstName, "type", "text");
		setAttribute(firstName, "name", "txtPaxFstname" + (g_paxNum + i));		
		td.appendChild(firstName);
		trPax.appendChild(td);
		// Date of birth
	  	td = document.createElement("td");
		var html = "<input size='10' name=\"txtPaxDOB" + (g_paxNum + i) + "\" id=\"txtPaxDOB" + (g_paxNum + i) + "\" type=\"text\"";
		html += "onFocus=\"showCalendar('txtPaxDOB" + (g_paxNum + i) + "', 'imgPaxDOB" + (g_paxNum + i) + "', 1920, 2008);\" ";
		html += "onClick=\"showCalendar('txtPaxDOB" + (g_paxNum + i) + "', 'imgPaxDOB" + (g_paxNum + i) + "', 1920, 2008);\">";
		html += "&nbsp;<img id=\"imgPaxDOB" + (g_paxNum + i) + "\" src='" + "modules\\dateIcon.gif' ";
		html += "onClick=\"showCalendar('txtPaxDOB" + (g_paxNum + i) + "', 'imgPaxDOB" + (g_paxNum + i) + "', 1920, 2008);\">";
		td.innerHTML = html;
		trPax.appendChild(td);
		// The More Passengers button		
		if (i == noOfPaxes) {
			var trBut = document.createElement("tr");
			trBut.className = "more_pax_line";
			td = document.createElement("td");
			trBut.appendChild(td);
			td = document.createElement("td");
				setAttribute(td, "id", "tdMorePax" + (g_paxNum / noOfPaxes + 1));
				setAttribute(td, "colspan", "4");
				td.innerHTML = "<a href=\"javascript: morePax(" + noOfPaxes + ");\" id=\"btnMorePax\">Add more passengers</a>";
			trBut.appendChild(td);		
		}
		
		// Add the row
		if (tblPax.firstChild.nodeType == "1") { // IE
			tblPax.firstChild.appendChild(trPax);
			tblPax.firstChild.appendChild(trBut);			
		} else if (tblPax.firstChild.nodeType == "3") { // FireFox
			tblPax.firstChild.nextSibling.appendChild(trPax);
			tblPax.firstChild.nextSibling.appendChild(trBut);
		}
	}	
	
	g_paxNum += noOfPaxes;
	document.frmEnquiry.hidPaxNum.value = g_paxNum;
}


/*
	This function sets the value of the attribute (attr) of the node (node) as (value) 
*/
function setAttribute(node, attr, value) {
	var tmp = document.createAttribute(attr);
    tmp.nodeValue = value;
    node.setAttributeNode(tmp);
}

/*
	This function validates the input of the form
	080708 changed by Sunny: validations
*/
function validate() { 
	// Verify the presence of all mandatory fields
	
		if (document.frmEnquiry.txtDeptDate1.value == "") 
			alertMsg += "Please enter your departure date.\n";		
		if (document.frmEnquiry.selDuration.value == "")
			alertMsg += "Please select number of nights.\n";
		if (document.frmEnquiry.txtDepAirp.value == "") 
			alertMsg += "Please select your departure airport.\n";		
		/*if (document.frmEnquiry.txtRetDate1.value == "") 
			alertMsg += "Please enter your return date.\n";*/	
		if (document.frmEnquiry.property1.value == "") 
			alertMsg += "Please select a property.\n";
		if (document.frmEnquiry.txtPaxTitle1.value == "") 
			alertMsg += "Please select title of the lead passenger.\n";			
		if (document.frmEnquiry.txtPaxSurname1.value == "") 
			alertMsg += "Please enter the surname of the lead passenger.\n";
		if (document.frmEnquiry.txtPaxFstname1.value == "") 
			alertMsg += "Please enter the first names of the lead passenger.\n";
		if (document.frmEnquiry.txtPaxDOB1.value == "") 
			alertMsg += "Please enter the date of birth of the lead passenger.\n";
		/*if (document.frmEnquiry.txtHouse.value == "") 
			alertMsg += "Please enter the house no/name of the lead passenger.\n";
		if (document.frmEnquiry.txtAddr1.value == "") 
			alertMsg += "Please enter the first address line of the lead passenger.\n";
		if (document.frmEnquiry.txtTown.value == "") 
			alertMsg += "Please enter the town of the lead passenger.\n";
		if (document.frmEnquiry.txtPost.value == "") 
			alertMsg += "Please enter the post code of the lead passenger.\n";*/
		if (document.frmEnquiry.txtTelDay.value == "")
			alertMsg += "Please enter the telephone number (day) of the lead passenger.\n";
		if (document.frmEnquiry.txtEmail.value == "")
			alertMsg += "Please enter the email address of the lead passenger.\n";
		if (document.frmEnquiry.txtReEmail.value == "")
			alertMsg += "Please re-enter the email address of the lead passenger.\n";
		if (document.frmEnquiry.txtEmail.value != "" && document.frmEnquiry.txtReEmail.value != "" &&
			document.frmEnquiry.txtReEmail.value != document.frmEnquiry.txtEmail.value)
			alertMsg += "Please make sure the email address of the lead passenger and the one re-entered are the same.\n";
	// Validate dates
	var depDate = document.frmEnquiry.txtDeptDate1.value;		
	var depDateValid = false;
	if (depDate != "") depDateValid = validateDate(depDate, "departure date");	
	/*var retDate = document.frmEnquiry.txtRetDate1.value;
	var retDateValid = false;
	if (retDate != "") retDateValid = validateDate(retDate, "return date");*/
	for (var i = 1; i <= g_paxNum; i++) {
		var dob = document.getElementById("txtPaxDOB" + i).value;
		if (dob.value != "" && i == 1) validateDate(dob, "date of birth of the lead passenger");
		else if (dob.value != "" && i > 1) validateDate(dob, "date of birth of passenger #" + i);
	}	
	
	// Whether depDate is behind retDate
	/*if (depDateValid && retDateValid) {
		parseDates();
		if (dYear > rYear || (dYear == rYear && (dMonth > rMonth || (dMonth == rMonth && dDay > rDay)))) 
			alertMsg += "The departure date should not be behind the return date. Please try again.\n";	
	}*/
	
	// Validate email address
	var emailRegex = /.+@.+\..+/;
	/*if (document.frmEnquiry.txtEmail.value != "" && !document.frmEnquiry.txtEmail.value.match(emailRegex))
		alertMsg += "Please enter the email address of the lead passenger in the following format: name@example.com.\n";
	*/
	if (alertMsg != "") {
		alert(alertMsg);
		alertMsg = "";
		return false;
	}
	
	return true;
}

/*
	This function validates a date input
*/
function validateDate(date, fieldPrompt) {
	if (date != "") {
		// Format
		var dateRegex = /^\d{2}-\w{3}-\d{4}$/;
		if (!date.match(dateRegex)) {
			alertMsg += "Please enter the " + fieldPrompt + " using the format dd-mmm-yyyy.\n";
			return false;
		}
		// Month
		date = date.toLowerCase();
		var month = date.substring(date.indexOf("-") + 1, date.lastIndexOf("-"));
		if (month != "jan" && month != "feb" && month != "mar" && month != "apr" && month != "may" 
			&& month != "jun" && month != "jul" && month != "aug" && month != "sep" && month != "oct" 
			&& month != "nov" && month != "dec") {
			alertMsg += "Please enter the month of the " + fieldPrompt + " correctly.\n";
			return false;
		}
		// Whether the date is invalid
		var day = parseInt(date.substring(0, date.indexOf("-")), 10);
		if (day < 1 || (day > 31 && (month == "jan" || month == "mar" || month == "may" || month == "jul" || month == "aug" || month == "oct" || month == "dec"))
			|| (day > 30 && (month == "apr" || month == "jun" || month == "sep" || month == "nov"))) {	
			alertMsg += "The date of the " + fieldPrompt + " is not valid. Please enter it correctly.\n";
			return false;
		}
		var year = parseInt(date.substring(date.lastIndexOf("-") + 1), 10);
		var leap = (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? true : false);
		if ((leap && month == "feb" && day > 29) || (!leap && month == "feb" && day > 28)) {		
			alertMsg += "The date of the " + fieldPrompt + " is not valid. Please enter it correctly.\n";
			return false;
		}
	}
	return true;
}

/*
	This function validates the property selection
 */
function validateProperty() {
	var property = document.frmEnquiry.property1.value;
	if (property == "destination") {
		var msg = "You have selected a destination.\n\n";
		msg    += "To select a property, please select a property name below the categories ";
		msg    += "'hotels', 'villas', or 'gulets'.";
		alert(msg);
	} else if (property == "resort") {
		var msg = "You have selected a resort.\n\n";
		msg    += "To select a property, please select a property name below the categories ";
		msg    += "'hotels', 'villas', or 'gulets'.";
		alert(msg);
	}
}

/*
	This function gets depDate and retDate and parse them
*/
function parseDates() {
	// Create a month hash table
	var monthHash = new Array();
	monthHash["jan"] = 0;
	monthHash["feb"] = 1;
	monthHash["mar"] = 2;
	monthHash["apr"] = 3;
	monthHash["may"] = 4;
	monthHash["jun"] = 5;
	monthHash["jul"] = 6;
	monthHash["aug"] = 7;
	monthHash["sep"] = 8;
	monthHash["oct"] = 9;
	monthHash["nov"] = 10;
	monthHash["dec"] = 11;
	// Get and parse depDate and retDate
	var depDate = document.frmEnquiry.txtDeptDate1.value;
	var retDate = document.frmEnquiry.txtRetDate1.value;
	dDay = depDate.substring(0, 2);
	dMonth = depDate.toLowerCase().substring(3, 6);
	dMonth = monthHash[dMonth];
	dYear = depDate.substring(7);
	rDay = retDate.substring(0, 2);
	rMonth = retDate.toLowerCase().substring(3, 6);
	rMonth = monthHash[rMonth];
	rYear = retDate.substring(7);
}

/*
	This function displays a Simple Calendar widget for the dates selection
*/
function showCalendar(txtField, imgIcon, baseYear, endYear) { 
	
	scwBaseYear = baseYear;
	scwDropDownYears = endYear - scwBaseYear + 1; 	
	scwDateDisplayFormat = "dd-mmm-yyyy"; 
	scwDateOutputFormat  = "dd-mmm-yyyy";
	scwShowInvalidDateMsg = false;
	scwShowOutOfRangeMsg = false;
	scwShowDoesNotExistMsg = false;
	scwShowInvalidAlert = false;
	scwShow(document.getElementById(txtField), document.getElementById(imgIcon));
}

