// JavaScript Document

var popUpWin=0;

function popUpWindow(width, height, URLStr)
  {
    if (typeof(SymRealWinOpen)!="undefined")
        window.open = SymRealWinOpen;

    var left = (screen.width/2) - width/2;
    var top = (screen.height/2) - height/2;
    if(popUpWin)
      {
        if(!popUpWin.closed) popUpWin.close();
      }
    popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
    if (popUpWin==null)
        ; //alert("Please enable popups for this site");
    else
        popUpWin.focus()
  }

function popUpWindowFlexible(width, height, URLStr)
  {
    if (typeof(SymRealWinOpen)!="undefined")
        window.open = SymRealWinOpen;

    var left = (screen.width/2) - width/2;
    var top = (screen.height/2) - height/2;
    if(popUpWin)
      {
        if(!popUpWin.closed) popUpWin.close();
      }
    popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=Yes,menubar=no,scrollbars=Yes,resizable=Yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
    if (popUpWin==null)
        ; //alert("Please enable popups for this site");
    else
        popUpWin.focus()
  }

function getElement(id)
  {
  	return typeof(document.getElementById)=="undefined" ?
           document.all[id] : document.getElementById(id);
  }
  
function dupField(me, other)
  {
	  other = me.form[other];
	  if (other.value=='')
	  		other.value = me.value;
	   
  }

/* Choice routines */

choiceLast = false;
function choiceAdd()
  {
     choiceAddValue('','',false,'','')
  }

function choiceAddValue(text, value, defaultValue, textError, valueError)
  {
	var table = getElement('multitable');
	var size  = table.rows.length;
	var row = table.insertRow(size-1);
	if (choiceLast==false)
	   choiceLast = size;

    row.id = 'choice' + String(choiceLast);

	var cell = row.insertCell(-1);
	cell.vAlign= 'top';
	cell.innerHTML = '<input onchange="dupField(this, \'MultiValue' + String(choiceLast) + '\')" name="MultiText' + String(choiceLast) + '" value="' + text + '" class="choiceText">' + textError;
	var cell = row.insertCell(-1);
	cell.vAlign= 'top';
	cell.innerHTML = '<input name="MultiValue' + String(choiceLast) + '" value="' + value + '" class="choiceText">'  + valueError;
	var cell = row.insertCell(-1);
	cell.vAlign= 'top';
    cell.innerHTML = '<input name="MultiDefault" onmousedown="onlyOne(this)"' + (defaultValue ? ' checked' : '') + ' type="checkbox" value="' + String(choiceLast) + '">';
	var cell = row.insertCell(-1);
	cell.vAlign= 'top';
	cell.innerHTML = '<td valign="top" nowrap><a href="javascript:choiceUp(' + String(choiceLast) + 
			         ')"><img src="/images/up.gif" border="0"></a><a href="javascript:choiceDown(' + String(choiceLast) + 
					 ')"><img src="/images/down.gif" border="0"></a><a href="javascript:choiceDelete(' + String(choiceLast) + 
					 ')"><img src="/images/delete.gif" border="0"></a>';
	choiceLast++;
  }

function choiceUp(no)
  {
	var myRow = getElement('choice' + String(no));
	var index = myRow.rowIndex;
	if (index < 2) return;
	
	var parent = myRow.parentNode;
	parent.removeChild(myRow);
	
	var table = parent;
	while (table.tagName != 'TABLE')
		table = table.parentNode;
		   
	var before = table.rows[index-1];
	parent.insertBefore(myRow, before);
  }
  
function choiceDown(no)
  {
	var myRow = getElement('choice' + String(no));
	var index = myRow.rowIndex;

	var parent = myRow.parentNode;
	
	var table = parent;
	while (table.tagName != 'TABLE')
		table = table.parentNode;
		   
	if ( (table.rows.length - index) < 3) return; 	   
	parent.removeChild(myRow);

	if (table.rows.length == index)
		parent.appendChild(myRow);
	else
	  {
		var before = table.rows[index+1];
		parent.insertBefore(myRow, before);
	  }
  }  
  
function choiceDelete(no)
  {
	var myRow = getElement('choice' + String(no));
	var parent = myRow.parentNode;
	parent.removeChild(myRow);
  }


/* Matrix routines */
matrixLast = false;
function matrixAdd()
  {
     matrixAddValue('','',false,'','')
  }

function matrixAddValue(text, value, defaultValue, textError, valueError)
  {
	var table = getElement('matrixtable');
	var size  = table.rows.length;
	var row = table.insertRow(size-1);
	if (matrixLast==false)
	   matrixLast = size;
	   
    row.id = 'matrix' + String(matrixLast);

	var cell = row.insertCell(-1);
	cell.vAlign= 'top';
	cell.innerHTML = '<input onchange="dupField(this, \'MatrixValue' + String(matrixLast) + '\')" name="MatrixText' + String(matrixLast) + '" value="' + text + '" class="choiceText">' + textError;
	var cell = row.insertCell(-1);
	cell.vAlign= 'top';
	cell.innerHTML = '<input name="MatrixValue' + String(matrixLast) + '" value="' + value + '" class="choiceText">'  + valueError;
	var cell = row.insertCell(-1);
	cell.vAlign= 'top';
    cell.innerHTML = '<input name="MatrixDefault" onmousedown="onlyOne(this)"' + (defaultValue ? ' checked' : '') + ' type="checkbox" value="' + String(matrixLast) + '">';
	var cell = row.insertCell(-1);
	cell.vAlign= 'top';
	cell.innerHTML = '<a href="javascript:matrixUp(' + String(matrixLast) + 
			         ')"><img src="/images/up.gif" border="0"></a><a href="javascript:matrixDown(' + String(matrixLast) + 
					 ')"><img src="/images/down.gif" border="0"></a><a href="javascript:matrixDelete(' + String(matrixLast) + 
					 ')"><img src="/images/delete.gif" border="0"></a>';
	matrixLast++;
  }

function matrixUp(no)
  {
	var myRow = getElement('matrix' + String(no));
	var index = myRow.rowIndex;
	if (index < 2) return;
	
	var parent = myRow.parentNode;
	parent.removeChild(myRow);
	
	var table = parent;
	while (table.tagName != 'TABLE')
		table = table.parentNode;
		   
	var before = table.rows[index-1];
	parent.insertBefore(myRow, before);
  }
  
function matrixDown(no)
  {
	var myRow = getElement('matrix' + String(no));
	var index = myRow.rowIndex;

	var parent = myRow.parentNode;
	
	var table = parent;
	while (table.tagName != 'TABLE')
		table = table.parentNode;
		   
	if ( (table.rows.length - index) < 3) return; 	   
	parent.removeChild(myRow);

	if (table.rows.length == index)
		parent.appendChild(myRow);
	else
	  {
		var before = table.rows[index+1];
		parent.insertBefore(myRow, before);
	  }
  }  
  
function matrixDelete(no)
  {
	var myRow = getElement('matrix' + String(no));
	var parent = myRow.parentNode;
	parent.removeChild(myRow);
  }
  
/* Matrix questions */
matrixQuestionLast = false;
function matrixQuestionAdd()
  {
     matrixQuestionAddValue('','','','')
  }

function matrixQuestionAddValue(value, questionError)
  {
	var table = getElement('matrixquestiontable');
	var size  = table.rows.length;
	var row = table.insertRow(size-1);
	if (matrixQuestionLast==false)
	   matrixQuestionLast = size;

    row.id = 'matrixquestion' + String(matrixQuestionLast);

	var cell = row.insertCell(-1);
	cell.vAlign= 'top';
	cell.innerHTML = '<input name="MatrixQuestion' + String(matrixQuestionLast) + '" value="' + value + '" class="matrixQuestion">'  + questionError;
	var cell = row.insertCell(-1);
	cell.vAlign= 'top';
	cell.innerHTML = '<td valign="top" nowrap><a href="javascript:matrixQuestionUp(' + String(matrixQuestionLast) + 
			         ')"><img src="/images/up.gif" border="0"></a><a href="javascript:matrixQuestionDown(' + String(matrixQuestionLast) + 
					 ')"><img src="/images/down.gif" border="0"></a><a href="javascript:matrixQuestionDelete(' + String(matrixQuestionLast) + 
					 ')"><img src="/images/delete.gif" border="0"></a>';
	matrixQuestionLast++;
  }

function matrixQuestionUp(no)
  {
	var myRow = getElement('matrixQuestion' + String(no));
	var index = myRow.rowIndex;
	if (index < 2) return;
	
	var parent = myRow.parentNode;
	parent.removeChild(myRow);
	
	var table = parent;
	while (table.tagName != 'TABLE')
		table = table.parentNode;
		   
	var before = table.rows[index-1];
	parent.insertBefore(myRow, before);
  }
  
function matrixQuestionDown(no)
  {
	var myRow = getElement('matrixQuestion' + String(no));
	var index = myRow.rowIndex;

	var parent = myRow.parentNode;
	
	var table = parent;
	while (table.tagName != 'TABLE')
		table = table.parentNode;
		   
	if ( (table.rows.length - index) < 3) return; 	   
	parent.removeChild(myRow);

	if (table.rows.length == index)
		parent.appendChild(myRow);
	else
	  {
		var before = table.rows[index+1];
		parent.insertBefore(myRow, before);
	  }
  }  
  
function matrixQuestionDelete(no)
  {
	var myRow = getElement('matrixQuestion' + String(no));
	var parent = myRow.parentNode;
	parent.removeChild(myRow);
  }
  
/* Control visibility */
function formatChange(me)
  {
	  var value = me.value;
	  getElement("multiplechoice").style.display = value=='Across' || value=='Down' ? '' : 'none';	
	  getElement("entry").style.display = value=='Entry' ? '' : 'none';	
	  getElement("matrix").style.display = value=='Matrix' ? '' : 'none';	
  }

function showElement(id)
  {
	getElement(id).style.display = 'block';		  
  }
  
function hideElement(id)
  {
	getElement(id).style.display = 'none';		  
  }
  
function confirmSurveyAction(link, text)
  {
	if (window.confirm(text))
	 	location.href= link;
	
  }
  
function submitLink(link)
  {
	document.forms[0]['nextpage'].value = link;
	document.forms[0].submit();
  }

function submitEditFormLink(link)
  {
	document.forms.EditForm['nextpage'].value = link;
	document.forms.EditForm.submit();
  }

function validateList(form)
  {
	if (String(form.ListName.value).search(/[a-zA-Z]+/)==-1)
	  {
		alert("Please enter a list name");
		return false;
	  }
  }

function onlyOne(me)
  {
	if (me.checked) return;
	var f = document.getElementsByName(me.name);
	for (var i=0; i<f.length; i++)
	    if (f!=me) f[i].checked = false;
  }

function setCookie (name, value)
  {
    var date = new Date();
    if (typeof(date.setFullYear)!="undefined")
       date.setFullYear(date.getFullYear()+2);
    else
       date.setYear(date.getYear()+2);
    document.cookie = name + "=" + escape(value) + ";path=/;expires=" + date.toGMTString();
  }
  
function setTimeZoneCookie()
  {
	var visitortime = new Date();
	setCookie('TimezoneOffset', visitortime.getTimezoneOffset()/60);
  }
  
setTimeZoneCookie();