// Returns true is a radio button is selected
// from radio button group.
function valRadioButtonGroup(rdoGroup) 
{
	var count = -1;
	
	for (var i=0; i < rdoGroup.length; i++) 
	{
		if (rdoGroup[i].checked)
		{
		  count = i;
		  i = rdoGroup.length;
		}
	}
	if (count > -1) 
	{
	  return true;
	}
	else
	{
	  return false;
	}
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)
{   
  var i;

    // Is s empty?
  if (isEmpty(s)) return true;

  // Search through string's characters one by one
  // until we find a non-whitespace character.
  // When we do, return false; if we don't, return true.

  for (i = 0; i < s.length; i++)
  {   
    // Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
  }
    // All characters are whitespace.
    return true;
}

// whitespace characters
var whitespace = " \t\n\r";


// Check whether string s is empty.
function isEmpty(s)
{   
  return ((s == null) || (s.length == 0))
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

function validateNumber(originalStr)
{
	var str = new String(originalStr);
	str = trim(str);
    
	if (isWhitespace(str)) 
	{	
		return false;
	}

	var i = 0;

	for (i = 0; i < str.length; i++)
	{
		if ((str.charAt(i) < '0' || str.charAt(i) > '9') && (str.charAt(i) != '.')) 
		{			
			return false;
		}
	}

	return true;
}

function setRadioGroupError(rdoGroupName, isValid)
{
    var inputs = document.getElementsByTagName("input");

    for(var i = 0; i < inputs.length; i++)
    {
        if(inputs[i].name == rdoGroupName)
        {
            setError(inputs[i], isValid);
        } 
    } 
}

function setItemHidden(element, isHidden) {
    //Setting the display to inline first is a workaround for some browser issues
    if (isHidden)
    {
        element.style.display = "inline";
        element.style.display = "none";
    } else
    {
        element.style.display = "inline";
        element.style.display = "";
    }
}


function setError(element, isValid)
{	  
  if(!isValid)
  {	    
    element.style.borderTopStyle    = "dashed";
    element.style.borderTopColor    = "red";
    element.style.borderTopWidth    = "2px";
  
    element.style.borderBottomStyle = "dashed";
    element.style.borderBottomColor = "red";
    element.style.borderBottomWidth = "2px";
  
    element.style.borderLeftStyle   = "dashed";
    element.style.borderLeftColor   = "red";
    element.style.borderLeftWidth   = "2px";
  
    element.style.borderRightStyle  = "dashed";
    element.style.borderRightColor  = "red";
    element.style.borderRightWidth  = "2px";
  }
  else
  {	    
    element.style.borderTopStyle    = "";
    element.style.borderTopColor    = "";
    element.style.borderTopWidth    = "";
  
    element.style.borderBottomStyle = "";
    element.style.borderBottomColor = "";
    element.style.borderBottomWidth = "";
  
    element.style.borderLeftStyle   = "";
    element.style.borderLeftColor   = "";
    element.style.borderLeftWidth   = "";
  
    element.style.borderRightStyle  = "";
    element.style.borderRightColor  = "";
    element.style.borderRightWidth  = "";	    
  }
}

//Numeric value handling
function numericInt(evt) {
    return numeric("int", evt);
}

function numericFloat(evt) {
    return numeric("float", evt);
}

function numeric(type, evt) {
    var oKey = (evt.which) ? evt.which : evt.keyCode;
    var obj = evt.srcElement;
    if (type == "int" && (oKey == 190 || oKey == 110)) {   // Do not allow '.'
        evt.returnValue = false;
        return false;
    }

    if ((oKey < 48 && oKey != 32) || ((oKey == 65 || oKey == 67 || oKey == 86 || oKey == 88) && evt.ctrlKey)) {
        return true; // let control chars thru
    }

    if ((oKey > 95 && oKey < 106) || (oKey > 47 && oKey < 58) && !evt.shiftKey) {
        return true; // 0-9 allowed
    }

    if ((oKey == 110 || oKey == 190) && !evt.shiftKey && obj.value.indexOf('.') == -1) {
        return true; // only allow 1 decimal
    }

    if ((oKey == 109 || oKey == 189) && !evt.shiftKey && getCaretPos(obj) == 0) {
        return true; // "-" allowed in first position
    }

    evt.returnValue = false;
    return false;
}
	
function numericPasteInt(evt)
{
	var result = window.clipboardData.getData('Text');
	var dot = result.indexOf(".");
	
	if (isNaN(result) || dot != -1)
	{
		alert("Unable to paste value:  This field only accepts non-formatted integer values.");
		return false;
	}
	
	return true;
}
	
// The following GetCaretPost() function only works in IE
function getCaretPos(element)
{
	var caret = -1;
	
	if (element.createTextRange)  
	{
 	  var range = document.selection.createRange().duplicate();
 	  
 	  if (range.text == element.value)
 	  {
  	    caret = 0;
	  }
	  else
	  {
	    range.moveStart('textedit', -1);
		caret = range.text.length;
	  }
	}
		
	return caret;
}
