﻿// JScript File for TextArea

var dbgkey = 0;

// shiny new cross browser Prototype based versions
function protoKeydown(evt) 
{ 
    var key = evt.which || evt.keyCode; 
    var control = evt.element();
    
    var maxLength = control.readAttribute('maxLength');
    var value = control.value;

    //if ( dbgkey ) 
    //    alert( 'key = ' + key );
        
    //if ( key == 17 ) dbgkey = 1-dbgkey;
    
    if( key != 8 && key != 16 && !(key >= 35 && key <= 40) && key != 46 &&  maxLength && value.length > maxLength-1)
    {
        maxLength = parseInt(maxLength);
        evt.stop(); 

    }
    
    //alert('key press = ' + key );
    
} 

function updateCounters(tb) 
{ 
    if ( tb == null )
        return;
        
    var maxLength = tb.readAttribute('maxLength');
    var counterSpanName = tb.readAttribute('counterSpan');
    var remainSpanName = tb.readAttribute('remainSpan');
    
    if ( maxLength && remainSpanName )
    {
        var remainSpan = $(remainSpanName);
        if ( remainSpan ) 
        {
            var left = maxLength - tb.value.length;
            remainSpan.innerHTML = left;
            remainSpan.writeAttribute( 'class', left < 10 ? 'warning' : '' );
        }
    }
    
    if ( counterSpanName )
    {
        var counterSpan = $(counterSpanName);
        if ( counterSpan ) counterSpan.innerHTML = tb.value.length;
    }
    
}

function protoKeyup(evt) 
{ 
    var tb = evt.element();
    updateCounters(tb);    
}

// crappy old IE versions    
// Keep user from entering more than maxLength characters
function doKeypress(control){
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
     if(  maxLength && value.length > maxLength-1){
          maxLength = parseInt(maxLength);
         return false;

     }
     return true;
}
// Cancel default behavior
function doBeforePaste(control){
    maxLength = control.attributes["maxLength"].value;
     if(maxLength)
     {
          event.returnValue = false;
          return false;
     }
     
     return true;
}
// Cancel default behavior and create a new paste routine
function doPaste(control){
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
     if(maxLength){
          //event.returnValue = false;
          maxLength = parseInt(maxLength);
          var oTR = control.document.selection.createRange();
          var iInsertLength = maxLength - value.length + oTR.text.length;
          var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
          oTR.text = sData;
          
          return false;
     }
     
     return true;
}

function doKeypressUpper(control){
    control.value = control.value.toUpperCase();
}


function doKeypressProper(control){
    control.value = convertToProper(control.value);
}

function convertToProper( v ){
    var returnValue = "";
    var vLength = v.length;

    if(vLength==0){
        return "";
    }
    var isUCaseNext = false;
    
    returnValue += v.charAt(0).toUpperCase();
    
    for(var i=1;i < vLength;i++) {
        if(isUCaseNext == true){
            returnValue += v.charAt(i).toUpperCase();
        }
        else
        {
            returnValue += v.charAt(i).toLowerCase();
        }
    
        var iChar = v.charCodeAt(i);
        if(iChar == 32 || iChar == 45 || iChar == 46){
            isUCaseNext = true;
        }
        else{
            isUCaseNext = false
        }
        if(iChar == 99 || iChar == 67){
            if(v.charCodeAt(i-1)==77 || v.charCodeAt(i-1)==109){
                isUCaseNext = true;
            }
        }
    } //End For

    return returnValue;
} //End Function

function doStripper(e, control, charsToStrip ){
    control.value = stripChars(control.value, charsToStrip );
}

function doKeyStrip(e, control, charsToStrip ){

    if ( stripChars == "" )
        return true;

    v = control.value;
    
    var vLength = v.length;

    if(vLength==0){
        return true;
    }

    var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var keych  = String.fromCharCode(code);
    
    if( charsToStrip.indexOf( keych ) >=0  )
    {
        return false;
    }
    
    return true;
}

function stripChars( v, stripChars ){
    var returnValue = "";
    
    if ( stripChars == "" )
        return v;

    var vLength = v.length;

    if(vLength==0){
        return "";
    }
    
    var ch = "";
    
    for(var i=0;i < vLength;i++) {
        ch = v.charAt(i);
        
        if ( stripChars.indexOf( ch ) < 0 )
        {
            returnValue += ch;
        }
    
    } //End For

    return returnValue;
} //End Function


