//Changes background color of the element passed as obj argument
function bgCC(obj, color) { //v1.1 by Project VII
	var b;
	if(!obj.style)
	{
		obj = findObj(obj);
	}
	b=(document.layers)?obj:obj.style;
	b.backgroundColor=color
}

//Changes css class
function cCSS(obj, cssClass) { //v1.1 by Project VII
	if(!obj.className)
	{
		obj = findObj(obj);
	}
	obj.className = cssClass;
}

//Locates an object within a page, based on it's ID
function findObj(n, d) 
{
  var p,i,x;  
  if(!d) d=document; 
  if((p=n.indexOf("?"))>0 && parent.frames.length) 
  {
    d=parent.frames[n.substring(p+1)].document; 
    n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) x = d.all[n]; 
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); 
  return x;
}


//Gets position of an element from the left
function DL_GetElementLeft(eElement)
{
	var nLeftPos = eElement.offsetLeft; // initialize var to store calculations
	var eParElement = eElement.offsetParent; // identify first offset parent element
	while (eParElement != null)
	{ // move up through element hierarchy
	nLeftPos += eParElement.offsetLeft; // appending left offset of each parent
	eParElement = eParElement.offsetParent; // until no more offset parents exist
	}
	return nLeftPos; // return the number calculated
}

//Gets position of an element from the top
function DL_GetElementTop(eElement)
{
	var nTopPos = eElement.offsetTop; // initialize var to store calculations
	var eParElement = eElement.offsetParent; // identify first offset parent element
	while (eParElement != null)
	{ // move up through element hierarchy
	nTopPos += eParElement.offsetTop; // appending top offset of each parent
	eParElement = eParElement.offsetParent; // until no more offset parents exist
	}
	return nTopPos; // return the number calculated
}

//Makes elemenent visible
function MakeVisible(elemID)
{
		var g, b;
		if((g=findObj(elemID))!=null) 
		{
			b=(document.layers)?g:g.style;
			b.visibility="visible";
		}
}

//Makes elemenent invisible
function MakeInvisible(elemID)
{
		var g, b;
		if((g=findObj(elemID))!=null) 
		{
			b=(document.layers)?g:g.style;
			b.visibility="hidden";
		}
}

function SetStyle(elemID, Attribute, Value)
{
		var g, b;
		if((g=findObj(elemID))!=null) 
		{
			b=(document.layers)?g:g.style;
			eval("b." + Attribute + "='" + Value + "'");
		}
}
function GetWinWidth() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myWidth;  
}
function GetWinHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myHeight;  
}


function DisableSubmitBtn(obj)
{
    obj.disabled = true;
    obj.value = 'Processing...';
    obj.className = "FormButtonSubmitted";
    return true;
}

// limit number of words in the textbox
function LimitWordCount(textboxID,labelID,max)
{
    if (document.getElementById(textboxID).value !='')
    {
        var split_words = document.getElementById(textboxID).value.split(' ');
        if (split_words.length > max) 
        {
            var remain = '';
            for (var i=0;i<max;i=i+1)
            {
                if (split_words[i] != null)
                {
                    if (i != max -1)
                        remain = remain + split_words[i] + ' ';
                    else
                        remain = remain + split_words[i];
                }
            }
            document.getElementById(textboxID).value = remain;
            alert('Your input text was too long and has been trimmed! Only ' + max + ' words allowed');
        }
        
        document.getElementById(labelID).innerHTML = document.getElementById(textboxID).value.split(' ').length;
    }
    else
    {
        document.getElementById(labelID).innerHTML = "0";
    }
}

// limit number of charaters in the textbox
function LimitCharCount(labelID, textBoxID, max)
{
    if (document.getElementById(textBoxID).value.length > max)
    {
        document.getElementById(textBoxID).value = document.getElementById(textBoxID).value.substring(0, max);
        alert('Your input text was too long and has been trimmed! Only ' + max + ' characters allowed');
    }
    document.getElementById(labelID).innerHTML = document.getElementById(textBoxID).value.length;
}

function ToggleElementVisible(id)
{
    var el = document.getElementById(id);
    if(el)
    {
        el.style.display = (el.style.display == 'none') ? '' : 'none';
    }
}

function setSelectionRange(input, selectionStart, selectionEnd) {
    if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    }
    else if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
    }
}