// *****************************************************************************
// Author: Nick
// 
// History:
//
// 2007-07-30
// * created!
// *****************************************************************************
function validateform() {
	var agentidobj = document.getElementById('WEBREPAGENTID');
	if (agentidobj !== null) {
		var agentid = agentidobj.value;
	} else {
		var agentid = 0;
	}
	
	
	if (!checkRepresentor(agentid)) {
	} else if (!checkAgentisClient(agentid)) {
	} else if (!checkSupObjCom()) {
	} else if (checkDraft()) {
		return true;
	} else if (!checkLegalAndSound()) {
	} else if (!checkSummary()) {
	} else if (!checkChangeToPlan()) {
	} else if (!checkValidSoundness()) {
	} else if (!checkExamRadio()) {
	} else if (!checkLPA()) {
	} else {
		//document.the_form.submit();
		return true;
	}
	document.getElementById('WEBREPDRAFT').clicked = false;
	return false;
}

function checkDraft() {
	if (document.getElementById('WEBREPDRAFT').clicked) {
		document.getElementById('WEBREPDRAFT').clicked = false;
		return true;
	}
	return false;
}

function checkRepresentor(agentid) {
	if (agentid > 0) {
		var respondentid = document.getElementById('WEBREPRESPONDENTID');
		if	(respondentid && respondentid.value > 0) {
			return true;		
		} else {
			//alert('You must specify who you\'re acting on behalf of.');
			alert(TRANS_error_specifyrespondent);
 			respondentid.focus();
 			return false;
		}
	} else {
		return true;		
	}
}

function checkAgentisClient(agentid) {
	if (agentid >0) {
		var confirmchoicediv = document.getElementById('repformagent');
		var confirmchoicebox = document.getElementById('validaterepchoice');
		if (confirmchoicediv && confirmchoicediv.style.display != 'none' && confirmchoicebox && confirmchoicebox.checked !== true) {
			//alert ("You must confirm you are acting on behalf of yourself");
			alert (TRANS_error_confirmselfrepresent);
 			confirmchoicebox.focus();
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

function checkSupObjCom() {
	var supobjcom = document.getElementById('WEBREPSUPOBJ');
	if (supobjcom.value==='') {
		//alert ("You must choose if you're supporting or objecting.");
		alert (TRANS_error_choosesupobj);
		supobjcom.focus();
		return false;
	} else {
		return true;
	}
}

function checkLegalAndSound() {
	var supobj = document.getElementById("WEBREPSUPOBJ");
	if(supobj.value==2) { 
		var legcomp0 = document.getElementById('WEBREPLEGCOMP0');
		var legcomp1 = document.getElementById('WEBREPLEGCOMP1');
		if (legcomp0&&legcomp1) {
			if (!legcomp0.checked&&!legcomp1.checked) {
				//alert ("You must choose if you're consider the DPD to be Legally compliant.");
				alert (TRANS_error_chooselegal);
				legcomp0.focus();
				return false;
			}
		}
		var sound0 = document.getElementById('WEBREPSOUND0');
		var sound1 = document.getElementById('WEBREPSOUND1');
		if (sound0&&sound1) {
			if (!sound0.checked&&!sound1.checked) {
				//alert ("You must choose if you're consider the DPD to be Sound.");
				alert (TRANS_error_choosesound);
				sound0.focus();
				return false;
			}
		}
	}
	return true;
}

function checkSummary()
{
	var fulltext = document.getElementById('WEBREPFULLTEXT');
	var fulltextvalue = fulltext.value;
	var fulltextarray = trim(fulltextvalue).split(' ');

	var summary = document.getElementById('WEBREPSUMMARY');
	var summaryvalue = summary.value;

	if (fulltextarray.length > 100)
	{
		if (summaryvalue.length < 1)
		{
			//alert('Your representation is over 100 words in length, please write a summary.');
			alert(TRANS_error_summaryneeded);
			var summarydisplay = document.getElementById('summarydisplay');
			if (summarydisplay.style.display==='none') {
				summarydisplay.style.display = '';
			}
			summary.focus();
			return false;
		}
		else
		{
			var summarytextarray = trim(summaryvalue).split(' ');
			if (summarytextarray.length > 100)
			{
				//alert('Your summary must not exceed 100 words, please amend it.');
				alert(TRANS_error_exceeded);
				summary.focus();
				return false;
			}
		}
	}
	else if (fulltextvalue === "")
	{
		//alert('You must enter some text for your representation');
		alert(TRANS_error_notext);
		fulltext.focus();
		return false;
	}

	else
	{
		if (summaryvalue.length > 1)
		{
			//alert('Your representation is less than 100 words in length, you do not need to summarise.');
			alert(TRANS_error_nosummaryneeded);
			fulltext.focus();
			return false;
		}
	}

	return true;
}


function checkChangeToPlan() {
	var changetoplan = document.getElementById("WEBREPCHANGETOPLAN");
	var supobj = document.getElementById("WEBREPSUPOBJ");

	if(supobj && changetoplan && supobj.value==2 && changetoplan.value==="") {
		//alert("As you have chosen to Object, you must outline your changes to the plan.");
		alert(TRANS_error_specifyplanchange);
		changetoplan.focus();
		return false;
	}
	return true;
}

function checkValidSoundness()
{
	var supobj = document.getElementById("WEBREPSUPOBJ");

	var ctr = 0;
	var ticked = 0;
	var snd_tick = document.getElementById("snd_0");
	// if no tick oxes on screen, set ticked to true, to momic that there were to bypass

	// check later ...
	if(!snd_tick)
	{
		ticked = 1;
	}
	else
	{
		while(snd_tick)
		{
			if(snd_tick && snd_tick.checked)
			{
				ticked = 1;
			}
			// get next tick box ...
			ctr++;
			snd_tick = document.getElementById("snd_" + ctr);
		}
	}

	if(supobj && supobj.value==2 && ticked===0)
	{
		//alert("As you have chosen to Object, you must specify at least one 'soundness Test' reason.");
		alert(TRANS_error_specifysoundness);
		return false;
	}
	else
	{
		return true;
	}
}

function checkExamRadio()
{
	// temporary code needs improving!
	// check that one of the radio buttons is checked.

	var supobj = document.getElementById("WEBREPSUPOBJ");
	var exam0 = document.getElementById("WEBREPAPPEAREXAM0");
	var exam1 = document.getElementById("WEBREPAPPEAREXAM1");
	if(supobj && supobj.value==2 && exam0 && exam1) {
		if (!exam0.checked && !exam1.checked) {
			//alert('You must choose how you wish your representation to be considered at the examination');
			alert(TRANS_error_chooseexam);
			exam0.focus();
			return false;
		}
		var whyexam = document.getElementById("WEBREPORALEXAMWHY");
		if (whyexam) {
			if(whyexam.value==='' && exam1.checked) {
				//alert('You must give a reason why you consider it necessary to appear at the examination to make your representation');
				alert(TRANS_error_reasonappear);
				whyexam.focus();
				return false;
			}
		}
	}
	return true;
}

function checkLPA() {
	var supobj = document.getElementById("WEBREPSUPOBJ");
	var raiselpa = document.getElementById("WEBREPRAISELPA");
	var raiselpawhy = document.getElementById("WEBREPRAISELPAWHY");
	if(supobj && supobj.value==2 && raiselpa && raiselpawhy) {
		if(raiselpa.value<1 || raiselpa.value>2) {
			//alert('You must indicate if this is the first time you have made the objection');
			alert(TRANS_error_choosefirsttime);
			raiselpa.focus();
			return false;
		}
		if(raiselpawhy.value===''){
			//alert('You must say why you have not raised this matter before or if you have at which stage of the process.');
			alert(TRANS_error_whynotbefore);
			raiselpawhy.focus();
			return false;
		
		}

	} else if (raiselpa && raiselpawhy) {
		if((raiselpa.value==1 || raiselpa.value==2) && raiselpawhy.value===''){
			//alert('You must say why you have not raised this matter before or if you have at which stage of the process.');
			alert(TRANS_error_whynotbefore);
			raiselpawhy.focus();
			return false;
		
		}
		if ((raiselpa.value<1 || raiselpa.value>2) && raiselpawhy.value!=='') {
			//alert('You must indicate if this is the first time you have raised this point.');  //Change to above
			alert(TRANS_error_choosefirsttime);
			raiselpawhy.focus();
			return false;
		}
	}
	return true;
}
