/**
 * @author Amit Jain
 */

var settingObj=null; 
function tripSetting()
{
	if(window.ActiveXObject)
	{
		settingObj = new ActiveXObject('Microsoft.XMLHTTP');		
	}
	else
	{
		if (window.XMLHttpRequest) 
		{ 
			//alert("It is Mozilla XMLHttpRequest");
			settingObj = new XMLHttpRequest();						
		} 
	}
	if(settingObj)
	{		
		try
		{
			settingObj.onreadystatechange=putTripInfo;
		}
		catch(ex)
		{
			alert('Exception in state change.')
		}
			
		url = '/servlet/GetTripInfo?&randomNum='+Math.random()*Math.random();
		settingObj.open('GET',url,false);
		//alert(url);	
		try
		{								
			if(window.XMLHttpRequest)
			{					
				settingObj.send();
			}
			else
			{							
				settingObj.send("");
			}				
		}
		catch(ajaxEx)
		{
			alert(ajaxEx);
		}
	}
	else
	{
		alert('Object not created');
	}
}
function putTripInfo()
{	
	if(settingObj.readyState==4)
	{		
		if(settingObj.status==200)
		{							
			//alert(settingObj.responseText);
			if(!(settingObj.responseText==''||settingObj.responseText=='session lost'))
			{
				//alert(settingObj.responseText);
				disTripSetInfo(settingObj.responseText);
			}
			else
			{
				alert('Session is lost.');
			}
		}
	}		
				
}
function disTripSetInfo()
{
	var output = settingObj.responseText.split('$');
	for(var i=0;i<output.length;i++)
	{
		//alert(output[i]);
		if(output[i]=="NA")
		{
			return;
		}
	}
	//tripSetBName tripSetBasketDesc tripSetShare
	document.getElementById('tripSetBName').value=output[0];
	document.getElementById('tripSetBasketDesc').value=output[1];
	document.getElementById('tripSetOrigin').innerHTML=output[3];
	if(output[2]=='0')
	{
		document.getElementById('priTripSet').checked=true;	
	}
	else
	{
		document.getElementById('pubTripSet').checked=true;
	}
	openTripSetting();
}

function closeTripSetting()
{
	document.getElementById('tripSetting').style.display='none';
	document.getElementById('tripSetBName').value="";
	document.getElementById('tripSetBasketDesc').value="";
	document.getElementById('tripSetOrigin').innerHTML="";
	document.getElementById('priTripSet').checked=false;
	document.getElementById('pubTripSet').checked=false;
} 

function openTripSetting()
{
	document.getElementById('tripSetting').style.display='block';
}

var updateTripSettingObj = null;
function updateTripSetting()
{
	var tripName = document.getElementById('tripSetBName').value;
	var tripDesc = document.getElementById('tripSetBasketDesc').value;
	var priType = document.getElementById('priTripSet');
	var pubType = document.getElementById('pubTripSet');
	var shareType;
	if(priType.checked)
	{
		shareType = 0;
	}
	else
	{
		shareType = 1;	
	}
	
	/*try
	{
		alert('123:-> '+removeSpace(tripName));	
	}
	catch(ex)
	{
		alert(ex);
	}*/
	if(removeSpaces(tripName)=='')
	{
		alert('You have not entered the Trip Name.');
		return;
	}
	else
	{
		//alert('else1');
		if(tripName.indexOf('$')>=0)
		{
			alert('Please do not use $ sign in Trip Name.');
			return;
		}
		if(tripName.indexOf('^')>=0)
		{
			alert('Please do not use ^ sign in Trip Name.');
			return;
		}
		if(tripName.indexOf('"')>=0)
		{
			alert('Please do not use \" sign in Trip Name.');
			return;
		}
		if(tripName.indexOf('\'')>=0)
		{
			alert('Please do not use \' sign in Trip Name.');
			return;
		}
		if(tripName.indexOf('\;')>=0)
		{
			alert('Please do not use \; sign in Trip Name.');
			return;
		}
	}
	if(removeSpaces(tripDesc)=='')
	{
		alert('You have not entered Trip Description.');
		return;
	}
	else
	{
		//alert('else2');
		if(tripDesc.indexOf('$')>=0)
		{
			alert('Please do not use $ sign in Trip Description.');
			return;
		}
		if(tripDesc.indexOf('^')>=0)
		{
			alert('Please do not use ^ sign in Trip Description.');
			return;
		}
		if(tripDesc.indexOf('"')>=0)
		{
			alert('Please do not use \" sign in Trip Description.');
			return;
		}
		if(tripDesc.indexOf('\'')>=0)
		{
			alert('Please do not use \' sign in Trip Description.');
			return;
		}
		if(tripDesc.indexOf('\;')>=0)
		{
			alert('Please do not use \; sign in Trip Description.');
			return;
		}
	}
		
	var answer = confirm('Do you want to update value');
	if (!answer)
		return;
		
	if(window.ActiveXObject)
	{
		updateTripSettingObj = new ActiveXObject('Microsoft.XMLHTTP');		
	}
	else
	{
		if (window.XMLHttpRequest) 
		{ 
			//alert("It is Mozilla XMLHttpRequest");
			updateTripSettingObj = new XMLHttpRequest();						
		} 
	}
	if(updateTripSettingObj)
	{		
		try
		{
			updateTripSettingObj.onreadystatechange=updateTripSetInfo;
		}
		catch(ex)
		{
			alert('Exception in state change.')
		}
			
		url = '/servlet/UpdateTripSetting?tripName='+tripName+'&tripDesc='+tripDesc+'&shareType='+shareType+'randomNum='+Math.random()*Math.random();
		updateTripSettingObj.open('GET',url,false);
		//alert(url);	
		try
		{								
			if(window.XMLHttpRequest)
			{					
				updateTripSettingObj.send();
			}
			else
			{							
				updateTripSettingObj.send("");
			}				
		}
		catch(ajaxEx)
		{
			alert(ajaxEx);
		}
	}
	else
	{
		alert('Object not created.');
	}
}
function updateTripSetInfo()
{
	if(updateTripSettingObj.readyState==4)
	{		
		if(updateTripSettingObj.status==200)
		{							
			//alert(settingObj.responseText);
			res = updateTripSettingObj.responseText
			if(res=='yes')
			{
				alert('Successfully Updated');
				closeTripSetting();				
				getAllPrivateTrip();
				getBasket();
			}
			else
			{
				alert('Error! Pleae try after some time.');
			}
		}
	}
}