﻿// JScript File
var DELETECONFIRM = "Are you sure you want to delete the selected friends";
var ERR_DELETE_SELECT = 'No friend selected for deletion';


function checkAll()
{
    for(i=0;i<document.getElementsByTagName("input").length;i++)
    {
        if(document.getElementsByTagName("input")[i].type == "checkbox")
        {
            document.getElementsByTagName("input")[i].checked = true;                
        }
    }
}

function uncheckAll()
{
    for(i=0;i<document.getElementsByTagName("input").length;i++)
    {
        if(document.getElementsByTagName("input")[i].type == "checkbox")
        {
            document.getElementsByTagName("input")[i].checked = false;                
        }
    }
}

function confirmDelete()
{    
    var count= 0;
    for(i=0;i<document.getElementsByTagName("input").length;i++)
    {
        if(document.getElementsByTagName("input")[i].type == "checkbox")
        {
            if(document.getElementsByTagName("input")[i].checked == true)
            {
                count++;
            }
        }
    }

    if(count == 0)
    {
        alert(ERR_DELETE_SELECT);
        return false;
    }
    else
    {
        var answer = confirm(DELETECONFIRM);
        if (answer)
            return true;
        else
            return false;
    }   

}

var bName = navigator.appName;
        
function taCount(taObj,maxL, Cnt) 
{ 
    objCnt=createObject(Cnt);
    objVal=taObj.value;
    if (objVal.length>maxL) objVal=objVal.substring(0,maxL);
    if (objCnt) {
        if(bName == "Netscape"){	
	        objCnt.textContent=maxL-objVal.length;}
        else{objCnt.innerText=maxL-objVal.length;}
    }
    return true;
}
function createObject(objId) 
{
    if (document.getElementById) return document.getElementById(objId);
    else if (document.layers) return eval("document." + objId);
    else if (document.all) return eval("document.all." + objId);
    else return eval("document." + objId);
}  
function textboxMultilineMaxNumber(obj, maxLength, evt)
{
    var charCode=(evt.which) ? evt.which : event.keyCode;
  
    var max = maxLength - 0;
    var text = obj.value;
    if(text.length < max)
    {
        return true;        
    }
    else
    {
        var ignoreKeys = [8,13,16,17,18];
        for(i=0;i<ignoreKeys.length;i++)
        {
            if(charCode==ignoreKeys[i])
            {                        
                return true;                        
            }
        }
        return false;
    }
} 

function scrapMaxLength(txtId,count)
{
   fieldlimiter.setup({
    thefield: document.getElementById(txtId), 
    maxlength: 300,
    statusids: [count], 
    onkeypress:function(maxlength, curlength){}
    })       
} 
function messageMaxLength(txtId,count)
{
   fieldlimiter.setup({
    thefield: document.getElementById(txtId), 
    maxlength: 1000,
    statusids: [count], 
    onkeypress:function(maxlength, curlength){}
    })       
} 
function smsMaxLength(txtId,count)
{
   
   fieldlimiter.setup({
    thefield: document.getElementById(txtId), 
    maxlength: 140,
    statusids: [count], 
    onkeypress:function(maxlength, curlength){}
    })       
} 
function profileMaxLength(txtId,count)
{
   
   fieldlimiter.setup({
    thefield: document.getElementById(txtId), 
    maxlength: 200,
    statusids: [count], 
    onkeypress:function(maxlength, curlength){}
    })       
} 

function emailMaxLength(txtId,count)
{
   fieldlimiter.setup({
    thefield: document.getElementById(txtId), 
    maxlength: 1000,
    statusids: [count], 
    onkeypress:function(maxlength, curlength){}
    })       
}  


var fieldlimiter={

defaultoutput: "<b>[int]</b> characters left.", //default message that gets output to statusid element

uncheckedkeycodes: /(8)|(13)|(16)|(17)|(18)/, //keycodes that are not checked, even when limit has been reached. See http://www.javascriptkit.com/jsref/eventkeyboardmouse.shtml for avail keycodes

limitinput:function(e, config){
	var e=window.event || e
	var thefield=config.thefield
	var keyunicode=e.charCode || e.keyCode
	if (!this.uncheckedkeycodes.test(keyunicode)){
		if (thefield.value.length>=config.maxlength){
			if (e.preventDefault)
				e.preventDefault()
			return false
		}
	}
},

showlimit:function(config){
	var thefield=config.thefield
	var statusids=config.statusids
	var charsleft=config.maxlength-thefield.value.length	
	
	if (charsleft<0) //if user has exceeded input limit (possible if cut and paste text into field)
		thefield.value=thefield.value.substring(0, config.maxlength) //trim input
	for (var i=0; i<statusids.length; i++){
		var statusdiv=document.getElementById(statusids[i])
		if (statusdiv) //if status DIV defined
			statusdiv.innerHTML=this.defaultoutput.replace("[int]", Math.max(0, charsleft))
	}
	config.onkeypress.call(thefield, config.maxlength, thefield.value.length)
},

cleanup:function(config){
	for (var prop in config){
		config[prop]=null
	}
},


addEvent:function(targetarr, functionref, tasktype){
	if (targetarr.length>0){
		var target=targetarr.shift()
		if (target.addEventListener)
			target.addEventListener(tasktype, functionref, false)
		else if (target.attachEvent)
			target.attachEvent('on'+tasktype, function(){return functionref.call(target, window.event)})
		this.addEvent(targetarr, functionref, tasktype)
	}
},

setup:function(config){
	if (config.thefield){ //if form field exists
		config.onkeypress=config.onkeypress || function(){}
		config.thefield.value=config.thefield.value
		this.showlimit(config)
		this.addEvent([window], function(e){fieldlimiter.showlimit(config)}, "load")
		this.addEvent([window], function(e){fieldlimiter.cleanup(config)}, "unload")		
		this.addEvent([config.thefield], function(e){return fieldlimiter.limitinput(e, config)}, "keypress")
		this.addEvent([config.thefield], function(){fieldlimiter.showlimit(config)}, "keyup")
	}
}

}




 

