﻿function attachEvents(obj, evt, func, bubbling)
{
    if(obj.attachEvent)
        obj.attachEvent('on' + evt,func);
    else if(obj.addEventListener)
        obj.addEventListener(evt,func,bubbling);
}

function appendOptionToCombo(comboId, value)
{
    var optionNew = document.createElement('option');
    optionNew.text = value;
    optionNew.value = value;
    var selectElement = document.getElementById(comboId);

    try 
    {
        selectElement.add(optionNew, null); // standards compliant; doesn't work in IE
    }
    catch(ex) {
        selectElement.add(optionNew); // IE only
    }
}

function setDateLimit(birthDateMonthControlId, birthDateDateControlId, birthDateYearControlId)
{
        var birthDateMonth = document.getElementById(birthDateMonthControlId); 
        var birthDateDate = document.getElementById(birthDateDateControlId); 
        var birthDateYear = document.getElementById(birthDateYearControlId); 
        
        var selectedMonth = birthDateMonth.options.selectedIndex;
        var selectedYear = birthDateYear.options[birthDateYear.options.selectedIndex].value;

        var dayValue = new Date(selectedMonth + " " + 1 + ", " + selectedYear);
        var numberOfDays = 32 - new Date(parseInt(selectedYear), selectedMonth, 32).getDate();
        
        birthDateDate.length = 0;
        for(var i=1; i<=numberOfDays; i++) 
        {
            birthDateDate.add(new Option(i, i),  null);
        }
}

function textCounter(field, countfield, maxlimit) {
var countfieldObj = document.getElementById(countfield);

if (field.value.length > maxlimit) // if too long...trim it!
    field.value = field.value.substring(0, maxlimit);

if(countfieldObj)
    countfieldObj.value = field.value.length;
}

function textCounterWithoutDisplay(field, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
    field.value = field.value.substring(0, maxlimit);
}

function Toggle(divControlID, rdoAccessListID, value)
{
    var lnkAccessRight = document.getElementById(divControlID);
    var rdoAccessList = document.getElementById(rdoAccessListID);
    
    if(rdoAccessList && lnkAccessRight)
    {
            if(rdoAccessList.style.visibility == "visible")
            {
                rdoAccessList.style.visibility = "hidden";
                rdoAccessList.style.display = "none";
            }
            else
            {
                rdoAccessList.style.visibility = "visible";
                rdoAccessList.style.display = "";
            }
            if(value)
                lnkAccessRight.innerHTML  = value;
                
            if(lnkAccessRight.style.visibility == "visible")
            {
                lnkAccessRight.style.visibility = "hidden";
                lnkAccessRight.style.display = "none";
            }
            else
            {
                lnkAccessRight.style.visibility = "visible";
                lnkAccessRight.style.display = "";
            }        
    }
}

function HideAccessLink(divControlID)
{
    var rdoAccessList = document.getElementById(rdoAccessListID);
    if(lnkAccessRight)
    {
        {
            lnkAccessRight.style.visibility = "visible";
            lnkAccessRight.style.display = "";
        }        
    }    
}

function TextAreaLimit(textAreaObj, maxValue, event)
{
var field=  textAreaObj;//event != null ? event.srcElement:e.target;
       if(field.maxChars  != null) {  
         if(field.value.length >= parseInt(field.maxChars)) {
           event.returnValue=false; 
           alert("more than " +field.maxChars + " chars");
           return false;
         }
       }

//    if(textAreaObj.value.length == maxValue)
//    return false;
}

function taCount(textname,no) 
{ 
	var taObj=event.srcElement;
	if (taObj.value.length > taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1);
	document.getElementById(textname).value=taObj.value.length;
	if(taObj.value.length==taObj.maxLength*1) alert("You can enter only "+no+" characters");

}


function searchClear(obj){	
	//var search_str = /\S\e\a\r\c\h\./;	
	var search_str = "search|Search|SEARCH|.";
	
	regex = new RegExp(search_str);
	if(regex.test(obj.value)){
		obj.value = "";
	}
}

function getViewportElement()
{
    return document.compatMode == "CSS1Compat" ? document.documentElement : document.body;
}

function getClientHeight() 
{
    var scrollbarWidth = 20;
    if(window.innerHeight || window.innerHeight == 0) {
        return window.innerHeight - scrollbarWidth;
    }
    var ve = getViewportElement();
    if(ve.clientHeight || ve.clientHeight == 0) 
        return ve.clientHeight - scrollbarWidth;
    return null;
}

function getClientWidth() 
{
    var scrollbarWidth = 20;
    if(window.innerWidth || window.innerWidth == 0) {
        return window.innerWidth - scrollbarWidth;
    }
    var ve = getViewportElement();
    if(ve.clientWidth || ve.clientWidth == 0) 
        return ve.clientWidth - scrollbarWidth;
    return null;
}

