//home_js.js
//contains all functions that are used primarily on the home page 

 // DQM_cancel_onload = true;
var today = new Date();
var day   = today.getDate();
var month = today.getMonth();
var month1= month;
var year  = today.getFullYear();
var text_field1 = "";
var text_field2 = "";
var text_field3 = "";
var monthArr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var cal1;
var cal2;
var changeRet=true;

function getMonthValue(mon) {
  for (var i=0; i<monthArr.length; i++) {
    if (monthArr[i]==mon.substr(0,3)) {
      break;
    }
  }
  var MonthValue = mon.substr(4,4);
  return (MonthValue/1 * 10 + i);
}

function checkDate() {
   var curMonthVal = monthArr[month] + " " + year;
   var departDate = futureDate(7);
   var returnDate = futureDate(14);
   var departDateMonth = departDate.getMonth();
   var departDateDay = departDate.getDate();
   var returnDateMonth = returnDate.getMonth();
   var returnDateDay = returnDate.getDate();
   var orbotForm = document.orbot;

   if (orbotForm) {
      if (getMonthValue(orbotForm.departMonth.value) < getMonthValue(curMonthVal)) {
         orbotForm.departMonth.selectedIndex = departDateMonth;
         orbotForm.departDay.selectedIndex = departDateDay - 1;
      }
      else if (getMonthValue(orbotForm.departMonth.value) == getMonthValue(curMonthVal)) {
         if (orbotForm.departDay.value < day) {
            orbotForm.departMonth.selectedIndex = departDateMonth;
            orbotForm.departDay.selectedIndex = departDateDay - 1;
         }
      }
      if (getMonthValue(orbotForm.returnMonth.value) < getMonthValue(orbotForm.departMonth.value)) {
         changeReturn();
      }
      else if (getMonthValue(orbotForm.returnMonth.value) == getMonthValue(orbotForm.departMonth.value)) {
         if (orbotForm.returnDay.value/1 < orbotForm.departDay.value/1) {
            changeReturn();
         }
      }
   }
}
function init_cal() {
	this.today = new Date();
	var departDate = futureDate(7);
	var returnDate = futureDate(14);

	var departDateMonth = departDate.getMonth();
	var departDateDay = departDate.getDate();
	var departDateYear = departDate.getFullYear();

	this.link1 = document.getElementById('dateLink1');
	this.link2 = document.getElementById('dateLink2');

	this.departMonth = document.getElementById('departMonth');
	this.departDay = document.getElementById('departDay');

	this.departMonth.selectedIndex = departDateMonth;
	this.departDay.selectedIndex = departDateDay-1;

	var returnDateMonth = returnDate.getMonth();
	var returnDateDay = returnDate.getDate();
	var returnDateYear = returnDate.getFullYear();

	this.returnMonth = document.getElementById('returnMonth');
	this.returnDay = document.getElementById('returnDay');

	this.returnMonth.selectedIndex = returnDateMonth;
	this.returnDay.selectedIndex = returnDateDay-1;

	cal1 = new YAHOO.widget.Calendar2up("cal1","container1",(departDateMonth+1)+"/"+departDateYear,(departDateMonth+1)+"/"+departDateDay+"/"+departDateYear);
	cal1.title = "Departure date:";
	cal1.setChildFunction("onSelect",setDate1);
	for (var i = 0 ; i < cal1.pages.length; i++){
                                cal1.pages[i].minDate=today;
                                cal1.pages[i].maxDate=futureDate(352);
	}
	cal1.render();
	cal2 = new YAHOO.widget.Calendar2up("cal2","container2",(returnDateMonth+1)+"/"+returnDateYear,(returnDateMonth+1)+"/"+returnDateDay+"/"+returnDateYear);
	cal2.title = "Return date:";
	cal2.setChildFunction("onSelect",setDate2);
	for (var i = 0 ; i < cal2.pages.length; i++){
                                cal2.pages[i].minDate=today;
                                cal2.pages[i].maxDate=futureDate(352);
	}
	cal2.render();
	}

function padout(number) { return (number < 10) ? '0' + number : number; }

function restart() {
	text_field1.selectedIndex = day-1;
	text_field2.selectedIndex = month - month1 + (12 * (month < month1));
	if (text_field3 != null)
		text_field3.selectedIndex = text_field2.selectedIndex;
	if(text_field1.name.match(/depart/)) {
		changeReturn();
	}
	mywindow.close();
}

function changeReturn() {
	if (changeRet == true){
		var frm = document.orbot;
		frm.returnMonth.selectedIndex = frm.departMonth.selectedIndex;
		var arrValues = frm.departMonth.value.split(' ');
		var monthValue = arrValues[0];
		var yearValue = year;
		if (arrValues.length == 2) {
			yearValue = parseInt(arrValues[1]);
		}
		var noOfDays = returnNoOfDays(monthValue, yearValue);
		if (parseInt(frm.departDay.selectedIndex) > (noOfDays - 1)) {
    			frm.departDay.selectedIndex = noOfDays - 1;
		}
		var dayValue = parseInt(frm.departDay.selectedIndex);
		var addFactor = 6;
		var index = parseInt(frm.departDay.selectedIndex) + 1 + addFactor;
		if (index >= noOfDays) {
			index = index % noOfDays;
			if (frm.returnMonth.selectedIndex < 11) {
				frm.returnMonth.selectedIndex += 1;
			}
			else if (frm.returnMonth.selectedIndex >= 11) {
				frm.returnMonth.selectedIndex=0;
				index = ((1+addFactor) - (returnNoOfDays(frm.returnMonth.selectedIndex, (yearValue+1)) -(frm.departDay.selectedIndex)));

			}
			else {
				index = returnNoOfDays(monthValue, yearValue) - 1;
			}
  		}
  		frm.returnDay.selectedIndex = index;
  	}
}


function returnNoOfDays(monthValue, yearValue) {
	var noOfDays = 31;
	if ((monthValue=='Apr')||(monthValue=='Jun')||(monthValue=='Sep')||(monthValue=='Nov')) {
		noOfDays = 30;
	}
	else if (monthValue=='Feb') {
		if ((yearValue % 4) > 0) {
			noOfDays = 28;
		}
		else {
			noOfDays = 29;
		}
	}
	return noOfDays;
}

function moreOptions(t) {
	if(t=="rev"){
		document.orbot.searchMethod[1].checked = true;
		document.orbot.searchMethod[0].checked = false;
	}
	document.orbot.moreOptions.value=true;
	document.orbot.multiflight.value=false;
	document.orbot.submit();
}

function multiFlight(t) {
	if(t=="rev"){
		document.orbot.searchMethod[0].checked = false;
		document.orbot.searchMethod[1].checked = true;
		document.orbot.searchType[0].checked = true;
	}
	document.orbot.multiflight.value=true;
	document.orbot.moreOptions.value=true;
	document.orbot.submit();
}


function lessOptions() {
	document.orbot.moreOptions.value=false;
	document.orbot.multiflight.value=false;
	document.orbot.submit();
}



function stringFilter (string) {
	sfilter = string;
	filteredValues = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var i;
	var returnString = "";
	for (i = 0; i < sfilter.length; i++) {
		var c = sfilter.charAt(i);
		if (filteredValues.indexOf(c) != -1) returnString += c;
	}
	return returnString;
}


function cityCodeFix (string) {
   sfilter = string.toUpperCase();
   if (sfilter == 'DET') { sfilter = 'DTW'; }
   if (sfilter == 'DWT') { sfilter = 'DTW'; }
   if (sfilter == 'MPL') { sfilter = 'MSP'; }
   if (sfilter == 'MPS') { sfilter = 'MSP'; }
   if (sfilter == 'ORL') { sfilter = 'MCO'; }
   if (sfilter == 'KCI') { sfilter = 'MCI'; }
   if (sfilter == 'TOK') { sfilter = 'NRT'; }
   if (sfilter == 'NAR') { sfilter = 'NRT'; }
   return sfilter;
}

function get_cookie(name) {
	var cname = name + "=";
	var allcookies = document.cookie;
	var pos = allcookies.indexOf(cname);
	if (pos != -1) {
		var start = pos + cname.length;
		var end = allcookies.indexOf(";", start);
		if (end == -1) end = allcookies.length;
			return(unescape(allcookies.substring(start,end)));
	}
	return("");
}


function changeSearchBy(type){
   document.orbot.flexSearchType.value = ""; // default
	if ((type=="price") && (document.orbot.searchMethod[1].checked==true)){
		document.orbot.searchMethod[0].checked = true;
	}
	else if ((type=="sched") && (document.orbot.mileageUpgradeType.checked==true)){
		alert("Mileage Upgrade Searches can not be preformed with a Schedule Search");
		document.orbot.mileageUpgradeType.checked = false;
	}
	else if ((type=="weekend") ){
		document.orbot.flexSearchType.value = "flexweekend";
		document.orbot.multiflight.value=false;
		document.orbot.searchMethod[1].checked = true;
		document.orbot.submit();
	}
	else if ((type=="rev")){
		document.orbot.submit();
	}
}


function futureDate(daysOut){
	var today = new Date();
	var todayInMS = today.getTime(); // today in MilliSeconds
	var nextDate = todayInMS + (60 * 60 * 24 * daysOut * 1000);
	return new Date(nextDate);
}

function showCalendar1() {
        cal2.hide();
        var cont1 = document.getElementById('container1')
       if (IE){
        cont1.style.top = (link1.offsetTop+400) + "px";
        cont1.style.left = (link1.offsetLeft+260) + "px";
       } else{
        cont1.style.top = (link1.offsetTop+40) + "px";
        cont1.style.left = (link1.offsetLeft+270) + "px";
       
       }
        cal1.outerContainer.style.display='inline';
}

function showCalendar2() {
        cal1.hide();
        var cont2 = document.getElementById('container2')
        if (IE){
			cont2.style.top = (link1.offsetTop+430) + "px";
			cont2.style.left = (link1.offsetLeft+260) + "px";
       	} else{
			cont2.style.top = (link1.offsetTop+60) + "px";
			cont2.style.left = (link1.offsetLeft+270) + "px";
       	}
        cal2.outerContainer.style.display='inline';
}

function setDate1() {
	var date1 = cal1.getSelectedDates()[0];
	departMonth.selectedIndex=date1.getMonth();
	departDay.selectedIndex=date1.getDate()-1;
	cal1.hide();
	changeReturn();
}

function setDate2() {
	var date2 = cal2.getSelectedDates()[0];
	returnMonth.selectedIndex=date2.getMonth();
	returnDay.selectedIndex=date2.getDate()-1;
	cal2.hide();
	//showTimeSelector();
}

function changeDate1() {
	var monthValues = document.orbot.departMonth.value.split(' ');
	var month = this.departMonth.selectedIndex;
	var day = this.departDay.selectedIndex ;
	var year= parseInt(monthValues[1]);
	cal1.setYear(year);
	cal1.setMonth(month);
	cal1.select((month+1) + "/" + (day+1) + "/" + year);
	cal1.render();
}

function changeDate2() {
	var month = this.returnMonth.selectedIndex;
	var day = this.returnDay.selectedIndex ;
	var monthValues = document.orbot.returnMonth.value.split(' ');//example: Jan 2007
	var year= parseInt(monthValues[1]);
	cal2.setYear(year);
	cal2.setMonth(month);
	cal2.select((month+1) + "/" + (day+1) + "/" + year);
	for (var i = 0 ; i < cal2.pages.length; i++){
		if (cal1.getSelectedDates()[0] < today.getDate){	
			cal2.pages[i].minDate=today.getDate();
		}
		else{
			cal2.pages[i].minDate=cal1.getSelectedDates()[0];
		}
		cal2.pages[i].maxDate=futureDate(352);
	}
	cal2.render();
}


function quickReset() {
	cal1.hide();
	cal2.hide();
}

function readPrefCookie() {

    /* Checks if user is logged in */
    if (!(get_cookie("SkyMiles"))) {
        return;
    }

    var cookPairs= new Array;
    var skipDepart= false;
    var skipReturn= false;
	var skipDepartDay= false;
    var skipReturnDay= false;
    var goodDates= true;
    var ca = document.cookie.split(';');
    var checkCheckInMMR = 0;
	var currentDate = futureDate(0);
	var currentDay = currentDate.getDate();

    //loop through all forms on page
    for (var h=0;h < document.forms.length; h++) {
		// loop only if it is check-in or MMR box
		if (checkCheckInMMR == 3) {
			break;
		}
        var fName =  document.forms[h].name;
        if (fName == "form0" || fName == "form1" || fName == "orbot") {
			checkCheckInMMR++;
            // loop through cookies if this cookie name is the same as the form name
            for (var i=0 ; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length)
                {
                    if (c.indexOf(fName) == 0)
                    {
                        cookPairs =  c.substring(fName.length,c.length).split('&');
                    }
                }
                // loop through cookie Param/Value pairs
                for (var j=0;j < cookPairs.length; j++) {
                    cookParams = new Array;
                    cookParams = cookPairs[j].split('||');
                    // compare the Form element name, to the Param Name in our cookie
                    // and set the Form element Value = Cookie Param Value
                    for (var k=0; k<document.forms[h].elements.length; k++) {
                        if (cookParams[0] ==  document.forms[h].elements[k].name) {
                            if (cookParams[1] != "") {
                                switch (document.forms[h].elements[k].type) {
                                    case "checkbox":
                                        if (cookParams[1]=="true")
                                        document.forms[h].elements[k].checked =  cookParams[1];
                                        break;
                                    case "radio":
                                        if (cookParams[1]==document.forms[h].elements[k].value) {
                                            document.forms[h].elements[k].checked =  true;
                                        }
                                        break;
                                    case "select-one":
                                        switch (cookParams[0]) {
                                        case "departMonth":
                                            if (getMonthValue(cookParams[1]) <  getMonthValue(monthArr[month] + " " + year)){
                                                skipDepart=true;
                                            }
                                            break;
                                        case "departDay":
                                            break;
                                        case "returnMonth":
                                            if (getMonthValue(cookParams[1]) <  getMonthValue(monthArr[month] + " " + year)) {
                                                skipReturn=true;
                                            }
                                            break;
                                        case "returnDay":
                                            break;
                                        default :
                                            if (cookParams[0] == "checkInType" || cookParams[0] == "viewResType") {
                                                document.forms[h].elements[k].selectedIndex =  cookParams[1];
                                            }
                                            else {
                                                document.forms[h].elements[k].value =  cookParams[1];
                                            }
                                            break;
                                        }
                                    default :
                                        if (((cookParams[0] == "departMonth") || (cookParams[0] == "departDay")) && (skipDepart == true) )
										{
                                            break;
                                        }
                                        else if (((cookParams[0] == "returnMonth") || (cookParams[0] == "returnDay")) && (skipReturn == true))
                                        {
                                            break;
                                        }
                                        else if (cookParams[0] == "checkInType" || cookParams[0] == "viewResType") {
                                            break;
                                        }
                                        else if (!cookParams[1]) {
                                            break;
                                        }
                                        else {
                                            document.forms[h].elements[k].value = cookParams[1];
                                        }
                                        break;
                                }//switch
                            }//if
                        }//if
                    }//for
                }//for
            }//for
        }//if
    }//for
	/* If WP # is not selected, reset MMR and check-in */
    checkDate();
    document.forms.form0.RL.value = "";
    document.forms.form0.ET.value = "";
    setCheckInType();
    setViewResType();
    checkReturn();
}//function

function setPrefCookie(theForm) {

    /* Checks if user is logged in */
    if (!(get_cookie("SkyMiles"))) {
        return;
    }

	/* Refresh cookie first */
	remove_cookie(theForm.name);

	var cookVal =  theForm.name +"=";
	var exdate=new Date()
	exdate.setTime(exdate.getTime()+(150*24*60*60*1000));
	var expires = "; expires="+exdate.toGMTString();
	expires += "; path=/; domain=.nwa.com;";
	for(i=0; i<theForm.elements.length; i++){
		switch (theForm.elements[i].type)
		{
			case "hidden":
				break;
			case "text" :
				if (theForm.elements[i].name != "WP" && theForm.elements[i].name != "RL" &&
					theForm.elements[i].name != "ET" && theForm.elements[i].name != "Pnr" &&
					theForm.elements[i].name != "WpNum" && theForm.elements[i].name != "EtktNum")
				{
					cookVal +=  '&' + theForm.elements[i].name + '||' +  theForm.elements[i].value;
				}
				break;
			case "textarea":
				cookVal += '&' + theForm.elements[i].name + '||' +  theForm.elements[i].value;
				break;
			case "checkbox":
				cookVal += '&' + theForm.elements[i].name + '||' +  theForm.elements[i].checked;
				break;
			case "select-one":
				if (theForm.elements[i].name == "checkInType" || theForm.elements[i].name == "viewResType") {
					cookVal += '&' + theForm.elements[i].name + '||' + theForm.elements[i].selectedIndex;
				} else {
					cookVal += '&' + theForm.elements[i].name + '||' + theForm.elements[i].options[theForm.elements[i].selectedIndex].value ;
				}
				break;
			case "radio":
				if (theForm.elements[i].checked == true)
				{
					cookVal += '&' + theForm.elements[i].name + '||' + theForm.elements[i].value;
				}
				else{
					//nothing
					break;
				}
				break;
			default:
				break;
		}//switch
	}//for
	document.cookie= cookVal +  expires;
}//setPrefCookie

function checkReturn(){
    if (document.orbot){
		if (document.orbot.searchType[0].checked || document.orbot.searchType[0].type == "hidden")
		{
			document.orbot.returnMonth.disabled=false;
			document.orbot.returnDay.disabled=false;
			document.orbot.returnTime.disabled=false;
		}
		else{
			document.orbot.returnMonth.disabled=true;
			document.orbot.returnDay.disabled=true;
			document.orbot.returnTime.disabled=true;
		}
    }
}
