//***********************************************************
// Style Names
//***********************************************************
var _hoverRow_css           = 'hover';

//***********************************************************
// End Style Names
//***********************************************************

//***********************************************************
// Functions
//***********************************************************
//---------------------------------------------------------//
//	Create a popup dispaly for showing more information    //
//      into a relative absolute div by button position    //
//---------------------------------------------------------//
function ShowMoreInformation(txt,btnCtrl) {
    //Check parameters passed in is valid
    if ((txt == null) || (btnCtrl == null))
        return false;
    //Create Div for rendering
    var div  = document.createElement('DIV');
    //Set Stlye
    div.className = "InfoPopup";
    //Create inside span
    var lbl = document.createElement('SPAN');
    lbl.className = "InfoText";
    //Set Text
    lbl.innerText = txt;
    div.appendChild(lbl);
    //Set location of div
    div.style.left = btnCtrl.offsetLeft + 'px';
    div.style.top = btnCtrl.offsetTop + 'px';
    //Attach div to parent
    btnCtrl.form.appendChild(div);
}

//---------------------------------------------------------//
//	Enables or Disables an object on the page if exists    //
//---------------------------------------------------------//
function Enable(id){
	var el = document.getElementById(id);
	if(el){
		el.disabled = false;
	}
}
function Disable(id){
	var el = document.getElementById(id);
	if(el){
		el.disabled = true;
	}
}
//----------------------------------------------------------//
//	PreventPostBack: prevents an event from bubbling		//
//				in essence, this prevents a postback		//
//----------------------------------------------------------//
function PreventPostBack(event)
{
    if (event && event.preventDefault) 
    {
        event.preventDefault();
        event.stopPropagation();
    } 
    else 
    {
      window.event.returnValue = false;
      window.event.cancelBubble = true;
    }
}
//----------------------------------------------------------//
//	IsValidAreaCode:										//
//	bool													//
//----------------------------------------------------------//
function IsValidAreaCode(val)
{
	var IsValid = true;
	var moneyRegex=/\d{3}/;
	
	if (!areacodeRegex.test(val) || val.length>3)
	{
		IsValid = false;
	}	
	
	return IsValid;
}
//----------------------------------------------------------//
//	IsValidPhoneRoot:										//
//	bool													//
//----------------------------------------------------------//
function IsValidPhoneRoot(val)
{
	var moneyRegex=/^(?!\d[1]{2}|[5]{3})([2-9]\d{2})([. -]*)\d{4}$/;
	return moneyRegex.test(val);
}
//----------------------------------------------------------//
//	IsValidMoney:											//
//	bool													//
//----------------------------------------------------------//
function IsValidMoney(val)
{
	var moneyRegex=/^\$?[0-9\,]+(\.\d{2})?$/;
	return moneyRegex.test(String(val).replace(/^\s+|\s+$/g, ""));
}
//----------------------------------------------------------//
//	IsValidEmail:											//
//	bool													//
//----------------------------------------------------------//
function IsValidEmail(val)
{
	var emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return emailRegex.test(val);
}
//----------------------------------------------------------//
//	IsValidAlpha:											//
//	bool													//
//----------------------------------------------------------//
function IsValidAlpha(val)
{
	var nameRegex = /^([a-zA-Z])+$/;
	return nameRegex.test(val);
}
//----------------------------------------------------------//
//	IsValidAlphaNumeric										//
//	bool													//
//----------------------------------------------------------//
function IsValidAlphaNumeric(val)
{
	var nameRegex = /^([a-zA-Z0-9])+$/;
	return nameRegex.test(val);
}
//----------------------------------------------------------//
//	CheckNumber:											//
//	bool													//
//----------------------------------------------------------//
function CheckNumber(data) 
{       
	var valid = "0123456789.$,";     // are valid numbers or a "."
	var ok = 1; var checktemp;
	for (var i=0; i < data.length; i++){
		checktemp = "" + data.substring(i, i+1);
		if (valid.indexOf(checktemp) == "-1") return 0; 
	}
	return 1;
}
//----------------------------------------------------------//
//	CheckNumber:											//
//	bool													//
//----------------------------------------------------------//
function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;


	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}
//----------------------------------------------------------//
//	SafeTrim:	    										//
//	bool													//
//----------------------------------------------------------//
function SafeTrim(str)
{  
	while(str.charAt(0) == (" ") )
	{  
		str = str.substring(1);
	}
	while(str.charAt(str.length-1) == " " )
	{  
		str = str.substring(0,str.length-1);
	}
	return str;
}
//***********************************
// Hover Table Functions
//***********************************
function SetHover(ctrl,turnOn,isSelectable) {
    if (ctrl)
    {
        if (turnOn) {
            if (isSelectable)
                ctrl.previousClass = ctrl.className;
            ctrl.className = _hoverRow_css;
        } else {
            //Check if it has been set to hide
            if (ctrl.className == 'Hide')
                return false;
            if ((isSelectable) && (ctrl.previousClass))
                ctrl.className = ctrl.previousClass;
            else
                ctrl.className = '';
        }
    }
    return false;
}
//----------------------------------------------------------//
//	Set Section drop down value:							//
//	bool													//
//----------------------------------------------------------//
function Set_DropDown_Value(ddId,value) {
    var dd = document.getElementById(ddId);
    if ((dd) && (value != null)) {
        for (var i=0; i<dd.options.length; i++) {
            if (dd.options[i].value == value) {
                dd.selectedIndex = i;
                return true;
            }
        }
    }
    return false;
}
//***********************************
// Scroll Table Functions
// This function accepts 4 parameters: DIV.ID, TABLE.ID, ROW.ID and if there is a header 
//  row or not (true or false)
// This function automatically scrolls to a specific table row in a DIV
// If the table has cellpadding or cellspacing then this fucntion will not work correctly
// NOTE: make sure horz scroll bars are not visible, otherwise the last visible row will be covered
//***********************************
function ScrollToRow(div,table,row,hasHeader)
{
	var oDiv = document.getElementById(div);
	var oTable = document.getElementById(table);
	var oRow = document.getElementById(row);

	//** Test if the objects exist, if not ignore command
	if(oDiv && oTable && oRow)
	{
		var r = oRow.rowIndex;
		var h = oDiv.clientHeight;
		var i = 0;
		var n = 0;

		for(i=0;i<r+1;i++)
		{
			n += oTable.rows(i).offsetHeight;
		}

		if(n < h)
		{
			oDiv.scrollTop = 0;
		}
		else
		{
			oDiv.scrollTop = n - ((hasHeader) ? (oRow.offsetHeight * 2) : (oRow.offsetHeight)); //If there is a header, subtract 2 rows otherwise, just one
		}
	}
	else
	{
	    alert("The elements necessary for the 'ScrollToRow' function to perform are missing.");
	}
}

//****************************************************************
//Error Messages
function ErrorMessages() {
    //properties
    this._messages = new Array();
    this._count = 0;
    //methods
    this.add = ErrorMessages.Add;
}

function ErrorMessages.Add(val) {
    this._count++;
    this._messages.push(val);
}
var _error                          = new ErrorMessages();



//------------------------------
// Add To Array - Adds Item to array passed in
// Returns:
//  Array
//------------------------------

function AddToArray(value, _addArray) {
    _addArray[_addArray.length] = value;
    return _addArray;
}

//------------------------------
// Remove From Array - Removes a value from the array passed in
// Returns: 
//  Array
//------------------------------

function RemoveFromArray(value, _remArray) {
    var tArray = new Array();
    for(var i = _remArray.length - 1; i >= 0; i--) {
        if (_remArray[i] != value)
            tArray[tArray.length] = _remArray[i];
    }
    return tArray;
}

function ChangeImg(id, src)
{
    var img = document.getElementById(id);
    if(img && img.tagName=='IMG')
    {
        img.src = src;
    }
}

function Get_RadCalendar_Value(id)
{
    var radControl = $find(id);
    value = radControl.get_selectedDates();
    if (value && value.length && (value.length > 0) && (value[0].length) && (value[0].length == 3))
    {
        value = value[0][1] + "/" + value[0][2] + "/" + value[0][0];
    }
    else
        value = "";
        
    //value = new Date(value);
        
    return value;
}
function Get_RadComboBox_Value(id)
{
    var radControl = $find(id);
    value = radControl.get_value();
    if (value == null)
        value = "";
    return value;
}
//***********************************************************
// End Functions
//***********************************************************