// 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);
						}
		});
}


function GetZoneCountry(zId)
{
	document.getElementById("submitSearch").disabled = true;
	var cId = document.getElementById('country');
	if(zId != 0)
	{
		resetcountry(cId);
		resetregion(document.getElementById('regionId'));
		resetcity(document.getElementById('cityId'));
		addOption(cId,'Loding..',0,0);
		new Ajax.Request('package.php?action=countryList&zoneId='+zId,
		{
			asynchronous: false,
			method: 'get',
			onSuccess: function(t) { var r = t.responseText; var countryObj = eval('(' + r + ')'); len = countryObj.length; addOption(cId,'All Countries',0,0); 
									for(i= 0; i < len; i++ ){ addOption(cId,countryObj[i].name,countryObj[i].id,i+1); } },
			onError: function(t) { alert("Unable to get response from server. Please try again "); }
		});
	}else{
		resetcountry(document.getElementById('country'));
		resetregion(document.getElementById('regionId'));
		resetcity(document.getElementById('cityId'));
	}
	document.getElementById("submitSearch").disabled = false;
}

function GetCountryRegions(countryId)
{
	document.getElementById("submitSearch").disabled = true;
	var cId = document.getElementById('regionId');
	if(countryId != 0)
	{
		resetregion(cId);
		resetcity(document.getElementById('cityId'));
		addOption(cId,'Loding..',0,0);
		new Ajax.Request('package.php?action=regionList&countryId='+countryId,
		{
			asynchronous: false,
			method: 'get',
			onSuccess: function(t) { var r = t.responseText; var regionObj = eval('(' + r + ')'); len = regionObj.length; addOption(cId,'All Regions',0,0); 
									for(i= 0; i < len; i++ ){ addOption(cId,regionObj[i].name,regionObj[i].id,i+1); } },
			onError: function(t) { alert("Unable to get response from server. Please try again "); }
		});
	}else{
		resetregion(document.getElementById('regionId'));
		resetcity(document.getElementById('cityId'));
	}
	document.getElementById("submitSearch").disabled = false;
}

function GetRegionCities(rId)
{
	document.getElementById("submitSearch").disabled = true;
	var cId = document.getElementById('cityId');
	if(rId != 0)
	{
		resetcity(cId);
		addOption(cId,'Loding..',0,0);
		new Ajax.Request('package.php?action=cityList&regionId='+rId,
		{
			asynchronous: false,
			method: 'get',
			onSuccess: function(t) { var r = t.responseText; var cityObj = eval('(' + r + ')'); len = cityObj.length; addOption(cId,'All Cities',0,0); 
									for(i= 0; i < len; i++ ){ addOption(cId,cityObj[i].name,cityObj[i].id,i+1); } },
			onError: function(t) { alert("Unable to get response from server. Please try again "); }
		});
	}else{
		resetcity(document.getElementById('cityId'));
	}
	document.getElementById("submitSearch").disabled = false;
}

function resetcountry(cId)
{
	RemoveAll(cId);
	addOption(cId,'All Countries',0,0);
} 

function resetregion(rId)
{
	RemoveAll(rId);
	addOption(rId,'All Regions',0,0);
}

function resetcity(cityId)
{
	RemoveAll(cityId);
	addOption(cityId,'All Cities',0,0);
}
