// spellman_cchd.js containing Javascript moved from opresources.php on 23/1/2009

//  this first bit is CCHD's 'ajax.js', used only in to record a bulletin has been read
// Code largely from Lee Babin "Beginning Ajax with PHP"

var  xmlhttp=false;

// Check if we are using IE
try
 {
   //  if the Javascript version > 5
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 }
 catch (e)
 {
  // if not, then use the older active X object
  try
   {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch (E)
   {
     xmlhttp=false;
   }
 }

// if we are using a non-IE browser, create a javascript instance
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
 {
   xmlhttp=new XMLHttpRequest();
 }

function makerequest(serverpage,objid)
{
 var obj=document.getElementById(objid);
 xmlhttp.open("GET",serverpage);
 xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");  // not necessary
 xmlhttp.onreadystatechange=function()
 {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
   {obj.innerHTML=xmlhttp.responseText;}
 }
 xmlhttp.send(null);
}



function CheckBLResponsibilityCode()
{
 if (undefined===document.getElementById("Code1")) return true;
 if (null===document.getElementById("Code1")) return true;

 var C1=document.getElementById("Code1").value.toUpperCase();
 var C2=document.getElementById("Code2").value.toUpperCase();
 var C3=document.getElementById("Code3").value.toUpperCase();
 var C4=document.getElementById("Code4").value.toUpperCase();
 var C5=document.getElementById("Code5").value.toUpperCase();

 var RC=C1+C2+C3+C4+C5;

 if (RC=='') {alert("You need to enter a Responsibility Code");return false;}

 var RV=true;  // unnecessary at present

 if(RC.match(/^[A-Z0-9]+$/)) {RV=true;}
 else {RV=false;alert("Responsiblity Code must contain only alphanumerics");return false;}




 var ll=RC.length;
 if (ll==16) {return true;}
 else
  {
   var er='';              // not used here yet
   if (C1.length!=3) {er= ' 1 ';}
   if (C2.length!=3) {er+= ' 2 ';}
   if (C3.length!=3) {er+= ' 3 ';}
   if (C4.length!=3) {er+= ' 4 ';}
   if (C5.length!=3) {er+= ' 5 ';}
   alert("Responsibility code must be 16 characters");
   document.getElementById("Code1").focus();
   return false;
  }

}



function DescKPress(field)          // in editjobs Description field
{
  var ll=89;
  // window.history.length=0;   // disable the back button doesn't work
  if (undefined===document.getElementById("DescWarning")) return true;
  if (null===document.getElementById("field")) return true;

  var div=document.getElementById("DescWarning");
  var txt=document.getElementById(field).value;
  var len=txt.length;         // not used
  var lens=len.toString();    // not used
  var substrings = txt.split('\n');  // split by newlines
  var countnl=substrings.length;     // count of real lines
  for (var i in substrings)
   {
     countnl+=Math.floor(substrings[i].length/ll);  // count exra lines
   }
  var countnls=countnl.toString();       // convert to string for display
  if (countnl>12)
  {
   var fnt=countnl;
   if (fnt>19) {fnt=19;}
   var style=" <span style='font-size:"+fnt+"pt; ";
   if (countnl>15) {style+=" color: red; ";}
   style+="'>";
   var t="Description is limited to 15 lines<BR>";
   t+="You have "+style+countnls+"</span>.<BR>";
   if (countnl>15) {t+="Please prune.";}
   div.innerHTML=t;
  }
else
{
  div.innerHTML='';
}
  return true;
}


function fieldfocus()
{

}

function Codecheck(field,num)  // in BL_freefields, project abandoned
  {
    e=document.getElementById(field);
    var v=e.value;
    var l=v.length;
    if (l!=num)
      {
       // alert('Field '+field+' should be '+num+' characters current contents: '+v);
       e.style.backgroundColor='pink';
       // if (e.onFocus) e.onFocus(e);

       setTimeout("e.focus();",400);

       return true;
       }
    else
      {
        e.style.backgroundColor='white';
        f=(v+'').toUpperCase();
        e.value=f;
      }

    return true;
  }






                     /* check(rownnum) is for productlist.php   */

  function check(count) {
   ok=true;
   var eqty=document.getElementById('numbers_'+count);
   var estk=document.getElementById('stock_'+count);
   var sets_in_unit=document.getElementById('sets_in_unit_'+count).value;
   var OrderByTotalQuantity=document.getElementById('OrderByTotalQuantity').value;
   var x;
   var message='';
   var qty=eqty.value;

   var stk1=estk.innerHTML;    // null if no stockquantity
   var stk2=estk.value;        // undefined if there is a stockquantity

   if (stk2==undefined) {var stk=stk1;} else {var stk=stk2;}

   stk=stk.replace(/,/,'');             // remove comma, not necessary in this case
   stk = stk.replace(/<.*?>/g,'');      // remove the red colour from negative quantities
   stk2=stk;
   if (+stk<0) {stk=0;}

   var cell1=document.getElementById('stock_'+count);
   if (OrderByTotalQuantity=='yes') {cell1=document.getElementById('TQS_'+count);}
   var cell2=document.getElementById('numbers_'+count);
   var cell3=document.getElementById('numberscell_'+count);
   var cell4=document.getElementById('maintable');
   var t='';
   var neww;
   var m;
   var bgcolor=cell4.style.backgroundColor;
   tabindex=count;         /* should be OK: cell2.tabindex doesn't work but tabIndex would*/

    if (OrderByTotalQuantity=='yes')
     {                                      // must now round up to next sets_in_unit
      x=+qty/+sets_in_unit;
      //alert('OrderByTotalQuantity x is '+x+' qty is '+qty);
      if (Math.ceil(x)!=Math.floor(x))
       {
         qty=Math.ceil(x)*sets_in_unit;
         message='Quantity rounded up to '+qty;
         cell2.value=+qty;
         newWindow=window.open('','popup','width=400,height=100');
         if(!newWindow) {alert(message);}
         else
         {
           newWindow.document.open();
           m='<html><head><title>'+message+'</title>';
           m+='<'+'script language="JavaScript" >';
           m+=' function clear()';
           m+=' {';
           m+='   timerID=setTimeout("window.close()",1500)';
           m+=' }';
           m+='<'+'/'+'script>';
           m+='</head><body onload="clear();"><h3>'+message+'</h3></body><.html>';
           newWindow.document.write(m);
           newWindow.document.close();
         }
       }
     }


    if (OrderByTotalQuantity=='yes') {stk*=sets_in_unit;}   // this is local to this call

   if ((+qty)>0 && (+qty)>(+stk))
    {
     cell1.style.backgroundColor='yellow';
     cell2.style.backgroundColor='yellow';
     cell3.style.backgroundColor='pink';

     cell2.value=+stk;

     cell2.focus();
      ok=false;
    }
   else
    {
     ok=true;
     cell1.style.backgroundColor=bgcolor;
     cell2.style.backgroundColor='white';
     cell3.style.backgroundColor=bgcolor;

 // section removed from here April 2008

    }


   if ((+qty)<0)                 /* negative numbers !!  */
     {
       cell2.style.backgroundColor='yellow';
       cell3.style.backgroundColor='red';
       cell2.value=0;
       cell2.focus();
       ok=false;
     }

   return(ok);

  }

 function checkfields() {
   okc=true;
   num=0;
   while (document.getElementById('numbers_'+num)!=undefined)
   {
      /* alert('num='+' '+num); */
      j=check(num);
      if (!j) {okc=false;}
      num++;
    }

 return okc;
 }


                      /*  FormSubmit is not seriously used  */

function FormSubmit()
{
   try
   {
      var deliverydate = GetFormFieldValue('orderproduct', 'DeliveryDate', '');
      if (deliverydate.length > 0)
      {
         if (!IsValueOfPattern(deliverydate, /^\d{2}-\d{2}-\d{4}$/))
         {
            alert('Delivery Date is not in the correct format.\n\nMust be specified as DD-MM-YYYY, or use the Date Selection Tool');
            focusField(document.orderproduct.DeliveryDate);
            return false;
         }
      }

      var quantity1 = GetFormFieldValue('orderproduct', 'Quantity1', '');
      if (quantity1.length == 0)
      {
         alert('Quantity has not been specified.\n\nPlease enter the Quantity required.');
         focusField(document.orderproduct.Quantity1);
         return false;
      }
   }
   catch(e)
   {
      alert("System Error !\n\n" + e.message);
      return false;
   }

   // Build Other Fields

   return true;
}




function BLkeydownCode(evt)            // this for BL code only,  dropped January 2009
 {

   //document.getElementById("RBS_GL_Code").value+='x';

   var charCode = (evt.which) ? evt.which : event.keyCode   //  maybe event.keyCode returns full range
   var t,l;
   //alert('charCode is '+charCode);

   if (charCode==9)  return true;             //  superfluous in this case
   if (charCode==13) return true;            // document.form.submit();  sometimes
   if (charCode==8 || charCode==127)
    {
       t=document.getElementById("RBS_GL_Code").value;
       l=t.length - 1;
       document.getElementById("RBS_GL_Code").value=t.substring(0,+l);
       document.getElementById("RBS_GL_Code").select();
      // document.getElementById("RBS_GL_Code").blur();
      // document.getElementById("RBS_GL_Code").focus();
       return false;
    }

   if (charCode>64 && charCode<91)
     {
       document.getElementById("RBS_GL_Code").value+=String.fromCharCode(+charCode);

       if (document.getElementById("RBS_GL_Code").value.length==3) {document.getElementById("RBS_GL_Code").value+=String.fromCharCode(160);}

       if (document.getElementById("RBS_GL_Code").value.length==7) {document.getElementById("RBS_GL_Code").value+=String.fromCharCode(160);}

       if (document.getElementById("RBS_GL_Code").value.length==11) {document.getElementById("RBS_GL_Code").value+=String.fromCharCode(160);}

       if (document.getElementById("RBS_GL_Code").value.length==15) {document.getElementById("RBS_GL_Code").value+=String.fromCharCode(160);}

       document.getElementById("RBS_GL_Code").select();
      // document.getElementById("RBS_GL_Code").blur();
      // document.getElementById("RBS_GL_Code").focus();
       return false;
     }
     else
     {
       document.getElementById("RBS_GL_Code").value+=String.fromCharCode(+charCode);

       if (document.getElementById("RBS_GL_Code").value.length==3) {document.getElementById("RBS_GL_Code").value+=String.fromCharCode(160);}

       if (document.getElementById("RBS_GL_Code").value.length==7) {document.getElementById("RBS_GL_Code").value+=String.fromCharCode(160);}

       if (document.getElementById("RBS_GL_Code").value.length==11) {document.getElementById("RBS_GL_Code").value+=String.fromCharCode(160);}

      if (document.getElementById("RBS_GL_Code").value.length==15) {document.getElementById("RBS_GL_Code").value+=String.fromCharCode(160);}


       document.getElementById("RBS_GL_Code").select();
      // document.getElementById("RBS_GL_Code").blur();
      // document.getElementById("RBS_GL_Code").focus();
       return false;
     }


   return true;
}
//  end of spellman_cchd.js
