var advance=false;
function AjaxRequest(type){      
            var depart=$("depart").value;
            var arrival=$("arrival").value;
            var via=$("via").value;
            var type1="Detail";
            if(type=="advance")
	    advance=true;
            
            var request = new Ajax.Request('Airportlst.php?depart='+depart+'&arrival='+arrival+'&via='+via+'&type='+type1,
                {
                 method : 'get',
                 asynchronous: true,
                 postBody: null,
                 onSuccess: parseRes,
                 onFailure: errorMsg
               }
               );
               Lightbox.showBoxString("<p>Please wait while we search for the airports...<br /><span align='center'><img src='images/ajax-loader.gif'></span></p>",500,100);       
    }

	
	
function parseRes(tr)
{
     var chk=false;      
   var result=parseJSON(tr.responseText);
   var temp='';
   
  
  temp+='<form onsubmit="return ConformCode()" class="niceform" method="post" name="orderform"> <table border="0" cellpadding="0" cellspacing="0"';
  temp+='<tr><td colspan=2 align="left"><h3>Select Departure Airport.</h3></td></tr>'
  if(result.Depart[0]=="Detail")
   {
            temp+='<tr><td><p class="indent">Please Enter Vaild Departue Airport Name.</p></td></tr>';
             chk=true;
   }
   else
   {
            for(var j=0;j<result.Depart.length;j++)
            {
             temp+='<tr><td><input type="radio" id='+result.Depart[j][0]+' name="depart"  value='+result.Depart[j][0]+' >  </td>';
             temp+='<td ><label align="left" for='+result.Depart[j][0]+'>'+result.Depart[j][1]+'</label></td></tr>';
            }
            temp+='<tr><td colspan="2"><label id="errordepart" style="color:red;display:none;">Please Select Departue Airport.</lable></td></tr>';
   }
  temp+='</table>';
  
  
  
  temp+='<table border="0" cellpadding="0" cellspacing="0"';
  temp+='<tr><td colspan=2 align="left"><h3>Select Arrival Airport.</h3></td></tr>'
  if(result.Arrival[0]=="Detail")
   {
            temp+='<tr><td><p class="indent">Please Enter Vaild Arrival Airport Name.</p></td></tr>';
           chk=true;
   }
   else
   {
            for(var i=0;i<result.Arrival.length;i++)
            {
             temp+='<tr><td><input type="radio" id='+result.Arrival[i][0]+' name="arrival" value='+result.Arrival[i][0]+' >  </td>';
             temp+='<td align="left"><label align="left" for='+result.Arrival[i][0]+'>'+result.Arrival[i][1]+'</label></td></tr>';
             temp+='<tr><td colspan="2"><label id="errorarrival" style="color:red; display:none;">Please Select Arrival Airport.</lable></td></tr>';
            }
   }
  temp+='</table>';
  
  
  if($("via").value!="")
  {
    temp+='<table border="0" cellpadding="0" cellspacing="0"';
    temp+='<tr><td colspan=2 align="left"><h3>Select Via Airport.</h3></td></tr>'
    if(result.Via[0]=="Detail")
   {
            temp+='<tr><td><p class="indent">Please Enter Vaild Via Airport Name.</p></td></tr>';
       chk=true;
   }
   else
   {
            for(var k=0;k<result.Via.length;k++)
            {
             temp+='<tr><td><input type="radio" id='+result.Via[k][0]+'  name="via" value='+result.Via[k][0]+' >  </td>';
             temp+='<td align="left"><label align="left" for='+result.Via[k][0]+'>'+result.Via[k][1]+'</label></td></tr>';
            }
            temp+='<tr><td colspan="2"><label id="errorvia" style="color:red; display:none;">Please Select Via Airport.</lable></td></tr>';
   }
    temp+='</table>';
  }
  if(chk==false)
  temp+='<table><tr> <td align="right"> <input type="image" src="images/calculator.gif" id="Submit" Value="Calculate"  name="Submit"></td></tr></table>'
  else
  temp+='<table><tr> <td align="right"> <img src="images/back.gif" alt="back" onclick="Lightbox.hideBox();"></td></tr></table>'
 temp+='<div><INPUT type="HIDDEN" id="Basic1" name="Basic"><INPUT type="HIDDEN" id="round1" name="round"><INPUT type="HIDDEN" id="nop1" name="nop"><INPUT type="HIDDEN" id="class1" name="class"><INPUT type="HIDDEN" id="air1" name="air"><INPUT type="HIDDEN" id="cal1" name="cal"></div></form>'
            
  Lightbox.showBoxString(temp,500,410);
                                                               
									$('round1').value= document.frmcalculator.round.value; 
									$('nop1').value=$('nop').value;								
								if(advance==true)
								{
									$('class1').value=$('class').value;
									$('air1').value=$('air').value;
								}
								else
								{
									$('cal1').value=$('cal').value;
								}
}


function errorMsg(er)
{
   // $('notify').innerHTML = er.responseText;
}



function ConformCode()
{
             $('errordepart').style.display="none";
              $('errorarrival').style.display="none";
               
     var departflag=false;
        if(document.orderform.depart.length!=undefined)
        {
                 
                  for (var i=0; i < document.orderform.depart.length; i++)
                  {
                       if (document.orderform.depart[i].checked)
                       {
                          departflag=true;
                          break;
                       }
                  }
        }
        else
        {
                  if (document.orderform.depart.checked)            
                  departflag=true;
        }
        
       
  
  
    var arrivalflag=false;
   if(document.orderform.arrival.length!=undefined)
    {
       
        for (var j=0; j < document.orderform.arrival.length; j++)
          {
               if (document.orderform.arrival[j].checked)
               {
                    arrivalflag=true;
                    break;
                 }
          }
    }
    else
    {
      if(document.orderform.arrival.checked)
      arrivalflag=true;
    }
    
    
    if($('via').value!="")
        {
            $('errorvia').style.display="none";
            var viaflag=false;
            if(document.orderform.via.length!=undefined)
            {
                for (var k=0; k < document.orderform.via.length; k++)
                      {
                           if (document.orderform.via[k].checked)
                            {
                              viaflag=true;
                                break;
                             }
                      }
                      
            }
            else
            {
               if(document.orderform.via.checked)
                viaflag=true;
            }
            
            if(departflag==false)
            {
               $('errordepart').style.display="";
                return false;
            }
            else if(arrivalflag==false)
            {
              $('errorarrival').style.display="";
               return false;
            }
            else if(viaflag==false)
            {
               $('errorvia').style.display="";
                return false;
            }
            else
             return true;
            
        }
        else
        {
            if(departflag==false)
            {
               $('errordepart').style.display="";
               return false;
            }
            else if(arrivalflag==false)
            {
               $('errorarrival').style.display="";
               return false;
            }
            else
                return true;
        }
}






function parseJSON(json){

try{

//if(/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(json)){

var j = eval('(' + json + ')');

return j;

//}

}catch(e){

}

throw new SyntaxError("parseJSON");

}


window.onload = initLB;
function initLB() {
	Lightbox.init();
      
}
  
        function test(value)
                        {
                                    
                                                if(value=='advance')
                                                {
                                                            var air= new LiveValidation( 'air',{click:true});
                                                             air.add(Validate.Presence);
                                                              air.add(Validate.Length, { minimum: 2, maximum: 3 } );
                                                }
                                     var depart = new LiveValidation( 'depart', {click: true } );
                                     depart.add( Validate.Presence );                                   
                            
                                     var arrival = new LiveValidation( 'arrival', {click: true } );
                                     arrival.add( Validate.Presence );
                            
                                     var nop = new LiveValidation( 'nop', {click: true } );
                                     nop.add( Validate.Presence );
                                     nop.add(Validate.Numericality);
                                      
                                    
                        }
                function ValidatePage(value)
                {
                       
                                    var depart = new LiveValidation( 'depart', {click: true } );
                                     depart.add( Validate.Presence );
                            
                                     var arrival = new LiveValidation( 'arrival', {click: true } );
                                     arrival.add( Validate.Presence );
                            
                                     var nop = new LiveValidation( 'nop', {click: true } );
                                     nop.add( Validate.Presence );
                                     nop.add(Validate.Numericality);
                                     
                                     if(value=='advance')
                                     {
                                                var air= new LiveValidation( 'air',{click:true});
                                                air.add(Validate.Presence);
                                                air.add(Validate.Length, { minimum: 2, maximum: 3 } );
                                    
                                                var areAllValid = LiveValidation.massValidate( [ depart,arrival,nop,air] );
                                    
                                                if(!areAllValid)
                                                {                                                       
                                                        return false;
                                                }
                                                else
                                                {                                 
                                                         return true;
                                                }
                                     }
                                     else
                                     {
                                                var areAllValid = LiveValidation.massValidate( [ depart,arrival,nop] );
                                                if(!areAllValid)
                                                {
                                                        
                                                        return false;
                                                }
                                                else
                                                {                                 
                                                         return true;
                                                }
                                     }

                }
                
                function loginpage()
                {
                        var name = new LiveValidation( 'txtname', {click: true } );
                        name.add( Validate.Presence );
                            
                        var email = new LiveValidation( 'txtemail', {click: true } );
                        email.add( Validate.Presence );
                        email.add(Validate.Email);
                            
                        var password = new LiveValidation( 'txtpassword', {click: true } );
                        password.add( Validate.Presence );
                        
                        var pass = new LiveValidation( 'txtconformpassword', {click: true } );
                        pass.add( Validate.Presence );                        
                        pass.add( Validate.Confirmation, { match: 'txtpassword' } );
                       
                        var country = new LiveValidation( 'Country', {click: true } );
                        country.add( Validate.Exclusion, { within: ['Select'], partialMatch: true });
                        
                         var areAllValid = LiveValidation.massValidate( [ name,email,password,pass,country] );
                         if(!areAllValid)
                        {
                                
                                return false;
                        }
                        else
                        {                                 
                                 return true;
                        }
                        
                }
                
                function Validatefields()
                {
                        var name = new LiveValidation( 'txtname', {click: true } );
                        name.add( Validate.Presence );
                            
                        var email = new LiveValidation( 'txtemail', {click: true } );
                        email.add( Validate.Presence );
                        email.add(Validate.Email);
                            
                        var password = new LiveValidation( 'txtpassword', {click: true } );
                        password.add( Validate.Presence );
                        
                        var pass = new LiveValidation( 'txtconformpassword', {click: true } );
                        pass.add( Validate.Presence );                        
                        pass.add( Validate.Confirmation, { match: 'txtpassword' } );
                       
                        var country = new LiveValidation( 'Country', {click: true } );
                        country.add( Validate.Exclusion, { within: ['Select'], partialMatch: true });
                }
                
                function validatefields()
                {
                        var name = new LiveValidation( 'userid', {click: true } );
                        name.add( Validate.Presence );
                        var password = new LiveValidation( 'userpassword', {click: true } );
                        password.add( Validate.Presence );
                }
                function validatesubmit()
                {
                        var name = new LiveValidation( 'userid', {click: true } );
                        name.add( Validate.Presence );
                        var password = new LiveValidation( 'userpassword', {click: true } );
                        password.add( Validate.Presence );
                        
                        
                         var areAllValid = LiveValidation.massValidate( [ name,password] );
                         if(!areAllValid)
                        {
                                
                                return false;
                        }
                        else
                        {                                 
                                 return true;
                        }
                        
                }
                
                function startUpload(id)
                  {
                        var name=new LiveValidation( 'upload', {click: true } );
                        name.add(Validate.Presence);
                        var areAllValid=LiveValidation.massValidate([name]);
                        if(!areAllValid)
                        {
                                    return false;
                        }
                        else
                        {
                                   document.getElementById('f1_upload_process').style.visibility = 'visible';
                                   document.getElementById('f1_upload_form').style.visibility = 'hidden';
                                   var type;
                                   var month=$('month').value;
                                   var overwrite=$('term').checked;
                                   var year=$('year').value;
                                   if(overwrite==false)
                                   type="exit";
                                   else
                                   type="del";
                                    
                                   
                                    var request = new Ajax.Request('Deletefile.php?type='+type+'&id='+id+'&month='+month+'&year='+year,
                                      {
                                        method : 'get',
                                        asynchronous: true,
                                        postBody: null,
                                        onSuccess: altresp,
                                        onFailure: errorMsg
                                     }
                                     );   
                            
                        }
                         return false;
                  }
                  
                  function stopUpload(success)
                  {
                        var result = '';
                        if (success == 1){
                           result = '<span class="msg">The file was uploaded successfully!<br/>You can now view the reports.</span><br/>';
                        }
                        else {
                           result = '<span class="emsg">There was an error during file upload!</span>Please try again<br/><br/>';
                        }
                        document.getElementById('f1_upload_process').style.visibility = 'hidden';
                        document.getElementById('f1_upload_form').innerHTML = result;
                        document.getElementById('f1_upload_form').style.visibility = 'visible';      
                        return true;   
                  }
                  
                  function Genrate(type,fmonth,fyear,smonth,syear)
                  {
                        $('firki').style.display="";
                        $('contentgraph').style.display="none";
                        $('img1').src="";
                        $('img2').src ="";
                        var value=$("type").value;
                        var request = new Ajax.Request('Genrate.php?type='+type+'&value='+value+'&fmonth='+fmonth+'&fyear='+fyear+'&tmonth='+smonth+'&tyear='+syear,
                                    {
                                     method : 'get',
                                     asynchronous: true,
                                     postBody: null,
                                     onSuccess: parseGenrate,
                                     onFailure: errorMsg
                                   }
                                   );
                  }
                  
                  function parseGenrate(res)
                  {
                        var num = Math.random() * 1000;
                        var result=parseJSON(res.responseText);
                                    if(result.result[0]=="ok")
                                    {
                                                $('contentgraph').style.display="";
                                                var str = result.result[1]+ '#'+ num;
                                                $('img1').src= str;
                                                var str = result.result[2]+ '#'+ num;
                                                $('img2').src = str;
                                                $('h3').innerHTML=result.result[3];
                                                $('h4').innerHTML=result.result[4];
                                                var tbl="";
                                                tbl+="<table width='100%' height='195' bgcolor='#cccccc' style='color:black;'>";
                                                tbl+="<tr bgcolor='#007c88'>";
                                                tbl+="<td class='title12'>City pair</td>";
                                                tbl+="<td class='title12'>Carbon (Kgs)</td>"
                                                tbl+="<td class='title12'>Cost(USD)</td></tr>"
                                                for(var i=0;i<result.result[5].table.length;i++)
                                                {
                                                           tbl+="<tr style='color:#F5DEB3;font-weight:bold;'>"                                                            
                                                            tbl+="<td bgcolor="+color(i)+" >"+result.result[5].table[i][0]+"</td>";
                                                            tbl+="<td bgcolor='#6B92BD'>"+result.result[5].table[i][1]+"</td>"
                                                            tbl+="<td bgcolor='#D65D5A'>"+result.result[5].table[i][2]+"</td></tr>"
                                                }
                                                tbl+=" </table>"
                                                $('table').innerHTML=tbl;
                                    }
                                    else
                                    {
                                                alert(result.result[0]+" No result Found");
                                    }
                        $('firki').style.display="none";
                  }
      
      
      function color(i)
      {
            var col="";
            if(i==0)
            col='#E7824A';            
            else if(i==1)
            col='#B54D4A';   
            else if(i==2)
            col='#7BA642';   
            else if(i==3)
             col='#6B5184';   
            else if(i==4)
             col='#CE3431';   
       return col;    
      }
            
      
      function ShowCritiria(value)
      {
            var temp="<div  align= 'center'id='loading' style='display:none;'><label>Please Wait while we Generate Your Report... </label><br /> <img src='images/loading.gif' alt='loading'></div>"
			temp+="<div style='display:' id='lightdiv'><table><tr><td colspan='4'>Please Select Your Report Criteria.</td></tr><tr><td><strong>From:</strong></td><td><select id='month'><option value='1'>January</option>";
            temp+="<option value='2'>Feburary</option><option value='3'>March</option><option value='4'>April</option><option value='5'>May</option><option value='6'>June</option><option value='7'>July</option><option value='8'>Auguest</option><option value='9'>Septembet</option><option value='10'>October</option><option value='11'>November</option><option value='12'>December</option></select></td>";
            temp+="<td><select style='width:120px;' id='year'>";
            var d = new Date();
            var today=d.getFullYear();           
            for(var i=today;i>=1980;i--)
            {
                        temp+="<option value="+i+">"+i+"</option>";
            }
           
            temp+="</select></td></tr><tr><td><strong>Till:</strong></td><td><select id='tmonth'><option value='1'>January</option>";
            temp+="<option value='2'>Feburary</option><option value='3'>March</option><option value='4'>April</option><option value='5'>May</option><option value='6'>June</option><option value='7'>July</option><option value='8'>Auguest</option><option value='9'>Septembet</option><option value='10'>October</option><option value='11'>November</option><option value='12'>December</option></select></td>";
            temp+="<td><select style='width:120px;' id='tyear'>";
            var d = new Date();
            var today=d.getFullYear();           
            for(var i=today;i>=1980;i--)
            {
                        temp+="<option value="+i+">"+i+"</option>";
            }
           
            temp+="</select></td></tr><tr><td align='right' colspan='4'><a onclick=hideseek('"+value+"') id='redirect'> <img  border='0' src='images/genrater.gif' alt='Generate' /> </a></td></tr></table></div>";
            Lightbox.showBoxString(temp,500,135);             
            $('redirect').href=value;
			
			
            return false;
      }
	  
	  function hideseek(value)
	  {
		  $('lightdiv').style.display="none";
		  $('loading').style.display='';
                  var month=$('month').value;
                  var year=$('year').value;
                  var smonth=$('tmonth').value;
                  var syear=$('tyear').value;
                  value=value+'&month='+month+'&year='+year+'&smonth='+smonth+'&syear='+syear;
                  $('redirect').href=value;
		  return true;
		  
	  }
	  
	  function detail(userid,month,year,total,decard,filename,datetime,i)
          {
            if($('detail'+i).style.display=="none")
            {
              $('detail'+i).style.display="";
            }
            else
            {
              $('detail'+i).style.display="none";
              return;
            }
            month=Number(month);
            decard=Number(decard);
            total=Number(total);
            total=total-1;
            var valid=total-decard;
            
            switch(month)
            {
                        case 1:
                             month="January";
                        break;
                        case 2:
                             month="Febuary";
                        break;
                        case 3:
                             month="March";
                        break;
                        case 4:
                             month="April";
                        break;
                         case 5:
                             month="May";
                        break;
                          case 6:
                             month="June";
                        break;
                        case 7:
                             month="July";
                        break;
                        case 8:
                             month="August";
                        break;
                        case 9:
                             month="September";
                        break;
                        case 10:
                             month="October";
                        break;
                        case 11:
                             month="November";
                        break;
                        case 12:
                             month="December";
                        break;
            }
            
            var temp="";
            temp+='<table><tr><td><strong>File Name</strong></td><td>'+filename+'</td></tr><tr><td><strong>Total no of Record</strong></td><td>'+total+'</td></tr><tr><td><strong>Total no of Discarded Record</strong></td><td>'+decard+'</td></tr>';
            temp+='<tr><td><strong>Total no of Valid Record</strong></td><td>'+valid+'</td></tr><tr><td><strong>Month</strong></td><td>'+month+'</td></tr><tr><td><strong>Year</strong></td><td>'+year+'</td></tr><tr><td><strong>Upload date</strong></td><td>'+datetime+'</td></tr></table>';
            $('detail'+i).innerHTML="";
            $('detail'+i).innerHTML=temp;
            
          }
          
          function deletefile(id)
            {
                        if (confirm("Do you want delete the record in the database."))
                        {         
                        
                                    var type="delete";
                                    var request = new Ajax.Request('Deletefile.php?type='+type+'&id='+id,
                                    {
                                    method : 'get',
                                    asynchronous: true,
                                    postBody: null,
                                    onSuccess: delresp,
                                    onFailure: errorMsg
                                    }
                                    );
                                    Lightbox.showBoxString("<p>Please wait ...<br /><span align='center'><img src='images/ajax-loader.gif'></span></p>",500,100);
                        }
                        
                        else
                        
                        {         
                        
                              return;
                        
                        }

          }
          
       
          function delresp(response)
          {
            Lightbox.hideBox();
            location.reload(true);
          }
          
          
          function altresp(response)
          {
            if(response.responseText!=" ")
            {
                 document.getElementById('f1_upload_process').style.visibility = 'hidden';
                 $('condition').style.display="";
                 document.getElementById('f1_upload_form').style.visibility = 'visible'; 
            }
            else
            {
               
                  $('condition').style.display="none";
                  document.excel.submit();
            }
            
          }
          
          function Changepassword(id)
          {
            if($('Passwordfield').style.display=="")
            {
                     $('Passwordfield').style.display="none";
                     return;
            }
            else
            {
                        $('Passwordfield').style.display="";
            }
            
            var temp="";
            temp+="<table align='right'><tr><td colspan=2>Change  Password</td><tr>";
            temp+="<tr><td>Old Password</td><td><input type='password' id='OldPassword' /></td></tr><tr><td>New Password</td><td><input type='password' id='NewPassword' /></td></tr>";
            temp+="<tr><td>Confirm Password</td><td><input type='password' id='ConfirmPassword' /></td></tr><tr><td colsapn='2' align='right'><a onclick='passwordChange("+id+")'><img border=0; src='images/changepassword.gif' alt='ChangePassword' /></a></td></tr>";
            $('Passwordfield').style.display="";
            $('Passwordfield').innerHTML=temp;
          }
          
          function passwordChange(id)
          {
                                    
                       
                         var password = new LiveValidation( 'OldPassword', {click: true } );
                         password.add( Validate.Presence );
                         var newpassword = new LiveValidation( 'NewPassword', {click: true } );
                         newpassword.add( Validate.Presence );
                         var confirmpassword=new LiveValidation('ConfirmPassword',{click: true});
                          confirmpassword.add( Validate.Presence );
                         confirmpassword.add( Validate.Confirmation, { match: 'NewPassword' } );
                        
                         var areAllValid = LiveValidation.massValidate( [password,newpassword,confirmpassword] );
                         if(!areAllValid)
                        {
                                
                                return false;
                        }
                        else
                        {                                 
                                            
            
                                    var type="ChangePassword";
                                    var request = new Ajax.Request('Deletefile.php?type='+type+'&id='+id+'&oldpassword='+$('OldPassword').value+'&newpassword='+$('NewPassword').value,
                                    {
                                      method : 'get',
                                      asynchronous: true,
                                      postBody: null,
                                      onSuccess: Passresp,
                                      onFailure: errorMsg
                                    }
                                    );
                                    Lightbox.showBoxString("<p>Please wait ...<br /><span align='center'><img src='images/ajax-loader.gif'></span></p>",500,100);
                                     return true;
                        }
            
          }
          
            function Passresp(response)
            {
                        Lightbox.hideBox();
                        var resp=response.responseText;
                        resp=Number(resp);
                        if(resp==1)
                        {
                                    $('Passwordfield').innerHTML="Your Password is Change Sucessfully.";
                        }
                        else
                        {
                                    alert("Please Enter Valid Password.");
                        }
            }