// JScript File

var http = getHTTPObject();

function getHTTPObject()
{
	//debugger;
	var xmlhttp;
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		// IE 6
		var XMLHTTP_IDS = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
		var success = false;
		for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
			try {
				xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
				success = true;
			} catch (e) {}
		}
		if (!success)
			throw new Error('Unable to create XMLHttpRequest.');
	}
	return xmlhttp;
}

function handleHttpResponse()
{
	//debugger;
	if (http.readyState < 4) {}
	if (http.readyState == 4) {}
}

function fnGetAllElementValues()
{
	//debugger;
	//do selectList elements
	var str = '?'+fnGetValueForSelectList(0);
	str += '&'+fnGetCodeFromSelectListValue(1);
	//str += fnGetValueForSelectList(1);
	//do textBox elements
	str += '&'+fnGetValueForTextBox(2);
	str += '&'+fnGetValueForTextBox(3);
	//do selectList elements
	str += '&'+fnGetValueForSelectList(4);
	str += '&'+fnGetValueForSelectList(5);
	str += '&'+fnGetValueForSelectList(6);
	str += '&'+fnGetValueForSelectList(7);
	//do radioButton elements
	str += '&'+fnGetValueForRadioButton(8);
	str += '&'+fnGetValueForRadioButton(9);
	str += '&'+fnGetValueForRadioButton(10);
	fnDoDeepLinkAjax(str);
}

function fnDoDeepLinkAjax(qVal)
{
    try {
		http.open("GET", siteBase+"/WebsiteContent/deeplink_submit.aspx" + qVal, false);
		//http.onreadystatechange = handleHttpResponse;
		http.send(null);
	} catch (e) {
		alert('There was an error connecting to the server. Please try again.\n\n[' + e.message + ']');
	}
}

function fnGetCodeFromSelectListValue(s)
{
	try {
		var selValue = document.getElementById(fnGetElementId(s)).value;
		//alert(selValue);
		var idx = selValue.indexOf('(');
		//alert(idx);
		selValue = selValue.substr(idx + 1);
		//alert(selValue);
		selValue = selValue.replace(')', '');
		//alert(selValue);
		return 'opt' + s + '=' + selValue;
	} catch(e) {
		alert('error name: ' + ename + '<br/>error message: ' + e.message);
	}
}

function fnGetValueForSelectList(s)
{
	try {
		return 'opt' + s + '=' + document.getElementById(fnGetElementId(s)).value;
	} catch(e) {
		alert('error name: ' + ename + '<br/>error message: ' + e.message);
	}
}

function fnGetValueForTextBox(s)
{
	try {
		return 'opt' + s + '=' + document.getElementById(fnGetElementId(s)).value;
	} catch(e) {
		alert('error name: ' + ename + '<br/>error message: ' + e.message);
	}
}

function fnGetValueForRadioButton(s) {
	var oElm = document.getElementById(fnGetElementId(s));
	var val = '';
	if (oElm.checked)
		val = oElm.value;
	return 'opt' + s + '=' + val;
}

function fnDoSpecialsSearchDateRangeAjax(from, to)
{
	if (from=='' || to=='') {
		alert("Please enter both a start date and an end date.");
		return 1;
	}
    var months = [];
    months['Jan'] = "01"; months['Feb'] = "02"; months['Mar'] = "03"; months['Apr'] = "04"; months['May'] = "05"; months['Jun'] = "06";
    months['Jul'] = "07"; months['Aug'] = "08"; months['Sep'] = "09"; months['Oct'] = "10"; months['Nov'] = "11"; months['Dec'] = "12";
	var td = new Date();
	var frD = splitStringDate(from);
	var toD = splitStringDate(to);
	if (frD[2]>toD[2] || (frD[2]==toD[2] && (months[frD[1]]>months[toD[1]] || (months[frD[1]]==months[toD[1]] && frD[0]>toD[0])))) {
		alert("Please select a start date before the end date.");
		return 1;
	}
	if (frD[2]<td.getFullYear() || months[frD[1]]<td.getMonth()+1 || frD[0]<td.getDate()) {
		alert("Please select a start date that is in the future.");
		return 1;
	}
	
	var d=new Date;
	try {
		http.open("GET", siteBase+"/WebsiteContent/date_range_submit.aspx?from=" + from + "&to=" + to + "&dummy=" + d.valueOf(), false);
		//http.onreadystatechange = handleHttpResponse;
		http.send(null);
	} catch (e) {
		alert('There was an error connecting to the server. Please try again.\n\n[' + e.message + ']');
	}
	return 0;
}

function validateDepartures(DepartureDateControl, ReturnDateControl, MinimumDateControl, LabelID, numAdults, numChildren, numInfants)
{
	var ctrlName='GettingThereFlightDropDowns';
	/*if (document.getElementById(ctrlName+'_'+numAdults).value == '') {
		alert("Please select a number of adults.");
		return false;
	}
	if (document.getElementById(ctrlName+'_'+numChildren).value == '') {
		alert("Please select a number of children.");
		return false;
	}
	if (document.getElementById(ctrlName+'_'+numInfants).value == '') {
		alert("Please select a number of infants.");
		return false;
	}*/

	var months = [];
	months['Jan'] = "01"; months['Feb'] = "02"; months['Mar'] = "03"; months['Apr'] = "04"; months['May'] = "05"; months['Jun'] = "06";
	months['Jul'] = "07"; months['Aug'] = "08"; months['Sep'] = "09"; months['Oct'] = "10"; months['Nov'] = "11"; months['Dec'] = "12";
	start = document.getElementById(ctrlName+'_'+DepartureDateControl).value;
	if (ReturnDateControl!='')
		end = document.getElementById(ctrlName+'_'+ReturnDateControl).value;
	check = document.getElementById(ctrlName+'_'+MinimumDateControl).value;

	myfirstdate = splitStringDate(start);
	if (ReturnDateControl!='')
		myseconddate = splitStringDate(end);
	mycheckdate = splitStringDate(check);

	dateA = myfirstdate[2] + months[myfirstdate[1]] + myfirstdate[0];
	if (ReturnDateControl!='')
		dateB = myseconddate[2] + months[myseconddate[1]] + myseconddate[0];
	dateC = mycheckdate[2] + months[mycheckdate[1]] + mycheckdate[0];

	if((ReturnDateControl=='' || dateB >= dateA) && dateA >= dateC)
		return true;
	if (ReturnDateControl!='' && dateB < dateA) {
		alert("Please enter a return date that is after your departure date.");
		return false;
	}
	if (dateA < dateC) {
		alert("Please enter a departure date that is more than 5 days from today's date to facilitate payment clearance and non e-ticket processing and postage.");
		return false;
	}
	return false;
}

function splitStringDate(DateString)//in the format dd-MMM-yyyy
{
	var day = "";
	var month = "";
	var year = "";
	day = DateString.substring(0,2);
	month = DateString.substring(3,6);
	year = DateString.substring(7,11);
	var result = new Array();
	result[0] = day;
	result[1] = month;
	result[2] = year;
	return result;
}