  var rowReq;
  var spanName;  
  //var numRows = 0;
  var formData;
  var fields;
  var removed = [];

  var numRowsArray = [];
  
  //create new row ctr object
  function createNewRow(groupId)
  {
		var rowCounter = new Object()
  		rowCounter.groupid = groupId;
  		rowCounter.rowcount = 0;
  		numRowsArray.push(rowCounter);
  		return rowCounter;
  }
  
  function showRow(groupid)
  {
  	var dirLevel = 1;
	var imgid = "rowimg_" + groupid;
	var selid = "addrows_" + groupid;
	var minusImg = new Image();
	minusImg.src = getImagePath(dirLevel) + "images/minus_icon.gif";	
	var plusImg = new Image();
	plusImg.src = getImagePath(dirLevel) + "images/plus_icon.gif"; 	

	if(document.getElementById(selid).style.display=='none'){
		document.getElementById(selid).style.display='block';
		document.getElementById(imgid).src = minusImg.src;		
		return true;
	}
	else
	{
		document.getElementById(selid).style.display='none';
		document.getElementById(imgid).src = plusImg.src;
		return false;
	}  
  
  } 


function getImagePath(dirLevel)
{
	if ( dirLevel == 2 ) {
		return "../../";
	} else  {
		return "../";
	}
}

function removeRow(url, groupid, rowNum, fieldList, curRowSize)
{
	rowReq = null;
	removed[removed.length] = rowNum;
	fields =  fieldList.split("|");
	var rmvFs = removed.join("|");
	//alert(fields);
	//alert(rmvFs);
  	var rowCounter = findNumRowCounter(groupid);
  	//alert("find row " + rowCounter);
  	if ( rowCounter == null ) {  	//should not happen
  		rowCounter = createNewRow(groupid);
  		//alert("row created");
  	}
  	var numRows = rowCounter.rowcount;
  	//alert(numRows);
  	if ( numRows == 0 ) 
  	{
  		numRows = curRowSize;
  	}  
  	numRows = numRows - 1;
  	rowCounter.rowcount = numRows;
  	url = url + '?groupid=' + groupid + '&removerow=' + rowNum + '&removed=' + rmvFs;
  	//alert(url);
  	ajaxRowAction(url, groupid);
}

  // sName = group id
  function addRows(url, sName, curRowSize) 
  {
  	/* Old using public var numRows above
	removed = Array[];
  	if ( curRowSize > numRows ) 
  	{
  		numRows = curRowSize;
  	} else 
  	{
  		numRows = numRows + 1;
  	} 
  	*/
	  
  	var rowCounter = findNumRowCounter(sName);
  	if ( rowCounter == null ) {  	
  		rowCounter = createNewRow(sName);
  	}
  	var numRows = rowCounter.rowcount;
  	if ( curRowSize > numRows ) 
  	{
  		numRows = curRowSize;
  	} else 
  	{
  		numRows = numRows + 1;
  	}
  	
  	if (numRows > 50)
  		alert("You have exceeded the total number of additional answers allowed for this question.");
  	else
  	{
  		rowCounter.rowcount = numRows;  	
  		fields =  null;   
  		rowReq = null;	
  		ajaxRowAction(url, sName);
  	}
  }


  function findNumRowCounter(groupId)
  {
	  	for ( var j = 0; j < numRowsArray.length; j++ ) 
	  	{
	  		if ( numRowsArray[j].groupid == groupId ) {
	  			return numRowsArray[j];
	  			
	  		}
	  	}
	  	return null;
  }

  function ajaxRowAction(url, sName) 
  {	  
	formData = getFormData(document.forms[0]);
  	var rowCounter = findNumRowCounter(sName);  // rowCounter is an object, not an int count	
  	//url = url + "&rows=" + numRows;
  	url = url + "&rows=" + rowCounter.rowcount;
  	spanName = "rowdata-" + sName;
    if (window.XMLHttpRequest) { // Non-IE browsers
      rowReq = new XMLHttpRequest();
      try {
        rowReq.open("GET", url, true);
      } catch (e) {
        alert(e);
      }      
      rowReq.onreadystatechange = processRowChange;
      rowReq.send(null);
    } else if (window.ActiveXObject) { // IE
      rowReq = new ActiveXObject("Microsoft.XMLHTTP");
      if (rowReq) {
        rowReq.open("GET", url, true);      
        rowReq.onreadystatechange = processRowChange;
        rowReq.send();
      }
    }
  }


  function processRowChange() {
    if (rowReq.readyState == 4) { // Complete
      if (rowReq.status == 200) { // OK response	
			renderRows(rowReq);
      } else {
        alert("Problem: " + rowReq.statusText);
      }
    }
  }
  
 
  function renderRows(req)
  {    			
	document.getElementById(spanName).innerHTML = req.responseText;  
	rePopulateData(formData);	
  }
    
    
  function rePopulateData(formData)
  {
	var qs2 = new Querystring(formData);
	//alert(qs2);
  	var keys = qs2.keys();
  	//alert(keys);
  	for ( var i = 0 ; i < keys.length; i++ ) 
  	{
  		var useField = 0;
  		if ( fields != null ) {
  			for ( var j=0; j< fields.length; j++ ) {
  				if ( keys[i] == fields[j] ) {  					
  					useField = 1;
  				}
  			}  		
  		}
  		if ( useField == 0 ) {
		  	var v = qs2.get(keys[i]);	
		  	try 
		  	{
		  		var elem = document.getElementsByName(keys[i]);	
		  		//alert (elem[0].name + ", type " + elem[0].type + ", val = " + v);
		  		if ( elem[0].type == "radio" ) {
		  			var radioLength = elem.length;
		  			//radioLength = isNaN(radioLength)?0:radioLength;
		  			//alert("radioLength " + radioLength);
					for(var radL = 0; radL < radioLength; radL++) 
					{
						elem[radL].checked = false;
						//alert(radL + ", name " + elem[radL].name + ", val = " + elem[radL].value + ", key val = " + v);
						if(elem[radL].value == v) {
							elem[radL].checked = true;
						}
					}				
		  		}
		  		else if ( elem[0].type == "checkbox" ) {
		  			elem[0].checked = true;
		  		}
		  		else {		  			
		  			elem[0].value = v;
		  		}
		  	} catch ( e ) 
		  	{
		  		//alert('unable to set element ' + keys[i]);
		  	} 
	  	}
  	}   
  }
    

function buildQueryString(params) {
    var query = "";
    for (var i = 0; i < params.length; i++) {
        query += (i > 0 ? "&" : "")
            + escape(params[i].name) + "="
            + escape(params[i].value);
    }
    return query;
}


function checkEnd(str, pattern)
{
    var start = str.length - pattern.length;
    return str.substring(start) == pattern;
}



  
