var num_recips = 1;

function checkform()
{
  var text_out = '';
  var error = 0;
  var missing_recip_name = 0;
  var missing_recip_mobnum = 0;
  var recip_mobnum_badform = 0;
  var recip_name_badform = 0;
  var same_mobnums = 0;
  
  var reg = /^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/;
  
  if (document.send_form.f_name.value == '') 
  {
    text_out = text_out + "- You must enter your name\r\n";
    error = 1;
  }
  else
  {
    if (document.send_form.f_name.value.length < 3 || document.send_form.f_name.value.length > 11) 
    {
      text_out = text_out + "- Your name must be between 3 and 11 letters long\r\n";
      error = 1;
    }
  }
  
  if (!reg.test(document.getElementById('f_mobnum').value))
  {
    text_out = text_out + "- You must enter your valid UK mobile number\r\n";
    error = 1;
  }
  
  if (document.send_form.f_mobnum.value == '') 
  {
    text_out = text_out + "- You must enter your mobile number\r\n";
    error = 1;
  }

  if (document.send_form.f_msg.value == '') 
  {
    text_out = text_out + "- You must enter your message\r\n";
    error = 1;
  }
  else
  {
    if (document.send_form.f_msg.value.length > 160) 
    {
      text_out = text_out + "- Your message is too long! 160 characters maximum.\r\n";
      error = 1;
    }
  }
   
  for (i = 1; i <= 5; i++)
  {
    if (document.getElementById('f_rec_name_'+i)) { if (document.getElementById('f_rec_name_'+i).value == '') {  missing_recip_name = 1; error=1;} }
  }
  if (missing_recip_name == 1) { text_out = text_out + "- You must enter all the recipients names\r\n"; error=1;}

  for (i = 1; i <= 5; i++)
  {
    if (document.getElementById('f_rec_name_'+i)) 
    { 
      if (document.getElementById('f_rec_name_'+i).value.length < 3 || document.getElementById('f_rec_name_'+i).value.length > 11) 
      {  
        recip_name_badform = 1; error=1;
      }
    }
  }
  if (recip_name_badform == 1) { text_out = text_out + "- All the recipients names must be between 3 and 11 letters long\r\n"; error=1;}

  for (i = 1; i <= 5; i++)
  {
    if (document.getElementById('f_rec_mobnum_'+i)) { if (document.getElementById('f_rec_mobnum_'+i).value == '') {  missing_recip_mobnum = 1; error=1;} }
  }
  if (missing_recip_mobnum == 1) { text_out = text_out + "- You must enter all the recipients mobile numbers\r\n"; error=1;}

  for (i = 1; i <= 5; i++)
  {
    if (document.getElementById('f_rec_mobnum_'+i)) { if (document.getElementById('f_rec_mobnum_'+i).value == document.getElementById('f_mobnum').value) {  same_mobnums = 1; error=1;} }
  }
  if (same_mobnums == 1) { text_out = text_out + "- You cannot text yourself!\r\n"; error=1;}
  
  for (i = 1; i <= 5; i++)
  {
    if (document.getElementById('f_rec_mobnum_'+i)) { if (!reg.test(document.getElementById('f_rec_mobnum_'+i).value)) { recip_mobnum_badform = 1; error=1; } }
  }  
  if (recip_mobnum_badform == 1) { text_out = text_out + "- Recipients mobile numbers must be valid (UK 07... or international +44... format)\r\n"; error=1;}
  
  if (document.send_form.f_terms.checked == 0) 
  {
    text_out = text_out + "- You must agree to the terms and conditions\r\n";
    error = 1;
  }
    
  if (error == 1)
  {
    alert ("There were errors with your message : \r\n\r\n" + text_out); return false;
  }

  return true;
}

function limitText(limitField, limitCount, limitNum) 
{
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function addRowToTable()
{
  if (num_recips >= 5)
  {
    alert("Only 5 recipients allowed..");
    return;
  }

  num_recips++;
  document.getElementById('recip_count').value++;
  
  var tbl = document.getElementById('reciever');
  var lastRow = tbl.rows.length;  var iteration = lastRow;  var row = tbl.insertRow(lastRow);
  var cell = row.insertCell(0);  
  cell.colSpan = "2";
  
  var el = document.createElement('span');
  el.innerHTML = "<hr>";
  cell.appendChild(el);  
  
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  
  var cellLeft = row.insertCell(0);
  var el = document.createElement('p');
  el.innerHTML = "<span class='normaltext'>Name :</span>";
  cellLeft.appendChild(el);  
    
  var cellRight = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text'; el.name = 'f_rec_name_'+num_recips;  el.id = 'f_rec_name_'+num_recips; //el.value = "name"+num_recips;
  cellRight.appendChild(el);
  var lastRow = tbl.rows.length;  var iteration = lastRow;  var row = tbl.insertRow(lastRow);
  
  var cellLeft = row.insertCell(0);
  var el = document.createElement('p');
  el.innerHTML = "<span class='normaltext'>Mobile Number :</span>";
  cellLeft.appendChild(el);  
  
  var cellRight = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text';  el.name = 'f_rec_mobnum_'+num_recips;  el.id = 'f_rec_mobnum_'+num_recips;// el.value = "num"+num_recips; 
  cellRight.appendChild(el);
}

function removeRowFromTable()
{
  var tbl = document.getElementById('reciever');
  var lastRow = tbl.rows.length;
  if (lastRow > 3) 
  {
    tbl.deleteRow(lastRow - 1);
    tbl.deleteRow(lastRow - 2);
    tbl.deleteRow(lastRow - 3);    
    num_recips--;
    document.getElementById('recip_count').value--;
  }
}