/*
							File Name :  SITE SCRAPING

	Created on : 7thJuly06
	        by : Deepak Rajpal
	The below given script is being used for scraping
	 -Spicejet
*/

	function scrape(byval){ //Start
	 var obj = eval("document."+byval);
     var origin = null;
     var destination = null;
	 var d_date = null;
	 var spl_d_date = null;
   	 var d_day = null;
	 var d_month = null;
	 var d_year = null;
	 var a_date = null;
	 var spl_a_day = null;
	 var a_day = null;
	 var a_month = null;
	 var a_year = null;
	 var adult = 0;
	 var child = 0;
	 var infant = 0;
	var nom= null
	var sector1_o= null
	var sector1_d= null
	var sector_1_d= null
	var sector_1_m= null
	var sector_1_range= null
	var m1= null
	
	var sector_2_d= null
	var sector_2_m= null
	var sector_2_range= null
	var m2= null
        

		 adult = obj.select1.value;
		 child = obj.select2.value;
		 infant = obj.select3.value;

	 	 //alert("Function called");
	 	 //alert("adult="+adult);
	 	 //alert("child="+child);
	 	 //alert("infant="+infant);
	
		obj.ADULT.value = adult;
		obj.CHILD.value = child;
		obj.INFANT.value = infant;


		 origin = obj.ddlFrom.value;
		 destination = obj.ddlTo.value;

		 //alert(origin);
		 //alert(destination);

        if(obj.rbDomestic.checked==true){ //if domestic      
			if(obj.rbRoundTrip.checked==true){ //Start

				//alert("Round Trip");

				//Depature Date
					d_date = obj.textfield2222.value;
					spl_d_date = d_date.split("/");
					d_day = spl_d_date[0];
					d_month = spl_d_date[1];
					d_year = spl_d_date[2];
				//	alert("d_day="+d_day);
				//Depature Date

			   //Arrival Date
				 a_date = obj.textfield232222.value;
				 spl_a_day = a_date.split("/");
				 a_day = spl_a_day[0];
				 a_month = spl_a_day[1];
				 a_year = spl_a_day[2];
				// alert("a_day="+a_day);
			   //Arrival Date
				
				//alert("Step1");
				nom = 2;  
				sector1_o = "a"+origin; 
				sector1_d = "a"+destination; 

				sector_1_d = d_day; 
				sector_1_m = d_month+d_year; 
				sector_1_range = 000; 
				m1 = d_year+d_month+d_day+origin+destination; 

				
				sector_2_d=a_day
				sector_2_m=a_month+a_year
				sector_2_range=0000
				m2 = a_year+a_month+a_day+destination+origin; 
				

				obj.nom.value=nom;
				obj.sector1_o.value=sector1_o;
				obj.sector1_d.value=sector1_d;

				obj.sector_1_d.value=sector_1_d;
				obj.sector_1_m.value=sector_1_m;
				obj.sector_1_range.value=sector_1_range;
				obj.m1.value=m1;

				obj.sector_2_d.value=sector_2_d;
				obj.sector_2_m.value=sector_2_m;
				obj.sector_2_range.value=sector_2_range;
				obj.m2.value=m2;

			 //obj.action = obj.action="http://book.spicejet.com/skylights/cgi-bin/skylights.cgi?travel_type=on&sector1_o=aBOM&sector1_d=aDEL&sector_1_d=16&sector_1_m=072006&sector_1_range=0000&sector_2_d=16&sector_2_m=072006&sector_2_range=0000&ADULT=1&CHILD=1&INFANT=1&oP=&pT=1ADULT0CHILD&nom=2&pM=0&tc=1&sid=&language=EN&mode=&module=SB&page=SELECT&openjaw_flag=true&m1=20060716BLRDEL&m1DP=00&m1DO=00&m2=20060716DELBLR&m2DP=00&m2DO=00";   
		   }else{ //Mid

				alert("One Way");

			  //Depature Date
				d_date = obj.textfield2222.value;
				spl_d_date = d_date.split("/");
				d_day = spl_d_date[0];
				d_month = spl_d_date[1];
				d_year = spl_d_date[2];
				alert("d_day="+d_day);
			  //Depature Date


				nom = 1;  
				sector1_o = "a"+origin; 
				sector1_d = "a"+destination; 
				sector_1_d = d_day; 
				sector_1_m = d_month+d_year; 
				sector_1_range = 000; 
				m1 = d_year+d_month+d_day+origin+destination; 
				
				
				obj.nom.value=nom;
				obj.sector1_o.value=sector1_o;
				obj.sector1_d.value=sector1_d;
				obj.sector_1_d.value=sector_1_d;
				obj.sector_1_m.value=sector_1_m;
				obj.sector_1_range.value=sector_1_range;
				obj.m1.value=m1;
				obj.submit();

		  } //End
		} //if domestic      
	}//Function End 	

	//calling funtion from the form
	function getFlights(byval)
	{
		var obj = eval("document."+byval);

		//checking if current page is index page or flight search page
		//as only these 2 pages have Domestic & International radio buttons
		if(byval=="Form_FlightsIndex" || byval=="Form_FlightSearch")
		{
			if(obj.rbDomestic.checked==true)
			{
				getSpiceJet(byval);		//Spice Jet flights
				getGoAir(byval);			//Go Air flights
				getAirDeccan(byval);
				return true;
			}
		}
		else
		{
			if(obj.hidRoute.value=="Domestic")
			{
				getSpiceJet(byval);		//Spice Jet flights
				getGoAir(byval);			//Go Air flights
				getAirDeccan(byval);
				return true;
			}
		}
	}//end of function getFlights

	//getting Spice Jet flights
	function getSpiceJet(byval)
	{
		var obj = eval("document."+byval);
		var origin = null;
		var destination = null;
		var d_date = null;
		var spl_d_date = null;
		var d_day = null;
		var d_month = null;
		var d_year = null;
		var a_date = null;
		var spl_a_day = null;
		var a_day = null;
		var a_month = null;
		var a_year = null;
		var adult = 0;
		var child = 0;
		var infant = 0;
		var nom= null
		var sector1_o= null
		var sector1_d= null

		var sector_1_d= null
		var sector_1_m= null
		var sector_1_range= null
		var m1= null

		var sector_2_d= null
		var sector_2_m= null
		var sector_2_range= null
		var m2= null

		var url=null

	   	 adult = obj.ddlAdults.value;
		 child = obj.ddlChildren.value;
		 infant = obj.ddlInfants.value;

		 origin = obj.ddlFrom.value;
		 
		 spl_ori= origin.split(" - ");
         origin = spl_ori[1];

         

		 destination = obj.ddlTo.value;
		 spl_dest = destination.split(" - ");
	     destination = spl_dest[1];

			if(obj.rbRoundTrip.checked==true){ //Start

				//Depature Date
					d_date = obj.txtDepDate.value;
					spl_d_date = d_date.split("/");
					d_day = spl_d_date[0];
					d_month = spl_d_date[1];
					d_year = spl_d_date[2];
				//	alert("d_day="+d_day);
				//Depature Date

			   //Arrival Date
				 a_date = obj.txtRetDate.value;
				 spl_a_day = a_date.split("/");
				 a_day = spl_a_day[0];
				 a_month = spl_a_day[1];
				 a_year = spl_a_day[2];
				// alert("a_day="+a_day);
			   //Arrival Date

			   //alert("Step1");
				nom = 2;  
				sector1_o = "a"+origin; 
				sector1_d = "a"+destination; 

				sector_1_d = d_day; 
				sector_1_m = d_month+d_year; 
				sector_1_range = 000; 
				m1 = d_year+d_month+d_day+origin+destination; 

				
				sector_2_d=a_day
				sector_2_m=a_month+a_year
				sector_2_range=0000
				m2 = a_year+a_month+a_day+destination+origin; 

								
			 }
			 else
			 {
				//Depature Date
					d_date = obj.txtDepDate.value;
					spl_d_date = d_date.split("/");
					d_day = spl_d_date[0];
					d_month = spl_d_date[1];
					d_year = spl_d_date[2];
				//	alert("d_day="+d_day);
				//Depature Date

				nom = 1;  
				sector1_o = "a"+origin; 
				sector1_d = "a"+destination; 
				sector_1_d = d_day; 
				sector_1_m = d_month+d_year; 
				sector_1_range = 000; 
				m1 = d_year+d_month+d_day+origin+destination; 

				sector_2_d="";
				sector_2_m="";
				sector_2_range=0000;
				m2 = "";
			 }//end
			 url = "http://book.spicejet.com/skylights/cgi-bin/skylights.cgi?travel_type=on&sector1_o="+sector1_o+"&sector1_d="+sector1_d+"&sector_1_d="+sector_1_d+"&sector_1_m="+sector_1_m+"&sector_1_range=0000&sector_2_d="+sector_2_d+"&sector_2_m="+sector_2_m+"&sector_2_range=0000&ADULT="+adult+"&CHILD="+child+"&INFANT="+infant+"&oP=&pT=&nom="+nom+"&pM=0&tc=1&sid=&language=EN&mode=&module=SB&page=SELECT&openjaw_flag=true&m1="+m1+"&m1DP=00&m1DO=00&m2="+m2+"&m2DP=00&m2DO=00";
			 obj.sjurl.value=url;
		 
	}//end of function getSpiceJet

	//getting AirDeccan flights
	function getAirDeccan(byval)
	{
		var obj = eval("document."+byval);
		var url="";				//stores the url for Air Deccan

		var trip="R";		//getting oneway or roundtrip

		//getting Departure Date
		var departDate = obj.txtDepDate.value;
		var spl_depDate = departDate.split("/");
		var depDay = spl_depDate[0];
		var depMonth = spl_depDate[1];
		var depYear = spl_depDate[2];
		var depart_date = depMonth + "/" + depDay + "/" + depYear; 

		//getting Return Date
		var returnDate = obj.txtRetDate.value;
		var spl_retDate = returnDate.split("/");
		var retDay = spl_retDate[0];
		var retMonth = spl_retDate[1];
		var retYear = spl_retDate[2];
		var return_date = retMonth + "/" + retDay + "/" + retYear;

		if(obj.rbRoundTrip.checked==false){
			trip="O";
			returnDate=departDate;
			return_date="";
		}//end of if round trip

		//getting passengers
		var adults = obj.ddlAdults.value;
		var children = obj.ddlChildren.value;	
		var infants = obj.ddlInfants.value;

		var origin=obj.ddlFrom.value.split(' - ')[1];
		var dest=obj.ddlTo.value.split(' - ')[1];
		
		url="flagfare="+trip+"&h_random=ZKS&DepartDate="+departDate+"&ReturnDate="+returnDate+"&str_class=A&depart_date="+depart_date+"&return_date="+return_date+"&schedule_period=3888000000&booking_period=31536000000&current_date=03/03/2006&max_seats=10&sector_diff=14400000&search_days=365&cmb_originCity="+origin+"&cmb_destCity="+dest+"&txt_adult="+adults+"&txt_child="+children+"&txt_infant="+infants;
				
		obj.adurl.value=url;
		return false;
	}//end of function getAirDeccan

	function getGoAir(byval)	//getting GoAir flights
	{
		var obj = eval("document."+byval);
		var url="";			//stores the url for Go Air
		
		var trip="round";		//getting oneway or roundtrip
		
		//getting Departure Date
		var depDate = obj.txtDepDate.value;
		var spl_depDate = depDate.split("/");
		var depDay = removeLeading0(spl_depDate[0]);
		var depMonth = removeLeading0(spl_depDate[1]);
		var depYear = spl_depDate[2];
		var depMonthYear=depMonth+","+depYear;

		//getting Return Date
		var retDate = obj.txtRetDate.value;
		var spl_retDate = retDate.split("/");
		var retDay = removeLeading0(spl_retDate[0]);
		var retMonth = removeLeading0(spl_retDate[1]);
		var retYear = spl_retDate[2];
		var retMonthYear=retMonth+","+retYear;

		if(obj.rbRoundTrip.checked==false){
			trip="one";
			retDay=depDay;
			retMonthYear=depMonthYear;
		}//end of if round trip

		//getting passengers
		var adult = eval(parseInt(obj.ddlAdults.value) + parseInt(obj.ddlChildren.value));	//children are counted as adults
		var infant = obj.ddlInfants.value;

		var origin=obj.ddlFrom.value.split(' - ')[1];
		var originCode=getOrigin(origin);
		origin = origin + originCode;

		if(originCode==0)		//city not found
		{
			url="";
		}
		else
		{
			var dest=obj.ddlTo.value.split(' - ')[1];
			var destCode=getDestination(dest,originCode);
			dest = dest + destCode;
			if(destCode==0)		//city not found
			{
				url;
			}
			else
			{
				url="__EVENTTARGET=BE_SearchBar11:lnkSearch&__EVENTARGUMENT=&rbRound="+trip+"&hidRound2="+trip+"&hidRound="+trip+"&radio="+trip+"&hidRemote=&cboOrigin="+origin+"&hidOrigin="+originCode+"&cboDestination="+dest+"&hidDestination="+destCode+"&cbodepday="+depDay+"&cbodepdayyear="+depMonthYear+"&hidRetDate=&cboretday="+retDay+"&cbomonthyearret="+retMonthYear+"&cboAdult="+adult+"&cboInfant="+infant+"&cboChild=0&cboClass=3&cboNationality=IN";
			}
		}
		obj.gaurl.value=url;
	}//end of function getGoAir //getting GoAir

	//removes the leading 0 from dates  //for Go Air
	function removeLeading0(strDate)
	{
		if(strDate.substring(0,1) == "0")
		{
			strDate = strDate.substring(1,2);
		}
		return strDate;
	}//end of function removeLeading0

	//gets code for the selected departure city			//for Go Air
	function getOrigin(origin)
	{
		var orgOptions="AMD:Ahmedabad,BLR:Bangalore,MAA:Chennai,COK:Cochin,CJB:Coimbatore,DEL:Delhi,GOI:Goa,HYD:Hyderabad,JAI:Jaipur,IXJ:Jammu,BOM:Mumbai,PNQ:Pune,SXR:Srinagar";
		var opts = orgOptions.split(',');
		var i=0;
		for(i=0;i<opts.length;i++)
		{
			if(opts[i].substring(0,3)==origin)
			{
				break;
			}
		}
		if(i==opts.length)		//selected city not found
		{
			i=0;
			return i;
		}
		else					//selected city found
		{
			return i+1;			//as city codes for Go Air start with 1
		}
	}//end of function getOrigin(origin)

	//gets code for selected destination city as per selected departure city	//for Go Air
	function getDestination(dest,originCode)
	{
		//dest options as per corresponding dep options
		var destOptions="BOM:Mumbai,HYD:Hyderabad,DEL:Delhi-HYD:Hyderabad-PNQ:Pune,BOM:Mumbai,BOM:Mumbai,MAA:Chennai-BOM:Mumbai,BOM:Mumbai,BLR:Bangalore-MAA:Chennai,BOM:Mumbai,SXR:Srinagar,AMD:Ahmedabad-COK:Cochin-CJB:Coimbatore-DEL:Delhi-GOI:Goa-JAI:Jaipur-SXR:Srinagar,MAA:Chennai-IXJ:Jammu,BOM:Mumbai";
		var opts=destOptions.split(',')[originCode-1].split('-');		//getting the dest option as per dep
					
		var i=0;
		for(i=0;i<opts.length;i++)		//getting particular city
		{
			if(opts[i].substring(0,3)==dest)
			{
				break;
			}
		}
		if(i==opts.length)		//selected city not found
		{
			i=0;
			return i;
		}
		else					//selected city found
		{
			return i+1;			//as city codes for Go Air start with 1
		}
	}//end of function getDestination(dest)
