/*
 * =============================================
 * Author:		<Prakasit Kitrakham>
 * Create date: <Feb 5, 2008>
 * Description:	<Plan Trip>
 * History Modify
 *		[Name]	[Date]: [Description] 
 * =============================================
 */

//var dayOfWeek = {{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}, {"อา.","จ.","อ.","พ.","พฤ.","ศ.","ส."}};

/*******************************************************************************/
PG = function() {}
PG.BIRTS = function() {}

PG.BIRTS.Language = function() {}
PG.BIRTS.Language.equal = function(lang) {
    var local = (navigator["language"])?navigator["language"]:navigator["userLanguage"];
    return (lang.index(local) > -1);
}

PG.BIRTS.Language.indexOf = function(lang) {
   
    switch(lang.toLowerCase()) {
        case "en-us": return 0;
        case "th-th": return 1;
		case "ja-jp": return 2;
		case "cn-cn": return 3;
    }               
}

PG.BIRTS.DateTime = function() {}
PG.BIRTS.DateTime.getLocalDate = function(d, l) {
    if (!PG.BIRTS.Language.equal(l)) {
        if (l == "th-th") {}
    }
}
/*******************************************************************************/
 
Pages_BIRTS_S_PlanTrip = function() {} 

Pages_BIRTS_S_PlanTrip.month = [["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], 
                                ["ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค."],
                                ["1\u6708","2\u6708","3\u6708","4\u6708","5\u6708","6\u6708","7\u6708","8\u6708","9\u6708","10\u6708","11\u6708","12\u6708"],
								["1\u6708","2\u6708","3\u6708","4\u6708","5\u6708","6\u6708","7\u6708","8\u6708","9\u6708","10\u6708","11\u6708","12\u6708"]];
//["มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม"],
//["ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค."],
Pages_BIRTS_S_PlanTrip.dayOfWeek = [["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], 
                                    ["อา.","จ.","อ.","พ.","พฤ.","ศ.","ส."],
                                    ["\u65E5","\u6708","\u706B","\u6C34","\u6728","\u91D1","\u571F"],
									["\u65E5","\u6708","\u706B","\u6C34","\u6728","\u91D1","\u571F"]];


Pages_BIRTS_S_PlanTrip.getLanguageIndex = function(lang) {
   
    switch(lang.toLowerCase()) {
        case "en-us": return 0;
        case "th-th": return 1;
        case "ja-jp": return 2;
		case "cn-cn": return 3;
    }               
}

Pages_BIRTS_S_PlanTrip.convertStrToDate = function(s) {
    var t, d, m, y;        
    
    t = s.split("/");    
    d = parseInt(t[0], 10); m = parseInt(t[1], 10) - 1; y = parseInt(t[2], 10);        
    
   return new Date(y, m, d);

}

Pages_BIRTS_S_PlanTrip.loadDepartureMonthYear = function(t1, t2, t3, t4, s, e, r, d, a, l) {    
    var min = Pages_BIRTS_S_PlanTrip.convertStrToDate(s);       
    var max = Pages_BIRTS_S_PlanTrip.convertStrToDate(e);    
    
    var target = document.getElementById(t1);                
    
    target.options.length = 0;    
    for(var i=new Date(min.getFullYear(), min.getMonth(), min.getDate()); ((i.getFullYear()*100) + i.getMonth()) <= ((max.getFullYear()*100) + max.getMonth()); i.setMonth(i.getMonth()+1)) {    
        var opt = document.createElement("OPTION");                
        opt.value = (i.getMonth()+1) + "," + i.getFullYear();
        opt.text = Pages_BIRTS_S_PlanTrip.month[Pages_BIRTS_S_PlanTrip.getLanguageIndex(l)][i.getMonth()] + " " + i.getFullYear();
        target.options.add(opt);
        if (i.getDate() != 1)
          i.setDate(1);
    }
    
    if (d != null) {
        var dd = Pages_BIRTS_S_PlanTrip.convertStrToDate(d);
        for(var i=0; i<target.options.length; i++) {
            if (target.options[i].value == ((dd.getMonth()+1) + "," + dd.getFullYear())) {        
                target.selectedIndex = i;
                break;
            }
        }
    }      
    
    Pages_BIRTS_S_PlanTrip.loadDepartureDay(t1, t2, t3, t4, s, e, r, d, a, l);
}

Pages_BIRTS_S_PlanTrip.loadDepartureDay = function(t1, t2, t3, t4, s, e, r, d, a, l) {              

    var min = Pages_BIRTS_S_PlanTrip.convertStrToDate(s);               
    var max = Pages_BIRTS_S_PlanTrip.convertStrToDate(e);           
    
    var target1 = document.getElementById(t1);            
    var target2 = document.getElementById(t2);    
        
    var sm = Pages_BIRTS_S_PlanTrip.convertStrToDate("1/" + target1.options[target1.selectedIndex].value.replace(",","/"));            
    var em = new Date(sm.getFullYear(), sm.getMonth(), sm.getDate());
    em.setMonth(em.getMonth()+1);
    em.setDate(em.getDate()-1);

    if (sm<min)
        sm = min;
    if (em>max)
        em = max;
            
    target2.options.length = 0;
    for(var i=new Date(sm.getFullYear(), sm.getMonth(), sm.getDate()); i<=em; i.setDate(i.getDate()+1)) {    
        var opt = document.createElement("OPTION");
        opt.value = i.getDate(); 
        opt.text = Pages_BIRTS_S_PlanTrip.dayOfWeek[Pages_BIRTS_S_PlanTrip.getLanguageIndex(l)][i.getDay()] + " " + i.getDate();
        target2.options.add(opt);        
    }
    
    if (d != null) {
        var dd = Pages_BIRTS_S_PlanTrip.convertStrToDate(d);
        for(var i=0; i<target2.options.length; i++) {
            if (target2.options[i].value == dd.getDate().toString()) {        
                target2.selectedIndex = i;
                break;
            }
        }
    } 
    
    if (t3 != null && t4 != null)
        Pages_BIRTS_S_PlanTrip.loadArrivalMonthYear(t1, t2, t3, t4, e, r, a, l);
}

Pages_BIRTS_S_PlanTrip.loadArrivalMonthYear = function(t1, t2, t3, t4, e, r, a, l) {                
    
    var target1 = document.getElementById(t1);
    var target2 = document.getElementById(t2);
    var target3 = document.getElementById(t3);
        
    try {
        var min = Pages_BIRTS_S_PlanTrip.convertStrToDate(target2.options[target2.selectedIndex].value + "/" + target1.options[target1.selectedIndex].value.replace(",","/"));        
        var max = Pages_BIRTS_S_PlanTrip.convertStrToDate(e);               
                
        var s = new Date(min.getFullYear(), min.getMonth(), min.getDate());      
        s.setDate(s.getDate()+r);        
        
        target3.options.length = 0;
        
        var td = new Date(min.getFullYear(), min.getMonth(), min.getDate());
        td.setDate(td.getDate()+1);
       
        for(var i=td; ((i.getFullYear()*100) + i.getMonth()) <= ((max.getFullYear()*100) + max.getMonth()); i.setMonth(i.getMonth()+1)) {                                   
            var opt = document.createElement("OPTION");                
            opt.value = (i.getMonth()+1) + "," + i.getFullYear();
            opt.text = Pages_BIRTS_S_PlanTrip.month[Pages_BIRTS_S_PlanTrip.getLanguageIndex(l)][i.getMonth()] + " " + i.getFullYear();
            if (i.getMonth() == s.getMonth() && i.getFullYear() == s.getFullYear())
                opt.selected = true;
            target3.options.add(opt);
            if (i.getDate() != 1)
                i.setDate(1);                         
        }
        
        if (a != null) {
            var ad = Pages_BIRTS_S_PlanTrip.convertStrToDate(a);
            for(var i=0; i<target3.options.length; i++) {
                if (target3.options[i].value == ((ad.getMonth()+1) + "," + ad.getFullYear())) {        
                    target3.selectedIndex = i;
                    break;
                }
            }
        }
                        
        Pages_BIRTS_S_PlanTrip.loadArrivalDay(t1, t2, t3, t4, e, r, a, l);
    
    }
    catch(e) {
        //alert(e.Message);
    }
}

Pages_BIRTS_S_PlanTrip.loadArrivalDay = function(t1, t2, t3, t4, e, r, a, l) {        
    var target1 = document.getElementById(t1);            
    var target2 = document.getElementById(t2);    
    var target3 = document.getElementById(t3);
    var target4 = document.getElementById(t4);        
    
    try {
        var min = Pages_BIRTS_S_PlanTrip.convertStrToDate(target2.options[target2.selectedIndex].value + "/" + target1.options[target1.selectedIndex].value.replace(",","/"));        
        var max = Pages_BIRTS_S_PlanTrip.convertStrToDate(e);       
        
        var s = new Date(min.getFullYear(), min.getMonth(), min.getDate());
        s.setDate(s.getDate()+r);                
        
        var sm = Pages_BIRTS_S_PlanTrip.convertStrToDate("1/" + target3.options[target3.selectedIndex].value.replace(",","/"));        
        var em = new Date(sm.getFullYear(), sm.getMonth(), sm.getDate());
        em.setMonth(em.getMonth()+1);
        em.setDate(em.getDate()-1);
        if (sm<min) sm = min;
        if (em>max) em = max;
                
        target4.options.length = 0;
        for(var i=new Date(sm.getFullYear(), sm.getMonth(), sm.getDate()); i<=em; i.setDate(i.getDate()+1)) {            
            var opt = document.createElement("OPTION");
            opt.value = i.getDate(); 
            opt.text = Pages_BIRTS_S_PlanTrip.dayOfWeek[Pages_BIRTS_S_PlanTrip.getLanguageIndex(l)][i.getDay()] + " " + i.getDate();
            if (i.getDate() == s.getDate()) 
                opt.selected = true;
            target4.options.add(opt);                        
        } 
        
        if (a != null) {
            var ad = Pages_BIRTS_S_PlanTrip.convertStrToDate(a);
            for(var i=0; i<target4.options.length; i++) {
                if (target4.options[i].value == ad.getDate().toString()) {        
                    target4.selectedIndex = i;
                    break;
                }
            }
        } 
    }
    catch(e) {
        //alert(e.message);
    }
}

