// function for radio button validation //
function radioB(btn) {
var cnt = -1;
for (var i=btn.length-1; i > -1; i--) {
if (btn[i].checked) {cnt = i; i = -1;}
}
if (cnt > -1) return btn[cnt].value;
else return null;
}

function checkB(btn) {
var cnt = -1;
for (var i=btn.length-1; i > -1; i--) {
if (btn[i].checked) {cnt = i; i = -1;}
}
if (cnt > -1) return btn[cnt].value;
else return null;
}

// end function

// form validation function //

function addRow() {
    /* Declare variables */
    var elements, templateRow, rowCount, row, className, newRow, element;
    var i, s, t;
    
    /* Get and count all "tr" elements with class="row".    The last one will
     * be serve as a template. */
    if (!document.getElementsByTagName)
        return false; /* DOM not supported */
    elements = document.getElementsByTagName("tr");
    templateRow = null;
    rowCount = 0;
    for (i = 0; i < elements.length; i++) {
        row = elements.item(i);
        
        /* Get the "class" attribute of the row. */
        className = null;
        if (row.getAttribute)
            className = row.getAttribute('class')
        if (className == null && row.attributes) {    // MSIE 5
            /* getAttribute('class') always returns null on MSIE 5, and
             * row.attributes doesn't work on Firefox 1.0.    Go figure. */
            className = row.attributes['class'];
            if (className && typeof(className) == 'object' && className.value) {
                // MSIE 6
                className = className.value;
            }
        } 
        
        /* This is not one of the rows we're looking for.    Move along. */
        if (className != "row_to_clone")
            continue;
        
        /* This *is* a row we're looking for. */
        templateRow = row;
        rowCount++;
    }
    if (templateRow == null)
        return false; /* Couldn't find a template row. */
    
    /* Make a copy of the template row */
    newRow = templateRow.cloneNode(true);

    /* Change the form variables e.g. price[x] -> price[rowCount] */
    elements = newRow.getElementsByTagName("input");
    for (i = 0; i < elements.length; i++) {
        element = elements.item(i);
        s = null;
        s = element.getAttribute("name");
        if (s == null)
            continue;
        t = s.split("[");
        if (t.length < 2)
            continue;
        s = t[0] + "[" + rowCount.toString() + "]";
        element.setAttribute("name", s);
        element.value = "";
    }
    
    /* Add the newly-created row to the table */
    templateRow.parentNode.appendChild(newRow);
    return true;
}

function ShtickyAdd(form) {
  var from = form.from.value;
  var counttxt = form.counttxt.value;
if(from == "") {
    inlineMsg('from','Please enter your Display Name.',2);
    return false;
  }
if(counttxt == "") {
    inlineMsg('counttxt','Please enter your note.',2);
    return false;
  }
return true;
}

function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {
        var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "counttxt";
        var countBody = opt_countBody ? opt_countBody : "countBody";
        var maxSize = opt_maxSize ? opt_maxSize : 1024;

        var field = document.getElementById(countedTextBox);

        if (field && field.value.length >= maxSize) {
                field.value = field.value.substring(0, maxSize);
        }
        var txtField = document.getElementById(countBody);
                if (txtField) { 
                txtField.innerHTML = field.value.length;
        }
}

function JobAdd(form) {
  var category = form.category.value;
  var type = form.type.value;
  var title = form.title.value;
  var companyName = form.companyName.value;
  var location = form.location.value;
  var zip = form.zip.value;
if(category == "") {
    inlineMsg('category','Please select a job category.',2);
    return false;
  }
if(type == "") {
    inlineMsg('type','Please select a job type.',2);
    return false;
  }
if(title == "") {
    inlineMsg('title','Please enter a job title.',2);
    return false;
  }
if(companyName == "") {
    inlineMsg('companyName','Please enter a company name.',2);
    return false;
  }
if(location == "") {
    inlineMsg('location','Please select a location.',2);
    return false;
  }
if (isNaN(zip)) {
	inlineMsg('zip','You must enter a valid zip code.',2);
    return false;
  }
  return true;
}

function EventsAdd(form) {
	var firstinput = form.firstinput.value;
	var title = form.title.value;
	var venue = form.venue.value;
	var address = form.address.value;
	var location = form.location.value;
if(firstinput == "") {
    inlineMsg('firstinput','Please select a start date.',2);
    return false;
  }
if(title == "") {
    inlineMsg('title','Please enter a event title.',2);
    return false;
  }
if(venue == "") {
    inlineMsg('venue','Please enter a venue name.',2);
    return false;
  }
if(address == "") {
    inlineMsg('address','Please enter the events address.',2);
    return false;
  }
if(location == "") {
    inlineMsg('location','Please select a location.',2);
    return false;
  }
  return true;
}

function PropertyAdd(form) {
  var type = form.type.value;
  var rfor = form.rfor.value;
  var title = form.title.value;
  var address = form.address.value;
  var bedrooms = form.bedrooms.value;
  var location = form.location.value;
  var price = form.price.value;
if(type == "") {
    inlineMsg('type','Please select what type of property.',2);
    return false;
  }
if(rfor == "") {
    inlineMsg('rfor','Please select what the property is for.',2);
    return false;
  }
if(title == "") {
    inlineMsg('title','Please enter a property title.',2);
    return false;
  }  
if(address == "") {
    inlineMsg('address','Please enter a street address.',2);
    return false;
  } 
if(location == "") {
    inlineMsg('location','Please select a location.',2);
    return false;
  }
if(bedrooms == "") {
    inlineMsg('bedrooms','Please select the number of bedrooms.',2);
    return false;
  }
if(price == "$") {
    inlineMsg('price','Please enter a property price.',2);
    return false;
  }    
if(price == "") {
    inlineMsg('price','Please enter a property price.',2);
    return false;
  }    
  return true;
}

function SynagoguesAdd(form) {
	var type = form.type.value;
	var name = form.name.value;
	var location = form.location.value;
	var phone = form.phone.value;
if(type == "") {
    inlineMsg('type','Please select the type of synagogue.',2);
    return false;
  }
if(name == "") {
    inlineMsg('name','Please enter a synagogue name.',2);
    return false;
  }
if(location == "") {
    inlineMsg('location','Please select a location.',2);
    return false;
  }
if(phone == "") {
    inlineMsg('phone','Please enter a phone number.',2);
    return false;
  }
  return true;
}

function BusinessAdd(form) {
	var category = form.category.value;
	var companyName = form.companyName.value;
	var address = form.address.value;
	var location = form.location.value;
	var phone = form.phone.value;
if(category == "") {
    inlineMsg('category','Please select a business category.',2);
    return false;
  }
if(companyName == "") {
    inlineMsg('companyName','Please enter a company name.',2);
    return false;
  }
if(address == "") {
    inlineMsg('address','Please enter a address.',2);
    return false;
  }
if(location == "") {
    inlineMsg('location','Please select a location.',2);
    return false;
  }
if(phone == "") {
    inlineMsg('phone','Please a phone number.',2);
    return false;
  }
  return true;
}

function HealthCareAdd(form) {
	var category = form.category.value;
	var companyName = form.companyName.value;
	var address = form.address.value;
	var location = form.location.value;
	var phone = form.phone.value;
if(category == "") {
    inlineMsg('category','Please select category.',2);
    return false;
  }
if(companyName == "") {
    inlineMsg('companyName','Please enter a company name.',2);
    return false;
  }
if(address == "") {
    inlineMsg('address','Please enter a address.',2);
    return false;
  }
if(location == "") {
    inlineMsg('location','Please select a location.',2);
    return false;
  }
if(phone == "") {
    inlineMsg('phone','Please a phone number.',2);
    return false;
  }
  return true;
}

function EngagementAdd(form) {
	var groomName = form.groomName.value;
	var brideName = form.brideName.value;
if(groomName == "") {
    inlineMsg('groomName','Please enter a Groom\'s Name.',2);
    return false;
  }
if(brideName == "") {
    inlineMsg('brideName','Please enter a Bride\'s Name.',2);
    return false;
  }
  return true;
}

function WeddingAdd(form) {
	var groomName = form.groomName.value;
	var brideName = form.brideName.value;
	var firstinput = form.firstinput.value;
if(groomName == "") {
    inlineMsg('groomName','Please enter a Groom\'s Name.',2);
    return false;
  }
if(brideName == "") {
    inlineMsg('brideName','Please enter a Bride\'s Name.',2);
    return false;
  }
if(firstinput == "") {
    inlineMsg('firstinput','Please enter a wedding date.',2);
    return false;
  }
  return true;
}

function MitzvahAdd(form) {
	var name = form.name.value;
	var location = form.location.value;
	var synagogue = form.synagogue.value;
	var frm_gender = radioB(form.gender);	
	var firstinput = form.firstinput.value;
	var childOf = form.childOf.value;

if(frm_gender == null) {
inlineMsg('gender','Please select bar or bat.',2);
return false;
}
if(name == "") {
    inlineMsg('name','Please enter a name.',2);
    return false;
  }
if(childOf == "") {
    inlineMsg('childOf','Please enter son/daughter of.',2);
    return false;
  }
if(firstinput == "") {
    inlineMsg('firstinput','Please enter a date.',2);
    return false;
  }
if(location == "") {
    inlineMsg('location','Please select a location.',2);
    return false;
  }
if(synagogue == "") {
    inlineMsg('synagogue','Please enter a synagogue name.',2);
    return false;
  }

  return true;
}


function BirthsAdd(form) {
	var name = form.name.value;
	var location = form.location.value;
	var frm_gender = radioB(form.gender);	
	var firstinput = form.firstinput.value;
	var childOf = form.childOf.value;

if(frm_gender == null) {
inlineMsg('gender','Please select son or daughter.',2);
return false;
}
if(name == "") {
    inlineMsg('name','Please enter a name.',2);
    return false;
  }
if(childOf == "") {
    inlineMsg('childOf','Please enter son/daughter of.',2);
    return false;
  }
if(firstinput == "") {
    inlineMsg('firstinput','Please enter a birthday.',2);
    return false;
  }
if(location == "") {
    inlineMsg('location','Please select a location.',2);
    return false;
  }

  return true;
}

function ObituariesAdd(form) {
	var name = form.name.value;
	var month = form.month.value;
	var day = form.day.value;
	var year = form.year.value;
	var firstinput = form.firstinput.value;
	var location = form.location.value;
if(name == "") {
    inlineMsg('name','Please enter a name.',2);
    return false;
  }
if(month == "") {
    inlineMsg('month','Please enter a month.',2);
    return false;
  }
if(day == "") {
    inlineMsg('day','Please enter a day.',2);
    return false;
  }
if(year == "") {
    inlineMsg('year','Please enter a year.',2);
    return false;
  }
if(year == "19") {
    inlineMsg('year','Please enter a year.',2);
    return false;
  }
if(firstinput == "") {
    inlineMsg('firstinput','Please enter date of death.',2);
    return false;
  }
if(location == "") {
    inlineMsg('location','Please select a location.',2);
    return false;
  }
  return true;
}

function AnniversaryAdd(form) {
	var name = form.name.value;
	var location = form.location.value;
	var month = form.month.value;
	var day = form.day.value;
	var year = form.year.value;
if(name == "") {
    inlineMsg('name','Please enter a couple name.',2);
    return false;
  }
if(location == "") {
    inlineMsg('location','Please select a location.',2);
    return false;
  }
if(month == "...") {
    inlineMsg('month','Please enter a month',2);
    return false;
  }
if(day == "...") {
    inlineMsg('day','Please enter a day',2);
    return false;
  }
if(year == "xxxx") {
    inlineMsg('year','Please enter a year',2);
    return false;
  }
if(year == "") {
    inlineMsg('year','Please enter a year',2);
    return false;
  }
  return true;
}
// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}

