var rse_t;
var rse_old_value;

function rse_stop_search_locations()
{
	clearTimeout(rse_t);
}

function rse_search_locations(string)
{
	if (string == rse_old_value) return;
	rse_t = setTimeout( function() {
		selbox = document.getElementById('rse_locations');
		var url = 'index.php?option=com_rsevents';
		var params = 'task=location_filter&tmpl=component&string='+ string +'&randomTime='+Math.random();
		xml=buildXmlHttp();

		xml.open('POST',url,true);
		

		xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xml.setRequestHeader("Content-length", params.length);
		xml.setRequestHeader("Connection", "close");
		
		xml.onreadystatechange=function()
		{
			if(xml.readyState==4)
			{		
				document.getElementById('rse_hideme').style.display = 'none';
				selbox.innerHTML  = xml.responseText;
			}
		}
		xml.send(params);
		rse_old_value = string;
		document.getElementById('rse_hideme').style.display = '';
	}, 1000);
}


function rse_refresh_locations(root,selectId)
{
	selbox = document.getElementById(selectId);
	selbox.length = 1;
	var url = 'index.php?option=com_rsevents';
	var params = 'view=locations&layout=refreshlocations&randomTime='+Math.random();
	xml=buildXmlHttp();

	xml.open('POST',root + url,true);
	

	xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xml.setRequestHeader("Content-length", params.length);
	xml.setRequestHeader("Connection", "close");
	
	xml.onreadystatechange=function()
    {
		if(xml.readyState==4)
		{
			lines = xml.responseText.split("\n");
			for(i=0;i<lines.length;i++)
			{
				row = lines[i].split('|');
				selbox.options[i+1] = new Option(row[1],row[0]);
				
			}
		}
    }
	xml.send(params);
}

function rse_import_contacts()
{
	str = '';
	checks = document.getElementsByName('importedContacts[]');
	for(i=0;i<checks.length;i++)
	{
		if(checks[i].checked == true)
		{
			str += checks[i].value + "\n";
		}
	}
	document.getElementById('rsevents-emails').value = str;
}

function rsevents_invite_validation()
{
	var form = document.adminForm;
	var ret = true;
	
	if(form.emails.value=='') { form.emails.className += ' rserror'; ret=false; } else { form.emails.className = ' rsevents-message';  }	
	
	return ret;
}

function rsevents_import_validation()
{
	var form = document.adminForm;
	var ret = true;
	
	if(form.username.value=='') { form.username.className += ' rserror'; ret=false; } else { form.username.className = ' rsevents-message';  }	
	if(form.password.value=='') { form.password.className += ' rserror'; ret=false; } else { form.password.className = ' rsevents-message';  }	
	
	if(document.getElementById('typename').value == 'gmail')
	{
		document.getElementById('task').value = 'importgmail';
	}else{
		document.getElementById('task').value = 'importyahoo';
	}
	
	return ret;
}

function rsevents_send_validation()
{
	var form = document.adminForm;
	var ret = true;
	
	if(form.subject.value=='') { form.subject.className += ' rserror'; ret=false; } else { form.subject.className = ' rsevents-message';  }	
	
	return ret;
}

function rse_change_carrier(carrier)
{
	if (carrier == 'gmail')
	{
		document.getElementById('rsevents-carrier').innerHTML = '@gmail.com';
		document.getElementById('typename').value = 'gmail';
		document.getElementById('task').value = 'importgmail';
	}
	if (carrier == 'yahoo')
	{
		document.getElementById('rsevents-carrier').innerHTML = '@yahoo.com';
		document.getElementById('typename').value = 'yahoo';
		document.getElementById('task').value = 'importyahoo';
	}
	
}


function buildXmlHttp()
{
	var xmlHttp;
	try
	{
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

function rse_change_ticket(IdTicket)
	{
		var selbox = document.getElementById('IdTicket');
		//first lets hide all the ticket descriptions
		for(i =0;i<selbox.options.length;i++)
		{
			document.getElementById('rsevents_ticket_' + selbox.options[i].value).style.display='none';
		}
		document.getElementById('rsevents_ticket_' + IdTicket).style.display='inline';
	}
	
function rse_hidePayment(id,type)
{
	if (type == 0)
	selbox = document.getElementById('rse_numtickets');
	else if (type == 1)
	selbox = document.getElementById('RSEventsNumTickets');
	
	xml2=buildXmlHttp();
	xml2.onreadystatechange=function()
    {
		if(xml2.readyState==4)
		{
			txt = xml2.responseText;
			txt = txt.split("\n");
			ticketPrice = txt[0];
			maxTickets = txt[1];
			
			if(ticketPrice == '0') 
			{	
				if(type == 0) 
				{
					document.getElementById('rse_hidePayment').style.display = 'none' ;
					document.getElementById('numtick').style.display = 'none' ;
				} else if (type == 1)
				{
					selbox.options.length = 0;
					for(i=1;i<=maxTickets;i++)
					{
					selbox.options[i-1] = new Option(i,i);
					}
				}
			}
			else 
			{
				if (type == 0)
				{
					document.getElementById('rse_hidePayment').style.display = '' ;
					document.getElementById('numtick').style.display = '' ;
				} else if (type == 1)
				{
					document.getElementById('RSEventsNumTickets').style.display = '';
				}
				selbox.options.length = 0;
				for(i=1;i<=maxTickets;i++)
				{
					selbox.options[i-1] = new Option(i,i);
					
				}
			}
		}
    }
	xml2.open('GET', 'index.php?option=com_rsevents&task=checkvalue&cid='+id+'&randomTime='+Math.random(),true);
	xml2.send(null);
}

function rse_get_payment()
{
	var payments = document.getElementsByName('payment');
	var checkedpayment = '';
	for (var i=0;i<payments.length;i++)
		if (payments[i].checked)
			checkedpayment = payments[i].value;
	return checkedpayment;
}


	