var dhxWindow=null;
var w1=null;
var dhxTabbar=null;
var dhxForm=null;
var dhxToolbar=null;
var w2=null;
var dhxTabbarReserv=null;
var dhxLayout=null;
var dhxToolbarReserva=null;
var iIdCliente="";
var total_cost=null;
var amount=null;
var price=0.00;
var nextTab=true;
var nextTabReserv=true; 
window.onload=function(){
  
   
   dhxWindow=new dhtmlXWindows();
   dhxWindow.setSkin('dhx_web');
   dhxWindow.enableAutoViewport(true);
   dhxWindow.attachViewportTo(document.body);
   dhxWindow.attachEvent("onClose",Close);
   dhxWindow.attachEvent("onContentLoaded",function(w){
                 
   });	
   var w="";
    var r="";
   if(navigator.appName.indexOf("Microsoft Internet Explorer")==-1)
    {  w=(window.innerWidth/2)-290;
	   r=(window.innerWidth/2)-280;
	}
	else
	 {w=(window.innerWidth/2)-290;
	   r=(window.innerWidth/2)-280;
	 }
   w1=dhxWindow.createWindow("w1",w,0,400,460);
   
   w1.setText("Create Account");
   w1.attachURL("form_register.php",true); 
  
   
   w2=dhxWindow.createWindow("w2",r,0,570,650);
   
   w2.setText("Reservar");
   dhxTabbarReserv=w2.attachTabbar();
   
   dhxTabbarReserv.setSkin("modern");
   dhxTabbarReserv.setImagePath("css/imgs/");
   
   dhxTabbarReserv.addTab("Schedule","Study Schedule","110px");
   dhxTabbarReserv.addTab("HomeStay","Homestay Accomodation","140px");
   dhxTabbarReserv.addTab("Airport","Airport Pick Up","100px");
   dhxTabbarReserv.addTab("MethodPayment","Payment Method","110px");
   dhxLayout=dhxTabbarReserv.cells("Schedule").attachLayout("1C","dhx_web");
   dhxLayout.cells("a").attachURL("form_schedule.php");
   dhxLayout.cells("a").hideHeader();
   dhxLayout.progressOn();
   
   //dhxTabbarReserv.cells("Schedule").attachURL("form_schedule.php");
   dhxTabbarReserv.cells("HomeStay").attachURL("form_homestay.php");
   dhxTabbarReserv.cells("Airport").attachURL("form_arrival.php");
   dhxTabbarReserv.cells("MethodPayment").attachURL("form_metodpayment.php");
   dhxTabbarReserv.setTabActive("Schedule");
   w3=dhxWindow.createWindow("w3",200,100,300,300);
   w3.setText("");
   w3.attachURL("confirmation.php");
   w1.hide();
   w2.hide();
   w3.hide();
  
    $("#displaylogeo").toggle(function(){
               $("#login").show("slow"); 
      },
      function(){
             $("#login").hide("slow");   
      })
      $('#valida').click(function(){
              $.ajax({
                      url:'validate_login.php',
                      type:'post',
                      data:$('#formlogin').serialize(),
                      success:function(data){
                          
                           if(data>0)                       
                           {
                            
                             
                              location.href=window.location.href;
                            
                           }
                           else
                           {
                                 alert('The username or password is invalid. Please try again. Thank you');	
                           }
                      }
                
              });
        
      });
     dhtmlx.skin = "dhx_web";
       a_tabbar.setSkin("modern");
       layout=a_tabbar.cells("Schedule").attachLayout("1C","dhx_web");
       layout.cells("a").setHeight(700);
        layout.cells("a").setWidth(800);
       layout.cells("a").attachURL("form_scheduleIndex.php");
       layout.cells("a").hideHeader();
       
       a_tabbar.cells("Airport").attachURL("form_arrivalIndex.php");
       a_tabbar.cells("HomeStay").attachURL("form_homestayIndex.php");
       a_tabbar.cells("MethodPayment").attachURL("form_metodpaymentIndex.php");
      if(document.getElementById('wellcome')!=null){
               document.getElementById('login').style.display='block';
         }

   
}

function saveData(){
     
    serialize=[];
    var messages=["This field is required","","","","","This field is required","",""];
    if(validate("personal",messages)){
       if($("#pass").val()==$("#confirmpassword").val())
     { 
	   $("#progressData").css('display','block');
    
        serialize=$("#personal").serialize().split('&');
	 
     
	
	   $.ajax({url:'save_customer.php',
		    type:'post',
			data:"dataClient="+$.toJSON(serialize),
			success:function(res){
				  if(parseInt(res)>=1){
        alert("You have successfully created your account. Please log in with your username and password and make your reservation in 'reserve now'. You have received an email with your username and password as well with a link to make your reservation. You can also click in the button 'reserve now' on our homepage.Thank you!");  
					    $("#progressData").css('display','none');
                        iIdCliente=res;
                       	w1.setModal(false);
	                    w1.close();
				   }
			 }
		   });
           }
           else{
                  alert("The Password and Confirm Password fields should be equal");
           }
	
	}

	
	
	
}
showDialogConfirmation=function(){
       w3.setModal(true);        
       w3.show();	
	
}
function saveReservation(){
        serialize=[];
        
       
               for(m=0;m<window.frames.length;m++){
                       if(typeof(window.frames[m].sh)!="undefined"){
                           form1=window.frames[m].document.getElementById('schedule');
                            promotional=window.frames[m].document.getElementById('promotionalCode');
                            pCode=window.frames[m].document.getElementById('pcode');
                            tblcotiza=window.frames[m].document.getElementById('tblcotiza');
                            
                       }
                       if(typeof(window.frames[m].v)!="undefined"){
                           form2=window.frames[m].document.getElementById('arrivalinfo');
                        
                       }
                       if(typeof(window.frames[m].v1)!="undefined"){
                          form3=window.frames[m].document.getElementById('homestay');
                       }
                       if(typeof(window.frames[m].metod)!="undefined"){
                           form4=window.frames[m].document.getElementById('methodpayment');
                           processData=window.frames[m].document.getElementById('processData');
                           option=window.frames[m].document.getElementById('opciones_reserva');
                           
                       }
               }
            
            
        
        dataForm1=$(form1).serialize().split('&');
       
        dataForm2=$(form2).serialize().split('&');
        dataForm3=$(form3).serialize().split("&");
        dataForm4=$(form4).serialize().split('&');
        dataDetails={"Item":[],"Description":[],"Price":[]};
         var trs=tblcotiza.getElementsByTagName('tr');
         for(i=1;i<trs.length;i++)
        {
                               
                 if(trs[i].childNodes.length>0)
               {  cell1=trs[i].getElementsByTagName('td')[0];
                 cell2=trs[i].getElementsByTagName('td')[1];
                 cell3=trs[i].getElementsByTagName('td')[2];
                 dataDetails.Item.push(cell1.innerHTML);             	
        	   dataDetails.Description.push(cell2.innerHTML);
        	   price=cell3.innerHTML.split('$');
        	   dataDetails.Price.push(price[1]);
              }
        }
        
           
        
         serialize=dataForm1.concat(dataForm2.concat(dataForm3.concat(dataForm4)));
         if(document.getElementById('wellcome')!=null){
               iIdCliente=document.getElementById('wellcome').getAttribute('cliente');
         }
         else{
              
         }
         serialize.push("iIdCliente="+iIdCliente);
         
             if(iIdCliente!="")
             {  
                 processData.style.display="block";
                
                $.ajax({url:'save_reservation.php',
        		    type:'post',
        			data:"dataReservation="+$.toJSON(serialize)+"&dataDetails="+$.toJSON(dataDetails),
        			success:function(res){
        				  if(parseInt(res)>=1){
        					   alert('Dear have receibed your reservation and have sent you a reseration number to your email, thank you.');  
      					           processData.style.display="none";
                                              if(option.checked){
                                               window.setTimeout("sendPayment()",3000);
                                                 }  
                                        }
        			 }
        		   });
        
             }
             else
             {
                
                  alert("Please login now");
             }
    
}
function saveReservation1(){
     serialize=[];
        // if(navigator.appName.indexOf("Microsoft Internet Explorer")==-1)
//         {   form1=window.frames[7].document.getElementById('schedule1');
//            
//            form2=window.frames[8].document.getElementById('arrivalinfo1');
//             
//            
//           
//            form3=window.frames[9].document.getElementById('homestay1');
//            form4=window.frames[10].document.getElementById('methodpayment1');
//            promotional=window.frames[7].document.getElementById('promotionalCode1');
//            pCode=window.frames[7].document.getElementById('pcode1');
//            pCode.value=promotional.value;
//            tblcotiza=window.frames[7].document.getElementById('tblcotiza');
//        }
        //else    
        //if(navigator.appName.indexOf("Microsoft Internet Explorer")>=0){
               for(m=0;m<window.frames.length;m++){
                       if(typeof(window.frames[m].sh1)!="undefined"){
                           form1=window.frames[m].document.getElementById('schedule1');
                            promotional=window.frames[m].document.getElementById('promotionalCode1');
                            pCode=window.frames[m].document.getElementById('pcode1');
                            
                            pCode.value=promotional.value;
                            tblcotiza=window.frames[m].document.getElementById('tblcotiza');
                            
                       }
                       if(typeof(window.frames[m].v2)!="undefined"){
                           form2=window.frames[m].document.getElementById('arrivalinfo1');
                        
                       }
                       if(typeof(window.frames[m].v3)!="undefined"){
                          form3=window.frames[m].document.getElementById('homestay1');
                       }
                       if(typeof(window.frames[m].metod1)!="undefined"){
                           form4=window.frames[m].document.getElementById('methodpayment1');
                            processData=window.frames[m].document.getElementById('processData');
                             option=window.frames[m].document.getElementById('opciones_reserva');

                       }
               }
            
            
        //}
        dataForm1=$(form1).serialize().split('&');
        
        dataForm2=$(form2).serialize().split('&');
        dataForm3=$(form3).serialize().split("&");
        dataForm4=$(form4).serialize().split('&');
      
        dataDetails={"Item":[],"Description":[],"Price":[]};
                
       
        var trs=tblcotiza.getElementsByTagName('tr');
         for(i=1;i<trs.length;i++)
        {
                               
                 if(trs[i].childNodes.length>0)
               {  cell1=trs[i].getElementsByTagName('td')[0];
                 cell2=trs[i].getElementsByTagName('td')[1];
                 cell3=trs[i].getElementsByTagName('td')[2];
                 dataDetails.Item.push(cell1.innerHTML);             	
        	   dataDetails.Description.push(cell2.innerHTML);
        	   price=cell3.innerHTML.split('$');
        	   dataDetails.Price.push(price[1]);
              }
        }
        
        
         serialize=dataForm1.concat(dataForm2.concat(dataForm3.concat(dataForm4)));
         if(document.getElementById('wellcome')!=null){
               iIdCliente=document.getElementById('wellcome').getAttribute('cliente');
         }
         else{
              
         }
         serialize.push("iIdCliente="+iIdCliente);
         
             if(iIdCliente!="")
             {    
                 processData.style.display="block";
                
                $.ajax({url:'save_reservation.php',
        		    type:'post',
        			data:"dataReservation="+$.toJSON(serialize)+"&dataDetails="+$.toJSON(dataDetails),
        			success:function(res){
        				  if(parseInt(res)>=1){
        					   alert('Dear have receibed your reservation and have sent you a reservation number to your email, thank you.');  
        					   processData.style.display="none";
                                             if(option.checked){
                                               window.setTimeout("sendPayment()",3000);
                                                 }                                          				   

                                           }
        			 }
        		   });
        
             }
             else
             {
                
                  alert("Please login now");
             }
    
}
function Close(wind){
  
    
    dhxWindow.window(wind.getId()).setModal(false);
    dhxWindow.window(wind.getId()).hide();
     if(navigator.appName.indexOf("Microsoft Internet Explorer")==-1)
	{	form1=window.frames[1].document.getElementById('register');
	 
		form2=window.frames[2].document.getElementById('contact');
		form3=window.frames[3].document.getElementById('emergency');
		form4=window.frames[4].document.getElementById('aboutyou');
    
		form5=window.frames[5].document.getElementById('additional');
   }
   else
   {
          for(l=0;l<window.frames.length;l++){
                   if(typeof(window.frames[l].vReg)!="undefined"){
                        form1=window.frames[l].document.getElementById('register');
                                          	
                   	}
                   if(typeof(window.frames[l].vContact)!="undefined"){
                         form2=window.frames[l].document.getElementById('contact');                   	
                   	}
                   if(typeof(window.frames[l].vEmerg)!="undefined"){
                        form3=window.frames[l].document.getElementById('emergency');                    	
                   	}
                   if(typeof(window.frames[l].vAbout)!="undefined"){
                         form4=window.frames[l].document.getElementById('aboutyou');                 	
                   	}
                   	if(typeof(window.frames[l].additional)!="undefined"){
                   		  form5=window.frames[l].document.getElementById('additional');
                   	}				
          }   	
   
   }	   
    if(wind.getId()=="w1")
    {        
            
          
            
        	  for(i=0;i<form1.elements.length;i++){
        	      if(form1[i].type=="text"){
        	           form1[i].value="";
                       padre=$(form1[i]).parent();
                       if($(padre).html().indexOf('error')>=0){
                            
                             padre=form1[i].parentNode;
                             divs=padre.getElementsByTagName('div')						       
        		         	           
        					  for(k=0;k<divs.length;k++){
                                if(divs[k].className==("error-"+form1[i].name))	
        			 	           {
        										    divs[k].parentNode.removeChild(divs[k]);                                          
        					       }
        								 
        									  
        			        }	  
                        
                       }
        	      }
               
        	} 
            
       
            for(i=0;i<form2.elements.length;i++){
        	      if(form2[i].type=="text"){
        	           form2[i].value="";
                       padre=$(form2[i]).parent();
                       if($(padre).html().indexOf('error')>=0){
                            
                             padre=form2[i].parentNode;
                             divs=padre.getElementsByTagName('div')						       
        		         	           
        					  for(k=0;k<divs.length;k++){
                                if(divs[k].className==("error-"+form2[i].name))	
        			 	           {
        										    divs[k].parentNode.removeChild(divs[k]);                                          
        					       }
        								 
        									  
        			        }	  
                        
                       }
        	      }
               
        	} 
        
            for(i=0;i<form3.elements.length;i++){
        	      if(form3[i].type=="text"){
        	           form3[i].value="";
                       padre=$(form3[i]).parent();
                       if($(padre).html().indexOf('error')>=0){
                            
                             padre=form3[i].parentNode;
                             divs=padre.getElementsByTagName('div')						       
        		         	           
        					  for(k=0;k<divs.length;k++){
                                if(divs[k].className==("error-"+form3[i].name))	
        			 	           {
        										    divs[k].parentNode.removeChild(divs[k]);                                          
        					       }
        								 
        									  
        			        }	  
                        
                       }
        	      }
               
        	} 
       
            for(i=0;i<form4.elements.length;i++){
        	      if(form4[i].type=="text"){
        	           form4[i].value="";
                       padre=$(form4[i]).parent();
                       if($(padre).html().indexOf('error')>=0){
                            
                             padre=form4[i].parentNode;
                             divs=padre.getElementsByTagName('div')						       
        		         	           
        					  for(k=0;k<divs.length;k++){
                                if(divs[k].className==("error-"+form4[i].name))	
        			 	           {
        										    divs[k].parentNode.removeChild(divs[k]);                                          
        					       }
        								 
        									  
        			        }	  
                        
                       }
        	      }
               
        	} 
    }
    document.getElementsByTagName("html")[0].style.overflow = "";	
}


showDialog=function(){
  w1.show();
  w1.setModal(true);
   dhxTabbar.setTabActive("PersonalInfo",false);

    
}
showDialogReserva=function(){
	// document.getElementsByTagName("html")[0].style.overflow = "hidden";
     w2.setModal(true);
	 w2.show();
    //total_cost=window.frames[10].document.getElementById('totalcost');
//    
//    
//    total_cost.innerHTML="$ "+price+"(US)"; 
     
    
     
    
     
     
     
         
}
function sendPayment(){
          
         location.href="http://www.spanishschoolecora.com/payment/paypal/paypal.php";
}

      
     

