﻿//var aspChildID='dnn_ctr678_Dispatch_Accommodation_CHILDREN';
//var aspAdultID='dnn_ctr678_Dispatch_Accommodation_ADULTS';
//var CHILDAGETABLEID = "tblMain";

var aspChildID='';
var aspAdultID='';
var CHILDAGETABLEID = '';



function setGlobalVariable(tableName,adult,child)
{
    aspChildID=child;
    aspAdultID=adult;
    CHILDAGETABLEID = tableName;
}

var browserName = window.navigator.appName;
function fnErrroDisplay(clid)
{
    txtCheckIN = document.getElementById(clid + "_txtCheckInDate").value;
    txtCheckOUT = document.getElementById(clid + "_txtCheckOutDate").value;
    cont = document.getElementById(clid + "_COUNTRY").value;
    loc = document.getElementById(clid + "_LOCATION").value;
    if(txtCheckIN == "")
    {
        document.getElementById(clid + "_checkinError").innerHTML = "please specify the check in date.";
        return false;
        
    }
    if(txtCheckOUT == "")
    {
        document.getElementById(clid + "_checkoutError").innerHTML = "please specify the check out date.";
        return false;
    }
    if(cont == "" || cont == "0")
    {
        document.getElementById(clid + "_Td1").innerHTML = "please specify the country.";
        return false;
    }
//     if(loc == "" || loc == "0")
//    {
//        document.getElementById(clid + "_Td2").innerHTML = "please specify the location.";
//        return false;
//    }
    
}
function setTName(cntType,roomNo,index)
{
    //For Accommodation Start
    if(cntType=='adult')
    {
        return 'R'+roomNo+'Adult';
    }
    else if(cntType=='childCount')
    {
        return 'R'+roomNo+'Child';
    }
    else if(cntType=='childAge')
    {
        return 'R'+index+'Child'+ index +'Age';
    }
    //For Accommodation End
}

function halert(s)
{
    //alert(s);
    //var obj = document.getElementById("debug");
    //obj.innerHTML += s + "</br>";
}
function hclear()
{
    //alert(s);
    //var obj = document.getElementById("debug");
    //obj.innerHTML ='';
}
function isEmptyValue(cnt)
{   
    tstrInput=cnt.value;
    tstrInput.replace(/^ *| *$/g,"");
    
    if(tstrInput=='')
    {
        return true;
    }
    return false;
}

function checkKeyValue(ev)
{
    var keyPressValue
    
    if(ev.which == undefined)
    {
        keyPressValue = ev.keyCode
    }
    else
    {
        keyPressValue = ev.which
    }
 
    if(keyPressValue >=48 && keyPressValue<=57){}
    else if(keyPressValue==0 || keyPressValue==8 || keyPressValue==13 ){}
    else{ return false }
    
    return true;
}

/* Accommodation Start */


function calculateCheckoutDate(checkinid,checkoutid,nightsid)
{
    var checkin = document.getElementById(checkinid)
    var checkout = document.getElementById(checkoutid)
    var nights = document.getElementById(nightsid)
    //alert(nights.value);
    var match = new RegExp(/^\d+$/).exec(nights.value);
    //alert(match);
    //if(match==null){ return false};
       
    var date2 = new Date(Date.parseDate(checkin.value,'%m/%d/%Y'))    
    var e = eval(24 * nights.value)    
    date2.setHours(e)
    checkout.value =  date2.print('%m/%d/%Y')
}

function fillDropdown(cnt,startIndex,EndIndex)
{
    var i = startIndex;
    cnt.options.length = parseInt(EndIndex-startIndex)+1;
    for (var k=0;k<cnt.options.length; k++ )
    {
        cnt.options[k].value = i;
        cnt.options[k].text = i;
        i = i + 1;
    }
}

function displyRoom(cnt,mAdultscnt,mChildrencnt)
{
    var mAdults = document.getElementById(mAdultscnt);
    var mChildren = document.getElementById(mChildrencnt);
    var aspChild= document.getElementById(aspChildID);
    var aspAdult= document.getElementById(aspAdultID);
    var dc = document.getElementById('divcontainer');
    
    var room = parseInt(cnt.value);
    
    if(dc.childNodes.length > room)
    {   
        if(room==1)
        {
            mAdults.style.display ='block';
            mChildren.style.display ='block';
            
            aspAdult.setAttribute('tName',setTName('childCount',1,1));
            aspChild.setAttribute('tName',setTName('adult',1,1));
        }
        else
        {
            room = room + 1
        }
        var len=dc.childNodes.length;
        
        
        if(browserName != 'Netscape')
        {
            len=len+1;
        }
        var i=0;
        for(i=len-1;i>=room;i--)
        {
             //alert('remove' + i);
            var span = document.getElementById('span' + i);
            dc.removeChild(span);
            if(getTable()!=null)
           {
                 if(i==1)
                 {
                     if(parseInt(document.getElementById(aspChildID).value)<1)
                     {
                           removeChilAgeRow(getTable(),i,0);
                     }
                }
               else
                    removeChilAgeRow(getTable(),i,0);
          }  
        } 
    }    
    else
    {
        if(room<2) return;
        mAdults.style.display ='none';
        mChildren.style.display ='none';
        
        if(aspAdult!=null)
            aspAdult.removeAttribute('tName');
        if(aspChild!=null)
            aspChild.removeAttribute('tName');
        
        var len = dc.childNodes.length;
        
        
        if(browserName != 'Netscape')
        {
            len=len+1;
        }
        
        for(i=len;i<=room;i++)
        {            
            dc.appendChild(newRow(i));
             if(i==1)
            {
                copyValue('aspcnt',0);
            }
        } 
    }
}

function newRow(index)
{
    var span = document.createElement('SPAN');
    span.id="span"+index;
    var oAdults,oChild,oRoom,oBr,childSpan;
    
    oRoom = document.createElement('SPAN');
    oRoom.id='room'+index;
    oRoom.name='room'+index;
    oRoom.innerHTML="Room " + index;
    oRoom.className ='normal';
    
    oAdults = document.createElement('SELECT');
    oAdults.id='adults'+index;
    oAdults.name='adults'+index;
    oAdults.className='combo-box'
    //oAdults.setAttribute("tName","NoOfAdult" + index);
    oAdults.setAttribute("tName",setTName('adult',index,-1));
    fillDropdown(oAdults,1,10);
    
    oChild = document.createElement('SELECT');    
    oChild.id='child'+index;
    oChild.name='child'+index;
    oChild.className='combo-box';
    //oChild.setAttribute("tName","NoOfChild" + index);
    oChild.setAttribute('tName',setTName('childCount',index,-1));

    fillDropdown(oChild,0,9);
    
    if(browserName != 'Netscape')
    {
        oChild.attachEvent('onchange',displayChild);
        oChild.attachEvent('onkeyup',displayChild);
    }
    else
    {
        oChild.setAttribute('onchange','displayChild(this);');
        oChild.setAttribute('onkeyup','displayChild(this);');
    }
        
    oBr= document.createElement('BR');
    
    span.appendChild(oRoom);
    span.appendChild(oAdults);
    span.appendChild(oChild);
    
    span.appendChild(oBr);
    
    return span;
}

function getTable()
{
    var tbl = document.getElementById(CHILDAGETABLEID);
    return tbl;
}

function createTable(cntName)
{   
    var tbl = document.getElementById(cntName);
    if(tbl==null)
    {
        tbl= document.createElement("TABLE");
        tbl.id=cntName;
        tbl.name=tbl.id;
        tbl.setAttribute("border",0);
        tbl.setAttribute("with",'100%');
        tbl.setAttribute("cellspacing",'1');
        tbl.setAttribute("cellspadding",'0');
    }
    return tbl;
}

function createChildAgeRow(oMainTable,RoomNo,rowIndex,position)
{
    var parentChildCnt = document.getElementById(oMainTable.id+'Room'+RoomNo+'Row1');
    
    if(document.getElementById(oMainTable.id+'Room'+RoomNo+'Row'+rowIndex)!=null) {return true;}
    
    var newRow = oMainTable.insertRow(position);
    newRow.id = oMainTable.id+'Room'+RoomNo+'Row'+rowIndex;
    newRow.name = newRow.id;
       
    var startIndex=1;
    var endIndex=4;
    var newCell;
    //if(parentChildCnt==null)
    //{   
        
        var newCell = newRow.insertCell(0);
        if(rowIndex==1)
        newCell.innerHTML= 'Room '+RoomNo;
        newCell.className ='normal';
        newCell.setAttribute('nowrap','nowrap');
        newCell.setAttribute('rmIndex',RoomNo);
    //}
//    else
//    {
//        alert('Set Attribut: ' + rowIndex);
//        parentChildCnt.cells[0].setAttribute('rowspan',rowIndex);
//        alert('Seted Attribut: ' + parentChildCnt.cells[0].getAttribute('rowspan'));
//        startIndex=0;
//        endIndex=3;
//    }
    for(var i=startIndex;i<endIndex;i++)
    {
        newCell = newRow.insertCell(i);
        newCell.className ='normal';
        newCell.id = oMainTable.id+'Room'+RoomNo+'Col'+i;
        newCell.name = newCell.id;
        
        
    }
    showDetail();
}

function showDetail()
{
    //document.getElementById('tableContent').value = getTable().innerHTML;
}

function copyValue(cntevent,val)
{   

    var aspCnt= document.getElementById(aspChildID);
    var jspCnt = document.getElementById('child1');
        
    //jspCnt.attributes[0]
    if(cntevent=='aspcnt')
    {   
        try{
            //jspCnt.value = document.getElementById('CHILDREN').value;
            jspCnt.value = aspCnt.value;
        }catch(e){}
    }
    else
    {
        aspCnt.value = val;
    }
}


function removeChilAgeRow(oMainTable,RoomNo,newChildSelected)
{
    var oldTableRowCount = oMainTable.rows.length;
     var k=7;
    for(j=3;j>0;j--)
    {
            if(newChildSelected<k)
            {
                for(i=oMainTable.rows.length-1;i>=0;i--)
               {
                    if(oMainTable.rows[i].id == oMainTable.id+'Room'+RoomNo+'Row'+j)
                    {
                        if(k!=1)
                         {
                            var parentChildCnt = document.getElementById(oMainTable.id+'Room'+RoomNo+'Row1');
                            var rowspanVal = parentChildCnt.cells[0].getAttribute('rowspan');
                            rowspanVal=rowspanVal-1;
                           parentChildCnt.cells[0].setAttribute('rowspan',rowspanVal);
                           }
                            oMainTable.deleteRow(i);
                           
                   }    
                }
            }
          k=k-3;
    }
    
    var contentTextCtn = document.getElementById('divChildAgeContainerText');
    if(oMainTable.rows.length<1)
    {
        contentTextCtn.style.display='none';
    }
    else
    {
        contentTextCtn.style.display='block';        
    }
//    
//    return (oMainTable.rows.length!=oldTableRowCount);       
}

//Return Row Current Position when new Room need to create
function findRowInsertPosition(oMainTable,RoomNo)
{
    //return 0;
    var roomIndex=oMainTable.rows.length; 
    var i=0;
    var n=0;
    if(oMainTable.rows.length==0) return 0;
    //debugger;
    for(i=oMainTable.rows.length-1;i>=0;i--)
    { 
            halert('V: ' + oMainTable.rows[i].cells[0].getAttribute('rmIndex'));
            n = parseInt(oMainTable.rows[i].cells[0].getAttribute('rmIndex'));  
            halert('['+n +'<='+RoomNo+']' + i);
            if(n>=RoomNo)
            {
                roomIndex = i;
                halert('New Position ' +  roomIndex);
            }      
    }
    halert('Ret : ' +  roomIndex);
    return roomIndex;
}

//Return subChild Table
function addChildAgeCombo(parId,RoomNo,index)
{
    var subTable = createTable(parId+'dd'+index);
    
    var subRow = subTable.insertRow(0);
    var subCell = subRow.insertCell(0);
    subCell.innerHTML="Child " + index+':';
    subCell.className ='normal';
    
    subRow = subTable.insertRow(1);
    subCell = subRow.insertCell(0);
    
    var oChildAge = document.createElement('INPUT');
    oChildAge.id='oChildAge'+index;
    oChildAge.name=oChildAge.id;
    oChildAge.className='combo-box';
    oChildAge.setAttribute('size','4');
    //set Tname Attribute
    oChildAge.setAttribute('tName',setTName('childAge',RoomNo,index));
    
    //fillDropdown(oChildAge,0,9);
    subCell.appendChild(oChildAge);
    
    return subTable;
}

//Generate Row in Table For ChildAge Combo
function displayChild(cnt)
{  
  
  if(browserName != 'Netscape')
  {
    cnt = event.srcElement;
  }
  
  var dc = document.getElementById('divChildAgeContainer');
    
  var oMainTable = document.getElementById(CHILDAGETABLEID);
    
    //Create Table
    if (oMainTable == null)
    {
        //halert('New Table Created');
        oMainTable =  createTable('tblMain');
        dc.appendChild(oMainTable);
    }
    
     var NoOfChildren = parseInt(cnt.value);
    var RoomNo ='';
    if(cnt.getAttribute('jsid')!=null)
    {
        RoomNo = parseInt(cnt.getAttribute('jsid').substring('child'.length,cnt.getAttribute('jsid').length));    
        //copyValue('aspcnt',NoOfChildren);
    }
    else
    {
        RoomNo = parseInt(cnt.id.substring('child'.length,cnt.id.length));    
        copyValue('jscnt',NoOfChildren);
    }  
    
    var isFindRow=-1;
    var i=0;
    var finalPosition=0;
   
    
    for(i=oMainTable.rows.length-1;i>=0;i--)
    {
        if(oMainTable.rows[i].id == oMainTable.id+'Room'+RoomNo+'Row3')
        {  
            isFindRow=3; 
            finalPosition=i;   
            break;
        }                    
        if(oMainTable.rows[i].id == oMainTable.id+'Room'+RoomNo+'Row2')
        {
            if(isFindRow < 2){isFindRow = 2;finalPosition=i;}
        }            
        if(oMainTable.rows[i].id == oMainTable.id+'Room'+RoomNo+'Row1')
        {
           
            if(isFindRow<1){isFindRow=1;finalPosition=i;}
        }    
    }
        
    if(isFindRow==-1)
    {   
        hclear();
        finalPosition = findRowInsertPosition(oMainTable,RoomNo);
        halert(':: ' + finalPosition );
        createChildAgeRow(oMainTable,RoomNo,1,finalPosition);
        isFindRow=1;
    }
   
      
        halert('-' + finalPosition + 'position');
        
        if(isFindRow==1)
        {
            if(NoOfChildren > 3) {createChildAgeRow(oMainTable, RoomNo, isFindRow+1, finalPosition+1); finalPosition=finalPosition+1; isFindRow=2;   }
        }
       
        if(isFindRow==2)
        {
           if(NoOfChildren>6) {createChildAgeRow(oMainTable,RoomNo,isFindRow+1,finalPosition+1); }
        }
   
    
    var dropDownIndex = 1;
    for(var tbRowIndex = 1; tbRowIndex < 4 ; tbRowIndex++)
    {   
        var tbRowCnt = document.getElementById(oMainTable.id+'Room'+RoomNo+'Row'+tbRowIndex);
        if(tbRowCnt == null)
        {
            halert('Combo add error'); 
            removeChilAgeRow(oMainTable,RoomNo,NoOfChildren); 
            return false;
        }
        
        
//        if(tbRowIndex==1){startIndex=1; endIndex=4;}
//        else{startIndex=0;endIndex=3}           
        var startIndex=1;
        var endIndex=4;
        
        for(var tbColIndex=startIndex;tbColIndex<endIndex;tbColIndex++)
        {
            if(dropDownIndex<=NoOfChildren)
            {
                if(tbRowCnt.cells[tbColIndex].childNodes.length==0)
                {
                    tbRowCnt.cells[tbColIndex].appendChild(addChildAgeCombo(oMainTable.id+'Room'+RoomNo+'Row'+tbRowIndex,RoomNo,dropDownIndex));
                }
            }
            else
            {            
               
                if(tbRowCnt.cells[tbColIndex]==null)
                {
                    //halert(" - NULL");
                }
                else
                {   
                    if(tbRowCnt.cells[tbColIndex].childNodes.length==1)
                    {       
                        var c = document.getElementById(oMainTable.id+'Room'+RoomNo+'Row'+tbRowIndex+'dd'+dropDownIndex);
                        if(c!=null)
                        {
                            tbRowCnt.cells[tbColIndex].removeChild(c);   
                        }
                    }
                }
                
            }   
            dropDownIndex=dropDownIndex+1; 
        }
    }
    
    removeChilAgeRow(oMainTable,RoomNo,NoOfChildren);
}


/* Accommodation End */
