// JavaScript Document
/* ************************************ */
//  Created By praveen
//praveen@icreon.net
//
/**************************************/
/* 
Folowing function remove all the options from a select dd
parameter: select object
*/

function RemoveAll(select)
{

select.options.length = 0;

}

/* 
Following function  find the index  of a option in the select dd with the specified value
parameter1: select object
parameter1: value of the required option
*/

function findIndexOf(select,OptionValue){

var index=-1;
for(i=0; i<select.length; i++)
    {
    if(select.options[i].value==OptionValue)
       {
         index=i;
		 break;
	   }
	}
return index;
}

/* 
Following function  slect an option in the select dd with the specified value
parameter1: select object
parameter1: value of the option to be select
*/

function selectOptionValue(select,OptionValue)
{

  index=findIndexOf(select,OptionValue);
  if(index!=-1)
   {
       select.options[index].selected = true;

   }else{
   // alert('OptionValue='+OptionValue+' Not found');
   }

}

/* 
Following function Add an option In the specified select dd
parameter1: select object
parameter1: option Text 
parameter1: option Value 
parameter1: pos index 
*/

function  addOption(select,OptionText,OptionValue,pos){

 select.options[pos] = new Option(OptionText, OptionValue);
}


/* 
Following function  Populate the State Input Box with values returned by Ajax
Depends On: Prototype
Assumes:Ajax Respone must contain an Associative Array with id a State ID And Value as sate name
Param1:Country id 
Param1:Name of state Input Tag
Param1:default selected option value 
*/

function getStateDD(MA_CountryId,select_dd,selected_value,folderLevel)
{

var select_id=document.getElementById(select_dd);
if(folderLevel == undefined)
	var	folderLevel = "";

new Ajax.Request(folderLevel+'cityState.php?action=getstate&MA_CountryId='+MA_CountryId, {

  method: 'get',onSuccess:function (transport){

 var result=transport.responseText;
 pos=0;
 eval(result);
	RemoveAll(select_id);

for(i in states) {  

  addOption(select_id,states[i],i,pos++);
}
selectOptionValue(select_id,selected_value);
}
 
});


}


/* 
Following function  Populate the City Input Box with values returned by Ajax
Depends On: Prototype
Assumes:Ajax Respone must contain an Associative Array with id a City ID And Value as City name
Param1:State ID
Param1:Name of City Input Tag
Param1:default selected option value 
*/

function getCityDD(MA_StateId,select_dd,selected_value,folderLevel)
{


    if(MA_StateId != '0' && MA_StateId != 's' && MA_StateId != '')
	{
	
		  var select_id=document.getElementById(select_dd);

		if(folderLevel == undefined)
			var	folderLevel = "";

			new Ajax.Request(folderLevel+'cityState.php?action=getcity&MA_StateId='+MA_StateId, {
			
			  method: 'get',onSuccess:function (transport){
			//alert(select_dd);
			 var result=transport.responseText;
			 pos=0;
			// alert(result);
			 eval(result);
			RemoveAll(select_id);
			for(i in cities) {  
			  addOption(select_id,cities[i],i,pos++);
			}
			selectOptionValue(select_id,selected_value);
			}
			 
			});
	}

}

/*
Following function  Populate the City Input Box with values returned by Ajax
Depends On: Prototype
Assumes:Ajax Respone must contain an Associative Array with id a City ID And Value as City name
Param1:State ID
Param1:Name of City Input Tag
Param1:default selected option value
*/

function getTotalCityDD(MA_CountryId,select_dd,selected_value)
{
    var select_id=document.getElementById(select_dd);
    new Ajax.Request('cityState.php?action=getTotalCity&MA_CountryId='+MA_CountryId, {

	  method: 'get',onSuccess:function (transport){
	//alert(select_dd);
	 var result=transport.responseText;
	 pos=0;
	// alert(result);
	 eval(result);
	RemoveAll(select_id);
	for(i in cities) {
	  addOption(select_id,cities[i],i,pos++);
	}
	
	selectOptionValue(select_id,selected_value);
	}

	});

}

function getTotaluserCityDD(MA_CountryId,select_dd,selected_value)
{
    var select_id=document.getElementById(select_dd);
	
    new Ajax.Request(folderLevel+'cityState.php?action=getTotalCity&MA_CountryId='+MA_CountryId, {

	  method: 'get',onSuccess:function (transport){
	//alert(select_dd);
	 var result=transport.responseText;
	 pos=0;
	//alert(result);
	 eval(result);
	RemoveAll(select_id);
	for(i in cities) {
	  addOption(select_id,cities[i],i,pos++);
	}
	addOption(select_id,'other',0,pos++);
	selectOptionValue(select_id,selected_value);
	}

	});

}

function getCountryCityDD(MA_CountryId,select_dd,selected_value,folderLevel)
{
  var select_id=document.getElementById(select_dd);

  if(folderLevel == undefined)
	var	folderLevel = "";
	new Ajax.Request(folderLevel+'cityState.php?action=getcountrycity&MA_CountryId='+MA_CountryId, {
		asynchronous: false,	
	  method: 'get',onSuccess:function (transport){

	 var result=transport.responseText;
	 pos=0;
	 eval(result);
RemoveAll(select_id);

	for(i in cities) {  
	  addOption(select_id,cities[i],i,pos++);
	}
	selectOptionValue(select_id,selected_value);
	}
	 
	});

}


function getTravelArrangers(id,select_dd,selected_value)
{

var select_id=document.getElementById(select_dd);


new Ajax.Request('../getTATO.php?id='+id, {

  method: 'get',onSuccess:function (transport){

 var result=transport.responseText;

 pos=0;
 eval(result);
RemoveAll(select_id);

for(i in travelArrangers) {  
  addOption(select_id,travelArrangers[i],i,pos++);
}
selectOptionValue(select_id,selected_value);
}
 
});


}

function addOrderComments(pnr,comments)
{
	
	var opt = {
    // Use POST
    method: 'get',
    // Send this lovely data

    // Handle successful response
    onSuccess: function(t) {
       },
    // Handle 404
    on404: function(t) {
        alert('Error 404: location "' + t.statusText + '" was not found.');
    },
    // Handle other errors
    onFailure: function(t) {
        alert('Error ' + t.status + ' -- ' + t.statusText);
    }
}


var data = 'status=1&pnr='+escape(pnr)+'&comments='+escape(comments);
new Ajax.Request('../saveOrderComments.php?'+data, opt);
}

function editOrderComments(id,comments,status)
{

	var opt = {
    // Use POST
    method: 'get',
    // Send this lovely data
    // Handle successful response
    onSuccess: function(t) {
    },
    // Handle 404
    on404: function(t) {
        alert('Error 404: location "' + t.statusText + '" was not found.');
    },
    // Handle other errors
    onFailure: function(t) {
        alert('Error ' + t.status + ' -- ' + t.statusText);
    }
}

var data = 'status='+status+'&id='+id+'&comments='+escape(comments);

new Ajax.Request('../saveOrderComments.php?'+data, opt);
}


function getpickupdropoff(cityvalue,type,pickdrop,selected_value)
{
	var select_id=document.getElementById(pickdrop);
	RemoveAll(select_id);
	new Ajax.Request('cityState.php?action=getpickdrop&Ma_cityId='+cityvalue+'&type='+type,
		{
			asynchronous: false,
			method: 'get',
			onSuccess:function (transport)
						{
							var result=transport.responseText;
							pos=0;
							eval(result);
							for(i in locations)
							{
								addOption(select_id,locations[i],i,pos++);
							}
							selectOptionValue(select_id,selected_value);
						}
		});
}
