function addRowToTable()
{
  //adding increment to hidden field...
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;
  //now start making element...
  var tbl = document.getElementById('tblSample');
  var lastRow = tbl.rows.length;
  var row = tbl.insertRow(lastRow);
  // left cell
  var cellLeft = row.insertCell(0);
  cellLeft.className =  "txt06";
  cellLeft.width = "177";
  cellLeft.align =  "right";
  var textNode = document.createTextNode("Friend E-mail Address "+ numi.value + " : ");
  cellLeft.appendChild(textNode);
  // right cell
  var cellRight = row.insertCell(1);
  cellRight.className =  "txt07";
  cellRight.width = "363";
  cellRight.align =  "left";
  var el = document.createElement('input');
  el.type = "text";
  el.name = "friend_email" + numi.value;
  el.id = "friend_email" + numi.value;
  el.size = "40";
  el.className = "fldStyle02";
  cellRight.appendChild(el);
  //now adding empty space row
  var rowempty = tbl.insertRow(lastRow+1);
  var cellempty = rowempty.insertCell(0);
  cellempty.colspan="2";
  cellempty.height = "8";
}
//
function keyPressTest(e, obj)
{
  var validateChkb = document.getElementById('chkValidateOnKeyPress');
  if (validateChkb.checked) {
    var displayObj = document.getElementById('spanOutput');
    var key;
    if(window.event) {
      key = window.event.keyCode; 
    }
    else if(e.which) {
      key = e.which;
    }
    var objId;
    if (obj != null) {
      objId = obj.id;
    } else {
      objId = this.id;
    }
    displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
  }
}
function removeRowFromTable()
{
  var tbl = document.getElementById('tblSample');
  var lastRow = tbl.rows.length;
  if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
function openInNewWindow(frm)
{
  // open a blank window
  var aWindow = window.open('', 'TableAddRowNewWindow',
   'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
   
  // set the target to the blank window
  frm.target = 'TableAddRowNewWindow';
  
  // submit
  frm.submit();
}
//************************email validation here ..//
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

//************************//
function ValidateForm()
{
    var my_name = document.getElementById('my_name');
    var my_email = document.getElementById('my_email');
    var friend_email1 = document.getElementById('friend_email1');
    var theValue = document.getElementById('theValue');
    var msg = document.getElementById('msg');
    var i;
	if (my_name.value==""){
		alert("Your name required");
		my_name.focus();
		return false;
    	}
        if (echeck(my_email.value)==false){
		my_email.focus();
		return false;
    	}
	if (echeck(friend_email1.value)==false){
		friend_email1.focus();
		return false;
    	}
      for (i=2; i<=theValue.value; i++) {
        var emailID = document.getElementById('friend_email' + i);
        if(emailID.value!=""){
            if (echeck(emailID.value)==false){
		emailID.focus();
		return false;
	    }
	  }
      }
     if (msg.value==""){
		alert("Please enter your message")
		msg.focus();
		return false;
      }
   return true;
}

