// <?php echo 'hello world'; ?>
// *****************************************************************************
// Author: Nick
// 
// History:
//
// 2007-09-04 - whyatexam()
// 2007-07-30 - created!
// *****************************************************************************
var p, text, img, br, input, label, textarea, form, div, option, select, fieldset, legend, heading;
var textareaarray = new Array();
var labelarray = new Array();
var radiocount = 0;

var previousid = 'none';
var previousparenthtml = 'none';

var globalcloseid;

function getHeadingTitle(holdingdiv,docid,html) {
	// ie doesn't like html.getAttribute('class') so have to use .className instead
	cheatTxT = html.className;
	if (cheatTxT!=''&&cheatTxT!=null) {
		//createHeading(holdingdiv,2,'You are about to comment on - ');
		createHeading(holdingdiv,2,TRANS_abouttocommenton+' - ');
		createHeading(holdingdiv,3,cheatTxT,'inlinerepindent');
		//createParagraph(holdingdiv,cheatTxT,'inlinerepindent')
	} else {
		//createHeading(holdingdiv,2,'You are about to comment on the above paragraph or heading.');
		createHeading(holdingdiv,2,TRANS_abouttocomment);
	}
}

function addRepDisplay(docid) {
	radiocount = 0;
	textareaarray = new Array();
	labelarray = new Array();
	
	if (!isloggedin()) {
		window.location = "makerep.php?action=comment&js=true&docelemid="+docid;
		return false;
	}	

	var testscreen = document.getElementById('ldf_document_holder');
	if (testscreen) {//document.body.offsetWidth && 
		//alert(testscreen.offsetWidth);
		if (testscreen.offsetWidth < 360) {//document.body.offsetWidth < 800 && 
			window.location = "makerep.php?action=comment&docelemid="+docid;
			return false;		
		}
	}

	
	var html = document.getElementById('d'+docid);
	var parenthtml = html.parentNode;
	
	//ban tables from inline
	var parentdaddy = parenthtml;
	while (parentdaddy.id != 'ldf_document_holder') {
		parentdaddy = parentdaddy.parentNode;
		if (parentdaddy.tagName == 'table' || parentdaddy.tagName == 'TABLE') {
			window.location = "makerep.php?action=comment&docelemid="+docid;
			return true;
		}
	}
	
	var newid = "makerep"+docid;


	if (previousid != newid)
	{
		var makerep = document.createElement("div");
		makerep.setAttribute("id", newid );
		makerep.className = 'makerepouter';
		var newform = createRepFrom(docid);

		var holdingdiv = document.createElement("div");
		holdingdiv.setAttribute("id", 'repinsert');
		holdingdiv.className = 'makerepinner';

		globalcloseid = docid;
		//createCloseForm(holdingdiv,'#d'+docid,'[close]',docid);
		createCloseForm(holdingdiv,'#d'+docid,TRANS_close,docid);

		getHeadingTitle(holdingdiv,docid,html);
		//createHeading(holdingdiv,2,getHeadingTitle(docid,html));

		isimportantinfo(holdingdiv);

		holdingdiv.appendChild(newform);
		makerep.appendChild(holdingdiv);

		if (removeRepDisplay()) {

			previousid = newid;

			previousparenthtml = parenthtml;
			
			//lastly display the change
			//makerep.style.display='none';
			parenthtml.appendChild(makerep);
			checkTextareaWidth();
			//makerep.style.display='';
		} else {
			window.location = "#d"+previousid.replace(/makerep/, "");
			return false;
		}
	}
	else if (previousid ==	newid)
	{
		removeRepDisplay();
	}
	window.location = "#d"+docid;
	return false;
}

function closeform() {
	addRepDisplay(globalcloseid);
}

function removeRepDisplay() {
	if (previousid != 'none' && previousparenthtml  != 'none')
	{
		//if (confirm('This will close the currently open form, any information it contains will be lost. Click \'Ok\' to Continue or \'Cancel\' to return to the form.')) {
		if (confirm(TRANS_closewarning)) {
			previousparenthtml.removeChild(document.getElementById(previousid)); 
			previousid = 'none';
			previousparenthtml = 'none';
			return true;
		} else {
			return false;
		}
	}
	return true;
}


function checkTextareaWidth() {
	var fieldsetid = document.getElementById('fieldsetid');
	var labelwidth = document.getElementById('WEBREPFULLTEXTlabel');
	var textareawidth = document.getElementById('WEBREPFULLTEXT');
	var repinsertwidth = document.getElementById('repinsert');
	var maxwidth = 0;
	if (fieldsetid && labelwidth && textareawidth && repinsertwidth) {
		maxwidth = (repinsertwidth.offsetWidth) - labelwidth.offsetWidth - textareawidth.offsetWidth - 40;
		if (maxwidth < 0) {
			var newwidth = textareawidth.offsetWidth + (maxwidth);
			if(newwidth < 200) {
				newwidth = repinsertwidth.offsetWidth-40;
				if(newwidth<200){
					newwidth = newwidth;
				}
			}
			//alert('new = '+newwidth+' //max = '+maxwidth+' field = '+fieldsetid.offsetWidth+' label = '+labelwidth.offsetWidth+' textarea = '+textareawidth.offsetWidth);
			for (var i = 0; i < textareaarray.length; i++) {
				var checktextarea = document.getElementById(textareaarray[i]);
				if (checktextarea) {
					checktextarea.style.width = ""+newwidth+"px";
				}
			
			}
		}
	}
}

function createRepFrom(docid) {

	//if (isloggedin()) {
		form = document.createElement("form");
		form.setAttribute("id", 'the_form');
		form.setAttribute("name", 'the_form');
		form.setAttribute("method", 'post');
		form.setAttribute("action", 'makerep.php?action=submit');

		//msie hack
		if (navigator.appName == 'Microsoft Internet Explorer') {
			form.attachEvent('onsubmit',validateform);
		} else {
			form.setAttribute("onsubmit", 'return validateform();');
		}


		fieldset = createFieldSet('blockoff','Representation Form:');
		fieldset.setAttribute("id", 'fieldsetid');
		
		createInput(fieldset,'changed','','','hidden');
		createInput(fieldset,'WEBREPDOCELEMID','',docid,'hidden');
		
		
		//check if user is a agent beware of Mr Smith!
		isagent(fieldset);
		
		createInput(fieldset,'WEBREPID','','','hidden');

		//formkey
		var key = uniquekey(100000000);
		createInput(fieldset,'formkey','',key,'hidden');

		issupportobjectcomment(fieldset);
		//issoundandlegal(fieldset);
		
		//you thank the dpd for this!
		var divswap = document.createElement("div");
		divswap.setAttribute("id", 'repformsoundobject');
		divswap.style.display = 'none';
		//soundnessTest(divswap);
		fieldset.appendChild(divswap);


		createExplanationtext(fieldset,WEBTEXTFORMFULLREP);//'Enter your full representation here');
		createTextarea(fieldset,'WEBREPFULLTEXT',WEBTEXTFORMFULLREPLABEL,'displayWordCountRepFull');//'Representation :');
		
		createWordCount(fieldset,'WEBREPFULLTEXT');
		
		var summarydisplay = document.createElement("div");
		summarydisplay.setAttribute("id",'summarydisplay');
		summarydisplay.style.display='none';
		
		createExplanationtext(summarydisplay,WEBTEXTFORMSUMMARY);//'Summarise your representation if it is more than 100 words in length');
		createTextarea(summarydisplay,'WEBREPSUMMARY',WEBTEXTFORMSUMMARYLABEL,'displayWordCountSummary');//'Summary:');
		createWordCount(summarydisplay,'WEBREPSUMMARY');

		
		fieldset.appendChild(summarydisplay);

		var divswap = document.createElement("div");
		divswap.setAttribute("id", 'repforminsert');
			divswap.style.display = 'none';

		ischangetoplan(divswap);
		issoundandlegal(divswap);
		soundnessTest(divswap);
		isappearexam(divswap);
		
		fieldset.appendChild(divswap);

		israiselpa(fieldset);
		isaskadopted(fieldset);
		//isspatialdata(fieldset,docid);
		
		createInput(fieldset,'submit','',TRANS_submit,'submit',true,true);
		//createInput(fieldset,'WEBREPDRAFT','',TRANS_saveasdraft,'submit',true); 
		createInput(fieldset,'WEBREPDRAFT','',TRANS_saveasdraft,'submit',true,true);
		if (WEBREP_UPLOADSENABLED)
			createInput(fieldset,'WEBREPATTACH','',TRANS_addattachment,'submit',true); 

		form.appendChild(fieldset);
	//} else {
	//
	//	window.location = "makerep.php?action=comment&docelemid="+docid+"&js=true";
	//	//please login
	//	//return true;
	//}
	return form;
}

function showform() {
	var temp1 = document.getElementById('repforminsert');
	var temp2 = document.getElementById('repformsoundobject');
	var dpd = document.getElementById('WEBREPSOUND1');
	if (document.getElementById('WEBREPSUPOBJ').value == 2) {
		if (temp1) {
			temp1.style.display = '';
		}
		if (temp2 && !dpd) {
			temp2.style.display = '';
		}
	} else {
		if (temp1) {
			temp1.style.display = 'none';
		}
		if (temp2 && !dpd) {
			temp2.style.display = 'none';
		}
	}
}

function showsoundform() {
	var temp = document.getElementById('repformsoundobject');
	if (temp) {
		if (document.getElementById('WEBREPSOUND1').checked) {
			temp.style.display = '';
		} else {
			temp.style.display = 'none';
		}
	}
}

function amiagent() {
	var temp = document.getElementById('repformagent');
	if (document.getElementById('WEBREPAGENTID').value == document.getElementById('WEBREPRESPONDENTID').value) {
		temp.style.display = '';
	} else {
		temp.style.display = 'none';
	}
}

function whyatexam() {
	var temp = document.getElementById('whyatexam');
	if (document.getElementById('WEBREPAPPEAREXAM1').checked === true) {
		temp.style.display = '';
	} else {
		temp.style.display = 'none';
	}
}

function createSelect(parentelement,idname,text,valuearray,dosomething,selectedvalue) {

	createLabel(parentelement,idname,text);

	var select = document.createElement("select");
	select.setAttribute("name", idname);
	select.setAttribute("id", idname);
	
	if (dosomething == "showform") {
		//msie hack
		if (navigator.appName == 'Microsoft Internet Explorer') {
			select.attachEvent('onchange',showform);
		} else {
			select.setAttribute("onchange", 'showform();');
		}
	} else if (dosomething == "amiagent") {
		//msie hack
		if (navigator.appName == 'Microsoft Internet Explorer') {
			select.attachEvent('onchange',amiagent);
		} else {
			select.setAttribute("onchange", 'amiagent();');
		}
	}
	
	for (var i = 0; i < valuearray.length; i++)
	{
		if (valuearray[i][0] == selectedvalue)
			createOption(select,valuearray[i][0],valuearray[i][1],true);
		else
			createOption(select,valuearray[i][0],valuearray[i][1]);
	}
	parentelement.appendChild(select);

	br = document.createElement("br");
	parentelement.appendChild(br);
}

function createRadio(parentelement,idname,text,value,dosomething) {

	if (navigator.appName == "Microsoft Internet Explorer") { //nasty browser which has evil plans
	 	input = document.createElement('<input type="radio" name="' + idname + '" id="' + idname + radiocount + '" value="' + value + '" />'); 
 	} else { //must be a browser which works
		input = document.createElement("input");
		input.setAttribute("name", idname);
		input.setAttribute("id", idname+radiocount);
		input.setAttribute("type", 'radio');
		input.setAttribute("value", value);
	}
	
	if (dosomething == "whyatexam") {
		//msie hack
		if (navigator.appName == 'Microsoft Internet Explorer') {
			input.attachEvent('onclick',whyatexam);
		} else {
			input.setAttribute("onclick", 'whyatexam();');
		}
	} else if (dosomething == "showsoundform") {
		//msie hack
		if (navigator.appName == 'Microsoft Internet Explorer') {
			input.attachEvent('onclick',showsoundform);
		} else {
			input.setAttribute("onclick", 'showsoundform();');
		}
	}
	
	parentelement.appendChild(input);

	createLabel(parentelement,idname+radiocount,text);
	
	br = document.createElement("br");
	parentelement.appendChild(br);

	radiocount++;
}

function createRadioList(parentelement,idname,text,valuearray,dosomething) {
	var checkboxform = document.createElement('div');
	checkboxform.className = 'inputlistform';
	radiocount=0;
	for (var i = 0; i < valuearray.length; i++)
	{
		createRadio(checkboxform,idname,valuearray[i][1],valuearray[i][0],dosomething);
	}
	parentelement.appendChild(checkboxform);
}


function createTextarea(parentelement,idname,text,dosomething) {


	createLabel(parentelement,idname,text);

	textarea = document.createElement("textarea");
	textarea.className = 'inlinetextarea';
	textarea.setAttribute("name", idname);
	textarea.setAttribute("id", idname);
	textarea.setAttribute("rows", '8');
	textarea.setAttribute("cols", '50');
	if (dosomething == 'displayWordCountRepFull') {
		textarea.onkeyup = function(){ displayWordCount('WEBREPFULLTEXT',100,'summarydisplay'); };
	} else 	if (dosomething == 'displayWordCountSummary') {
		textarea.onkeyup = function(){ displayWordCount('WEBREPSUMMARY'); };
	}


	parentelement.appendChild(textarea);
	br = document.createElement("br");
	parentelement.appendChild(br);
	
	textareaarray[textareaarray.length] = idname;
}

