// JavaScript Document

function approveBooking(id)
{
  if(confirm("Approve this booking ?"))
  {
    window.location.href = 'processAction.php?action=approve&id='+id;
  }
}

function rejectBooking(id)
{
  if(confirm("Reject this booking ?"))
  {
    window.location.href = 'processAction.php?action=reject&id='+id;
  }
}

function checkExportBooking2()
{
  with(window.document.frmExportBooking)
  {
    for(var i=0; i<sel_fac.length; i++)
    {
      if(sel_fac[i].selected)
      {
        var fac = sel_fac[i].value;
      }
    }
    
    for(var i=0; i<sel_month_1.length; i++)
    {
      if(sel_month_1[i].selected)
      {
        var mth1 = sel_month_1[i].value;
      }
    }
    
    for(var i=0; i<sel_month_2.length; i++)
    {
      if(sel_month_2[i].selected)
      {
        var mth2 = sel_month_2[i].value;
      }
    }
    
    for(var i=0; i<sel_year_1.length; i++)
    {
      if(sel_year_1[i].selected)
      {
        var year1 = sel_year_1[i].value;
      }
    }
    
    for(var i=0; i<sel_year_2.length; i++)
    {
      if(sel_year_2[i].selected)
      {
        var year2 = sel_year_2[i].value;
      }
    }
    
    if(mth1=="" || year1==""){
      dateNotSelected = true;
    }else{
      dateNotSelected = false;
    }
        
    if(fac=='')
    {
      alert("Please select the facility/group.");
    }
    else if(dateNotSelected)
    {
      alert("Please select the date.");
    }
    else
    {
      window.location.href='processAction.php?action=export&fac='+fac+'&mth1='+mth1+'&year1='+year1+'&mth2='+mth2+'&year2='+year2;
    }
    
  }
}

function checkExportBooking1()
{
  with(window.document.frmExportBooking)
  {
    for(var i=0; i<sel_time_frame.length; i++)
    {
      if(sel_time_frame[i].selected)
      {
        var rad_val = sel_time_frame[i].value;
      }
    }
        
    if(rad_val=='')
    {
      alert("Please select the timeframe.");
    }
    else
    {
      window.location.href='processAction.php?action=quickExport&val='+rad_val;
    }
    
  }
}


function archiveContent(id)
{
  if(confirm("Are you sure you want to archive?"))
  {
    window.location.href = 'processAction.php?action=archive&id='+id;
  }
}

function unarchiveContent(id)
{
  if(confirm("Are you sure you want to unarchive?"))
  {
    window.location.href = 'processAction.php?action=unarchive&id='+id;
  }
}

function archiveVote(id)
{
  if(confirm("Are you sure you want to archive?"))
  {
    window.location.href = 'processAction.php?action=archiveVote&id='+id;
  }
}

function checkEventForm()
{
  with(window.document.frmEvent)
  {
    if(txt_title.value=="")
    {
      alert("Please fill in the Title.");
      return false;
    }
    else if(txt_date.value=="")
    {
      alert("Please fill in the date.");
      return false;
    }
    else
    {
      return true;
    }
  }
}



function getFacInfo(id)
{
  OpenWindow=window.open("facInfo.php?id="+id, "newwin", "height=300, width=600, resizable=yes, scrollbars=yes,menubar=no");  
  OpenWindow.document.close()
  self.name="main"
}

function checkBanUserForm()
{
  with(window.document.frmBanUser)
  {
    if(sel_users.value=="")
    {
      alert("Please select the Unit.");
      return false;
    }
    else if(sel_fac.value=="")
    {
      alert("Please select the Facility.");
      return false;
    }
    else if(txtdate_start.value=="")
    {
      alert("Please select Start Date.");
      return false;
    }
    else if(txtdate_end.value=="")
    {
      alert("Please select End Date.");
      return false;
    }
    else if(txtdate_start.value>txtdate_end.value)
    {
      alert("Invalid Ban Period.\r Please check the dates.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function checkVoteForm()
{
  with(window.document.frmVote)
  {
    if(txtTitle.value=="")
    {
      alert("Please fill in the title.");
      return false;
    }
    else if(txtdate_start.value=="")
    {
      alert("Please fill in the start date.");
      return false;
    }
    else if(txtdate_end.value=="")
    {
      alert("Please fill in the end date.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function castVote()
{
  for(i=0; i<document.frmVote.rad_choice.length; i++)
  {
    if(document.frmVote.rad_choice[i].checked==true)
    {
      var val = document.frmVote.rad_choice[i].value;      
    } 
  }
  
  if(val!=undefined)
  {
    if(confirm("Are you sure of this choice?"))
    {
      window.document.frmVote.submit();
    }
  }
  else{
    alert("Please select a choice.");
  }
}


function remove_choice(id)
{
  if(confirm("Are you sure you want to delete this choice?"))
  {
    window.location.href = 'processAction.php?action=removeChoice&id='+id;
  }
}

function checkContentForm()
{
  with(window.document.frmContent)
  {
    if(txt_title.value=="")
    {
      alert("Please fill in the Title.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function deleteEvent(id)
{
  if(confirm("Are you sure you want to delete this event?"))
  {
    window.location.href = 'processAction.php?action=deleteEvent&id='+id;
  }
}

function displayEvent(id)
{
  OpenWindow=window.open("getEvent.php?id="+id, "newwin", "height=300, width=520, resizable=yes, scrollbars=yes,menubar=no");  
  OpenWindow.document.close()
  self.name="main"
}

function addEvent(id)
{
  window.location.href = "index.php?view=addEvent&id="+id;
}

function del_attach(id, type)
{
  if(confirm("Are you sure you want to delete this attachment?"))
  {
    window.location.href = 'processAction.php?action=deleteAttach&id='+id+'&type='+type;
  }
}

function del_image(img)
{
  if(confirm("Are you sure you want to delete this image?"))
  {
    window.location.href = 'processAction.php?action=deleteImg&img='+img;
  }
}

function addContent(id)
{
  window.location.href = "index.php?view=addContent&id="+id;
}

function viewCurrent()
{
  window.location.href = 'index.php';
}

function viewPast()
{
  window.location.href = 'index.php?view=past';
}

function viewPastBookings()
{
  window.location.href = 'index.php?view=past';
}

function checkExportForm()
{
  with(window.document.frmExport)
  {
    if(chk_user_acct.checked==1 || chk_owner_details.checked==1 || chk_user_profile.checked==1)
    {
      return true;
    }
    else
    {
      alert("Please select the details that you want to export out.")
      return false;
    }
  }
}

function setAck(id)
{
  if(confirm("Are your sure you want to acknowledge this cancelation?"))
  {
    window.location.href = 'processAction.php?action=ack&id='+id;
  }
}

function checkExportSuggestionForm()
{
  with(window.document.frmSuggestionExport)
  {
    if(sel_month_1.value=="")
    {
      alert("Please select a month to export from.");
      return false;
    }
    else if(sel_year_1.value=="")
    {
      alert("Please select the year.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function checkExportFaultForm()
{
  myOpt=-1;
  with(window.document.frmFaultExport)
  {
    
    for(i=rad_genre.length-1; i>-1; i--){
      if(rad_genre[i].checked){
        myOpt=i; i=-1;
      }
    }
    
    if(myOpt==-1)
    {
      alert("Please select the genre.");
      return false;
    }
    else if(sel_month_1.value=="")
    {
      alert("Please select a month to export from.");
      return false;
    }
    else if(sel_year_1.value=="")
    {
      alert("Please select the year.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function checkExportAnnForm()
{
  with(window.document.frmAnnExport)
  {
    if(sel_month_1.value=="")
    {
      alert("Please select the starting month to export from");
      return false;
    }
    else if(sel_year_1.value=="")
    {
      alert("Please select the starting year to export from.");
      return false;
    }

  }
}

function exportUserList()
{
  window.location.href = 'processAction.php?action=exportUserList';
}

function chkFrmPwordRetrievel()
{
  with(window.document.frmPwordRetrieval)
  {
    if(txt_username.value=="")
    {
      alert("Please enter your username.");
      return false;
    }
    else if(txt_email.value=="")
    {
      alert("Please enter your email.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function checkVehicleForm()
{
  with(window.document.frmVehicleForm)
  {
    if(txtVehPlateNum.value=="")
    {
      alert("Please fill in the vehicle plate number.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function checkAccessCardForm()
{
  with(window.document.frmAccessCardForm)
  {
    if(txtAccessCardNum.value=="")
    {
      alert("Please fill in the access card number.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function checkModuleForm()
{
  with(window.document.frmModule)
  {
    if(txtModName.value=="")
    {
      alert("Please fill in the Module Name.");
      return false;
    }
    else if(txtModFolder.value=="")
    {
      alert("Please fill in the Folder Name.");
      return false;
    }
    else if(txtModIcon.value=="")
    {
      alert("Please fill in the Icon Filename");
      return false;
    }
    else
    {
      return true;
    }
  }
}











//--------------------------------------------------------------------------------------

function delImg(num, img)
{
  if(confirm("Are you sure you want to remove this image?"))
  {
    window.location.href = 'processAction.php?action=deleteImg&num='+num+'&img=' + img;
  }
}

function delVehicle(id)
{
  if(confirm("Are you sure you want to remove this Vehicle?"))
  {
    window.location.href = 'processAction.php?action=deleteVehicle&id=' + id;
  }
}

function activateAccessCard(id)
{
  if(confirm("Are you sure you want to activate this Access Card?"))
  {
    window.location.href = 'processAction.php?action=activateAccessCard&id=' + id;
  }
}

function deactivateAccessCard(id)
{
  if(confirm("Are you sure you want to deactivate this Access Card?"))
  {
    window.location.href = 'processAction.php?action=deactivateAccessCard&id=' + id;
  }
}


function delAccessCard(id)
{
  if(confirm("Are you sure you want to remove this Access Card?"))
  {
    window.location.href = 'processAction.php?action=deleteAccessCard&id=' + id;
  }
}

function delModule(id)
{
  if(confirm("Are you sure you want to remove this module?"))
  {
    window.location.href = 'processAction.php?action=deleteModule&id=' + id;
  }
}

function setStatus(st, id, type)
{
  var str;
  
  if(type=="suggestion")
  {
    switch(st)
    {
      case 0:
        str = "Approved";
        break;
      case 1:
        str = "Accepted";
        break;
      case 2:
        str = "Rejected";
        break;
      
    }
    /*
    if(st=="y"){
      str = "Accepted";
    }else{
      str = "Rejected";
    }*/
    
    if(confirm( str +" this suggestion?"))
    {
      window.location.href = 'processAction.php?action=setStatus&state='+st+'&id='+id;
    }
  }
  else if(type=="fault")
  {
    if(confirm( "Approved this fault for viewing?"))
    {
      window.location.href = 'processAction.php?action=setStatus&state='+st+'&id='+id;
    }
  }
}

function delPhoto(id)
{
  if(confirm("Are you sure you want to remove this image?"))
  {
    window.location.href = 'processAction.php?action=deletePhoto&id=' + id;
  }
}

function viewArchiveVendor()
{
  window.location.href = 'index.php?view=viewArchive&archive=1';
}

function delete_vendor(id)
{
  if(confirm("Are you sure you want to remove this vendor?"))
  {
    window.location.href = 'processAction.php?action=delete&id=' + id;
  }
}

function delTask(id)
{
  if(confirm("Are you sure you want to remove?"))
  {
    window.location.href = 'processAction.php?action=delete&id=' + id;
  }
}

//check if str contains @ and .
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function addRoster(val)
{
  window.location.href = "index.php?view=add&val="+val;
}

function del_task(id)
{
  if(confirm("Are you sure you want to delete this duty?"))
  {
    window.location.href = 'processAction.php?action=deleteTask&id=' + id;
  }
}

function del_profile(id)
{
  if(confirm("Are you sure you want to delete this profile? \rAll information related to profile will be deleted including bookings of facilites!"))
  {
    window.location.href = 'processAction.php?action=delete_profile&id=' + id;
  }
}

function delProfile(id)
{
  if(confirm("Are you sure you want to delete this profile?"))
  {
    window.location.href = 'processAction.php?action=delete&id=' + id;
  }
}

function delNOK(id)
{
  if(confirm("Are you sure you want to delete this Next of Kin?"))
  {
    window.location.href = 'processAction.php?action=deleteNOK&id=' + id;
  }
}

function delAddress(id)
{
  if(confirm("Are you sure you want to delete this address?"))
  {
    window.location.href = 'processAction.php?action=deleteAddress&id=' + id;
  }
}

function del_email(id)
{
  if(confirm("Are you sure you want to delete this email?"))
  {
    window.location.href = 'processAction.php?action=deleteEmail&id=' + id;
  }
}

function delContact(id)
{
  if(confirm("Are you sure you want to delete this contact?"))
  {
    window.location.href = 'processAction.php?action=deleteContact&id=' + id;
  }
}

function delRMK(id)
{
  if(confirm("Are you sure you want to delete this remark?"))
  {
    window.location.href = 'processAction.php?action=deleteRemark&id=' + id;
  }
}

function cancel_booking(id)
{
  if(confirm("Are you sure you want to cancel this booking?"))
  {
    window.location.href = 'processAction.php?action=delete&id=' + id;
  }
}

function del_glist(id)
{
  if(confirm("Are you sure you want to delete this guest list?"))
  {
    window.location.href = 'processAction.php?action=delete&id=' + id;
  }
}

function del_guard(id)
{
  if(confirm("Are you sure you want to delete this guard?"))
  {
    window.location.href = 'processAction.php?action=delete&id=' + id;
  }
}

function del_grp(id)
{
  if(confirm("Are you sure you want to delete this group?"))
  {
    window.location.href = 'processAction.php?action=delete&id=' + id;
  }
}

function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

function textCounter(field, countfield, maxlimit)
{
  if (field.value.length > maxlimit)
  {
    field.value = field.value.substring(0, maxlimit);
  }
  else
  {
    countfield.value = maxlimit - field.value.length;
  }
}

function setOwner(pid, uid)
{
  if(confirm("Are you sure you want to set this profile as the owner?"))
  {
    window.location.href = 'processAction.php?action=setOwner&pid=' + pid + "&uid=" + uid;
  }
}

function del_user(id)
{
  if(confirm("Are you sure you want to delete this user?"))
  {
    window.location.href = 'processAction.php?action=delete&id=' + id;
  }
}

function del_time_slot(id)
{
  if(confirm("Are you sure you want to delete this time slot?"))
  {
    window.location.href = 'processAction.php?action=del_ts&id=' + id;
  }
}

function lock_thread(id)
{
  if(confirm("Are you sure you want to lock this thread?"))
  {
    window.location.href = 'processAction.php?action=lock_thread&id=' + id;
  }
}

function unlock_thread(id)
{
  if(confirm("Are you sure you want to unlock this thread?"))
  {
    window.location.href = 'processAction.php?action=unlock_thread&id=' + id;
  }
}

function delete_thread(id)
{
  if(confirm("Are you sure you want to delete this thread?"))
  {
    window.location.href = 'processAction.php?action=del_thread&id=' + id;
  }
}

function delete_post(id)
{
  if(confirm("Are you sure you want to delete this post?"))
  {
    window.location.href = 'processAction.php?action=del_post&id=' + id;
  }
}

function delete_announcement(id)
{
  if(confirm("Are you sure you want to delete this news?"))
  {
    window.location.href = 'processAction.php?action=del_announcement&id=' + id;
  }
}

function del_attachment(id)
{
  if(confirm("Delete this attachment?"))
  {
    window.location.href = 'processAction.php?action=del_attach&id=' + id;
  }
}

function addGrp()
{
  targetUrl = 'index.php?view=addGrp';
	window.location.href = targetUrl;
}

function add()
{  
  targetUrl = 'index.php?view=add';
	window.location.href = targetUrl;
}

function checkVendorForm()
{
  with(window.document.frmVendor)
  {
    if(txtName.value=="")
    {
      alert("Please fill in the name.");
      return false;
    }
    else if(txtRole.value=="" && selRole.value=="")
    {
      alert("Please select or fill in the role.");
      return false;
    }
    else
    {
      return true;
    }
  } 
}


function chkAddProfileForm()
{
  with(window.document.frmProfile)
  {
    if(txt_name.value=="")
    {
      alert("Please fill in the name.");
      return false;
    }
    /*
    else if(txt_nric.value=="")
    {
      alert("Please fill in the NRIC.");
      return false;
    }*/
    else if(txt_email.value=="")
    {
      alert("Please fill in the email.");
      return false;
    }
    else if(!isValidEmail(txt_email.value))
    {
      alert("Please fill in a proper email address.");
      return false;
    }
    else if(txt_contact.value=="")
    {
      alert("Please fill in the contact number.");
      return false;
    }
    else
    {
      return true;
    }
  }  
}


function checkProfileForm()
{
  with(window.document.frmProfile)
  {
    if(txtName.value=="")
    {
      alert("Please fill in the Name.");
      return false;
    }
    else if(txtNRIC.value=="")
    {
      alert("Please fill in the NRIC.");
      return false;
    }
    /*
    else if(txtNRIC.value.length<9)
    {
      alert("Please enter a valid NRIC.");
      return false;
    }
    else if(txtNRIC.value.length>9)
    {
      alert("Please enter a valid NRIC.");
      return false;
    }*/
    else
    {
      return true;
    }
  }
}

function checkAddProfileForm()
{
  with(window.document.frmProfile)
  {
    if(txt_name.value=="")
    {
      alert("Please fill in the name.");
      return false;
    }
    else if(txt_nric.value=="")
    {
      alert("Please fill in the NRIC.");
      return false;
    }
    else if(txt_email.value=="")
    {
      alert("Please fill in the email.");
      return false;
    }
    else if(txt_contact.value=="")
    {
      alert("Please fill in the contact number.");
      return false;
    }
    else
    {
      return true;
    }
    
  }
}

//---------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!!
function checkAddBookingForm()
{
  with(window.document.frmBooking)
  {
/*
    for(var i=0; i<rad_fac.length; i++)
    {
      if(rad_fac[i].checked)
      {
        var rad_val = rad_fac[i].value;
      }
    }
*/    
    if(val_uid.value=="")
    {
      alert("Please select the unit.");
      return false;
    }
    else if(val_pid.value=="")
    {
      alert("Please select the profile.");
      return false;
    }
/*
    else if(rad_val==undefined)
    {
      alert("Please select the facility.");
      return false;
    }
*/    
    else if(val_fid=="")
    {
      alert("Please select the facility.");
      return false;
    }
    else if(val_date.value=="")
    {
      alert("Pelase select the date.");
      return false;
    }
    else if(val_tsid.value=="")
    {
      alert("Please select the timeslot.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function checkGuestListForm()
{
  with(window.document.frmGuestList)
  {
    if(txtdate.value=="")
    {
      alert("Please select the date.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function checkAddGuardForm()
{
  with(window.document.frmGuard)
  {
    if(txt_name.value=='')
    {
      alert("Please fill in the name.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function checkBookingDetailForm()
{
  with(window.document.frmBookingDetail)
  {
    if(app.value=="")
    {
      alert("Please set the approval.");
      return false;    
    }
    else
    {
      return true;
    }
  }
}

function checkGuardDetailForm(n)
{
  with(window.document.frmGuardDetail)
  {
    if(txt_name.value=='')
    {
      alert("Please fill in the name.");
      return false;
    }
    else
    {
      
      if(n==1)
      {
        if(txt_full_name.value=="")
        {
          alert("Please fill in the Profile Name.");
          return false;
        }
        else if(txt_nric.value=="")
        {
          alert("Please fill in the NRIC.");
          return false;
        }
        else if(txt_email.value=="")
        {
          alert("Please fill in the email.");
          return false;
        }
        else
        {
          return true;
        }
      }
      else
      {
        return true;
      }
      
    }
  }
}

function checkSettingForm()
{
  with(window.document.frmSettings)
  {
    if(txtRec.value=="")
    {
      alert("Please fill in the Records Per Page.");
      return false;
    }
    else if(txtRec.value<1)
    {
      alert("Please enter a value greater than 0 for Records Per Page.");
      return false;
    }
    else if(txtBookPeriod.value=="")
    {
      alert("Please fill in the Max Booking Period.");
      return false;
    }
    else if(txtBookPeriod.value<1)
    {
      alert("Please enter a value greater than 0 for Max Booking Period.");
      return false;
    }
    else if(txtCancelPeriod.value=="")
    {
      alert("Please fill in the Max Cancelation Leeway.");
      return false;
    }
    else if(txtBookPP.value=="")
    {
      alert("Please fill in the Max Booking Per Person.");
      return false;
    }
    else if(txtBookPP.value<1)
    {
      alert("Please enter a value greater than 0 for Max Booking Per Person.");
      return false;
    }
  }
}

function checkAddUserForm()
{
  with(window.document.frmUserForm)
  {
    if(txtName.value=="")
    {
      alert("Please fill in the name.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function checkAddFacForm()
{
  with(window.document.frmFacility)
  {
    if(txtName.value=="")
    {
      alert("Please fill in the Name.");
      return false;
    }
    else if(val_grp.value=="")
    {
      alert("Please select the Facility Group.")
      return false;
    }
    else
    {
      return true;
    }
  }
}

function checkPostForm()
{
  with(window.document.frmNewPost)
  {
    if(textareaDesc.value=="")
    {
      alert("Please fill in the description.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

function checkAddThreadForm()
{
  with(window.document.frmNewThread)
  {
    if(sel_genre.value==0)
    {
      alert("Please select the genre.");
      return false;
    }
    else if(txtTitle.value=="")
    {
      alert("Please fill in the title.");
      return false;
    }
    else if(txtareaDesc.value=="")
    {
      alert("Please fill in the description.");
      return false;
    }
    else
    {
      return true;
    }
  }
}

//added 29 April 2009
function checkAnnouncementForm()
{
  with(window.document.frmAnnounce)
  { 
    if(txtTitle.value=="")
    {
      alert('Please fill in the title.');
      return false;
    }
    else if(txtarea_desc.value=="")
    {
      alert('Please fill in the description.');
      return false;
    }
    else
    {
      return true;
    }
  }
}

//checks the loginform -- added 18 marc 2009
function checkLogin()
{
  with(window.document.frmLogin)
  { 
    if(txtUserName.value=="")
    {
      alert('Please enter your login name');
      return false;
    }
    else if(password.value==hex_md5(challenge.value+hex_md5(hex_md5(''))))
    {      
      alert('Please enter your password.');
      return false;
    }
    else
    {
      return true;
    }
  }
}


function nl2br (str, is_xhtml) {
    // Converts newlines to HTML line breaks  
    // 
    // version: 903.3016
    // discuss at: http://phpjs.org/functions/nl2br
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli Þór
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brettz9.blogspot.com)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');
    // *     returns 1: 'Kevin<br />\nvan<br />\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '<br />\nOne<br />\nTwo<br />\n<br />\nThree<br />\n'
    var breakTag = '';

    breakTag = '<br />';
    if (typeof is_xhtml != 'undefined' && !is_xhtml) {
        breakTag = '<br>';
    }

    return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

