/* JS code for the data generator */

var g_numRows = 0;
var g_countries = new Array();
var g_allCountries = new Array("canada", "netherlands", "uk", "us");

(new Image(15,14)).src = "images/question_odd.jpg";
(new Image(15,14)).src = "images/question_even.jpg";

function changeRowType(rowType, choice)
{
  var row = rowType.replace(/^type_/, "");
  var rowType = (row%2 == 0) ? "even" : "odd";

  switch (choice)
  {
    case "Name":
      document.getElementById('example_' + row).innerHTML = document.getElementById("HTML_Block_Name").innerHTML.replace(/\$ROW\$/g, row);
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = "<input type='text' name='option_" + row + "' style='width: 230px;' />";}, 10);
      document.getElementById('help_' + row).innerHTML    = "<a href='#' onclick=\"return showPostit('postit1', 3, 300, 200)\"><img src='images/question_" + rowType + ".jpg' border='0'/></a>";
      break;
    case "Phone":
      document.getElementById('example_' + row).innerHTML = document.getElementById("HTML_Block_Phone").innerHTML.replace(/\$ROW\$/g, row);
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = "<input type='text' name='option_" + row + "' style='width: 230px;' />";}, 10);
      document.getElementById('help_' + row).innerHTML    = "<a href='#' onclick=\"return showPostit('postit2', 3, 340, 180)\"><img src='images/question_" + rowType + ".jpg' border='0'/></a>";
      break;
    case "Email":
      document.getElementById('example_' + row).innerHTML = "&nbsp;No examples available.";
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = "&nbsp;No options available.";},10);
      document.getElementById('help_' + row).innerHTML = "";      
      break;
    case "Number-Range":
      document.getElementById('example_' + row).innerHTML = "&nbsp;No examples available.";
      newHTML = document.getElementById("HTML_Block_Number-Range").innerHTML.replace(/\$ROW\$/g, row);
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = newHTML;}, 10);
      document.getElementById('help_' + row).innerHTML    = "<a href='#' onclick=\"return showPostit('postit7', 3, 300, 80)\"><img src='images/question_" + rowType + ".jpg' border='0'/></a>";
      break;
    case "Alphanumeric":
      exampleHTML = document.getElementById("HTML_Block_Alphanumeric").innerHTML.replace(/\$ROW\$/g, row);
      setTimeout(function(){document.getElementById('example_' + row).innerHTML = exampleHTML;}, 10);
      document.getElementById('options_' + row).innerHTML = "<input type='text' name='option_" + row + "' style='width: 230px;' />";
      document.getElementById('help_' + row).innerHTML    = "<a href='#' onclick=\"return showPostit('postit8', 3, 400, 180)\"><img src='images/question_" + rowType + ".jpg' border='0'/></a>";
      break;
    case "Text-Fixed":
      document.getElementById('example_' + row).innerHTML = "&nbsp;No examples available.";
      newHTML = document.getElementById("HTML_Block_Text-Fixed").innerHTML.replace(/\$ROW\$/g, row);
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = newHTML;}, 10);
      document.getElementById('help_' + row).innerHTML    = "<a href='#' onclick=\"return showPostit('postit3', 3, 300, 120)\"><img src='images/question_" + rowType + ".jpg' border='0'/></a>";
      break;
    case "Text-Random":
      document.getElementById('example_' + row).innerHTML = "&nbsp;No examples available.";
      newHTML = document.getElementById("HTML_Block_Text-Random").innerHTML.replace(/\$ROW\$/g, row);
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = newHTML;}, 10);
      document.getElementById('help_' + row).innerHTML    = "<a href='#' onclick=\"return showPostit('postit4', 3, 300, 110)\"><img src='images/question_" + rowType + ".jpg' border='0'/></a>";
      break;
    case "Postal-Zip":
      document.getElementById('example_' + row).innerHTML = "&nbsp;No examples available.";
      optionHTML = document.getElementById("HTML_Block_Postal").innerHTML.replace(/\$ROW\$/g, row);
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = optionHTML;}, 10);
      document.getElementById('help_' + row).innerHTML    = "<a href='#' onclick=\"return showPostit('postit9', 3, 300, 80)\"><img src='images/question_" + rowType + ".jpg' border='0'/></a>";
      break;
    case "Street-Address":
      document.getElementById('example_' + row).innerHTML = "&nbsp;No examples available.";
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = "&nbsp;No options available.";},10);
      document.getElementById('help_' + row).innerHTML    = "";
      break;
    case "City":
      document.getElementById('example_' + row).innerHTML = "&nbsp;No examples available.";
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = "&nbsp;No options available.";},10);
      document.getElementById('help_' + row).innerHTML    = "";
      break;
    case "State-Province":
      document.getElementById('example_' + row).innerHTML = "&nbsp;No examples available.";
      optionHTML = document.getElementById("HTML_Block_State").innerHTML.replace(/\$ROW\$/g, row);
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = optionHTML;}, 10);
      document.getElementById('help_' + row).innerHTML    = "<a href='#' onclick=\"return showPostit('postit10', 3, 320, 150)\"><img src='images/question_" + rowType + ".jpg' border='0'/></a>";
      break;
    case "Country":
      document.getElementById('example_' + row).innerHTML = "&nbsp;No examples available.";
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = "&nbsp;No options available.";},10);
      document.getElementById('help_' + row).innerHTML    = "";
      break;
    case "Date":
      document.getElementById('example_' + row).innerHTML = document.getElementById("HTML_Block_Date").innerHTML.replace(/\$ROW\$/g, row);
      optionHTML = document.getElementById("HTML_Block_DateOptions").innerHTML.replace(/\$ROW\$/g, row);
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = optionHTML;}, 10);
      document.getElementById('help_' + row).innerHTML    = "<a href='#' onclick=\"return showPostit('postit11', 3, 450, 670)\"><img src='images/question_" + rowType + ".jpg' border='0'/></a>";
      break;
    case "List":
      optionHTML = document.getElementById("HTML_Block_List").innerHTML.replace(/\$ROW\$/g, row);
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = optionHTML;}, 10);
      document.getElementById('help_' + row).innerHTML    = "<a href='#' onclick=\"return showPostit('postit6', 3, 300, 110)\"><img src='images/question_" + rowType + ".jpg' border='0'/></a>";
      exampleHTML = document.getElementById("HTML_Block_CustomListExamples").innerHTML.replace(/\$ROW\$/g, row);
      setTimeout(function(){document.getElementById('example_' + row).innerHTML = exampleHTML;}, 10);
      break;
      
    default:
      setTimeout(function(){document.getElementById('help_' + row).innerHTML = "";}, 10);
      setTimeout(function(){document.getElementById('example_' + row).innerHTML = "";}, 10);
      setTimeout(function(){document.getElementById('options_' + row).innerHTML = "";}, 10);
      break;
  }
}


function addRows(numRows)
{
  // check numRows is an integer
  if (numRows.match(/\D/) || numRows == 0 || numRows == "")
  {
    alert("Please enter the number of rows to add.");
    document.data.numRows.focus();
    return;
  }

  // get the current table
  var tbody = document.getElementById("dataTable").getElementsByTagName("tbody")[0];

  for (var i=1; i<=numRows; i++)
  {
    var currRow = ++g_numRows;
    var rowClass = (currRow%2 == 0) ? "evenRow" : "oddRow";

    var row = document.createElement("tr");
    row.setAttribute("class", rowClass); // for Firefox
    row.className = rowClass;            // for IE

    // [1] first cell: row number
    var td1 = document.createElement("td");
    td1.setAttribute("align", "center");
    td1.appendChild(document.createTextNode(currRow));

    // [2] second <td> cell: "title" input field
    var td2 = document.createElement("td");
    var title = document.createElement("input");
    title.setAttribute("type", "text");
    title.setAttribute("name", "title_" + currRow);
    title.setAttribute("style", "width: 130px;");
    td2.appendChild(title);

    // [3] <td> cell: data type dropdown
    var td3 = document.createElement("td");
    var dataTypeDropdown = document.createElement("select");
    dataTypeDropdown.setAttribute("name", "type_" + currRow);
    dataTypeDropdown.onchange = function (evt) { changeRowType(this.name, this.value) };

    var option1 = document.createElement("option");
    option1.setAttribute("value", "");
    option1.appendChild(document.createTextNode("Please Select"));

    dataTypeDropdown.appendChild(option1);  

    // opgroup 1: human data
    var optgroup1 = document.createElement("optgroup");
    optgroup1.setAttribute("label", "Human Data");

    var option2 = document.createElement("option");
    option2.setAttribute("value", "Name");
    option2.appendChild(document.createTextNode("Name"));
    var option3 = document.createElement("option");
    option3.setAttribute("value", "Phone");
    option3.appendChild(document.createTextNode("Phone/Fax"));   
    var option4 = document.createElement("option");
    option4.setAttribute("value", "Email");
    option4.appendChild(document.createTextNode("Email"));

    var option14 = document.createElement("option");
    option14.setAttribute("value", "Street-Address");
    option14.appendChild(document.createTextNode("Street Address"));
    var option15 = document.createElement("option");
    option15.setAttribute("value", "City");
    option15.appendChild(document.createTextNode("City"));

    var option5 = document.createElement("option");
    option5.setAttribute("value", "Postal-Zip");
    option5.appendChild(document.createTextNode("Postal / Zip"));
    var option6 = document.createElement("option");
    option6.setAttribute("value", "State-Province");
    option6.appendChild(document.createTextNode("State / Province / County"));
    var option7 = document.createElement("option");
    option7.setAttribute("value", "Country");
    option7.appendChild(document.createTextNode("Country"));
    var option8 = document.createElement("option");
    option8.setAttribute("value", "Date");
    option8.appendChild(document.createTextNode("Date"));

    // add the options to the optgroup
    optgroup1.appendChild(option2);
    optgroup1.appendChild(option3);
    optgroup1.appendChild(option4);
    optgroup1.appendChild(option14);
    optgroup1.appendChild(option15);
    optgroup1.appendChild(option5);
    optgroup1.appendChild(option6);
    optgroup1.appendChild(option7);
    optgroup1.appendChild(option8);

    // opgroup 2: text data
    var optgroup2 = document.createElement("optgroup");
    optgroup2.setAttribute("label", "Text");

    var option9 = document.createElement("option");
    option9.setAttribute("value", "Text-Fixed");
    option9.appendChild(document.createTextNode("Fixed Number of Words"));
    var option10 = document.createElement("option");
    option10.setAttribute("value", "Text-Random");
    option10.appendChild(document.createTextNode("Random Number of Words"));

    // add the options to the optgroup
    optgroup2.appendChild(option9);
    optgroup2.appendChild(option10);


    // opgroup 3: custom field 
    var optgroup3 = document.createElement("optgroup");
    optgroup3.setAttribute("label", "Custom");
    
    var option11 = document.createElement("option");
    option11.setAttribute("value", "Number-Range");
    option11.appendChild(document.createTextNode("Number Range"));   
    var option12 = document.createElement("option");
    option12.setAttribute("value", "Alphanumeric");
    option12.appendChild(document.createTextNode("Alpha-numeric"));
    var option13 = document.createElement("option");
    option13.setAttribute("value", "List");
    option13.appendChild(document.createTextNode("Custom List"));

    // add the options to the optgroup
    optgroup3.appendChild(option11);
    optgroup3.appendChild(option12);
    optgroup3.appendChild(option13);

    // add the optgroups to the select box
    dataTypeDropdown.appendChild(optgroup1);
    dataTypeDropdown.appendChild(optgroup2);
    dataTypeDropdown.appendChild(optgroup3);
    
    // and, complete the td cell by adding the entire dropdown
    td3.appendChild(dataTypeDropdown);


    // [4] fourth <td> cell: example
    var td4 = document.createElement("td");
    var exampleDiv = document.createElement("div");
    exampleDiv.setAttribute("id", "example_" + currRow);
    td4.appendChild(exampleDiv);
    
    // [5] fifth <td> cell: options 
    var td5 = document.createElement("td");
    td5.setAttribute("id", "options_" + currRow);
    var optionsDiv = document.createElement("div");
    optionsDiv.setAttribute("id", "options_" + currRow);
    td5.appendChild(optionsDiv);

    // [6] sixth <td> cell: help
    var td6 = document.createElement("td");
    td6.setAttribute("align", "center");
    var helpDiv = document.createElement("div");
    helpDiv.setAttribute("id", "help_" + currRow);
    td6.appendChild(helpDiv);

    // add the table data cells to the row
    row.appendChild(td1);
    row.appendChild(td2);
    row.appendChild(td3);
    row.appendChild(td4);
    row.appendChild(td5);
    row.appendChild(td6);

    // add the row to the table
    tbody.appendChild(row);
  }
  
  // update the number of columns
  document.data.numCols.value = g_numRows;
}


// determines the target of the form: the hidden iframe for excel or a new window for HTML
function submitForm()
{
  numResults = document.data.numResults.value;
  numCols    = document.data.numCols.value;
  
  // check numResults is an integer
  if (numResults.match(/\D/) || numResults == 0 || numResults == "")
  {
    alert("Please enter a valid number of results.");
    document.data.numResults.focus();
    return false;
  }

  // XML
  var error = false;
  if (document.data.resultType[2].checked)
  {
    var nodeNum = 1;
    for (nodeNum=1; nodeNum<=numCols; nodeNum++)
    {
      if (!document.data["title_" + nodeNum].value && document.data["type_" + nodeNum].value != "")
      {
        alert("Please enter every node name.");
        document.data["title_" + nodeNum].focus();
        error = true;
        break;
      }
      else if (document.data["title_" + nodeNum].value.match(/\W/))
      {
        alert("XML node names can only alphanumeric characters and must begin with a letter.");
        document.data["title_" + nodeNum].focus();
        error = true;
        break;
      }
    }
  }  
  if (document.data.resultType[3].checked)
  {
    var nodeNum = 1;
    for (nodeNum=1; nodeNum<=numCols; nodeNum++)
    {
      if (!document.data["title_" + nodeNum].value && document.data["type_" + nodeNum].value != "")
      {
        alert("Please enter every column name.");
        document.data["title_" + nodeNum].focus();
        error = true;
        break;
      }
      else if (document.data["title_" + nodeNum].value.match(/\W/))
      {
        alert("Database column names can only alphanumeric characters and must begin with a letter.");
        document.data["title_" + nodeNum].focus();
        error = true;
        break;
      }
    }
  }  
  
  if (error)
    return false;


  // all checks out, set the form target 
  if (document.data.resultType[0].checked)
    document.data.target = "_blank";
  else if (document.data.resultType[1].checked)
    document.data.target = "hiddenIframe";      
  else if (document.data.resultType[2].checked)
    document.data.target = "_blank";
  else if (document.data.resultType[3].checked)
    document.data.target = "hiddenIframe";
  else if (document.data.resultType[4].checked)
    document.data.target = "_blank";
  return true;
}


// optionType: State, Province, County
function hideShowStateProvCounty(row, isChecked, country)
{
  if (isChecked)
  {
    var ch1 = document.getElementById("includeRegion" + country + "Full_" + row);
    ch1.disabled = false;
    var label1 = document.getElementById("includeRegion" + country + "FullLabel_" + row);
    label1.className = "suboptionActive";

    var ch2 = document.getElementById("includeRegion" + country + "Short_" + row);
    ch2.disabled = false;
    var label1 = document.getElementById("includeRegion" + country + "ShortLabel_" + row);
    label1.className = "suboptionActive";
  }
  else
  {
    var ch1 = document.getElementById("includeRegion" + country + "Full_" + row);
    ch1.disabled = true;
    var label1 = document.getElementById("includeRegion" + country + "FullLabel_" + row);
    label1.className = "suboptionInactive";

    var ch2 = document.getElementById("includeRegion" + country + "Short_" + row);
    ch2.disabled = true;
    var label1 = document.getElementById("includeRegion" + country + "ShortLabel_" + row);
    label1.className = "suboptionInactive";  
  }
}

// called whenever the user changes the result type, to hide/show custom options 
function changeResultType(resultType)
{
  switch (resultType)
  {
    case "HTML":
    case "Excel":
      document.getElementById("custom_col_name").innerHTML = "Column Title";
      document.getElementById("sql_options").style.display = "none";
      document.getElementById("xml_options").style.display = "none";      
      document.getElementById("csv_options").style.display = "none";
      break;
    case "XML":
      document.getElementById("custom_col_name").innerHTML = "Node Name";
      document.getElementById("sql_options").style.display = "none";
      document.getElementById("xml_options").style.display = "block";     
      document.getElementById("csv_options").style.display = "none";
      break;      
    case "CSV":
      document.getElementById("custom_col_name").innerHTML = "Column Title";
      document.getElementById("sql_options").style.display = "none";
      document.getElementById("xml_options").style.display = "none";      
      document.getElementById("csv_options").style.display = "block";
      break;      
    case "SQL":   
      document.getElementById("custom_col_name").innerHTML = "Table Column";
      document.getElementById("sql_options").style.display = "block";
      document.getElementById("xml_options").style.display = "none";      
      document.getElementById("csv_options").style.display = "none";
      break;      
  }
}

// called on page load. Hides/shows the resultType-specific box
function initResultType()
{
  for (i=0;i<document.data.resultType.length; i++)
  {
    if (document.data.resultType[i].checked)
    {
      switch (document.data.resultType[i].value)
      {
        case "XML":
          document.getElementById("custom_col_name").innerHTML = "Node Name";
          document.getElementById("xml_options").style.display = "block";
          break;
        case "SQL":
          document.getElementById("custom_col_name").innerHTML = "Table Column";
          document.getElementById("sql_options").style.display = "block";
          break;
        case "CSV":
          document.getElementById("custom_col_name").innerHTML = "Table Column";
          document.getElementById("csv_options").style.display = "block";
          break;
      }
    }
  }
}

// hides/shows 
function updateCountryChoice()
{
  // empty g_countries
  g_countries.length = 0;

  for (i=0; i<document.data["countryChoice[]"].length; i++)
  {
    if (document.data["countryChoice[]"][i].checked)
      g_countries.push(document.data["countryChoice[]"][i].value);
  }
  
/*
  if (g_countries.length == 0)
  {
    alert("You have unselected all countries. By doing this, you will be unable to generate certain data types, like zip/postal/postcodes.");
    return;   
  }
*/

  // now hide/show all country-specific elements, based on what the user has selected)  
  for (i=0; i<g_allCountries.length; i++)
  {
    var elements = getElementsByClassName(document, "div", "country_" + g_allCountries[i])

    // if selected, ensure that elements with that language's classes are visible
    if (g_countries.contains(g_allCountries[i]))
      display = "block";
    else
      display = "none";

    if (elements.length > 0)
    {
      for (k=0; k<elements.length; k++)
        elements[k].style.display = display;
    }
  }
}


/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/
function getElementsByClassName(oElm, strTagName, strClassName){
  var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}


Array.prototype.contains = function(value)
{
  var found = false;

  for (j=0; j<this.length; j++)
  {
    if (this[j] == value)
      found = true;
  }
  return found;
}


