function validate_login(formbox)
{
	var login_error = 0;
	if (formbox.username.value == '') { formbox.username.style.border = 'solid 1px red'; login_error = 1; }
	else { formbox.username.style.border = 'solid 1px #c0c0c0'; }
	if (formbox.password.value == '') { formbox.password.style.border = 'solid 1px red'; login_error = 1; }
	else { formbox.password.style.border = 'solid 1px #c0c0c0'; }
	if (login_error == 1)	return false;
	else return true;
}

function validate_department(formbox)
{
	var department_error = 0;
	if (formbox.DepartmentName.value == '') { formbox.DepartmentName.style.border = 'solid 1px red'; department_error = 1; }
	else { formbox.DepartmentName.style.border = 'solid 1px #c0c0c0'; }
	if (formbox.DepartmentPrefix.value == '') { formbox.DepartmentPrefix.style.border = 'solid 1px red'; department_error = 1; }
	else { formbox.DepartmentPrefix.style.border = 'solid 1px #c0c0c0'; }
	
	var custom_fields_table = document.getElementById('custom_fields_table');
	// get the number of custom fields
	for(var i=0;i<custom_fields_table.getElementsByTagName('textarea').length;i++)
	{
		if (custom_fields_table.getElementsByTagName('input')[parseInt(i*3+1)].value == '') { custom_fields_table.getElementsByTagName('input')[parseInt(i*3+1)].style.border = 'solid 1px red'; department_error = 1; }
		else { custom_fields_table.getElementsByTagName('input')[parseInt(i*3+1)].style.border = 'solid 1px #c0c0c0'; }	
		if (custom_fields_table.getElementsByTagName('input')[parseInt(i*3+2)].value == '') { custom_fields_table.getElementsByTagName('input')[parseInt(i*3+2)].style.border = 'solid 1px red'; department_error = 1; }
		else { custom_fields_table.getElementsByTagName('input')[parseInt(i*3+2)].style.border = 'solid 1px #c0c0c0'; }
		if ((custom_fields_table.getElementsByTagName('select')[parseInt(i*2)].value == 'select' || custom_fields_table.getElementsByTagName('select')[parseInt(i*2)].value == 'multipleselect' || custom_fields_table.getElementsByTagName('select')[parseInt(i*2)].value == 'checkbox' || custom_fields_table.getElementsByTagName('select')[parseInt(i*2)].value == 'radio') && custom_fields_table.getElementsByTagName('textarea')[i].value == '') { custom_fields_table.getElementsByTagName('textarea')[i].style.border = 'solid 1px red'; department_error = 1; }
		else { custom_fields_table.getElementsByTagName('textarea')[i].style.border = 'solid 1px #c0c0c0'; }
	}
	if (department_error == 1)	return false;
	else return true;
}

function validate_group(formbox)
{
	var group_error = 0;
	if (formbox.GroupName.value == '') { formbox.GroupName.style.border = 'solid 1px red'; group_error = 1; }
	else { formbox.GroupName.style.border = 'solid 1px #c0c0c0'; }
	if (formbox.CanAddTicket.checked == false && formbox.CanUpdateTicket.checked == false && formbox.CanDeleteTicket.checked == false && formbox.CanAnswerTicket.checked == false && formbox.CanDeleteTicketReplies.checked == false && formbox.CanUpdateTicketReplies.checked == false && formbox.CanAssignTickets.checked == false && formbox.CanChangeTicketStatus.checked == false && formbox.CanSeeUnallocatedTickets.checked == false && formbox.CanSeeOtherTickets.checked == false) { group_error = 1; }
	if (group_error == 1)	return false;
	else return true;
}

function validate_ticket(formbox)
{
	var ticket_error = 0;
	if (formbox.DepartmentId.value == '') { document.getElementById('DepartmentIdDiv').style.border = 'solid 1px red'; ticket_error = 1; }
	else { document.getElementById('DepartmentIdDiv').style.border = '0px'; }
	if (formbox.TicketSubject.value == '') { formbox.TicketSubject.style.border = 'solid 1px red'; ticket_error = 1; }
	else { formbox.TicketSubject.style.border = 'solid 1px #c0c0c0'; }
	if (formbox.TicketMessage.value == '') { formbox.TicketMessage.style.border = 'solid 1px red'; ticket_error = 1; }
	else { formbox.TicketMessage.style.border = 'solid 1px #c0c0c0'; }
	if (formbox.TicketPriority.value == '') { formbox.TicketPriority.style.border = 'solid 1px red'; ticket_error = 1; }
	else { formbox.TicketPriority.style.border = 'solid 1px #c0c0c0'; }
	if (formbox.UserId)
	{
		if (formbox.UserId.value == '') { document.getElementById('UserIdDiv').style.border = 'solid 1px red'; ticket_error = 1; }
		else { document.getElementById('UserIdDiv').style.border = '0px'; }
	}
	if (formbox.Email)
	{
		if (formbox.Email.value == '') { formbox.Email.style.border = 'solid 1px red'; ticket_error = 1; }
		else { formbox.Email.style.border = 'solid 1px #c0c0c0'; }
	}
	if (formbox.Fullname)
	{
		if (formbox.Fullname.value == '') { formbox.Fullname.style.border = 'solid 1px red'; ticket_error = 1; }
		else { formbox.Fullname.style.border = 'solid 1px #c0c0c0'; }
	}
	if (ticket_error == 1)	return false;
	else return true;	
}

function validate_reply(formbox)
{
	var reply_error = 0;
	if (formbox.TicketMessage.value == '') { formbox.TicketMessage.style.border = 'solid 1px red'; reply_error = 1; }
	else { formbox.TicketMessage.style.border = 'solid 1px #c0c0c0'; }
	if (reply_error == 1)	return false;
	else return true;	
}

function validate_save_search(formbox)
{
	var save_error = 0;
	if (formbox.SearchName.value == '') { formbox.SearchName.style.border = 'solid 1px red'; save_error = 1; }
	else { formbox.SearchName.style.border = 'solid 1px #c0c0c0'; }
	if (save_error == 1) return false;
	else return true;	
}

function add_custom_field()
{
	if (document.getElementById('no_fields')) document.getElementById('custom_fields_table').deleteRow(document.getElementById('no_fields').rowIndex);
	var custom_fields_table = document.getElementById('custom_fields_table');
	var x=custom_fields_table.insertRow(custom_fields_table.rows.length);
	var currentTime = new Date();
	var t = currentTime.getTime();
	x.id = 'id'+t;
	var y=x.insertCell(0);y.innerHTML='<input type="hidden" name="CustomFieldId[]" value="" /><input type="text" name="CustomFieldName[]" value="" />';y.className = 'tdGrey';
	var y=x.insertCell(1);y.innerHTML='<input type="text" name="CustomFieldLabel[]" value="" />';y.className = 'tdGrey';
	var y=x.insertCell(2);y.innerHTML='<select name="CustomFieldType[]"><option value="textbox">Text box</option><option value="textarea">Text area</option><option value="select">Select list</option><option value="multipleselect">Multiple select list</option><option value="checkbox">Checkbox group</option><option value="radio">Radio group</option><option value="calendar">Calendar</option><option value="calendartime">Calendar and time</option></select>';y.className = 'tdGrey';
	var y=x.insertCell(3);y.innerHTML='<textarea rows="5" cols="20" name="CustomFieldValues[]"></textarea>';y.className = 'tdGrey';
	var y=x.insertCell(4);y.innerHTML='<select name="Required[]"><option value="0">No</option><option value="1">Yes</option></select>';y.className = 'tdGrey';
	var y=x.insertCell(5);y.innerHTML='<a onclick="if (confirm(\'Are you sure ?\')) document.getElementById(\'custom_fields_table\').deleteRow(document.getElementById(\'id'+t+'\').rowIndex)" href="#" class="deleteButton">Delete</a>';
}

function show_custom_fields(what,thetext)
{
	for (var i=0;i<document.getElementsByName('DepartmentIdHolder').length;i++)
	{
		document.getElementsByName('DepartmentIdHolder')[i].parentNode.parentNode.className = 'custom_fields';
		if (document.getElementsByName('DepartmentIdHolder')[i].value == what.value) document.getElementsByName('DepartmentIdHolder')[i].parentNode.parentNode.className='';
	}
	if (document.getElementById('file_upload'))
		document.getElementById('file_upload').className = departments_file_upload[what.value];
	document.getElementById('rst_attachment_ext').innerHTML = thetext + departments_file_upload_ext[what.value];
	
	document.getElementsByName('TicketPriority')[0].value = departments_priority[what.value];
}

function check_all_checkboxes(checkbox, checkboxes)
{
	if (checkbox.checked == true)
		var value=true;
	else
		var value=false;

	if (document.getElementsByName('tickets[]').length > 0)
		for (var i=0;i<=document.getElementsByName('tickets[]').length-1;i++)
			document.getElementsByName('tickets[]')[i].checked = value;
			
	if (value==true)
		document.getElementById('bulkChangeDiv').style.display = 'block';
	else
		document.getElementById('bulkChangeDiv').style.display = 'none';
}

function show_bulk_change()
{
	if (document.getElementsByName('tickets[]').length > 0)
		for (var i=0;i<=document.getElementsByName('tickets[]').length-1;i++)
		{
			var value = document.getElementsByName('tickets[]')[i].checked;
			if (value==true) { document.getElementById('bulkChangeDiv').style.display = 'block'; return; }
			else document.getElementById('bulkChangeDiv').style.display = 'none';
		}
		
}

function filter_tickets()
{
	if (document.getElementsByName('TicketTimeFilter')[0]) document.getElementById('filterContainer').TicketTime.value = document.getElementsByName('TicketTimeFilter')[0].value;
	if (document.getElementsByName('TicketCodeFilter')[0]) document.getElementById('filterContainer').TicketCode.value = document.getElementsByName('TicketCodeFilter')[0].value;
	if (document.getElementsByName('TicketSubjectFilter')[0]) document.getElementById('filterContainer').TicketSubject.value = document.getElementsByName('TicketSubjectFilter')[0].value;
	if (document.getElementsByName('CustomerFullnameFilter')[0]) document.getElementById('filterContainer').CustomerFullname.value = document.getElementsByName('CustomerFullnameFilter')[0].value;
	if (document.getElementsByName('StaffFullnameFilter')[0]) document.getElementById('filterContainer').StaffFullname.value = document.getElementsByName('StaffFullnameFilter')[0].value;
	if (document.getElementsByName('TicketPriorityFilter')[0]) document.getElementById('filterContainer').TicketPriority.value = document.getElementsByName('TicketPriorityFilter')[0].value;
	if (document.getElementsByName('TicketStatusFilter')[0]) document.getElementById('filterContainer').TicketStatus.value = document.getElementsByName('TicketStatusFilter')[0].value;
	document.getElementById('filterContainer').submit();
}

function rst_predefined_search(selectname,itemid)
{
	if (selectname.value != '')
		location.href='index.php?option=com_rstickets&action=predefined_search&id='+selectname.value+(itemid > 0 ? '&Itemid='+itemid : '');
}

function show_settings(settings)
{
	for (var i=0;i<document.getElementsByTagName('form').length;i++)
		if (document.getElementsByTagName('form')[i].id == settings) document.getElementsByTagName('form')[i].style.display = 'block';
			else document.getElementsByTagName('form')[i].style.display = 'none';
}

function check_enter_filter_tickets(e)
{
	var keynum;
	if (window.event)
		keynum = e.keyCode;
	else if (e.which)
		keynum = e.which;
	
	if (keynum == 13)
	{
		filter_tickets();
		return false;
	}
}

function submitbutton(pressbutton) {
	if(pressbutton!='false')submitform(pressbutton);
}

var xmlHttp

function rst_search_users(str,script_path)
{
	//if (str.length==0)
	//{
	//	document.getElementsByName("UserId")[0].innerHTML='';
	//	return;
	//}
	//rst_date = new Date();
	//if (rst_date.getTime() - document.getElementById('rst_last_search').value < 750)
	//	return;
	//document.getElementById('rst_last_search').value = rst_date.getTime();
	
	if (document.getElementById('rst_last_search_value').value == str)
		return;
	document.getElementById('rst_last_search_value').value = str;
	
	document.getElementById('LoadingDiv').style.display = 'inline';
	
	xmlHttp=rst_get_xml_http_object();
	if (xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	var url=script_path+"?option=com_rstickets&action=search_users";
	url=url+"&q="+str;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=rst_state_changed;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function rst_state_changed() 
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if (xmlHttp.responseText.substr(0,7) == '<select' || xmlHttp.responseText.length == 0)
			document.getElementById('UserIdDiv').innerHTML=xmlHttp.responseText;
		else
			document.getElementById('UserIdDiv').innerHTML='<select name="UserId"><option value="">You have timed out &amp; been logged out. Please refresh the page.</option></select>';
		document.getElementById('LoadingDiv').style.display = 'none';
	}
}

function rst_get_xml_http_object()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function rst_change_timezone(hour,gmt)
{
	switch (gmt)
	{
		default:case '':var offset = 0;break;
		case 'Etc/GMT+12':var offset = -12;break;
		case 'Etc/GMT+11':var offset = -11;break;
		case 'Etc/GMT+10':var offset = -10;break;
		case 'Etc/GMT+9':var offset = -9;break;
		case 'Etc/GMT+8':var offset = -8;break;
		case 'Etc/GMT+7':var offset = -7;break;
		case 'Etc/GMT+6':var offset = -6;break;
		case 'Etc/GMT+5':var offset = -5;break;
		case 'Etc/GMT+4':var offset = -4;break;
		case 'Etc/GMT+3':var offset = -3;break;
		case 'Etc/GMT+2':var offset = -2;break;
		case 'Etc/GMT+1':var offset = -1;break;
		case 'Etc/GMT-1':var offset = 1;break;
		case 'Etc/GMT-2':var offset = 2;break;
		case 'Etc/GMT-3':var offset = 3;break;
		case 'Etc/GMT-4':var offset = 4;break;
		case 'Etc/GMT-5':var offset = 5;break;
		case 'Etc/GMT-6':var offset = 6;break;
		case 'Etc/GMT-7':var offset = 7;break;
		case 'Etc/GMT-8':var offset = 8;break;
		case 'Etc/GMT-9':var offset = 9;break;
		case 'Etc/GMT-10':var offset = 10;break;
		case 'Etc/GMT-11':var offset = 11;break;
		case 'Etc/GMT-12':var offset = 12;break;
		case 'Etc/GMT-13':var offset = 13;break;
	}
	var newhour = parseInt(hour) + parseInt(offset);
	if (newhour > 23)
		newhour = newhour - 24;
	if (newhour < 0)
		newhour = 24 + newhour;
	if (newhour < 10)
		newhour = 0 + newhour.toString();
	document.getElementById('rst_timezone_hour').innerHTML = newhour;
}