var months=[31,28,31,30,31,30,31,31,30,31,30,31,29]

function advOpt(ind){
    document.getElementById('opt_1').style.display="none"
    var optSel1=document.getElementById('opt_1').getElementsByTagName("select")
    for(i=0;i<optSel1.length;i++){
        optSel1[i].value=0
    }
    document.getElementById('opt_0').style.display="none"
    var optSel0=document.getElementById('opt_0').getElementsByTagName("select")
    for(i=0;i<optSel0.length;i++){
        if(i==0){
            optSel0[i].value="";
        }else{
        optSel0[i].value=0;
    }
}
if(ydom.inDocument("opt_"+prType[ind])){
    document.getElementById("opt_"+prType[ind]).style.display="block"
}
}

ye.onContentReady("srBody",initData);

function initData(){
    var setCountry=function(e,o){
        var dstn=ydom.get("destination");
        dstn.length=1
        
        otInd=1;
        for(i=0;o.a[i];i++){
            if(o.a[i].cnt==o.elm.value){
                dstn.options[otInd]=new Option(o.a[i].city,o.a[i].city)
                otInd=otInd+1;
            }
        }
    }
    function checkDate(d,my){
        return ;
    }
    var changeDate=function(e,o){
        var currDate=new Date();
        var elm_name=o.elm.id;
        prefx=elm_name.substring(0,2);
        var ch_dt=true;
            if(o.fd.value!=0 && o.fm.value!=0){
                sp_vl=(o.fm.value).split("/");
                var fr_dt=new Date(sp_vl[1],sp_vl[0]-1,o.fd.value);
            }else{
                if(o.td.value!=0 && o.tm.value!=0){
                    sp_tvl=(o.tm.value).split("/");
                     var tr_dt=new Date(sp_tvl[1],sp_tvl[0]-1,o.td.value);
                     if(tr_dt<=currDate){
                        ch_dt=false;
                        alert("The Date you selected is past date.\nPlease Select a Date after "+currDate)
                        o.td.value=0;
                        o.tm.value=0;
                        }
                }
            }

        cYear=currDate.getFullYear();
        if(ch_dt){
            sp_vl=(o.fm.value).split("/");
        if(o.elm.id=="fromMonthYear"){
            ind=o.elm.selectedIndex;
            var curntMnt=o.tm.value;
            o.tm.options.length=0;
            for(i=0;i<o.elm.length-ind;i++){
                o.tm.options[i]=new Option(o.elm.options[ind+i].text,o.elm.options[ind+i].value)
            }
            td=ydom.get("toDDate");
            var crDay=td.value;
            d=(o.tm.options[0].value).split("/");
            td.options.length=0;
            if(parseInt(d[1])%4==0 && d[0]==2){
                m=months[12]
            }else{
                m=months[d[0]-1]
            }
            if((currDate.getMonth()+1)==d[0] && (currDate.getYear()==d[1])){
                var f=0;
                for(i=currDate.getDate()-1;i<m;i++){
                    el.options[f]=new Option(i+1,i+1);
                    f=f+1;
                }
                if(selcDay>currDate.getDate()){
                    el.value=selcDay
                }
            }else{
                for(i=0;i<m;i++){
                    td.options[i]=new Option(i+1,i+1);
                }
                if(td.options.length>=crDay){
                    td.value=crDay;
                }      
            }
        }
        if(o.elm.id.indexOf("Month")!=-1){
            var el;
            if(o.elm.id.indexOf("from")!=-1){
                el=o.fd;
            }else{
                el=o.td;
            }
            d=(o.elm.value).split("/");
            
            if(parseInt(d[1])%4==0 && d[0]==2){
                m=months[12];
            }else{
                m=months[d[0]-1];
            }
            if(o.elm.id.indexOf("from")!=-1 && (currDate.getMonth()+1)==parseInt(d[0]) && (currDate.getFullYear()==parseInt(d[1]))){
                var f=0;
                var selcDay=el.value;
                el.options.length=0;
                for(i=currDate.getDate()-1;i<m;i++){
                    el.options[f]=new Option(i+1,i+1);
                    f=f+1;
                }
                if(selcDay>currDate.getDate()){
                    el.value=selcDay
                }
            }else{
                    var selcDay=el.value;
                    el.options.length=0;
                    for(i=0;i<m;i++){
                        el.options[i]=new Option(i+1,i+1);
                    }
                    if(el.options.length>=selcDay){
                    el.value=selcDay
                    }
            }
          }
            if(o.elm.id.indexOf('fromDDate')!=-1){
                var cr_vl=parseInt(o.elm.value)+parseInt(minDays)
                d=(o.fm.value).split("/");var m=months[d[0]-1];
                if(parseInt(d[1])%4==0 && d[0]==2){m=months[12];}
                if(parseInt(o.elm.value)>parseInt(o.td.value)){
                    if(o.elm.value==m){
                        tc_mn=parseInt(d[0])+1;
                        tc_yr=parseInt(d[1]);
                        tm_stind=parseInt(o.fm.selectedIndex)+1
                        if(o.fm.length>tm_stind){
                            o.tm.length=0;
                            var op_ind=0;
                            for(i=tm_stind;o.fm.length>i;i++){
                                o.tm.options[op_ind]=new Option(o.fm.options[i].text,o.fm.options[i].value);
                                op_ind++;
                            }
                            ds=months[d[0]];
                            o.td.length;
                            for(i=1;ds>=i;i++){
                                o.td.options[i-1]=new Option(i,i);
                            }
                            o.td.value=cr_vl-ds;
                        }
                    }else{
                      if(cr_vl>=m){
                        tc_mn=parseInt(d[0])+1;
                        tc_yr=parseInt(d[1]);
                        tm_stind=parseInt(o.fm.selectedIndex)+1
                        if(o.fm.length>tm_stind){
                            o.tm.length=0;
                            var op_ind=0;
                            for(i=tm_stind;o.fm.length>i;i++){
                                o.tm.options[op_ind]=new Option(o.fm.options[i].text,o.fm.options[i].value);
                                op_ind++;
                            }
                            ds=months[d[0]];
                            o.td.length;
                            for(i=1;ds>=i;i++){
                                o.td.options[i-1]=new Option(i,i);
                            }
                            o.td.value=cr_vl-ds;
                        }
                        }else{
                            o.td.value=cr_vl;
                        }
                    }
                }   
            }
        }
        }
    var plc=eval(ydom.get('cntpl').innerHTML);
    var frDay=ydom.get("fromDDate");
    var toDay=ydom.get("toDDate");
    var frMonth=ydom.get("fromMonthYear");
    var toMonth=ydom.get("toMonthYear");
    ye.addListener("isoCountry","change",setCountry,{a:plc,elm:ydom.get('isoCountry')});
    ye.addListener("fromDDate","change",changeDate,{elm:frDay,fd:frDay,td:toDay,fm:frMonth,tm:toMonth});
    ye.addListener("toDDate","change",changeDate,{elm:toDay,fd:frDay,td:toDay,fm:frMonth,tm:toMonth});
    ye.addListener("fromMonthYear","change",changeDate,{elm:frMonth,fd:frDay,td:toDay,fm:frMonth,tm:toMonth});
    ye.addListener("toMonthYear","change",changeDate,{elm:toMonth,fd:frDay,td:toDay,fm:frMonth,tm:toMonth});
    
}

function setDates(){
    var currDate=new Date();
    cYear=currDate.getFullYear();
    cMonth=currDate.getMonth();
    var dfsel=ydom.get("fromDDate");
    var dtsel=ydom.get("toDDate");
    var f=0;
    var strDay=currDate.getDate()+1;
    var scStrDay=minDays+strDay;

    if(strDay>months[cMonth]){
        cMonth=cMonth+1;
        strDay=strDay-currDate.getDate();
        scStrDay=minDays+strDay;
        if(cMonth>11){
            cMonth=0;
            cYear=cYear+1;
        }
    }
    for(i=1;i<=months[cMonth];i++){
         if(i>=strDay){
            dfsel.options[f]=new Option(i,i);
            if(f>0){
                if(i>=scStrDay){
                    dtsel.options[f-minDays]=new Option(i,i);
                }
            }
            f=f+1;
        }
    }
    nYear=cYear;
    nMonth=cMonth;
    if(scStrDay>months[cMonth]){
        f=0;
        for(i=1;i<=months[cMonth+1];i++){
            if(i>=scStrDay-months[cMonth]){
                    dtsel.options[f]=new Option(i,i);
                    f=f+1;
                }
        }
        nYear=cYear;
        nMonth=cMonth+1;
        if(nMonth>11){
        nMonth=0;
        nYear=nYear+1;
        }
    }
    
    var dfmysel=ydom.get("fromMonthYear");
    var dtmysel=ydom.get("toMonthYear");
    var sYear=cYear;
    var sMonth=cMonth
    for(i=0;i<=12;i++){
        dfmysel.options[i]=new Option(mNames[sMonth]+" "+sYear,(sMonth+1)+"/"+sYear);
        dtmysel.options[i]=new Option(mNames[nMonth]+" "+nYear,(nMonth+1)+"/"+nYear);
        if(sMonth>11){sMonth=0;}
        var cNew=sYear*12+sMonth+1;
        var cewDate=new Date((Math.round((cNew/12)-0.5)),(cNew%12),1);
        sMonth=cewDate.getMonth();
        sYear=cewDate.getFullYear();
        var yNew=nYear*12+nMonth+1;
        var newDate=new Date((Math.round((yNew/12)-0.5)),(yNew%12),1);
        nYear=newDate.getFullYear();
        nMonth=newDate.getMonth();
    }
    dfsel.value=strDay;
    dfmysel.value=(cMonth+1)+'/'+cYear;
}

 $(function(){
    $("#search_box_header").corner("round top");
    if(!$.browser.msie){
        $("#search_box_content").wrap("<div class='src_btm_outer'></div>")
        $("#search_box_content").corner("round bottom").parent().corner("round bottom");
    }
    
    setDates();
    $("#find_prop").click(function(e){
        try{e.preventDefault()}catch(err){}
        var msg="";
        var dfsel=ydom.get("fromDDate").value;
        var dtsel=ydom.get("toDDate").value;
        var dfmysel=ydom.get("fromMonthYear").value;
        var dtmysel=ydom.get("toMonthYear").value;
        if(dfsel!=0 && dtsel!=0 && dfmysel!=0 && dtmysel!=0){
            frStr=dfmysel.split("/");
            tStr=dtmysel.split("/");
            fr_dt=frStr[0]+"/"+dfsel+"/"+frStr[1];
            tr_dt=tStr[0]+"/"+dtsel+"/"+tStr[1];
            frDate=new Date(frStr[1],frStr[0],dfsel)
            toDate=new Date(tStr[1],tStr[0],dtsel)
            if(frDate<toDate){
                flag=true;
            }else{
                flag=false;msg=errMsg
                }
            ydom.get("fromDate").value=fr_dt;
            ydom.get("toDate").value=tr_dt;

        }
        var frId=ydom.get("search");
        if(flag){
            frId.submit();
            $("#src_msghld").show()
        }else{
            alert(msg)
        }
    })
})
