﻿<!--

var	nextContent	= 5;
var	deleted		= 0;

function Submit()
{
	alert('This is the button your users will use\nto submit their eForm results.');
}

function Edit(section)
{
	document.getElementById(section +'view').style.visibility  = "hidden";
	document.getElementById(section +'edit').style.visibility  = "visible";
	document.getElementById(section +'save').style.visibility  = "visible";
	document.getElementById(section +'edit').focus();
}

function Save(section)
{
	'null'.match(/(null)/g);
	document.getElementById(section +'edit').value.match(/([a-zA-Z0-9]{1,})/g);
	if(RegExp.$1 != 'null')
	{
		document.getElementById(section +'view').innerHTML  = document.getElementById(section +'edit').value;
		document.getElementById('content'+ section +'save').value	= document.getElementById('content'+ section +'save').value.replace(/Text:([^;]*);/, 'Text:'+ document.getElementById(section +'edit').value +';');
	}
	document.getElementById(section +'view').style.visibility  = "visible";
	document.getElementById(section +'edit').style.visibility  = "hidden";
	document.getElementById(section +'save').style.visibility  = "hidden";

	//IE needs a focus on visible page
	document.getElementById('sendtoemails').focus();
}

function Set(section, value)
{
	document.getElementById(section +'edit').value  = value;
	Save(section);
}

function AddQuestion(type)
{
	var	html	= '';
	var	value	= '';
	
	html	+= '<br />';
	html	+= '<span style="background-color:#E1E6E8; position:relative; top:1px;">';
	html	+=		'<a href="#" onfocus="FindAndShow('+ nextContent +');" onclick="MoveQuestionUp('+ nextContent +');">'+ upimage +'</a>';
	html	+=		'<a href="#" onclick="MoveQuestionDown('+ nextContent +');">'+ downimage +'</a>';
	html	+=		'<a href="#" onclick="DeleteQuestion('+ nextContent +');">'+ deleteimage +'</a>';
	html	+= '</span>';
	html	+= '<div id="content'+ nextContent +'group">';
	html	+= '	<a name="anchor'+ nextContent +'" onfocus="FindAndShow('+ nextContent +');"></a>';
	html	+= '	<div id="content'+ nextContent +'question" onmouseover="ShowProperties('+ nextContent +', true);">';

	switch(type)
	{
		case '110':
			value	= 'Type:110;Text:;Min:1;Max:10;Required:0;';
			break;
		case 'check':
			value	= 'Type:check;Text:;List:;Max:;Required:0;';
			break;
		case 'hyper':
			value	= 'Type:hyper;Text:;URL:;Required:0;';
			break;
		case 'label':
			value	= 'Type:label;Text:;Required:0;';
			break;
		case 'radio':
			value	= 'Type:radio;Text:;List:;Required:0;';
			break;
		case 'text':
			value	= 'Type:text;Text:;Rows:1;Required:0;';
			break;
		case 'true':
			value	= 'Type:radio;Text:;List:True,False;Required:0;';
			break;
		default:
			break;
	}
	value	+= 'Placement:'+ nextContent +';';

	html	+= MakeQuestion(nextContent, value);
	html	+= '	</div>';
	html	+= '	<input name="xmlSave" class="hidden" id="content'+ nextContent +'save" value="'+ value +'">';
	html	+= '	<div id="content'+ nextContent +'edit"></div>';
	html	+= '</div>';
	html	+= '';
	html	+= '<div id="content'+ (nextContent + 1) +'"></div>';

	document.getElementById('content'+ nextContent).innerHTML	= html;
	nextContent++;
	//document.location.href='#anchor'+ nextContent;
}

function AddQuestionFromDB(webPartId, value)
{
	var	html	= '';
	var	next	= nextContent;
	
	if(webPartId > next)
	{
		next	= webPartId;
	}
	next++;
	
	html	+= '<br />';
	html	+= '<span style="background-color:#E1E6E8; position:relative; top:1px;">';
	html	+= 		'<a href="#" onfocus="FindAndShow('+ nextContent +');" onclick="MoveQuestionUp('+ nextContent +');">'+ upimage +'</a>';
	html	+=		'<a href="#" onclick="MoveQuestionDown('+ nextContent +');">'+ downimage +'</a>';
	html	+=		'<a href="#" onclick="DeleteQuestion('+ nextContent +');">'+ deleteimage +'</a>';
	html	+= '</span>';
	html	+= '<div id="content'+ nextContent +'group">';
	html	+= '	<a name="anchor'+ webPartId +'" onfocus="FindAndShow('+ nextContent +');"></a>';
	html	+= '	<div id="content'+ webPartId +'question" onmouseover="ShowProperties('+ webPartId +', true)">';
	value	+= 'Placement:'+ nextContent +';';
	html	+= MakeQuestion(webPartId, value);
	html	+= '	</div>';
	html	+= '	<input name="xmlSave" class="hidden" id="content'+ webPartId +'save" value="'+ value +'">';
	html	+= '	<div id="content'+ webPartId +'edit"></div>';
	html	+= '</div>';
	html	+= '';
	html	+= '<div id="content'+ next +'"></div>';

	document.getElementById('content'+ nextContent).innerHTML	= html;
	nextContent	= next;
}

function MakeQuestion(question, value)
{
	var	html		= OneMatch('Text:([^;]*);', value);
	var	type		= OneMatch('Type:([^;]*);', value);
	var	list		= OneMatch('List:([^;]*);', value);
	var	url			= OneMatch('URL:([^;]*);', value);
	var	required	= OneMatch('Required:([^;]*);', value);
	var	min			= Math.abs(OneMatch('Min:([^;]*);', value));
	var	max			= Math.abs(OneMatch('Max:([^;]*);', value));
	var	rows		= Math.abs(OneMatch('Rows:([^;]*);', value));
	var	text		= 'Use the Properties Box to Edit this Question';
	var	matches		= null;
	var	pattern		= /([^,]{1,})/g;

	if(min < 0)
	{
		min	= min * -1;
	}
	if(min > 100)
	{
		min	= 100;
	}
	if(max < 0)
	{
		max	= max * -1;
	}
	if(max > 100)
	{
		max	= 100;
	}

	if(html == '')
	{
		html	= '<span style="color: red;">'+ text +'</span>';
	}
	html	+= '<br />\n';

	switch(type)
	{
		case '110':
			html	+= '<select size="1">\n';
			for(var o = min; o <= max; o++)
			{
				html	+= '<option value="'+ o +'">'+ o +'</option>\n';
			}
			html	+= '</select>\n';
			break;
			
		case 'check':
			matches	= list.match(pattern);
			if(matches == null)
			{
				html	+= '<input type="checkbox" id="check'+ question +'-0" name="group'+ question +'" /> <span style="color: red;">Use the Properties Box to add Check Items Use a comma (,) to separate the items</span>';
			}
			for(var m = 0; matches != null && m < matches.length; m++)
			{
				matches[m].match(pattern);
				html	+= '<input type="checkbox" id="check'+ question +'-'+ m +'" name="group'+ question +'"';
				if(max != 0)
				{
					html	
						+= 'onclick = '
						+  '"'
						+  '	var	count		= 0;'
						+  '	var	elements	= document.getElementsByName(\'group'+ question +'\');'
						+  '	for(var e = 0; e < elements.length; e++)'
						+  '	{'
						+  '		if(elements[e].checked)'
						+  '		{'
						+  '			count++;'
						+  '		}'
						+  '	}'
						+  '	if(count > '+ max +')'
						+  '	{'
						+  '		document.getElementById(\'check'+ question +'-'+ m +'\').checked	= false;'
						+  '	}'
						+  '"'
					;
				}
				html	+= ' />'+ RegExp.$1 +'<br />\n';
			}
			break;
			
		case 'hyper':
			html	= html.replace(text, 'Use the Properties Box to Edit this Link');
			html	= '<a href="#" onclick="alert(\'Your users will use this link to surf too\\n'+ url +'\');">'+ html +'</a>\n';
			break;
			
		case 'label':
			html	= html.replace(text, 'Use the Properties Box to Edit this Label');
			break;
		case 'radio':
			matches	= list.match(pattern);
			if(matches == null)
			{
				html	+= '<input type="radio" name="group'+ question +'" /> <span style="color: red;">Use the Properties Box to add Radio Items Use a comma (,) to separate the items</span>';
			}
			for(var m = 0; matches != null && m < matches.length; m++)
			{
				matches[m].match(pattern);
				html	+= '<input type="radio" name="group'+ question +'" />'+ RegExp.$1 +'<br />\n';
			}
			break;
			
		case 'text':
			if(rows < 2)
			{
				html	+= '<input type="text" style="width: 80%;" DISABLED/>\n';
			}
			else
			{
				html	+= '<textarea style="width: 80%;" rows="'+ rows +'" DISABLED></textarea>\n';
			}
			break;
			
		default:
			break;
	}

	return html;
}

function OneMatch(pattern, text)
{
	var	exp		= new RegExp(pattern, 'g');
	var	match	= '';
	
	'null'.match(/(null)/g);
	text.match(pattern);
	if(RegExp.$1 != 'null')
	{
		match	= RegExp.$1;
	}

	return match;
}

function FindAndShow(context)
{
	document.getElementById('content'+ context +'group').innerHTML.match(/content([0-9]*)question/g);
	ShowProperties(RegExp.$1, true);
}

var	rendering	= 9999;
function ShowProperties(context, moveDetails)
{
	if(rendering != context)
	{
		try
		{
			//document.onmousemove	= TrackMouse;
			var	html		= '<table width="100%">';
			var	matches		= null;
			var	pattern		= /([^:]*):([^;]*);/g;
			var	required	= '';
			var	elements	= null;
			var	focuson		= false;

			elements	= document.getElementsByTagName('div');
			for(var e = 0; e < elements.length; e++)
			{
				if(elements[e].id.indexOf('content') != -1 && elements[e].id.indexOf('question') != -1)
				{
					elements[e].style.backgroundColor	= '';
				}
				if(elements[e].id.indexOf('content') != -1 && elements[e].id.indexOf('edit') != -1)
				{
					elements[e].innerHTML			= '';
				}
			}
			
			document.getElementById('content'+ context +'question').style.backgroundColor	= '#E1E6E8';
			//if(moveDetails)
			//{
			//	document.getElementById('propertieDetails').style.top		= dragy - 25;
			//	document.getElementById('propertieDetails').style.left		= '40%';
			//}

			if(document.getElementById('content'+ context +'save') != null)
			{
				matches	= document.getElementById('content'+ context +'save').value.match(pattern);
				for(var m = 0; matches != null && m < matches.length; m++)
				{
					matches[m].match(pattern);
					switch(RegExp.$1)
					{
						case 'Type':
							type	= RegExp.$1;
							break;
						case 'Placement':
							//hide the placement
							break;
						case 'Required':
							if(RegExp.$2 == '1' || RegExp.$2 == 'true')
							{
								required	= 'checked';
							}
							else
							{
								required	= '';
							}
							html	+= '<tr><td width="30" style="color:blue;">'+ RegExp.$1 +'</td>';
							html	+= '<td><input style="color:blue;text-align:left;" id="'+ RegExp.$1 +'" type="checkbox" '+ required +' onmouseup="UpdatePropertie('+ context +', \''+ type +'\', \''+ RegExp.$1 +'\', !document.getElementById(\''+ RegExp.$1 +'\').checked)"></td></tr>';
							break;
						default:
							html	+= '<tr><td width="30" style="color:blue;">'+ RegExp.$1 +'</td>';
							//html	+= '<td><input style="color:blue;width:90%;" id="'+ RegExp.$1 +'" type="text" value="'+ RegExp.$2 +'" onkeyup="UpdatePropertie('+ context +', \''+ type +'\', \''+ RegExp.$1 +'\', document.getElementById(\''+ RegExp.$1 +'\').value)"></td></tr>';
							html	+= '<td><textarea rows="2" style="color:blue;width:90%;" id="'+ RegExp.$1 +'" onkeyup="UpdatePropertie('+ context +', \''+ type +'\', \''+ RegExp.$1 +'\', document.getElementById(\''+ RegExp.$1 +'\').value)">'+ RegExp.$2 +'</textarea></td></tr>';
							focuson	= true;
							break;
					}
				}
			}
			html	+= '	<tr><td></td><td><font size="1" color="blue">Lists are separated by commas</font></td></tr>';
			html	+= '</table>';

			document.getElementById('content'+ context +'edit').innerHTML			= html;
			
			if(focuson)
			{
				document.getElementById('Text').focus();
			}
			
			rendering	= context;
		}
		catch(exc)
		{
			alert(exc +' context='+ context);
		}
	}
}

var	updatingPropertie	= false;
function UpdatePropertie(context, type, propertie, value)
{
	var	pattern	= new RegExp(propertie +':[^;]*;', 'g');
	var	curPos	= 0;

	curPos	= GetCursorPos(document.getElementById(propertie));
	
	value	= (value +'').replace(/[:;']/g, '');
	value	= (value +'').replace(/[\&]/g, 'and');
	
	if(propertie == 'Min' || propertie == 'Max')
	{
		value	= value.replace(/[^0-9]/g, '');
	}

	document.getElementById('content'+ context +'save').value			= document.getElementById('content'+ context +'save').value.replace(pattern, propertie +':'+ value +';');
	if(document.getElementById('content'+ context +'question') != null)
	{
		document.getElementById('content'+ context +'question').innerHTML	= MakeQuestion(context, document.getElementById('content'+ context +'save').value);
		ShowProperties(context, false);
	}
	
	//SetCursorPos(document.getElementById(propertie), curPos);
}

function SaveForm()
{
	SaveXMLPage('test.aspx');
}

function PublishForm()
{
	document.getElementById('checkpublish').checked	= ! document.getElementById('checkpublish').checked;
}

function MoveQuestionUp(contentnum)
{
	var	temp	= '';
	var	buff	= '';
	var tplace	= 0;
	var bplace	= 0;
	var	pattern	= /Placement:([^;]*);/g;
	
	for(var n = contentnum-1; n > contentnum-10 && document.getElementById('content'+ contentnum +'group'); n--)
	{
		if(document.getElementById('content'+ n +'group') != null)
		{
			buff	= document.getElementById('content'+ n +'group').innerHTML;
			temp	= document.getElementById('content'+ contentnum +'group').innerHTML;
			
			bplace	= temp.match(pattern);
			tplace	= buff.match(pattern);
			buff	= buff.replace(pattern, bplace);
			temp	= temp.replace(pattern, tplace);
			
			document.getElementById('content'+ n +'group').innerHTML			= temp;
			document.getElementById('content'+ contentnum +'group').innerHTML	= buff;
			
			//document.location.href='#anchor'+ contentnum;
			
			n	= contentnum-10;
		}
	}
}

function MoveQuestionDown(contentnum)
{
	var	temp	= '';
	var	buff	= '';
	var tplace	= 0;
	var bplace	= 0;
	var	pattern	= /Placement:([^;]*);/g;
	
	for(var n = contentnum+1; n < contentnum+10 && document.getElementById('content'+ contentnum +'group') != null; n++)
	{
		if(document.getElementById('content'+ n +'group') != null)
		{
			buff	= document.getElementById('content'+ n +'group').innerHTML;
			temp	= document.getElementById('content'+ contentnum +'group').innerHTML;
			
			bplace	= temp.match(pattern);
			tplace	= buff.match(pattern);
			buff	= buff.replace(pattern, bplace);
			temp	= temp.replace(pattern, tplace);
			
			document.getElementById('content'+ n +'group').innerHTML			= temp;
			document.getElementById('content'+ contentnum +'group').innerHTML	= buff;
			
			//document.location.href='#anchor'+ contentnum;
			
			n	= contentnum+10;
		}
	}
}

function DeleteQuestion(contentnum)
{
	var	temp	= '';
	
	if(document.getElementById('content'+ contentnum +'group') != null)
	{
		//document.getElementById('Debug').value	= document.getElementById('content'+ contentnum +'group').innerHTML;
		temp	= document.getElementById('content'+ contentnum +'group').innerHTML.match(/<input[^>]*name="*xmlSave"*[^>]*>/i) +'';
		temp	= temp.replace(/Type:([^;]*);/g, 'Type:DeLeTeMe;');
		temp	= '<a name="anchor'+ contentnum +'"></a>' + temp;
		
		document.getElementById('content'+ contentnum +'group').innerHTML	= temp;
		//document.location.href='#anchor'+ contentnum;
	}
}

function enhex(input)
{
	var	output	= '';
	var	buff	= '';

	for(var i = 0; i < input.length; i++)
	{
		if(input.charAt(i).match(/[a-zA-Z0-9]/i))
		{
			output	+= input.charAt(i);
		}
		else
		{
			buff	=  input.charCodeAt(i).toString(16);
			if(buff.length < 2)
			{
				buff	= '0'+ buff;
			}
			output	+= '%'+ buff;
		}
	}

	return output;
}

//-->
