// functions.js

var ua = window.navigator.userAgent;
var msie = ua.indexOf ( "MSIE " );

function togglePass()
{
        document.WMAdd.silPass.disabled = !document.WMAdd.silPass.disabled;
        document.WMAdd.roomPass.disabled = !document.WMAdd.roomPass.disabled;
}

function getRowNum(tableName){
  var tbl = document.getElementById(tableName);
  var lastRow = tbl.rows.length;
  return lastRow;
}

function addRowToTable(tableName)
{
  var tbl = document.getElementById(tableName);
  var lastRow = tbl.rows.length;
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
        if(lastRow==0)
        {
                oCell = row.insertCell(0);
                oCell.align = "center";
                oCell.style.fontWeight = "bold";
				/*new add*/
				oCell.style.fontSize = "9px";
                oCell.innerHTML ='Email';
                lastRow = tbl.rows.length;
                row = tbl.insertRow(lastRow);
        }
  // if there's no header row in the table, then iteration = lastRow + 1




  var cell2 = row.insertCell(0);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'email[]';
  el.onblur = function () {vemail(this)};
  el.size = 22;
  cell2.appendChild(el);


  var cell4 = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'button';
  el.name = 'remove';
  el.setAttribute('value', 'Delete');
        if(msie > 0)
        {
                 el.setAttribute('className', 'warn');
        }
        else
        {
                 el.setAttribute('class', 'warn');
        }
  el.onclick = function () {deleteCurrentRow(this)};
  cell4.appendChild(el);
}






function vemail(obj)
{
var fld=0;
var goodEmail = obj.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);

fld = obj;

if (goodEmail){
   good = true
} else {
   alert("Please enter a valid email address.")
setTimeout("fld.focus();",1);
setTimeout("fld.select();",1);
   good = false
}
return good;
}

function deleteCurrentRow(obj)
{
                var delRow = obj.parentNode.parentNode;
                var tbl = delRow.parentNode.parentNode;
                var rIndex = delRow.sectionRowIndex;
                var rowArray = new Array(delRow);
                deleteRows(rowArray);
                reorderRows(tbl, rIndex);
                var lastRow = tbl.rows.length;
                if(lastRow==1)
                {
                        tbl.deleteRow(0);
                }
}

function reorderRows(tbl, startingIndex)
{
                if (tbl.tBodies[0].rows[startingIndex]) {
                        var count = startingIndex + ROW_BASE;
                        for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {

                                // CONFIG: next line is affected by myRowObject settings
                                tbl.tBodies[0].rows[i].myRow.one.data = count; // text

                                //teElement CONFIG: next line is affected by myRowObject settings
                                tbl.tBodies[0].rows[i].myRow.two.name = INPUT_NAME_PREFIX + count; // input text

                                // CONFIG: next line is affected by myRowObject settings
                                var tempVal = tbl.tBodies[0].rows[i].myRow.two.value.split(' '); // for debug purposes
                                tbl.tBodies[0].rows[i].myRow.two.value = count + ' was' + tempVal[0]; // for debug purposes

                                // CONFIG: next line is affected by myRowObject settings
                                tbl.tBodies[0].rows[i].myRow.four.value = count; // input radio

                                // CONFIG: requires class named classy0 and classy1
                                tbl.tBodies[0].rows[i].className = 'classy' + (count % 2);

                                count++;
                        }
                }
}

function deleteRows(rowObjArray)
{
                for (var i=0; i<rowObjArray.length; i++) {
                        var rIndex = rowObjArray[i].sectionRowIndex;
                        rowObjArray[i].parentNode.deleteRow(rIndex);
                }
}



// end.




//pk 20090616



function addRowToTableSMS(tableName)
{

  var tbl = document.getElementById(tableName);
  var lastRow = tbl.rows.length;
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
        if(lastRow==0)
        {
                oCell = row.insertCell(0);
                oCell.align = "center";
                oCell.style.fontWeight = "bold";
				/*new add*/
				oCell.style.fontSize = "9px";
                oCell.innerHTML ='SMS';
                lastRow = tbl.rows.length;
                row = tbl.insertRow(lastRow);
        }
  // if there's no header row in the table, then iteration = lastRow + 1




  var cell2 = row.insertCell(0);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'sms[]';
  el.onblur = function () {vsms(this)};
  el.size = 22;
  cell2.appendChild(el);


  var cell4 = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'button';
  el.name = 'remove';
  el.setAttribute('value', 'Delete');
        if(msie > 0)
        {
                 el.setAttribute('className', 'warn');
        }
        else
        {
                 el.setAttribute('class', 'warn');
        }
  el.onclick = function () {deleteCurrentRow(this)};
  cell4.appendChild(el);
}


function vsms(obj)
{
var fld=0;
var goodsms = obj.value.match(/\b(^852(6|9)(\d){7}$)\b/gi)||obj.value.match(/\b(^8613(\d){9}$)\b/gi);

fld = obj;

if (goodsms){
   good = true
} else {
   alert("Please enter a valid phonenumber.")
setTimeout("fld.focus();",1);
setTimeout("fld.select();",1);
   good = false
}
return good;
}

//end pk 20090616



