﻿
//window.onload = function(){ sendRequestXML(1,'a') };
//global miles
var darsh;
var path = new Array();
//***************************************************************
//create the ajax obj
function createRequestObject() {

    var req;

    if(window.XMLHttpRequest){

        // Firefox, Safari, Opera...
        req = new XMLHttpRequest();

    } else if(window.ActiveXObject) {

        // Internet Explorer 5+
        req = new ActiveXObject("Microsoft.XMLHTTP");

    } else {

        // There is an error creating the object,
        // just as an old browser is being used.
        alert('Problem creating the XMLHttpRequest object');

    }

    return req;

}

// Make the XMLHttpRequest object
var http = createRequestObject();
//***************************************************************
//send the ajax request
function sendRequestXML( id , string ) {

//    var code = document.getElementById( inputID ).value;
//    var nearby = document.getElementById( inputID + '_nearby' );
//    var nearby_loading = document.getElementById( inputID + '_nearby_loading' );

//    nearby.className = 'hide';
//    nearby_loading.className = 'show';
    
//    http.open( 'get', './aircrafts/aircrafts.xml' , true );
//    http.onreadystatechange = function(){ handleResponseG( id , string ) };
//	http.send( null );

}
//***************************************************************
//handle the ajax response
function handleResponseG( id , string ){
//if(string) alert(string);
//else alert('no string');
	if(http.readyState == 4 && http.status == 200){
        
        var xResponse = http.responseXML;
        //alert(response);
        
        if( xResponse ) {
        
            //hide loading
            $("ll").style.display = 'none';
            
            //get types
            var root = xResponse.getElementsByTagName('type');
            //alert(root.length);
            
            if( id == 1 ){ // get main tree childs
            
                //loop on types
                for( i=0 ; i < root.length ; i++ ){
                    
                    //get node value
                    //node = xResponse.getElementsByTagName('name')[i].childNodes[0].nodeValue;//first 4 names in the doc
                    //node = root[i].childNodes[0].nodeValue;//writes correct order but with empty text
                    node = root[i].getElementsByTagName('name')[0].childNodes[0].nodeValue;
                    
                    //write main node value to div
                    //$("leftPanel").innerHTML += "<span id='"+node+"'><a id='"+node+"2' href='#' onclick='Effect.toggle(node+\"1\",\"blind\");showLoadingTree(this.parentNode.id);sendRequestXML(2,this.parentNode.id);return false;' style='text-decoration:none;'>+</a> ";
                    $("leftPanel").innerHTML += "<div id='"+node+"' class='t_'><a id='"+node+"2' href='#' onclick='showLoadingTree(this.parentNode.id);sendRequestXML(2,this.parentNode.id);return false;' style='text-decoration:none;'>+</a> <a href='#' onclick='showLoadingContent(\"aircraft_content\");sendRequestXML(4,this.parentNode.id);return false;' style='text-decoration:none;'>" + node + "</a></div><span id='"+node+"1' style='border:0px solid;'></span><br />";
                    
                }
                
                                
            }else if( id == 2 ){ // get second tree sub-childs
                
                //loop on types
                for( j=0 ; j < root.length ; j++ ){
                
                    //get node values
                    node_to_srch = root[j].getElementsByTagName('name')[0].childNodes[0];
                    //alert(node_to_srch);
                    
                    //if id matches the name, get models
                    if( node_to_srch.nodeValue == string ){
                    
                        $( string + '1' ).innerHTML = ' ';
                         
                        models = root[j].getElementsByTagName('models')[0].getElementsByTagName('model');
                        
                        
                        //$( string + '1' ).innerHTML = "<span class='sub_cat'><div>";
                            
                        //loop on models
                        for( k=0 ; k < models.length; k++ ){
                        
                            //get models
                            model = models[k].getElementsByTagName('name')[0].childNodes[0].nodeValue;
                            
                            //write models
                            $( string + '1' ).innerHTML += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='sub_cat' id='"+model+"' href='#' onclick='showLoadingContent(\"aircraft_content\");sendRequestXML(3,this.parentNode.id.slice(0,-1)+\"|\"+this.id);return false;'>" + model + "</a><br />";
                            //alert(this.id);alert(this.parentNode.id.slice(0,-1));
                            
                        }
                        $( string + '1' ).innerHTML += "<br />";
                        
                    }//if
                    
                }//for
                
            }else if( id == 3 ){ // get data of sub-childs
            
                s = string.split("|");
                
                //loop on types
                for( m=0 ; m < root.length ; m++ ){
                
                    //get node values
                    node_to_srch = root[m].getElementsByTagName('name')[0].childNodes[0];
                    //alert(node_to_srch);
                    
                    //if id matches the name, get models
                    if( node_to_srch.nodeValue == s[0] ){
                    
                        //$( string + '1' ).innerHTML = ' ';
                         
                        models = root[m].getElementsByTagName('models')[0].getElementsByTagName('model');
                        
                        //loop on models
                        for( n=0 ; n < models.length; n++ ){
                        
                            //get the values from XML file associated with n
                            getN(n);
                            
                            //write models
                            if( modelName == s[1] ){
                                //alert(darsh);
                                
                                writeXMLContent();
                                hideMap('map');
                                m = models[n].getElementsByTagName('name')[0].childNodes[0].nodeValue;
                                $('selected_airjet').value = m;
                                
                            }
                            
                        }//end for
                        
                    }//if
                    
                }//for
            
		}else if( id == 4 ){ // get notes
		
                //loop on types
                for( p=0 ; p < root.length ; p++ ){
                
                    //get node values
                    name_to_compare = root[p].getElementsByTagName('name')[0].childNodes[0];
                    
                    
                    //if id matches the name, get models
                    if( name_to_compare.nodeValue == string ){
                         
                        notes = root[p].getElementsByTagName('notes')[0].childNodes[0].nodeValue;
                        
                        
                    }//if
                    
                }//for
                $("aircraft_content").innerHTML = notes;
            
		}//end if( xResponse )
		
	  }//end if xResponse
	  
	}//end if ready state && status
	
}//end function
//***************************************************************
function addCatalogSuggest( id ){
    //add suggest
    //$( 'sug_' ).innerHTML += "<div style='width:100%; position:absolute; top:80%; border:0px solid red;'><hr /><br /><div style='position:relative;'><div id='ent_ac' class='ac'>Enter city name...</div><input id='ent' type='text' onkeyup='searchXML2(\"ent\",\"ent_ac\");' autocomplete='off' style='width:170px;' /></div>Select airport to check aircraft range:</div>";
}
//***************************************************************
function hideMap( map ){
    if( $(map) ) $(map).style.display = 'none';
}
//***************************************************************
function showLoadingTree( id ){
    $( id + '1' ).innerHTML = 'Loading...<br />'; //show loading...
    $( id + '2' ).innerHTML = '- '; // show minus sign
}
function showLoadingContent( id ){
    $( id ).innerHTML = 'Loading...<br />'; //show loading...
    //$( id ).innerHTML = '- '; // show minus sign
}
//***************************************************************
function writeXMLContent(){

    var c = '';
    //alert(modelName);
    //alert(darsh);
    darsh = +miles;
    
    //suggest
    c += "<input id='from_ent_suggest' type='hidden' value='0' /><div id='sug' style='border:0px solid red; height:30px; float:left;'>Select airport to check aircraft range:<br /><div style='width:100%; position:absolute;'><div style='position:relative;'><div id='from_ent_ac' class='ac'></div><input id='from_ent' type='text' onkeyup='searchXML2(\"from_ent\",\"from_ent_ac\");' autocomplete='off' style='width:170px;' /><span id='from_ent_nearby_loading' class='hide' style='vertical-align:bottom;'><img src='SiteImages/loading.gif' width='16' height='16' /></span></div></div></div>";
    
    if( path_.length != 0 ){
    
        divID = "_mostafa";
        
        //xxx = path[0];
        
        
        c += '<div style="margin-top:20px;text-align:center;margin:5px;right:75px;float:right; border:5px solid #82BF82;"><a href="#" onclick="$(\'cover\').style.display=\'block\';Effect.Grow($(divID),{duration:.5});showImage(0);hideMap(\'map\');return false;"><img src="./'+path[0]+'" width="150" height="150" /></a></div>';
        
        var counter = 1;
        var lBreak;
        
        c += '<div style="text-align:right;margin:5px;right:75px; float:right; border:0px solid #82BF82; z-index:3; width:140px;">';
        
        //alert(path[0]);
        //alert(path[1]);
        
        for( r = 1 ; r < path_.length ; r++ ){
        
            
            if( counter < 2 ){
                lBreak = '';
            }else{
                lBreak = '<br />';
                counter = 0;
            }

            c += '<a href="#" onclick="$(\'cover\').style.display=\'block\';Effect.Grow($(divID),{duration:.5});showImage('+r+');return false;"><img src="./'+path[r]+'" width="50" height="50" style="padding:5px;border:1px solid #82BF82;" /></a>'+lBreak;
            
            counter++;
        }
        
        c += '</div>';
        //alert(c);
        
    }else if( path_.length == 0 ){
    
        c += '<div style="text-align:center; float:right;right:75px;margin:5px;"><div style=" border:5px solid red; width:150px; height:150px;">No Image</div></div>';
        
    }
    
    c += '<span style="height:50px; border:0px solid red;"><br /><br />&nbsp;<br /><br /><br /></span>';
    c += '<span class="bold_text">Type:</span> '+s[0]+'<br /><br />';
	c += '<span class="bold_text">Model:</span> '+s[1]+'<br /><br />';
	c += '<span class="bold_text">Manufacturer:</span> '+manufacturer+'<br /><br />';
	c += '<span class="bold_text">PAX Exclucing Crew:</span> '+PAXExclucingCrew+'<br /><br />';
//	c += '<span class="bold_text">Certification AOCDoc:</span> '+CertificationAOCDoc+'<br /><br />';
	c += '<span class="bold_text">Required Landing Distance:</span> '+RequiredLandingDistance+'<br /><br />';
	c += '<span class="bold_text">Maximum Speed:</span> '+MaximumSpeed+'<br /><br />';
	c += '<span class="bold_text">Max CertifiedAltitude:</span> '+maxCertifiedAltitude+'<br /><br />';
	c += '<span class="bold_text">Range Max PAX:</span> '+RangeMaxPAX+'<br /><br />';
	c += '<span class="bold_text">Max Range:</span> '+maxRange+'<br /><br />';
    c += '<span class="bold_text">Cabin Length:</span> '+cabinLength+'<br /><br />';
    c += '<span class="bold_text">Cabin Height:</span> '+cabinHeight+'<br /><br />';
    c += '<span class="bold_text">Cabin Width:</span> '+cabinWidth+'<br /><br />';
    c += '<span class="bold_text">Additional Info:</span> '+additionalInfo+'<br /><br />';
    c += '<span class="bold_text">Additional Features:</span> <br />';
        c += '<div style="margin-left:30px;">';
            c += '<span class="bold_text">Entertainment:</span> '+entertainment+'<br /><br />';
            c += '<span class="bold_text">Lavatory:</span> '+lavatory+'<br /><br />';
            c += '<span class="bold_text">Refreshment Options:</span> '+refreshmentOoptions+'<br /><br />';
            c += '<span class="bold_text">Medical Equipment:</span> '+MedicalEquipment+'<br /><br />';
            c += '<span class="bold_text">Microwave:</span> '+Microwave+'<br /><br />';
            c += '<span class="bold_text">DVD Air Show:</span> '+DVDAirShow+'<br /><br />';
            c += '<span class="bold_text">Linens For Beds:</span> '+LinensForBeds+'<br /><br />';
            c += '<span class="bold_text">Data Port:</span> '+DataPort+'<br /><br />';
            c += '<span class="bold_text">Leather Seats:</span> '+LeatherSeats+'<br /><br />';
            c += '<span class="bold_text">Satellite Phone:</span> '+SatellitePhone+'<br /><br />';
        c += '</div>';
          
    $("aircraft_content").innerHTML = c;
}
//***************************************************************
//***************************************************************
function getN( n ){

  //reset m
  //m = 0;

  //get models
  
  modelName = models[n].getElementsByTagName('name')[0].childNodes[0].nodeValue;
  //alert(modelName);
  manufacturer = models[n].getElementsByTagName('manufacturer')[0].childNodes[0].nodeValue;
  
  PAXExclucingCrew_ = models[n].getElementsByTagName('PAXExclucingCrew')[0];
  if( PAXExclucingCrew_.hasChildNodes() ){
    PAXExclucingCrew = PAXExclucingCrew_.childNodes[0].nodeValue;
  }else PAXExclucingCrew = 'none';
  
  if( models[n].getElementsByTagName('CertificationAOCDoc')[0].hasChildNodes() ){
    CertificationAOCDoc = models[n].getElementsByTagName('CertificationAOCDoc')[0].childNodes[0].nodeValue;
  }else CertificationAOCDoc = 'none';
  
  if( models[n].getElementsByTagName('RequiredLandingDistance')[0].hasChildNodes() ){
    RequiredLandingDistance = models[n].getElementsByTagName('RequiredLandingDistance')[0].childNodes[0].nodeValue;
  }else RequiredLandingDistance = 'none';
  
  if(models[n].getElementsByTagName('MaximumSpeed')[0].hasChildNodes() ){
    MaximumSpeed = models[n].getElementsByTagName('MaximumSpeed')[0].childNodes[0].nodeValue;
  }else MaximumSpeed = 'none';
  
  if(models[n].getElementsByTagName('maxCertifiedAltitude')[0].hasChildNodes() ){
    maxCertifiedAltitude = models[n].getElementsByTagName('maxCertifiedAltitude')[0].childNodes[0].nodeValue;
  }else maxCertifiedAltitude = 'none';
  
  if(models[n].getElementsByTagName('RangeMaxNBA')[0].hasChildNodes() ){
    RangeMaxNBA = models[n].getElementsByTagName('RangeMaxNBA')[0].childNodes[0].nodeValue;
  }else RangeMaxNBA = 'none';
  
  if(models[n].getElementsByTagName('RangeMaxIFR')[0].hasChildNodes() ){
    RangeMaxIFR = models[n].getElementsByTagName('RangeMaxIFR')[0].childNodes[0].nodeValue;
  }else RangeMaxIFR = 'none';
  
  if(models[n].getElementsByTagName('RangeMaxPAX')[0].hasChildNodes() ){
    RangeMaxPAX = models[n].getElementsByTagName('RangeMaxPAX')[0].childNodes[0].nodeValue;
  }else RangeMaxPAX = 'none';
  
  if(models[n].getElementsByTagName('maxRange')[0].hasChildNodes() ){
    maxRange = models[n].getElementsByTagName('maxRange')[0].childNodes[0].nodeValue;
  }else maxRange = 'none';
  
  if(models[n].getElementsByTagName('cabinLength')[0].hasChildNodes() ){
    cabinLength = models[n].getElementsByTagName('cabinLength')[0].childNodes[0].nodeValue;
  }else cabinLength = 'none';
  
  if(models[n].getElementsByTagName('cabinHeight')[0].hasChildNodes() ){
    cabinHeight = models[n].getElementsByTagName('cabinHeight')[0].childNodes[0].nodeValue;
  }else cabinHeight = 'none';
  
  if(models[n].getElementsByTagName('cabinWidth')[0].hasChildNodes() ){
    cabinWidth = models[n].getElementsByTagName('cabinWidth')[0].childNodes[0].nodeValue;
  }else cabinWidth = 'none';
  
  if(models[n].getElementsByTagName('additionalInfo')[0].hasChildNodes() ){
    additionalInfo = models[n].getElementsByTagName('additionalInfo')[0].childNodes[0].nodeValue;
  }else additionalInfo = 'none';
  
  if(models[n].getElementsByTagName('miles_')[0].hasChildNodes() ){
      
      miles = models[n].getElementsByTagName('miles_')[0].childNodes[0].nodeValue;
      //alert(+miles);
  
      //save miles in global js var
      //darsh = +miles;
      
  }else{ miles = 0; /*darsh = +miles;*/ }
  //alert(darsh);

  
  //additionalFeatures
  if(models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('entertainment')[0].hasChildNodes() ){
    entertainment = models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('entertainment')[0].childNodes[0].nodeValue;
  }else entertainment = 'none';
    
    if(models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('lavatory')[0].hasChildNodes() ){
        lavatory = models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('lavatory')[0].childNodes[0].nodeValue;
  }else lavatory = 'none';
    
    if(models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('refreshmentOoptions')[0].hasChildNodes() ){
        refreshmentOoptions = models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('refreshmentOoptions')[0].childNodes[0].nodeValue;
  }else refreshmentOoptions = 'none';
    
    if(models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('MedicalEquipment')[0].hasChildNodes() ){
        MedicalEquipment = models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('MedicalEquipment')[0].childNodes[0].nodeValue;
  }else MedicalEquipment = 'none';
    
    if(models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('Microwave')[0].hasChildNodes() ){
        Microwave = models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('Microwave')[0].childNodes[0].nodeValue;
  }else Microwave = 'none';
    
    if(models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('DVDAirShow')[0].hasChildNodes() ){
        DVDAirShow = models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('DVDAirShow')[0].childNodes[0].nodeValue;
  }else DVDAirShow = 'none';
    
    if(models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('LinensForBeds')[0].hasChildNodes() ){
       LinensForBeds = models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('LinensForBeds')[0].childNodes[0].nodeValue;
  }else LinensForBeds = 'none';
    
    if(models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('DataPort')[0].hasChildNodes() ){
       DataPort = models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('DataPort')[0].childNodes[0].nodeValue;
  }else DataPort = 'none';
    
    if(models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('LeatherSeats')[0].hasChildNodes() ){
       LeatherSeats = models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('LeatherSeats')[0].childNodes[0].nodeValue;
  }else LeatherSeats = 'none';
    
    if(models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('SatellitePhone')[0].hasChildNodes() ){
       SatellitePhone = models[n].getElementsByTagName('additionalFeatures')[0].getElementsByTagName('SatellitePhone')[0].childNodes[0].nodeValue;
  }else SatellitePhone = 'none';
    
    
    path_ = models[n].getElementsByTagName('images')[0].getElementsByTagName('path');
    
    if( path_.length == 0 ) { /*path.length = 0;*/ }
    else{
        
        for( i = 0 ; i<path_.length ; i++ ){
        
            path[i] = models[n].getElementsByTagName('images')[0].getElementsByTagName('path')[i].childNodes[0].nodeValue;
            
        }
        //alert(path.length);
        
    }
    
}
//***************************************************************
function showImage( imgID ){

    //$('_mostafa').innerHTML = '';
    //alert(imgID);
    //alert(path[+imgID]);
    $('_mostafa').innerHTML = '<span style="float:left; padding:20px;" class="text">'+s[0]+' > <strong>'+s[1]+'</strong></span><a href="#" onclick="hideMap(\'map\');Effect.Shrink($(divID),{duration:.5});xx();return false;" style="float:right; padding:20px;" class="text">Close</a><img src="./'+path[+imgID]+'" width="600" height="300" />';
    
}
function xx(){
    setTimeout(yy,500);
}
function yy(){
    $('cover').style.display='none';
}
//***************************************************************
