var errors;

function init()
{
	//set flag in input to say js in enabled
	jsField = document.getElementById('jsEnabled');
	if(jsField) { jsField.value=true; }
}

function clearErrorMessages() {
	errors=false;
	document.getElementById('error_yourname').style.display='none';
	document.getElementById('error_emailaddress').style.display='none';
	document.getElementById('error_enquiry').style.display='none';
}

function validate_email(field, errormessageid) {
	var emailerror;
	var at="@";
	var dot=".";

	str = field.value;

	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);

	if (str.indexOf(at)==-1){
		emailerror = true;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		emailerror = true;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		emailerror = true;
	}
	if (str.indexOf(at,(lat+1))!=-1){
		emailerror = true;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		emailerror = true;
	}
	if (str.indexOf(dot,(lat+2))==-1){
		emailerror = true;
	}
	if (str.indexOf(" ")!=-1){
		emailerror = true;
	}
	if (emailerror) {
		document.getElementById(errormessageid).style.display='block';
		field.focus();
		errors=true;
	}
}

function validate_required(field, errormessageid) {
	with (field) {
		if (value==null || value=="") {
			document.getElementById(errormessageid).style.display='block';
			field.focus();
			errors=true;
		}
	}
}

function checkForm(form) {
	clearErrorMessages();
	with (form) {
		validate_required(yourname,'error_yourname');
		validate_email(emailaddress,'error_emailaddress');
		validate_required(enquiry,'error_enquiry');
	}
	return !errors;
}




function openCompletedReport(id) {
	var url = '/client/viewcompletedreport.php?id=' + id;
	completedReport = window.open(url,'completedReport','width=825,height=500,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
	completedReport.focus();
	return false;
}

function submitDateSelection(s, range)
{
	togglePleaseWait(s);
	
	var rangeType = document.getElementById('rangeType');
	if(range=='dateRange')
	{
		rangeType.value = 'date';
	}
	else if(range=='period')
	{
		rangeType.value = 'period';
	}
}

function toggleDisabledAllInputButtons(bool)
{
	for (var x=0;x<document.forms.length;x++)
	{
		for (var i=0;i<document.forms[x].elements.length;i++)
		{
			var e = document.forms[x].elements[i];
			if (e.type=="submit")
			{
				e.disabled=bool;
			}
		}
	}
}

function togglePleaseWait(s)
{	
	var indicatorDiv = document.getElementById('pleaseWaitDiv');
	var table = document.getElementById('dataToLoad');
	
	if (s == 'show')
	{
		toggleDisabledAllInputButtons(true);
		indicatorDiv.style.display = 'block';
		if (table != null) { table.style.display = 'none'; }
	}
	else
	{
		toggleDisabledAllInputButtons(false);
		indicatorDiv.style.display = 'none';
		if (table != null) { table.style.display = 'block'; }
	}
}


function showDetail(sender, qId, vShow)
{
	//if vShow not passed in, get it based on sender.innerHTML
	var show = typeof(vShow) == 'undefined' ? sender.innerHTML == "[show detail]" : show = vShow;
	
	sender.innerHTML = show ? "[hide detail]" : "[show detail]" ;
	
	if (show)
	{
		document.getElementById("qDetail" + qId).style.display = "block";
	}
	else
	{
		document.getElementById("qDetail" + qId).style.display = "none";
	}
	
	sender.blur();
	return false;
}

function printTimePeriodReport()
{
	var url = '/client/print_timeperiodreport.html';
	printWindow = window.open(url,'printWindow','width=825,height=500,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
	printWindow.focus();
	return false;
}

function getElement(id)
{
	return document.getElementById(id);
}

function viewAllQDetail(sender)
{
	try
	{		
		var show;
		show = sender.firstChild.src.indexOf("/tick.gif") > 0;
		
		sender.firstChild.src = show ? "/images/untick.gif" : "/images/tick.gif";
		
		var showMoreLinks = document.getElementsByName('showQDetail');
		
		for (var i=0 ; i < showMoreLinks.length ; i++)
		{	
			if(typeof(showMoreLinks[i]) == 'object')
			{
				showDetail(showMoreLinks[i], showMoreLinks[i].id, !show);
			}
		}
		
		sender.blur();
		return false;
	}
	catch (e)
	{
	}
}