﻿// JScript File


//Function for Character Counter
/*
Deepak Kothari
July 20, 2008
*/

function countChar(fieldId,cntfieldId,maxLength,optValue) 
    {
        var field = document.getElementById(fieldId);
        var cntfield = document.getElementById(cntfieldId);
        if(field.value.length > maxLength)
        {
            field.value = field.value.substring(0,maxLength);
         }
        cntfield.innerHTML = (maxLength - field.value.length) + ' characters remaining.';
        
    }

//Text Masking Functions begins
/*
Deepak Kothari
July 20, 2008
*/
var sMaskSet = 'aAlLn?'
var sUAscii = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
var bStarting = true;

function doKeyDown(e, textbox, sMask) {
 // trap and cancel keys that are not appropriate
 var iKeyCode = 0;  // collect key code
 if (window.event) iKeyCode = window.event.keyCode;
 else if (e) iKeyCode = e.which;
 if (iKeyCode == 32 || iKeyCode == 39 || iKeyCode == 35
 || iKeyCode == 8 || iKeyCode == 9)
  return true;    // space left end backspace tab
 if (iKeyCode < 47)  // non-printable character
  return false;
}

function doKeyPress(e, textbox, sMask) {

 window.status = '';
 var iKeyCode = 0;  // collect key code
 if (window.event) iKeyCode = window.event.keyCode;
 else if (e) iKeyCode = e.which;

 // check if mask already filled, and not backspace
 var iLength = textbox.value.length;
 if ((iLength == sMask.length) && (iKeyCode != 8))
  return false;

 // get mask character for this position in textbox
 var sMaskChar = sMask.charAt(iLength);

 // see if it's a special character
 if (sMaskSet.indexOf(sMaskChar) > -1) {

  // masked character required
  switch (sMaskChar) {

   case 'a':  // any alphanumeric character
    if ((iKeyCode > 47 && iKeyCode < 58)
    || (iKeyCode > 64 && iKeyCode < 91)
    || (iKeyCode > 96 && iKeyCode < 123))
     return true
    else return false;

   case 'A':  // uppercase alphanumeric character
    if ((iKeyCode > 47 && iKeyCode < 58)
    || (iKeyCode > 64 && iKeyCode < 91))
     return true
    else if (iKeyCode > 96 && iKeyCode < 123) {
     textbox.value += sUAscii.charAt(iKeyCode - 97);
     return false;
    }
    else return false;

   case 'l':  // any letter
    if ((iKeyCode > 64 && iKeyCode < 91)
    || (iKeyCode > 96 && iKeyCode < 123))
     return true
    else
     return false;

   case 'L':  // uppercase letter
    if (iKeyCode > 64 && iKeyCode < 91)
     return true
    else if (iKeyCode > 96 && iKeyCode < 123) {
     textbox.value += sUAscii.charAt(iKeyCode - 97);
     return false;
    }
    else return false;

   case 'n':  // any numeric character
    if (iKeyCode > 47 && iKeyCode < 58)
     return true
    else return false;
   case '?':  // any character
    return true;

   default: return false;
  }
 }
 else
  return true;
}

function doKeyUp(e, textbox, sMask) {
 if (bStarting != true) {
   var iKeyCode = 0;  // collect key code
   if (window.event) iKeyCode = window.event.keyCode;
   else if (e) iKeyCode = e.which;
   if (iKeyCode < 47 && iKeyCode != 32) return;
 }
 // check if next mask characters are literals
 // and add to text box if they are
 while ((textbox.value.length < sMask.length) &&
 (sMaskSet.indexOf(sMask.charAt(textbox.value.length)) == -1)) {
  textbox.value += sMask.charAt(textbox.value.length);
 }
 var sNext;
 if (textbox.value.length == sMask.length)
  sNext = 'Complete'
 else
  switch (sMask.charAt(textbox.value.length)) {
   case 'a':
    sNext = 'Expecting any alphanumeric character (0-9,A-Z,a-z)';
    break;
   case 'A':
    sNext = 'Expecting an uppercase alphanumeric char (0-9,A-Z)';
    break;
   case 'l':
    sNext = 'Expecting any letter (A-Z, a-z)';
    break;
   case 'L':
    sNext = 'Expecting an uppercase letter (A-Z)';
    break;
   case 'n':
    sNext = 'Expecting any numeric character (0-9)';
    break;
   case '?':
    sNext = 'Expecting any character';
    break;
   default: sNext = '';
  }
 window.status = sNext;
}

function doFocus(e, textbox, sMask) {
 bStarting = true;
 doKeyUp(e, textbox, sMask);
 setCursorAtEnd(textbox.id);
 bStarting = false;
}
//Text Masking functions end
/*
Function to set cursor at the end in a text box
Deepak Kothari
Jul 25, 2008
*/
function setCursorAtEnd(sTextboxID) {
    var oTextbox = document.all.item(sTextboxID);
    if (oTextbox .createTextRange) {
        var r = (oTextbox.createTextRange());
        r.moveStart('character', (oTextbox.value.length));
        r.collapse();
        r.select();
    }
}

//Function to show a text box if the value is not listed
/*
Function to set cursor at the end in a text box
Deepak Kothari
Jul 25, 2008
*/
function showTextBox(obj, txtId)
{
var textBox = document.getElementById(txtId);
    if((obj.value.toUpperCase() == 'OTHER') || (obj.value.toUpperCase() == 'YES'))
    {
        
        textBox.style.display = 'block';
        textBox.focus();
    }
    else
    textBox.style.display = 'none';
}

//Function to open demo popup
/*
Function to set cursor at the end in a text box
Deepak Kothari
Jul 25, 2008
*/
function showDemo(arg, wdth, heght)
{
    if (window.showModalDialog)
    {
        returnUrl=window.open('DemoPopup.aspx?id=' + arg,'New Template Demo', 'width='+ wdth +'px,height='+ heght + 'px');
    
        //returnUrl=window.showModalDialog('DemoPopup.aspx?id=' + arg,'','center:on;dialogwidth:' + wdth +'px;dialogheight:'+ heght +'px;scroll:1')
    }
    else
    {
        window.open('DemoPopup.aspx?id=' + arg,'New Template Demo', 'width='+ wdth +'px,height='+ heght + 'px,scroll:1');
    }
}

function isNumberKey(evt,str,obj,maxVal, maxLength) //Function to check for Numeric Values Only
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)&& charCode != 46)
return false;
else
if((charCode == 46)&& (str.indexOf(".")!=-1))
return false;
else
if(typeof(obj) !='undefined')
{
if(parseFloat(obj.value) > maxVal)
{
return false;
}
else
if(typeof(maxLength) !='undefined')
{
if(obj.value.length > maxLength)
return false;
}
}
return true;
}