<!--  Author: 	   Maria Geary  -->
<!--  Description: Stores all generic javascript functions needed by an application  -->
<!--  Name:	   Utilities -->

function isNumber(textObj)
{
  var valueOk = true
  var decPointExists = false
  textObj.value = rTrim(textObj.value)
  textObj.value = lTrim(textObj.value)
  
  var textLen = textObj.value.length
  for (var i = 0; i < textLen; i++)
  {
    var textChar = textObj.value.substring(i, i+1)
    if ((textChar < '0') || (textChar > '9'))
    {
      /* Check if a minus sign exists and if so, that only one exists.
         The minus sign must be the first character */
      if (textChar == '-')
      {
        if (textLen == 1 || i > 0)
          valueOk = false
      }
      else
      {
        // Check if a decimal point exists and if so, that only one exists
        if (textChar == '.')
        {
          if (decPointExists == false && textLen > 1)
            decPointExists = true
          else
            valueOk = false
        }
        else //if its any other value besides a number, a minus sign or a decimal point then invalid
          valueOk = false
      }
    }
    if (valueOk == false)
      break
  }
  
  if (valueOk == false)
  {
    if(textObj.value!=textObj.lastvalue)
    {
     alert(textObj.value + ' is not a valid number.')
     textObj.focus();
     textObj.select();
     textObj.lastvalue = textObj.value;
     return false;
     }
  
  }
  else
    return true
}


function isNumberWSpace(textObj)
{
  var valueOk = true
  var decPointExists = false
  textObj.value = rTrim(textObj.value)
  textObj.value = lTrim(textObj.value)
  
  var textLen = textObj.value.length
  for (var i = 0; i < textLen; i++)
  {
    var textChar = textObj.value.substring(i, i+1)
    if ((textChar < '0') || (textChar > '9'))
    {
      /* Check if a minus sign exists and if so, that only one exists.
         The minus sign must be the first character */
      if (textChar == '-')
      {
        if (textLen == 1 || i > 0)
          valueOk = false
      }
      else
      {
        // Check if a decimal point exists and if so, that only one exists
        if (textChar == '.')
        {
          if (decPointExists == false && textLen > 1)
            decPointExists = true
          else
            valueOk = false
        }
        else
          {
           // Check if a space exists and if so, its ok
	        if (textChar == ' '||testChar == '')
	        { valueOk = true }
        else //if its any other value besides a number, a minus sign or a decimal point then invalid
          valueOk = false
      }
     }
    }
    if (valueOk == false)
      break
  }
  
  if (valueOk == false)
  {
    if(textObj.value!=textObj.lastvalue)
    {
     alert(textObj.value + ' is not a valid number.')
     textObj.focus();
     textObj.select();
     textObj.lastvalue = textObj.value;
     return false;
     }
  
  }
  else
    return true
}



function isAlphaNumeric(textObj)
  { 
    var validAlphaNum = true
    //textObj.value = textObj.value.toUpperCase()
    textObj.value = rTrim(textObj.value)
    textObj.value = lTrim(textObj.value)
        
    var textLen   = textObj.value.length
    
    for (var i = 0; i < textLen; i++)
     {
       var c = textObj.value.substring(i, i+1)
      
       if (((c >= 'a') && (c <= 'z'))||
           ((c >= 'A') && (c <= 'Z'))|| 
           ((c >= '0') && (c <= '9')))
        {
         validAlphaNum = true
        } 
       else
         {
          validAlphaNum = false
          }
       
       if (validAlphaNum == false)
         break
     }
     
     if (validAlphaNum == false)
     {
       if(textObj.value!=textObj.lastvalue)
          {
          alert(textObj.value + ' is not a valid alpha numeric value.')
          textObj.lastvalue = textObj.value;
          textObj.focus();
          textObj.select();
          return false;
          }
    }
     else
          return true   
  }
  
function isAlphaNumericWSpace(textObj)
  { 
    var validAlphaNum = true
    textObj.value = rTrim(textObj.value)
    textObj.value = lTrim(textObj.value)
        
    var textLen   = textObj.value.length
    
    for (var i = 0; i < textLen; i++)
     {
       var c = textObj.value.substring(i, i+1)
        
       
       if (((c >= 'a') && (c <= 'z'))||
           ((c >= 'A') && (c <= 'Z'))||   
           ((c >= '0') && (c <= '9'))||
            (c == '') || (c == ' '))
        { validAlphaNum = true } 
       else
         { validAlphaNum = false }
       
       
       if (validAlphaNum == false)
         break
     }
     
     if (validAlphaNum == false)
     {
       if(textObj.value!=textObj.lastvalue)
          {
          alert(textObj.value + ' is not a valid alpha numeric value.')
          textObj.lastvalue = textObj.value;
          textObj.focus();
          textObj.select();
          return false;
          }
    }
     else
          return true   
  }
  
 
  
 function isAlpha(textObj)
  {
    var validAlpha = true
    textObj.value = textObj.value.toUpperCase()
    textObj.value = rTrim(textObj.value)
    textObj.value = lTrim(textObj.value)
    
    var textLen   = textObj.value.length
      
    for (var i = 0; i < textLen; i++)
     {
       var c = textObj.value.substring(i, i+1)
      
       if (((c >= 'a') && (c <= 'z'))||
           ((c >= 'A') && (c <= 'Z'))
           )
        {
         validAlpha = true
        } 
       else
         {
          validAlpha = false
          }
       
       if (validAlpha == false)
         break
     }
     
     if (validAlpha == false)
     {
       if(textObj.value!=textObj.lastvalue)
          {
          alert(textObj.value + ' is not a valid alpha charachter value.')
          textObj.lastvalue = textObj.value;
          textObj.focus();
          textObj.select();
          return false;
          }
    }
     else
          return true   
  } 
  
  
function isYesNo(textObj)
  {
    textObj.value = rTrim(textObj.value)
    textObj.value = lTrim(textObj.value)
  
    if (textObj.value.toUpperCase() == 'Y'||textObj.value.toUpperCase() == 'N'||textObj.value == '')
    {
      textObj.value = textObj.value.toUpperCase();    
      return true;
    }
    else
    if (textObj.value == '1')
      {
        textObj.value = 'Y';    
        return true;
    }
    else
    if (textObj.value == '2')
        {
          textObj.value = 'N';    
          return true;
        }
    
        
    if(textObj.value!=textObj.lastvalue)
    {   alert('Invalid Signature.Please enter Y or N.');
        textObj.focus();
        textObj.select();
        textObj.lastvalue = textObj.value;
        return false;
    }
  }


function isYesNoInvalid(textObj)
  {
    textObj.value = rTrim(textObj.value)
    textObj.value = lTrim(textObj.value)
  
    if (textObj.value.toUpperCase() == 'I'||textObj.value.toUpperCase() == 'Y'||textObj.value.toUpperCase() == 'N'||textObj.value == '')
    {
      textObj.value = textObj.value.toUpperCase();    
      return true;
    }
    else
    if (textObj.value == '1')
      {
        textObj.value = 'Y';    
        return true;
    }
    else
    if (textObj.value == '2')
        {
          textObj.value = 'N';    
          return true;
        }
    else
    if (textObj.value == '3')
        {
          textObj.value = 'I';    
          return true;
        }    
        
    if(textObj.value!=textObj.lastvalue)
    {   alert('Invalid Entry.Please enter Y, I or N.');
        textObj.focus();
        textObj.select();
        textObj.lastvalue = textObj.value;
        return false;
    }
  }

  



function isLtr(textObj)
  {
    textObj.value = replace(textObj.value,' ','');
    textObj.value = textObj.value.toUpperCase();  
    var validLtr  = true
    var textLen   = textObj.value.length
    var validValues = "123456789ABCDEFGHI"
    var validValuesLen = validValues.length
    var validNumLen    = (validValues.length)/2
    
    if (textObj.value == "")
          return true
     
        for (var a=0;a<validValuesLen;a++)
            {
              if (textObj.value==validValues.substring(a,a+1))
                  {
                      if (a<validNumLen)
                        {
                         switch (validValues.substring(a,a+1))
                              {
                               case "1" :
                                 textObj.value = "A";
                                 break;
                               case "2" :
                                 textObj.value = "B";
                                 break;
                               case "3" :
                                 textObj.value = "C";
                                 break;
                               case "4" :
                                 textObj.value = "D";
                                 break;
                               case "5" :
                                 textObj.value = "E";
                                 break;
                               case "6" :
                                 textObj.value = "F";
                                 break;
                               case "7" :
                                 textObj.value = "G";
                                 break;
                               case "8" :
                                 textObj.value = "H";
                                 break;
                               case "9" :
                                 textObj.value = "I";
                                 break;        
                              }
                        }
                 }
             }
    
    
    // check to see if input is alpha
    for (var i = 0; i < textLen; i++)
     {
       var c = textObj.value.substring(i, i+1)
      
       if (((c >= 'a') && (c <= 'z'))||((c >= 'A') && (c <= 'Z')))
        {
         validLtr = true
        } 
       else
         {
          validLtr = false
          }
       
       if (validLtr == false)
         break
     }
     
     if (validLtr == false)
     {
       if(textObj.value!=textObj.lastvalue)
          {
          alert(textObj.value + ' is not a valid letter.')
          textObj.lastvalue = textObj.value;
          textObj.focus();
          textObj.select();
          return false;
          }
    }
     else
          return true   
  }
    
   

function selectDoc(textObj)
   {
   window.status = "Please enter Doctor Number.";
   textObj.select();
   }
   
function selectPharm(textObj)
   {
   window.status = "Please enter Pharmacist Number.";
   textObj.select();
   }   
   

function selectOptician(textObj)
   {
   window.status = "Please enter Optician Number.";
   textObj.select();
   }   
      
 
function selectYearMth(textObj)
       {
 	window.status = "Please Enter Year and Month in Format YYYYMM.";
 	textObj.select();
       }
    
function selectCard(textObj)
  {
   window.status = "Please enter Patient Card Number.";
   textObj.select();
  }
  
function selectLtr(textObj)
    {
     window.status = "Please enter Patient Letter.";
     textObj.select();
    }

function selectForm(textObj)
  {
  window.status = "Please enter Form Number.";
  textObj.select();
  }

function selectDate(textObj)	
  {
   window.status = "Please enter Date in format DDMMYYYY.";
   textObj.select();
  }
  
function selectTime(textObj)
    {
     window.status = "Please enter claim time in format HHMM.";
     textObj.select();
    }  

function selectSig(textObj)
  {
   window.status = "Please enter Y or N for signature.";
   textObj.select();
  }  
  



function isCodeValid(textObj, codeList)
{
 
  textObj.value = replace(textObj.value,' ','')
  var textVal = textObj.value.toUpperCase()
  var startPos = 0
  var endPos = 0
  var commaPos = 0
  var codeOk = false

 if (textObj.value == "") {    // nulls dont count
         return true;
    }

  while (commaPos!= -1)
  {
    commaPos = codeList.indexOf(',',startPos)
    if (commaPos == -1)
      endPos = codeList.length
    else
      endPos = commaPos
    code = codeList.substring(startPos,endPos)
    if (textVal == code)
    {
      codeOk = true
      break
    }
    startPos = commaPos + 1
  }
  if (codeOk == false)
  {
    if (textObj.value!=textObj.lastvalue)
        { alert("Code entered must be one of the following : " +codeList);
          textObj.focus();
          textObj.lastvalue = textObj.value;
          return false;
          }
    else
       { return true;
       }
  }
  else
    textObj.value = textObj.value.toUpperCase()
    return true
}
  
  

function replace(origStr, find, replaceWith)
  {
    var pos = 0;    
    while (1 == 1)
    {
      pos = origStr.indexOf(find)
      if (pos == -1)
        break
        origStr = origStr.substring(0,pos) + replaceWith + origStr.substring(pos+1, origStr.length)
    }
    return origStr
}


function valPPSN(ppsn)
        {
        total=0;
        if (
     	    ppsn.substr(0,7).search("[0-9]{7}") != -1
	&&
	   (
	   ( ppsn.length == 9 && ppsn.substr(7,2).search("[A-Z]W") != -1 )
		||
	   (ppsn.length==8  && ppsn.substr(7,1).search("[A-Z]") != -1 )
	   )
	 )
 {
  ltr = ppsn.substring(6,7);
  for(i=0,j=8;i<7;++i,--j)
  {
   total=total+ (ppsn.substring(i,i+1)*j);
  }
  cdmod = total%23;
  if( cdmod == 0 )
  {
  	cd = "W";
  }
  else
      cd = String.fromCharCode(cdmod+64) ;
  if ( cd == ppsn.substring(7,8) )
	 return true;
 }
 return false;
}

function isValPPSN(textObj)
{ 
 if (textObj.value == "")
          return true
 
 textObj.value = replace(textObj.value,' ','');
 
 if ( isAlphaNumeric(textObj) )
    { var retval   = valPPSN(textObj.value);
 if (! retval )
 {
   if(textObj.value!=textObj.lastvalue)
      {
       alert("Invalid PPS Number.");
       textObj.focus();
       textObj.select();
       textObj.lastvalue = textObj.value;
       return false;
      }
  }
  
  }
 
 return retval;

} 

function rTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;

var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function lTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function



function isRequired(textObj)
  {
    textObj.value = rTrim(textObj.value)
    textObj.value = lTrim(textObj.value)
    var textLen   = textObj.value.length 
    
    
  
    
    if (textObj.value == "" || textLen == 0) 
    {  
      if(textObj.value!=textObj.lastvalue)
      { alert('This field is Mandatory. Please enter a value.');
        textObj.focus();
        textObj.select();
        textObj.lastvalue = textObj.value;
        return false;
      }
      else
        return true;
    
    }

     else
          return true;      
  }
  
//Courtesty of http://www.faqts.com/knowledge_base/view.phtml/aid/32261
//for blocking function keys and other keys across IE, NS, and Mozilla

var msg = 'That functionality is restricted.';
var asciiBack       = 8;
var asciiTab        = 9;
var asciiSHIFT      = 16;
var asciiCTRL       = 17;
var asciiALT        = 18;
var asciiHome       = 36;
var asciiLeftArrow  = 37;
var asciiRightArrow = 39;
var asciiMS         = 92;
var asciiView       = 93;
var asciiF1         = 112;
var asciiF2         = 113;
var asciiF3         = 114;
var asciiF4         = 115;
var asciiF5         = 116;
var asciiF6         = 117;
var asciiF11        = 122;
var asciiF12        = 123;


function onKeyPress(evt)
   { window.status = '';

    //get the event object
    var oEvent = (window.event) ? window.event : evt;

    //hmmm in mozilla this is jacked, so i have to record these seperate
    //what key was pressed
    var nKeyCode =  oEvent.keyCode ? oEvent.keyCode :
                    oEvent.which ? oEvent.which :
                    void 0;

    var bIsFunctionKey = false;

    //hmmm in mozilla the keycode would contain a function key ONLY IF the charcode IS 0
    //else key code and charcode read funny, the charcode for 't'
    //returns 116, which is the same as the ascii for F5
    //SOOO,... to check if a the keycode is truly a function key,
    //ONLY check when the charcode is null OR 0, IE returns null, mozilla returns 0

    if(oEvent.charCode == null || oEvent.charCode == 0)
         { bIsFunctionKey = (nKeyCode >= asciiF2 && nKeyCode <= asciiF12)||
                 (    nKeyCode == asciiALT
                   || nKeyCode == asciiMS
                   || nKeyCode == asciiView
                   || nKeyCode == asciiHome
                   || nKeyCode == asciiBack
                 )
        }

    //convert the key to a character, makes for more readable code
    var sChar = String.fromCharCode(nKeyCode).toUpperCase();

    //get the active tag that has the focus on the page, and its tag type
    var oTarget  = (oEvent.target) ? oEvent.target : oEvent.srcElement;
    var sTag     = oTarget.tagName.toLowerCase();
    var sTagType = oTarget.getAttribute("type");

    var bAltPressed   = (oEvent.altKey) ? oEvent.altKey :oEvent.modifiers & 1 > 0;
    var bShiftPressed = (oEvent.shiftKey) ? oEvent.shiftKey :oEvent.modifiers & 4 > 0;
    var bCtrlPressed  = (oEvent.ctrlKey) ? oEvent.ctrlKey :oEvent.modifiers & 2 > 0;
    //var bMetaPressed = (oEvent.metaKey) ? oEvent.metaKey :oEvent.modifiers & 8 > 0;

    var bRet = true; //assume true as that will be the case most times
   // alert (nKeyCode + ' ' + sChar + ' ' + sTag + ' ' + sTagType+ ' ' + bShiftPressed + ' ' + bCtrlPressed + ' ' + bAltPressed);

    if(sTagType != null)
      { sTagType = sTagType.toLowerCase(); }

    //allow these keys inside a text box
    if  (sTag == "textarea" || (sTag == "input" && (sTagType == "text"|| sTagType == "password")) &&
        ( nKeyCode == asciiBack || nKeyCode == asciiSHIFT || nKeyCode == asciiHome || bShiftPressed ||
          (bCtrlPressed && (nKeyCode == asciiLeftArrow || nKeyCode== asciiRightArrow)))
        )
       { return true; }
    else if(bAltPressed && (nKeyCode == asciiLeftArrow || nKeyCode == asciiRightArrow))
       { // block alt + left or right arrow
         bRet = false; }
    else if(bCtrlPressed && (sChar == 'A' || sChar == 'C' || sChar== 'V' || sChar == 'X'))
       { // ALLOW cut, copy and paste, and SELECT ALL
        bRet = true; }
    else if(bShiftPressed && nKeyCode == asciiTab)
       {//allow shift + tab
        bRet = true; }
    else if(bIsFunctionKey)
       { // Capture and stop these keys
        bRet = false; }
    else if(bCtrlPressed || bShiftPressed || bAltPressed){ //blockALL other sequences, includes CTRL+O, CTRL+P, CTRL+N, etc....
        bRet = false; }

    if(!bRet){
        try{
            oEvent.returnValue = false;
            oEvent.cancelBubble = true;

            if(document.all){ //IE
                oEvent.keyCode = 0;
            }else{ //NS
                oEvent.preventDefault();
                oEvent.stopPropagation();
            }
            window.status = msg;
        }catch(ex){
            alert(ex);
        }
    }
    return bRet;
}






