var PanelOne = null; var PanelTwo = null;
var domainname = "http://book.alliancetravel.com";
var siid="13"
if (PanelOne == null) {
    PanelOne = document.getElementById("PanelOneTBL")
}
if (PanelTwo == null) {
    PanelTwo = document.getElementById("PanelTwoTBL")
}

function focusObj(a) {
    if (a.value == a.getAttribute("defValue")) {
        a.value = "";
    }
}

function blurObj(a) {
    if (a.value == "") {
        a.value = a.getAttribute("defValue");
    }
}

function highlight_tocity() {

    if (document.getElementById("toCity").value == "Airport or City") {
        document.getElementById("toCity").style.border = "1px dotted #19ACE2";
        document.getElementById("toCity").value = ""; 
        } 
}

function highlight_fromCity() {

    if (document.getElementById("fromCity").value == "Airport or City") {
        document.getElementById("fromCity").style.border = "1px dotted #19ACE2";
        document.getElementById("fromCity").value = "";
    }
}

function chSearchType(a) {
    debugger;
    switch (a) {

        case "RoundTrip":
//            $("#newdiv").contents().remove();
            $('#newdiv').load('include/roundtrip.html');
//            document.getElementById("ReturnFlightTR3").style.display = "block";
//            document.getElementById("ReturnFlightTR4").style.display = "block";
//            document.getElementById("booking_engine_center").style.background = "transparent url(images/engine_part2_bgd.png)  no-repeat ";
//            document.getElementById("booking_engine_center").style.height = "380px";
            break;
        case "OneWay":
//            $("#newdiv").contents().remove();
            $('#newdiv').load('include/roundtrip.html');           
            //document.getElementById("SearchTypeOneWay").checked = true;
            //$('#booking_engine_center').load('include/roundtrip.html');
//            document.getElementById("ReturnFlightTR3").style.display = "none";
//            document.getElementById("ReturnFlightTR4").style.display = "none";
//            document.getElementById("booking_engine_center").style.background = "transparent url(images/engine_part2_bgd.png)  no-repeat ";
//            document.getElementById("booking_engine_center").style.height = "380px";
//           
            break;
        case "MultiDestination":
//            $("#newdiv").contents().remove();
            $('#newdiv').load('include/multidestination.html');
//            document.getElementById("booking_engine_center").style.background = "transparent url(images/engine_part2_bgd_large.png) no-repeat ";
//            document.getElementById("booking_engine_center").style.height = "474px";

            break;
    }
}
// How many segment need to display
function newbuildPanelTwo() {
    segTotal = document.forms.AirSearchForm.segCount.value;
    for (var a = 0; a < segTotal; a++) {
        PanelTwo.rows[a].style.display = "block"
    }
}

var dtCh = "/";
var minYear = 1900;
var maxYear = 2100;

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}

function isDate(dtStr) {
    dtCh = "/";
    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
    var strMonth = dtStr.substring(0, pos1)
    var strDay = dtStr.substring(pos1 + 1, pos2)
    var strYear = dtStr.substring(pos2 + 1)
    strYr = strYear
    if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
    }
    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYr)
    var Error = '';
    var flag = true;
    if (pos1 == -1 || pos2 == -1) {

        flag = false;
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {

        flag = false;
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {

        flag = false;
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {

        flag = false;
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {

        flag = false;
    }
    if (flag == true)
        return ''
    else
        return 'Enter Date as mm/dd/yyyy'
}


function ValidateFlightSearch() {

    alert_message = "";
    var flag = true;
    formObj = document.forms["AirSearchForm"];

    if (formObj.searchType[0].checked == true || formObj.searchType[1].checked == true) {
        // start check4_def_val
        if (formObj.toCity.value == "Airport or City") {


            alert_message += " - Destination airport\n";
           

        }
        if (formObj.fromCity.value == "Airport or City") {
            alert_message += " - Arrival airport\n";
          
        }
        
    }


    //End check4_def_val

    if (formObj.searchType[0].checked == true || formObj.searchType[1].checked == true) {
        var Error = '';
        if (formObj.flyDate_from.value == "" || formObj.flyDate_from.value == formObj.flyDate_from.getAttribute("defValue"))
            alert_message += " - Departure date\n";


        else if (Error = isDate(formObj.flyDate_from.value)) {
            if (Error != '') {
                alert_message += " - Departure Date - " + Error + "\n";
            }
        }
        if (formObj.searchType[0].checked == true) {

            if (formObj.flyDate_return.value == "" || formObj.flyDate_return.value == formObj.flyDate_return.getAttribute("defValue"))
                alert_message += " - Return date\n";

            else if (formObj.flyDate_from.value != "" && formObj.flyDate_return.value != "" && formObj.flyDate_from.value != formObj.flyDate_from.getAttribute("defValue") && formObj.flyDate_return.value != formObj.flyDate_return.getAttribute("defValue")) {
                Error = '';
                if (Error = isDate(formObj.flyDate_return.value)) {

                    if (Error != '') {
                        alert_message += " - Return Date - " + Error + "\n";
                    }
                }

                if (formObj.flyDate_from.getAttribute("defValue") == "dd/mm/yyyy") {
                    var tmp = formObj.flyDate_from.value.split('/');
                    var flyDate_from;
                    var flyDate_return;

                    if (tmp.length == 3)
                        flyDate_from = tmp[1] + "/" + tmp[0] + "/" + tmp[2];

                    tmp = formObj.flyDate_return.value.split('/');

                    if (tmp.length == 3)
                        flyDate_return = tmp[1] + "/" + tmp[0] + "/" + tmp[2];

                    if (new Date(flyDate_from) > new Date(flyDate_return))
                        alert_message += " - Return date should be greater than departure date\n";


                }
                else {
                    if (new Date(formObj.flyDate_from.value) > new Date(formObj.flyDate_return.value))
                        alert_message += " - Return date should be greater than departure date\n";
                }
            }
        }
    }


    //date validation end


    if (formObj.searchType[2] != null) {

        if (formObj.searchType[2].checked) {
            var isExit = false;
            for (i = 1; i <= document.forms["AirSearchForm"].segCount.value; i++) {
                var Error = '';
                if (formObj["fromCity_" + i].value == "" || formObj["fromCity_" + i].value == "Airport or City") alert_message += " - flight #" + i + " departure city\n";
                if (formObj["toCity_" + i].value == "" || formObj["toCity_" + i].value == "Airport or City") alert_message += " - flight #" + i + " arrival city\n";
                if (formObj["flyDate_" + i].value == "" || formObj["flyDate_" + i].value == formObj["flyDate_" + i].getAttribute("defValue")) alert_message += " - flight #" + i + " departure date\n";

                if (Error = isDate(formObj["flyDate_" + i].value) && formObj["flyDate_" + i].value != "") {
                    if (Error != '') {
                        Error = isDate(formObj["flyDate_" + i].value);
                        alert_message += " - flight #" + i + " departure date - " + Error + "\n";
                    }
                }
                else {
                    if (i > 1) {
                        for (j = 1; j < i; j++) {

                            if (formObj["flyDate_" + i].getAttribute("defValue") == "dd/mm/yyyy") {
                                var tmp = formObj["flyDate_" + i].value.split('/');
                                var flyDate_from;
                                var flyDate_return;

                                if (tmp.length == 3)
                                    flyDate_from = tmp[1] + "/" + tmp[0] + "/" + tmp[2];

                                tmp = formObj["flyDate_" + j].value.split('/');

                                if (tmp.length == 3)
                                    flyDate_return = tmp[1] + "/" + tmp[0] + "/" + tmp[2];

                                if (flyDate_from < flyDate_return && formObj["flyDate_" + i].value != "" && formObj["flyDate_" + i].value != formObj["flyDate_" + i].getAttribute("defValue")) {
                                    isExit = true;
                                    alert_message += " - flight #" + i + " departure date\n";
                                    break;
                                }
                            }
                            else {
                                //alert(formObj["flyDate_" + j].value);

                                if (Date.parse(formObj["flyDate_" + i].value) < Date.parse(formObj["flyDate_" + j].value) && formObj["flyDate_" + i].value != "" && formObj["flyDate_" + i].value != formObj["flyDate_" + i].getAttribute("defValue")) {
                                    isExit = true;
                                    alert_message += " - flight #" + i + " departure date should not be less than - flight #" + j + "\n";
                                    break;
                                }
                            }
                        }
                    }
                    if (isExit) break;
                }

            }
        }
    }
    //date validation end


    if (formObj.searchType[0].checked == false && formObj.searchType[1].checked == false) {
        if (formObj.searchType[2] != null) {
            if (formObj.searchType[2].checked == false) {
                alert("Please choose trip type");
                return false;
            }
        }
        else {
            alert("Please choose trip type");
            return false;
        }
    }
    if (formObj.searchType[0].checked) {
        if (formObj.fromCity.value == "" || formObj.fromCity.value == formObj.fromCity.getAttribute("defValue")) alert_message += " - departure city\n";
        if (formObj.toCity.value == "" || formObj.toCity.value == formObj.toCity.getAttribute("defValue")) alert_message += " - arrival city\n";


    }
    else if (formObj.searchType[1].checked) {
        if (formObj.fromCity.value == "" || formObj.fromCity.value == formObj.fromCity.getAttribute("defValue")) alert_message += " - departure city\n";
        if (formObj.toCity.value == "" || formObj.toCity.value == formObj.toCity.getAttribute("defValue")) alert_message += " - arrival city\n";

    }


    passNum = parseInt(formObj.aCount.value) + parseInt(formObj.cCount.value);
    if (formObj.sCount != null) {
        passNum = passNum + parseInt(formObj.sCount.value);
    }
    if (formObj.yCount != null) {
        passNum = passNum + parseInt(formObj.yCount.value);
    }

    if (passNum == 0) {
        alert_message += " - the number of passengers travelling \n";
    }

    if (alert_message != "") {
        alert("The following information was not provided : \n" + alert_message);
        return false;
    }
   
	 
 if(domainname != undefined || domainname != ""){
	OpenSiteSearchWindow();
	}

return true;
}
function OpenSiteSearchWindow() {
    var dateformat = "mm/dd/yyyy";
	//specify site name where to redirect
   

	//specify the site id
	
    var SearchType;
    var segCount, AirSegCount, SearchAppend = '',
        SegmentData = '';
    var flyDate_from, flyDate_return, fromCity, toCity;
    var flyDate = new Array(8);

    var formObj = document.forms["AirSearchForm"];
	
    if (formObj.searchType[0].checked == true) {
        SearchType = 'RoundTrip';
        flyDate_from = formObj.flyDate_from.value;
        flyDate_return = formObj.flyDate_return.value;
    } else if (formObj.searchType[1].checked == true) {
        SearchType = 'OneWay';
        flyDate_from = formObj.flyDate_from.value;
        flyDate_return = formObj.flyDate_return.value;
    } else {
        SearchType = 'MultiDestination';
        segCount = parseInt(formObj.segCount.value);
        for (var i = 1; i <= segCount; i++)
        flyDate[i] = formObj["flyDate_" + i].value;
    }

    if (dateformat == "dd/mm/yyyy") {
        var tmp;
        if (SearchType != "MultiDestination") {
            tmp = flyDate_from.Split("/");
            flyDate_from = tmp[1] + "/" + tmp[0] + "/" + tmp[2];

            tmp = flyDate_return.Split("/");
            flyDate_return = tmp[1] + "/" + tmp[0] + "/" + tmp[2];
        } else {
            for (var i = 1; i <= segCount; i++) {
                var tmpDate = flyDate[i];
                tmp = tmpDate.Split("/");
                flyDate[i] = tmp[1] + "/" + tmp[0] + "/" + tmp[2];
            }
        }
    }

    if (SearchType == "RoundTrip") {

		CallOpenPopupforClickTripz();
        AirSegCount = 2;
		fromCity = formObj.fromCity.value.substring(0, 3);
		toCity = formObj.toCity.value.substring(0, 3);
        SegmentData = SegmentData + "AirSeg_1=" + encodeURI(fromCity + "|" + toCity + "|" + flyDate_from);
        SegmentData = SegmentData + "&AirSeg_2=" + encodeURI(toCity + "|" + fromCity + "|" + flyDate_return);
        SearchAppend = fromCity + "|" + toCity + "|" + flyDate_from + "|" + flyDate_return;
    } else if (SearchType == "OneWay") {
		CallOpenPopupforClickTripz();
        AirSegCount = 1;
		fromCity = formObj.fromCity.value.substring(0, 3);
		toCity = formObj.toCity.value.substring(0, 3);
        SegmentData = SegmentData + "AirSeg_1=" + encodeURI(fromCity + "|" + toCity + "|" + flyDate_from);
        SearchAppend = fromCity + "|" + toCity + "|" + flyDate_from + "|" + flyDate_return;
    } else if (SearchType == "MultiDestination") {
        AirSegCount = segCount;
        //add segments data
        var fromCity_m;
        var toCity_m;
        for (var i = 1; i <= parseInt(AirSegCount); i++) {
            fromCity_m = '';
            fromCity_m = formObj["fromCity_" + i].value.substring(0, 3);

            toCity_m = '';
            toCity_m = formObj["toCity_" + i].value.substring(0, 3);

            SegmentData = SegmentData + "AirSeg_" + i + "=" + encodeURI(fromCity_m + "|" + toCity_m + "|" + flyDate[i]) + "&";
            SearchAppend = SearchAppend + fromCity_m + "|" + toCity_m + "|" + flyDate[i] + "|";
        }
        SearchAppend = SearchAppend.substring(0, SearchAppend.length - 1);
    }

    var aCount = formObj.aCount.value;
    var sCount = formObj.sCount.value;
    var yCount = formObj.yCount.value;
    var cCount = formObj.cCount.value;
    var iSCount = formObj.iSCount.value;
    var iLCount = formObj.iLCount.value;
    var Airline = formObj.prefAirline.value;
    var SelClass = formObj.prefClass.value;

    var DirectFlight = 'false';
    var nonStopOption = new String(formObj.NonStopOption.value);
    if (nonStopOption == "true") DirectFlight = 'true';

    var Refundable = 'false';

    DeleteCookie('OneWay', '', '');
    DeleteCookie('RoundTrip', '', '');
    DeleteCookie('MultiDestination', '', '');

    setCookie(SearchType, SearchAppend, 30);

	var winHeight = 0;
	var winWidth = 0;
	var winLeft = 0;
	var winTop = 100;
	if(screen.height > 600)
		winHeight = 600;
	else
		winHeight = screen.height - 155;
	if(screen.width > 800){
		winWidth = 800;
		winLeft = Math.round(screen.width / 2) - 400;
	}
	else{
		winWidth = screen.width - 10;
	}
	
	if(domainname != null || domainname != ""){
		window.open(domainname + "/web/air/default.aspx?siid=" + siid + "&AirSegCount=" + AirSegCount + "&aCount=" + aCount + "&sCount=" + sCount + "&cCount=" + cCount + "&yCount=" + yCount + "&iSCount=" + iSCount + "&iLCount=" + iLCount + "&Airline=" + Airline + "&Class=" + SelClass + "&DirectFlight=" + DirectFlight + "&Refundable=" + Refundable + "&" + SegmentData, '', 
		'height=' + winHeight.toString() + ',width=' + winWidth.toString() + ',left=' + winLeft.toString() + ',top=100,resizable=yes,scrollbars=yes');
	}
}
 
function CallOpenPopupforClickTripz() {

	var popupWidth = Math.round((screen.width / 3)) - 10;
    if (document.getElementById('chkcompareexpedia') != null && document.getElementById('chkcompareexpedia').checked) {
        redirectToClickTripz(document.getElementById('hdcompareexpedia').value, "0", popupWidth);
    }
    if (document.getElementById('chkcompareorbitz') != null && document.getElementById('chkcompareorbitz').checked) {
        redirectToClickTripz(document.getElementById('hdcompareorbitz').value, popupWidth + 10, popupWidth);
    }
    if (document.getElementById('chkcomparepriceline') != null && document.getElementById('chkcomparepriceline').checked) {
        redirectToClickTripz(document.getElementById('hdcomparepriceline').value, (popupWidth + 10) * 2, popupWidth);
    }
}

function redirectToClickTripz(advertiserID, left, width) {

    var redirectLink = "http://www.clicktripz.com/click_flight.php?";
    var formObj = document.forms["AirSearchForm"];
    var trip, fromcity, tocity, fromdate, todate, searchType, process, adults, children, cabinType, str, MerchantNames, numTravelers;
    fromcity = formObj.fromCity.value;
    tocity = formObj.toCity.value;
    fromdate = formObj.flyDate_from.value;
    adults = formObj.aCount.value;
    children = formObj.cCount.value;
    iLCount = formObj.iLCount.value;
    iSCount = formObj.iSCount.value;
    numTravelers = parseInt(adults) + parseInt(children) + parseInt(iLCount) + parseInt(iSCount);
    if (formObj.searchType[0].checked == true) {
        trip = 0;
        todate = formObj.flyDate_return.value;
    } else if (formObj.searchType[1].checked == true) {
        trip = 1;
        todate = fromdate;
    }
    str = ("departureDate=" + fromdate);
    str += ("&returnDate=" + todate);
    str += ("&numTravelers=" + numTravelers);
    str += ("&from=" + fromcity.substring(0, 3));
    str += ("&to=" + tocity.substring(0, 3));
    str += ("&publisherID=" + 42);
    str += ("&advertiserID=" + advertiserID);
    str += ("&referralURL=" + encodeURIComponent('http://www.getawayasap.com/index.asp'));
    var popupwin = window.open(redirectLink + str, '', 'height=' + (screen.height - 55).toString() + ',width=' + width + ',left=' + left + ',top=0,scrollbars=yes');
	if (popupwin == null || typeof(popupwin)=='undefined') {   
	  alert('Please disable your pop-up blocker and click the "Search For Flights" button again.'); 
	} 

}

function OpenBookingWizPopUp() {

    var trip, fromcity, tocity, fromdate, todate, searchType, process,adults, children,str,segCount;
    var formObj = document.forms["AirSearchForm"];

 //if (formObj.searchType[2].checked == true) OpenCFarePopUP();
   // else {


   // var redirectLink = "http://www.TravelDealASAP/TEmp2.aspx?";
    var redirectLink = "http://www.TravelDealsASAP.com/Deals_bookingWiz.aspx?";
    fromcity = formObj.fromCity.value;
    tocity = formObj.toCity.value;
    fromdate = formObj.flyDate_from.value;
    
    adults =formObj.aCount.value; 
    children = formObj.cCount.value;
    
    if (formObj.searchType[0].checked == true) {
        trip = 2;
	  todate = formObj.flyDate_return.value;

    }
   else if (formObj.searchType[1].checked == true) {
        trip = 1;
       todate = fromdate;
    }
	else  if (formObj.searchType[2].checked == true) {
        trip = 2;
		segCount = formObj.segCount.value;
		fromdate = formObj["flyDate_" + 1].value;
		todate = formObj["flyDate_" + segCount].value;	      
		fromcity = formObj["fromCity_" + 1].value;        
        tocity = formObj["fromCity_" + 2].value;
    }
    
    str = ("flighttype=" + trip);
    str += ("&city1=" + fromcity);
    str += ("&city2=" + tocity);
    str += ("&date1=" + fromdate);
    str += ("&date2=" + todate);
    str += ("&adults=" + adults);
    str += ("&children=" + children);

    
    var newWin = window.open(redirectLink + str, '', 'height=600,width=800,scrollbars=yes');
    newWin.blur();
    window.focus();
   

//}
}





function OpenCFarePopUP() {

    var str, searchType, site,date,segCount,fromcity,fromcities,tocity,tocities,dates;
    site = 'CF';
    searchType = 'MC';
    fromcities = '';
    dates = '';
    tocities = '';

    var formObj = document.forms["AirSearchForm"];
    segCount = formObj.segCount.value;
    
    for (var i = 1; i <= segCount; i++) {

        date = formObj["flyDate_" + i].value;
        dates += date;
        dates += '|';
        
        fromcity = formObj["fromCity_" + i].value;
        fromcities += fromcity;
        fromcities += '|';

        tocity = formObj["toCity_" + i].value;
        tocities += tocity;
        tocities += '|';
    }

    fromcities = fromcities.substring(0, fromcities.length - 1);
    tocities = tocities.substring(0, tocities.length - 1);
    dates = dates.substring(0, dates.length - 1);      
    str += ("&o1=" + fromcities);
    str += ("&hd_o1=" + fromcities);
    str += ("&d1=" + tocities);
    str += ("&hd_d1=" + tocities);
    str += ("&od1=" + dates);
    str += ("&ttype=" + searchType);
    str += ("&site=" + site);   
    
    var newWin = window.open('http://www.TravelDealsASAP.com/Compare.aspx?' + str);
    newWin.blur();
    window.focus();
    
}

